All files / src/Connector BraniUnsubscribeWebhook.ts

100% Statements 17/17
50% Branches 2/4
100% Functions 4/4
100% Lines 16/16

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 469x   9x 9x       9x   9x     9x       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 AConnector from '@orchesty/nodejs-sdk/dist/lib/Connector/AConnector';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
import { IOutput as IInput } from '../Batch/BraniListWebhooks';
 
export const NAME = 'brani-unsubscribe-webhook';
 
export default class BraniUnsubscribeWebhook extends AConnector {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: ProcessDto<IInput>): Promise<ProcessDto<IOutput>> {
        const { id, event_type } = dto.getJsonData();
 
        const req = await this.getApplication().getRequestDto(
            dto,
            await this.getApplicationInstallFromProcess(dto, null),
            HttpMethods.DELETE,
            `webhook/${id}`,
        );
        const resp = await this.getSender().send<IOutput>(req, [200]);
 
        await this.cleanWebhookFromDb(dto.getUser() ?? '', dto.getCurrentApp(), event_type);
 
        return dto.setNewJsonData(resp.getJsonBody());
    }
 
    private async cleanWebhookFromDb(user: string, app: string, event: string): Promise<void> {
        const repo = this.getDbClient().getRepository(Webhook) as WebhookRepository;
        const registered = await repo.findMany({ users: [user], apps: [app] });
        const deletedWebhook = registered.find((webhook) => webhook.getName() === event);
        Eif (deletedWebhook) {
            await repo.remove(deletedWebhook);
        }
    }
 
}
 
export interface IOutput {
    detail: string;
}