fix: sync add sub to progress and track login / logout to different account

This commit is contained in:
Michal Szczepanski 2023-10-18 03:51:43 +02:00
parent e70feb3d27
commit 1d756ff462
7 changed files with 36 additions and 23 deletions

@ -24,14 +24,20 @@ import { TokenStorageGetCommand } from '../../../../common/command/server/token/
import { TokenDecodeCommand } from '../../../../common/command/server/token/token-decode.command'; import { TokenDecodeCommand } from '../../../../common/command/server/token/token-decode.command';
export class SyncGetProgressCommand implements ICommand<Promise<SyncProgress | undefined>> { export class SyncGetProgressCommand implements ICommand<Promise<SyncProgress | undefined>> {
constructor(private sub: string) {}
async execute(): Promise<SyncProgress | undefined> { async execute(): Promise<SyncProgress | undefined> {
const sync = await BrowserStorage.get<SyncProgress | undefined>(ObjectStoreKeys.SYNC_PROGRESS);
if (sync) return sync;
const token = await new TokenStorageGetCommand().execute(); const token = await new TokenStorageGetCommand().execute();
if (!token) return; if (!token) return;
const accessToken = new TokenDecodeCommand(token?.access_token).execute(); const accessToken = new TokenDecodeCommand(token?.access_token).execute();
// check sub so we can ge
if (this.sub !== accessToken.sub) {
return { timestamp: -1, id: -1, serverId: -1, mode: SyncMode.RESET, sub: accessToken.sub };
}
const sync = await BrowserStorage.get<SyncProgress | undefined>(ObjectStoreKeys.SYNC_PROGRESS);
if (sync) return sync;
const obj = await SyncGetProgressCommand.getFirstObject(); const obj = await SyncGetProgressCommand.getFirstObject();
if (!obj) return { timestamp: -1, id: -1, serverId: -1, mode: SyncMode.OFF, sub: accessToken.sub }; 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 }; return { timestamp: obj.createdAt, id: obj.id, serverId: -1, mode: SyncMode.OFF, sub: accessToken.sub };

@ -26,10 +26,13 @@ import { SyncMode } from '../../../../common/model/sync.model';
import { SyncSetProgressCommand } from './sync-set-progress.command'; import { SyncSetProgressCommand } from './sync-set-progress.command';
import { TokenStorageGetCommand } from '../../../../common/command/server/token/token-storage-get.command'; import { TokenStorageGetCommand } from '../../../../common/command/server/token/token-storage-get.command';
import { TokenDecodeCommand } from '../../../../common/command/server/token/token-decode.command'; import { TokenDecodeCommand } from '../../../../common/command/server/token/token-decode.command';
import { SwSyncStore } from '../../../sw-sync.store';
export class SyncResetProgressCommand implements ICommand<Promise<void>> { export class SyncResetProgressCommand implements ICommand<Promise<void>> {
constructor(private refreshUpdateList = false) {} constructor(private refreshUpdateList = false) {}
async execute(): Promise<void> { async execute(): Promise<void> {
if (SwSyncStore.isInSync) return;
SwSyncStore.isInSync = true;
const obj = await SyncGetProgressCommand.getFirstObject(); const obj = await SyncGetProgressCommand.getFirstObject();
const timestamp = obj?.createdAt || -1; const timestamp = obj?.createdAt || -1;
const id = obj?.id || -1; const id = obj?.id || -1;
@ -44,7 +47,8 @@ export class SyncResetProgressCommand implements ICommand<Promise<void>> {
mode: SyncMode.OFF, mode: SyncMode.OFF,
sub: accessToken.sub sub: accessToken.sub
}).execute(); }).execute();
// await this.resetObjects(); await this.resetObjects();
SwSyncStore.isInSync = false;
} }
async resetObjects(): Promise<void> { async resetObjects(): Promise<void> {

@ -24,8 +24,9 @@ import { SwSyncStore } from '../../sw-sync.store';
import { ApiAuthUrlCommand } from '../api/api-auth-url.command'; import { ApiAuthUrlCommand } from '../api/api-auth-url.command';
export class SyncServerIncomingCommand implements ICommand<Promise<void>> { export class SyncServerIncomingCommand implements ICommand<Promise<void>> {
constructor(private progress: SyncProgress) {} constructor(private progress?: SyncProgress) {}
async execute(): Promise<void> { async execute(): Promise<void> {
if (!this.progress) return;
if (SwSyncStore.isInSync) return; if (SwSyncStore.isInSync) return;
SwSyncStore.isInSync = true; SwSyncStore.isInSync = true;
try { try {

@ -23,9 +23,10 @@ import { SyncProgress } from '../../../common/model/sync.model';
import { SwSyncStore } from '../../sw-sync.store'; import { SwSyncStore } from '../../sw-sync.store';
export class SyncServerOutgoingCommand implements ICommand<Promise<void>> { export class SyncServerOutgoingCommand implements ICommand<Promise<void>> {
constructor(private progress: SyncProgress) {} constructor(private progress?: SyncProgress) {}
async execute(): Promise<void> { async execute(): Promise<void> {
if (!this.progress) return;
if (SwSyncStore.isInSync) return; if (SwSyncStore.isInSync) return;
SwSyncStore.isInSync = true; SwSyncStore.isInSync = true;
try { try {

@ -27,10 +27,12 @@ import { SyncServerOutgoingCommand } from './sync-server-outgoing.command';
export class SyncServerCommand implements ICommand<Promise<void>> { export class SyncServerCommand implements ICommand<Promise<void>> {
async execute(): Promise<void> { async execute(): Promise<void> {
if (SwSyncStore.isInSync) return; if (SwSyncStore.isInSync) return;
if (!(await SyncTxHelper.shouldSync())) return; const sub = await SyncTxHelper.syncSub();
if (!sub) return;
try { try {
const a = Date.now(); const a = Date.now();
const progress = await new SyncGetProgressCommand().execute(); const progress = await new SyncGetProgressCommand(sub).execute();
if (!progress) return;
switch (progress.mode) { switch (progress.mode) {
case SyncMode.OUTGOING_INCOMING: case SyncMode.OUTGOING_INCOMING:
await new SyncServerOutgoingCommand(progress).execute(); await new SyncServerOutgoingCommand(progress).execute();

@ -60,26 +60,22 @@ export class SyncTxHelper {
await BrowserStorage.remove(ObjectStoreKeys.SYNC_TX); await BrowserStorage.remove(ObjectStoreKeys.SYNC_TX);
} }
static async shouldSync(): Promise<boolean> { static async syncSub(): Promise<string | undefined> {
const interval = (await BrowserStorage.get<number | undefined>(ObjectStoreKeys.SYNC_INTERVAL)) || 0; const interval = (await BrowserStorage.get<number | undefined>(ObjectStoreKeys.SYNC_INTERVAL)) || 0;
fnConsoleLog('SyncServerCommand->shouldSync', Date.now() - interval); fnConsoleLog('SyncServerCommand->shouldSync', Date.now() - interval);
if (Date.now() - interval > SYNC_DELAY) { if (Date.now() - interval < SYNC_DELAY) return;
await BrowserStorage.set<number>(ObjectStoreKeys.SYNC_INTERVAL, Date.now()); await BrowserStorage.set<number>(ObjectStoreKeys.SYNC_INTERVAL, Date.now());
const isPremiumUser = await this.isPremiumUser(); const sub = await this.userSub();
fnConsoleLog('SyncServerCommand->isPremiumUser', isPremiumUser); fnConsoleLog('SyncServerCommand->isPremiumUser', !!sub);
return isPremiumUser; return sub;
}
return false;
} }
private static async isPremiumUser(): Promise<boolean> { private static async userSub(): Promise<string | undefined> {
const token = await new TokenStorageGetCommand().execute(); const token = await new TokenStorageGetCommand().execute();
if (token) { if (!token) return;
const accessToken = new TokenDecodeCommand(token?.access_token).execute(); const accessToken = new TokenDecodeCommand(token?.access_token).execute();
return accessToken.data.role.includes(3); if (accessToken.data.role.includes(3)) return accessToken.sub;
}
return false;
} }
static async getList(key: string): Promise<ObjDateIndex[]> { static async getList(key: string): Promise<ObjDateIndex[]> {

@ -42,6 +42,7 @@ import { fnConsoleLog } from '../common/fn/fn-console';
import { SyncManualOutgoingCommand } from './command/sync/manual/sync-manual-outgoing.command'; import { SyncManualOutgoingCommand } from './command/sync/manual/sync-manual-outgoing.command';
import { SyncServerIncomingCommand } from './command/sync/sync-server-incoming.command'; import { SyncServerIncomingCommand } from './command/sync/sync-server-incoming.command';
import { SyncGetProgressCommand } from './command/sync/progress/sync-get-progress.command'; import { SyncGetProgressCommand } from './command/sync/progress/sync-get-progress.command';
import { SyncTxHelper } from './command/sync/sync-tx.helper';
const handleMessage = async ( const handleMessage = async (
msg: BusMessage<any>, msg: BusMessage<any>,
@ -109,7 +110,9 @@ const handleMessage = async (
break; break;
} }
case BusMessageType.OPTIONS_SYNC_INCOMING_CHANGES: { case BusMessageType.OPTIONS_SYNC_INCOMING_CHANGES: {
const progress = await new SyncGetProgressCommand().execute(); const sub = await SyncTxHelper.syncSub();
if (!sub) break;
const progress = await new SyncGetProgressCommand(sub).execute();
await new SyncServerIncomingCommand(progress).execute(); await new SyncServerIncomingCommand(progress).execute();
break; break;
} }