@koloseum/utils 0.1.14 → 0.1.16
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/dist/utils.d.ts +24 -8
- package/dist/utils.js +176 -43
- package/package.json +3 -2
package/dist/utils.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Database } from "@koloseum/types/database";
|
|
2
|
-
import type { CustomError, Microservice, MicroserviceGroup, MicroserviceObject
|
|
2
|
+
import type { CustomError, Microservice, MicroserviceGroup, MicroserviceObject } from "@koloseum/types/general";
|
|
3
3
|
import type { PronounsCheckboxes, SocialMediaPlatform } from "@koloseum/types/public-auth";
|
|
4
4
|
import type { Page } from "@sveltejs/kit";
|
|
5
5
|
import type { SupabaseClient, PostgrestError } from "@supabase/supabase-js";
|
|
6
|
-
import type {
|
|
6
|
+
import type { suprsend } from "@suprsend/node-sdk";
|
|
7
|
+
import type { IOptions } from "@suprsend/web-components/dist/types/interface.d.ts";
|
|
7
8
|
import type { MobilePhoneLocale } from "validator";
|
|
8
|
-
import { Novu } from "@novu/api";
|
|
9
9
|
export declare const Status: {
|
|
10
10
|
/**
|
|
11
11
|
* A generic error message.
|
|
@@ -74,12 +74,12 @@ export declare const Utility: {
|
|
|
74
74
|
*/
|
|
75
75
|
getAge: (dateOfBirth: string) => number;
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
78
|
-
* @param
|
|
79
|
-
* @param
|
|
80
|
-
* @returns The
|
|
77
|
+
* Generate a SuprSend configuration object for a user.
|
|
78
|
+
* @param {string} userId - The user ID to generate the configuration for.
|
|
79
|
+
* @param {string} publicApiKey - The public API key to use for SuprSend.
|
|
80
|
+
* @returns The SuprSend configuration object.
|
|
81
81
|
*/
|
|
82
|
-
|
|
82
|
+
getSuprSendConfig: (userId: string, publicApiKey: string) => IOptions;
|
|
83
83
|
/**
|
|
84
84
|
* Handles the click event for pronouns checkboxes.
|
|
85
85
|
* @param {MouseEvent} e - The click event
|
|
@@ -174,4 +174,20 @@ export declare const Utility: {
|
|
|
174
174
|
* @returns `true` if the handle is valid; `false` otherwise
|
|
175
175
|
*/
|
|
176
176
|
validateSocialMediaHandle: (handle: string) => boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Validates a SuprSend subscriber. If the subscriber does not exist, it will be created with the provided person data.
|
|
179
|
+
* @param {suprsend.Suprsend} suprSend - The SuprSend instance.
|
|
180
|
+
* @param {string} userId - The user ID.
|
|
181
|
+
* @param {Object} personData - The person data; required if the subscriber does not exist.
|
|
182
|
+
* @returns {Promise<{ error?: CustomError; success?: true }>} A promise that resolves to an object with an error if the subscriber validation fails, or indicating success otherwise.
|
|
183
|
+
*/
|
|
184
|
+
validateSuprSendSubscriber: (suprSend: suprsend.Suprsend, userId: string, personData?: {
|
|
185
|
+
firstName: string;
|
|
186
|
+
lastName: string;
|
|
187
|
+
phone: string;
|
|
188
|
+
pseudonym?: string;
|
|
189
|
+
}) => Promise<{
|
|
190
|
+
error?: CustomError;
|
|
191
|
+
success?: true;
|
|
192
|
+
}>;
|
|
177
193
|
};
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { error as svelteError } from "@sveltejs/kit";
|
|
2
2
|
import { FunctionsFetchError, FunctionsHttpError, FunctionsRelayError } from "@supabase/supabase-js";
|
|
3
|
-
import { ErrorDto } from "@novu/api/models/errors/errordto.js";
|
|
4
3
|
import sanitize from "sanitize-html";
|
|
5
4
|
import validator from "validator";
|
|
6
5
|
/* Helper functions */
|
|
@@ -181,52 +180,148 @@ export const Utility = {
|
|
|
181
180
|
return monthDiff < 0 || (monthDiff === 0 && currentDate.getDate() < birthDate.getDate()) ? --age : age;
|
|
182
181
|
},
|
|
183
182
|
/**
|
|
184
|
-
*
|
|
185
|
-
* @param
|
|
186
|
-
* @param
|
|
187
|
-
* @returns The
|
|
183
|
+
* Generate a SuprSend configuration object for a user.
|
|
184
|
+
* @param {string} userId - The user ID to generate the configuration for.
|
|
185
|
+
* @param {string} publicApiKey - The public API key to use for SuprSend.
|
|
186
|
+
* @returns The SuprSend configuration object.
|
|
188
187
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
188
|
+
getSuprSendConfig: (userId, publicApiKey) => ({
|
|
189
|
+
distinctId: userId,
|
|
190
|
+
publicApiKey,
|
|
191
|
+
inbox: {
|
|
192
|
+
stores: [
|
|
193
|
+
{ storeId: "all", label: "Inbox", query: { archived: false } },
|
|
194
|
+
{ storeId: "archived", label: "Archived", query: { archived: true } }
|
|
195
|
+
],
|
|
196
|
+
theme: {
|
|
197
|
+
bell: {
|
|
198
|
+
color: "var(--color-accent)"
|
|
199
|
+
},
|
|
200
|
+
badge: {
|
|
201
|
+
backgroundColor: "var(--color-primary)",
|
|
202
|
+
color: "var(--color-primary-content)"
|
|
203
|
+
},
|
|
204
|
+
header: {
|
|
205
|
+
container: {
|
|
206
|
+
backgroundColor: "var(--color-base-100)",
|
|
207
|
+
borderColor: "var(--color-base-200)"
|
|
208
|
+
},
|
|
209
|
+
headerText: {
|
|
210
|
+
color: "var(--color-neutral)"
|
|
211
|
+
},
|
|
212
|
+
markAllReadText: {
|
|
213
|
+
color: "var(--color-accent)"
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
tabs: {
|
|
217
|
+
color: "var(--color-primary)",
|
|
218
|
+
unselectedColor: "var(--color-accent)",
|
|
219
|
+
bottomColor: "var(--color-primary)",
|
|
220
|
+
badgeColor: "var(--color-base-200)",
|
|
221
|
+
badgeText: "var(--color-accent)"
|
|
222
|
+
},
|
|
223
|
+
notificationsContainer: {
|
|
224
|
+
container: {
|
|
225
|
+
backgroundColor: "var(--color-base-100)",
|
|
226
|
+
borderColor: "var(--color-base-200)",
|
|
227
|
+
height: "75vh",
|
|
228
|
+
marginTop: "0.75rem"
|
|
229
|
+
},
|
|
230
|
+
noNotificationsText: {
|
|
231
|
+
color: "var(--color-warning)"
|
|
232
|
+
},
|
|
233
|
+
noNotificationsSubtext: {
|
|
234
|
+
color: "var(--color-neutral)"
|
|
235
|
+
},
|
|
236
|
+
loader: {
|
|
237
|
+
color: "var(--color-primary)"
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
notification: {
|
|
241
|
+
container: {
|
|
242
|
+
borderColor: "var(--color-base-200)",
|
|
243
|
+
readBackgroundColor: "var(--color-base-100)",
|
|
244
|
+
unreadBackgroundColor: "var(--color-base-150)",
|
|
245
|
+
hoverBackgroundColor: "var(--color-base-200)"
|
|
246
|
+
},
|
|
247
|
+
headerText: {
|
|
248
|
+
color: "var(--color-secondary)"
|
|
249
|
+
},
|
|
250
|
+
bodyText: {
|
|
251
|
+
color: "var(--color-neutral)",
|
|
252
|
+
linkColor: "var(--color-secondary)"
|
|
253
|
+
},
|
|
254
|
+
unseenDot: {
|
|
255
|
+
backgroundColor: "var(--color-warning)"
|
|
256
|
+
},
|
|
257
|
+
createdOnText: {
|
|
258
|
+
color: "var(--color-accent)"
|
|
259
|
+
},
|
|
260
|
+
subtext: {
|
|
261
|
+
color: "var(--color-neutral)"
|
|
262
|
+
},
|
|
263
|
+
expiresText: {
|
|
264
|
+
color: "var(--color-warning)",
|
|
265
|
+
expiringBackgroundColor: "var(--color-warning)",
|
|
266
|
+
expiringColor: "var(--color-neutral)"
|
|
267
|
+
},
|
|
268
|
+
actions: [
|
|
269
|
+
{
|
|
270
|
+
text: {
|
|
271
|
+
color: "var(--color-primary-content)"
|
|
272
|
+
},
|
|
273
|
+
container: {
|
|
274
|
+
backgroundColor: "var(--color-primary)",
|
|
275
|
+
hoverBackgroundColor: "var(--color-secondary)",
|
|
276
|
+
border: "unset"
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
text: {
|
|
281
|
+
color: "var(--color-primary-content)"
|
|
282
|
+
},
|
|
283
|
+
container: {
|
|
284
|
+
backgroundColor: "var(--color-primary)",
|
|
285
|
+
hoverBackgroundColor: "var(--color-secondary)",
|
|
286
|
+
border: "unset"
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
],
|
|
290
|
+
actionsMenuIcon: {
|
|
291
|
+
hoverBackgroundColor: "var(--color-base-200)",
|
|
292
|
+
color: "var(--color-neutral)"
|
|
293
|
+
},
|
|
294
|
+
actionsMenu: {
|
|
295
|
+
backgroundColor: "var(--color-base-100)",
|
|
296
|
+
borderColor: "var(--color-base-200)"
|
|
297
|
+
},
|
|
298
|
+
actionsMenuItem: {
|
|
299
|
+
hoverBackgroundColor: "var(--color-base-200)"
|
|
300
|
+
},
|
|
301
|
+
actionsMenuItemIcon: {
|
|
302
|
+
color: "var(--color-accent)"
|
|
303
|
+
},
|
|
304
|
+
actionsMenuItemText: {
|
|
305
|
+
color: "var(--color-neutral)"
|
|
306
|
+
}
|
|
218
307
|
}
|
|
219
|
-
|
|
220
|
-
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
toast: {
|
|
311
|
+
theme: {
|
|
312
|
+
container: {
|
|
313
|
+
backgroundColor: "var(--color-base-100)",
|
|
314
|
+
borderColor: "var(--color-base-200)"
|
|
315
|
+
},
|
|
316
|
+
headerText: {
|
|
317
|
+
color: "var(--color-primary)"
|
|
318
|
+
},
|
|
319
|
+
bodyText: {
|
|
320
|
+
color: "var(--color-neutral)"
|
|
221
321
|
}
|
|
222
322
|
}
|
|
223
|
-
// Otherwise, log error
|
|
224
|
-
else
|
|
225
|
-
console.error(error);
|
|
226
323
|
}
|
|
227
|
-
|
|
228
|
-
return subscriber;
|
|
229
|
-
},
|
|
324
|
+
}),
|
|
230
325
|
/**
|
|
231
326
|
* Handles the click event for pronouns checkboxes.
|
|
232
327
|
* @param {MouseEvent} e - The click event
|
|
@@ -595,5 +690,43 @@ export const Utility = {
|
|
|
595
690
|
*/
|
|
596
691
|
validateSocialMediaHandle: (handle) => !isURL(handle, { require_protocol: false }) &&
|
|
597
692
|
!handle.startsWith("@") &&
|
|
598
|
-
Boolean(handle.match(Utility.socialMediaHandleRegex))
|
|
693
|
+
Boolean(handle.match(Utility.socialMediaHandleRegex)),
|
|
694
|
+
/**
|
|
695
|
+
* Validates a SuprSend subscriber. If the subscriber does not exist, it will be created with the provided person data.
|
|
696
|
+
* @param {suprsend.Suprsend} suprSend - The SuprSend instance.
|
|
697
|
+
* @param {string} userId - The user ID.
|
|
698
|
+
* @param {Object} personData - The person data; required if the subscriber does not exist.
|
|
699
|
+
* @returns {Promise<{ error?: CustomError; success?: true }>} A promise that resolves to an object with an error if the subscriber validation fails, or indicating success otherwise.
|
|
700
|
+
*/
|
|
701
|
+
validateSuprSendSubscriber: async (suprSend, userId, personData) => {
|
|
702
|
+
// Check if subscriber exists
|
|
703
|
+
try {
|
|
704
|
+
await suprSend.users.get(userId);
|
|
705
|
+
}
|
|
706
|
+
catch (_err) {
|
|
707
|
+
// If person data is not provided, return an error
|
|
708
|
+
if (!personData)
|
|
709
|
+
return { error: Utility.customError(400, "Person data is required to create a SuprSend subscriber.") };
|
|
710
|
+
// Destructure person data
|
|
711
|
+
const { firstName, lastName, phone, pseudonym } = personData;
|
|
712
|
+
// Create subscriber
|
|
713
|
+
const subscriber = suprSend.user.get_instance(userId);
|
|
714
|
+
if (!subscriber)
|
|
715
|
+
return { error: Utility.customError(500, Status.ERROR) };
|
|
716
|
+
// Set subscriber details
|
|
717
|
+
subscriber.set_preferred_language("en");
|
|
718
|
+
subscriber.set_timezone("Africa/Nairobi");
|
|
719
|
+
subscriber.set("firstName", firstName);
|
|
720
|
+
subscriber.set("lastName", lastName);
|
|
721
|
+
subscriber.set("phone", `+${phone}`);
|
|
722
|
+
if (pseudonym)
|
|
723
|
+
subscriber.set("pseudonym", pseudonym);
|
|
724
|
+
// Save subscriber
|
|
725
|
+
const response = await subscriber.save();
|
|
726
|
+
if (!response.success)
|
|
727
|
+
return { error: Utility.customError(500, Status.ERROR) };
|
|
728
|
+
}
|
|
729
|
+
// Return success
|
|
730
|
+
return { success: true };
|
|
731
|
+
}
|
|
599
732
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koloseum/utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"author": "Koloseum Technologies Limited",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Utility logic for use across Koloseum web apps (TypeScript)",
|
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
"test": "vitest --run"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@novu/api": "^0.6.1",
|
|
33
32
|
"@supabase/supabase-js": "^2.48.1",
|
|
34
33
|
"@sveltejs/kit": "^2.17.1",
|
|
35
34
|
"sanitize-html": "^2.14.0",
|
|
@@ -38,6 +37,8 @@
|
|
|
38
37
|
"devDependencies": {
|
|
39
38
|
"@koloseum/types": "^0.1.12",
|
|
40
39
|
"@playwright/test": "^1.50.1",
|
|
40
|
+
"@suprsend/node-sdk": "^1.13.0",
|
|
41
|
+
"@suprsend/web-components": "^0.2.1",
|
|
41
42
|
"@types/sanitize-html": "^2.13.0",
|
|
42
43
|
"@types/validator": "^13.12.2",
|
|
43
44
|
"prettier": "^3.5.1",
|