@winible/winible-typed 2.66.1 → 2.66.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@winible/winible-typed",
3
- "version": "2.66.1",
3
+ "version": "2.66.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -78,6 +78,7 @@
78
78
  "files": [
79
79
  "typed-model/**/*",
80
80
  "event-collector-models/**/*",
81
+ "utils/**/*",
81
82
  "support/**/*",
82
83
  "src/**/*",
83
84
  "index.ts",
@@ -0,0 +1,137 @@
1
+ import { CampaignMessage, User } from "../typed-model";
2
+
3
+ type CampaignMessageConfiguration = {
4
+ scheduleSendTime?: Date | null;
5
+ replacements?: Record<string, string>;
6
+ };
7
+
8
+ type EmailSendQueueInput = {
9
+ campaignId: number;
10
+ campaignMessageId: number;
11
+ senderId: string;
12
+ contactId: string;
13
+ contactEmail: string;
14
+ configuration: CampaignMessageConfiguration;
15
+ };
16
+
17
+ type SmsSendQueueInput = {
18
+ campaignId: number;
19
+ campaignMessageId: number;
20
+ senderId: string;
21
+ contactId: string;
22
+ contactPhoneNumber: string;
23
+ configuration: CampaignMessageConfiguration;
24
+ };
25
+
26
+ const CLIENT_URL = process.env.CLIENT_URL;
27
+
28
+ const VARIABLE_MAP = {
29
+ FIRST_NAME: "{FIRST_NAME}",
30
+ CHECKOUT_URL: "{CHECKOUT_URL}",
31
+ PLAN_NAME: "{PLAN_NAME}",
32
+ FOR_PROMOTION: "{FOR_PROMOTION}",
33
+ JOINED_PLAN_NAME: "{JOINED_PLAN_NAME}",
34
+ };
35
+
36
+ const buildReplacements = (contact: User, campaignMessage: CampaignMessage) => {
37
+ let replacements: Record<string, string> = {};
38
+
39
+ const subject = (campaignMessage.subject || "").replace(/<[^>]*>/g, "");
40
+ const content = campaignMessage.content || "";
41
+ const metadata = campaignMessage.metadata || {};
42
+ const dynamicFields = metadata.dynamicFields || {};
43
+
44
+ if (
45
+ subject.includes(VARIABLE_MAP.FIRST_NAME) ||
46
+ content.includes(VARIABLE_MAP.FIRST_NAME)
47
+ ) {
48
+ replacements[VARIABLE_MAP.FIRST_NAME] = contact.firstName || "";
49
+ }
50
+
51
+ if (
52
+ subject.includes(VARIABLE_MAP.CHECKOUT_URL) ||
53
+ content.includes(VARIABLE_MAP.CHECKOUT_URL)
54
+ ) {
55
+ const checkoutUrl = dynamicFields.checkoutUrl || `${CLIENT_URL}/404`;
56
+
57
+ replacements[VARIABLE_MAP.CHECKOUT_URL] = checkoutUrl;
58
+ }
59
+
60
+ if (
61
+ subject.includes(VARIABLE_MAP.PLAN_NAME) ||
62
+ content.includes(VARIABLE_MAP.PLAN_NAME)
63
+ ) {
64
+ const planName = dynamicFields.planName || "";
65
+
66
+ replacements[VARIABLE_MAP.PLAN_NAME] = planName;
67
+ }
68
+
69
+ if (
70
+ subject.includes(VARIABLE_MAP.FOR_PROMOTION) ||
71
+ content.includes(VARIABLE_MAP.FOR_PROMOTION)
72
+ ) {
73
+ const promotion = dynamicFields.promotion || "";
74
+
75
+ replacements[VARIABLE_MAP.FOR_PROMOTION] = promotion;
76
+ }
77
+
78
+ return replacements;
79
+ };
80
+
81
+ /**
82
+ * Builds a single SMS queue input object.
83
+ */
84
+ export const buildSmsQueueInput = (
85
+ senderId: string,
86
+ contact: User,
87
+ campaignMessage: CampaignMessage,
88
+ scheduleSendTime?: Date | null // Optional: Used for absolute/relative timing
89
+ ): SmsSendQueueInput => {
90
+ const replacements = buildReplacements(contact, campaignMessage);
91
+ const configuration: Partial<SmsSendQueueInput["configuration"]> = {
92
+ replacements,
93
+ };
94
+
95
+ if (scheduleSendTime) {
96
+ configuration.scheduleSendTime = scheduleSendTime;
97
+ }
98
+
99
+ return {
100
+ campaignId: campaignMessage.campaignId,
101
+ campaignMessageId: campaignMessage.id,
102
+ senderId,
103
+ contactId: contact.id,
104
+ contactPhoneNumber: contact.phoneNumber || "",
105
+ configuration: configuration as SmsSendQueueInput["configuration"],
106
+ };
107
+ };
108
+
109
+ /**
110
+ * Builds a single Email queue input object.
111
+ */
112
+ export const buildEmailQueueInput = (
113
+ senderId: string,
114
+ contact: User,
115
+ campaignMessage: CampaignMessage,
116
+ scheduleSendTime?: Date | null // Optional: Used for absolute/relative timing
117
+ ): EmailSendQueueInput => {
118
+ const replacements = buildReplacements(contact, campaignMessage);
119
+ const configuration: Partial<EmailSendQueueInput["configuration"]> = {
120
+ replacements,
121
+ };
122
+
123
+ if (scheduleSendTime) {
124
+ configuration.scheduleSendTime = scheduleSendTime;
125
+ }
126
+
127
+ return {
128
+ campaignId: campaignMessage.campaignId,
129
+ campaignMessageId: campaignMessage.id,
130
+ senderId,
131
+ contactId: contact.id,
132
+ contactEmail: contact.email || "",
133
+ configuration: configuration as EmailSendQueueInput["configuration"],
134
+ };
135
+ };
136
+
137
+ export default { buildReplacements };
package/utils/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default as campaignUtils } from "./campaignUtils";