Edit SMS automation

This commit is contained in:
2026-09-13 21:22:41 +03:30
parent cc23e453de
commit 8925c77ae0
8 changed files with 410 additions and 56 deletions
@@ -14,6 +14,7 @@ function testDelay() {
assert(delayMs(0, 'minutes') === 0, 'zero delay');
assert(delayMs(2, 'hours') === 2 * 3_600_000, '2 hours');
assert(delayMs(2, 'days') === 2 * 86_400_000, '2 days');
assert(delayMs(1, 'weeks') === 7 * 86_400_000, '1 week');
}
function testSendAts() {
+1 -1
View File
@@ -18,7 +18,7 @@ import {
type MessageAutomationTrigger,
} from '@mento/shared';
const UNITS = ['minutes', 'hours', 'days'] as const;
const UNITS = ['minutes', 'hours', 'days', 'weeks', 'months'] as const;
const CHANNELS = ['app', 'sms', 'both'] as const;
const TARGETS = ['staff', 'person', 'both'] as const;
@@ -1,4 +1,4 @@
export type MessageDelayUnit = 'minutes' | 'hours' | 'days';
export type MessageDelayUnit = 'minutes' | 'hours' | 'days' | 'weeks' | 'months';
export const MESSAGE_AUTOMATION_TRIGGERS = [
'person_created',
@@ -22,7 +22,23 @@ export function delayMs(value: number, unit: MessageDelayUnit): number {
const n = Math.max(0, Math.trunc(Number(value) || 0));
if (unit === 'minutes') return n * 60_000;
if (unit === 'hours') return n * 3_600_000;
return n * 86_400_000;
if (unit === 'days') return n * 86_400_000;
if (unit === 'weeks') return n * 7 * 86_400_000;
return n * 30 * 86_400_000;
}
export function addDelay(
from: Date,
value: number,
unit: MessageDelayUnit,
): Date {
const n = Math.max(0, Math.trunc(Number(value) || 0));
if (unit === 'months') {
const next = new Date(from.getTime());
next.setMonth(next.getMonth() + n);
return next;
}
return new Date(from.getTime() + delayMs(n, unit));
}
export function buildSendAts(
@@ -37,9 +53,14 @@ export function buildSendAts(
MAX_MESSAGE_REPEAT,
Math.max(1, Math.trunc(Number(repeatCount) || 1)),
);
const first = new Date(now.getTime() + delayMs(startValue, startUnit));
const gap = delayMs(Math.max(1, intervalValue), intervalUnit);
return Array.from({ length: count }, (_, i) => new Date(first.getTime() + i * gap));
const first = addDelay(now, startValue, startUnit);
const times = [first];
let cursor = first;
for (let i = 1; i < count; i += 1) {
cursor = addDelay(cursor, Math.max(1, intervalValue), intervalUnit);
times.push(cursor);
}
return times;
}
export function applyMessageTemplate(text: string, title: string): string {