All files / src/Batch FapiSubscribeWebhooksBatch.ts

69.23% Statements 18/26
50% Branches 1/2
40% Functions 2/5
75% Lines 18/24

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 948x   8x   8x 8x   8x 8x 8x   8x   8x     8x       1x 1x 1x       1x             1x         1x       1x 1x                                                                                                    
import Webhook from '@orchesty/nodejs-sdk/dist/lib/Application/Database/Webhook';
import { IWebhookQueryFilter } from '@orchesty/nodejs-sdk/dist/lib/Application/Database/WebhookRepository';
import ABatchNode from '@orchesty/nodejs-sdk/dist/lib/Batch/ABatchNode';
import { IPaging, ISorter } from '@orchesty/nodejs-sdk/dist/lib/Storage/Database/Repository';
import TopologyRunner from '@orchesty/nodejs-sdk/dist/lib/Topology/TopologyRunner';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
import crypto from 'crypto';
import { StatusCodes } from 'http-status-codes';
import FapiApplication, { NAME as APPLICATION_NAME } from '../FapiApplication';
 
export const NAME = `${APPLICATION_NAME}-subscribe-webhooks-batch`;
 
export default class FapiSubscribeWebhooksBatch extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
        const application = this.getApplication<FapiApplication>();
        const applicationInstall = await this.getApplicationInstallFromProcess(dto);
        const webhookRepository = this
            .getDbClient()
            .getRepository<Webhook, IWebhookQueryFilter, ISorter, IPaging>(Webhook);
 
        const webhookSubscriptions = application.getWebhookSubscriptions().map((webhookSubscription) => ({
            event: webhookSubscription.getName(),
            topology: webhookSubscription.getTopology(),
            node: webhookSubscription.getNode(),
            token: crypto.randomBytes(64).toString('hex'),
        }));
 
        const subscribedWebhooks = await webhookRepository.findMany({
            users: [applicationInstall.getUser()],
            apps: [applicationInstall.getName()],
        });
 
        const unsubscribedWebhook = webhookSubscriptions.filter(
            ({ event }) => !subscribedWebhooks.find((subscribedWebhook) => subscribedWebhook.getName() === event),
        ).shift();
 
        Eif (!unsubscribedWebhook) {
            return dto;
        }
 
        const { event, topology, node, token } = unsubscribedWebhook;
        const requestDto = application.getRequestDto(
            dto,
            applicationInstall,
            HttpMethods.POST,
            'connections',
            {
                /* eslint-disable @typescript-eslint/naming-convention */
                application: 'global-webhook',
                credentials: {
                    event,
                    url: TopologyRunner.getWebhookUrl(topology, node, token),
                },
                meta_data: {
                    description: 'Orchesty Webhook',
                },
                /* eslint-enable @typescript-eslint/naming-convention */
            },
        );
 
        const responseDto = await this.getSender().send<IResponse>(requestDto, [StatusCodes.CREATED]);
 
        await webhookRepository.insert(
            new Webhook()
                .setWebhookId(String(responseDto.getJsonBody().id))
                .setApplication(applicationInstall.getName())
                .setUser(applicationInstall.getUser())
                .setTopology(topology)
                .setNode(node)
                .setToken(token)
                .setName(event),
        );
 
        return dto.setBatchCursor('1', true);
    }
 
}
 
interface IResponse {
    id: number;
    user_id: number;
    application: string;
    credentials: {
        url: string;
        event: string;
    };
}