All files / src/Batch WflowUnsubscribeWebhookBatch.ts

60% Statements 15/25
25% Branches 1/4
100% Functions 2/2
60% Lines 15/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 54 55 56 57 58 59 6015x   15x 15x 15x   15x 15x   15x   15x     15x       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 WflowApplication, { NAME as WFLOW_APP_NAME } from '../WflowApplication';
 
export const NAME = `${WFLOW_APP_NAME}-unsubscribe-webhooks-batch`;
 
export default class WflowUnsubscribeWebhookBatch extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
        const app = this.getApplication<WflowApplication>();
        const appInstall = await this.getApplicationInstallFromProcess(dto, null);
        const repository = this.getDbClient().getRepository(Webhook) as WebhookRepository;
 
        const webhook = await repository.findOne({
            users: [appInstall.getUser()],
            apps: [appInstall.getName()],
            sdks: [appInstall.getSdk()],
        });
 
        Eif (!webhook) {
            return dto;
        }
 
        let organization: string;
 
        try {
            organization = app.getOrganization(appInstall);
        } catch {
            return dto;
        }
 
        const request = app.getRequestDto(
            dto,
            appInstall,
            HttpMethods.DELETE,
            `/${organization}/webhookregistrations/${webhook.getWebhookId()}`,
        );
 
        const response = await this.getSender().send(request);
 
        if (response.getResponseCode() !== StatusCodes.OK) {
            await repository.update(webhook.setUnsubscribeFailed(true));
            throw new OnRepeatException(300, 12, response.getBody());
        }
 
        await repository.remove(webhook);
        return dto.setBatchCursor('1', true);
    }
 
}