All files / src/Batch OutlookUnsubscribeWebhook.ts

85.71% Statements 24/28
66.66% Branches 8/12
100% Functions 2/2
85.71% Lines 24/28

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 60 617x   7x 7x 7x   7x     7x   7x     7x       2x 2x 2x 2x 2x 2x 1x     2x   2x 2x 2x             2x 2x           2x   2x         2x                
import Webhook from '@orchesty/nodejs-sdk/dist/lib/Application/Database/Webhook';
import WebhookRepository, { IWebhookQueryFilter } 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 OutlookApplication from '../OutlookApplication';
 
export const NAME = 'outlook-unsubscribe-webhook';
 
export default class OutlookUnsubscribeWebhook extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto<IInput>): Promise<BatchProcessDto> {
        const { id } = dto.getJsonData();
        const app = this.getApplication<OutlookApplication>();
        const appInstall = await this.getApplicationInstallFromProcess(dto, null, true);
        const repo = this.getDbClient().getRepository(Webhook) as WebhookRepository;
        let filter: IWebhookQueryFilter = { users: [appInstall.getUser()], apps: [app.getName()] };
        if (id) {
            filter = { ids: [id] };
        }
 
        const webhooks = await repo.findMany(filter);
 
        Eif (webhooks && webhooks.length > 0) {
            const webhookId = webhooks[0].getWebhookId();
            const requestDto = app.getRequestDto(
                dto,
                appInstall,
                HttpMethods.DELETE,
                `/subscriptions/${webhookId}`,
            );
 
            const res = await this.getSender().send(requestDto);
            Iif (res.getResponseCode() !== StatusCodes.NO_CONTENT && 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', true);
            }
        }
 
        return dto;
    }
 
}
 
export interface IInput {
    id?: string;
}