@terreno/comms 57.3.0 → 57.6.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.
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "author": "Josh Gachnang",
3
3
  "bugs": {
4
- "url": "https://github.com/FlourishHealth/terreno/issues"
4
+ "url": "https://github.com/TerrenoLabs/terreno/issues"
5
5
  },
6
6
  "dependencies": {
7
- "@terreno/api": "57.3.0",
7
+ "@terreno/api": "57.6.0",
8
8
  "libphonenumber-js": "^1.13.8",
9
9
  "luxon": "^3.7.2"
10
10
  },
11
11
  "description": "Pluggable mail, SMS, push, and verification providers — part of Terreno, the batteries-included TypeScript framework for universal apps.",
12
12
  "devDependencies": {
13
13
  "@biomejs/biome": "^2.3.6",
14
- "@terreno/test": "57.3.0",
14
+ "@terreno/test": "57.6.0",
15
15
  "@types/bun": "^1.4.0",
16
16
  "@types/express": "^5.0.6",
17
17
  "@types/luxon": "^3.7.1",
@@ -22,27 +22,27 @@
22
22
  },
23
23
  "exports": {
24
24
  ".": {
25
- "types": "./dist/index.d.ts",
26
- "default": "./dist/index.js"
25
+ "default": "./dist/index.js",
26
+ "types": "./dist/index.d.ts"
27
27
  },
28
28
  "./adapters/expoPush": {
29
- "types": "./dist/adapters/expoPush.d.ts",
30
- "default": "./dist/adapters/expoPush.js"
29
+ "default": "./dist/adapters/expoPush.js",
30
+ "types": "./dist/adapters/expoPush.d.ts"
31
31
  },
32
32
  "./adapters/sendgrid": {
33
- "types": "./dist/adapters/sendgrid.d.ts",
34
- "default": "./dist/adapters/sendgrid.js"
33
+ "default": "./dist/adapters/sendgrid.js",
34
+ "types": "./dist/adapters/sendgrid.d.ts"
35
35
  },
36
36
  "./adapters/twilioSms": {
37
- "types": "./dist/adapters/twilioSms.d.ts",
38
- "default": "./dist/adapters/twilioSms.js"
37
+ "default": "./dist/adapters/twilioSms.js",
38
+ "types": "./dist/adapters/twilioSms.d.ts"
39
39
  },
40
40
  "./adapters/twilioVerify": {
41
- "types": "./dist/adapters/twilioVerify.d.ts",
42
- "default": "./dist/adapters/twilioVerify.js"
41
+ "default": "./dist/adapters/twilioVerify.js",
42
+ "types": "./dist/adapters/twilioVerify.d.ts"
43
43
  }
44
44
  },
45
- "homepage": "https://github.com/FlourishHealth/terreno#readme",
45
+ "homepage": "https://github.com/TerrenoLabs/terreno#readme",
46
46
  "keywords": [
47
47
  "communications",
48
48
  "email",
@@ -75,7 +75,7 @@
75
75
  },
76
76
  "repository": {
77
77
  "type": "git",
78
- "url": "git+ssh://git@github.com/FlourishHealth/terreno.git"
78
+ "url": "git+ssh://git@github.com/TerrenoLabs/terreno.git"
79
79
  },
80
80
  "scripts": {
81
81
  "compile": "bun tsc",
@@ -89,5 +89,5 @@
89
89
  "test:coverage": "bun run ../scripts/check-coverage.ts"
90
90
  },
91
91
  "types": "dist/index.d.ts",
92
- "version": "57.3.0"
92
+ "version": "57.6.0"
93
93
  }
package/src/commsQuery.ts CHANGED
@@ -77,6 +77,7 @@ const parseIsoDate = (value: string, field: "startDate" | "endDate"): DateTime =
77
77
  return parsed;
78
78
  };
79
79
 
80
+ /** @internal */
80
81
  export const defaultStatsRange = (): {endDate: DateTime; startDate: DateTime} => {
81
82
  const endDate = DateTime.utc();
82
83
  return {endDate, startDate: endDate.minus({days: DEFAULT_STATS_DAYS})};
package/src/commsRetry.ts CHANGED
@@ -4,9 +4,9 @@ import {DateTime} from "luxon";
4
4
  import type {CommsMessageDocument} from "./modelTypes";
5
5
  import type {CommsChannel} from "./types";
6
6
 
7
- export const COMMS_RETRY_NOT_RETRYABLE = "comms-retry-not-retryable";
8
- export const COMMS_RETRY_PAYLOAD_EXPIRED = "comms-retry-payload-expired";
9
- export const COMMS_RETRY_CHANNEL_UNCONFIGURED = "comms-retry-channel-unconfigured";
7
+ const COMMS_RETRY_NOT_RETRYABLE = "comms-retry-not-retryable";
8
+ const COMMS_RETRY_PAYLOAD_EXPIRED = "comms-retry-payload-expired";
9
+ const COMMS_RETRY_CHANNEL_UNCONFIGURED = "comms-retry-channel-unconfigured";
10
10
 
11
11
  export interface CommsRetryBlock {
12
12
  code: string;
@@ -15,7 +15,7 @@ export interface CommsRetryBlock {
15
15
 
16
16
  const RETRYABLE_STATUSES = new Set(["bounced", "failed"]);
17
17
 
18
- export const isPayloadPresent = (message: CommsMessageDocument): boolean => {
18
+ const isPayloadPresent = (message: CommsMessageDocument): boolean => {
19
19
  if (message.payload === undefined || message.payload === null) {
20
20
  return false;
21
21
  }
@@ -712,6 +712,66 @@ describe("CommsService", () => {
712
712
  assert.equal(result.loggedMessageId, String(row._id));
713
713
  });
714
714
 
715
+ it("passes the send option userId to mail and SMS beforeSend hooks", async (): Promise<void> => {
716
+ const seenUserIds: Array<string | undefined> = [];
717
+ const mailUserId = new mongoose.Types.ObjectId();
718
+ const smsUserId = new mongoose.Types.ObjectId();
719
+ const service = new CommsService({
720
+ beforeSend: async (context): Promise<undefined> => {
721
+ seenUserIds.push(context.userId);
722
+ return undefined;
723
+ },
724
+ mail: {
725
+ id: "user-aware-mail",
726
+ sendMail: async (): Promise<SendResult> => ({accepted: true}),
727
+ },
728
+ sms: {
729
+ id: "user-aware-sms",
730
+ sendSms: async (): Promise<SendResult> => ({accepted: true}),
731
+ },
732
+ });
733
+
734
+ await service.sendMail({subject: "Welcome", to: "person@example.com"}, {userId: mailUserId});
735
+ await service.sendSms({body: "Hello", to: "+15555550100"}, {userId: smsUserId});
736
+
737
+ assert.deepEqual(seenUserIds, [String(mailUserId), String(smsUserId)]);
738
+ const mailLog = await CommsMessage.findExactlyOne({channel: "mail"});
739
+ const smsLog = await CommsMessage.findExactlyOne({channel: "sms"});
740
+ assert.equal(String(mailLog.userId), String(mailUserId));
741
+ assert.equal(String(smsLog.userId), String(smsUserId));
742
+ });
743
+
744
+ it("preserves the original userId when retrying mail", async (): Promise<void> => {
745
+ const userId = new mongoose.Types.ObjectId();
746
+ const seenUserIds: Array<string | undefined> = [];
747
+ const service = new CommsService({
748
+ beforeSend: async (context): Promise<undefined> => {
749
+ seenUserIds.push(context.userId);
750
+ return undefined;
751
+ },
752
+ mail: {
753
+ id: "retry-user-aware-mail",
754
+ sendMail: async (): Promise<SendResult> => ({accepted: true}),
755
+ },
756
+ });
757
+ const original = await CommsMessage.create({
758
+ channel: "mail",
759
+ payload: {subject: "Retry me", to: "person@example.com"},
760
+ payloadExpiresAt: DateTime.utc().plus({days: 1}).toJSDate(),
761
+ provider: "retry-user-aware-mail",
762
+ status: "failed",
763
+ subject: "Retry me",
764
+ to: "person@example.com",
765
+ userId,
766
+ });
767
+
768
+ await service.retryMessage({messageId: String(original._id)});
769
+
770
+ assert.deepEqual(seenUserIds, [String(userId)]);
771
+ const retried = await CommsMessage.findExactlyOne({retriedFromId: original._id});
772
+ assert.equal(String(retried.userId), String(userId));
773
+ });
774
+
715
775
  it("attaches loggedMessageId when beforeSend cancels a push send", async (): Promise<void> => {
716
776
  const userId = new mongoose.Types.ObjectId();
717
777
  await PushToken.upsert(
@@ -632,6 +632,7 @@ export class CommsService {
632
632
  isRetry: sendOptions?.isRetry === true,
633
633
  message: this.applyMailDefaults(message),
634
634
  provider: provider.id,
635
+ userId: sendOptions?.userId ? String(sendOptions.userId) : undefined,
635
636
  };
636
637
  const before = await this.applyBeforeSend(context, hookErrors);
637
638
  const activeMessage = this.applyMailDefaults(before.message as MailMessage);
@@ -651,6 +652,7 @@ export class CommsService {
651
652
  subject: activeMessage.subject,
652
653
  templateId: activeMessage.templateId,
653
654
  to: activeMessage.to,
655
+ userId: context.userId,
654
656
  });
655
657
  return this.withLoggedId(result, logged);
656
658
  }
@@ -667,6 +669,7 @@ export class CommsService {
667
669
  subject: activeMessage.subject,
668
670
  templateId: activeMessage.templateId,
669
671
  to: activeMessage.to,
672
+ userId: context.userId,
670
673
  },
671
674
  provider: provider.id,
672
675
  retry: (): Promise<SendResult> => this.sendMailOnce(provider, activeMessage),
@@ -682,6 +685,7 @@ export class CommsService {
682
685
  isRetry: sendOptions?.isRetry === true,
683
686
  message,
684
687
  provider: provider.id,
688
+ userId: sendOptions?.userId ? String(sendOptions.userId) : undefined,
685
689
  };
686
690
  const before = await this.applyBeforeSend(context, hookErrors);
687
691
  const activeMessage = before.message as SmsMessage;
@@ -699,6 +703,7 @@ export class CommsService {
699
703
  retriedFromId: sendOptions?.retriedFromId,
700
704
  status: "cancelled",
701
705
  to: activeMessage.to,
706
+ userId: context.userId,
702
707
  });
703
708
  return this.withLoggedId(result, logged);
704
709
  }
@@ -713,6 +718,7 @@ export class CommsService {
713
718
  metadata: this.mergeSendMetadata(first, sendOptions),
714
719
  retriedFromId: sendOptions?.retriedFromId,
715
720
  to: activeMessage.to,
721
+ userId: context.userId,
716
722
  },
717
723
  provider: provider.id,
718
724
  retry: (): Promise<SendResult> => this.sendSmsOnce(provider, activeMessage),
@@ -998,6 +1004,7 @@ export class CommsService {
998
1004
  : undefined,
999
1005
  isRetry: true,
1000
1006
  retriedFromId: String(original._id),
1007
+ userId: original.userId,
1001
1008
  };
1002
1009
  const payload = original.payload as Record<string, unknown>;
1003
1010
  let sendResult: SendResult | SendResult[] | undefined;
@@ -322,6 +322,3 @@ export const addCommsDashboardRoutes = (
322
322
  })
323
323
  );
324
324
  };
325
-
326
- /** @deprecated Use {@link addCommsDashboardRoutes}. */
327
- export const addCommsExplorerRoute = addCommsDashboardRoutes;
package/src/types.ts CHANGED
@@ -135,6 +135,7 @@ export interface CommsSendOptions {
135
135
  extraMetadata?: Record<string, unknown>;
136
136
  isRetry?: boolean;
137
137
  retriedFromId?: string;
138
+ userId?: mongoose.Types.ObjectId | string;
138
139
  }
139
140
 
140
141
  export interface RetryMessageOptions {
@@ -93,7 +93,7 @@ const applySendGridEvent = async ({
93
93
  }
94
94
  };
95
95
 
96
- export const registerSendGridCommsWebhooks = ({
96
+ const registerSendGridCommsWebhooks = ({
97
97
  basePath,
98
98
  publicKey,
99
99
  service,
@@ -41,7 +41,7 @@ const mapStatus = (
41
41
  return undefined;
42
42
  };
43
43
 
44
- export const registerTwilioCommsWebhooks = ({
44
+ const registerTwilioCommsWebhooks = ({
45
45
  authToken,
46
46
  basePath,
47
47
  publicUrl,
package/tsconfig.json CHANGED
@@ -1,32 +1,18 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "allowJs": true,
4
- "allowSyntheticDefaultImports": true,
5
- "baseUrl": "./",
6
3
  "declaration": true,
7
- "downlevelIteration": true,
8
- "esModuleInterop": true,
9
- "forceConsistentCasingInFileNames": true,
10
- "ignoreDeprecations": "6.0",
11
- "lib": ["es2017", "dom", "es2017.object"],
12
- "module": "commonjs",
13
- "moduleResolution": "node",
14
- "noEmit": false,
4
+ "isolatedModules": true,
5
+ "lib": ["es2023"],
6
+ "module": "nodenext",
15
7
  "noImplicitAny": false,
16
8
  "noImplicitReturns": true,
17
- "noImplicitThis": true,
18
- "noUnusedLocals": false,
19
- "noUnusedParameters": false,
20
9
  "outDir": "./dist",
21
- "preserveConstEnums": true,
22
- "removeComments": false,
23
10
  "rootDir": "./src",
24
11
  "skipLibCheck": true,
25
12
  "strict": true,
26
- "strictFunctionTypes": true,
27
- "strictNullChecks": true,
28
- "target": "es5"
13
+ "target": "es2023",
14
+ "types": ["node"]
29
15
  },
30
16
  "exclude": ["node_modules", "src/**/*.test.ts", "src/tests/**"],
31
- "include": ["src/**/*.ts", "src/*.ts"]
17
+ "include": ["src"]
32
18
  }