diff --git a/src/frontend/src/lib/services/attach-detach/_attach.mission-control.services.ts b/src/frontend/src/lib/services/attach-detach/_attach.mission-control.services.ts deleted file mode 100644 index 239cfb458..000000000 --- a/src/frontend/src/lib/services/attach-detach/_attach.mission-control.services.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { setOrbiter, setSatellite } from '$lib/api/mission-control.api'; -import type { OptionIdentity } from '$lib/types/itentity'; -import type { MissionControlId } from '$lib/types/mission-control'; -import type { Identity } from '@icp-sdk/core/agent'; -import type { Principal } from '@icp-sdk/core/principal'; - -interface AttachWithMissionControlParams { - missionControlId: MissionControlId; - canisterId: Principal; - identity: OptionIdentity; -} - -export const attachWithMissionControl = async ({ - segment, - segmentId, - ...rest -}: { - missionControlId: MissionControlId; - identity: Identity; - segment: 'satellite' | 'orbiter'; - segmentId: Principal; -}) => { - const attachOrbiter = async ({ canisterId, ...rest }: AttachWithMissionControlParams) => { - await setOrbiter({ ...rest, orbiterId: canisterId }); - }; - - const attachSatellite = async ({ - canisterId, - missionControlId, - identity - }: AttachWithMissionControlParams) => { - await setSatellite({ missionControlId, satelliteId: canisterId, identity }); - }; - - const fn = segment === 'orbiter' ? attachOrbiter : attachSatellite; - - await fn({ - ...rest, - canisterId: segmentId - }); -}; diff --git a/src/frontend/src/lib/services/attach-detach/attach.services.ts b/src/frontend/src/lib/services/attach-detach/attach.services.ts index a60770636..420a5fcb1 100644 --- a/src/frontend/src/lib/services/attach-detach/attach.services.ts +++ b/src/frontend/src/lib/services/attach-detach/attach.services.ts @@ -1,5 +1,5 @@ import { setSegment } from '$lib/api/console.api'; -import { attachWithMissionControl } from '$lib/services/attach-detach/_attach.mission-control.services'; +import { setOrbiter, setSatellite } from '$lib/api/mission-control.api'; import { loadSegments } from '$lib/services/segments.services'; import { i18n } from '$lib/stores/app/i18n.store'; import { toasts } from '$lib/stores/app/toasts.store'; @@ -21,6 +21,12 @@ interface AttachParams { segmentId: Principal; } +interface AttachWithMissionControlParams { + missionControlId: MissionControlId; + canisterId: Principal; + identity: OptionIdentity; +} + export const attachSegment = async ({ identity, missionControlId, @@ -129,3 +135,33 @@ const attachWithConsole = async ({ } }); }; + +const attachWithMissionControl = async ({ + segment, + segmentId, + ...rest +}: { + missionControlId: MissionControlId; + identity: Identity; + segment: 'satellite' | 'orbiter'; + segmentId: Principal; +}) => { + const attachOrbiter = async ({ canisterId, ...rest }: AttachWithMissionControlParams) => { + await setOrbiter({ ...rest, orbiterId: canisterId }); + }; + + const attachSatellite = async ({ + canisterId, + missionControlId, + identity + }: AttachWithMissionControlParams) => { + await setSatellite({ missionControlId, satelliteId: canisterId, identity }); + }; + + const fn = segment === 'orbiter' ? attachOrbiter : attachSatellite; + + await fn({ + ...rest, + canisterId: segmentId + }); +}; diff --git a/src/frontend/src/lib/services/attach-detach/out-of-sync.services.ts b/src/frontend/src/lib/services/attach-detach/out-of-sync.services.ts index ad376b065..b83c3a5d0 100644 --- a/src/frontend/src/lib/services/attach-detach/out-of-sync.services.ts +++ b/src/frontend/src/lib/services/attach-detach/out-of-sync.services.ts @@ -4,14 +4,18 @@ import { mctrlOrbiters } from '$lib/derived/mission-control/mission-control-orbi import { mctrlSatellites } from '$lib/derived/mission-control/mission-control-satellites.derived'; import { outOfSyncOrbiters, outOfSyncSatellites } from '$lib/derived/out-of-sync.derived'; import { execute } from '$lib/services/_progress.services'; -import { attachWithMissionControl as attachWithMissionControlService } from '$lib/services/attach-detach/_attach.mission-control.services'; +import { + setMissionControlAsControllerAndAttachOrbiter, + setMissionControlAsControllerAndAttachSatellite +} from '$lib/services/factory/_factory.attach.services'; import { loadSegments } from '$lib/services/segments.services'; import { i18n } from '$lib/stores/app/i18n.store'; import { toasts } from '$lib/stores/app/toasts.store'; import type { OptionIdentity } from '$lib/types/itentity'; -import type { Metadata } from '$lib/types/metadata'; import type { MissionControlId } from '$lib/types/mission-control'; +import type { Orbiter } from '$lib/types/orbiter'; import { type OutOfSyncProgress, OutOfSyncProgressStep } from '$lib/types/progress-out-of-sync'; +import type { Satellite } from '$lib/types/satellite'; import type { Option } from '$lib/types/utils'; import { isNullish, toNullable } from '@dfinity/utils'; import type { Identity } from '@icp-sdk/core/agent'; @@ -121,14 +125,27 @@ const reconcileSatellites = async ({ ) === undefined ); - await attachWithMissionControl({ - missionControlId, - identity, + type SegmentWithoutId = Omit; + + const attachFn: AttachFn = async ({ segment: { segmentId, ...rest } }) => { + await setMissionControlAsControllerAndAttachSatellite({ + missionControlId, + identity, + satellite: { + satellite_id: segmentId, + ...rest + } + }); + }; + + await attachWithMissionControl({ onTextProgress, segmentType: 'satellite', - segments: attachMctrlSatellites.map(({ satellite_id: segmentId }) => ({ + segments: attachMctrlSatellites.map(({ satellite_id: segmentId, ...rest }) => ({ + ...rest, segmentId - })) + })), + attachFn }); await attachWithConsole({ @@ -162,15 +179,27 @@ const reconcileOrbiters = async ({ undefined ); - await attachWithMissionControl({ - missionControlId, - identity, + type SegmentWithoutId = Omit; + + const attachFn: AttachFn = async ({ segment: { segmentId, ...rest } }) => { + await setMissionControlAsControllerAndAttachOrbiter({ + missionControlId, + identity, + orbiter: { + orbiter_id: segmentId, + ...rest + } + }); + }; + + await attachWithMissionControl({ onTextProgress, segmentType: 'satellite', - segments: attachMctrlOrbiters.map(({ orbiter_id: segmentId }) => ({ - segmentId, - segment: 'orbiter' - })) + segments: attachMctrlOrbiters.map(({ orbiter_id: segmentId, ...rest }) => ({ + ...rest, + segmentId + })), + attachFn }); await attachWithConsole({ @@ -184,25 +213,30 @@ const reconcileOrbiters = async ({ }); }; -interface AttachSegment { +type SegmentWithoutId = Omit | Omit; + +type AttachSegment = { segmentId: Principal; - metadata: Metadata; -} +} & T; interface AttachWithMissionControlProgressStats { index: number; total: number; } -const attachWithMissionControl = async ({ - identity, - missionControlId, +type AttachFn = (params: { + segment: AttachSegment; +}) => Promise; + +const attachWithMissionControl = async ({ onTextProgress, segments, - segmentType -}: ReconcileParams & { - segments: Omit[]; + segmentType, + attachFn +}: Pick & { + segments: AttachSegment[]; segmentType: 'satellite' | 'orbiter'; + attachFn: AttachFn; }) => { // We do the check for the lengths here. Not really graceful but, // avoid to duplicate the assertions for both Satellites and Orbiters @@ -229,25 +263,20 @@ const attachWithMissionControl = async ({ onTextProgress(text.replace('{0}', `${progress.index} / ${progress.total}`)); }; - for (const { segmentId } of segments) { + for (const segment of segments) { incrementProgress(); - await attachWithMissionControlService({ - segment: segmentType, - segmentId, - missionControlId, - identity - }); + await attachFn({ segment }); } }; -const attachWithConsole = async ({ +const attachWithConsole = async ({ identity, onTextProgress, segments, segmentType }: { - segments: Omit[]; + segments: Pick, 'segmentId' | 'metadata'>[]; segmentType: 'satellite' | 'orbiter'; } & Pick) => { // We do the check for the lengths here. Not really graceful but, diff --git a/src/frontend/src/lib/services/factory/_factory.attach.services.ts b/src/frontend/src/lib/services/factory/_factory.attach.services.ts index f41217963..cbea1212e 100644 --- a/src/frontend/src/lib/services/factory/_factory.attach.services.ts +++ b/src/frontend/src/lib/services/factory/_factory.attach.services.ts @@ -169,7 +169,7 @@ const attachSatellites = async ({ for (const satellite of satellites) { incrementProgress(); - const result = await attachSatellite({ + const result = await setMissionControlAsControllerAndAttachSatellite({ satellite, missionControlId, identity @@ -201,7 +201,7 @@ const attachOrbiters = async ({ for (const orbiter of orbiters) { incrementProgress(); - const result = await attachOrbiter({ + const result = await setMissionControlAsControllerAndAttachOrbiter({ orbiter, missionControlId, identity @@ -217,7 +217,7 @@ const attachOrbiters = async ({ return canisterIds; }; -const attachSatellite = async ({ +export const setMissionControlAsControllerAndAttachSatellite = async ({ satellite, missionControlId, identity @@ -255,7 +255,7 @@ const attachSatellite = async ({ }); }; -const attachOrbiter = async ({ +export const setMissionControlAsControllerAndAttachOrbiter = async ({ orbiter, missionControlId, identity