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 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 = (
|
const delayAsMilliseconds = (step: Step) => {
|
||||||
delayForUnit: TDelayForUnit,
|
let delayDuration = 0;
|
||||||
delayForValue: number
|
|
||||||
) => {
|
if (step.key === 'delayFor') {
|
||||||
switch (delayForUnit) {
|
const { delayForUnit, delayForValue } = step.parameters;
|
||||||
case 'minutes':
|
|
||||||
return delayForValue * 60 * 1000;
|
delayDuration = delayForAsMilliseconds(
|
||||||
case 'hours':
|
delayForUnit as TDelayForUnit,
|
||||||
return delayForValue * 60 * 60 * 1000;
|
Number(delayForValue)
|
||||||
case 'days':
|
);
|
||||||
return delayForValue * 24 * 60 * 60 * 1000;
|
} else if (step.key === 'delayUntil') {
|
||||||
case 'weeks':
|
const { delayUntil } = step.parameters;
|
||||||
return delayForValue * 7 * 24 * 60 * 60 * 1000;
|
delayDuration = delayUntilAsMilliseconds(delayUntil as string);
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return delayDuration;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default delayAsMilliseconds;
|
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_30_DAYS_OR_150_JOBS,
|
||||||
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
||||||
} from '../helpers/remove-job-configuration';
|
} from '../helpers/remove-job-configuration';
|
||||||
import delayAsMilliseconds, {
|
import delayAsMilliseconds from '../helpers/delay-as-milliseconds';
|
||||||
TDelayForUnit,
|
|
||||||
} from '../helpers/delay-as-milliseconds';
|
|
||||||
|
|
||||||
type JobData = {
|
type JobData = {
|
||||||
flowId: string;
|
flowId: string;
|
||||||
@@ -47,12 +45,7 @@ export const worker = new Worker(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (step.appKey === 'delay') {
|
if (step.appKey === 'delay') {
|
||||||
const { delayForUnit, delayForValue } = step.parameters;
|
jobOptions.delay = delayAsMilliseconds(step);
|
||||||
|
|
||||||
jobOptions.delay = delayAsMilliseconds(
|
|
||||||
delayForUnit as TDelayForUnit,
|
|
||||||
Number(delayForValue)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await actionQueue.add(jobName, jobPayload, jobOptions);
|
await actionQueue.add(jobName, jobPayload, jobOptions);
|
||||||
|
Reference in New Issue
Block a user