Spaces:
Sleeping

File size: 3,193 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Request, Response } from "express";
import { PgConnectorRepository } from "@shared/infrastructure/postgres/repositories/PgConnectorRepository";
import { logger } from "../../../../../libs/common/logger";
import { ShopifyWebhookRegistrationService } from "src/modules/shopify/domain/services/ShopifyWebhookRegistrationService";
import { InitiateOauthUseCase } from "src/modules/shopify/application/useCases/oauth/InitiateOauthUseCase";
import { env } from "src/config/env";
import { CompleteOauthUseCase } from "src/modules/shopify/application/useCases/oauth/CompleteOauthUseCase";

import { UnitOfWorkFactory } from "@shared/infrastructure/postgres/unitOfWork/UnitOfWorkFactory";

export class ShopifyOauthController {
    constructor(
        private readonly uowFactory: UnitOfWorkFactory,
        private readonly webhookService: ShopifyWebhookRegistrationService = new ShopifyWebhookRegistrationService()
    ) { }

    async initiate(req: Request, res: Response): Promise<void> {
    try {
        const tenantContext = (req as any).tenantContext;
        const { shop, clientId, clientSecret } = req.body;

        if (!shop || !clientId || !clientSecret) {
            res.status(400).json({ error: "Missing 'shop', 'clientId', or 'clientSecret' in request body" });
            return;
        }

        const useCase = new InitiateOauthUseCase(tenantContext); // ← no uowFactory

        const { redirectUrl } = await useCase.execute({ shop, clientId, clientSecret });

        res.redirect(redirectUrl);
    } catch (error) {
        logger.error("shopify.oauth_initiate_failed", {
            error: error instanceof Error ? error.message : String(error),
        });
        res.status(500).json({ error: error instanceof Error ? error.message : "Failed to initiate OAuth" });
    }
}

    async callback(req: Request, res: Response): Promise<void> {
        try {
            const code = req.query.code as string;
            const state = req.query.state as string;
            const rawQuery = req.url.split("?")[1] || "";

            if (!code || !state) {
                res.status(400).json({ error: "Missing 'code' or 'state' parameter" });
                return;
            }

            const useCase = new CompleteOauthUseCase(
                (req as any).tenantContext || ({} as any),
                this.uowFactory,
                this.webhookService
            );

            const result = await useCase.execute({
                code,
                state,
                rawQuery,
            });

            // Redirect back to frontend
            const frontendUrl = env.FRONTEND_URL || "http://localhost:3000";
            res.redirect(`${frontendUrl}/settings/shopify?connected=true&shop=${result.shopDomain}`);
        } catch (error) {
            logger.error("shopify.oauth_callback_failed", {
                error: error instanceof Error ? error.message : String(error),
            });
            const frontendUrl = env.FRONTEND_URL || "http://localhost:3000";
            res.redirect(`${frontendUrl}/settings/shopify?error=${encodeURIComponent(error instanceof Error ? error.message : "OAuth failed")}`);
        }
    }
}