feat(webhook): register in mutation calls

This commit is contained in:
Ali BARIN
2022-11-30 19:33:57 +01:00
parent 77a7df1cff
commit 687c6a09bc
2 changed files with 28 additions and 28 deletions

View File

@@ -40,8 +40,22 @@ const updateFlowStatus = async (
pattern: interval || EVERY_15_MINUTES_CRON, pattern: interval || EVERY_15_MINUTES_CRON,
}; };
if (trigger.type === 'webhook') {
const $ = await globalVariable({
flow,
connection: await triggerStep.$relatedQuery('connection'),
app: await triggerStep.getApp(),
step: triggerStep,
testRun: false,
});
if (flow.active) {
await trigger.registerHook($);
} else {
await trigger.unregisterHook($);
}
} else {
if (flow.active) { if (flow.active) {
// add the flow job in the queue.
flow = await flow.$query().patchAndFetch({ flow = await flow.$query().patchAndFetch({
published_at: new Date().toISOString(), published_at: new Date().toISOString(),
}); });
@@ -52,31 +66,19 @@ const updateFlowStatus = async (
jobName, jobName,
{ flowId: flow.id }, { flowId: flow.id },
{ {
// do not repeat webhook job for immediate webhook registration repeat: repeatOptions,
repeat: trigger.type === 'webhook' ? null : repeatOptions,
jobId: flow.id, jobId: flow.id,
removeOnComplete: REMOVE_AFTER_7_DAYS_OR_50_JOBS, removeOnComplete: REMOVE_AFTER_7_DAYS_OR_50_JOBS,
removeOnFail: REMOVE_AFTER_30_DAYS_OR_150_JOBS removeOnFail: REMOVE_AFTER_30_DAYS_OR_150_JOBS
} }
); );
} else if (!flow.active && trigger.type === 'webhook') {
// unregister webhook from the application
const $ = await globalVariable({
flow,
connection: await triggerStep.$relatedQuery('connection'),
app: await triggerStep.getApp(),
step: triggerStep,
testRun: false,
});
await trigger.unregisterHook($);
} else { } else {
// remove the job out of the queue
const repeatableJobs = await flowQueue.getRepeatableJobs(); const repeatableJobs = await flowQueue.getRepeatableJobs();
const job = repeatableJobs.find((job) => job.id === flow.id); const job = repeatableJobs.find((job) => job.id === flow.id);
await flowQueue.removeRepeatableByKey(job.key); await flowQueue.removeRepeatableByKey(job.key);
} }
}
return flow; return flow;
}; };

View File

@@ -25,8 +25,6 @@ export const processFlow = async (options: ProcessFlowOptions) => {
try { try {
if (triggerCommand.type === 'webhook' && !flow.active) { if (triggerCommand.type === 'webhook' && !flow.active) {
await triggerCommand.testRun($); await triggerCommand.testRun($);
} else if (triggerCommand.type === 'webhook' && flow.active) {
await triggerCommand.registerHook($);
} else { } else {
await triggerCommand.run($); await triggerCommand.run($);
} }