Montify: mark sync running before responding; no 400 when already running

This commit is contained in:
2026-10-10 16:18:25 +03:30
parent e82bd2b2df
commit f27b4c264a
@@ -156,10 +156,11 @@ export class MontifySyncService {
): Promise<MontifyConnectionDto> { ): Promise<MontifyConnectionDto> {
const row = await this.connections.getOrThrow(organizationId, connectionId); const row = await this.connections.getOrThrow(organizationId, connectionId);
if (!row.enabled) throw new BadRequestException('این اتصال غیرفعال است'); if (!row.enabled) throw new BadRequestException('این اتصال غیرفعال است');
if (this.running.has(connectionId)) { if (!this.running.has(connectionId)) {
throw new BadRequestException('همگام‌سازی این فروشگاه در حال اجراست'); await new Promise<void>((started) => {
void this.runSync(row, mode, started);
});
} }
void this.runSync(row, mode);
return this.connections.getDto(organizationId, connectionId); return this.connections.getDto(organizationId, connectionId);
} }
@@ -175,8 +176,15 @@ export class MontifySyncService {
} }
} }
private async runSync(row: MontifyConnection, mode: 'incremental' | 'full'): Promise<void> { private async runSync(
if (this.running.has(row.id)) return; row: MontifyConnection,
mode: 'incremental' | 'full',
onStarted?: () => void,
): Promise<void> {
if (this.running.has(row.id)) {
onStarted?.();
return;
}
this.running.add(row.id); this.running.add(row.id);
const startedAt = new Date(); const startedAt = new Date();
const result: MontifySyncResultDto = { const result: MontifySyncResultDto = {
@@ -196,14 +204,22 @@ export class MontifySyncService {
data: { lastSyncResult: result as unknown as Prisma.InputJsonValue }, data: { lastSyncResult: result as unknown as Prisma.InputJsonValue },
}); });
await this.prisma.montifyConnection.update({ try {
where: { id: row.id }, await this.prisma.montifyConnection.update({
data: { where: { id: row.id },
syncStatus: 'running', data: {
syncStartedAt: startedAt, syncStatus: 'running',
lastSyncResult: result as unknown as Prisma.InputJsonValue, syncStartedAt: startedAt,
}, lastSyncResult: result as unknown as Prisma.InputJsonValue,
}); },
});
} catch (e) {
this.running.delete(row.id);
this.logger.warn(`Montify sync ${row.id} start: ${e instanceof Error ? e.message : e}`);
return;
} finally {
onStarted?.();
}
let cursor = row.customersCursor; let cursor = row.customersCursor;
let fatal: string | null = null; let fatal: string | null = null;