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:
Gabriel Jablonski 2026-02-04 18:23:25 -03:00 committed by GitHub
parent 614b887110
commit 42b2530a53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 58 additions and 14 deletions

View File

@ -1,5 +1,5 @@
<script setup> <script setup>
import { computed, ref, onMounted } from 'vue'; import { computed, ref, onBeforeMount } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useStore } from 'dashboard/composables/store'; import { useStore } from 'dashboard/composables/store';
import { useAlert } from 'dashboard/composables'; 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 NextButton from 'dashboard/components-next/button/Button.vue';
import WhatsappTemplates from 'dashboard/components/widgets/conversation/WhatsappTemplates/Modal.vue'; import WhatsappTemplates from 'dashboard/components/widgets/conversation/WhatsappTemplates/Modal.vue';
import { DURATION_UNITS } from 'dashboard/components-next/input/constants'; 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({ const props = defineProps({
modelValue: { modelValue: {
@ -28,7 +29,6 @@ const props = defineProps({
const emit = defineEmits(['update:modelValue']); const emit = defineEmits(['update:modelValue']);
const DEFAULT_DELAY_MINUTES = 24 * 60; // 24 hours
const MAX_DELAY_MINUTES = 999 * 24 * 60; // 999 days const MAX_DELAY_MINUTES = 999 * 24 * 60; // 999 days
const { t } = useI18n(); const { t } = useI18n();
@ -59,7 +59,9 @@ const content = computed({
}); });
const delayMinutes = computed({ const delayMinutes = computed({
get: () => normalizedParams.value.delay_minutes ?? DEFAULT_DELAY_MINUTES, get: () =>
normalizedParams.value.delay_minutes ??
DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES,
set: value => { set: value => {
const numValue = Math.min( const numValue = Math.min(
Math.max(1, Number(value) || 1), Math.max(1, Number(value) || 1),
@ -79,15 +81,23 @@ const detectUnit = minutes => {
return DURATION_UNITS.MINUTES; return DURATION_UNITS.MINUTES;
}; };
onMounted(() => { onBeforeMount(() => {
// Always emit the properly formatted params on mount // Normalize delay_minutes for existing automations with invalid/out-of-range values
// This ensures the data is in the correct array format for validation // For new actions, resetAction in useAutomation.js sets the default
// and sets default delay_minutes if not present const rawDelay =
const currentDelay = normalizedParams.value.delay_minutes; normalizedParams.value.delay_minutes ??
const rawDelay = currentDelay ?? DEFAULT_DELAY_MINUTES; DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES;
const delay = Math.min(Math.max(1, Number(rawDelay) || 1), MAX_DELAY_MINUTES); const clampedDelay = Math.min(
updateParams({ delay_minutes: delay }); Math.max(1, Number(rawDelay) || 1),
delayUnit.value = detectUnit(delay); 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 // Attachment handling

View File

@ -190,6 +190,27 @@ describe('useAutomation', () => {
expect(automation.value.actions[0].action_params).toEqual([]); 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', () => { it('manifests custom attributes correctly', () => {
const { manifestCustomAttributes, automationTypes } = useAutomation(); const { manifestCustomAttributes, automationTypes } = useAutomation();
automationTypes.message_created = { conditions: [] }; automationTypes.message_created = { conditions: [] };

View File

@ -15,6 +15,7 @@ import {
// AUTOMATION_RULE_EVENTS, // AUTOMATION_RULE_EVENTS,
// AUTOMATION_ACTION_TYPES, // AUTOMATION_ACTION_TYPES,
AUTOMATIONS, AUTOMATIONS,
DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES,
} from 'dashboard/routes/dashboard/settings/automation/constants.js'; } 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. * @param {number} index - The index of the action to reset.
*/ */
const resetAction = index => { const resetAction = index => {
const action = automation.value.actions[index];
const newActions = [...automation.value.actions]; 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] = {
...newActions[index], ...newActions[index],
action_params: [], action_params: actionParams,
}; };
automation.value.actions = newActions; automation.value.actions = newActions;

View File

@ -73,7 +73,8 @@ export function useEditableAutomation() {
); );
} }
if (inputType === 'scheduled_message') { 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') { if (inputType === 'team_message') {
return { return {

View File

@ -724,3 +724,6 @@ export const AUTOMATION_ACTION_TYPES = [
inputType: 'search_select', inputType: 'search_select',
}, },
]; ];
// Default delay for scheduled messages (24 hours in minutes)
export const DEFAULT_SCHEDULED_MESSAGE_DELAY_MINUTES = 24 * 60;