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 | 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x | 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 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 BraniApplication from '../BraniApplication';
export const NAME = 'brani-subscribe-webhooks';
export default class BraniSubscribeWebhooks extends ABatchNode {
public getName(): string {
return NAME;
}
public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
const repo = this.getDbClient().getRepository(Webhook) as WebhookRepository;
const app = this.getApplication<BraniApplication>();
const appInstall = await this.getApplicationInstallFromProcess(dto);
const registered = await repo.findMany({ users: [appInstall.getUser()], apps: [appInstall.getName()] });
const all = app.getWebhookSubscriptions().map((sub) => ({
event: sub.getName(),
token: this.getRandomToken(),
node: sub.getNode(),
topology: sub.getTopology(),
}));
const unregistered = all.filter((wh) => !registered.find((reg) => reg.getName() === wh.event)).shift();
if (unregistered) {
const body = {
// eslint-disable-next-line @typescript-eslint/naming-convention
event_type: unregistered.event,
url: TopologyRunner.getWebhookUrl(unregistered.topology, unregistered.node, unregistered.token),
};
const requestDto = app.getRequestDto(dto, appInstall, HttpMethods.POST, 'webhook', body);
await this.getSender().send<IOutput>(requestDto, [200]);
const wb = new Webhook()
.setWebhookId('n/a')
.setUser(appInstall.getUser())
.setNode(unregistered.node)
.setToken(unregistered.token)
.setApplication(app.getName())
.setTopology(unregistered.topology)
.setName(unregistered.event);
await repo.insert(wb);
dto.setBatchCursor('next', true);
}
return dto;
}
private getRandomToken(): string {
return crypto.randomBytes(12).toString('hex');
}
}
export interface IOutput {
detail: string;
}
|