Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/transport/src/modules/sync/SyncController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<number | undefined> {
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<boolean> {
Expand Down
17 changes: 16 additions & 1 deletion packages/transport/test/modules/sync/SyncController.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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();

Expand Down
Loading