All files / src/Connector UpgatesDeleteWebhooks.ts

77.5% Statements 31/40
58.33% Branches 7/12
85.71% Functions 6/7
77.5% Lines 31/40

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96  8x   8x 8x 8x   8x     8x 8x   8x     8x       1x     1x 1x           1x 1x 2x   1x   1x 1x     1x 1x           1x 1x                   1x 1x 1x   1x 1x   1x 1x                 1x         1x                              
import { ApplicationInstall } from '@orchesty/nodejs-sdk/dist/lib/Application/Database/ApplicationInstall';
import Webhook from '@orchesty/nodejs-sdk/dist/lib/Application/Database/Webhook';
import WebhookRepository from '@orchesty/nodejs-sdk/dist/lib/Application/Database/WebhookRepository';
import AConnector from '@orchesty/nodejs-sdk/dist/lib/Connector/AConnector';
import OnRepeatException from '@orchesty/nodejs-sdk/dist/lib/Exception/OnRepeatException';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
import { StatusCodes } from 'http-status-codes';
import UpgatesApplication from '../UpgatesApplication';
 
export const NAME = 'upgates-delete-webhooks';
const UPGATES_DELETE_WEBHOOK_ENDPOINT = 'api/v2/webhooks';
 
export default class UpgatesDeleteWebhooks extends AConnector {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: ProcessDto): Promise<ProcessDto> {
        const app = this.getApplication<UpgatesApplication>();
 
        let appInstall: ApplicationInstall;
        try {
            appInstall = await this.getApplicationInstallFromProcess(dto, null, true);
            // eslint-disable-next-line @typescript-eslint/no-unused-vars
        } catch (e) {
            appInstall = await this.getApplicationInstallFromProcess(dto, null, false);
        }
 
        const repo = this.getDbClient().getRepository(Webhook) as WebhookRepository;
        const webhooks = (await repo.findMany({ users: [appInstall.getUser()], apps: [app.getName()] }))
            .filter((item) => !item.getUnsubscribeFailed());
 
        const webhooksIds: number[] = [];
 
        webhooks.forEach((webhook) => {
            webhooksIds.push(Number(webhook.getWebhookId()));
        });
 
        Eif (webhooks && webhooks.length > 0) {
            const requestDto = app.getRequestDto(
                dto,
                appInstall,
                HttpMethods.DELETE,
                `${UPGATES_DELETE_WEBHOOK_ENDPOINT}?ids=${webhooksIds.join(';')}`,
            );
            const res = await this.getSender().send<IResponse>(requestDto);
            Iif (res.getResponseCode() !== StatusCodes.OK && res.getResponseCode() !== StatusCodes.NOT_FOUND) {
                await Promise.all(
                    webhooks.map(async (wantedDelete) => {
                        wantedDelete.setUnsubscribeFailed(true);
                        return repo.update(wantedDelete);
                    }),
                );
                throw new OnRepeatException(60, 10, res.getBody());
            }
 
            let repeat = false;
            const resData = res.getJsonBody().webhooks;
            await Promise.all(
                webhooks.map(async (wantedDelete) => {
                    const foundWebhook = resData?.find(
                        (item) => String(item.id) === wantedDelete.getWebhookId(),
                    );
                    Eif (foundWebhook) {
                        return repo.remove(wantedDelete);
                    }
 
                    repeat = true;
                    wantedDelete.setUnsubscribeFailed(true);
                    return repo.update(wantedDelete);
                }),
            );
 
            Iif (repeat) {
                throw new OnRepeatException(60, 10, res.getBody());
            }
        }
 
        return dto;
    }
 
}
 
export interface IResponse {
    webhooks: IWebhook[]
}
 
export interface IWebhook {
    id: string|number;
 
    deleted: boolean;
    messages: string[];
}