All files / src/Batch ShopifyUnregisterWebhook.ts

84% Statements 21/25
60% Branches 6/10
100% Functions 2/2
84% Lines 21/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 5416x   16x 16x 16x   16x     16x   16x   16x     16x       1x   1x   1x 1x   1x 1x 1x           1x 1x           1x   1x         1x        
import Webhook from '@orchesty/nodejs-sdk/dist/lib/Application/Database/Webhook';
import WebhookRepository from '@orchesty/nodejs-sdk/dist/lib/Application/Database/WebhookRepository';
import ABatchNode from '@orchesty/nodejs-sdk/dist/lib/Batch/ABatchNode';
import OnRepeatException from '@orchesty/nodejs-sdk/dist/lib/Exception/OnRepeatException';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
import { StatusCodes } from 'http-status-codes';
import ShopifyApplication from '../ShopifyApplication';
 
export const NAME = 'shopify-unregister-webhook';
 
const SHOPIFY_UNREGISTER_WEBHOOK_ENDPOINT = 'admin/api/2024-01/webhooks/{id}.json';
 
export default class ShopifyUnregisterWebhook extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
        const app = this.getApplication<ShopifyApplication>();
 
        const appInstall = await this.getApplicationInstallFromProcess(dto, null, true);
 
        const repo = this.getDbClient().getRepository(Webhook) as WebhookRepository;
        const webhooks = await repo.findMany({ users: [appInstall.getUser()], apps: [app.getName()] });
 
        Eif (webhooks && webhooks.length > 0) {
            const webhookId = webhooks[0].getWebhookId();
            const requestDto = await app.getRequestDto(
                dto,
                appInstall,
                HttpMethods.DELETE,
                SHOPIFY_UNREGISTER_WEBHOOK_ENDPOINT.replace('{id}', webhookId),
            );
            const res = await this.getSender().send(requestDto);
            Iif (res.getResponseCode() !== StatusCodes.OK && res.getResponseCode() !== StatusCodes.NOT_FOUND) {
                webhooks[0].setUnsubscribeFailed(true);
                await repo.update(webhooks[0]);
                throw new OnRepeatException(60, 10, res.getBody());
            }
 
            await repo.remove(webhooks[0]);
 
            Iif (webhooks.length > 1) {
                dto.setBatchCursor('next');
            }
        }
 
        return dto;
    }
 
}