@dev.smartpricing/message-composer-layer 4.4.1-next.0 → 4.5.1-automation.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/README.md +182 -7
  2. package/app/api/automations.ts +74 -0
  3. package/app/api/brands.ts +20 -13
  4. package/app/api/client.ts +3 -6
  5. package/app/api/dynamicValues.ts +2 -1
  6. package/app/api/index.ts +1 -0
  7. package/app/api/media.ts +6 -3
  8. package/app/api/messageGroups.ts +11 -3
  9. package/app/api/meta.ts +62 -4
  10. package/app/api/organization.ts +10 -1
  11. package/app/api/tracking.ts +2 -2
  12. package/app/app.config.ts +10 -0
  13. package/app/components/Automations/ActionCard.vue +174 -0
  14. package/app/components/Automations/ActionEditor.vue +398 -0
  15. package/app/components/Automations/Actions.vue +158 -0
  16. package/app/components/Automations/CloneModal.vue +78 -0
  17. package/app/components/Automations/Conditions.vue +155 -0
  18. package/app/components/Automations/Create.vue +114 -0
  19. package/app/components/Automations/Data.vue +46 -0
  20. package/app/components/Automations/DeleteModal.vue +80 -0
  21. package/app/components/Automations/Edit.vue +145 -0
  22. package/app/components/Automations/Form.vue +282 -0
  23. package/app/components/Automations/List.vue +370 -0
  24. package/app/components/Automations/PageGate.vue +50 -0
  25. package/app/components/Automations/SaveProblems.vue +54 -0
  26. package/app/components/Automations/Unavailable.vue +71 -0
  27. package/app/components/Automations/UnavailableBadge.vue +35 -0
  28. package/app/components/Common/AppContextErrors.vue +2 -2
  29. package/app/components/Common/MessageGroupSelect.vue +43 -3
  30. package/app/components/Common/OrganizationGate.vue +51 -0
  31. package/app/components/Common/OrganizationSelect.vue +27 -0
  32. package/app/components/Common/WhatsappNotConfigured.vue +20 -0
  33. package/app/components/Composer/EmailComposer.vue +9 -1
  34. package/app/components/Composer/MessageLibrary.vue +8 -0
  35. package/app/components/Composer/WabaSelectModal.vue +73 -0
  36. package/app/components/Composer/WhatsappComposer.vue +83 -4
  37. package/app/components/Editor/RichTextEditor.vue +14 -2
  38. package/app/components/Filters/DateInput.vue +57 -0
  39. package/app/components/Filters/Editor.vue +163 -0
  40. package/app/components/Filters/Row.vue +119 -0
  41. package/app/components/Filters/Summary.vue +215 -0
  42. package/app/components/Filters/UnavailableRow.vue +74 -0
  43. package/app/components/Filters/ValueInput.vue +215 -0
  44. package/app/components/Layout/BasePage.vue +2 -0
  45. package/app/components/MessageSendFlow/EmailPreview.vue +3 -0
  46. package/app/components/MessageSendFlow/MessageSendFlow.vue +15 -2
  47. package/app/components/MetaImport/Button.vue +50 -0
  48. package/app/components/MetaImport/Dialog.vue +261 -0
  49. package/app/components/MetaImport/Outcome.vue +136 -0
  50. package/app/components/MetaImport/ProposalList.vue +192 -0
  51. package/app/components/TemplateList/BaseTable.vue +22 -1
  52. package/app/components/Whatsapp/Preview.vue +5 -12
  53. package/app/components/Whatsapp/Translator/Header.vue +4 -19
  54. package/app/composables/apiErrorMessages.ts +1 -0
  55. package/app/composables/automationsContext.ts +42 -0
  56. package/app/composables/composerContext.ts +5 -5
  57. package/app/composables/useAutomationClone.ts +33 -0
  58. package/app/composables/useAutomationEditorTrip.ts +77 -0
  59. package/app/composables/useAutomationSaveProblems.ts +82 -0
  60. package/app/composables/useAutomationUnavailable.ts +98 -0
  61. package/app/composables/useAutomationsGate.ts +68 -0
  62. package/app/composables/useBuiltInOptionLists.ts +44 -0
  63. package/app/composables/useEditorTripBack.ts +15 -0
  64. package/app/composables/useEffectiveOrganization.ts +34 -0
  65. package/app/composables/useMessageGroupWaba.ts +76 -0
  66. package/app/composables/useMessageSendFlow.ts +46 -8
  67. package/app/composables/usePagedMessageGroups.ts +14 -2
  68. package/app/composables/useTemplateOperations.ts +18 -4
  69. package/app/composables/useTracking.ts +14 -5
  70. package/app/composables/useWhatsappConfiguration.ts +32 -0
  71. package/app/layouts/default.vue +4 -1
  72. package/app/middleware/automations-enabled.ts +12 -0
  73. package/app/pages/automations/[id].vue +87 -0
  74. package/app/pages/automations/index.vue +28 -0
  75. package/app/pages/automations/new.vue +53 -0
  76. package/app/pages/brands.vue +1 -0
  77. package/app/pages/images.vue +4 -0
  78. package/app/pages/index.vue +6 -1
  79. package/app/pages/preview/[id].vue +2 -0
  80. package/app/pages/template/email/[id].vue +5 -0
  81. package/app/pages/template/email/create.vue +5 -0
  82. package/app/pages/template/whatsapp/[id].vue +6 -0
  83. package/app/pages/template/whatsapp/create.vue +5 -0
  84. package/app/queries/automations.ts +206 -0
  85. package/app/queries/brands.ts +27 -7
  86. package/app/queries/compilation.ts +15 -2
  87. package/app/queries/dynamicValues.ts +13 -2
  88. package/app/queries/index.ts +1 -0
  89. package/app/queries/media.ts +8 -3
  90. package/app/queries/messageGroups.ts +60 -25
  91. package/app/queries/meta.ts +100 -5
  92. package/app/stores/appContext.ts +9 -45
  93. package/app/stores/editorTrip.ts +67 -0
  94. package/app/stores/effectiveOrganization.ts +128 -0
  95. package/app/stores/emailEditor.ts +5 -1
  96. package/app/stores/metaSubmission.ts +7 -17
  97. package/app/types/app-config.d.ts +3 -0
  98. package/app/types/tracking.ts +33 -0
  99. package/app/utils/automations/form.ts +389 -0
  100. package/app/utils/automations/metadata.ts +71 -0
  101. package/app/utils/automations/unavailable.ts +90 -0
  102. package/app/utils/filters/data/countries.json +998 -0
  103. package/app/utils/filters/data/languages.json +754 -0
  104. package/app/utils/filters/rows.ts +367 -0
  105. package/app/utils/testIds.const.ts +108 -0
  106. package/i18n/locales/de.ts +363 -0
  107. package/i18n/locales/en.ts +345 -0
  108. package/i18n/locales/es.ts +349 -0
  109. package/i18n/locales/fr.ts +357 -0
  110. package/i18n/locales/it.ts +354 -0
  111. package/package.json +4 -4
  112. package/app.config.ts +0 -5
@@ -0,0 +1,145 @@
1
+ <script setup lang="ts">
2
+ import type { Automation, AutomationCatalog } from '@dev.smartpricing/message-composer-utils/types'
3
+ import { resolveErrorToast } from '~/composables/apiErrorMessages'
4
+ import type { AutomationsContext } from '~/composables/automationsContext'
5
+ import { useUpdateAutomationMutation } from '~/queries'
6
+ import {
7
+ automationEngineRejected,
8
+ automationPartlySaved,
9
+ automationSaveProblems,
10
+ automationSetupProperties,
11
+ cloneFormData,
12
+ toAutomationForm,
13
+ toUpdateAutomationBody,
14
+ withPartlySavedActions,
15
+ type AutomationFormState,
16
+ } from '../../utils/automations/form'
17
+ import { automationMetadataForm, isSystemAutomation } from '../../utils/automations/metadata'
18
+
19
+ /**
20
+ * An existing Automation: the form, its save, and what a refused or a
21
+ * half-saved save says. The page around it holds the header and calls
22
+ * `submit`.
23
+ */
24
+ const props = defineProps<{
25
+ catalog: AutomationCatalog
26
+ /** The Automation as it was read. The form starts from it and does not follow it. */
27
+ automation: Automation
28
+ context: AutomationsContext
29
+ }>()
30
+
31
+ const { t } = useI18n()
32
+ const toast = useToast()
33
+ const { trackEvent } = useTracking()
34
+
35
+ const trip = useAutomationEditorTrip('automation-edit', props.automation.id)
36
+
37
+ /** The Automation as the Automation Engine holds it: what a save is an update of. */
38
+ const stored = shallowRef(trip.restored?.stored ?? props.automation)
39
+ const form = ref<AutomationFormState>(trip.restored?.form ?? toAutomationForm(props.automation))
40
+ const submitted = ref(false)
41
+ const formElement = useTemplateRef('formElement')
42
+
43
+ /** A System Automation: the form lets through only what its user may change. */
44
+ const system = computed(() => isSystemAutomation(stored.value))
45
+ const dataForm = computed(() => automationMetadataForm(stored.value.metadata))
46
+ const { askClone } = useAutomationClone()
47
+
48
+ const incomplete = computed(() => !form.value.name.trim() || form.value.actions.length === 0)
49
+
50
+ const problems = useAutomationSaveProblems(() => props.catalog)
51
+
52
+ const { mutateAsync: updateAutomation, isLoading: saving } = useUpdateAutomationMutation()
53
+
54
+ async function submit() {
55
+ submitted.value = true
56
+ problems.clear()
57
+ // Something unavailable stops the save: nothing is sent, the alert says what to fix.
58
+ if (formElement.value?.blocked) {
59
+ formElement.value.showUnavailable()
60
+ return
61
+ }
62
+ if (incomplete.value || saving.value) return
63
+
64
+ const sent = cloneFormData(form.value)
65
+ try {
66
+ await updateAutomation({ id: stored.value.id, ...toUpdateAutomationBody(sent, stored.value) })
67
+ } catch (error) {
68
+ const refused = automationSaveProblems(error)
69
+ const partlySaved = automationPartlySaved(error)
70
+ const rejected = automationEngineRejected(error)
71
+ if (refused) {
72
+ problems.show(refused, sent)
73
+ } else if (rejected !== undefined) {
74
+ toast.add({
75
+ title: t('automations.toast.engine_rejected'),
76
+ description: rejected,
77
+ color: 'error',
78
+ })
79
+ } else if (partlySaved) {
80
+ // Nothing was rolled back (ADR 0016): the next save is an update of
81
+ // what the Automation Engine now holds; the form stays as the user left it.
82
+ stored.value = partlySaved.automation
83
+ form.value.actions = withPartlySavedActions(form.value.actions, sent.actions, partlySaved)
84
+ toast.add({
85
+ title: t('automations.toast.partly_saved'),
86
+ description: t('automations.toast.partly_saved_description'),
87
+ color: 'error',
88
+ })
89
+ } else {
90
+ // The request carries no error toast of its own, so a 422 gets none.
91
+ const { data, status = 0 } = error as { data?: { code?: string }; status?: number }
92
+ toast.add({ ...resolveErrorToast({ status, code: data?.code }, t), color: 'error' })
93
+ }
94
+ return
95
+ }
96
+
97
+ trackEvent(
98
+ 'Automation setup',
99
+ automationSetupProperties(
100
+ sent,
101
+ props.catalog.triggers.find(({ type }) => type === sent.triggerType),
102
+ ),
103
+ )
104
+ toast.add({ title: t('automations.toast.updated'), color: 'success' })
105
+ await navigateTo({ name: 'automations-list' })
106
+ }
107
+
108
+ defineExpose({ submit, saving })
109
+ </script>
110
+
111
+ <template>
112
+ <UAlert
113
+ v-if="system"
114
+ color="info"
115
+ variant="soft"
116
+ icon="ph:lock-simple-bold"
117
+ :title="t('automations.system.alert')"
118
+ :data-testid="automationsTestIds.lockedAlert"
119
+ :actions="[
120
+ {
121
+ label: t('common.actions.duplicate'),
122
+ color: 'info',
123
+ variant: 'outline',
124
+ size: 'sm',
125
+ 'data-testid': automationsTestIds.lockedDuplicateButton,
126
+ onClick: () => askClone(stored),
127
+ },
128
+ ]"
129
+ />
130
+
131
+ <AutomationsSaveProblems :lines="problems.lines.value" :incomplete="submitted && incomplete" />
132
+
133
+ <AutomationsForm
134
+ ref="formElement"
135
+ v-model="form"
136
+ :catalog="catalog"
137
+ :context="context"
138
+ :submitted="submitted"
139
+ :invalid-action-keys="problems.invalidActionKeys.value"
140
+ :open-action="trip.restored?.openAction"
141
+ :data-form="dataForm"
142
+ :system="system"
143
+ @edit-message="trip.leave({ form, stored }, $event)"
144
+ />
145
+ </template>
@@ -0,0 +1,282 @@
1
+ <script setup lang="ts">
2
+ import {
3
+ AUTOMATION_CHANNELS,
4
+ type AutomationCatalog,
5
+ type AutomationChannel,
6
+ type FiltersReport,
7
+ } from '@dev.smartpricing/message-composer-utils/types'
8
+ import type { AutomationsContext } from '~/composables/automationsContext'
9
+ import type {
10
+ AutomationMessageRequest,
11
+ OpenAutomationAction,
12
+ } from '~/composables/useAutomationEditorTrip'
13
+ import {
14
+ AUTOMATION_CONDITIONS_ELEMENT_ID,
15
+ AUTOMATION_TRIGGER_ELEMENT_ID,
16
+ } from '~/composables/useAutomationUnavailable'
17
+ import { useMessageGroupApprovalsQuery } from '~/queries'
18
+ import { cloneFormData, type AutomationFormState } from '../../utils/automations/form'
19
+ import type { AutomationMetadataForm } from '../../utils/automations/metadata'
20
+ import type {
21
+ ActionAvailability,
22
+ AutomationUnavailableItem,
23
+ } from '../../utils/automations/unavailable'
24
+
25
+ /**
26
+ * The fields of an Automation: its name, its Trigger, its Conditions and its
27
+ * Actions. Everything a Trigger offers comes from the Automation Catalog, and
28
+ * every option list from the Host's context (ADR 0017).
29
+ */
30
+ const props = defineProps<{
31
+ catalog: AutomationCatalog
32
+ context: AutomationsContext
33
+ /** The form was submitted: what is missing shows on its field. */
34
+ submitted?: boolean
35
+ /** The keys of the Actions a refused save pointed at. */
36
+ invalidActionKeys?: string[]
37
+ /** The Action whose editor starts open: the one an editor trip left from. */
38
+ openAction?: OpenAutomationAction
39
+ /** The values the Automation asks for, when it asks for any. */
40
+ dataForm?: AutomationMetadataForm
41
+ /**
42
+ * A System Automation: only the Active switch of its Actions and the values
43
+ * it asks for can be changed (ADR 0016).
44
+ */
45
+ system?: boolean
46
+ }>()
47
+
48
+ const emit = defineEmits<{
49
+ /** The user leaves for the Message editor from an Action. */
50
+ editMessage: [request: AutomationMessageRequest]
51
+ }>()
52
+
53
+ const form = defineModel<AutomationFormState>({ required: true })
54
+
55
+ const { t } = useI18n()
56
+ const { trackEvent } = useTracking()
57
+ const { confirm } = useConfirm()
58
+
59
+ const trigger = computed(() =>
60
+ props.catalog.triggers.find(({ type }) => type === form.value.triggerType),
61
+ )
62
+
63
+ /** A stored Trigger the Automation Catalog no longer lists shows as it is, and cannot be picked. */
64
+ const triggerOptions = computed(() => [
65
+ ...(trigger.value
66
+ ? []
67
+ : [{ value: form.value.triggerType, label: form.value.triggerType, disabled: true }]),
68
+ ...props.catalog.triggers.map(({ type, label }) => ({ value: type, label })),
69
+ ])
70
+
71
+ /**
72
+ * A Trigger change starts over from the new Trigger's default Conditions
73
+ * (ADR 0017), and without Actions: their Date References are the old
74
+ * Trigger's. The user is asked first when that loses something.
75
+ */
76
+ async function changeTrigger(type: string) {
77
+ const next = props.catalog.triggers.find((candidate) => candidate.type === type)
78
+ const current = trigger.value
79
+ if (!next || type === form.value.triggerType) return
80
+
81
+ const untouched =
82
+ form.value.actions.length === 0 &&
83
+ JSON.stringify(form.value.conditions) === JSON.stringify(current?.default_conditions ?? [])
84
+ if (!untouched) {
85
+ const confirmed = await confirm({
86
+ title: t('automations.form.changeTrigger.title'),
87
+ message: t('automations.form.changeTrigger.message'),
88
+ confirmProps: { label: t('automations.form.changeTrigger.confirm') },
89
+ cancelProps: { label: t('common.actions.cancel') },
90
+ })
91
+ if (!confirmed) return
92
+ }
93
+
94
+ trackEvent('automation.trigger_changed', { from: form.value.triggerType, to: type })
95
+ form.value = {
96
+ ...form.value,
97
+ triggerType: type,
98
+ conditions: cloneFormData(next.default_conditions),
99
+ actions: [],
100
+ }
101
+ }
102
+
103
+ const { isChannelAllowed } = useChannels()
104
+ const { status: whatsappStatus } = useWhatsappConfiguration()
105
+
106
+ /**
107
+ * The Trigger's Channels the Host offers; WhatsApp only once the
108
+ * Organization is known to have a WABA (ADR 0015).
109
+ */
110
+ const channels = computed<AutomationChannel[]>(() =>
111
+ (trigger.value?.channels ?? [])
112
+ .map(({ channel }) => channel)
113
+ .filter(
114
+ (channel) =>
115
+ isChannelAllowed(channel) &&
116
+ (channel !== 'whatsapp' || whatsappStatus.value === 'configured'),
117
+ ),
118
+ )
119
+
120
+ /**
121
+ * The Channels the Organization cannot use: WhatsApp without a WABA. A lookup
122
+ * still running or failed says nothing, and a Channel the Host does not offer
123
+ * is not the Organization's doing: neither is called unusable.
124
+ */
125
+ const unusableChannels = computed<AutomationChannel[]>(() =>
126
+ AUTOMATION_CHANNELS.filter(
127
+ (channel) => channel === 'whatsapp' && whatsappStatus.value === 'not_configured',
128
+ ),
129
+ )
130
+
131
+ const { data: approvals } = useMessageGroupApprovalsQuery(() =>
132
+ form.value.actions.map((action) => action.messageGroupId),
133
+ )
134
+
135
+ const availability = computed<ActionAvailability | undefined>(
136
+ () =>
137
+ trigger.value && {
138
+ trigger: trigger.value,
139
+ unusableChannels: unusableChannels.value,
140
+ approvals: approvals.value,
141
+ },
142
+ )
143
+
144
+ const unavailable = useAutomationUnavailable()
145
+
146
+ /** What `FiltersSummary` reports on the form's Conditions. */
147
+ const filtersReport = ref<FiltersReport>()
148
+ const filtersOpen = ref(false)
149
+
150
+ const unavailableByAction = computed(() => {
151
+ const current = availability.value
152
+ return new Map(
153
+ form.value.actions.map((action) => [
154
+ action.key,
155
+ current ? unavailable.actionItems(action, current) : [],
156
+ ]),
157
+ )
158
+ })
159
+
160
+ /**
161
+ * Everything unavailable in the Automation, top to bottom. Under a Trigger
162
+ * the catalog no longer lists nothing else can be told, or shown.
163
+ */
164
+ const unavailableItems = computed<AutomationUnavailableItem[]>(() => {
165
+ if (!trigger.value) return [unavailable.triggerItem()].map(inSystemAutomation)
166
+
167
+ return [
168
+ ...unavailable.conditionItems(filtersReport.value, trigger.value.condition_fields),
169
+ ...unavailableByAction.value.values(),
170
+ ]
171
+ .flat()
172
+ .map(inSystemAutomation)
173
+ })
174
+
175
+ /**
176
+ * What stops the save of a System Automation: only an Active Action on an
177
+ * unapproved Message Group, which its user can turn off. The rest is its
178
+ * Host's, kept as stored by the backend too (ADR 0016).
179
+ */
180
+ function inSystemAutomation(item: AutomationUnavailableItem): AutomationUnavailableItem {
181
+ if (!props.system) return item
182
+ return { ...item, blocks: item.kind === 'message_group_active' }
183
+ }
184
+
185
+ const blocked = computed(() => unavailableItems.value.some((item) => item.blocks))
186
+
187
+ const UNAVAILABLE_ELEMENT_ID = 'automation-unavailable'
188
+
189
+ function scrollTo(id: string) {
190
+ document.getElementById(id)?.scrollIntoView({ behavior: 'smooth', block: 'center' })
191
+ }
192
+
193
+ function show(item: AutomationUnavailableItem) {
194
+ if (item.target) scrollTo(item.target)
195
+ if (item.inFilters) filtersOpen.value = true
196
+ }
197
+
198
+ defineExpose({
199
+ /** Something unavailable stops the save: the caller does not send it. */
200
+ blocked,
201
+ /** Brings the user back to the alert that says what to fix. */
202
+ showUnavailable: () => scrollTo(UNAVAILABLE_ELEMENT_ID),
203
+ })
204
+ </script>
205
+
206
+ <template>
207
+ <UCard
208
+ :ui="{
209
+ root: 'flex flex-col flex-1 min-h-0',
210
+ body: 'flex-1 w-full max-w-3xl mx-auto px-3 sm:pt-10 overflow-auto space-y-6',
211
+ }"
212
+ :data-testid="automationsTestIds.form"
213
+ >
214
+ <AutomationsUnavailable :id="UNAVAILABLE_ELEMENT_ID" :items="unavailableItems" @show="show" />
215
+
216
+ <h3 class="heading-sm">{{ t('automations.form.title') }}</h3>
217
+
218
+ <UFormField
219
+ :label="t('automations.form.name')"
220
+ :error="submitted && !form.name.trim() ? t('automations.form.required') : undefined"
221
+ >
222
+ <UInput
223
+ v-model="form.name"
224
+ :placeholder="t('automations.form.namePlaceholder')"
225
+ :disabled="system"
226
+ class="w-full"
227
+ :data-testid="automationsTestIds.nameInput"
228
+ />
229
+ </UFormField>
230
+
231
+ <template v-if="dataForm">
232
+ <USeparator />
233
+
234
+ <AutomationsData v-model="form.data" :form="dataForm" />
235
+ </template>
236
+
237
+ <USeparator />
238
+
239
+ <UFormField :id="AUTOMATION_TRIGGER_ELEMENT_ID" :label="t('automations.form.trigger')">
240
+ <template v-if="!trigger" #hint>
241
+ <AutomationsUnavailableBadge :item="unavailable.triggerItem()" />
242
+ </template>
243
+ <USelect
244
+ :model-value="form.triggerType"
245
+ :items="triggerOptions"
246
+ :disabled="system"
247
+ :data-testid="automationsTestIds.triggerSelect"
248
+ @update:model-value="changeTrigger"
249
+ />
250
+ </UFormField>
251
+
252
+ <template v-if="trigger">
253
+ <USeparator />
254
+
255
+ <AutomationsConditions
256
+ :id="AUTOMATION_CONDITIONS_ELEMENT_ID"
257
+ v-model="form.conditions"
258
+ v-model:open="filtersOpen"
259
+ :trigger="trigger"
260
+ :option-lists="context.optionLists.value"
261
+ :currency="context.currency?.value"
262
+ :read-only="system"
263
+ @report="filtersReport = $event"
264
+ />
265
+
266
+ <USeparator />
267
+
268
+ <AutomationsActions
269
+ v-model="form.actions"
270
+ :trigger="trigger"
271
+ :channels="channels"
272
+ :unusable-channels="unusableChannels"
273
+ :unavailable="unavailableByAction"
274
+ :invalid-keys="invalidActionKeys"
275
+ :submitted="submitted"
276
+ :open-action="openAction"
277
+ :read-only="system"
278
+ @edit-message="emit('editMessage', $event)"
279
+ />
280
+ </template>
281
+ </UCard>
282
+ </template>