@dev.smartpricing/message-composer-layer 4.5.1-next.4 → 4.5.1-next.5
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 +4 -133
- package/app/api/index.ts +0 -1
- package/app/api/messageGroups.ts +1 -5
- package/app/api/meta.ts +36 -0
- package/app/components/Common/MessageGroupSelect.vue +1 -30
- package/app/components/Composer/EmailComposer.vue +1 -5
- package/app/components/Composer/MessageLibrary.vue +8 -0
- package/app/components/Composer/WabaSelectModal.vue +1 -3
- package/app/components/Composer/WhatsappComposer.vue +1 -5
- package/app/components/Layout/BasePage.vue +0 -2
- 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 +22 -1
- package/app/composables/apiErrorMessages.ts +0 -1
- package/app/composables/composerContext.ts +3 -3
- package/app/composables/useMessageGroupWaba.ts +10 -7
- package/app/composables/useTemplateOperations.ts +3 -8
- package/app/pages/index.vue +5 -1
- package/app/pages/template/email/[id].vue +0 -5
- package/app/pages/template/email/create.vue +0 -5
- package/app/pages/template/whatsapp/[id].vue +0 -5
- package/app/pages/template/whatsapp/create.vue +0 -5
- package/app/queries/index.ts +0 -1
- package/app/queries/messageGroups.ts +29 -20
- package/app/queries/meta.ts +91 -1
- package/app/stores/appContext.ts +1 -1
- package/app/types/app-config.d.ts +0 -3
- package/app/types/tracking.ts +0 -33
- package/app/utils/testIds.const.ts +1 -107
- package/app.config.ts +5 -0
- package/i18n/locales/de.ts +64 -269
- package/i18n/locales/en.ts +61 -255
- package/i18n/locales/es.ts +62 -258
- package/i18n/locales/fr.ts +63 -264
- package/i18n/locales/it.ts +65 -260
- package/package.json +4 -4
- package/app/api/automations.ts +0 -74
- package/app/app.config.ts +0 -10
- package/app/components/Automations/ActionCard.vue +0 -174
- package/app/components/Automations/ActionEditor.vue +0 -398
- package/app/components/Automations/Actions.vue +0 -148
- package/app/components/Automations/CloneModal.vue +0 -78
- package/app/components/Automations/Conditions.vue +0 -155
- package/app/components/Automations/Create.vue +0 -114
- package/app/components/Automations/Data.vue +0 -46
- package/app/components/Automations/DeleteModal.vue +0 -80
- package/app/components/Automations/Edit.vue +0 -145
- package/app/components/Automations/Form.vue +0 -282
- package/app/components/Automations/List.vue +0 -370
- package/app/components/Automations/PageGate.vue +0 -50
- package/app/components/Automations/SaveProblems.vue +0 -54
- package/app/components/Automations/Unavailable.vue +0 -71
- package/app/components/Automations/UnavailableBadge.vue +0 -35
- package/app/components/Filters/DateInput.vue +0 -57
- package/app/components/Filters/Editor.vue +0 -163
- package/app/components/Filters/Row.vue +0 -119
- package/app/components/Filters/Summary.vue +0 -215
- package/app/components/Filters/UnavailableRow.vue +0 -74
- package/app/components/Filters/ValueInput.vue +0 -215
- package/app/composables/automationsContext.ts +0 -42
- package/app/composables/useAutomationClone.ts +0 -33
- package/app/composables/useAutomationEditorTrip.ts +0 -77
- package/app/composables/useAutomationSaveProblems.ts +0 -82
- package/app/composables/useAutomationUnavailable.ts +0 -98
- package/app/composables/useAutomationsGate.ts +0 -68
- package/app/composables/useBuiltInOptionLists.ts +0 -44
- package/app/composables/useEditorTripBack.ts +0 -15
- package/app/middleware/automations-enabled.ts +0 -12
- package/app/pages/automations/[id].vue +0 -87
- package/app/pages/automations/index.vue +0 -28
- package/app/pages/automations/new.vue +0 -53
- package/app/queries/automations.ts +0 -206
- package/app/stores/editorTrip.ts +0 -67
- package/app/utils/automations/form.ts +0 -389
- package/app/utils/automations/metadata.ts +0 -71
- package/app/utils/automations/unavailable.ts +0 -90
- package/app/utils/filters/data/countries.json +0 -998
- package/app/utils/filters/data/languages.json +0 -754
- package/app/utils/filters/rows.ts +0 -367
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { useMutation, useQuery, useQueryCache } from '@pinia/colada'
|
|
2
|
-
import {
|
|
3
|
-
AUTOMATION_CATALOG_LANGUAGES,
|
|
4
|
-
type Automation,
|
|
5
|
-
type AutomationCatalogLanguage,
|
|
6
|
-
type CreateAutomationBody,
|
|
7
|
-
} from '@dev.smartpricing/message-composer-utils/types'
|
|
8
|
-
import type { MaybeRefOrGetter } from 'vue'
|
|
9
|
-
import { toValue } from 'vue'
|
|
10
|
-
import { automationsApi, messageGroupsApi } from '~/api'
|
|
11
|
-
import type { AutomationChanges } from '~/utils/automations/form'
|
|
12
|
-
|
|
13
|
-
function isCatalogLanguage(language: string): language is AutomationCatalogLanguage {
|
|
14
|
-
return (AUTOMATION_CATALOG_LANGUAGES as readonly string[]).includes(language)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** The Automations of the Effective Organization for the Host's Product. */
|
|
18
|
-
export function useAutomationsQuery() {
|
|
19
|
-
const { product } = storeToRefs(useAppContextStore())
|
|
20
|
-
const organization = useEffectiveOrganizationStore()
|
|
21
|
-
|
|
22
|
-
return useQuery({
|
|
23
|
-
key: () => ['automations', product.value ?? '', organization.organizationId],
|
|
24
|
-
query: () =>
|
|
25
|
-
automationsApi.getAll({
|
|
26
|
-
organization_id: organization.requireOrganizationId(),
|
|
27
|
-
product: product.value!,
|
|
28
|
-
}),
|
|
29
|
-
enabled: () => Boolean(product.value && organization.organizationId),
|
|
30
|
-
staleTime: 2 * 60 * 1000,
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* One Automation of the Host's Product, read once for the page that edits it:
|
|
36
|
-
* nothing is kept for the next visit, and no refetch replaces what the user
|
|
37
|
-
* is editing. Its key is not under the list's, so a save does not refetch it.
|
|
38
|
-
*/
|
|
39
|
-
export function useAutomationQuery(id: MaybeRefOrGetter<string>) {
|
|
40
|
-
const { product } = storeToRefs(useAppContextStore())
|
|
41
|
-
const organization = useEffectiveOrganizationStore()
|
|
42
|
-
|
|
43
|
-
return useQuery({
|
|
44
|
-
key: () => ['automation', product.value ?? '', organization.organizationId, toValue(id)],
|
|
45
|
-
query: () => automationsApi.getById(toValue(id), { product: product.value! }),
|
|
46
|
-
enabled: () => Boolean(product.value && organization.organizationId),
|
|
47
|
-
staleTime: 0,
|
|
48
|
-
gcTime: 0,
|
|
49
|
-
refetchOnWindowFocus: false,
|
|
50
|
-
refetchOnReconnect: false,
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The Automation Catalog of the Host's Product, with its labels in the
|
|
56
|
-
* language of the interface (ADR 0017).
|
|
57
|
-
*/
|
|
58
|
-
export function useAutomationCatalogQuery() {
|
|
59
|
-
const { product } = storeToRefs(useAppContextStore())
|
|
60
|
-
const organization = useEffectiveOrganizationStore()
|
|
61
|
-
const { locale } = useI18n()
|
|
62
|
-
const language = computed(() => (isCatalogLanguage(locale.value) ? locale.value : 'en'))
|
|
63
|
-
|
|
64
|
-
return useQuery({
|
|
65
|
-
key: () => [
|
|
66
|
-
'automation-catalog',
|
|
67
|
-
product.value ?? '',
|
|
68
|
-
language.value,
|
|
69
|
-
organization.organizationId,
|
|
70
|
-
],
|
|
71
|
-
query: () =>
|
|
72
|
-
automationsApi.getCatalog({
|
|
73
|
-
organization_id: organization.requireOrganizationId(),
|
|
74
|
-
product: product.value!,
|
|
75
|
-
language: language.value,
|
|
76
|
-
}),
|
|
77
|
-
enabled: () => Boolean(product.value && organization.organizationId),
|
|
78
|
-
staleTime: 30 * 60 * 1000,
|
|
79
|
-
})
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** A new Automation names its Organization and its Product (ADR 0014, ADR 0019). */
|
|
83
|
-
export function useCreateAutomationMutation() {
|
|
84
|
-
const queryCache = useQueryCache()
|
|
85
|
-
const { product } = storeToRefs(useAppContextStore())
|
|
86
|
-
const organization = useEffectiveOrganizationStore()
|
|
87
|
-
|
|
88
|
-
return useMutation({
|
|
89
|
-
mutation: (automation: Omit<CreateAutomationBody, 'organization_id' | 'product'>) =>
|
|
90
|
-
automationsApi.create({
|
|
91
|
-
...automation,
|
|
92
|
-
organization_id: organization.requireOrganizationId(),
|
|
93
|
-
product: product.value!,
|
|
94
|
-
}),
|
|
95
|
-
onSuccess: () => {
|
|
96
|
-
queryCache.invalidateQueries({ key: ['automations'] })
|
|
97
|
-
},
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** An update names its Product; the Organization is the stored one (ADR 0016). */
|
|
102
|
-
export function useUpdateAutomationMutation() {
|
|
103
|
-
const queryCache = useQueryCache()
|
|
104
|
-
const { product } = storeToRefs(useAppContextStore())
|
|
105
|
-
|
|
106
|
-
return useMutation({
|
|
107
|
-
mutation: ({ id, ...changes }: AutomationChanges & { id: string }) =>
|
|
108
|
-
automationsApi.update(id, { ...changes, product: product.value! }),
|
|
109
|
-
// After a failure too: an update can be saved in part.
|
|
110
|
-
onSettled: () => {
|
|
111
|
-
queryCache.invalidateQueries({ key: ['automations'] })
|
|
112
|
-
},
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** A clone names its Product; the copy stays in the source's Organization (ADR 0016). */
|
|
117
|
-
export function useCloneAutomationMutation() {
|
|
118
|
-
const queryCache = useQueryCache()
|
|
119
|
-
const { product } = storeToRefs(useAppContextStore())
|
|
120
|
-
|
|
121
|
-
return useMutation({
|
|
122
|
-
mutation: ({ id, name }: { id: string; name: string }) =>
|
|
123
|
-
automationsApi.clone(id, { name, product: product.value! }),
|
|
124
|
-
// The copy comes with Message Groups of its own. After a failure too: it
|
|
125
|
-
// can be made when the answer is lost.
|
|
126
|
-
onSettled: () => {
|
|
127
|
-
queryCache.invalidateQueries({ key: ['automations'] })
|
|
128
|
-
queryCache.invalidateQueries({ key: ['message-groups'] })
|
|
129
|
-
},
|
|
130
|
-
})
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* The Message Groups the Actions of an Automation use, each once, by their
|
|
135
|
-
* current name. One that cannot be read (deleted, or of an Organization the
|
|
136
|
-
* user cannot reach) is left out: it could not be deleted either.
|
|
137
|
-
*/
|
|
138
|
-
export function useAutomationMessageGroupsQuery(automation: MaybeRefOrGetter<Automation>) {
|
|
139
|
-
const ids = computed(() => [
|
|
140
|
-
...new Set(toValue(automation).actions.map((action) => action.config.template.toLowerCase())),
|
|
141
|
-
])
|
|
142
|
-
|
|
143
|
-
return useQuery({
|
|
144
|
-
key: () => ['automation-message-groups', toValue(automation).id, ...ids.value],
|
|
145
|
-
query: async () => {
|
|
146
|
-
const results = await Promise.allSettled(
|
|
147
|
-
ids.value.map((id) => messageGroupsApi.getById(id, { skipErrorToast: true })),
|
|
148
|
-
)
|
|
149
|
-
return results.flatMap((result) =>
|
|
150
|
-
result.status === 'fulfilled' ? [{ id: result.value.id, name: result.value.name }] : [],
|
|
151
|
-
)
|
|
152
|
-
},
|
|
153
|
-
staleTime: 0,
|
|
154
|
-
gcTime: 0,
|
|
155
|
-
})
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Whether each of these Message Groups is approved in every language, by its
|
|
160
|
-
* id in lower case. One that cannot be read is left out: it is not known to
|
|
161
|
-
* be unapproved. Under `message-groups`, so a change to any of them asks again.
|
|
162
|
-
*/
|
|
163
|
-
export function useMessageGroupApprovalsQuery(messageGroupIds: MaybeRefOrGetter<string[]>) {
|
|
164
|
-
const ids = computed(() =>
|
|
165
|
-
[...new Set(toValue(messageGroupIds).map((id) => id.toLowerCase()))].filter(Boolean).sort(),
|
|
166
|
-
)
|
|
167
|
-
|
|
168
|
-
return useQuery({
|
|
169
|
-
key: () => ['message-groups', 'approvals', ...ids.value],
|
|
170
|
-
query: async () => {
|
|
171
|
-
const requested = ids.value
|
|
172
|
-
const results = await Promise.allSettled(
|
|
173
|
-
requested.map((id) => messageGroupsApi.getById(id, { skipErrorToast: true })),
|
|
174
|
-
)
|
|
175
|
-
return new Map(
|
|
176
|
-
results.flatMap((result, index) => {
|
|
177
|
-
if (result.status !== 'fulfilled') return []
|
|
178
|
-
const { messages } = result.value
|
|
179
|
-
const approved =
|
|
180
|
-
messages.length > 0 && messages.every((message) => message.status === 'approved')
|
|
181
|
-
return [[requested[index]!, approved] as const]
|
|
182
|
-
}),
|
|
183
|
-
)
|
|
184
|
-
},
|
|
185
|
-
enabled: () => ids.value.length > 0,
|
|
186
|
-
// The set changes with every Action added or re-pointed: what was known stays while it loads.
|
|
187
|
-
placeholderData: (previous) => previous,
|
|
188
|
-
staleTime: 2 * 60 * 1000,
|
|
189
|
-
})
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/** A delete names its Product, and the Message Groups that go with the Automation (ADR 0016). */
|
|
193
|
-
export function useDeleteAutomationMutation() {
|
|
194
|
-
const queryCache = useQueryCache()
|
|
195
|
-
const { product } = storeToRefs(useAppContextStore())
|
|
196
|
-
|
|
197
|
-
return useMutation({
|
|
198
|
-
mutation: ({ id, messageGroupIds }: { id: string; messageGroupIds: string[] }) =>
|
|
199
|
-
automationsApi.delete(id, { product: product.value!, message_group_ids: messageGroupIds }),
|
|
200
|
-
// After a failure too: the Automation can be gone when the answer is lost.
|
|
201
|
-
onSettled: (_result, _error, { messageGroupIds }) => {
|
|
202
|
-
queryCache.invalidateQueries({ key: ['automations'] })
|
|
203
|
-
if (messageGroupIds.length > 0) queryCache.invalidateQueries({ key: ['message-groups'] })
|
|
204
|
-
},
|
|
205
|
-
})
|
|
206
|
-
}
|
package/app/stores/editorTrip.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/** The Message Group an editor saved. */
|
|
2
|
-
export interface EditorTripResult {
|
|
3
|
-
id: string
|
|
4
|
-
name: string
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface EditorTrip<Kept = unknown> {
|
|
8
|
-
/** Where the trip started: the route it returns to, and the back button's label, already translated. */
|
|
9
|
-
origin: { routeName: string; backLabel: string }
|
|
10
|
-
/** What the opener wants back on its return. The editor never reads it. */
|
|
11
|
-
kept: Kept
|
|
12
|
-
/** The last Message Group saved during the trip; none until a save. */
|
|
13
|
-
result?: EditorTripResult
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** The pages a trip goes through, by Channel. */
|
|
17
|
-
export const EDITOR_ROUTES = {
|
|
18
|
-
email: { create: 'email-message-create', edit: 'email-message-edit' },
|
|
19
|
-
whatsapp: { create: 'whatsapp-message-create', edit: 'whatsapp-message-edit' },
|
|
20
|
-
} as const
|
|
21
|
-
|
|
22
|
-
const EDITOR_ROUTE_NAMES: ReadonlySet<string> = new Set(
|
|
23
|
-
Object.values(EDITOR_ROUTES).flatMap((routes) => Object.values(routes)),
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* A round trip from a page to the Message editor and back. Layer-internal and
|
|
28
|
-
* in memory only: not Host contract, and a reload loses it.
|
|
29
|
-
*
|
|
30
|
-
* The opener starts it and takes it back on its return. The editor knows a
|
|
31
|
-
* trip, not who opened it: it shows the trip's back button and records what
|
|
32
|
-
* it saves.
|
|
33
|
-
*/
|
|
34
|
-
export const useEditorTripStore = defineStore('editorTrip', () => {
|
|
35
|
-
const trip = shallowRef<EditorTrip>()
|
|
36
|
-
|
|
37
|
-
// A trip ends where the user leaves its path: an editor opened later, from
|
|
38
|
-
// the library, is not part of it.
|
|
39
|
-
useRouter().afterEach((to, _from, failure) => {
|
|
40
|
-
if (failure || !trip.value) return
|
|
41
|
-
const name = String(to.name)
|
|
42
|
-
if (name === trip.value.origin.routeName || EDITOR_ROUTE_NAMES.has(name)) return
|
|
43
|
-
trip.value = undefined
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
function start<Kept>(origin: EditorTrip['origin'], kept: Kept) {
|
|
47
|
-
trip.value = { origin, kept }
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** The last Message Group saved wins, also if the user then cancels. */
|
|
51
|
-
function recordSaved(saved: EditorTripResult) {
|
|
52
|
-
if (trip.value) trip.value = { ...trip.value, result: saved }
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** The trip that started on this route, handed over once. */
|
|
56
|
-
function consume<Kept>(routeName: string): EditorTrip<Kept> | undefined {
|
|
57
|
-
const current = trip.value
|
|
58
|
-
if (current?.origin.routeName !== routeName) return undefined
|
|
59
|
-
trip.value = undefined
|
|
60
|
-
return current as EditorTrip<Kept>
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** The label of the editor's back button; none without a trip. */
|
|
64
|
-
const backLabel = computed(() => trip.value?.origin.backLabel)
|
|
65
|
-
|
|
66
|
-
return { start, recordSaved, consume, backLabel }
|
|
67
|
-
})
|
|
@@ -1,389 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Automation,
|
|
3
|
-
AutomationAction,
|
|
4
|
-
AutomationActionConfig,
|
|
5
|
-
AutomationActionInput,
|
|
6
|
-
AutomationActionStatus,
|
|
7
|
-
AutomationChannel,
|
|
8
|
-
AutomationEngineRejectedDetails,
|
|
9
|
-
AutomationPartlySavedDetails,
|
|
10
|
-
AutomationSaveProblem,
|
|
11
|
-
AutomationTrigger,
|
|
12
|
-
Condition,
|
|
13
|
-
CreateAutomationBody,
|
|
14
|
-
DateReferenceDirection,
|
|
15
|
-
UpdateAutomationBody,
|
|
16
|
-
} from '@dev.smartpricing/message-composer-utils/types'
|
|
17
|
-
import { automationMetadataForm, metadataFormValue } from './metadata'
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The Automation form's own model and what it turns into on a save. The
|
|
21
|
-
* engine's shapes (a delay in signed seconds, `template` for a Message Group)
|
|
22
|
-
* stay in here.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
export const SECONDS_PER_DAY = 24 * 60 * 60
|
|
26
|
-
|
|
27
|
-
export const DEFAULT_SENDING_TIME = '09:00:00Z'
|
|
28
|
-
|
|
29
|
-
export const AUTOMATION_RECURRENCES = ['daily', 'monthly', 'yearly'] as const
|
|
30
|
-
|
|
31
|
-
export type AutomationRecurrence = (typeof AUTOMATION_RECURRENCES)[number]
|
|
32
|
-
|
|
33
|
-
export interface AutomationFormAction {
|
|
34
|
-
/**
|
|
35
|
-
* What the form tells its Actions apart by. It is the layer's alone: an
|
|
36
|
-
* Action that is not saved yet has no `id`, and this is never sent as one.
|
|
37
|
-
*/
|
|
38
|
-
key: string
|
|
39
|
-
/** The id of the stored Action; none while the Action is not saved yet. */
|
|
40
|
-
id?: string
|
|
41
|
-
/**
|
|
42
|
-
* The `config` of the stored Action. The form shows only part of it: the
|
|
43
|
-
* rest goes back as it is on a save.
|
|
44
|
-
*/
|
|
45
|
-
storedConfig?: AutomationActionConfig
|
|
46
|
-
status: AutomationActionStatus
|
|
47
|
-
channel: AutomationChannel
|
|
48
|
-
messageGroupId: string
|
|
49
|
-
messageGroupName: string
|
|
50
|
-
/** The key of the Date Reference the delay counts from. */
|
|
51
|
-
dateReference: string
|
|
52
|
-
direction: DateReferenceDirection
|
|
53
|
-
/** The delay, in whole days before or after the Date Reference. */
|
|
54
|
-
days: number
|
|
55
|
-
sendingTimeUTC: string
|
|
56
|
-
recurrence?: AutomationRecurrence
|
|
57
|
-
strictDelay: boolean
|
|
58
|
-
preventDuplicates: boolean
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export interface AutomationFormState {
|
|
62
|
-
name: string
|
|
63
|
-
triggerType: string
|
|
64
|
-
conditions: Condition[]
|
|
65
|
-
actions: AutomationFormAction[]
|
|
66
|
-
/** The values the Automation asks for (`metadata.form`), by the key of their field. */
|
|
67
|
-
data: Record<string, string>
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* A copy the form can change freely. The form's state is plain data, and it
|
|
72
|
-
* can hold Vue proxies, which `structuredClone` refuses.
|
|
73
|
-
*/
|
|
74
|
-
export function cloneFormData<T>(value: T): T {
|
|
75
|
-
return JSON.parse(JSON.stringify(value)) as T
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** A new Automation starts from its Trigger's default Conditions (ADR 0017). */
|
|
79
|
-
export function newAutomationForm(trigger: AutomationTrigger): AutomationFormState {
|
|
80
|
-
return {
|
|
81
|
-
name: '',
|
|
82
|
-
triggerType: trigger.type,
|
|
83
|
-
conditions: cloneFormData(trigger.default_conditions),
|
|
84
|
-
actions: [],
|
|
85
|
-
data: {},
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function newAutomationFormAction(
|
|
90
|
-
trigger: AutomationTrigger,
|
|
91
|
-
channel: AutomationChannel,
|
|
92
|
-
): AutomationFormAction {
|
|
93
|
-
const dateReference = trigger.date_references[0]
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
key: crypto.randomUUID(),
|
|
97
|
-
status: 'active',
|
|
98
|
-
channel,
|
|
99
|
-
messageGroupId: '',
|
|
100
|
-
messageGroupName: '',
|
|
101
|
-
dateReference: dateReference?.key ?? '',
|
|
102
|
-
direction: dateReference?.directions[0]?.direction ?? 'after',
|
|
103
|
-
days: 0,
|
|
104
|
-
sendingTimeUTC: DEFAULT_SENDING_TIME,
|
|
105
|
-
strictDelay: false,
|
|
106
|
-
preventDuplicates: true,
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** The one Trigger the form edits: the Automation Engine can hold several, the pages write one. */
|
|
111
|
-
function triggerTypeOf(automation: Automation): string {
|
|
112
|
-
return [automation.settings.trigger.type].flat()[0] ?? ''
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** What the form shows of an Action's `config`. */
|
|
116
|
-
type AutomationFormSchedule = Omit<AutomationFormAction, 'key' | 'id' | 'storedConfig' | 'status'>
|
|
117
|
-
|
|
118
|
-
function toFormSchedule(config: AutomationActionConfig): AutomationFormSchedule {
|
|
119
|
-
const delay = typeof config.delay === 'number' ? config.delay : 0
|
|
120
|
-
// The sign of the delay is its direction; `delayDirection` keeps it at 0.
|
|
121
|
-
const direction =
|
|
122
|
-
delay < 0 || (delay === 0 && config.delayDirection === 'before') ? 'before' : 'after'
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
channel: config.channel as AutomationChannel,
|
|
126
|
-
messageGroupId: config.template,
|
|
127
|
-
messageGroupName: typeof config.templateName === 'string' ? config.templateName : '',
|
|
128
|
-
dateReference: typeof config.dateTimeReference === 'string' ? config.dateTimeReference : '',
|
|
129
|
-
direction,
|
|
130
|
-
days: Math.abs(delay) / SECONDS_PER_DAY,
|
|
131
|
-
sendingTimeUTC:
|
|
132
|
-
typeof config.sendingTimeUTC === 'string' ? config.sendingTimeUTC : DEFAULT_SENDING_TIME,
|
|
133
|
-
recurrence: AUTOMATION_RECURRENCES.find((recurrence) => recurrence === config.recurrence),
|
|
134
|
-
strictDelay: config.strictDelay === true,
|
|
135
|
-
preventDuplicates: config.preventDuplicates === true,
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function toFormAction({ id, status, config }: AutomationAction): AutomationFormAction {
|
|
140
|
-
return { key: id, id, storedConfig: config, status, ...toFormSchedule(config) }
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** A stored Automation in the form. Its Conditions come as they are (ADR 0018). */
|
|
144
|
-
export function toAutomationForm(automation: Automation): AutomationFormState {
|
|
145
|
-
return cloneFormData({
|
|
146
|
-
name: automation.name,
|
|
147
|
-
triggerType: triggerTypeOf(automation),
|
|
148
|
-
conditions: automation.settings.conditions as Condition[],
|
|
149
|
-
actions: automation.actions.map(toFormAction),
|
|
150
|
-
data: Object.fromEntries(
|
|
151
|
-
(automationMetadataForm(automation.metadata)?.fields ?? []).map(({ key }) => [
|
|
152
|
-
key,
|
|
153
|
-
metadataFormValue(automation.metadata, key),
|
|
154
|
-
]),
|
|
155
|
-
),
|
|
156
|
-
})
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** "before:checkinDate": a Date Reference and the direction of the delay, as one select value. */
|
|
160
|
-
export function dateReferenceValue(reference: {
|
|
161
|
-
direction: DateReferenceDirection
|
|
162
|
-
dateReference: string
|
|
163
|
-
}): string {
|
|
164
|
-
return `${reference.direction}:${reference.dateReference}`
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export function parseDateReferenceValue(value: string): {
|
|
168
|
-
direction: DateReferenceDirection
|
|
169
|
-
dateReference: string
|
|
170
|
-
} {
|
|
171
|
-
const separator = value.indexOf(':')
|
|
172
|
-
return {
|
|
173
|
-
direction: value.slice(0, separator) === 'before' ? 'before' : 'after',
|
|
174
|
-
dateReference: value.slice(separator + 1),
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** The label of an Action's Date Reference; its key when the Trigger no longer lists it. */
|
|
179
|
-
export function dateReferenceLabel(action: AutomationFormAction, trigger: AutomationTrigger) {
|
|
180
|
-
return (
|
|
181
|
-
trigger.date_references
|
|
182
|
-
.find((reference) => reference.key === action.dateReference)
|
|
183
|
-
?.directions.find((entry) => entry.direction === action.direction)?.label ??
|
|
184
|
-
action.dateReference
|
|
185
|
-
)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/** The sending time on the user's clock: the engine keeps it in UTC. */
|
|
189
|
-
export function localSendingTime(sendingTimeUTC: string): string {
|
|
190
|
-
return new Date(`2000-01-01T${sendingTimeUTC}`).toLocaleTimeString([], {
|
|
191
|
-
hour: '2-digit',
|
|
192
|
-
minute: '2-digit',
|
|
193
|
-
hour12: false,
|
|
194
|
-
})
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function toActionConfig(action: AutomationFormSchedule): AutomationActionInput['config'] {
|
|
198
|
-
// The sign of the delay is its direction; `delayDirection` keeps it at 0.
|
|
199
|
-
const delay =
|
|
200
|
-
action.direction === 'before' ? -action.days * SECONDS_PER_DAY : action.days * SECONDS_PER_DAY
|
|
201
|
-
|
|
202
|
-
return {
|
|
203
|
-
template: action.messageGroupId,
|
|
204
|
-
templateName: action.messageGroupName,
|
|
205
|
-
channel: action.channel,
|
|
206
|
-
delay: delay || 0,
|
|
207
|
-
dateTimeReference: action.dateReference,
|
|
208
|
-
delayDirection: action.days === 0 ? action.direction : undefined,
|
|
209
|
-
sendingTimeUTC: action.sendingTimeUTC,
|
|
210
|
-
recurrence: action.recurrence,
|
|
211
|
-
strictDelay: action.strictDelay || undefined,
|
|
212
|
-
preventDuplicates: action.preventDuplicates,
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* A stored Action goes back with its `config` as stored, and over it only
|
|
218
|
-
* what the user changed: the form shows a default where the `config` has no
|
|
219
|
-
* value (no sending time, no name), and a default written back would change
|
|
220
|
-
* an Action the user did not touch.
|
|
221
|
-
*/
|
|
222
|
-
function toActionInput(action: AutomationFormAction): AutomationActionInput {
|
|
223
|
-
const config = toActionConfig(action)
|
|
224
|
-
const { storedConfig } = action
|
|
225
|
-
if (!storedConfig) return { status: action.status, config }
|
|
226
|
-
|
|
227
|
-
const unchanged = toActionConfig(toFormSchedule(storedConfig))
|
|
228
|
-
const changes = Object.fromEntries(
|
|
229
|
-
Object.entries(config).filter(([key, value]) => value !== unchanged[key]),
|
|
230
|
-
)
|
|
231
|
-
return {
|
|
232
|
-
status: action.status,
|
|
233
|
-
config: {
|
|
234
|
-
...storedConfig,
|
|
235
|
-
channel: config.channel,
|
|
236
|
-
dateTimeReference: config.dateTimeReference,
|
|
237
|
-
...changes,
|
|
238
|
-
},
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/** A create without its scope: the mutation names the Organization and the Product. */
|
|
243
|
-
export type NewAutomation = Omit<CreateAutomationBody, 'organization_id' | 'product'>
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* The body of a create. No recipient, sender or trigger source: the backend
|
|
247
|
-
* writes them (ADR 0017).
|
|
248
|
-
*/
|
|
249
|
-
export function toCreateAutomationBody(form: AutomationFormState): NewAutomation {
|
|
250
|
-
return {
|
|
251
|
-
name: form.name.trim(),
|
|
252
|
-
settings: {
|
|
253
|
-
trigger: { type: form.triggerType },
|
|
254
|
-
conditions: form.conditions,
|
|
255
|
-
},
|
|
256
|
-
actions: form.actions.map(toActionInput),
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/** An update without its Product: the mutation names it, and the Automation by its id. */
|
|
261
|
-
export type AutomationChanges = Omit<UpdateAutomationBody, 'product'>
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* The body of an update of `stored`: every Action the form holds, a stored
|
|
265
|
-
* one with its `id` and a new one without (ADR 0016). What `settings` holds
|
|
266
|
-
* that the form does not show goes back as stored. Of `metadata` only the
|
|
267
|
-
* asked-for values the user changed are sent: the backend merges them into
|
|
268
|
-
* the stored one, and keeps the rest.
|
|
269
|
-
*/
|
|
270
|
-
export function toUpdateAutomationBody(
|
|
271
|
-
form: AutomationFormState,
|
|
272
|
-
stored: Automation,
|
|
273
|
-
): AutomationChanges {
|
|
274
|
-
const sameTrigger = triggerTypeOf(stored) === form.triggerType
|
|
275
|
-
const changedData = Object.entries(form.data).filter(
|
|
276
|
-
([key, value]) => value !== metadataFormValue(stored.metadata, key),
|
|
277
|
-
)
|
|
278
|
-
|
|
279
|
-
return {
|
|
280
|
-
name: form.name.trim(),
|
|
281
|
-
...(changedData.length > 0 && { metadata: Object.fromEntries(changedData) }),
|
|
282
|
-
settings: {
|
|
283
|
-
...stored.settings,
|
|
284
|
-
trigger: { ...(sameTrigger && stored.settings.trigger), type: form.triggerType },
|
|
285
|
-
conditions: form.conditions,
|
|
286
|
-
},
|
|
287
|
-
actions: form.actions.map((action) => ({ ...toActionInput(action), id: action.id })),
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* The properties of `Automation setup`, as Smartconnect's pages send them.
|
|
293
|
-
* `trigger` is the form's, when the Automation Catalog still lists it.
|
|
294
|
-
*/
|
|
295
|
-
export function automationSetupProperties(
|
|
296
|
-
form: AutomationFormState,
|
|
297
|
-
trigger: AutomationTrigger | undefined,
|
|
298
|
-
) {
|
|
299
|
-
const action = form.actions[0]
|
|
300
|
-
const dateReference = action?.dateReference ?? ''
|
|
301
|
-
|
|
302
|
-
return {
|
|
303
|
-
// The backend writes the sender: this is the template it will write.
|
|
304
|
-
automation_sender:
|
|
305
|
-
trigger?.channels.find((entry) => entry.channel === action?.channel)?.sender ?? '',
|
|
306
|
-
automation_name: form.name.trim(),
|
|
307
|
-
automation_trigger: form.triggerType,
|
|
308
|
-
automation_type: dateReference,
|
|
309
|
-
automation_rules_audience: form.conditions.length > 0 ? 'segmented' : 'all',
|
|
310
|
-
automation_subject: action?.messageGroupName ?? '',
|
|
311
|
-
automation_moment: dateReference,
|
|
312
|
-
automation_channel: action?.channel ?? '',
|
|
313
|
-
automation_status: action?.status ?? '',
|
|
314
|
-
automation_days: action?.days ?? 0,
|
|
315
|
-
created_by: 'user',
|
|
316
|
-
} as const
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* The `problems` of a refused save (422 `AUTOMATION_ACTIONS_INVALID`), or
|
|
321
|
-
* `undefined` for any other error. The entries are not checked one by one: a
|
|
322
|
-
* client skips a `target` or a `reason` it does not know.
|
|
323
|
-
*/
|
|
324
|
-
export function automationSaveProblems(error: unknown): AutomationSaveProblem[] | undefined {
|
|
325
|
-
const data = (error as { data?: { code?: unknown; details?: { problems?: unknown } } } | null)
|
|
326
|
-
?.data
|
|
327
|
-
if (data?.code !== 'AUTOMATION_ACTIONS_INVALID') return undefined
|
|
328
|
-
|
|
329
|
-
const problems = data.details?.problems
|
|
330
|
-
return Array.isArray(problems) ? (problems as AutomationSaveProblem[]) : []
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Why the Automation Engine refused a request (422
|
|
335
|
-
* `AUTOMATION_ENGINE_REJECTED`), in the engine's own words, or `undefined`
|
|
336
|
-
* for any other error.
|
|
337
|
-
*/
|
|
338
|
-
export function automationEngineRejected(error: unknown): string | undefined {
|
|
339
|
-
const data = (
|
|
340
|
-
error as {
|
|
341
|
-
data?: { code?: unknown; details?: Partial<AutomationEngineRejectedDetails> }
|
|
342
|
-
} | null
|
|
343
|
-
)?.data
|
|
344
|
-
if (data?.code !== 'AUTOMATION_ENGINE_REJECTED') return undefined
|
|
345
|
-
|
|
346
|
-
const { message = '', errors } = data.details ?? {}
|
|
347
|
-
const reasons = Array.isArray(errors)
|
|
348
|
-
? errors.map(({ field, message }) => `${field}: ${message}`)
|
|
349
|
-
: []
|
|
350
|
-
return reasons.length > 0 ? reasons.join('; ') : message
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* What the Automation Engine holds after an update that was saved only in
|
|
355
|
-
* part (502 `UPSTREAM_ERROR` with the current state, ADR 0016), or
|
|
356
|
-
* `undefined` for any other error.
|
|
357
|
-
*/
|
|
358
|
-
export function automationPartlySaved(error: unknown): AutomationPartlySavedDetails | undefined {
|
|
359
|
-
const data = (
|
|
360
|
-
error as {
|
|
361
|
-
data?: { code?: unknown; details?: Partial<AutomationPartlySavedDetails> }
|
|
362
|
-
} | null
|
|
363
|
-
)?.data
|
|
364
|
-
const details = data?.details
|
|
365
|
-
if (data?.code !== 'UPSTREAM_ERROR') return undefined
|
|
366
|
-
if (!details?.automation?.id || !Array.isArray(details.action_ids)) return undefined
|
|
367
|
-
|
|
368
|
-
return details as AutomationPartlySavedDetails
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* The form's Actions after an update that was saved only in part. Each
|
|
373
|
-
* Action of `sent`, the list that was submitted, takes the id and the stored
|
|
374
|
-
* `config` the Automation Engine now has for it, so that saving again creates
|
|
375
|
-
* nothing twice. What the user sees and typed is not touched.
|
|
376
|
-
*/
|
|
377
|
-
export function withPartlySavedActions(
|
|
378
|
-
actions: AutomationFormAction[],
|
|
379
|
-
sent: AutomationFormAction[],
|
|
380
|
-
{ automation, action_ids }: AutomationPartlySavedDetails,
|
|
381
|
-
): AutomationFormAction[] {
|
|
382
|
-
const idByKey = new Map(sent.map((action, position) => [action.key, action_ids[position]]))
|
|
383
|
-
|
|
384
|
-
return actions.map((action) => {
|
|
385
|
-
const id = idByKey.get(action.key)
|
|
386
|
-
const stored = automation.actions.find((candidate) => candidate.id === id)
|
|
387
|
-
return stored ? { ...action, id: stored.id, storedConfig: stored.config } : action
|
|
388
|
-
})
|
|
389
|
-
}
|