From e70feb3d27aff2ef0dd9287c9b2e2b489822d24a Mon Sep 17 00:00:00 2001 From: Michal Szczepanski Date: Wed, 18 Oct 2023 03:38:06 +0200 Subject: [PATCH] fix: sync progress add user sub --- src/common/model/obj/obj.dto.ts | 1 - src/common/model/sync.model.ts | 1 + .../sync/progress/sync-get-progress.command.ts | 15 +++++++++++---- .../sync/progress/sync-reset-progress.command.ts | 16 ++++++++++++---- .../command/sync/sync-month.command.ts | 4 +++- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/common/model/obj/obj.dto.ts b/src/common/model/obj/obj.dto.ts index 75d0b65..5d02d4f 100644 --- a/src/common/model/obj/obj.dto.ts +++ b/src/common/model/obj/obj.dto.ts @@ -40,7 +40,6 @@ export enum ObjTypeDto { PageSnapshot = 'PAGE_SNAPSHOT', PageNote = 'PAGE_NOTE', PageAlter = 'PAGE_ALTER', - PageTask = 'PAGE_TASK', Note = 'NOTE', Pdf = 'PDF', Removed = 'REMOVED' diff --git a/src/common/model/sync.model.ts b/src/common/model/sync.model.ts index 562029d..28d4f0b 100644 --- a/src/common/model/sync.model.ts +++ b/src/common/model/sync.model.ts @@ -18,6 +18,7 @@ export interface SyncProgress { timestamp: number; id: number; serverId: number; + sub: string; mode: SyncMode; } diff --git a/src/service-worker/command/sync/progress/sync-get-progress.command.ts b/src/service-worker/command/sync/progress/sync-get-progress.command.ts index 99fc6f6..d09e70a 100644 --- a/src/service-worker/command/sync/progress/sync-get-progress.command.ts +++ b/src/service-worker/command/sync/progress/sync-get-progress.command.ts @@ -20,14 +20,21 @@ import { ObjDto } from '../../../../common/model/obj/obj.dto'; import { ObjGetCommand } from '../../../../common/command/obj/obj-get.command'; import { ObjectStoreKeys } from '../../../../common/keys/object.store.keys'; import { SyncMode, SyncProgress } from '../../../../common/model/sync.model'; +import { TokenStorageGetCommand } from '../../../../common/command/server/token/token-storage-get.command'; +import { TokenDecodeCommand } from '../../../../common/command/server/token/token-decode.command'; -export class SyncGetProgressCommand implements ICommand> { - async execute(): Promise { +export class SyncGetProgressCommand implements ICommand> { + async execute(): Promise { const sync = await BrowserStorage.get(ObjectStoreKeys.SYNC_PROGRESS); if (sync) return sync; + + const token = await new TokenStorageGetCommand().execute(); + if (!token) return; + const accessToken = new TokenDecodeCommand(token?.access_token).execute(); + const obj = await SyncGetProgressCommand.getFirstObject(); - if (!obj) return { timestamp: -1, id: -1, serverId: -1, mode: SyncMode.OFF }; - return { timestamp: obj.createdAt, id: obj.id, serverId: -1, mode: SyncMode.OFF }; + if (!obj) return { timestamp: -1, id: -1, serverId: -1, mode: SyncMode.OFF, sub: accessToken.sub }; + return { timestamp: obj.createdAt, id: obj.id, serverId: -1, mode: SyncMode.OFF, sub: accessToken.sub }; } static async getFirstObject(): Promise { diff --git a/src/service-worker/command/sync/progress/sync-reset-progress.command.ts b/src/service-worker/command/sync/progress/sync-reset-progress.command.ts index 9a329c0..1a23924 100644 --- a/src/service-worker/command/sync/progress/sync-reset-progress.command.ts +++ b/src/service-worker/command/sync/progress/sync-reset-progress.command.ts @@ -22,7 +22,10 @@ import { ObjDto } from '../../../../common/model/obj/obj.dto'; import { fnDateKeyFormat } from '../../../../common/fn/fn-date-format'; import { ObjDateIndex } from '../../../../common/command/obj/index/obj-update-index-add.command'; import { fnConsoleLog } from '../../../../common/fn/fn-console'; -import { SyncMode, SyncProgress } from '../../../../common/model/sync.model'; +import { SyncMode } from '../../../../common/model/sync.model'; +import { SyncSetProgressCommand } from './sync-set-progress.command'; +import { TokenStorageGetCommand } from '../../../../common/command/server/token/token-storage-get.command'; +import { TokenDecodeCommand } from '../../../../common/command/server/token/token-decode.command'; export class SyncResetProgressCommand implements ICommand> { constructor(private refreshUpdateList = false) {} @@ -30,12 +33,17 @@ export class SyncResetProgressCommand implements ICommand> { const obj = await SyncGetProgressCommand.getFirstObject(); const timestamp = obj?.createdAt || -1; const id = obj?.id || -1; - await BrowserStorage.set(ObjectStoreKeys.SYNC_PROGRESS, { + + const token = await new TokenStorageGetCommand().execute(); + if (!token) return; + const accessToken = new TokenDecodeCommand(token?.access_token).execute(); + await new SyncSetProgressCommand({ timestamp, id, serverId: -1, - mode: SyncMode.OFF - }); + mode: SyncMode.OFF, + sub: accessToken.sub + }).execute(); // await this.resetObjects(); } diff --git a/src/service-worker/command/sync/sync-month.command.ts b/src/service-worker/command/sync/sync-month.command.ts index 8fcf0fd..6527d06 100644 --- a/src/service-worker/command/sync/sync-month.command.ts +++ b/src/service-worker/command/sync/sync-month.command.ts @@ -104,7 +104,9 @@ export class SyncMonthCommand implements ICommand> { await new SyncSetProgressCommand({ id, timestamp, - serverId: this.progress.serverId + sub: this.progress.sub, + serverId: this.progress.serverId, + mode: this.progress.mode }).execute(); } }