diff --git a/packages/transport/src/modules/sync/SyncController.ts b/packages/transport/src/modules/sync/SyncController.ts index c42c4d672..53e4a1303 100644 --- a/packages/transport/src/modules/sync/SyncController.ts +++ b/packages/transport/src/modules/sync/SyncController.ts @@ -130,6 +130,12 @@ export class SyncController extends TransportController { const identityDatawalletVersion = await this.getIdentityDatawalletVersion(); + if (identityDatawalletVersion === undefined) { + await this.setInititalDatawalletVersion(this.config.supportedDatawalletVersion); + await this.setLastCompletedDatawalletSyncTime(); + return; + } + if (this.config.supportedDatawalletVersion < identityDatawalletVersion) { // This means that the datawallet of the identity was upgraded by another device with a higher version. // It is necessary to update the current device. @@ -317,9 +323,16 @@ export class SyncController extends TransportController { await this.finalizeDatawalletVersionUpgradeSyncRun(version); } - private async getIdentityDatawalletVersion() { - const datawalletInfo = (await this.client.getDatawallet()).value; - return datawalletInfo.version; + private async getIdentityDatawalletVersion(): Promise { + const result = await this.client.getDatawallet(); + + if (result.isError) { + if (result.error.code === "error.platform.recordNotFound") return undefined; + + throw result.error; + } + + return result.value.version; } private async startExternalEventsSyncRun(): Promise { diff --git a/packages/transport/test/modules/sync/SyncController.test.ts b/packages/transport/test/modules/sync/SyncController.test.ts index eaf0744b6..b6a7fee23 100644 --- a/packages/transport/test/modules/sync/SyncController.test.ts +++ b/packages/transport/test/modules/sync/SyncController.test.ts @@ -1,7 +1,7 @@ import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions"; import { sleep } from "@js-soft/ts-utils"; import { CoreDate } from "@nmshd/core-types"; -import { AccountController, Transport } from "../../../src"; +import { AccountController, ClientResult, RequestError, Transport } from "../../../src"; import { FakeSyncClient } from "../../testHelpers/FakeSyncClient"; import { TestUtil } from "../../testHelpers/TestUtil"; @@ -55,6 +55,21 @@ describe("SyncController", function () { } }); + test("syncDatawallet creates a missing Datawallet", async function () { + const syncClient = new FakeSyncClient(); + const account = await TestUtil.createAccount(transport, connection, { syncClient }); + syncClient.finalizeDatawalletVersionUpgradeRequest = undefined; + + jest.spyOn(syncClient, "getDatawallet").mockResolvedValue( + ClientResult.fail(new RequestError("GET", "/api/v2/Datawallet", undefined, "error.platform.recordNotFound", "Datawallet not found.", "", 404)) + ); + + await account.syncDatawallet(); + + expect(syncClient.finalizeDatawalletVersionUpgradeRequest).toBeDefined(); + expect(syncClient.finalizeDatawalletVersionUpgradeRequest!.newDatawalletVersion).toBe(account.config.supportedDatawalletVersion); + }); + test("syncDatawallet upgrades identityDatawalletVersion to supportedDatawalletVersion", async function () { const syncClient = new FakeSyncClient();