@koloseum/utils 0.1.10 → 0.1.11

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,7 +1,9 @@
1
- import type { CustomError } from "@koloseum/types/general";
1
+ import type { CustomError, UserWithAppMetadata } from "@koloseum/types/general";
2
2
  import type { PronounsCheckboxes, SocialMediaPlatform } from "@koloseum/types/public-auth";
3
+ import type { SubscriberResponseDto } from "@novu/api/models/components/subscriberresponsedto.js";
3
4
  import type { PostgrestError } from "@supabase/supabase-js";
4
5
  import type { MobilePhoneLocale } from "validator";
6
+ import { Novu } from "@novu/api";
5
7
  export declare const Status: {
6
8
  /**
7
9
  * A generic error message.
@@ -55,6 +57,13 @@ export declare const Utility: {
55
57
  * @returns The age in years, or `NaN` if the input is invalid
56
58
  */
57
59
  getAge: (dateOfBirth: string) => number;
60
+ /**
61
+ * Get a Novu subscriber.
62
+ * @param novu - The Novu client.
63
+ * @param user - The user object.
64
+ * @returns The subscriber object or `null` if no Novu client or user is provided.
65
+ */
66
+ getNovuSubscriber: (novu: Novu, user: UserWithAppMetadata | null) => Promise<SubscriberResponseDto | null>;
58
67
  /**
59
68
  * Handles the click event for pronouns checkboxes.
60
69
  * @param {MouseEvent} e - The click event
package/dist/utils.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { error as svelteError } from "@sveltejs/kit";
2
+ import { ErrorDto } from "@novu/api/models/errors/errordto.js";
2
3
  import sanitize from "sanitize-html";
3
4
  import validator from "validator";
4
5
  /* Helper functions */
@@ -108,6 +109,48 @@ export const Utility = {
108
109
  const monthDiff = currentDate.getMonth() - birthDate.getMonth();
109
110
  return monthDiff < 0 || (monthDiff === 0 && currentDate.getDate() < birthDate.getDate()) ? --age : age;
110
111
  },
112
+ /**
113
+ * Get a Novu subscriber.
114
+ * @param novu - The Novu client.
115
+ * @param user - The user object.
116
+ * @returns The subscriber object or `null` if no Novu client or user is provided.
117
+ */
118
+ getNovuSubscriber: async (novu, user) => {
119
+ // Initialise subscriber as null
120
+ let subscriber = null;
121
+ // Return null if no Novu client or user is provided
122
+ if (!novu || !user)
123
+ return subscriber;
124
+ // Return null if no person data is provided
125
+ if (!user.app_metadata.person_data)
126
+ return subscriber;
127
+ try {
128
+ // Get subscriber
129
+ ({ result: subscriber } = await novu.subscribers.retrieve(user.id));
130
+ }
131
+ catch (error) {
132
+ // Create subscriber if not found
133
+ if (error instanceof ErrorDto && error.statusCode === 404 && user.app_metadata.person_data) {
134
+ // Get person data from user metadata
135
+ const { first_name: firstName, last_name: lastName, pseudonym } = user.app_metadata.person_data;
136
+ // Create subscriber
137
+ ({ result: subscriber } = await novu.subscribers.create({
138
+ subscriberId: user.id,
139
+ firstName,
140
+ lastName,
141
+ phone: user.phone,
142
+ timezone: "Africa/Nairobi",
143
+ locale: "en_KE",
144
+ data: { pseudonym }
145
+ }));
146
+ }
147
+ // Otherwise, log error
148
+ else
149
+ console.error(error);
150
+ }
151
+ // Return subscriber
152
+ return subscriber;
153
+ },
111
154
  /**
112
155
  * Handles the click event for pronouns checkboxes.
113
156
  * @param {MouseEvent} e - The click event
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koloseum/utils",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "author": "Koloseum Technologies Limited",
5
5
  "type": "module",
6
6
  "description": "Utility logic for use across Koloseum web apps (TypeScript)",
@@ -29,13 +29,14 @@
29
29
  "test": "vitest --run"
30
30
  },
31
31
  "dependencies": {
32
+ "@novu/api": "^0.6.1",
32
33
  "@supabase/supabase-js": "^2.48.1",
33
34
  "@sveltejs/kit": "^2.17.1",
34
35
  "sanitize-html": "^2.14.0",
35
36
  "validator": "^13.12.0"
36
37
  },
37
38
  "devDependencies": {
38
- "@koloseum/types": "^0.1.9",
39
+ "@koloseum/types": "^0.1.11",
39
40
  "@playwright/test": "^1.50.1",
40
41
  "@types/sanitize-html": "^2.13.0",
41
42
  "@types/validator": "^13.12.2",