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 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 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 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 WflowApplication, { NAME as WFLOW_APP_NAME } from '../WflowApplication';
export const NAME = `${WFLOW_APP_NAME}-subscribe-webhooks-batch`;
export default class WflowSubscribeWebhookBatch extends ABatchNode {
public getName(): string {
return NAME;
}
public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
const app = this.getApplication<WflowApplication>();
const subscriptions = app.getWebhookSubscriptions();
const appInstall = await this.getApplicationInstallFromProcess(dto);
const repository = this.getDbClient().getRepository(Webhook) as WebhookRepository;
const registeredEvents = (await repository.findMany({
users: [appInstall.getUser()],
apps: [appInstall.getName()],
sdks: [appInstall.getSdk()],
})).map((webhook) => webhook.getName());
const unsubscribed = subscriptions.find(
(subscription) => !registeredEvents.some(
(event) => subscription.getName() === event,
),
);
Eif (!unsubscribed) {
return dto;
}
let organization: string;
try {
organization = app.getOrganization(appInstall);
} catch {
return dto;
}
const token = crypto.randomBytes(64).toString('hex');
const request = app.getRequestDto(
dto,
appInstall,
HttpMethods.PUT,
`/${organization}/webhookregistrations`,
{
actions: [unsubscribed.getName()],
webhookUri: TopologyRunner.getWebhookUrl(
unsubscribed.getTopology(),
unsubscribed.getNode(),
token,
),
},
);
const id = (
await this.getSender().send<string>(request)
).getJsonBody();
await repository.insert(
new Webhook()
.setWebhookId(id)
.setUser(appInstall.getUser())
.setSdk(appInstall.getSdk())
.setNode(unsubscribed.getNode())
.setToken(token)
.setApplication(appInstall.getName())
.setTopology(unsubscribed.getTopology())
.setName(unsubscribed.getName()),
);
return dto.setBatchCursor('1', true);
}
}
|