@terreno/comms 57.3.0 → 57.4.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/dist/commsQuery.d.ts +1 -0
- package/dist/commsQuery.js +1 -0
- package/dist/commsRetry.d.ts +0 -4
- package/dist/commsRetry.js +10 -11
- package/dist/commsService.js +7 -0
- package/dist/routes/commsDashboard.d.ts +0 -2
- package/dist/routes/commsDashboard.js +1 -3
- package/dist/types.d.ts +1 -0
- package/dist/webhooks/sendgridWebhooks.d.ts +0 -6
- package/dist/webhooks/sendgridWebhooks.js +2 -3
- package/dist/webhooks/twilioWebhooks.d.ts +0 -7
- package/dist/webhooks/twilioWebhooks.js +2 -3
- package/package.json +13 -13
- package/src/commsQuery.ts +1 -0
- package/src/commsRetry.ts +4 -4
- package/src/commsService.test.ts +60 -0
- package/src/commsService.ts +7 -0
- package/src/routes/commsDashboard.ts +0 -3
- package/src/types.ts +1 -0
- package/src/webhooks/sendgridWebhooks.ts +1 -1
- package/src/webhooks/twilioWebhooks.ts +1 -1
package/dist/commsQuery.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export interface CommsListPagination {
|
|
|
21
21
|
export declare const parseCommsListFilters: (source: Record<string, unknown>) => CommsListFilters;
|
|
22
22
|
export declare const parseCommsListPagination: (source: Record<string, unknown>) => CommsListPagination;
|
|
23
23
|
export declare const parseRetryManyLimit: (value: unknown) => number;
|
|
24
|
+
/** @internal */
|
|
24
25
|
export declare const defaultStatsRange: () => {
|
|
25
26
|
endDate: DateTime;
|
|
26
27
|
startDate: DateTime;
|
package/dist/commsQuery.js
CHANGED
|
@@ -55,6 +55,7 @@ var parseIsoDate = function (value, field) {
|
|
|
55
55
|
}
|
|
56
56
|
return parsed;
|
|
57
57
|
};
|
|
58
|
+
/** @internal */
|
|
58
59
|
var defaultStatsRange = function () {
|
|
59
60
|
var endDate = luxon_1.DateTime.utc();
|
|
60
61
|
return { endDate: endDate, startDate: endDate.minus({ days: DEFAULT_STATS_DAYS }) };
|
package/dist/commsRetry.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import type { CommsMessageDocument } from "./modelTypes";
|
|
2
2
|
import type { CommsChannel } from "./types";
|
|
3
|
-
export declare const COMMS_RETRY_NOT_RETRYABLE = "comms-retry-not-retryable";
|
|
4
|
-
export declare const COMMS_RETRY_PAYLOAD_EXPIRED = "comms-retry-payload-expired";
|
|
5
|
-
export declare const COMMS_RETRY_CHANNEL_UNCONFIGURED = "comms-retry-channel-unconfigured";
|
|
6
3
|
export interface CommsRetryBlock {
|
|
7
4
|
code: string;
|
|
8
5
|
title: string;
|
|
9
6
|
}
|
|
10
|
-
export declare const isPayloadPresent: (message: CommsMessageDocument) => boolean;
|
|
11
7
|
export declare const evaluateRetryBlock: ({ isChannelConfigured, message, }: {
|
|
12
8
|
isChannelConfigured: (channel: CommsChannel) => boolean;
|
|
13
9
|
message: CommsMessageDocument;
|
package/dist/commsRetry.js
CHANGED
|
@@ -11,12 +11,12 @@ var __assign = (this && this.__assign) || function () {
|
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.serializeCommsMessage = exports.throwRetryBlock = exports.evaluateRetryBlock =
|
|
14
|
+
exports.serializeCommsMessage = exports.throwRetryBlock = exports.evaluateRetryBlock = void 0;
|
|
15
15
|
var api_1 = require("@terreno/api");
|
|
16
16
|
var luxon_1 = require("luxon");
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
var COMMS_RETRY_NOT_RETRYABLE = "comms-retry-not-retryable";
|
|
18
|
+
var COMMS_RETRY_PAYLOAD_EXPIRED = "comms-retry-payload-expired";
|
|
19
|
+
var COMMS_RETRY_CHANNEL_UNCONFIGURED = "comms-retry-channel-unconfigured";
|
|
20
20
|
var RETRYABLE_STATUSES = new Set(["bounced", "failed"]);
|
|
21
21
|
var isPayloadPresent = function (message) {
|
|
22
22
|
if (message.payload === undefined || message.payload === null) {
|
|
@@ -27,36 +27,35 @@ var isPayloadPresent = function (message) {
|
|
|
27
27
|
}
|
|
28
28
|
return true;
|
|
29
29
|
};
|
|
30
|
-
exports.isPayloadPresent = isPayloadPresent;
|
|
31
30
|
var evaluateRetryBlock = function (_a) {
|
|
32
31
|
var isChannelConfigured = _a.isChannelConfigured, message = _a.message;
|
|
33
32
|
if (message.channel === "verification") {
|
|
34
33
|
return {
|
|
35
|
-
code:
|
|
34
|
+
code: COMMS_RETRY_NOT_RETRYABLE,
|
|
36
35
|
title: "Verification messages are not retryable",
|
|
37
36
|
};
|
|
38
37
|
}
|
|
39
38
|
if (!RETRYABLE_STATUSES.has(message.status)) {
|
|
40
39
|
return {
|
|
41
|
-
code:
|
|
40
|
+
code: COMMS_RETRY_NOT_RETRYABLE,
|
|
42
41
|
title: "Only failed or bounced messages can be retried",
|
|
43
42
|
};
|
|
44
43
|
}
|
|
45
44
|
if (message.errorClass === "permanent") {
|
|
46
45
|
return {
|
|
47
|
-
code:
|
|
46
|
+
code: COMMS_RETRY_NOT_RETRYABLE,
|
|
48
47
|
title: "Permanent failures cannot be retried",
|
|
49
48
|
};
|
|
50
49
|
}
|
|
51
|
-
if (!
|
|
50
|
+
if (!isPayloadPresent(message)) {
|
|
52
51
|
return {
|
|
53
|
-
code:
|
|
52
|
+
code: COMMS_RETRY_PAYLOAD_EXPIRED,
|
|
54
53
|
title: "Retained payload is missing or expired",
|
|
55
54
|
};
|
|
56
55
|
}
|
|
57
56
|
if (!isChannelConfigured(message.channel)) {
|
|
58
57
|
return {
|
|
59
|
-
code:
|
|
58
|
+
code: COMMS_RETRY_CHANNEL_UNCONFIGURED,
|
|
60
59
|
title: "Comms channel is not configured",
|
|
61
60
|
};
|
|
62
61
|
}
|
package/dist/commsService.js
CHANGED
|
@@ -719,6 +719,7 @@ var CommsService = /** @class */ (function () {
|
|
|
719
719
|
isRetry: (sendOptions === null || sendOptions === void 0 ? void 0 : sendOptions.isRetry) === true,
|
|
720
720
|
message: this.applyMailDefaults(message),
|
|
721
721
|
provider: provider.id,
|
|
722
|
+
userId: (sendOptions === null || sendOptions === void 0 ? void 0 : sendOptions.userId) ? String(sendOptions.userId) : undefined,
|
|
722
723
|
};
|
|
723
724
|
return [4 /*yield*/, this.applyBeforeSend(context, hookErrors)];
|
|
724
725
|
case 1:
|
|
@@ -740,6 +741,7 @@ var CommsService = /** @class */ (function () {
|
|
|
740
741
|
subject: activeMessage.subject,
|
|
741
742
|
templateId: activeMessage.templateId,
|
|
742
743
|
to: activeMessage.to,
|
|
744
|
+
userId: context.userId,
|
|
743
745
|
})];
|
|
744
746
|
case 2:
|
|
745
747
|
logged = _a.sent();
|
|
@@ -758,6 +760,7 @@ var CommsService = /** @class */ (function () {
|
|
|
758
760
|
subject: activeMessage.subject,
|
|
759
761
|
templateId: activeMessage.templateId,
|
|
760
762
|
to: activeMessage.to,
|
|
763
|
+
userId: context.userId,
|
|
761
764
|
},
|
|
762
765
|
provider: provider.id,
|
|
763
766
|
retry: function () { return _this.sendMailOnce(provider, activeMessage); },
|
|
@@ -781,6 +784,7 @@ var CommsService = /** @class */ (function () {
|
|
|
781
784
|
isRetry: (sendOptions === null || sendOptions === void 0 ? void 0 : sendOptions.isRetry) === true,
|
|
782
785
|
message: message,
|
|
783
786
|
provider: provider.id,
|
|
787
|
+
userId: (sendOptions === null || sendOptions === void 0 ? void 0 : sendOptions.userId) ? String(sendOptions.userId) : undefined,
|
|
784
788
|
};
|
|
785
789
|
return [4 /*yield*/, this.applyBeforeSend(context, hookErrors)];
|
|
786
790
|
case 1:
|
|
@@ -800,6 +804,7 @@ var CommsService = /** @class */ (function () {
|
|
|
800
804
|
retriedFromId: sendOptions === null || sendOptions === void 0 ? void 0 : sendOptions.retriedFromId,
|
|
801
805
|
status: "cancelled",
|
|
802
806
|
to: activeMessage.to,
|
|
807
|
+
userId: context.userId,
|
|
803
808
|
})];
|
|
804
809
|
case 2:
|
|
805
810
|
logged = _a.sent();
|
|
@@ -816,6 +821,7 @@ var CommsService = /** @class */ (function () {
|
|
|
816
821
|
metadata: this.mergeSendMetadata(first, sendOptions),
|
|
817
822
|
retriedFromId: sendOptions === null || sendOptions === void 0 ? void 0 : sendOptions.retriedFromId,
|
|
818
823
|
to: activeMessage.to,
|
|
824
|
+
userId: context.userId,
|
|
819
825
|
},
|
|
820
826
|
provider: provider.id,
|
|
821
827
|
retry: function () { return _this.sendSmsOnce(provider, activeMessage); },
|
|
@@ -1168,6 +1174,7 @@ var CommsService = /** @class */ (function () {
|
|
|
1168
1174
|
: undefined,
|
|
1169
1175
|
isRetry: true,
|
|
1170
1176
|
retriedFromId: String(original._id),
|
|
1177
|
+
userId: original.userId,
|
|
1171
1178
|
};
|
|
1172
1179
|
payload = original.payload;
|
|
1173
1180
|
if (!(original.channel === "mail")) return [3 /*break*/, 3];
|
|
@@ -6,6 +6,4 @@ interface CommsDashboardRouteOptions {
|
|
|
6
6
|
service: CommsService;
|
|
7
7
|
}
|
|
8
8
|
export declare const addCommsDashboardRoutes: (app: express.Application, options: CommsDashboardRouteOptions) => void;
|
|
9
|
-
/** @deprecated Use {@link addCommsDashboardRoutes}. */
|
|
10
|
-
export declare const addCommsExplorerRoute: (app: express.Application, options: CommsDashboardRouteOptions) => void;
|
|
11
9
|
export {};
|
|
@@ -86,7 +86,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
86
86
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
87
87
|
};
|
|
88
88
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
89
|
-
exports.
|
|
89
|
+
exports.addCommsDashboardRoutes = void 0;
|
|
90
90
|
var api_1 = require("@terreno/api");
|
|
91
91
|
var mongoose_1 = __importDefault(require("mongoose"));
|
|
92
92
|
var commsQuery_1 = require("../commsQuery");
|
|
@@ -410,5 +410,3 @@ var addCommsDashboardRoutes = function (app, options) {
|
|
|
410
410
|
}); }));
|
|
411
411
|
};
|
|
412
412
|
exports.addCommsDashboardRoutes = addCommsDashboardRoutes;
|
|
413
|
-
/** @deprecated Use {@link addCommsDashboardRoutes}. */
|
|
414
|
-
exports.addCommsExplorerRoute = exports.addCommsDashboardRoutes;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import { type WebhooksApp } from "@terreno/api";
|
|
2
2
|
import type { CommsService } from "../commsService";
|
|
3
|
-
export declare const registerSendGridCommsWebhooks: ({ basePath, publicKey, service, webhooks, }: {
|
|
4
|
-
basePath: string;
|
|
5
|
-
publicKey: string;
|
|
6
|
-
service: CommsService;
|
|
7
|
-
webhooks: WebhooksApp;
|
|
8
|
-
}) => void;
|
|
9
3
|
export declare const maybeRegisterSendGridCommsWebhooks: ({ basePath, mail, service, webhooks, }: {
|
|
10
4
|
basePath: string;
|
|
11
5
|
mail?: {
|
|
@@ -47,7 +47,7 @@ var __values = (this && this.__values) || function(o) {
|
|
|
47
47
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
48
|
};
|
|
49
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.maybeRegisterSendGridCommsWebhooks =
|
|
50
|
+
exports.maybeRegisterSendGridCommsWebhooks = void 0;
|
|
51
51
|
var api_1 = require("@terreno/api");
|
|
52
52
|
var sendgrid_1 = require("../adapters/sendgrid");
|
|
53
53
|
var commsMessage_1 = require("../models/commsMessage");
|
|
@@ -230,7 +230,6 @@ var registerSendGridCommsWebhooks = function (_a) {
|
|
|
230
230
|
verify: (0, api_1.sendgridEventSignature)({ publicKey: publicKey }),
|
|
231
231
|
});
|
|
232
232
|
};
|
|
233
|
-
exports.registerSendGridCommsWebhooks = registerSendGridCommsWebhooks;
|
|
234
233
|
var maybeRegisterSendGridCommsWebhooks = function (_a) {
|
|
235
234
|
var basePath = _a.basePath, mail = _a.mail, service = _a.service, webhooks = _a.webhooks;
|
|
236
235
|
if (!webhooks || !(mail instanceof sendgrid_1.SendGridMailProvider)) {
|
|
@@ -241,7 +240,7 @@ var maybeRegisterSendGridCommsWebhooks = function (_a) {
|
|
|
241
240
|
api_1.logger.error("[comms] Skipping SendGrid webhook routes; set webhookVerificationKey or SENDGRID_WEBHOOK_VERIFICATION_KEY");
|
|
242
241
|
return;
|
|
243
242
|
}
|
|
244
|
-
|
|
243
|
+
registerSendGridCommsWebhooks({
|
|
245
244
|
basePath: basePath,
|
|
246
245
|
publicKey: publicKey,
|
|
247
246
|
service: service,
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import { type WebhooksApp } from "@terreno/api";
|
|
2
2
|
import type { CommsService } from "../commsService";
|
|
3
|
-
export declare const registerTwilioCommsWebhooks: ({ authToken, basePath, publicUrl, service, webhooks, }: {
|
|
4
|
-
authToken: string;
|
|
5
|
-
basePath: string;
|
|
6
|
-
publicUrl: string;
|
|
7
|
-
service: CommsService;
|
|
8
|
-
webhooks: WebhooksApp;
|
|
9
|
-
}) => void;
|
|
10
3
|
export declare const maybeRegisterTwilioCommsWebhooks: ({ basePath, publicUrl, service, sms, webhooks, }: {
|
|
11
4
|
basePath: string;
|
|
12
5
|
publicUrl?: string;
|
|
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.maybeRegisterTwilioCommsWebhooks =
|
|
39
|
+
exports.maybeRegisterTwilioCommsWebhooks = void 0;
|
|
40
40
|
var api_1 = require("@terreno/api");
|
|
41
41
|
var twilioSms_1 = require("../adapters/twilioSms");
|
|
42
42
|
var formString = function (body, key) {
|
|
@@ -146,7 +146,6 @@ var registerTwilioCommsWebhooks = function (_a) {
|
|
|
146
146
|
verify: (0, api_1.twilioSignature)({ authToken: authToken, url: inboundUrl }),
|
|
147
147
|
});
|
|
148
148
|
};
|
|
149
|
-
exports.registerTwilioCommsWebhooks = registerTwilioCommsWebhooks;
|
|
150
149
|
var maybeRegisterTwilioCommsWebhooks = function (_a) {
|
|
151
150
|
var _b, _c;
|
|
152
151
|
var basePath = _a.basePath, publicUrl = _a.publicUrl, service = _a.service, sms = _a.sms, webhooks = _a.webhooks;
|
|
@@ -164,7 +163,7 @@ var maybeRegisterTwilioCommsWebhooks = function (_a) {
|
|
|
164
163
|
return;
|
|
165
164
|
}
|
|
166
165
|
sms.applyDefaultStatusCallbackUrl("".concat(resolvedPublicUrl).concat(basePath, "/webhooks/twilio/status"));
|
|
167
|
-
|
|
166
|
+
registerTwilioCommsWebhooks({
|
|
168
167
|
authToken: authToken,
|
|
169
168
|
basePath: basePath,
|
|
170
169
|
publicUrl: resolvedPublicUrl,
|
package/package.json
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
"url": "https://github.com/FlourishHealth/terreno/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@terreno/api": "57.
|
|
7
|
+
"@terreno/api": "57.4.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.
|
|
14
|
+
"@terreno/test": "57.4.0",
|
|
15
15
|
"@types/bun": "^1.4.0",
|
|
16
16
|
"@types/express": "^5.0.6",
|
|
17
17
|
"@types/luxon": "^3.7.1",
|
|
@@ -22,24 +22,24 @@
|
|
|
22
22
|
},
|
|
23
23
|
"exports": {
|
|
24
24
|
".": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
25
|
+
"default": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts"
|
|
27
27
|
},
|
|
28
28
|
"./adapters/expoPush": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"default": "./dist/adapters/expoPush.js",
|
|
30
|
+
"types": "./dist/adapters/expoPush.d.ts"
|
|
31
31
|
},
|
|
32
32
|
"./adapters/sendgrid": {
|
|
33
|
-
"
|
|
34
|
-
"
|
|
33
|
+
"default": "./dist/adapters/sendgrid.js",
|
|
34
|
+
"types": "./dist/adapters/sendgrid.d.ts"
|
|
35
35
|
},
|
|
36
36
|
"./adapters/twilioSms": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
37
|
+
"default": "./dist/adapters/twilioSms.js",
|
|
38
|
+
"types": "./dist/adapters/twilioSms.d.ts"
|
|
39
39
|
},
|
|
40
40
|
"./adapters/twilioVerify": {
|
|
41
|
-
"
|
|
42
|
-
"
|
|
41
|
+
"default": "./dist/adapters/twilioVerify.js",
|
|
42
|
+
"types": "./dist/adapters/twilioVerify.d.ts"
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
"homepage": "https://github.com/FlourishHealth/terreno#readme",
|
|
@@ -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.
|
|
92
|
+
"version": "57.4.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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
18
|
+
const isPayloadPresent = (message: CommsMessageDocument): boolean => {
|
|
19
19
|
if (message.payload === undefined || message.payload === null) {
|
|
20
20
|
return false;
|
|
21
21
|
}
|
package/src/commsService.test.ts
CHANGED
|
@@ -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(
|
package/src/commsService.ts
CHANGED
|
@@ -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;
|
package/src/types.ts
CHANGED