Spaces:
Sleeping

File size: 1,837 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
// ShopifyWebhookRegistrationService.ts — updated

import { env } from "src/config/env";
import { logger } from "src/libs/common/logger";

export interface RegisterWebhooksInput {
    shopDomain: string;
    accessToken: string;
    webhookSecret: string;
    organizationId: string;
    storeId: string;
}

export class ShopifyWebhookRegistrationService {
    private readonly topics = ["products/update", "customers/update", "orders/create", "orders/updated"];

    async registerWebhooks(input: RegisterWebhooksInput): Promise<void> {
        const endpoints = [
            `${env.API_BASE_URL}/api/shopify/webhook`,
            `${env.N8N_WEBHOOK_URL}`,
        ].filter(Boolean);

        for (const topic of this.topics) {
            for (const address of endpoints) {
                await this.register({ ...input, topic, address });
            }
        }
    }

    private async register(input: { shopDomain: string; accessToken: string; topic: string; address: string }): Promise<void> {
        const response = await fetch(
            `https://${input.shopDomain}/admin/api/2025-01/webhooks.json`,
            {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                    "X-Shopify-Access-Token": input.accessToken,
                },
                body: JSON.stringify({
                    webhook: { topic: input.topic, address: input.address, format: "json" },
                }),
            }
        );
        if (!response.ok) {
            const err = await response.text();
            logger.warn("shopify.webhook_registration_failed", { topic: input.topic, address: input.address, err });
        } else {
            logger.info("shopify.webhook_registered", { topic: input.topic, address: input.address });
        }
    }
}