Spaces:
Sleeping
Sleeping
Download src/modules/shopify/application/useCases/sync/TriggerSyncUseCase.ts from fouadkhalid123/asd: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/modules/shopify/application/useCases/sync/TriggerSyncUseCase.ts
- Command line
-
hf download hf://spaces/fouadkhalid123/asd/src/modules/shopify/application/useCases/sync/TriggerSyncUseCase.ts
-
curl -L -o TriggerSyncUseCase.ts https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/modules/shopify/application/useCases/sync/TriggerSyncUseCase.ts
2.72 kB
| import { BaseService } from "../../../../../libs/shared/application/BaseService"; | |
| import { TenantContext } from "../../../../../libs/shared/domain/valueObjects/TenantContext"; | |
| import { ISyncJobRepository } from "../../../domain/repositories/ISyncJobRepository"; | |
| import { IEventPublisher } from "../../ports/IEventPublisher"; | |
| import { SyncJobCreatedEvent } from "../../../domain/events/SyncJobCreatedEvent"; | |
| export interface TriggerSyncInput { | |
| tenantId: string; | |
| action: "full" | "retry_failed"; | |
| } | |
| export class TriggerSyncUseCase extends BaseService { | |
| constructor( | |
| tenantContext: TenantContext, | |
| private readonly repository: ISyncJobRepository, | |
| private readonly eventPublisher: IEventPublisher | |
| ) { | |
| super(tenantContext); | |
| } | |
| async execute(input: TriggerSyncInput) { | |
| // ββ 2. Check for already active job (dedup) ββββββββββββββββββββββββ | |
| const activeJob = await this.repository.findActiveSyncJob(input.tenantId); | |
| if (activeJob) { | |
| return { | |
| jobId: activeJob.id, | |
| status: activeJob.status.getValue(), | |
| reused: true, | |
| }; | |
| } | |
| // ββ 3. Handle retry_failed: re-enqueue latest failed job's type ββββ | |
| if (input.action === "retry_failed") { | |
| const failedJob = await this.repository.findLatestFailedJob(input.tenantId); | |
| if (failedJob) { | |
| const job = await this.repository.createSyncJob({ | |
| tenantId: input.tenantId, | |
| type: failedJob.type, // preserve original type | |
| triggeredBy: "manual_retry_failed", | |
| }); | |
| await this.eventPublisher.publish( | |
| new SyncJobCreatedEvent(job.id, job.tenantId, job.type) | |
| ); | |
| return { | |
| jobId: job.id, | |
| status: job.status.getValue(), | |
| reused: false, | |
| }; | |
| } | |
| // No failed job found β fall through to full sync | |
| } | |
| // ββ 4. Create new full backfill job ββββββββββββββββββββββββββββββββ | |
| const job = await this.repository.createSyncJob({ | |
| tenantId: input.tenantId, | |
| type: "full_backfill", | |
| triggeredBy: "manual", | |
| }); | |
| await this.eventPublisher.publish( | |
| new SyncJobCreatedEvent(job.id, job.tenantId, job.type) | |
| ); | |
| return { | |
| jobId: job.id, | |
| status: job.status.getValue(), | |
| reused: false, | |
| }; | |
| } | |
| } |