All files / src/Batch ZendeskDeleteWebhookConnector.ts

34.78% Statements 8/23
0% Branches 0/6
50% Functions 1/2
34.78% Lines 8/23

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 405x   5x 5x 5x   5x   5x   5x     5x                                                    
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';
 
export const ZENDESK_DELETE_WEBHOOK = 'zendesk-delete-webhook';
 
export default class ZendeskDeleteWebhookConnector extends ABatchNode {
 
    public getName(): string {
        return ZENDESK_DELETE_WEBHOOK;
    }
 
    public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
        const app = this.getApplication();
        const appInstall = await this.getApplicationInstallFromProcess(dto, null);
        const repo = this.getDbClient().getRepository(Webhook) as WebhookRepository;
        const webhook = await repo.findOne({ users: [appInstall.getUser()], apps: [appInstall.getName()] });
 
        if (webhook) {
            const url = `/webhooks/${webhook.getWebhookId()}`;
            const requestDto = await app.getRequestDto(dto, appInstall, HttpMethods.DELETE, url);
            const res = await this.getSender().send(requestDto);
            if (res.getResponseCode() !== StatusCodes.OK && res.getResponseCode() !== StatusCodes.NOT_FOUND) {
                webhook.setUnsubscribeFailed(true);
                await repo.update(webhook);
                throw new OnRepeatException(300, 12, res.getBody());
            }
            await repo.remove(webhook);
            dto.setBatchCursor('1', true);
        }
 
        return dto;
    }
 
}