Spaces:
Sleeping

File size: 1,284 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Request, Response } from "express";
import { TriggerSyncUseCase } from "../../../application/useCases/sync/TriggerSyncUseCase";
import { PgSyncJobRepository } from "../../../infrastructure/postgres/repositories/PgSyncJobRepository";
import { InMemoryEventPublisher } from "../../../application/ports/IEventPublisher";

export const ShopifySyncController = async (req: Request, res: Response): Promise<void> => {
    // ── Shared instances β€” created inside function to ensure DB connection is ready ──
    const syncJobRepository = new PgSyncJobRepository();
    const eventPublisher = new InMemoryEventPublisher();

    const tenantContext = req.tenantContext;
    if (!tenantContext) {
        // Should never happen β€” TenantMiddleware runs before this
        res.status(401).json({ error: { code: "UNAUTHORIZED", message: "Tenant context missing" } });
        return;
    }

    const action = (req.body?.action as "full" | "retry_failed") || "full";

    const useCase = new TriggerSyncUseCase(tenantContext, syncJobRepository, eventPublisher);

    const result = await useCase.execute({
        tenantId: tenantContext.tenantId,
        action,
    });

    res.status(202).json(result);
    // Errors bubble to GlobalErrorHandler β€” no try/catch here
};