@koloseum/utils 0.1.15 → 0.1.17

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 CHANGED
@@ -1,10 +1,22 @@
1
1
  import type { Database } from "@koloseum/types/database";
2
- import type { CustomError, Microservice, MicroserviceGroup, MicroserviceObject } from "@koloseum/types/general";
2
+ import type { CustomError, Microservice, MicroserviceGroup, MicroserviceObject, UserWithAppMetadata } 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 { suprsend } from "@suprsend/node-sdk";
6
7
  import type { IOptions } from "@suprsend/web-components/dist/types/interface.d.ts";
7
8
  import type { MobilePhoneLocale } from "validator";
9
+ export declare const Data: {
10
+ /**
11
+ * A generic authenticated user.
12
+ * @param {string} id - The user ID; defaults to a random UUID
13
+ * @param {Date} date - The date and time; defaults to the current date and time
14
+ * @param {string} phone - The phone number; defaults to a generic Kenyan phone number
15
+ * @param {string} identityId - A default identity ID; defaults to a random UUID
16
+ * @returns A generic authenticated user.
17
+ */
18
+ authenticatedUser: (id?: string, date?: Date, phone?: string, identityId?: string) => UserWithAppMetadata;
19
+ };
8
20
  export declare const Status: {
9
21
  /**
10
22
  * A generic error message.
@@ -173,4 +185,20 @@ export declare const Utility: {
173
185
  * @returns `true` if the handle is valid; `false` otherwise
174
186
  */
175
187
  validateSocialMediaHandle: (handle: string) => boolean;
188
+ /**
189
+ * Validates a SuprSend subscriber. If the subscriber does not exist, it will be created with the provided person data.
190
+ * @param {suprsend.Suprsend} suprSend - The SuprSend instance.
191
+ * @param {string} userId - The user ID.
192
+ * @param {Object} personData - The person data; required if the subscriber does not exist.
193
+ * @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.
194
+ */
195
+ validateSuprSendSubscriber: (suprSend: suprsend.Suprsend, userId: string, personData?: {
196
+ firstName: string;
197
+ lastName: string;
198
+ phone: string;
199
+ pseudonym?: string;
200
+ }) => Promise<{
201
+ error?: CustomError;
202
+ success?: true;
203
+ }>;
176
204
  };
package/dist/utils.js CHANGED
@@ -1,9 +1,67 @@
1
+ import { v4 as uuidv4 } from "uuid";
1
2
  import { error as svelteError } from "@sveltejs/kit";
2
3
  import { FunctionsFetchError, FunctionsHttpError, FunctionsRelayError } from "@supabase/supabase-js";
3
4
  import sanitize from "sanitize-html";
4
5
  import validator from "validator";
5
6
  /* Helper functions */
6
7
  const { isMobilePhone, isURL } = validator;
8
+ /* Dummy data */
9
+ export const Data = {
10
+ /**
11
+ * A generic authenticated user.
12
+ * @param {string} id - The user ID; defaults to a random UUID
13
+ * @param {Date} date - The date and time; defaults to the current date and time
14
+ * @param {string} phone - The phone number; defaults to a generic Kenyan phone number
15
+ * @param {string} identityId - A default identity ID; defaults to a random UUID
16
+ * @returns A generic authenticated user.
17
+ */
18
+ authenticatedUser: (id = uuidv4(), date = new Date(), phone = "254111222333", identityId = uuidv4()) => {
19
+ // Convert date to ISO string
20
+ const time = date.toISOString();
21
+ // User data
22
+ return {
23
+ id,
24
+ aud: "authenticated",
25
+ role: "authenticated",
26
+ email: "",
27
+ phone,
28
+ phone_confirmed_at: time,
29
+ confirmation_sent_at: time,
30
+ confirmed_at: time,
31
+ last_sign_in_at: time,
32
+ app_metadata: {
33
+ person_data: { first_name: "John", last_name: "Test", pseudonym: "JDtest" },
34
+ provider: "phone",
35
+ providers: ["phone"],
36
+ roles: ["player", "backroom_superuser"]
37
+ },
38
+ user_metadata: {
39
+ email_verified: false,
40
+ phone_verified: false,
41
+ sub: id
42
+ },
43
+ identities: [
44
+ {
45
+ identity_id: identityId,
46
+ id,
47
+ user_id: id,
48
+ identity_data: {
49
+ email_verified: false,
50
+ phone_verified: false,
51
+ sub: id
52
+ },
53
+ provider: "phone",
54
+ last_sign_in_at: time,
55
+ created_at: time,
56
+ updated_at: time
57
+ }
58
+ ],
59
+ created_at: time,
60
+ updated_at: time,
61
+ is_anonymous: false
62
+ };
63
+ }
64
+ };
7
65
  /* Status messages */
8
66
  export const Status = {
9
67
  /**
@@ -690,5 +748,43 @@ export const Utility = {
690
748
  */
691
749
  validateSocialMediaHandle: (handle) => !isURL(handle, { require_protocol: false }) &&
692
750
  !handle.startsWith("@") &&
693
- Boolean(handle.match(Utility.socialMediaHandleRegex))
751
+ Boolean(handle.match(Utility.socialMediaHandleRegex)),
752
+ /**
753
+ * Validates a SuprSend subscriber. If the subscriber does not exist, it will be created with the provided person data.
754
+ * @param {suprsend.Suprsend} suprSend - The SuprSend instance.
755
+ * @param {string} userId - The user ID.
756
+ * @param {Object} personData - The person data; required if the subscriber does not exist.
757
+ * @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.
758
+ */
759
+ validateSuprSendSubscriber: async (suprSend, userId, personData) => {
760
+ // Check if subscriber exists
761
+ try {
762
+ await suprSend.users.get(userId);
763
+ }
764
+ catch (_err) {
765
+ // If person data is not provided, return an error
766
+ if (!personData)
767
+ return { error: Utility.customError(400, "Person data is required to create a SuprSend subscriber.") };
768
+ // Destructure person data
769
+ const { firstName, lastName, phone, pseudonym } = personData;
770
+ // Create subscriber
771
+ const subscriber = suprSend.user.get_instance(userId);
772
+ if (!subscriber)
773
+ return { error: Utility.customError(500, Status.ERROR) };
774
+ // Set subscriber details
775
+ subscriber.set_preferred_language("en");
776
+ subscriber.set_timezone("Africa/Nairobi");
777
+ subscriber.set("firstName", firstName);
778
+ subscriber.set("lastName", lastName);
779
+ subscriber.set("phone", `+${phone}`);
780
+ if (pseudonym)
781
+ subscriber.set("pseudonym", pseudonym);
782
+ // Save subscriber
783
+ const response = await subscriber.save();
784
+ if (!response.success)
785
+ return { error: Utility.customError(500, Status.ERROR) };
786
+ }
787
+ // Return success
788
+ return { success: true };
789
+ }
694
790
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koloseum/utils",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "author": "Koloseum Technologies Limited",
5
5
  "type": "module",
6
6
  "description": "Utility logic for use across Koloseum web apps (TypeScript)",
@@ -37,8 +37,10 @@
37
37
  "devDependencies": {
38
38
  "@koloseum/types": "^0.1.12",
39
39
  "@playwright/test": "^1.50.1",
40
+ "@suprsend/node-sdk": "^1.13.0",
40
41
  "@suprsend/web-components": "^0.2.1",
41
42
  "@types/sanitize-html": "^2.13.0",
43
+ "@types/uuid": "^10.0.0",
42
44
  "@types/validator": "^13.12.2",
43
45
  "prettier": "^3.5.1",
44
46
  "typescript": "^5.0.0",