All files / src/Batch RaynetCRMUnregisterWebhook.ts

83.33% Statements 20/24
60% Branches 6/10
100% Functions 2/2
83.33% Lines 20/24

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 5310x   10x 10x 10x   10x     10x   10x     10x       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 RaynetCRMApplication from '../RaynetCRMApplication';
 
export const NAME = 'raynet-crm-unregister-webhook';
 
export default class RaynetCRMUnregisterWebhook extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
        const app = this.getApplication<RaynetCRMApplication>();
 
        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 = app.getRequestDto(
                dto,
                appInstall,
                HttpMethods.DELETE,
                `webhook/${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;
    }
 
}