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

View File

@ -24,14 +24,20 @@ import { TokenStorageGetCommand } from '../../../../common/command/server/token/
import { TokenDecodeCommand } from '../../../../common/command/server/token/token-decode.command';
export class SyncGetProgressCommand implements ICommand<Promise<SyncProgress | undefined>> {
constructor(private sub: string) {}
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();
if (!token) return;
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();
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 };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -42,6 +42,7 @@ import { fnConsoleLog } from '../common/fn/fn-console';
import { SyncManualOutgoingCommand } from './command/sync/manual/sync-manual-outgoing.command';
import { SyncServerIncomingCommand } from './command/sync/sync-server-incoming.command';
import { SyncGetProgressCommand } from './command/sync/progress/sync-get-progress.command';
import { SyncTxHelper } from './command/sync/sync-tx.helper';
const handleMessage = async (
msg: BusMessage<any>,
@ -109,7 +110,9 @@ const handleMessage = async (
break;
}
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();
break;
}