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 | 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 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 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 ResultCode from '@orchesty/nodejs-sdk/dist/lib/Utils/ResultCode';
import crypto from 'crypto';
import RaynetCRMApplication from '../RaynetCRMApplication';
export const NAME = 'raynet-crm-subscribe-webhook';
export function getEntities(): string[] {
return ['Task', 'Email', 'Meeting', 'Letter', 'PhoneCall', 'Event'];
}
export default class RaynetCRMSubscribeWebhook extends ABatchNode {
public getName(): string {
return NAME;
}
public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
const app = this.getApplication<RaynetCRMApplication>();
const webhookIndex = Number(dto.getBatchCursor('0'));
const webhooks = app.getWebhookSubscriptions();
Iif (webhooks.length > 0) {
const appInstall = await this.getApplicationInstallFromProcess(dto);
const repo = this.getDbClient().getRepository(Webhook);
const token = this.getRandomToken();
const requestDto = app.getRequestDto(
dto,
appInstall,
HttpMethods.PUT,
'webhook',
{
url: TopologyRunner.getWebhookUrl(
webhooks[webhookIndex].getTopology(),
webhooks[webhookIndex].getNode(),
token,
),
events: webhooks[webhookIndex].getName().split(','),
entityFilter: getEntities(),
},
);
let res;
try {
res = await this.getSender().send<IResponseJson>(requestDto, [201]);
const respBody = res.getJsonBody();
await repo.insert(new Webhook()
.setWebhookId(respBody.data.uuid)
.setUser(appInstall.getUser())
.setNode(webhooks[webhookIndex].getNode())
.setToken(token)
.setApplication(app.getName())
.setTopology(webhooks[webhookIndex].getTopology())
.setName(webhooks[webhookIndex].getName()));
if (webhooks.length - 1 > webhookIndex) {
dto.setBatchCursor((webhookIndex + 1).toString());
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
appInstall.setEnabled(false);
await this.getDbClient().getRepository(ApplicationInstall).update(appInstall);
dto.setStopProcess(ResultCode.STOP_AND_FAILED, res?.getBody() ?? 'Webhook could not be registered!');
}
}
return dto;
}
private getRandomToken(): string {
return crypto.randomBytes(64).toString('hex');
}
}
export interface IResponseJson {
success: boolean;
data: {
uuid: string;
};
}
|