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 97 | 5x 5x 5x 5x 5x 5x 5x 5x | 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 ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
import crypto from 'crypto';
import { ZendeskWebhook } from '../types/webhook.types';
import ZendeskApplication from '../ZendeskApplication';
export const ZENDESK_CREATE_WEBHOOK = 'zendesk-create-webhook';
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable no-await-in-loop */
export default class ZendeskCreateWebhookConnector extends ABatchNode {
public getName(): string {
return ZENDESK_CREATE_WEBHOOK;
}
public async processAction(
dto: BatchProcessDto<ZendeskCreateWebhookInput>,
): Promise<BatchProcessDto> {
const repo = this.getDbClient().getRepository(Webhook) as WebhookRepository;
const app = this.getApplication<ZendeskApplication>();
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(),
name: sub.getName(),
topology: sub.getTopology(),
events: JSON.parse(sub.getParameters().events || '[]'),
}));
const unregistered = all.filter((wh) => !registered.find((reg) => reg.getName() === wh.event));
for (const toRegister of unregistered) {
const reqBody = {
webhook: {
endpoint: TopologyRunner.getWebhookUrl(
toRegister.topology,
toRegister.node,
toRegister.token,
),
http_method: 'POST',
name: toRegister.name,
request_format: 'json',
status: 'active',
subscriptions: toRegister.events,
},
};
const requestDto = await app.getRequestDto(dto, appInstall, HttpMethods.POST, '/webhooks', reqBody);
const data = (await this.getSender().send<Response>(requestDto)).getJsonBody();
await repo.insert(
new Webhook()
.setWebhookId(data.webhook.id)
.setUser(appInstall.getUser())
.setNode(toRegister.node)
.setToken(toRegister.token)
.setApplication(app.getName())
.setTopology(toRegister.topology)
.setName(data.webhook.name),
);
}
return dto;
}
protected getData(dto: ProcessDto<ZendeskCreateWebhookInput>): ZendeskCreateWebhookInput {
return dto.getJsonData();
}
protected getRandomToken(): string {
return crypto.randomBytes(64).toString('hex');
}
}
interface Response {
webhook: ZendeskWebhook;
}
export interface ZendeskCreateWebhookInput {
topology: string;
node: string;
name: string;
events: string[];
}
|