fix: update default delay for scheduled messages in automation (#209)
* fix: update default delay for scheduled messages in automation * fix: set default delay for scheduled messages to 24 hours in automation
This commit is contained in:
parent
614b887110
commit
42b2530a53
@ -1,5 +1,5 @@
|
||||
<script setup>
|
||||
import { computed, ref, onMounted } from 'vue';
|
||||
import { computed, ref, onBeforeMount } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { useStore } from 'dashboard/composables/store';
|
||||
import { useAlert } from 'dashboard/composables';
|
||||
@ -10,6 +10,7 @@ import DurationInput from 'dashboard/components-next/input/DurationInput.vue';
|
||||
import NextButton from 'dashboard/components-next/button/Button.vue';
|
||||
import WhatsappTemplates from 'dashboard/components/widgets/conversation/WhatsappTemplates/Modal.vue';
|
||||
import { DURATION_UNITS } from 'dashboard/components-next/input/constants';
|
||||
import { DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES } from 'dashboard/routes/dashboard/settings/automation/constants.js';
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: {
|
||||
@ -28,7 +29,6 @@ const props = defineProps({
|
||||
|
||||
const emit = defineEmits(['update:modelValue']);
|
||||
|
||||
const DEFAULT_DELAY_MINUTES = 24 * 60; // 24 hours
|
||||
const MAX_DELAY_MINUTES = 999 * 24 * 60; // 999 days
|
||||
|
||||
const { t } = useI18n();
|
||||
@ -59,7 +59,9 @@ const content = computed({
|
||||
});
|
||||
|
||||
const delayMinutes = computed({
|
||||
get: () => normalizedParams.value.delay_minutes ?? DEFAULT_DELAY_MINUTES,
|
||||
get: () =>
|
||||
normalizedParams.value.delay_minutes ??
|
||||
DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES,
|
||||
set: value => {
|
||||
const numValue = Math.min(
|
||||
Math.max(1, Number(value) || 1),
|
||||
@ -79,15 +81,23 @@ const detectUnit = minutes => {
|
||||
return DURATION_UNITS.MINUTES;
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
// Always emit the properly formatted params on mount
|
||||
// This ensures the data is in the correct array format for validation
|
||||
// and sets default delay_minutes if not present
|
||||
const currentDelay = normalizedParams.value.delay_minutes;
|
||||
const rawDelay = currentDelay ?? DEFAULT_DELAY_MINUTES;
|
||||
const delay = Math.min(Math.max(1, Number(rawDelay) || 1), MAX_DELAY_MINUTES);
|
||||
updateParams({ delay_minutes: delay });
|
||||
delayUnit.value = detectUnit(delay);
|
||||
onBeforeMount(() => {
|
||||
// Normalize delay_minutes for existing automations with invalid/out-of-range values
|
||||
// For new actions, resetAction in useAutomation.js sets the default
|
||||
const rawDelay =
|
||||
normalizedParams.value.delay_minutes ??
|
||||
DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES;
|
||||
const clampedDelay = Math.min(
|
||||
Math.max(1, Number(rawDelay) || 1),
|
||||
MAX_DELAY_MINUTES
|
||||
);
|
||||
|
||||
// Only emit if the value needs normalization to avoid unnecessary updates
|
||||
if (clampedDelay !== normalizedParams.value.delay_minutes) {
|
||||
updateParams({ delay_minutes: clampedDelay });
|
||||
}
|
||||
|
||||
delayUnit.value = detectUnit(clampedDelay);
|
||||
});
|
||||
|
||||
// Attachment handling
|
||||
|
||||
@ -190,6 +190,27 @@ describe('useAutomation', () => {
|
||||
expect(automation.value.actions[0].action_params).toEqual([]);
|
||||
});
|
||||
|
||||
it('resets scheduled message action with default delay_minutes', () => {
|
||||
const { resetAction, automation } = useAutomation();
|
||||
automation.value = {
|
||||
event_name: 'message_created',
|
||||
conditions: [],
|
||||
actions: [
|
||||
{
|
||||
action_name: 'create_scheduled_message',
|
||||
action_params: [{ content: 'test', delay_minutes: 60 }],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
resetAction(0);
|
||||
|
||||
// Should reset with default delay of 24 hours (1440 minutes)
|
||||
expect(automation.value.actions[0].action_params).toEqual([
|
||||
{ delay_minutes: 1440 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('manifests custom attributes correctly', () => {
|
||||
const { manifestCustomAttributes, automationTypes } = useAutomation();
|
||||
automationTypes.message_created = { conditions: [] };
|
||||
|
||||
@ -15,6 +15,7 @@ import {
|
||||
// AUTOMATION_RULE_EVENTS,
|
||||
// AUTOMATION_ACTION_TYPES,
|
||||
AUTOMATIONS,
|
||||
DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES,
|
||||
} from 'dashboard/routes/dashboard/settings/automation/constants.js';
|
||||
|
||||
/**
|
||||
@ -123,10 +124,18 @@ export function useAutomation(startValue = null) {
|
||||
* @param {number} index - The index of the action to reset.
|
||||
*/
|
||||
const resetAction = index => {
|
||||
const action = automation.value.actions[index];
|
||||
const newActions = [...automation.value.actions];
|
||||
|
||||
// For scheduled messages, initialize with default delay
|
||||
const actionParams =
|
||||
action.action_name === 'create_scheduled_message'
|
||||
? [{ delay_minutes: DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES }]
|
||||
: [];
|
||||
|
||||
newActions[index] = {
|
||||
...newActions[index],
|
||||
action_params: [],
|
||||
action_params: actionParams,
|
||||
};
|
||||
|
||||
automation.value.actions = newActions;
|
||||
|
||||
@ -73,7 +73,8 @@ export function useEditableAutomation() {
|
||||
);
|
||||
}
|
||||
if (inputType === 'scheduled_message') {
|
||||
return params[0] || {};
|
||||
// Keep as array to maintain consistent format with how the component emits updates
|
||||
return params[0] ? [params[0]] : [];
|
||||
}
|
||||
if (inputType === 'team_message') {
|
||||
return {
|
||||
|
||||
@ -724,3 +724,6 @@ export const AUTOMATION_ACTION_TYPES = [
|
||||
inputType: 'search_select',
|
||||
},
|
||||
];
|
||||
|
||||
// Default delay for scheduled messages (24 hours in minutes)
|
||||
export const DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES = 24 * 60;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user