2024-04-16 07:26:37 +02:00
|
|
|
import { DateTime } from 'luxon';
|
|
|
|
|
import { AUDIT_LOG_MAX_DURATION } from 'src/constants';
|
|
|
|
|
import { AssetResponseDto, mapAsset } from 'src/dtos/asset-response.dto';
|
|
|
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
|
|
|
|
import { AssetDeltaSyncDto, AssetDeltaSyncResponseDto, AssetFullSyncDto } from 'src/dtos/sync.dto';
|
2024-08-15 06:57:01 -04:00
|
|
|
import { DatabaseAction, EntityType, Permission } from 'src/enum';
|
2024-10-02 10:54:35 -04:00
|
|
|
import { BaseService } from 'src/services/base.service';
|
2024-06-14 18:29:32 -04:00
|
|
|
import { getMyPartnerIds } from 'src/utils/asset.util';
|
2024-04-29 05:24:21 +02:00
|
|
|
import { setIsEqual } from 'src/utils/set';
|
|
|
|
|
|
|
|
|
|
const FULL_SYNC = { needsFullSync: true, deleted: [], upserted: [] };
|
2024-04-16 07:26:37 +02:00
|
|
|
|
2024-10-02 10:54:35 -04:00
|
|
|
export class SyncService extends BaseService {
|
2024-04-29 05:24:21 +02:00
|
|
|
async getFullSync(auth: AuthDto, dto: AssetFullSyncDto): Promise<AssetResponseDto[]> {
|
|
|
|
|
// mobile implementation is faster if this is a single id
|
2024-04-16 07:26:37 +02:00
|
|
|
const userId = dto.userId || auth.user.id;
|
2024-10-10 11:53:53 -04:00
|
|
|
await this.requireAccess({ auth, permission: Permission.TIMELINE_READ, ids: [userId] });
|
2024-04-16 07:26:37 +02:00
|
|
|
const assets = await this.assetRepository.getAllForUserFullSync({
|
|
|
|
|
ownerId: userId,
|
|
|
|
|
updatedUntil: dto.updatedUntil,
|
|
|
|
|
lastId: dto.lastId,
|
|
|
|
|
limit: dto.limit,
|
|
|
|
|
});
|
2024-04-29 05:24:21 +02:00
|
|
|
return assets.map((a) => mapAsset(a, { auth, stripMetadata: false, withStack: true }));
|
2024-04-16 07:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-29 05:24:21 +02:00
|
|
|
async getDeltaSync(auth: AuthDto, dto: AssetDeltaSyncDto): Promise<AssetDeltaSyncResponseDto> {
|
|
|
|
|
// app has not synced in the last 100 days
|
2024-04-16 07:26:37 +02:00
|
|
|
const duration = DateTime.now().diff(DateTime.fromJSDate(dto.updatedAfter));
|
2024-04-29 05:24:21 +02:00
|
|
|
if (duration > AUDIT_LOG_MAX_DURATION) {
|
|
|
|
|
return FULL_SYNC;
|
|
|
|
|
}
|
2024-04-16 07:26:37 +02:00
|
|
|
|
2024-04-29 05:24:21 +02:00
|
|
|
// app does not have the correct partners synced
|
2024-06-14 18:29:32 -04:00
|
|
|
const partnerIds = await getMyPartnerIds({ userId: auth.user.id, repository: this.partnerRepository });
|
|
|
|
|
const userIds = [auth.user.id, ...partnerIds];
|
2024-04-29 05:24:21 +02:00
|
|
|
if (!setIsEqual(new Set(userIds), new Set(dto.userIds))) {
|
|
|
|
|
return FULL_SYNC;
|
2024-04-16 07:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-10 11:53:53 -04:00
|
|
|
await this.requireAccess({ auth, permission: Permission.TIMELINE_READ, ids: dto.userIds });
|
2024-04-29 05:24:21 +02:00
|
|
|
|
2024-04-16 07:26:37 +02:00
|
|
|
const limit = 10_000;
|
|
|
|
|
const upserted = await this.assetRepository.getChangedDeltaSync({ limit, updatedAfter: dto.updatedAfter, userIds });
|
|
|
|
|
|
2024-04-29 05:24:21 +02:00
|
|
|
// too many changes, need to do a full sync
|
2024-04-16 07:26:37 +02:00
|
|
|
if (upserted.length === limit) {
|
2024-04-29 05:24:21 +02:00
|
|
|
return FULL_SYNC;
|
2024-04-16 07:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleted = await this.auditRepository.getAfter(dto.updatedAfter, {
|
2024-04-29 05:24:21 +02:00
|
|
|
userIds,
|
2024-04-16 07:26:37 +02:00
|
|
|
entityType: EntityType.ASSET,
|
|
|
|
|
action: DatabaseAction.DELETE,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
|
needsFullSync: false,
|
2024-04-29 05:24:21 +02:00
|
|
|
upserted: upserted
|
|
|
|
|
// do not return archived assets for partner users
|
|
|
|
|
.filter((a) => a.ownerId === auth.user.id || (a.ownerId !== auth.user.id && !a.isArchived))
|
|
|
|
|
.map((a) =>
|
|
|
|
|
mapAsset(a, {
|
|
|
|
|
auth,
|
|
|
|
|
stripMetadata: false,
|
|
|
|
|
// ignore stacks for non partner users
|
2024-06-14 18:29:32 -04:00
|
|
|
withStack: a.ownerId === auth.user.id,
|
2024-04-29 05:24:21 +02:00
|
|
|
}),
|
|
|
|
|
),
|
2024-04-16 07:26:37 +02:00
|
|
|
deleted,
|
|
|
|
|
};
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|