feat: Add delay until action for delay app
This commit is contained in:
28
packages/backend/src/apps/delay/actions/delay-until/index.ts
Normal file
28
packages/backend/src/apps/delay/actions/delay-until/index.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import defineAction from '../../../../helpers/define-action';
|
||||
|
||||
export default defineAction({
|
||||
name: 'Delay Until',
|
||||
key: 'delayUntil',
|
||||
description:
|
||||
'Delays the execution of the next action until a specified date.',
|
||||
arguments: [
|
||||
{
|
||||
label: 'Delay until (Date)',
|
||||
key: 'delayUntil',
|
||||
type: 'string' as const,
|
||||
required: true,
|
||||
description: 'Delay until the date. E.g. 2022-12-18',
|
||||
variables: true,
|
||||
},
|
||||
],
|
||||
|
||||
async run($) {
|
||||
const { delayUntil } = $.step.parameters;
|
||||
|
||||
const dataItem = {
|
||||
delayUntil,
|
||||
};
|
||||
|
||||
$.setActionItem({ raw: dataItem });
|
||||
},
|
||||
});
|
@@ -1,3 +1,4 @@
|
||||
import delayFor from './delay-for';
|
||||
import delayUntil from './delay-until';
|
||||
|
||||
export default [delayFor];
|
||||
export default [delayFor, delayUntil];
|
||||
|
@@ -1,21 +1,25 @@
|
||||
export type TDelayForUnit = 'minutes' | 'hours' | 'days' | 'weeks';
|
||||
import Step from '../models/step';
|
||||
import delayForAsMilliseconds, {
|
||||
TDelayForUnit,
|
||||
} from './delay-for-as-milliseconds';
|
||||
import delayUntilAsMilliseconds from './delay-until-as-milliseconds';
|
||||
|
||||
const delayAsMilliseconds = (
|
||||
delayForUnit: TDelayForUnit,
|
||||
delayForValue: number
|
||||
) => {
|
||||
switch (delayForUnit) {
|
||||
case 'minutes':
|
||||
return delayForValue * 60 * 1000;
|
||||
case 'hours':
|
||||
return delayForValue * 60 * 60 * 1000;
|
||||
case 'days':
|
||||
return delayForValue * 24 * 60 * 60 * 1000;
|
||||
case 'weeks':
|
||||
return delayForValue * 7 * 24 * 60 * 60 * 1000;
|
||||
default:
|
||||
return 0;
|
||||
const delayAsMilliseconds = (step: Step) => {
|
||||
let delayDuration = 0;
|
||||
|
||||
if (step.key === 'delayFor') {
|
||||
const { delayForUnit, delayForValue } = step.parameters;
|
||||
|
||||
delayDuration = delayForAsMilliseconds(
|
||||
delayForUnit as TDelayForUnit,
|
||||
Number(delayForValue)
|
||||
);
|
||||
} else if (step.key === 'delayUntil') {
|
||||
const { delayUntil } = step.parameters;
|
||||
delayDuration = delayUntilAsMilliseconds(delayUntil as string);
|
||||
}
|
||||
|
||||
return delayDuration;
|
||||
};
|
||||
|
||||
export default delayAsMilliseconds;
|
||||
|
21
packages/backend/src/helpers/delay-for-as-milliseconds.ts
Normal file
21
packages/backend/src/helpers/delay-for-as-milliseconds.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export type TDelayForUnit = 'minutes' | 'hours' | 'days' | 'weeks';
|
||||
|
||||
const delayAsMilliseconds = (
|
||||
delayForUnit: TDelayForUnit,
|
||||
delayForValue: number
|
||||
) => {
|
||||
switch (delayForUnit) {
|
||||
case 'minutes':
|
||||
return delayForValue * 60 * 1000;
|
||||
case 'hours':
|
||||
return delayForValue * 60 * 60 * 1000;
|
||||
case 'days':
|
||||
return delayForValue * 24 * 60 * 60 * 1000;
|
||||
case 'weeks':
|
||||
return delayForValue * 7 * 24 * 60 * 60 * 1000;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
export default delayAsMilliseconds;
|
@@ -0,0 +1,8 @@
|
||||
const delayUntilAsMilliseconds = (delayUntil: string) => {
|
||||
const delayUntilDate = new Date(delayUntil);
|
||||
const now = new Date();
|
||||
|
||||
return delayUntilDate.getTime() - now.getTime();
|
||||
};
|
||||
|
||||
export default delayUntilAsMilliseconds;
|
@@ -8,9 +8,7 @@ import {
|
||||
REMOVE_AFTER_30_DAYS_OR_150_JOBS,
|
||||
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
||||
} from '../helpers/remove-job-configuration';
|
||||
import delayAsMilliseconds, {
|
||||
TDelayForUnit,
|
||||
} from '../helpers/delay-as-milliseconds';
|
||||
import delayAsMilliseconds from '../helpers/delay-as-milliseconds';
|
||||
|
||||
type JobData = {
|
||||
flowId: string;
|
||||
@@ -47,12 +45,7 @@ export const worker = new Worker(
|
||||
};
|
||||
|
||||
if (step.appKey === 'delay') {
|
||||
const { delayForUnit, delayForValue } = step.parameters;
|
||||
|
||||
jobOptions.delay = delayAsMilliseconds(
|
||||
delayForUnit as TDelayForUnit,
|
||||
Number(delayForValue)
|
||||
);
|
||||
jobOptions.delay = delayAsMilliseconds(step);
|
||||
}
|
||||
|
||||
await actionQueue.add(jobName, jobPayload, jobOptions);
|
||||
|
Reference in New Issue
Block a user