@dev.smartpricing/message-composer-layer 4.3.2-fix.1 → 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.
- package/README.md +357 -46
- package/app/api/automations.ts +74 -0
- package/app/api/brands.ts +20 -13
- package/app/api/client.ts +15 -6
- package/app/api/compilation.ts +13 -3
- package/app/api/dynamicValues.ts +6 -2
- package/app/api/index.ts +1 -0
- package/app/api/media.ts +6 -3
- package/app/api/messageGroups.ts +34 -15
- package/app/api/meta.ts +62 -4
- package/app/api/organization.ts +10 -1
- package/app/api/tracking.ts +2 -2
- package/app/app.config.ts +10 -0
- package/app/components/Automations/ActionCard.vue +174 -0
- package/app/components/Automations/ActionEditor.vue +398 -0
- package/app/components/Automations/Actions.vue +158 -0
- package/app/components/Automations/CloneModal.vue +78 -0
- package/app/components/Automations/Conditions.vue +155 -0
- package/app/components/Automations/Create.vue +114 -0
- package/app/components/Automations/Data.vue +46 -0
- package/app/components/Automations/DeleteModal.vue +80 -0
- package/app/components/Automations/Edit.vue +145 -0
- package/app/components/Automations/Form.vue +282 -0
- package/app/components/Automations/List.vue +370 -0
- package/app/components/Automations/PageGate.vue +50 -0
- package/app/components/Automations/SaveProblems.vue +54 -0
- package/app/components/Automations/Unavailable.vue +71 -0
- package/app/components/Automations/UnavailableBadge.vue +35 -0
- package/app/components/Common/AppContextErrors.vue +30 -0
- package/app/components/Common/HtmlPreviewIframe.vue +17 -3
- package/app/components/Common/MessageGroupSelect.vue +193 -26
- package/app/components/Common/MessagePreviewSlideover.vue +3 -0
- package/app/components/Common/OrganizationGate.vue +51 -0
- package/app/components/Common/OrganizationSelect.vue +27 -0
- package/app/components/Common/WhatsappNotConfigured.vue +20 -0
- package/app/components/Composer/EmailComposer.vue +9 -1
- package/app/components/Composer/MessageLibrary.vue +25 -7
- package/app/components/Composer/WabaSelectModal.vue +73 -0
- package/app/components/Composer/WhatsappComposer.vue +83 -4
- package/app/components/Editor/RichTextEditor.vue +14 -2
- package/app/components/Filters/DateInput.vue +57 -0
- package/app/components/Filters/Editor.vue +163 -0
- package/app/components/Filters/Row.vue +119 -0
- package/app/components/Filters/Summary.vue +215 -0
- package/app/components/Filters/UnavailableRow.vue +74 -0
- package/app/components/Filters/ValueInput.vue +215 -0
- package/app/components/Layout/BasePage.vue +2 -0
- package/app/components/MessageSendFlow/EmailPreview.vue +81 -0
- package/app/components/MessageSendFlow/LanguageSelect.vue +39 -0
- package/app/components/MessageSendFlow/MessageSendFlow.vue +265 -0
- package/app/components/MessageSendFlow/ParametersForm.vue +51 -0
- package/app/components/MetaImport/Button.vue +50 -0
- package/app/components/MetaImport/Dialog.vue +261 -0
- package/app/components/MetaImport/Outcome.vue +136 -0
- package/app/components/MetaImport/ProposalList.vue +192 -0
- package/app/components/TemplateList/BaseTable.vue +177 -108
- package/app/components/Whatsapp/Preview.vue +17 -16
- package/app/components/Whatsapp/PreviewPanel.vue +7 -1
- package/app/components/Whatsapp/Translator/Header.vue +4 -19
- package/app/composables/apiErrorMessages.ts +1 -0
- package/app/composables/automationsContext.ts +42 -0
- package/app/composables/composerContext.ts +9 -7
- package/app/composables/useAutomationClone.ts +33 -0
- package/app/composables/useAutomationEditorTrip.ts +77 -0
- package/app/composables/useAutomationSaveProblems.ts +82 -0
- package/app/composables/useAutomationUnavailable.ts +98 -0
- package/app/composables/useAutomationsGate.ts +68 -0
- package/app/composables/useBuiltInOptionLists.ts +44 -0
- package/app/composables/useEditorTripBack.ts +15 -0
- package/app/composables/useEffectiveOrganization.ts +34 -0
- package/app/composables/useMessageGroupList.ts +186 -0
- package/app/composables/useMessageGroupWaba.ts +76 -0
- package/app/composables/useMessageSendFlow.ts +284 -0
- package/app/composables/usePagedMessageGroups.ts +78 -0
- package/app/composables/useTemplateOperations.ts +31 -7
- package/app/composables/useTracking.ts +14 -5
- package/app/composables/useWhatsappConfiguration.ts +32 -0
- package/app/layouts/default.vue +5 -1
- package/app/middleware/automations-enabled.ts +12 -0
- package/app/pages/automations/[id].vue +87 -0
- package/app/pages/automations/index.vue +28 -0
- package/app/pages/automations/new.vue +53 -0
- package/app/pages/brands.vue +1 -0
- package/app/pages/images.vue +4 -0
- package/app/pages/index.vue +6 -1
- package/app/pages/preview/[id].vue +2 -0
- package/app/pages/template/email/[id].vue +5 -0
- package/app/pages/template/email/create.vue +5 -0
- package/app/pages/template/whatsapp/[id].vue +6 -0
- package/app/pages/template/whatsapp/create.vue +5 -0
- package/app/queries/automations.ts +206 -0
- package/app/queries/brands.ts +27 -7
- package/app/queries/compilation.ts +16 -3
- package/app/queries/dynamicValues.ts +19 -2
- package/app/queries/index.ts +1 -0
- package/app/queries/media.ts +8 -3
- package/app/queries/messageGroups.ts +84 -36
- package/app/queries/meta.ts +100 -5
- package/app/stores/appContext.ts +18 -48
- package/app/stores/editorTrip.ts +67 -0
- package/app/stores/effectiveOrganization.ts +128 -0
- package/app/stores/emailEditor.ts +5 -1
- package/app/stores/metaSubmission.ts +7 -17
- package/app/types/app-config.d.ts +3 -0
- package/app/types/tracking.ts +50 -0
- package/app/utils/automations/form.ts +389 -0
- package/app/utils/automations/metadata.ts +71 -0
- package/app/utils/automations/unavailable.ts +90 -0
- package/app/utils/filters/data/countries.json +998 -0
- package/app/utils/filters/data/languages.json +754 -0
- package/app/utils/filters/rows.ts +367 -0
- package/app/utils/testIds.const.ts +137 -0
- package/i18n/locales/de.ts +419 -2
- package/i18n/locales/en.ts +398 -2
- package/i18n/locales/es.ts +403 -2
- package/i18n/locales/fr.ts +410 -2
- package/i18n/locales/it.ts +409 -2
- package/package.json +4 -4
- package/app.config.ts +0 -5
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { useQuery } from '@pinia/colada'
|
|
2
|
+
import type { MaybeRefOrGetter, Ref } from 'vue'
|
|
3
|
+
import { computed, ref, toValue, watch } from 'vue'
|
|
4
|
+
import type {
|
|
5
|
+
EmailMessage,
|
|
6
|
+
MessageStatus,
|
|
7
|
+
PostEntity,
|
|
8
|
+
RenderType,
|
|
9
|
+
SendableMessage,
|
|
10
|
+
WhatsappMessage,
|
|
11
|
+
} from '@dev.smartpricing/message-composer-utils/types'
|
|
12
|
+
import { compilationApi } from '~/api'
|
|
13
|
+
import { useResolvedParametersQuery } from '~/queries/compilation'
|
|
14
|
+
import { useMessageGroupQuery } from '~/queries/messageGroups'
|
|
15
|
+
import { useTracking } from '~/composables/useTracking'
|
|
16
|
+
|
|
17
|
+
/** Codes a host receives on `error`; the backend ones pass through unchanged. */
|
|
18
|
+
export type MessageSendFlowErrorCode =
|
|
19
|
+
| 'NOT_SENDABLE'
|
|
20
|
+
| 'NOT_OFFERED'
|
|
21
|
+
| 'MISSING_PARAMETERS'
|
|
22
|
+
| 'TEMPLATE_NOT_SUBMITTED'
|
|
23
|
+
| 'TEMPLATE_NOT_APPROVED'
|
|
24
|
+
| 'COMPILE_FAILED'
|
|
25
|
+
|
|
26
|
+
const BACKEND_CODES = new Set<MessageSendFlowErrorCode>([
|
|
27
|
+
'TEMPLATE_NOT_SUBMITTED',
|
|
28
|
+
'TEMPLATE_NOT_APPROVED',
|
|
29
|
+
'MISSING_PARAMETERS',
|
|
30
|
+
])
|
|
31
|
+
|
|
32
|
+
export interface MessageSendFlowError {
|
|
33
|
+
code: MessageSendFlowErrorCode
|
|
34
|
+
/** The layer's translated text, the same string the inline alert shows. */
|
|
35
|
+
message: string
|
|
36
|
+
/** Missing keys (client-side) or the backend error body. */
|
|
37
|
+
details?: unknown
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface MessageSendFlowEmits {
|
|
41
|
+
compiled: [message: SendableMessage]
|
|
42
|
+
cancel: []
|
|
43
|
+
error: [error: MessageSendFlowError]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface MessageSendFlowLanguage {
|
|
47
|
+
code: string
|
|
48
|
+
status: MessageStatus
|
|
49
|
+
approved: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What an operator must fill; `brand_*` keys are baked server-side. */
|
|
53
|
+
export function isOperatorParameter(key: string): boolean {
|
|
54
|
+
return !key.startsWith('brand_')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Data flow of the Message Send Flow (spec #67): Sendable group → approved
|
|
59
|
+
* language → parameters → one `/compile` on Confirm. The component owns the
|
|
60
|
+
* steps and the buttons; the host owns the container and the send.
|
|
61
|
+
*/
|
|
62
|
+
export function useMessageSendFlow(options: {
|
|
63
|
+
groupId: Ref<string | undefined>
|
|
64
|
+
renderType: MaybeRefOrGetter<RenderType>
|
|
65
|
+
preferredLanguage: MaybeRefOrGetter<string | undefined>
|
|
66
|
+
prefill: MaybeRefOrGetter<Record<string, string> | undefined>
|
|
67
|
+
brandId: MaybeRefOrGetter<string | undefined>
|
|
68
|
+
/** The Organization the flow works on; unset, the one the layer resolved. */
|
|
69
|
+
organizationId?: MaybeRefOrGetter<string | undefined>
|
|
70
|
+
/** The WABA the Host sends from: only WhatsApp groups on it are offered (ADR 0015). */
|
|
71
|
+
wabaId?: MaybeRefOrGetter<string | undefined>
|
|
72
|
+
emit: <K extends keyof MessageSendFlowEmits>(event: K, ...args: MessageSendFlowEmits[K]) => void
|
|
73
|
+
}) {
|
|
74
|
+
const { groupId, emit } = options
|
|
75
|
+
const { t } = useI18n()
|
|
76
|
+
const { trackEvent } = useTracking({ organizationId: options.organizationId })
|
|
77
|
+
|
|
78
|
+
// --- Group -------------------------------------------------------------
|
|
79
|
+
const { data: group, isLoading: groupLoading } = useMessageGroupQuery(() => groupId.value ?? '', {
|
|
80
|
+
enabled: () => !!groupId.value,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
const messages = computed(() =>
|
|
84
|
+
(group.value?.messages ?? []).filter((m) => m.render_type === toValue(options.renderType)),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
const languages = computed<MessageSendFlowLanguage[]>(() =>
|
|
88
|
+
messages.value.map((m) => ({
|
|
89
|
+
code: m.language_id,
|
|
90
|
+
status: m.status,
|
|
91
|
+
approved: m.status === 'approved',
|
|
92
|
+
})),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
// A group reaches the flow by id too (the Host's preselection): one of
|
|
96
|
+
// another Organization, or on another WABA, is not the picker's to offer.
|
|
97
|
+
const offered = computed(() => {
|
|
98
|
+
if (!group.value) return true
|
|
99
|
+
const organizationId = toValue(options.organizationId)
|
|
100
|
+
if (
|
|
101
|
+
organizationId &&
|
|
102
|
+
group.value.organization_id?.toLowerCase() !== organizationId.toLowerCase()
|
|
103
|
+
)
|
|
104
|
+
return false
|
|
105
|
+
const wabaId = toValue(options.wabaId)
|
|
106
|
+
return (
|
|
107
|
+
!wabaId || toValue(options.renderType) !== 'whatsapp_v1' || group.value.waba_id === wabaId
|
|
108
|
+
)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const sendable = computed(() => offered.value && languages.value.some((l) => l.approved))
|
|
112
|
+
const notSendable = computed(
|
|
113
|
+
() => !!groupId.value && !!group.value && !groupLoading.value && !sendable.value,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
// --- Language rule: preferred if approved, else first approved -----------
|
|
117
|
+
const selectedLanguage = ref<string>()
|
|
118
|
+
watch(
|
|
119
|
+
[messages, () => toValue(options.preferredLanguage)],
|
|
120
|
+
([msgs, preferred]) => {
|
|
121
|
+
const approved = msgs.filter((m) => m.status === 'approved')
|
|
122
|
+
const match = approved.find((m) => m.language_id === preferred) ?? approved[0]
|
|
123
|
+
selectedLanguage.value = match?.language_id
|
|
124
|
+
},
|
|
125
|
+
{ immediate: true },
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
const selectedMessage = computed(
|
|
129
|
+
() =>
|
|
130
|
+
messages.value.find((m) => m.language_id === selectedLanguage.value) as
|
|
131
|
+
| PostEntity<WhatsappMessage>
|
|
132
|
+
| PostEntity<EmailMessage>
|
|
133
|
+
| undefined,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
// --- Parameters ----------------------------------------------------------
|
|
137
|
+
const parametersEnabled = () => !!groupId.value && !!selectedLanguage.value
|
|
138
|
+
const organization = useEffectiveOrganizationStore()
|
|
139
|
+
|
|
140
|
+
const { data: parametersData, isLoading: parametersLoading } = useQuery({
|
|
141
|
+
key: () => [
|
|
142
|
+
'send-flow',
|
|
143
|
+
'parameters',
|
|
144
|
+
groupId.value ?? '',
|
|
145
|
+
selectedLanguage.value ?? '',
|
|
146
|
+
organization.organizationId,
|
|
147
|
+
],
|
|
148
|
+
query: () => compilationApi.getParameters(groupId.value ?? '', selectedLanguage.value ?? ''),
|
|
149
|
+
enabled: parametersEnabled,
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
// Same query as WhatsappPreview, so the Preview step reuses the cached values.
|
|
153
|
+
const { data: resolvedData } = useResolvedParametersQuery(
|
|
154
|
+
() => groupId.value ?? '',
|
|
155
|
+
() => selectedLanguage.value ?? '',
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
const parameterKeys = computed(() =>
|
|
159
|
+
(parametersData.value?.parameters ?? []).filter(isOperatorParameter),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
/** Organization-level values, shown as input placeholders. */
|
|
163
|
+
const placeholders = computed<Record<string, string>>(() => resolvedData.value?.parameters ?? {})
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* One entry per visible key; prefill seeds new keys, unknown prefill keys
|
|
167
|
+
* are ignored. Rebuilt only once the keys are known, so a language switch
|
|
168
|
+
* keeps what the operator typed while the next list loads.
|
|
169
|
+
*/
|
|
170
|
+
const parameterValues = ref<Record<string, string>>({})
|
|
171
|
+
watch(
|
|
172
|
+
parameterKeys,
|
|
173
|
+
(keys) => {
|
|
174
|
+
if (!parametersData.value) return
|
|
175
|
+
const prefill = toValue(options.prefill) ?? {}
|
|
176
|
+
const next: Record<string, string> = {}
|
|
177
|
+
for (const key of keys) next[key] = parameterValues.value[key] ?? prefill[key] ?? ''
|
|
178
|
+
parameterValues.value = next
|
|
179
|
+
},
|
|
180
|
+
{ immediate: true },
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
const missing = computed(() =>
|
|
184
|
+
parameterKeys.value.filter((k) => !parameterValues.value[k]?.trim()),
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
// --- Errors ---------------------------------------------------------------
|
|
188
|
+
const flowError = ref<MessageSendFlowError>()
|
|
189
|
+
|
|
190
|
+
function fail(code: MessageSendFlowErrorCode, details?: unknown) {
|
|
191
|
+
const error: MessageSendFlowError = {
|
|
192
|
+
code,
|
|
193
|
+
message: t(`send_flow.errors.${code}`, { name: group.value?.name ?? '' }),
|
|
194
|
+
details,
|
|
195
|
+
}
|
|
196
|
+
flowError.value = error
|
|
197
|
+
trackEvent('send_flow.error', { code })
|
|
198
|
+
emit('error', error)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// The reason a picked group cannot enter the flow. Watched with the group,
|
|
202
|
+
// so another group, or another reason for the same one, is reported anew.
|
|
203
|
+
const groupError = computed<MessageSendFlowErrorCode | undefined>(() => {
|
|
204
|
+
if (!notSendable.value) return undefined
|
|
205
|
+
return offered.value ? 'NOT_SENDABLE' : 'NOT_OFFERED'
|
|
206
|
+
})
|
|
207
|
+
watch([groupError, () => group.value?.id], ([code]) => {
|
|
208
|
+
if (code) {
|
|
209
|
+
fail(code)
|
|
210
|
+
} else if (
|
|
211
|
+
flowError.value?.code === 'NOT_SENDABLE' ||
|
|
212
|
+
flowError.value?.code === 'NOT_OFFERED'
|
|
213
|
+
) {
|
|
214
|
+
flowError.value = undefined
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
// --- Compile (once, on Confirm) --------------------------------------------
|
|
219
|
+
const compiling = ref(false)
|
|
220
|
+
const canConfirm = computed(() => sendable.value && !!selectedMessage.value && !compiling.value)
|
|
221
|
+
|
|
222
|
+
async function confirm() {
|
|
223
|
+
flowError.value = undefined
|
|
224
|
+
if (!groupId.value || !selectedLanguage.value || !group.value) return
|
|
225
|
+
|
|
226
|
+
if (missing.value.length > 0) {
|
|
227
|
+
fail('MISSING_PARAMETERS', { missing: missing.value })
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
trackEvent('send_flow.confirm', {
|
|
232
|
+
message_name: group.value.name,
|
|
233
|
+
message_category: group.value.category,
|
|
234
|
+
language: selectedLanguage.value,
|
|
235
|
+
parameters_count: parameterKeys.value.length,
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
compiling.value = true
|
|
239
|
+
try {
|
|
240
|
+
const res = await compilationApi.compile(
|
|
241
|
+
groupId.value,
|
|
242
|
+
{
|
|
243
|
+
language: selectedLanguage.value,
|
|
244
|
+
parameters: { ...parameterValues.value },
|
|
245
|
+
strict: true,
|
|
246
|
+
strict_language: true,
|
|
247
|
+
brand_id: toValue(options.brandId),
|
|
248
|
+
},
|
|
249
|
+
{ skipErrorToast: true },
|
|
250
|
+
)
|
|
251
|
+
if (!('message' in res)) {
|
|
252
|
+
fail('COMPILE_FAILED', res)
|
|
253
|
+
return
|
|
254
|
+
}
|
|
255
|
+
emit('compiled', res.message)
|
|
256
|
+
} catch (e) {
|
|
257
|
+
const { status, data } = e as { status?: number; data?: { code?: string } }
|
|
258
|
+
// 401 reaches the host through `composer:auth-error`; nothing to add here.
|
|
259
|
+
if (status === 401) return
|
|
260
|
+
const code = data?.code as MessageSendFlowErrorCode | undefined
|
|
261
|
+
fail(code && BACKEND_CODES.has(code) ? code : 'COMPILE_FAILED', data ?? e)
|
|
262
|
+
} finally {
|
|
263
|
+
compiling.value = false
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
group,
|
|
269
|
+
groupLoading,
|
|
270
|
+
languages,
|
|
271
|
+
sendable,
|
|
272
|
+
selectedLanguage,
|
|
273
|
+
selectedMessage,
|
|
274
|
+
parameterKeys,
|
|
275
|
+
parametersLoading,
|
|
276
|
+
placeholders,
|
|
277
|
+
parameterValues,
|
|
278
|
+
flowError,
|
|
279
|
+
compiling,
|
|
280
|
+
canConfirm,
|
|
281
|
+
confirm,
|
|
282
|
+
trackEvent,
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter, Ref, WatchSource } from 'vue'
|
|
2
|
+
import type { RenderType } from '@dev.smartpricing/message-composer-utils/types'
|
|
3
|
+
import type { ListMessageGroupsOptions } from '~/api/messageGroups'
|
|
4
|
+
import { useMessageGroupListQuery } from '~/queries/messageGroups'
|
|
5
|
+
|
|
6
|
+
export const SEARCH_DEBOUNCE_MS = 300
|
|
7
|
+
|
|
8
|
+
type PagedListFilters = Omit<
|
|
9
|
+
ListMessageGroupsOptions,
|
|
10
|
+
'search' | 'page' | 'per_page' | 'render_type'
|
|
11
|
+
>
|
|
12
|
+
|
|
13
|
+
export interface PagedMessageGroupsOptions {
|
|
14
|
+
/** Raw search term as typed; it is debounced and trimmed before it reaches the request. */
|
|
15
|
+
search: Ref<string>
|
|
16
|
+
/** Current page. Lands on one whenever the search settles or a `resetOn` source changes. */
|
|
17
|
+
page: Ref<number>
|
|
18
|
+
perPage: number
|
|
19
|
+
renderType: MaybeRefOrGetter<RenderType | undefined>
|
|
20
|
+
/** Remaining filters and sorting, merged into the request as they are. */
|
|
21
|
+
filters?: MaybeRefOrGetter<PagedListFilters>
|
|
22
|
+
/** Further sources whose change lands on page one, besides the search and render type. */
|
|
23
|
+
resetOn?: WatchSource[]
|
|
24
|
+
/** When false no request is made (the query stays pending). */
|
|
25
|
+
enabled?: MaybeRefOrGetter<boolean>
|
|
26
|
+
/** Lists this Organization instead of the resolved one. */
|
|
27
|
+
organizationId?: MaybeRefOrGetter<string | undefined>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* One server-side paginated Message Group list (ADR 0012) as a reactive source:
|
|
32
|
+
* debounced search, page reset, request options and the query itself. The
|
|
33
|
+
* library table and the in-composer picker both build on it and own only what
|
|
34
|
+
* differs (filters, page size, how rows are shown).
|
|
35
|
+
*/
|
|
36
|
+
export function usePagedMessageGroups(options: PagedMessageGroupsOptions) {
|
|
37
|
+
const debouncedSearch = refDebounced(options.search, SEARCH_DEBOUNCE_MS)
|
|
38
|
+
|
|
39
|
+
const listOptions = computed<ListMessageGroupsOptions>(() => ({
|
|
40
|
+
...toValue(options.filters),
|
|
41
|
+
render_type: toValue(options.renderType),
|
|
42
|
+
search: debouncedSearch.value.trim() || undefined,
|
|
43
|
+
page: options.page.value,
|
|
44
|
+
per_page: options.perPage,
|
|
45
|
+
}))
|
|
46
|
+
|
|
47
|
+
const query = useMessageGroupListQuery(listOptions, {
|
|
48
|
+
enabled: options.enabled,
|
|
49
|
+
organizationId: options.organizationId,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
// The reset happens in the same tick as the change (sync flush) so the query
|
|
53
|
+
// sees one key change, not two, and the user never opens an empty page.
|
|
54
|
+
const organization = useEffectiveOrganizationStore()
|
|
55
|
+
watch(
|
|
56
|
+
[
|
|
57
|
+
debouncedSearch,
|
|
58
|
+
() => toValue(options.renderType),
|
|
59
|
+
// Another Organization is another list: its page N may not exist.
|
|
60
|
+
() => toValue(options.organizationId) || organization.resolvedOrganizationId,
|
|
61
|
+
...(options.resetOn ?? []),
|
|
62
|
+
],
|
|
63
|
+
() => {
|
|
64
|
+
options.page.value = 1
|
|
65
|
+
},
|
|
66
|
+
{ flush: 'sync' },
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
debouncedSearch,
|
|
71
|
+
listOptions,
|
|
72
|
+
query,
|
|
73
|
+
rows: computed(() => query.data.value?.data ?? []),
|
|
74
|
+
meta: computed(() => query.data.value?.meta),
|
|
75
|
+
/** True on the first load and while a new page or filter set is fetching over previous data. */
|
|
76
|
+
loading: computed(() => query.isPending.value || query.isPlaceholderData.value),
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
MessageStatus,
|
|
14
14
|
MessageGroup,
|
|
15
15
|
} from '@dev.smartpricing/message-composer-utils/types'
|
|
16
|
+
import { useAppContextStore } from '@/stores/appContext'
|
|
16
17
|
|
|
17
18
|
interface TemplateData<T = Message | PostEntity<Message>> {
|
|
18
19
|
messageGroup: { name: string; category: MessageGroupCategory; brand_id?: string | null }
|
|
@@ -20,13 +21,16 @@ interface TemplateData<T = Message | PostEntity<Message>> {
|
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
interface UseTemplateOperationsOptions {
|
|
23
|
-
|
|
24
|
+
/** Called with the Message Group that was saved, before anything navigates. */
|
|
25
|
+
onSuccess?: (saved?: { id: string; name: string }) => void
|
|
24
26
|
onError?: (error: Error) => void
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
export function useTemplateOperations(options: UseTemplateOperationsOptions = {}) {
|
|
28
30
|
const saving = ref(false)
|
|
29
31
|
const saved = ref(false)
|
|
32
|
+
const appContext = useAppContextStore()
|
|
33
|
+
const organization = useEffectiveOrganizationStore()
|
|
30
34
|
|
|
31
35
|
// Hoist composables to setup scope (they use inject() internally)
|
|
32
36
|
const { mutateAsync: createMessageGroup } = useCreateMessageGroupMutation()
|
|
@@ -39,9 +43,20 @@ export function useTemplateOperations(options: UseTemplateOperationsOptions = {}
|
|
|
39
43
|
async function createTemplate(data: TemplateData, status: MessageStatus = 'draft') {
|
|
40
44
|
saving.value = true
|
|
41
45
|
try {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
)
|
|
46
|
+
// Every Message Group belongs to the context Product (ADR 0011).
|
|
47
|
+
const product = appContext.product
|
|
48
|
+
if (!product) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
'[message-composer] Cannot create a template: no Product in the composer context',
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
const createdMessageGroup = await createMessageGroup({
|
|
54
|
+
...data.messageGroup,
|
|
55
|
+
product,
|
|
56
|
+
// The Effective Organization (ADR 0014): the selected one, or the
|
|
57
|
+
// edited group's on "save as new".
|
|
58
|
+
organization_id: organization.requireOrganizationId(),
|
|
59
|
+
} as PostEntity<MessageGroup>)
|
|
45
60
|
|
|
46
61
|
if (createdMessageGroup?.id) {
|
|
47
62
|
for (const message of Object.values(data.messages)) {
|
|
@@ -53,7 +68,11 @@ export function useTemplateOperations(options: UseTemplateOperationsOptions = {}
|
|
|
53
68
|
}
|
|
54
69
|
|
|
55
70
|
saved.value = true
|
|
56
|
-
options.onSuccess?.(
|
|
71
|
+
options.onSuccess?.(
|
|
72
|
+
createdMessageGroup?.id
|
|
73
|
+
? { id: String(createdMessageGroup.id), name: createdMessageGroup.name }
|
|
74
|
+
: undefined,
|
|
75
|
+
)
|
|
57
76
|
return createdMessageGroup
|
|
58
77
|
} catch (error) {
|
|
59
78
|
options.onError?.(error as Error)
|
|
@@ -87,7 +106,7 @@ export function useTemplateOperations(options: UseTemplateOperationsOptions = {}
|
|
|
87
106
|
}
|
|
88
107
|
|
|
89
108
|
saved.value = true
|
|
90
|
-
options.onSuccess?.()
|
|
109
|
+
options.onSuccess?.({ id, name: data.messageGroup.name })
|
|
91
110
|
} catch (error) {
|
|
92
111
|
options.onError?.(error as Error)
|
|
93
112
|
throw error
|
|
@@ -102,7 +121,12 @@ export function useTemplateOperations(options: UseTemplateOperationsOptions = {}
|
|
|
102
121
|
try {
|
|
103
122
|
const result = await duplicateMessageGroup({
|
|
104
123
|
id: sourceId,
|
|
105
|
-
data:
|
|
124
|
+
data: {
|
|
125
|
+
...(newName && { name: newName }),
|
|
126
|
+
// The Effective Organization (ADR 0014): the selected one, or the
|
|
127
|
+
// edited group's on "save as new".
|
|
128
|
+
organization_id: organization.requireOrganizationId(),
|
|
129
|
+
},
|
|
106
130
|
})
|
|
107
131
|
|
|
108
132
|
saved.value = true
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter } from 'vue'
|
|
1
2
|
import type { TrackingEventMap } from '~/types/tracking'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
/** `organizationId` names the Organization of the events instead of the Effective one. */
|
|
5
|
+
export function useTracking(
|
|
6
|
+
options: { organizationId?: MaybeRefOrGetter<string | undefined> } = {},
|
|
7
|
+
) {
|
|
4
8
|
const { product, mainLanguage, isAdmin } = storeToRefs(useAppContextStore())
|
|
9
|
+
const effectiveOrganization = useEffectiveOrganizationStore()
|
|
10
|
+
const organizationId = computed(
|
|
11
|
+
() => toValue(options.organizationId) || effectiveOrganization.organizationId,
|
|
12
|
+
)
|
|
5
13
|
|
|
6
14
|
function getDefaultProperties(): Record<string, unknown> {
|
|
7
15
|
return {
|
|
@@ -17,12 +25,13 @@ export function useTracking() {
|
|
|
17
25
|
properties?: TrackingEventMap[T],
|
|
18
26
|
): Promise<void> {
|
|
19
27
|
if (!useAppConfig().composer?.tracking) return
|
|
28
|
+
const payload = { ...getDefaultProperties(), ...properties }
|
|
20
29
|
try {
|
|
30
|
+
// The tracking route needs an Organization: an event fired while it is
|
|
31
|
+
// still being resolved (the first page view) waits instead of being lost.
|
|
32
|
+
const organization = await until(organizationId).toBeTruthy()
|
|
21
33
|
const { trackingApi } = await import('@/api/tracking')
|
|
22
|
-
await trackingApi.event(eventName,
|
|
23
|
-
...getDefaultProperties(),
|
|
24
|
-
...properties,
|
|
25
|
-
})
|
|
34
|
+
await trackingApi.event(eventName, organization, payload)
|
|
26
35
|
} catch {
|
|
27
36
|
if (import.meta.dev) {
|
|
28
37
|
console.warn(`[Tracking] Failed to track "${eventName}"`, properties)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* - `unavailable`: the Host's channels do not include WhatsApp
|
|
3
|
+
* - `checking`: the WABA lookup of the Effective Organization is running
|
|
4
|
+
* - `configured`: the Effective Organization has at least one WABA
|
|
5
|
+
* - `not_configured`: the lookup answered, the Organization has no WABA
|
|
6
|
+
* - `failed`: the lookup failed, so whether there is a WABA is not known
|
|
7
|
+
*/
|
|
8
|
+
export type WhatsappConfigurationStatus =
|
|
9
|
+
| 'unavailable'
|
|
10
|
+
| 'checking'
|
|
11
|
+
| 'configured'
|
|
12
|
+
| 'not_configured'
|
|
13
|
+
| 'failed'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether WhatsApp is configured: the Host's channels include WhatsApp and
|
|
17
|
+
* the Effective Organization has at least one WABA (ADR 0015). The WABAs
|
|
18
|
+
* follow the Organization, so the answer changes with it.
|
|
19
|
+
* A lookup still running or failed is never read as "no WABA".
|
|
20
|
+
*/
|
|
21
|
+
export function useWhatsappConfiguration() {
|
|
22
|
+
const { hasWhatsapp } = useChannels()
|
|
23
|
+
const { data, status: lookupStatus } = useOrganizationWabasQuery()
|
|
24
|
+
|
|
25
|
+
const status = computed<WhatsappConfigurationStatus>(() => {
|
|
26
|
+
if (!hasWhatsapp.value) return 'unavailable'
|
|
27
|
+
if (data.value) return data.value.length > 0 ? 'configured' : 'not_configured'
|
|
28
|
+
return lookupStatus.value === 'error' ? 'failed' : 'checking'
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return { status }
|
|
32
|
+
}
|
package/app/layouts/default.vue
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<SNavigationPage :panel-props="{ ui: { body: 'p-0 sm:p-0 gap-0 sm:gap-0 overflow-hidden' } }">
|
|
3
|
-
<
|
|
3
|
+
<CommonAppContextErrors />
|
|
4
|
+
<CommonWhatsappNotConfigured />
|
|
5
|
+
<CommonOrganizationGate>
|
|
6
|
+
<slot />
|
|
7
|
+
</CommonOrganizationGate>
|
|
4
8
|
</SNavigationPage>
|
|
5
9
|
</template>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Automation pages are off until a Host sets `composer.automations`
|
|
3
|
+
* (ADR 0019): their routes always exist, and answer 404 while it is off.
|
|
4
|
+
*/
|
|
5
|
+
export default defineNuxtRouteMiddleware(() => {
|
|
6
|
+
if (useAppConfig().composer?.automations) return
|
|
7
|
+
|
|
8
|
+
// Fatal, or a navigation inside the app would be cancelled without a word.
|
|
9
|
+
return abortNavigation(
|
|
10
|
+
createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true }),
|
|
11
|
+
)
|
|
12
|
+
})
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
definePageMeta({
|
|
3
|
+
name: 'automation-edit',
|
|
4
|
+
middleware: 'automations-enabled',
|
|
5
|
+
})
|
|
6
|
+
|
|
7
|
+
const { t } = useI18n()
|
|
8
|
+
const route = useRoute()
|
|
9
|
+
const gate = useAutomationsGate()
|
|
10
|
+
const { goBackOrFallback } = useGoBack()
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
data: automation,
|
|
14
|
+
error,
|
|
15
|
+
status,
|
|
16
|
+
refetch,
|
|
17
|
+
} = useAutomationQuery(() => String(route.params.id))
|
|
18
|
+
const notFound = computed(() => (error.value as { status?: number } | null)?.status === 404)
|
|
19
|
+
|
|
20
|
+
const edit = useTemplateRef('edit')
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<template>
|
|
24
|
+
<LayoutBasePage :title="t('automations.editTitle')" :test-id="automationsTestIds.editPage">
|
|
25
|
+
<template #actions>
|
|
26
|
+
<UButton
|
|
27
|
+
variant="ghost"
|
|
28
|
+
size="lg"
|
|
29
|
+
:label="t('common.actions.cancel')"
|
|
30
|
+
:data-testid="automationsTestIds.editPageCancelButton"
|
|
31
|
+
@click="goBackOrFallback({ name: 'automations-list' })"
|
|
32
|
+
/>
|
|
33
|
+
<UButton
|
|
34
|
+
v-if="edit"
|
|
35
|
+
size="lg"
|
|
36
|
+
:loading="edit.saving"
|
|
37
|
+
:label="t('common.actions.save')"
|
|
38
|
+
:data-testid="automationsTestIds.editPageCreateButton"
|
|
39
|
+
@click="edit.submit()"
|
|
40
|
+
/>
|
|
41
|
+
</template>
|
|
42
|
+
|
|
43
|
+
<AutomationsPageGate v-slot="{ catalog, context }" :gate="gate">
|
|
44
|
+
<AutomationsEdit
|
|
45
|
+
v-if="automation"
|
|
46
|
+
ref="edit"
|
|
47
|
+
:key="automation.id"
|
|
48
|
+
:catalog="catalog"
|
|
49
|
+
:automation="automation"
|
|
50
|
+
:context="context"
|
|
51
|
+
/>
|
|
52
|
+
<UAlert
|
|
53
|
+
v-else-if="status === 'error'"
|
|
54
|
+
color="error"
|
|
55
|
+
variant="subtle"
|
|
56
|
+
icon="i-lucide-alert-triangle"
|
|
57
|
+
:title="
|
|
58
|
+
notFound ? t('automations.gate.not_found_title') : t('automations.gate.load_failed_title')
|
|
59
|
+
"
|
|
60
|
+
:description="
|
|
61
|
+
notFound
|
|
62
|
+
? t('automations.gate.not_found_description')
|
|
63
|
+
: t('automations.gate.load_failed_description')
|
|
64
|
+
"
|
|
65
|
+
:actions="
|
|
66
|
+
notFound
|
|
67
|
+
? undefined
|
|
68
|
+
: [
|
|
69
|
+
{
|
|
70
|
+
label: t('common.organization.retry'),
|
|
71
|
+
color: 'error',
|
|
72
|
+
variant: 'outline',
|
|
73
|
+
onClick: () => void refetch(),
|
|
74
|
+
},
|
|
75
|
+
]
|
|
76
|
+
"
|
|
77
|
+
/>
|
|
78
|
+
<div
|
|
79
|
+
v-else
|
|
80
|
+
class="flex flex-1 flex-col items-center justify-center gap-3 py-12 text-muted"
|
|
81
|
+
role="status"
|
|
82
|
+
>
|
|
83
|
+
<UIcon name="i-heroicons-arrow-path" class="animate-spin h-8 w-8" />
|
|
84
|
+
</div>
|
|
85
|
+
</AutomationsPageGate>
|
|
86
|
+
</LayoutBasePage>
|
|
87
|
+
</template>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
definePageMeta({
|
|
3
|
+
name: 'automations-list',
|
|
4
|
+
middleware: 'automations-enabled',
|
|
5
|
+
})
|
|
6
|
+
|
|
7
|
+
const { t } = useI18n()
|
|
8
|
+
const gate = useAutomationsGate()
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<template>
|
|
12
|
+
<LayoutBasePage :title="t('automations.title')" :test-id="automationsTestIds.page">
|
|
13
|
+
<template #actions>
|
|
14
|
+
<CommonOrganizationSelect />
|
|
15
|
+
<UButton
|
|
16
|
+
v-if="gate.state === 'ready'"
|
|
17
|
+
icon="ph:plus-bold"
|
|
18
|
+
:label="t('automations.new')"
|
|
19
|
+
:to="{ name: 'automation-create' }"
|
|
20
|
+
:data-testid="automationsTestIds.newButton"
|
|
21
|
+
/>
|
|
22
|
+
</template>
|
|
23
|
+
|
|
24
|
+
<AutomationsPageGate v-slot="{ catalog }" :gate="gate">
|
|
25
|
+
<AutomationsList :catalog="catalog" />
|
|
26
|
+
</AutomationsPageGate>
|
|
27
|
+
</LayoutBasePage>
|
|
28
|
+
</template>
|