From cc23e453de6c81ab2a0d95a735aa5c1129e55c72 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Sun, 13 Sep 2026 20:11:48 +0330 Subject: [PATCH] Edit UI SMS automation --- apps/web/src/views/MessageAutomationsView.vue | 489 ++++++++++++------ 1 file changed, 324 insertions(+), 165 deletions(-) diff --git a/apps/web/src/views/MessageAutomationsView.vue b/apps/web/src/views/MessageAutomationsView.vue index 48e7bce..5f4263f 100644 --- a/apps/web/src/views/MessageAutomationsView.vue +++ b/apps/web/src/views/MessageAutomationsView.vue @@ -2,6 +2,8 @@ import { computed, onMounted, ref } from 'vue'; import axios from 'axios'; import { api } from '@/lib/api'; +import JalaliDateInput from '@/components/JalaliDateInput.vue'; +import { formatLocalDateTimeInput, parseLocalInput } from '@/lib/jalali'; import type { CreateMessageAutomationRuleRequest, MessageAutomationChannel, @@ -27,13 +29,10 @@ const items = ref([]); const editingId = ref(null); const name = ref(''); -const isActive = ref(true); const trigger = ref('invoice_issued'); -const startDelayValue = ref(0); -const startDelayUnit = ref('minutes'); +const startAt = ref(''); const repeatCount = ref(1); -const intervalValue = ref(2); -const intervalUnit = ref('days'); +const intervalAt = ref(''); const notifyTarget = ref('person'); const channel = ref('sms'); const title = ref(''); @@ -45,16 +44,38 @@ const personNote = computed( 'ناتیف داخل برنامه برای کارکنان است؛ برای مشتری پیامک ارسال می‌شود.', ); +function delayMs(value: number, unit: MessageAutomationDelayUnit) { + 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; +} + +function delayToIso(value: number, unit: MessageAutomationDelayUnit) { + if (!value) return ''; + return formatLocalDateTimeInput(new Date(Date.now() + delayMs(value, unit))); +} + +function isoToDelay(iso: string): { + value: number; + unit: MessageAutomationDelayUnit; +} { + const date = parseLocalInput(iso); + if (!date) return { value: 0, unit: 'minutes' }; + const mins = Math.max(0, Math.round((date.getTime() - Date.now()) / 60_000)); + if (mins === 0) return { value: 0, unit: 'minutes' }; + if (mins % (24 * 60) === 0) return { value: mins / (24 * 60), unit: 'days' }; + if (mins % 60 === 0) return { value: mins / 60, unit: 'hours' }; + return { value: mins, unit: 'minutes' }; +} + function resetForm() { editingId.value = null; name.value = ''; - isActive.value = true; trigger.value = 'invoice_issued'; - startDelayValue.value = 0; - startDelayUnit.value = 'minutes'; + startAt.value = ''; repeatCount.value = 1; - intervalValue.value = 2; - intervalUnit.value = 'days'; + intervalAt.value = delayToIso(2, 'days'); notifyTarget.value = 'person'; channel.value = 'sms'; title.value = ''; @@ -65,13 +86,10 @@ function resetForm() { function startEdit(row: MessageAutomationRuleDto) { editingId.value = row.id; name.value = row.name; - isActive.value = row.isActive; trigger.value = row.trigger; - startDelayValue.value = row.startDelayValue; - startDelayUnit.value = row.startDelayUnit; + startAt.value = delayToIso(row.startDelayValue, row.startDelayUnit); repeatCount.value = row.repeatCount; - intervalValue.value = row.intervalValue; - intervalUnit.value = row.intervalUnit; + intervalAt.value = delayToIso(row.intervalValue, row.intervalUnit); notifyTarget.value = row.notifyTarget; channel.value = row.channel; title.value = row.title; @@ -104,15 +122,17 @@ async function load() { } function payload(): CreateMessageAutomationRuleRequest { + const start = isoToDelay(startAt.value); + const interval = isoToDelay(intervalAt.value); return { name: name.value.trim(), - isActive: isActive.value, + isActive: true, trigger: trigger.value, - startDelayValue: startDelayValue.value, - startDelayUnit: startDelayUnit.value, + startDelayValue: start.value, + startDelayUnit: start.unit, repeatCount: repeatCount.value, - intervalValue: intervalValue.value, - intervalUnit: intervalUnit.value, + intervalValue: Math.max(1, interval.value || 1), + intervalUnit: interval.value ? interval.unit : 'days', notifyTarget: notifyTarget.value, channel: channel.value, title: title.value.trim(), @@ -130,7 +150,8 @@ async function save() { ok.value = ''; try { if (editingId.value) { - const bodyDto: UpdateMessageAutomationRuleRequest = payload(); + const { isActive: _ignore, ...rest } = payload(); + const bodyDto: UpdateMessageAutomationRuleRequest = rest; await api.patch(`/message-automations/${editingId.value}`, bodyDto); ok.value = 'قانون به‌روز شد.'; } else { @@ -178,6 +199,11 @@ onMounted(load); جدا از اتوماسیون گردش کار. فقط برای پیامک و ناتیف: چه اتفاقی بیفتد، چند بار و با چه فاصله‌ای.

+
+ +

{{ error }}

@@ -185,59 +211,44 @@ onMounted(load);

{{ editingId ? 'ویرایش قانون' : 'قانون جدید' }}

- - -
-