@infuro/cms-core 1.0.45 → 1.0.46
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 +19 -2
- package/dist/admin.cjs +873 -214
- package/dist/admin.d.cts +7 -1
- package/dist/admin.d.ts +7 -1
- package/dist/admin.js +874 -215
- package/dist/api.cjs +40 -37
- package/dist/api.d.cts +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/api.js +8 -5
- package/dist/auth.cjs +23 -23
- package/dist/auth.d.cts +1 -0
- package/dist/auth.d.ts +1 -0
- package/dist/auth.js +2 -2
- package/dist/chunk-2KUCQVAQ.js +936 -0
- package/dist/chunk-3K5AIEIZ.js +256 -0
- package/dist/{chunk-I6NH72MO.js → chunk-57O3HZPG.js} +9 -1
- package/dist/chunk-7PMHZRF3.cjs +872 -0
- package/dist/chunk-BXYZDMTZ.cjs +953 -0
- package/dist/chunk-CQHXW4TR.js +107 -0
- package/dist/{chunk-NNQBWDCI.js → chunk-CRYKVJTO.js} +55 -959
- package/dist/chunk-DBPSJYLZ.js +47 -0
- package/dist/{chunk-TAHX46EI.cjs → chunk-FBKDMRQL.cjs} +9 -1
- package/dist/chunk-GUSHM5HN.js +862 -0
- package/dist/chunk-HY3AXLOI.cjs +265 -0
- package/dist/chunk-L3525VXI.js +243 -0
- package/dist/{chunk-ZMRQ72F6.cjs → chunk-NBY4NVTY.cjs} +3 -3
- package/dist/chunk-OY2YTBMP.cjs +50 -0
- package/dist/{chunk-BLR6H5GL.js → chunk-SF2XKMCI.js} +3 -3
- package/dist/{chunk-22FFHLTO.cjs → chunk-TCSGFAWB.cjs} +56 -970
- package/dist/{chunk-F5XFHYTG.cjs → chunk-UDVLFVEC.cjs} +661 -382
- package/dist/{chunk-MVXLRANV.js → chunk-UKC3HYXI.js} +591 -323
- package/dist/chunk-V25RDUOU.cjs +248 -0
- package/dist/chunk-XMRMZ6NQ.cjs +109 -0
- package/dist/cli.cjs +9 -4
- package/dist/cli.js +9 -4
- package/dist/{email-queue-TPZWLTIV.cjs → email-queue-6NJY6HNM.cjs} +7 -5
- package/dist/email-queue-OI2HSCGE.js +4 -0
- package/dist/erp-order-invoice-3GXDE443.js +3 -0
- package/dist/erp-order-invoice-EYYNR526.cjs +24 -0
- package/dist/index.cjs +391 -364
- package/dist/index.d.cts +267 -14
- package/dist/index.d.ts +267 -14
- package/dist/index.js +12 -110
- package/dist/migrations/1781810000000-OrderItemsVariantId.ts +44 -0
- package/dist/migrations/1781900000000-UserInviteStatusToken.ts +46 -0
- package/dist/migrations/1782000000000-ComboSlug.ts +58 -0
- package/dist/migrations/1782100000000-DiscountDeviceType.ts +20 -0
- package/dist/order-inventory-2MYRKEPC.cjs +39 -0
- package/dist/order-inventory-76V4XIGW.js +2 -0
- package/dist/order-notification-dispatcher-SBQAMM5V.cjs +21 -0
- package/dist/{order-notification-dispatcher-XWIJYDGA.js → order-notification-dispatcher-ZJZB2HUN.js} +5 -2
- package/dist/retire-soft-deleted-unique-5VXBA5XX.cjs +15 -0
- package/dist/retire-soft-deleted-unique-NXQIH4BC.js +2 -0
- package/dist/send-order-placed-emails-B5ZVJT7T.cjs +15 -0
- package/dist/send-order-placed-emails-WPI37KRZ.js +6 -0
- package/package.json +5 -4
- package/src/admin/admin.css +27 -24
- package/dist/chunk-5ELSW2UP.js +0 -150
- package/dist/chunk-PNOKNSG2.cjs +0 -222
- package/dist/chunk-WEMDMVHQ.js +0 -216
- package/dist/chunk-XW5ATPRW.cjs +0 -155
- package/dist/email-queue-CGZBVVAS.js +0 -2
- package/dist/erp-order-invoice-3YISSOWW.js +0 -3
- package/dist/erp-order-invoice-ZR5F2KTA.cjs +0 -12
- package/dist/order-notification-dispatcher-RU7BTGGX.cjs +0 -18
|
@@ -0,0 +1,862 @@
|
|
|
1
|
+
import { renderLayout } from './chunk-CRYKVJTO.js';
|
|
2
|
+
import { notificationTriggerEmitter } from './chunk-HCIRL37O.js';
|
|
3
|
+
import { queueEmail } from './chunk-L3525VXI.js';
|
|
4
|
+
import { mergeEmailLayoutCompanyDetails } from './chunk-2KUCQVAQ.js';
|
|
5
|
+
import { applyTemplateVars } from './chunk-CRFV5WJK.js';
|
|
6
|
+
import { __name } from './chunk-SHUYVCID.js';
|
|
7
|
+
|
|
8
|
+
// src/lib/event-order-template-context.ts
|
|
9
|
+
function resolvePublicSiteUrl() {
|
|
10
|
+
if (typeof process !== "undefined") {
|
|
11
|
+
const u = (process.env.CMS_PUBLIC_URL || process.env.NEXT_PUBLIC_SITE_URL || "").trim();
|
|
12
|
+
if (u) return u.replace(/\/+$/, "");
|
|
13
|
+
const v = (process.env.VERCEL_URL || "").trim();
|
|
14
|
+
if (v) return v.startsWith("http") ? v.replace(/\/+$/, "") : `https://${v.replace(/\/+$/, "")}`;
|
|
15
|
+
}
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
__name(resolvePublicSiteUrl, "resolvePublicSiteUrl");
|
|
19
|
+
function buildOrderTrackUrl(qrToken, siteUrl) {
|
|
20
|
+
const token = qrToken?.trim();
|
|
21
|
+
if (!token) return "";
|
|
22
|
+
const base = (siteUrl ?? resolvePublicSiteUrl()).replace(/\/+$/, "");
|
|
23
|
+
if (!base) return `/track/${token}`;
|
|
24
|
+
return `${base}/track/${token}`;
|
|
25
|
+
}
|
|
26
|
+
__name(buildOrderTrackUrl, "buildOrderTrackUrl");
|
|
27
|
+
function buildOrderQrImageUrl(trackUrl, size = 200) {
|
|
28
|
+
if (!trackUrl.trim()) return "";
|
|
29
|
+
return `https://api.qrserver.com/v1/create-qr-code/?size=${size}x${size}&data=${encodeURIComponent(trackUrl)}`;
|
|
30
|
+
}
|
|
31
|
+
__name(buildOrderQrImageUrl, "buildOrderQrImageUrl");
|
|
32
|
+
function formatOrderDetailsText(input) {
|
|
33
|
+
const { orderNumber, total, currency, lines } = input;
|
|
34
|
+
const header = `Order: ${orderNumber}
|
|
35
|
+
Total: ${total} ${currency}`;
|
|
36
|
+
if (!lines.length) return header;
|
|
37
|
+
const items = lines.map((line) => `- ${line.name} \xD7 ${line.quantity} (${line.total} ${line.currency})`).join("\n");
|
|
38
|
+
return `${header}
|
|
39
|
+
|
|
40
|
+
Items:
|
|
41
|
+
${items}`;
|
|
42
|
+
}
|
|
43
|
+
__name(formatOrderDetailsText, "formatOrderDetailsText");
|
|
44
|
+
function formatOrderDetailsHtml(input) {
|
|
45
|
+
const text = formatOrderDetailsText(input);
|
|
46
|
+
const escaped = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
47
|
+
return `<pre style="font-family:inherit;white-space:pre-wrap;margin:0">${escaped}</pre>`;
|
|
48
|
+
}
|
|
49
|
+
__name(formatOrderDetailsHtml, "formatOrderDetailsHtml");
|
|
50
|
+
function buildEventOrderTemplateVariables(input) {
|
|
51
|
+
const { event, order, contact, productNames, orderLines = [], siteUrl } = input;
|
|
52
|
+
const trackUrl = buildOrderTrackUrl(order.qrToken, siteUrl);
|
|
53
|
+
const qrImageUrl = buildOrderQrImageUrl(trackUrl);
|
|
54
|
+
const orderDetailsPayload = {
|
|
55
|
+
orderNumber: String(order.orderNumber ?? ""),
|
|
56
|
+
total: String(order.total ?? ""),
|
|
57
|
+
currency: String(order.currency ?? "INR"),
|
|
58
|
+
lines: orderLines
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
eventName: event.name ?? "",
|
|
62
|
+
eventSlug: event.slug ?? "",
|
|
63
|
+
venue: event.venue ?? "",
|
|
64
|
+
startDate: formatEventDate(event.startDate),
|
|
65
|
+
orderNumber: String(order.orderNumber ?? ""),
|
|
66
|
+
orderTotal: String(order.total ?? ""),
|
|
67
|
+
currency: String(order.currency ?? "INR"),
|
|
68
|
+
customerName: contact.name?.trim() || contact.email?.split("@")[0] || "Guest",
|
|
69
|
+
customerEmail: contact.email?.trim() || "",
|
|
70
|
+
customerPhone: contact.phone?.trim() || "",
|
|
71
|
+
productNames,
|
|
72
|
+
trackUrl,
|
|
73
|
+
qrImageUrl,
|
|
74
|
+
orderDetails: formatOrderDetailsText(orderDetailsPayload),
|
|
75
|
+
orderDetailsHtml: formatOrderDetailsHtml(orderDetailsPayload)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
__name(buildEventOrderTemplateVariables, "buildEventOrderTemplateVariables");
|
|
79
|
+
function formatEventDate(d) {
|
|
80
|
+
if (!d) return "";
|
|
81
|
+
try {
|
|
82
|
+
return new Date(d).toLocaleString(void 0, {
|
|
83
|
+
dateStyle: "medium",
|
|
84
|
+
timeStyle: "short"
|
|
85
|
+
});
|
|
86
|
+
} catch {
|
|
87
|
+
return String(d);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
__name(formatEventDate, "formatEventDate");
|
|
91
|
+
|
|
92
|
+
// src/lib/order-notification-context.ts
|
|
93
|
+
async function loadOrderLines(dataSource, entityMap, orderId, currency) {
|
|
94
|
+
if (!entityMap.order_items) return [];
|
|
95
|
+
const itemRows = await dataSource.getRepository(entityMap.order_items).find({
|
|
96
|
+
where: {
|
|
97
|
+
orderId
|
|
98
|
+
},
|
|
99
|
+
relations: [
|
|
100
|
+
"product"
|
|
101
|
+
],
|
|
102
|
+
order: {
|
|
103
|
+
id: "ASC"
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return itemRows.map((item) => {
|
|
107
|
+
const row = item;
|
|
108
|
+
const name = row.product?.name || row.product?.title || `Product #${row.productId}`;
|
|
109
|
+
return {
|
|
110
|
+
name: String(name),
|
|
111
|
+
quantity: Number(row.quantity) || 1,
|
|
112
|
+
total: row.total,
|
|
113
|
+
currency
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
__name(loadOrderLines, "loadOrderLines");
|
|
118
|
+
async function loadPrimaryEvent(dataSource, entityMap, itemRows) {
|
|
119
|
+
if (!entityMap.event_products || !entityMap.events) {
|
|
120
|
+
return {
|
|
121
|
+
productNames: []
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const epRepo = dataSource.getRepository(entityMap.event_products);
|
|
125
|
+
const eventRepo = dataSource.getRepository(entityMap.events);
|
|
126
|
+
const eventMap = /* @__PURE__ */ new Map();
|
|
127
|
+
for (const item of itemRows) {
|
|
128
|
+
const productId = item.productId;
|
|
129
|
+
const productName = item.product?.name || item.product?.title || `Product #${productId}`;
|
|
130
|
+
const eps = await epRepo.find({
|
|
131
|
+
where: {
|
|
132
|
+
productId,
|
|
133
|
+
isActive: true
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
for (const ep of eps) {
|
|
137
|
+
const eventId = ep.eventId;
|
|
138
|
+
const existing = eventMap.get(eventId);
|
|
139
|
+
if (existing) {
|
|
140
|
+
if (!existing.productNames.includes(String(productName))) {
|
|
141
|
+
existing.productNames.push(String(productName));
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
const event = await eventRepo.findOne({
|
|
145
|
+
where: {
|
|
146
|
+
id: eventId,
|
|
147
|
+
deleted: false
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
if (event) {
|
|
151
|
+
eventMap.set(eventId, {
|
|
152
|
+
event,
|
|
153
|
+
productNames: [
|
|
154
|
+
String(productName)
|
|
155
|
+
]
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const first = eventMap.values().next().value;
|
|
162
|
+
if (!first) return {
|
|
163
|
+
productNames: []
|
|
164
|
+
};
|
|
165
|
+
return {
|
|
166
|
+
event: first.event,
|
|
167
|
+
productNames: first.productNames
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
__name(loadPrimaryEvent, "loadPrimaryEvent");
|
|
171
|
+
async function loadOrderNotificationContexts(orderId, deps) {
|
|
172
|
+
if (!deps.entityMap.orders) return null;
|
|
173
|
+
const order = await deps.dataSource.getRepository(deps.entityMap.orders).findOne({
|
|
174
|
+
where: {
|
|
175
|
+
id: orderId,
|
|
176
|
+
deleted: false
|
|
177
|
+
},
|
|
178
|
+
relations: [
|
|
179
|
+
"contact"
|
|
180
|
+
]
|
|
181
|
+
});
|
|
182
|
+
if (!order) return null;
|
|
183
|
+
const o = order;
|
|
184
|
+
const contact = o.contact ?? {};
|
|
185
|
+
const currency = String(o.currency ?? "INR");
|
|
186
|
+
const siteUrl = resolvePublicSiteUrl();
|
|
187
|
+
const itemRows = await deps.dataSource.getRepository(deps.entityMap.order_items).find({
|
|
188
|
+
where: {
|
|
189
|
+
orderId
|
|
190
|
+
},
|
|
191
|
+
relations: [
|
|
192
|
+
"product"
|
|
193
|
+
],
|
|
194
|
+
order: {
|
|
195
|
+
id: "ASC"
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
const orderLines = await loadOrderLines(deps.dataSource, deps.entityMap, orderId, currency);
|
|
199
|
+
const { event, productNames: eventProductNames } = await loadPrimaryEvent(deps.dataSource, deps.entityMap, itemRows);
|
|
200
|
+
const allProductNames = orderLines.map((line) => line.name);
|
|
201
|
+
const productNames = eventProductNames.length > 0 ? eventProductNames.join(", ") : allProductNames.join(", ");
|
|
202
|
+
const variables = event ? buildEventOrderTemplateVariables({
|
|
203
|
+
event,
|
|
204
|
+
order: {
|
|
205
|
+
orderNumber: o.orderNumber,
|
|
206
|
+
total: o.total,
|
|
207
|
+
currency,
|
|
208
|
+
qrToken: o.qrToken
|
|
209
|
+
},
|
|
210
|
+
contact,
|
|
211
|
+
productNames,
|
|
212
|
+
orderLines,
|
|
213
|
+
siteUrl
|
|
214
|
+
}) : buildEventOrderTemplateVariables({
|
|
215
|
+
event: {
|
|
216
|
+
name: productNames || "Your order",
|
|
217
|
+
slug: "",
|
|
218
|
+
venue: "",
|
|
219
|
+
startDate: null
|
|
220
|
+
},
|
|
221
|
+
order: {
|
|
222
|
+
orderNumber: o.orderNumber,
|
|
223
|
+
total: o.total,
|
|
224
|
+
currency,
|
|
225
|
+
qrToken: o.qrToken
|
|
226
|
+
},
|
|
227
|
+
contact,
|
|
228
|
+
productNames,
|
|
229
|
+
orderLines,
|
|
230
|
+
siteUrl
|
|
231
|
+
});
|
|
232
|
+
return {
|
|
233
|
+
order: {
|
|
234
|
+
id: o.id,
|
|
235
|
+
orderNumber: String(o.orderNumber ?? ""),
|
|
236
|
+
total: o.total,
|
|
237
|
+
currency,
|
|
238
|
+
qrToken: o.qrToken
|
|
239
|
+
},
|
|
240
|
+
contact,
|
|
241
|
+
orderLines,
|
|
242
|
+
productNames,
|
|
243
|
+
event,
|
|
244
|
+
variables
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
__name(loadOrderNotificationContexts, "loadOrderNotificationContexts");
|
|
248
|
+
|
|
249
|
+
// src/plugins/whatsapp/whatsapp-queue.ts
|
|
250
|
+
var WHATSAPP_QUEUE_NAME = "whatsapp";
|
|
251
|
+
function maskPhone(raw) {
|
|
252
|
+
const digits = raw.replace(/\D/g, "");
|
|
253
|
+
if (digits.length <= 4) return "****";
|
|
254
|
+
return `${"*".repeat(Math.max(0, digits.length - 4))}${digits.slice(-4)}`;
|
|
255
|
+
}
|
|
256
|
+
__name(maskPhone, "maskPhone");
|
|
257
|
+
function registerWhatsAppQueueProcessor(cms) {
|
|
258
|
+
const queue = cms.getPlugin("queue");
|
|
259
|
+
const whatsapp = cms.getPlugin("whatsapp");
|
|
260
|
+
if (!queue) {
|
|
261
|
+
console.warn("[whatsapp] queue processor not registered: queue plugin missing");
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (!whatsapp || typeof whatsapp.send !== "function") {
|
|
265
|
+
console.warn("[whatsapp] queue processor not registered: whatsapp plugin missing or not configured");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
queue.registerProcessor(WHATSAPP_QUEUE_NAME, async (data) => {
|
|
269
|
+
const payload = data;
|
|
270
|
+
if (!payload.to) {
|
|
271
|
+
console.warn("[whatsapp] queue job skipped: missing recipient");
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
console.log("[whatsapp] queue job processing", {
|
|
275
|
+
to: maskPhone(payload.to),
|
|
276
|
+
templateKey: payload.templateKey ?? null,
|
|
277
|
+
externalTemplateRef: payload.externalTemplateRef ?? null
|
|
278
|
+
});
|
|
279
|
+
try {
|
|
280
|
+
const ok = await whatsapp.send(payload);
|
|
281
|
+
if (ok) {
|
|
282
|
+
console.log("[whatsapp] queue job completed", {
|
|
283
|
+
to: maskPhone(payload.to),
|
|
284
|
+
sent: true
|
|
285
|
+
});
|
|
286
|
+
} else {
|
|
287
|
+
console.warn("[whatsapp] queue job completed without delivery", {
|
|
288
|
+
to: maskPhone(payload.to),
|
|
289
|
+
sent: false
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
} catch (err) {
|
|
293
|
+
console.error("[whatsapp] queue job failed", {
|
|
294
|
+
to: maskPhone(payload.to),
|
|
295
|
+
error: err instanceof Error ? err.message : String(err)
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
console.log("[whatsapp] queue processor registered", {
|
|
300
|
+
queue: WHATSAPP_QUEUE_NAME
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
__name(registerWhatsAppQueueProcessor, "registerWhatsAppQueueProcessor");
|
|
304
|
+
async function queueWhatsApp(cms, payload) {
|
|
305
|
+
const queue = cms.getPlugin("queue");
|
|
306
|
+
if (queue) {
|
|
307
|
+
console.log("[whatsapp] enqueue", {
|
|
308
|
+
to: maskPhone(payload.to),
|
|
309
|
+
templateKey: payload.templateKey ?? null,
|
|
310
|
+
externalTemplateRef: payload.externalTemplateRef ?? null
|
|
311
|
+
});
|
|
312
|
+
await queue.add(WHATSAPP_QUEUE_NAME, payload);
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
const whatsapp = cms.getPlugin("whatsapp");
|
|
316
|
+
if (whatsapp && typeof whatsapp.send === "function") {
|
|
317
|
+
console.log("[whatsapp] no queue plugin \u2014 sending immediately", {
|
|
318
|
+
to: maskPhone(payload.to)
|
|
319
|
+
});
|
|
320
|
+
return whatsapp.send(payload);
|
|
321
|
+
}
|
|
322
|
+
console.warn("[whatsapp] send skipped: neither queue nor whatsapp plugin available", {
|
|
323
|
+
to: maskPhone(payload.to)
|
|
324
|
+
});
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
__name(queueWhatsApp, "queueWhatsApp");
|
|
328
|
+
|
|
329
|
+
// src/plugins/whatsapp/whatsapp-service.ts
|
|
330
|
+
function norm(v) {
|
|
331
|
+
const t = v?.trim();
|
|
332
|
+
return t || void 0;
|
|
333
|
+
}
|
|
334
|
+
__name(norm, "norm");
|
|
335
|
+
function pick(plugin, db, env, ...keys) {
|
|
336
|
+
for (const key of keys) {
|
|
337
|
+
const pk = key;
|
|
338
|
+
if (pk in plugin && plugin[pk] !== void 0 && String(plugin[pk]).trim() !== "") {
|
|
339
|
+
const v = norm(String(plugin[pk]));
|
|
340
|
+
if (v) return v;
|
|
341
|
+
}
|
|
342
|
+
if (norm(db[key])) return norm(db[key]);
|
|
343
|
+
if (norm(env[key])) return norm(env[key]);
|
|
344
|
+
}
|
|
345
|
+
return void 0;
|
|
346
|
+
}
|
|
347
|
+
__name(pick, "pick");
|
|
348
|
+
function mergeWhatsAppConfigLayers(env, db, plugin) {
|
|
349
|
+
return {
|
|
350
|
+
accessToken: pick(plugin, db, env, "accessToken", "WHATSAPP_ACCESS_TOKEN"),
|
|
351
|
+
phoneNumberId: pick(plugin, db, env, "phoneNumberId", "WHATSAPP_PHONE_NUMBER_ID"),
|
|
352
|
+
apiVersion: pick(plugin, db, env, "apiVersion", "WHATSAPP_API_VERSION") ?? "v21.0"
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
__name(mergeWhatsAppConfigLayers, "mergeWhatsAppConfigLayers");
|
|
356
|
+
function isWhatsAppPluginEnabled(db) {
|
|
357
|
+
const v = (db.enabled ?? "true").toLowerCase();
|
|
358
|
+
return v !== "false" && v !== "0";
|
|
359
|
+
}
|
|
360
|
+
__name(isWhatsAppPluginEnabled, "isWhatsAppPluginEnabled");
|
|
361
|
+
function whatsAppConfigured(config) {
|
|
362
|
+
return Boolean(config.accessToken?.trim() && config.phoneNumberId?.trim());
|
|
363
|
+
}
|
|
364
|
+
__name(whatsAppConfigured, "whatsAppConfigured");
|
|
365
|
+
function normalizeWhatsAppRecipient(to) {
|
|
366
|
+
const digits = to.replace(/\D/g, "");
|
|
367
|
+
return digits;
|
|
368
|
+
}
|
|
369
|
+
__name(normalizeWhatsAppRecipient, "normalizeWhatsAppRecipient");
|
|
370
|
+
function resolveMetaWhatsAppTemplateName(opts) {
|
|
371
|
+
const ref = opts.externalTemplateRef?.trim();
|
|
372
|
+
const rowName = opts.name?.trim();
|
|
373
|
+
if (ref && !/^\d+$/.test(ref)) return ref;
|
|
374
|
+
if (rowName) return rowName;
|
|
375
|
+
return ref || void 0;
|
|
376
|
+
}
|
|
377
|
+
__name(resolveMetaWhatsAppTemplateName, "resolveMetaWhatsAppTemplateName");
|
|
378
|
+
function maskPhone2(digits) {
|
|
379
|
+
if (digits.length <= 4) return "****";
|
|
380
|
+
return `${"*".repeat(Math.max(0, digits.length - 4))}${digits.slice(-4)}`;
|
|
381
|
+
}
|
|
382
|
+
__name(maskPhone2, "maskPhone");
|
|
383
|
+
function logWhatsApp(level, message, extra) {
|
|
384
|
+
const line = extra ? `${message} ${JSON.stringify(extra)}` : message;
|
|
385
|
+
if (level === "info") console.log(`[whatsapp] ${line}`);
|
|
386
|
+
else if (level === "warn") console.warn(`[whatsapp] ${line}`);
|
|
387
|
+
else console.error(`[whatsapp] ${line}`);
|
|
388
|
+
}
|
|
389
|
+
__name(logWhatsApp, "logWhatsApp");
|
|
390
|
+
var WhatsAppService = class {
|
|
391
|
+
static {
|
|
392
|
+
__name(this, "WhatsAppService");
|
|
393
|
+
}
|
|
394
|
+
getWhatsAppSettings;
|
|
395
|
+
config;
|
|
396
|
+
getMessageTemplateRow;
|
|
397
|
+
constructor(env, plugin = {}, getWhatsAppSettings, getMessageTemplateRow) {
|
|
398
|
+
this.getWhatsAppSettings = getWhatsAppSettings;
|
|
399
|
+
this.getMessageTemplateRow = getMessageTemplateRow;
|
|
400
|
+
this.config = mergeWhatsAppConfigLayers(env, {}, plugin);
|
|
401
|
+
}
|
|
402
|
+
async mergedConfig() {
|
|
403
|
+
let db = {};
|
|
404
|
+
try {
|
|
405
|
+
db = await this.getWhatsAppSettings?.() ?? {};
|
|
406
|
+
} catch {
|
|
407
|
+
}
|
|
408
|
+
return mergeWhatsAppConfigLayers((typeof process !== "undefined" ? process.env : {}) ?? {}, db, this.config);
|
|
409
|
+
}
|
|
410
|
+
async send(opts) {
|
|
411
|
+
const cfg = await this.mergedConfig();
|
|
412
|
+
if (!whatsAppConfigured(cfg)) {
|
|
413
|
+
logWhatsApp("warn", "send skipped: not configured (Plugins \u2192 WhatsApp or WHATSAPP_ACCESS_TOKEN + WHATSAPP_PHONE_NUMBER_ID)");
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
const to = normalizeWhatsAppRecipient(opts.to);
|
|
417
|
+
if (!to) {
|
|
418
|
+
logWhatsApp("warn", "send skipped: invalid recipient", {
|
|
419
|
+
rawTo: opts.to
|
|
420
|
+
});
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
logWhatsApp("info", "send started", {
|
|
424
|
+
to: maskPhone2(to),
|
|
425
|
+
templateKey: opts.templateKey ?? null,
|
|
426
|
+
externalTemplateRef: opts.externalTemplateRef ?? null,
|
|
427
|
+
phoneNumberId: cfg.phoneNumberId
|
|
428
|
+
});
|
|
429
|
+
let templateName = resolveMetaWhatsAppTemplateName({
|
|
430
|
+
externalTemplateRef: opts.externalTemplateRef
|
|
431
|
+
});
|
|
432
|
+
let templateLanguage = opts.templateLanguage?.trim();
|
|
433
|
+
let body = opts.body?.trim() ?? "";
|
|
434
|
+
const variables = opts.variables ?? {};
|
|
435
|
+
if (opts.templateKey?.trim() && this.getMessageTemplateRow) {
|
|
436
|
+
const { resolveEventOrderTemplate, EVENT_ORDER_TEMPLATE_KEY } = await import('./event-order-defaults-SDRNAZHC.js');
|
|
437
|
+
if (opts.templateKey.trim() === EVENT_ORDER_TEMPLATE_KEY) {
|
|
438
|
+
const resolved = await resolveEventOrderTemplate("whatsapp", async (ch, key) => {
|
|
439
|
+
const row = await this.getMessageTemplateRow(ch, key);
|
|
440
|
+
if (!row) return null;
|
|
441
|
+
return {
|
|
442
|
+
subject: null,
|
|
443
|
+
body: row.body,
|
|
444
|
+
externalTemplateRef: row.externalTemplateRef,
|
|
445
|
+
enabled: row.enabled
|
|
446
|
+
};
|
|
447
|
+
});
|
|
448
|
+
if (resolved && !resolved.enabled) {
|
|
449
|
+
logWhatsApp("warn", "send skipped: template disabled in message_templates", {
|
|
450
|
+
templateKey: opts.templateKey
|
|
451
|
+
});
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
if (resolved) {
|
|
455
|
+
body = resolved.body;
|
|
456
|
+
if (resolved.externalTemplateRef) templateName = resolved.externalTemplateRef;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
const { applyTemplateVars: applyTemplateVars2 } = await import('./interpolate-WDUWDF3I.js');
|
|
461
|
+
if (body) body = applyTemplateVars2(body, variables);
|
|
462
|
+
if (templateName) {
|
|
463
|
+
const paramValues = opts.templateParamOrder?.length ? opts.templateParamOrder.map((key) => String(variables[key] ?? "").slice(0, 1024)) : Object.keys(variables).length ? Object.values(variables) : body ? [
|
|
464
|
+
body
|
|
465
|
+
] : [];
|
|
466
|
+
logWhatsApp("info", "sending template message", {
|
|
467
|
+
to: maskPhone2(to),
|
|
468
|
+
templateName,
|
|
469
|
+
paramCount: paramValues.length,
|
|
470
|
+
paramOrder: opts.templateParamOrder ?? null
|
|
471
|
+
});
|
|
472
|
+
return this.sendTemplateMessage(cfg, to, templateName, paramValues, templateLanguage);
|
|
473
|
+
}
|
|
474
|
+
if (!body) {
|
|
475
|
+
logWhatsApp("warn", "send skipped: no template name and empty body", {
|
|
476
|
+
to: maskPhone2(to)
|
|
477
|
+
});
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
logWhatsApp("info", "sending text message", {
|
|
481
|
+
to: maskPhone2(to),
|
|
482
|
+
bodyLength: body.length
|
|
483
|
+
});
|
|
484
|
+
return this.sendTextMessage(cfg, to, body);
|
|
485
|
+
}
|
|
486
|
+
async sendTextMessage(cfg, to, body) {
|
|
487
|
+
const url = `https://graph.facebook.com/${cfg.apiVersion}/${cfg.phoneNumberId}/messages`;
|
|
488
|
+
const res = await fetch(url, {
|
|
489
|
+
method: "POST",
|
|
490
|
+
headers: {
|
|
491
|
+
Authorization: `Bearer ${cfg.accessToken}`,
|
|
492
|
+
"Content-Type": "application/json"
|
|
493
|
+
},
|
|
494
|
+
body: JSON.stringify({
|
|
495
|
+
messaging_product: "whatsapp",
|
|
496
|
+
to,
|
|
497
|
+
type: "text",
|
|
498
|
+
text: {
|
|
499
|
+
body
|
|
500
|
+
}
|
|
501
|
+
})
|
|
502
|
+
});
|
|
503
|
+
if (!res.ok) {
|
|
504
|
+
const errText = await res.text().catch(() => "");
|
|
505
|
+
logWhatsApp("error", "text send failed", {
|
|
506
|
+
status: res.status,
|
|
507
|
+
to: maskPhone2(to),
|
|
508
|
+
error: errText
|
|
509
|
+
});
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
const data = await res.json().catch(() => ({}));
|
|
513
|
+
logWhatsApp("info", "text send succeeded", {
|
|
514
|
+
to: maskPhone2(to),
|
|
515
|
+
messageId: data.messages?.[0]?.id ?? null
|
|
516
|
+
});
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
519
|
+
async sendTemplateMessage(cfg, to, templateName, paramValues, templateLanguage) {
|
|
520
|
+
const components = paramValues.length > 0 ? [
|
|
521
|
+
{
|
|
522
|
+
type: "body",
|
|
523
|
+
parameters: paramValues.map((text) => ({
|
|
524
|
+
type: "text",
|
|
525
|
+
text: String(text).slice(0, 1024)
|
|
526
|
+
}))
|
|
527
|
+
}
|
|
528
|
+
] : void 0;
|
|
529
|
+
const url = `https://graph.facebook.com/${cfg.apiVersion}/${cfg.phoneNumberId}/messages`;
|
|
530
|
+
const payload = {
|
|
531
|
+
messaging_product: "whatsapp",
|
|
532
|
+
to,
|
|
533
|
+
type: "template",
|
|
534
|
+
template: {
|
|
535
|
+
name: templateName,
|
|
536
|
+
language: {
|
|
537
|
+
code: templateLanguage?.trim() || "en"
|
|
538
|
+
},
|
|
539
|
+
...components ? {
|
|
540
|
+
components
|
|
541
|
+
} : {}
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
const res = await fetch(url, {
|
|
545
|
+
method: "POST",
|
|
546
|
+
headers: {
|
|
547
|
+
Authorization: `Bearer ${cfg.accessToken}`,
|
|
548
|
+
"Content-Type": "application/json"
|
|
549
|
+
},
|
|
550
|
+
body: JSON.stringify(payload)
|
|
551
|
+
});
|
|
552
|
+
if (!res.ok) {
|
|
553
|
+
const errText = await res.text().catch(() => "");
|
|
554
|
+
logWhatsApp("error", "template send failed", {
|
|
555
|
+
status: res.status,
|
|
556
|
+
to: maskPhone2(to),
|
|
557
|
+
templateName,
|
|
558
|
+
paramCount: paramValues.length,
|
|
559
|
+
error: errText
|
|
560
|
+
});
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
const data = await res.json().catch(() => ({}));
|
|
564
|
+
logWhatsApp("info", "template send succeeded", {
|
|
565
|
+
to: maskPhone2(to),
|
|
566
|
+
templateName,
|
|
567
|
+
messageId: data.messages?.[0]?.id ?? null
|
|
568
|
+
});
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
// src/lib/order-notification-dispatcher.ts
|
|
574
|
+
var initialized = false;
|
|
575
|
+
function maskPhone3(raw) {
|
|
576
|
+
const digits = String(raw ?? "").replace(/\D/g, "");
|
|
577
|
+
if (digits.length <= 4) return "(none)";
|
|
578
|
+
return `***${digits.slice(-4)}`;
|
|
579
|
+
}
|
|
580
|
+
__name(maskPhone3, "maskPhone");
|
|
581
|
+
async function getSettingsGroup(dataSource, entityMap, group) {
|
|
582
|
+
if (!entityMap.configs) return {};
|
|
583
|
+
const rows = await dataSource.getRepository(entityMap.configs).find({
|
|
584
|
+
where: {
|
|
585
|
+
settings: group,
|
|
586
|
+
deleted: false
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
return Object.fromEntries(rows.map((r) => [
|
|
590
|
+
r.key,
|
|
591
|
+
r.value
|
|
592
|
+
]));
|
|
593
|
+
}
|
|
594
|
+
__name(getSettingsGroup, "getSettingsGroup");
|
|
595
|
+
async function isOrderNotificationsEnabled(dataSource, entityMap) {
|
|
596
|
+
const settings = await getSettingsGroup(dataSource, entityMap, "event_notifications");
|
|
597
|
+
return settings.enabled !== "false" && settings.enabled !== "0";
|
|
598
|
+
}
|
|
599
|
+
__name(isOrderNotificationsEnabled, "isOrderNotificationsEnabled");
|
|
600
|
+
async function isTriggerCatalogEnabled(triggerKey, dataSource, entityMap) {
|
|
601
|
+
if (!entityMap.order_notification_triggers) {
|
|
602
|
+
console.warn("[order-notifications] order_notification_triggers missing \u2014 run migration 1781230000000-AddOrderNotificationTriggers");
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
try {
|
|
606
|
+
const row = await dataSource.getRepository(entityMap.order_notification_triggers).findOne({
|
|
607
|
+
where: {
|
|
608
|
+
triggerKey,
|
|
609
|
+
enabled: true
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
if (!row) {
|
|
613
|
+
console.warn("[order-notifications] trigger disabled or not in DB", {
|
|
614
|
+
triggerKey
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
return Boolean(row);
|
|
618
|
+
} catch (err) {
|
|
619
|
+
console.error("[order-notifications] failed to load trigger catalog", {
|
|
620
|
+
triggerKey,
|
|
621
|
+
err
|
|
622
|
+
});
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
__name(isTriggerCatalogEnabled, "isTriggerCatalogEnabled");
|
|
627
|
+
async function loadBoundTemplate(triggerKey, channel, dataSource, entityMap) {
|
|
628
|
+
if (!entityMap.order_notification_bindings || !entityMap.message_templates) {
|
|
629
|
+
console.warn("[order-notifications] order_notification_bindings or message_templates missing from entityMap");
|
|
630
|
+
return null;
|
|
631
|
+
}
|
|
632
|
+
const binding = await dataSource.getRepository(entityMap.order_notification_bindings).findOne({
|
|
633
|
+
where: {
|
|
634
|
+
triggerKey,
|
|
635
|
+
channel,
|
|
636
|
+
enabled: true
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
if (!binding) {
|
|
640
|
+
console.warn("[order-notifications] no order_notification_bindings row", {
|
|
641
|
+
triggerKey,
|
|
642
|
+
channel
|
|
643
|
+
});
|
|
644
|
+
return null;
|
|
645
|
+
}
|
|
646
|
+
const templateId = binding.messageTemplateId;
|
|
647
|
+
if (templateId == null) {
|
|
648
|
+
console.warn("[order-notifications] binding exists but no template selected", {
|
|
649
|
+
triggerKey,
|
|
650
|
+
channel
|
|
651
|
+
});
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
const template = await dataSource.getRepository(entityMap.message_templates).findOne({
|
|
655
|
+
where: {
|
|
656
|
+
id: templateId,
|
|
657
|
+
channel,
|
|
658
|
+
deleted: false,
|
|
659
|
+
enabled: true
|
|
660
|
+
}
|
|
661
|
+
});
|
|
662
|
+
if (!template) {
|
|
663
|
+
console.warn("[order-notifications] bound template not found or disabled", {
|
|
664
|
+
triggerKey,
|
|
665
|
+
channel,
|
|
666
|
+
templateId
|
|
667
|
+
});
|
|
668
|
+
return null;
|
|
669
|
+
}
|
|
670
|
+
return template;
|
|
671
|
+
}
|
|
672
|
+
__name(loadBoundTemplate, "loadBoundTemplate");
|
|
673
|
+
function mergeTriggerVariables(payload, rich) {
|
|
674
|
+
const basic = {
|
|
675
|
+
orderId: String(payload.orderId ?? ""),
|
|
676
|
+
orderNumber: payload.orderNumber,
|
|
677
|
+
customerName: payload.customerName,
|
|
678
|
+
customerPhone: payload.customerPhone ?? "",
|
|
679
|
+
total: String(payload.total),
|
|
680
|
+
orderTotal: String(payload.total),
|
|
681
|
+
currency: payload.currency,
|
|
682
|
+
vendorId: String(payload.vendorId ?? ""),
|
|
683
|
+
...payload.refundAmount != null ? {
|
|
684
|
+
refundAmount: String(payload.refundAmount)
|
|
685
|
+
} : {}
|
|
686
|
+
};
|
|
687
|
+
return {
|
|
688
|
+
...basic,
|
|
689
|
+
...rich ?? {}
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
__name(mergeTriggerVariables, "mergeTriggerVariables");
|
|
693
|
+
async function sendEmailForTrigger(triggerKey, orderId, variables, contactEmail, dataSource, entityMap, getCms) {
|
|
694
|
+
const template = await loadBoundTemplate(triggerKey, "email", dataSource, entityMap);
|
|
695
|
+
if (!template) return;
|
|
696
|
+
const [branding, emailSettings] = await Promise.all([
|
|
697
|
+
getSettingsGroup(dataSource, entityMap, "branding"),
|
|
698
|
+
getSettingsGroup(dataSource, entityMap, "email")
|
|
699
|
+
]);
|
|
700
|
+
const companyDetails = mergeEmailLayoutCompanyDetails(branding, emailSettings);
|
|
701
|
+
const subject = applyTemplateVars(template.subject ?? "Order update", variables);
|
|
702
|
+
const bodyHtml = applyTemplateVars(template.body, variables);
|
|
703
|
+
const html = renderLayout({
|
|
704
|
+
bodyHtml,
|
|
705
|
+
companyDetails
|
|
706
|
+
});
|
|
707
|
+
const cms = await getCms();
|
|
708
|
+
await queueEmail(cms, {
|
|
709
|
+
to: contactEmail,
|
|
710
|
+
subject,
|
|
711
|
+
html,
|
|
712
|
+
text: bodyHtml.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim()
|
|
713
|
+
});
|
|
714
|
+
console.log("[order-notifications] email queued", {
|
|
715
|
+
triggerKey,
|
|
716
|
+
orderId,
|
|
717
|
+
to: contactEmail
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
__name(sendEmailForTrigger, "sendEmailForTrigger");
|
|
721
|
+
async function sendWhatsAppForTrigger(triggerKey, orderId, phone, variables, dataSource, entityMap, getCms) {
|
|
722
|
+
const template = await loadBoundTemplate(triggerKey, "whatsapp", dataSource, entityMap);
|
|
723
|
+
if (!template) return;
|
|
724
|
+
const paramOrder = template.providerMeta?.paramOrder ?? [];
|
|
725
|
+
if (paramOrder.length === 0) {
|
|
726
|
+
console.warn("[order-notifications] WhatsApp template has no paramOrder set \u2014 variables may send blank/wrong", {
|
|
727
|
+
triggerKey
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
const cms = await getCms();
|
|
731
|
+
const metaTemplateName = resolveMetaWhatsAppTemplateName({
|
|
732
|
+
externalTemplateRef: template.externalTemplateRef,
|
|
733
|
+
name: template.name
|
|
734
|
+
});
|
|
735
|
+
if (!metaTemplateName) {
|
|
736
|
+
console.warn("[order-notifications] bound WhatsApp template has no Meta template name", {
|
|
737
|
+
triggerKey,
|
|
738
|
+
templateId: template.id
|
|
739
|
+
});
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
const sent = await queueWhatsApp(cms, {
|
|
743
|
+
to: phone.trim(),
|
|
744
|
+
templateKey: triggerKey,
|
|
745
|
+
externalTemplateRef: metaTemplateName,
|
|
746
|
+
templateLanguage: template.providerMeta?.language ?? void 0,
|
|
747
|
+
body: template.body,
|
|
748
|
+
variables,
|
|
749
|
+
templateParamOrder: paramOrder
|
|
750
|
+
});
|
|
751
|
+
if (!sent) {
|
|
752
|
+
console.warn("[order-notifications] WhatsApp send failed", {
|
|
753
|
+
triggerKey,
|
|
754
|
+
orderId,
|
|
755
|
+
hint: "Register whatsappPlugin() + queuePlugin() in getCms(), or set WHATSAPP_ACCESS_TOKEN and WHATSAPP_PHONE_NUMBER_ID"
|
|
756
|
+
});
|
|
757
|
+
} else {
|
|
758
|
+
console.log("[order-notifications] WhatsApp queued/sent", {
|
|
759
|
+
triggerKey,
|
|
760
|
+
orderId,
|
|
761
|
+
phone: maskPhone3(phone)
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
__name(sendWhatsAppForTrigger, "sendWhatsAppForTrigger");
|
|
766
|
+
async function handleTrigger(triggerKey, payload, dataSource, entityMap, getCms) {
|
|
767
|
+
console.log("[order-notifications] received", {
|
|
768
|
+
triggerKey,
|
|
769
|
+
orderId: payload.orderId,
|
|
770
|
+
phone: maskPhone3(payload.customerPhone)
|
|
771
|
+
});
|
|
772
|
+
const pluginEnabled = await isOrderNotificationsEnabled(dataSource, entityMap);
|
|
773
|
+
if (!pluginEnabled) {
|
|
774
|
+
console.warn("[order-notifications] skip: Plugins \u2192 Order notifications disabled");
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
const triggerEnabled = await isTriggerCatalogEnabled(triggerKey, dataSource, entityMap);
|
|
778
|
+
if (!triggerEnabled) return;
|
|
779
|
+
const context = await loadOrderNotificationContexts(payload.orderId, {
|
|
780
|
+
dataSource,
|
|
781
|
+
entityMap
|
|
782
|
+
});
|
|
783
|
+
const variables = mergeTriggerVariables(payload, context?.variables);
|
|
784
|
+
const contactEmail = context?.contact.email?.trim() || "";
|
|
785
|
+
const contactPhone = context?.contact.phone?.trim() || payload.customerPhone?.trim() || "";
|
|
786
|
+
if (contactEmail) {
|
|
787
|
+
await sendEmailForTrigger(triggerKey, payload.orderId, variables, contactEmail, dataSource, entityMap, getCms);
|
|
788
|
+
} else {
|
|
789
|
+
console.warn("[order-notifications] skip email: no customer email", {
|
|
790
|
+
triggerKey,
|
|
791
|
+
orderId: payload.orderId
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
if (contactPhone) {
|
|
795
|
+
await sendWhatsAppForTrigger(triggerKey, payload.orderId, contactPhone, variables, dataSource, entityMap, getCms);
|
|
796
|
+
} else {
|
|
797
|
+
console.warn("[order-notifications] skip WhatsApp: no customer phone", {
|
|
798
|
+
triggerKey,
|
|
799
|
+
orderId: payload.orderId
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
__name(handleTrigger, "handleTrigger");
|
|
804
|
+
function initWhatsappTriggerDispatcher(deps) {
|
|
805
|
+
if (initialized) return;
|
|
806
|
+
initialized = true;
|
|
807
|
+
const { dataSource, entityMap, getCms } = deps;
|
|
808
|
+
notificationTriggerEmitter.onAnyTrigger((triggerKey, payload) => {
|
|
809
|
+
void handleTrigger(triggerKey, payload, dataSource, entityMap, getCms).catch((err) => {
|
|
810
|
+
console.error("[order-notifications]", triggerKey, err);
|
|
811
|
+
});
|
|
812
|
+
});
|
|
813
|
+
console.log("[order-notifications] dispatcher initialized (email + WhatsApp, unified via order_notification_bindings)");
|
|
814
|
+
}
|
|
815
|
+
__name(initWhatsappTriggerDispatcher, "initWhatsappTriggerDispatcher");
|
|
816
|
+
async function resendOrderNotification(triggerKey, orderId, deps) {
|
|
817
|
+
if (!deps.entityMap.orders) return {
|
|
818
|
+
ok: false,
|
|
819
|
+
error: "orders entity missing"
|
|
820
|
+
};
|
|
821
|
+
const pluginEnabled = await isOrderNotificationsEnabled(deps.dataSource, deps.entityMap);
|
|
822
|
+
if (!pluginEnabled) return {
|
|
823
|
+
ok: false,
|
|
824
|
+
error: "Order notifications plugin is disabled"
|
|
825
|
+
};
|
|
826
|
+
const triggerEnabled = await isTriggerCatalogEnabled(triggerKey, deps.dataSource, deps.entityMap);
|
|
827
|
+
if (!triggerEnabled) {
|
|
828
|
+
return {
|
|
829
|
+
ok: false,
|
|
830
|
+
error: `Trigger "${triggerKey}" is disabled or migration 1781230000000 not applied`
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
const order = await deps.dataSource.getRepository(deps.entityMap.orders).findOne({
|
|
834
|
+
where: {
|
|
835
|
+
id: orderId,
|
|
836
|
+
deleted: false
|
|
837
|
+
},
|
|
838
|
+
relations: [
|
|
839
|
+
"contact"
|
|
840
|
+
]
|
|
841
|
+
});
|
|
842
|
+
if (!order) return {
|
|
843
|
+
ok: false,
|
|
844
|
+
error: "Order not found"
|
|
845
|
+
};
|
|
846
|
+
const o = order;
|
|
847
|
+
await handleTrigger(triggerKey, {
|
|
848
|
+
orderId,
|
|
849
|
+
orderNumber: String(o.orderNumber ?? ""),
|
|
850
|
+
customerName: o.contact?.name ?? "",
|
|
851
|
+
customerPhone: o.contact?.phone ?? null,
|
|
852
|
+
total: o.total ?? 0,
|
|
853
|
+
currency: String(o.currency ?? "INR"),
|
|
854
|
+
vendorId: o.vendorId ?? null
|
|
855
|
+
}, deps.dataSource, deps.entityMap, deps.getCms);
|
|
856
|
+
return {
|
|
857
|
+
ok: true
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
__name(resendOrderNotification, "resendOrderNotification");
|
|
861
|
+
|
|
862
|
+
export { WhatsAppService, buildEventOrderTemplateVariables, initWhatsappTriggerDispatcher, isWhatsAppPluginEnabled, mergeWhatsAppConfigLayers, queueWhatsApp, registerWhatsAppQueueProcessor, resendOrderNotification, whatsAppConfigured };
|