@dev.smartpricing/message-composer-utils 4.3.2-fix.1 → 4.4.1-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/schemas/dynamic-values.js +4 -1
  2. package/dist/schemas/dynamic-values.js.map +1 -1
  3. package/dist/schemas/index.js +1 -0
  4. package/dist/schemas/index.js.map +1 -1
  5. package/dist/schemas/message-group.js +27 -6
  6. package/dist/schemas/message-group.js.map +1 -1
  7. package/dist/schemas/onboarding.js +3 -1
  8. package/dist/schemas/onboarding.js.map +1 -1
  9. package/dist/schemas/pagination.js +40 -0
  10. package/dist/schemas/pagination.js.map +1 -0
  11. package/dist/schemas/pagination.test.js +82 -0
  12. package/dist/schemas/pagination.test.js.map +1 -0
  13. package/dist/types/app.js.map +1 -1
  14. package/dist/types/index.js +1 -0
  15. package/dist/types/index.js.map +1 -1
  16. package/dist/types/message.js +22 -0
  17. package/dist/types/message.js.map +1 -1
  18. package/dist/types/pagination.js +2 -0
  19. package/dist/types/pagination.js.map +1 -0
  20. package/dist/utils/templates/whatsapp-compiler.test.js +241 -0
  21. package/dist/utils/templates/whatsapp-compiler.test.js.map +1 -0
  22. package/package.json +1 -1
  23. package/src/schemas/dynamic-values.ts +7 -2
  24. package/src/schemas/index.ts +1 -0
  25. package/src/schemas/message-group.ts +36 -6
  26. package/src/schemas/onboarding.ts +3 -0
  27. package/src/schemas/pagination.test.ts +101 -0
  28. package/src/schemas/pagination.ts +49 -0
  29. package/src/types/api/apiMessages.ts +3 -0
  30. package/src/types/app.ts +7 -2
  31. package/src/types/index.ts +1 -0
  32. package/src/types/kafka-events.ts +7 -1
  33. package/src/types/message.ts +84 -0
  34. package/src/types/pagination.ts +36 -0
  35. package/src/utils/templates/whatsapp-compiler.test.ts +282 -0
@@ -1,4 +1,5 @@
1
1
  export * from "./common.js";
2
+ export * from "./pagination.js";
2
3
  export * from "./ai-integration.js";
3
4
  export * from "./brand.js";
4
5
  export * from "./dynamic-values.js";
@@ -1,21 +1,46 @@
1
1
  import { z } from "zod";
2
2
  import {
3
3
  DuplicateMessageGroupBody,
4
+ ListMessageGroupsQuery,
4
5
  MessageGroupFromDb,
5
6
  MessageGroupWithMessageSummary,
6
7
  MESSAGE_GROUP_CATEGORIES,
8
+ MESSAGE_GROUP_LIST_SORT,
9
+ MESSAGE_GROUP_LIST_VISIBILITY,
10
+ MESSAGE_GROUP_PRODUCTS,
7
11
  MESSAGE_GROUP_VISIBILITY,
8
12
  MESSAGE_STATUS,
9
13
  MESSAGE_RENDER_TYPES,
14
+ SORT_ORDER,
10
15
  } from "../types/index.js";
11
16
  import { dbTimestampSchema } from "./common.js";
17
+ import {
18
+ paginatedResponseSchema,
19
+ paginationQuerySchema,
20
+ } from "./pagination.js";
12
21
  import { messageResponseSchema } from "./message.js";
13
22
 
14
- export const listMessageGroupsQuerySchema = z.object({
15
- render_type: z.string().optional(),
16
- tags: z.union([z.string(), z.array(z.string())]).optional(),
17
- tags_mode: z.enum(["and", "or"]).default("and"),
18
- });
23
+ /**
24
+ * GET /message-groups: server-side filter, sort and pagination (ADR 0012).
25
+ * `visibility` other than `public` is admin-only (403 otherwise, enforced by
26
+ * the route). An empty or whitespace `search` is treated as absent.
27
+ */
28
+ export const listMessageGroupsQuerySchema = paginationQuerySchema.extend({
29
+ search: z
30
+ .string()
31
+ .trim()
32
+ .max(200)
33
+ .optional()
34
+ .transform((value) => (value === "" ? undefined : value)),
35
+ category: z.enum(MESSAGE_GROUP_CATEGORIES).optional(),
36
+ status: z.enum(MESSAGE_STATUS).optional(),
37
+ product: z.enum(MESSAGE_GROUP_PRODUCTS).optional(),
38
+ visibility: z.enum(MESSAGE_GROUP_LIST_VISIBILITY).default("public"),
39
+ render_type: z.enum(MESSAGE_RENDER_TYPES).optional(),
40
+ sort: z.enum(MESSAGE_GROUP_LIST_SORT).default("updated_at"),
41
+ order: z.enum(SORT_ORDER).default("desc"),
42
+ }) satisfies z.ZodType<ListMessageGroupsQuery, unknown>;
43
+ export type { ListMessageGroupsQuery };
19
44
 
20
45
  export const updateMessageGroupBodySchema = z.object({
21
46
  name: z
@@ -24,12 +49,14 @@ export const updateMessageGroupBodySchema = z.object({
24
49
  (name) => name.trim() !== "",
25
50
  "Name is required and cannot be empty",
26
51
  ),
52
+ product: z.enum(MESSAGE_GROUP_PRODUCTS).optional(),
27
53
  brand_id: z.string().nullable().optional(),
28
54
  });
29
55
 
30
56
  export const createMessageGroupBodySchema = z.object({
31
57
  name: z.string(),
32
58
  category: z.enum(MESSAGE_GROUP_CATEGORIES),
59
+ product: z.enum(MESSAGE_GROUP_PRODUCTS),
33
60
  visibility: z.enum(MESSAGE_GROUP_VISIBILITY).optional(),
34
61
  brand_id: z.string().nullable().optional(),
35
62
  });
@@ -38,6 +65,7 @@ export const duplicateMessageGroupBodySchema = z
38
65
  .object({
39
66
  name: z.string().optional(),
40
67
  visibility: z.enum(MESSAGE_GROUP_VISIBILITY).optional(),
68
+ product: z.enum(MESSAGE_GROUP_PRODUCTS).optional(),
41
69
  brand_id: z.string().nullable().optional(),
42
70
  })
43
71
  .optional() satisfies z.ZodType<DuplicateMessageGroupBody | undefined>;
@@ -47,6 +75,7 @@ export const messageGroupResponseSchema = z.object({
47
75
  id: z.uuid(),
48
76
  name: z.string(),
49
77
  category: z.enum(MESSAGE_GROUP_CATEGORIES),
78
+ product: z.enum(MESSAGE_GROUP_PRODUCTS),
50
79
  visibility: z.enum(MESSAGE_GROUP_VISIBILITY).nullable().optional(),
51
80
  brand_id: z.string().nullable().optional(),
52
81
  organization_id: z.string().nullable().optional(),
@@ -80,7 +109,8 @@ export const messageGroupWithMessageSummaryResponseSchema =
80
109
  ),
81
110
  }) satisfies z.ZodType<unknown, MessageGroupWithMessageSummary>;
82
111
 
83
- export const messageGroupSummaryListResponseSchema = z.array(
112
+ /** Paginated envelope of list items (ADR 0012). */
113
+ export const messageGroupSummaryListResponseSchema = paginatedResponseSchema(
84
114
  messageGroupWithMessageSummaryResponseSchema,
85
115
  );
86
116
 
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import {
3
+ MESSAGE_GROUP_PRODUCTS,
3
4
  brandDefaultEmailSettingsSchema,
4
5
  brandDefaultButtonSettingsSchema,
5
6
  brandSocialsSchema,
@@ -21,6 +22,8 @@ export const onboardingBodySchema = z.object({
21
22
  userId: z
22
23
  .string({ error: "Both organizationId and userId are required" })
23
24
  .min(1, "Both organizationId and userId are required"),
25
+ // Required, no fallback to the sm-product header (ADR 0011)
26
+ product: z.enum(MESSAGE_GROUP_PRODUCTS),
24
27
  tags: z.array(z.string()).optional(),
25
28
  });
26
29
 
@@ -0,0 +1,101 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { z } from "zod";
3
+ import {
4
+ paginationQuerySchema,
5
+ paginatedResponseSchema,
6
+ } from "./pagination.js";
7
+
8
+ describe("paginationQuerySchema", () => {
9
+ it("defaults to page 1 and 25 per page", () => {
10
+ expect(paginationQuerySchema.parse({})).toEqual({ page: 1, per_page: 25 });
11
+ });
12
+
13
+ it("coerces query-string numbers", () => {
14
+ expect(paginationQuerySchema.parse({ page: "3", per_page: "50" })).toEqual({
15
+ page: 3,
16
+ per_page: 50,
17
+ });
18
+ });
19
+
20
+ it("accepts the bounds 1 and 100 for per_page", () => {
21
+ expect(paginationQuerySchema.parse({ per_page: "1" }).per_page).toBe(1);
22
+ expect(paginationQuerySchema.parse({ per_page: "100" }).per_page).toBe(100);
23
+ });
24
+
25
+ it("rejects page 0, per_page 0 and per_page 101", () => {
26
+ expect(paginationQuerySchema.safeParse({ page: "0" }).success).toBe(false);
27
+ expect(paginationQuerySchema.safeParse({ per_page: "0" }).success).toBe(
28
+ false,
29
+ );
30
+ expect(paginationQuerySchema.safeParse({ per_page: "101" }).success).toBe(
31
+ false,
32
+ );
33
+ });
34
+
35
+ it("rejects non-integer values", () => {
36
+ expect(paginationQuerySchema.safeParse({ page: "1.5" }).success).toBe(
37
+ false,
38
+ );
39
+ expect(paginationQuerySchema.safeParse({ page: "abc" }).success).toBe(
40
+ false,
41
+ );
42
+ });
43
+ });
44
+
45
+ describe("paginatedResponseSchema", () => {
46
+ const schema = paginatedResponseSchema(z.object({ id: z.string() }));
47
+
48
+ it("accepts the Laravel resource-collection envelope", () => {
49
+ const envelope = {
50
+ data: [{ id: "a" }],
51
+ links: { first: "/x?page=1", last: "/x?page=1", prev: null, next: null },
52
+ meta: {
53
+ current_page: 1,
54
+ from: 1,
55
+ last_page: 1,
56
+ path: "/x",
57
+ per_page: 25,
58
+ to: 1,
59
+ total: 1,
60
+ },
61
+ };
62
+ expect(schema.parse(envelope)).toEqual(envelope);
63
+ });
64
+
65
+ it("allows null from/to on an empty page and has no meta.links", () => {
66
+ const parsed = schema.parse({
67
+ data: [],
68
+ links: { first: "/x?page=1", last: "/x?page=1", prev: null, next: null },
69
+ meta: {
70
+ current_page: 1,
71
+ from: null,
72
+ last_page: 1,
73
+ path: "/x",
74
+ per_page: 25,
75
+ to: null,
76
+ total: 0,
77
+ links: [{ url: null, label: "x", active: false }],
78
+ },
79
+ });
80
+ expect(parsed.meta.from).toBeNull();
81
+ expect(parsed.meta.to).toBeNull();
82
+ expect("links" in parsed.meta).toBe(false);
83
+ });
84
+
85
+ it("validates items with the given schema", () => {
86
+ const result = schema.safeParse({
87
+ data: [{ id: 1 }],
88
+ links: { first: "/x?page=1", last: "/x?page=1", prev: null, next: null },
89
+ meta: {
90
+ current_page: 1,
91
+ from: 1,
92
+ last_page: 1,
93
+ path: "/x",
94
+ per_page: 25,
95
+ to: 1,
96
+ total: 1,
97
+ },
98
+ });
99
+ expect(result.success).toBe(false);
100
+ });
101
+ });
@@ -0,0 +1,49 @@
1
+ import { z } from "zod";
2
+ import type {
3
+ PaginationLinks,
4
+ PaginationMeta,
5
+ PaginationQuery,
6
+ } from "../types/index.js";
7
+
8
+ export const PAGINATION_DEFAULT_PER_PAGE = 25;
9
+ export const PAGINATION_MAX_PER_PAGE = 100;
10
+
11
+ /** Laravel-style offset pagination query params (ADR 0012). */
12
+ export const paginationQuerySchema = z.object({
13
+ page: z.coerce.number().int().min(1).default(1),
14
+ per_page: z.coerce
15
+ .number()
16
+ .int()
17
+ .min(1)
18
+ .max(PAGINATION_MAX_PER_PAGE)
19
+ .default(PAGINATION_DEFAULT_PER_PAGE),
20
+ }) satisfies z.ZodType<PaginationQuery, unknown>;
21
+
22
+ export const paginationLinksSchema = z.object({
23
+ first: z.string(),
24
+ last: z.string(),
25
+ prev: z.string().nullable(),
26
+ next: z.string().nullable(),
27
+ }) satisfies z.ZodType<unknown, PaginationLinks>;
28
+
29
+ export const paginationMetaSchema = z.object({
30
+ current_page: z.number().int(),
31
+ from: z.number().int().nullable(),
32
+ last_page: z.number().int(),
33
+ path: z.string(),
34
+ per_page: z.number().int(),
35
+ to: z.number().int().nullable(),
36
+ total: z.number().int(),
37
+ }) satisfies z.ZodType<unknown, PaginationMeta>;
38
+
39
+ /**
40
+ * Laravel API resource-collection envelope around `item`:
41
+ * `{ data, links: { first, last, prev, next }, meta: { … } }`, no `meta.links`.
42
+ */
43
+ export function paginatedResponseSchema<T extends z.ZodType>(item: T) {
44
+ return z.object({
45
+ data: z.array(item),
46
+ links: paginationLinksSchema,
47
+ meta: paginationMetaSchema,
48
+ });
49
+ }
@@ -1,6 +1,7 @@
1
1
  import type {
2
2
  MessageFromDb,
3
3
  MessageGroupFromDb,
4
+ MessageGroupProduct,
4
5
  MessageGroupVisibility,
5
6
  MessageStatus,
6
7
  RenderType,
@@ -14,6 +15,8 @@ export type MessageGroupWithMessages = MessageGroupFromDb & {
14
15
  export interface DuplicateMessageGroupBody {
15
16
  name?: string;
16
17
  visibility?: MessageGroupVisibility;
18
+ /** Defaults to the source group's Product. */
19
+ product?: MessageGroupProduct;
17
20
  brand_id?: string | null;
18
21
  }
19
22
 
package/src/types/app.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { MessageGroupProduct } from "./message.js";
2
+
1
3
  export const errorCodes = {
2
4
  INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR",
3
5
  UNAUTHORIZED: "UNAUTHORIZED",
@@ -34,7 +36,8 @@ export interface AppError {
34
36
  export interface AppContextConfig {
35
37
  /** @deprecated No longer used — auth is cookie-based via platform layer */
36
38
  authenticationToken?: string;
37
- product?: "connect" | "chat" | "pms" | "pricing" | "browser";
39
+ /** The Product the host app embeds the composer in (ADR 0011). Required. */
40
+ product: MessageGroupProduct;
38
41
  strategy?: "embed" | "native";
39
42
  channels?: Array<"email" | "whatsapp">;
40
43
  languages?: string[];
@@ -45,7 +48,9 @@ export interface AppContextConfig {
45
48
  default_tags?: string[];
46
49
  }
47
50
 
48
- export interface AppContextState extends AppContextConfig {
51
+ export interface AppContextState extends Omit<AppContextConfig, "product"> {
52
+ /** Undefined when the host gave no Product; surfaced as a validation error. */
53
+ product?: MessageGroupProduct;
49
54
  initializationSource:
50
55
  | "query"
51
56
  | "postMessage"
@@ -13,3 +13,4 @@ export * from "./submission-job.js";
13
13
  export * from "./kafka-events.js";
14
14
  export * from "./dynamic-values.js";
15
15
  export * from "./brand.js";
16
+ export * from "./pagination.js";
@@ -1,10 +1,16 @@
1
- import type { MessageStatus, MessageGroupCategory } from "./message.js";
1
+ import type {
2
+ MessageStatus,
3
+ MessageGroupCategory,
4
+ MessageGroupProduct,
5
+ } from "./message.js";
2
6
 
3
7
  export interface KafkaEventBase {
4
8
  event_id: string;
5
9
  event_type: string;
6
10
  timestamp: string;
7
11
  message_group_id: string;
12
+ /** Product of the Message Group the event is about (ADR 0011). */
13
+ product: MessageGroupProduct;
8
14
  organization_id?: string | null;
9
15
  user_id: string;
10
16
  }
@@ -8,6 +8,7 @@ import type {
8
8
  ButtonBlockV1,
9
9
  } from "./whatsappblocks.v1.js";
10
10
  import type { EmailBlock, EmailGlobalSettings } from "./emailblocks.v1.js";
11
+ import type { PaginationQuery, SortOrder } from "./pagination.js";
11
12
  import {
12
13
  brandDefaultEmailSettingsSchema,
13
14
  brandDefaultButtonSettingsSchema,
@@ -79,10 +80,55 @@ export type MessageGroupCategory = (typeof MESSAGE_GROUP_CATEGORIES)[number];
79
80
  export const MESSAGE_GROUP_VISIBILITY = ["public", "internal"] as const;
80
81
  export type MessageGroupVisibility = (typeof MESSAGE_GROUP_VISIBILITY)[number];
81
82
 
83
+ /** `visibility` filter of the list route; `internal` and `all` are admin-only. */
84
+ export const MESSAGE_GROUP_LIST_VISIBILITY = [
85
+ ...MESSAGE_GROUP_VISIBILITY,
86
+ "all",
87
+ ] as const;
88
+ export type MessageGroupListVisibility =
89
+ (typeof MESSAGE_GROUP_LIST_VISIBILITY)[number];
90
+
91
+ /** Sortable columns of the list route (ADR 0012); aggregates are not sortable. */
92
+ export const MESSAGE_GROUP_LIST_SORT = [
93
+ "name",
94
+ "category",
95
+ "created_at",
96
+ "updated_at",
97
+ ] as const;
98
+ export type MessageGroupListSort = (typeof MESSAGE_GROUP_LIST_SORT)[number];
99
+
100
+ /**
101
+ * Parsed query of `GET /message-groups` (ADR 0012): what the route handler
102
+ * receives after defaults. The layer sends any subset of these as query params.
103
+ */
104
+ export interface ListMessageGroupsQuery extends PaginationQuery {
105
+ search?: string;
106
+ category?: MessageGroupCategory;
107
+ status?: MessageStatus;
108
+ product?: MessageGroupProduct;
109
+ visibility: MessageGroupListVisibility;
110
+ render_type?: RenderType;
111
+ sort: MessageGroupListSort;
112
+ order: SortOrder;
113
+ }
114
+
115
+ /**
116
+ * The Smartness application a Message Group is authored for (ADR 0011).
117
+ * Anything outside this list is not a Product and is rejected on write.
118
+ */
119
+ export const MESSAGE_GROUP_PRODUCTS = [
120
+ "connect",
121
+ "chat",
122
+ "pms",
123
+ "pricing",
124
+ ] as const;
125
+ export type MessageGroupProduct = (typeof MESSAGE_GROUP_PRODUCTS)[number];
126
+
82
127
  export interface MessageGroup extends DbEntity {
83
128
  id: string;
84
129
  name: string;
85
130
  category: MessageGroupCategory;
131
+ product: MessageGroupProduct;
86
132
  visibility?: MessageGroupVisibility | null;
87
133
  brand_id?: string | null;
88
134
  organization_id?: string | null;
@@ -311,6 +357,40 @@ interface WhatsAppTemplateMessage extends WhatsAppMessageBase {
311
357
  */
312
358
  export type WhatsAppSendPayload = WhatsAppTemplateMessage;
313
359
 
360
+ /**
361
+ * Sendable Message (WhatsApp): the content object a host hands to its own
362
+ * send pipeline. `meta.data` is the compiled Meta template object (name,
363
+ * language, components) and `meta.template` the translated Meta template the
364
+ * bubble is rendered from.
365
+ */
366
+ export interface WhatsappSendableMessage {
367
+ type: "WhatsappTemplateMessage";
368
+ text: string;
369
+ meta: {
370
+ data: WhatsAppTemplateMessage["template"];
371
+ template: MetaTemplate;
372
+ };
373
+ }
374
+
375
+ /**
376
+ * Email Sendable Message: the content object the email compile-and-send path
377
+ * publishes (Smartchat `HtmlMessage`), returned by `/compile` as `message`.
378
+ */
379
+ export interface EmailSendableMessage {
380
+ type: "HtmlMessage";
381
+ skipTranslation: true;
382
+ /** The compiled HTML, as the message API expects it in both fields. */
383
+ text: string;
384
+ rawHtml: string;
385
+ subject: string;
386
+ }
387
+
388
+ /**
389
+ * Sendable Message: what `/compile` returns as `message` and what
390
+ * compile-and-send publishes, one member per channel.
391
+ */
392
+ export type SendableMessage = WhatsappSendableMessage | EmailSendableMessage;
393
+
314
394
  /**
315
395
  * Response for compile endpoint (WhatsApp)
316
396
  */
@@ -319,6 +399,8 @@ export interface CompileWhatsAppResponse {
319
399
  language: string;
320
400
  template_name: string;
321
401
  payload: Omit<WhatsAppTemplateMessage, "to">;
402
+ /** The Sendable Message, ready for the host's send pipeline. */
403
+ message: SendableMessage;
322
404
  parameters_used: string[];
323
405
  missingParameters?: string[];
324
406
  }
@@ -332,6 +414,8 @@ export interface CompileEmailResponse {
332
414
  subject: string;
333
415
  html: string;
334
416
  plaintext?: string;
417
+ /** The Sendable Message, ready for the host's send pipeline. */
418
+ message: EmailSendableMessage;
335
419
  parameters_used: string[];
336
420
  missingParameters?: string[];
337
421
  }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Laravel API resource-collection envelope for paginated lists (ADR 0012).
3
+ * Links are relative (path + query, no host); `meta.links` is omitted.
4
+ */
5
+ export interface PaginationLinks {
6
+ first: string;
7
+ last: string;
8
+ prev: string | null;
9
+ next: string | null;
10
+ }
11
+
12
+ export interface PaginationMeta {
13
+ current_page: number;
14
+ /** 1-based index of the first item on this page; null on an empty page. */
15
+ from: number | null;
16
+ last_page: number;
17
+ path: string;
18
+ per_page: number;
19
+ /** 1-based index of the last item on this page; null on an empty page. */
20
+ to: number | null;
21
+ total: number;
22
+ }
23
+
24
+ export interface PaginatedResponse<T> {
25
+ data: T[];
26
+ links: PaginationLinks;
27
+ meta: PaginationMeta;
28
+ }
29
+
30
+ export interface PaginationQuery {
31
+ page: number;
32
+ per_page: number;
33
+ }
34
+
35
+ export const SORT_ORDER = ["asc", "desc"] as const;
36
+ export type SortOrder = (typeof SORT_ORDER)[number];