@terreno/comms 57.4.0 → 57.6.1
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/README.md +2 -2
- package/dist/adapters/console.js +48 -109
- package/dist/adapters/expoPush.js +159 -345
- package/dist/adapters/sendgrid.js +94 -164
- package/dist/adapters/twilioSms.js +96 -151
- package/dist/adapters/twilioVerify.js +70 -132
- package/dist/commsApp.js +27 -27
- package/dist/commsQuery.js +33 -34
- package/dist/commsRetry.js +17 -25
- package/dist/commsService.js +674 -1098
- package/dist/models/commsMessage.js +64 -172
- package/dist/models/pushToken.js +5 -5
- package/dist/routes/commsDashboard.js +174 -325
- package/dist/routes/pushTokens.js +122 -217
- package/dist/templates.js +27 -41
- package/dist/webhooks/sendgridWebhooks.js +109 -226
- package/dist/webhooks/twilioWebhooks.js +59 -117
- package/package.json +6 -6
- package/tsconfig.json +6 -20
|
@@ -1,73 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.TwilioVerifyProvider = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
var verifyServiceSid = (_c = options === null || options === void 0 ? void 0 : options.verifyServiceSid) !== null && _c !== void 0 ? _c : process.env.TWILIO_VERIFY_SERVICE_SID;
|
|
4
|
+
const node_module_1 = require("node:module");
|
|
5
|
+
const api_1 = require("@terreno/api");
|
|
6
|
+
const PERMANENT_CODES = new Set([60200, 60202, 60203]);
|
|
7
|
+
const TRANSIENT_CODES = new Set([20429]);
|
|
8
|
+
const CONFIG_CODES = new Set([20003, 20404]);
|
|
9
|
+
const TWILIO_VERIFY_CONSOLE_BASE = "https://console.twilio.com/us1/develop/verify/services";
|
|
10
|
+
const nodeRequire = (0, node_module_1.createRequire)(__filename);
|
|
11
|
+
const resolveCredentials = (options) => {
|
|
12
|
+
const accountSid = options?.accountSid ?? process.env.TWILIO_ACCOUNT_SID;
|
|
13
|
+
const authToken = options?.authToken ?? process.env.TWILIO_AUTH_TOKEN;
|
|
14
|
+
const verifyServiceSid = options?.verifyServiceSid ?? process.env.TWILIO_VERIFY_SERVICE_SID;
|
|
52
15
|
if (!accountSid || !authToken) {
|
|
53
16
|
throw new Error("TwilioVerifyProvider requires TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN or matching constructor options");
|
|
54
17
|
}
|
|
55
18
|
if (!verifyServiceSid) {
|
|
56
19
|
throw new Error("TwilioVerifyProvider requires TWILIO_VERIFY_SERVICE_SID or a verifyServiceSid constructor option");
|
|
57
20
|
}
|
|
58
|
-
return { accountSid
|
|
21
|
+
return { accountSid, authToken, verifyServiceSid };
|
|
59
22
|
};
|
|
60
|
-
|
|
23
|
+
const loadTwilio = () => {
|
|
61
24
|
try {
|
|
62
25
|
// Optional peer — keep `twilio` out of core dependencies.
|
|
63
26
|
return nodeRequire("twilio");
|
|
64
27
|
}
|
|
65
|
-
catch
|
|
28
|
+
catch {
|
|
66
29
|
throw new Error("TwilioVerifyProvider requires optional peer dependency twilio. " +
|
|
67
30
|
"Install it with: bun add twilio");
|
|
68
31
|
}
|
|
69
32
|
};
|
|
70
|
-
|
|
33
|
+
const checkErrorForStatus = (status) => {
|
|
71
34
|
if (status === "approved") {
|
|
72
35
|
return undefined;
|
|
73
36
|
}
|
|
@@ -76,22 +39,22 @@ var checkErrorForStatus = function (status) {
|
|
|
76
39
|
}
|
|
77
40
|
return status;
|
|
78
41
|
};
|
|
79
|
-
|
|
42
|
+
const twilioErrorFields = (error) => {
|
|
80
43
|
if (!error || typeof error !== "object") {
|
|
81
44
|
return {
|
|
82
45
|
message: error instanceof Error ? error.message : "Twilio Verify request failed",
|
|
83
|
-
payload: { error
|
|
46
|
+
payload: { error },
|
|
84
47
|
};
|
|
85
48
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
49
|
+
const raw = error;
|
|
50
|
+
const code = typeof raw.code === "number" ? raw.code : undefined;
|
|
51
|
+
const status = typeof raw.status === "number" ? raw.status : undefined;
|
|
52
|
+
const message = typeof raw.message === "string" && raw.message.length > 0
|
|
90
53
|
? raw.message
|
|
91
54
|
: error instanceof Error
|
|
92
55
|
? error.message
|
|
93
56
|
: "Twilio Verify request failed";
|
|
94
|
-
|
|
57
|
+
const payload = { message };
|
|
95
58
|
if (code !== undefined) {
|
|
96
59
|
payload.code = code;
|
|
97
60
|
}
|
|
@@ -101,9 +64,9 @@ var twilioErrorFields = function (error) {
|
|
|
101
64
|
if (typeof raw.moreInfo === "string") {
|
|
102
65
|
payload.moreInfo = raw.moreInfo;
|
|
103
66
|
}
|
|
104
|
-
return { code
|
|
67
|
+
return { code, message, payload, status };
|
|
105
68
|
};
|
|
106
|
-
|
|
69
|
+
const classifyTwilioVerifyFailure = (fields) => {
|
|
107
70
|
if (fields.code !== undefined && PERMANENT_CODES.has(fields.code)) {
|
|
108
71
|
return { errorClass: "permanent", errorCode: String(fields.code) };
|
|
109
72
|
}
|
|
@@ -116,7 +79,7 @@ var classifyTwilioVerifyFailure = function (fields) {
|
|
|
116
79
|
if (fields.status === 429 || (fields.status !== undefined && fields.status >= 500)) {
|
|
117
80
|
return {
|
|
118
81
|
errorClass: "transient",
|
|
119
|
-
errorCode: fields.code !== undefined ? String(fields.code) :
|
|
82
|
+
errorCode: fields.code !== undefined ? String(fields.code) : `twilio-${fields.status}`,
|
|
120
83
|
};
|
|
121
84
|
}
|
|
122
85
|
if (fields.code !== undefined) {
|
|
@@ -124,89 +87,64 @@ var classifyTwilioVerifyFailure = function (fields) {
|
|
|
124
87
|
}
|
|
125
88
|
return { errorClass: "transient", errorCode: "twilio-network" };
|
|
126
89
|
};
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
90
|
+
const failedStartResult = (error) => {
|
|
91
|
+
const fields = twilioErrorFields(error);
|
|
92
|
+
const { errorClass, errorCode } = classifyTwilioVerifyFailure(fields);
|
|
130
93
|
if (errorClass === "config") {
|
|
131
|
-
api_1.logger.error(
|
|
94
|
+
api_1.logger.error(`[comms:twilio-verify] ${errorCode}: ${fields.message}`);
|
|
132
95
|
}
|
|
133
96
|
return {
|
|
134
97
|
accepted: false,
|
|
135
98
|
error: fields.message,
|
|
136
|
-
errorClass
|
|
137
|
-
errorCode
|
|
99
|
+
errorClass,
|
|
100
|
+
errorCode,
|
|
138
101
|
isPermanentFailure: errorClass === "permanent",
|
|
139
102
|
metadata: { twilioError: fields.payload },
|
|
140
103
|
};
|
|
141
104
|
};
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
105
|
+
class TwilioVerifyProvider {
|
|
106
|
+
id = "twilio-verify";
|
|
107
|
+
client;
|
|
108
|
+
verifyServiceSid;
|
|
109
|
+
constructor(options) {
|
|
110
|
+
const { accountSid, authToken, verifyServiceSid } = resolveCredentials(options);
|
|
146
111
|
this.verifyServiceSid = verifyServiceSid;
|
|
147
|
-
if (options
|
|
112
|
+
if (options?.client) {
|
|
148
113
|
this.client = options.client;
|
|
149
114
|
return;
|
|
150
115
|
}
|
|
151
116
|
this.client = loadTwilio()(accountSid, authToken);
|
|
152
117
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return __generator(this, function (_a) {
|
|
186
|
-
switch (_a.label) {
|
|
187
|
-
case 0:
|
|
188
|
-
_a.trys.push([0, 2, , 3]);
|
|
189
|
-
return [4 /*yield*/, (0, api_1.withApiErrorHandling)(function () {
|
|
190
|
-
return _this.client.verify.v2
|
|
191
|
-
.services(_this.verifyServiceSid)
|
|
192
|
-
.verificationChecks.create({ code: options.code, to: options.to });
|
|
193
|
-
}, { apiName: "twilio-verify", operation: "checkVerification" })];
|
|
194
|
-
case 1:
|
|
195
|
-
checked = _a.sent();
|
|
196
|
-
error = checkErrorForStatus(checked.status);
|
|
197
|
-
if (error === undefined) {
|
|
198
|
-
return [2 /*return*/, { valid: true }];
|
|
199
|
-
}
|
|
200
|
-
return [2 /*return*/, { error: error, valid: false }];
|
|
201
|
-
case 2:
|
|
202
|
-
error_2 = _a.sent();
|
|
203
|
-
fields = twilioErrorFields(error_2);
|
|
204
|
-
return [2 /*return*/, { error: fields.message, valid: false }];
|
|
205
|
-
case 3: return [2 /*return*/];
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
};
|
|
210
|
-
return TwilioVerifyProvider;
|
|
211
|
-
}());
|
|
118
|
+
async startVerification(options) {
|
|
119
|
+
try {
|
|
120
|
+
const created = await (0, api_1.withApiErrorHandling)(() => this.client.verify.v2
|
|
121
|
+
.services(this.verifyServiceSid)
|
|
122
|
+
.verifications.create({ channel: options.channel, to: options.to }), { apiName: "twilio-verify", operation: "startVerification" });
|
|
123
|
+
return {
|
|
124
|
+
accepted: true,
|
|
125
|
+
metadata: { consoleUrl: `${TWILIO_VERIFY_CONSOLE_BASE}/${this.verifyServiceSid}` },
|
|
126
|
+
providerMessageId: created.sid,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
return failedStartResult(error);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async checkVerification(options) {
|
|
134
|
+
try {
|
|
135
|
+
const checked = await (0, api_1.withApiErrorHandling)(() => this.client.verify.v2
|
|
136
|
+
.services(this.verifyServiceSid)
|
|
137
|
+
.verificationChecks.create({ code: options.code, to: options.to }), { apiName: "twilio-verify", operation: "checkVerification" });
|
|
138
|
+
const error = checkErrorForStatus(checked.status);
|
|
139
|
+
if (error === undefined) {
|
|
140
|
+
return { valid: true };
|
|
141
|
+
}
|
|
142
|
+
return { error, valid: false };
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
const fields = twilioErrorFields(error);
|
|
146
|
+
return { error: fields.message, valid: false };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
212
150
|
exports.TwilioVerifyProvider = TwilioVerifyProvider;
|
package/dist/commsApp.js
CHANGED
|
@@ -1,53 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CommsApp = exports.getCommsService = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const commsService_1 = require("./commsService");
|
|
6
|
+
const pushToken_1 = require("./models/pushToken");
|
|
7
|
+
const commsDashboard_1 = require("./routes/commsDashboard");
|
|
8
|
+
const pushTokens_1 = require("./routes/pushTokens");
|
|
9
|
+
const sendgridWebhooks_1 = require("./webhooks/sendgridWebhooks");
|
|
10
|
+
const twilioWebhooks_1 = require("./webhooks/twilioWebhooks");
|
|
11
|
+
let registeredCommsService;
|
|
12
|
+
const getCommsService = () => {
|
|
13
13
|
if (!registeredCommsService) {
|
|
14
14
|
throw new api_1.APIError({ status: 500, title: "CommsApp is not registered" });
|
|
15
15
|
}
|
|
16
16
|
return registeredCommsService;
|
|
17
17
|
};
|
|
18
18
|
exports.getCommsService = getCommsService;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
class CommsApp {
|
|
20
|
+
service;
|
|
21
|
+
options;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.options = options ?? {};
|
|
22
24
|
this.service = new commsService_1.CommsService(this.options);
|
|
23
25
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var basePath = (_a = this.options.basePath) !== null && _a !== void 0 ? _a : "/comms";
|
|
26
|
+
register(app, openApi) {
|
|
27
|
+
const basePath = this.options.basePath ?? "/comms";
|
|
27
28
|
registeredCommsService = this.service;
|
|
28
29
|
// Atomic push-token registration depends on the unique token index to reject concurrent
|
|
29
30
|
// claims, so build it explicitly instead of relying on Mongoose autoIndex being enabled.
|
|
30
|
-
void pushToken_1.PushToken.init().catch(
|
|
31
|
-
api_1.logger.error(
|
|
32
|
-
|
|
31
|
+
void pushToken_1.PushToken.init().catch((error) => {
|
|
32
|
+
api_1.logger.error(`[comms] Failed to build PushToken indexes; concurrent token registration cannot be ` +
|
|
33
|
+
`rejected safely until the unique token index exists: ${String(error)}`);
|
|
33
34
|
});
|
|
34
|
-
(0, pushTokens_1.addPushTokenRoutes)(app, { basePath:
|
|
35
|
-
(0, commsDashboard_1.addCommsDashboardRoutes)(app, { basePath
|
|
35
|
+
(0, pushTokens_1.addPushTokenRoutes)(app, { basePath: `${basePath}/pushTokens`, openApi });
|
|
36
|
+
(0, commsDashboard_1.addCommsDashboardRoutes)(app, { basePath, openApi, service: this.service });
|
|
36
37
|
(0, twilioWebhooks_1.maybeRegisterTwilioCommsWebhooks)({
|
|
37
|
-
basePath
|
|
38
|
+
basePath,
|
|
38
39
|
publicUrl: this.options.webhookPublicUrl,
|
|
39
40
|
service: this.service,
|
|
40
41
|
sms: this.options.sms,
|
|
41
42
|
webhooks: this.options.webhooks,
|
|
42
43
|
});
|
|
43
44
|
(0, sendgridWebhooks_1.maybeRegisterSendGridCommsWebhooks)({
|
|
44
|
-
basePath
|
|
45
|
+
basePath,
|
|
45
46
|
mail: this.options.mail,
|
|
46
47
|
service: this.service,
|
|
47
48
|
webhooks: this.options.webhooks,
|
|
48
49
|
});
|
|
49
|
-
}
|
|
50
|
-
|
|
50
|
+
}
|
|
51
|
+
adminContribution() {
|
|
51
52
|
return {
|
|
52
53
|
customScreens: [
|
|
53
54
|
{
|
|
@@ -57,7 +58,6 @@ var CommsApp = /** @class */ (function () {
|
|
|
57
58
|
},
|
|
58
59
|
],
|
|
59
60
|
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
}());
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
63
|
exports.CommsApp = CommsApp;
|
package/dist/commsQuery.js
CHANGED
|
@@ -4,21 +4,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.buildCommsMessageMatch = exports.defaultStatsRange = exports.parseRetryManyLimit = exports.parseCommsListPagination = exports.parseCommsListFilters = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
const api_1 = require("@terreno/api");
|
|
8
|
+
const luxon_1 = require("luxon");
|
|
9
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
10
|
+
const DEFAULT_LIMIT = 20;
|
|
11
|
+
const MAX_LIMIT = 100;
|
|
12
|
+
const DEFAULT_STATS_DAYS = 7;
|
|
13
|
+
const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
14
|
+
const queryString = (value) => {
|
|
15
15
|
if (typeof value !== "string") {
|
|
16
16
|
return undefined;
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
const trimmed = value.trim();
|
|
19
19
|
return trimmed.length > 0 ? trimmed : undefined;
|
|
20
20
|
};
|
|
21
|
-
|
|
21
|
+
const parseCommsListFilters = (source) => ({
|
|
22
22
|
channel: queryString(source.channel),
|
|
23
23
|
endDate: queryString(source.endDate),
|
|
24
24
|
errorClass: queryString(source.errorClass),
|
|
@@ -31,38 +31,37 @@ var parseCommsListFilters = function (source) { return ({
|
|
|
31
31
|
templateId: queryString(source.templateId),
|
|
32
32
|
to: queryString(source.to),
|
|
33
33
|
userId: queryString(source.userId),
|
|
34
|
-
});
|
|
34
|
+
});
|
|
35
35
|
exports.parseCommsListFilters = parseCommsListFilters;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return { limit: limit, page: page, skip: (page - 1) * limit };
|
|
36
|
+
const parseCommsListPagination = (source) => {
|
|
37
|
+
const page = Math.max(1, Number.parseInt(String(source.page ?? ""), 10) || 1);
|
|
38
|
+
const limit = Math.min(MAX_LIMIT, Math.max(1, Number.parseInt(String(source.limit ?? ""), 10) || DEFAULT_LIMIT));
|
|
39
|
+
return { limit, page, skip: (page - 1) * limit };
|
|
41
40
|
};
|
|
42
41
|
exports.parseCommsListPagination = parseCommsListPagination;
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
const parseRetryManyLimit = (value) => {
|
|
43
|
+
const parsed = Number.parseInt(String(value ?? ""), 10);
|
|
45
44
|
if (Number.isNaN(parsed)) {
|
|
46
45
|
return MAX_LIMIT;
|
|
47
46
|
}
|
|
48
47
|
return Math.min(MAX_LIMIT, Math.max(1, parsed));
|
|
49
48
|
};
|
|
50
49
|
exports.parseRetryManyLimit = parseRetryManyLimit;
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
const parseIsoDate = (value, field) => {
|
|
51
|
+
const parsed = luxon_1.DateTime.fromISO(value, { zone: "utc" });
|
|
53
52
|
if (!parsed.isValid) {
|
|
54
|
-
throw new api_1.APIError({ status: 400, title:
|
|
53
|
+
throw new api_1.APIError({ status: 400, title: `Invalid ${field} format` });
|
|
55
54
|
}
|
|
56
55
|
return parsed;
|
|
57
56
|
};
|
|
58
57
|
/** @internal */
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return { endDate
|
|
58
|
+
const defaultStatsRange = () => {
|
|
59
|
+
const endDate = luxon_1.DateTime.utc();
|
|
60
|
+
return { endDate, startDate: endDate.minus({ days: DEFAULT_STATS_DAYS }) };
|
|
62
61
|
};
|
|
63
62
|
exports.defaultStatsRange = defaultStatsRange;
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
const buildCommsMessageMatch = (filters, options) => {
|
|
64
|
+
const match = {
|
|
66
65
|
deleted: { $ne: true },
|
|
67
66
|
};
|
|
68
67
|
if (filters.channel) {
|
|
@@ -98,10 +97,10 @@ var buildCommsMessageMatch = function (filters, options) {
|
|
|
98
97
|
}
|
|
99
98
|
match.retriedFromId = new mongoose_1.default.Types.ObjectId(filters.retriedFromId);
|
|
100
99
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
100
|
+
let start = filters.startDate ? parseIsoDate(filters.startDate, "startDate") : undefined;
|
|
101
|
+
let end = filters.endDate ? parseIsoDate(filters.endDate, "endDate") : undefined;
|
|
102
|
+
if (options?.applyDefaultStatsRange && !start && !end) {
|
|
103
|
+
const range = (0, exports.defaultStatsRange)();
|
|
105
104
|
start = range.startDate;
|
|
106
105
|
end = range.endDate;
|
|
107
106
|
}
|
|
@@ -118,15 +117,15 @@ var buildCommsMessageMatch = function (filters, options) {
|
|
|
118
117
|
}
|
|
119
118
|
}
|
|
120
119
|
if (filters.q) {
|
|
121
|
-
|
|
122
|
-
|
|
120
|
+
const escaped = escapeRegex(filters.q);
|
|
121
|
+
const clauses = [
|
|
123
122
|
{ subject: { $options: "i", $regex: escaped } },
|
|
124
123
|
{ error: { $options: "i", $regex: escaped } },
|
|
125
124
|
{ to: { $options: "i", $regex: escaped } },
|
|
126
125
|
];
|
|
127
|
-
|
|
126
|
+
const last4 = filters.q.replace(/\s/g, "");
|
|
128
127
|
if (/^[A-Za-z0-9]{4}$/.test(last4)) {
|
|
129
|
-
clauses.push({ to: { $options: "i", $regex:
|
|
128
|
+
clauses.push({ to: { $options: "i", $regex: `${escapeRegex(last4)}$` } });
|
|
130
129
|
}
|
|
131
130
|
match.$or = clauses;
|
|
132
131
|
}
|
package/dist/commsRetry.js
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.serializeCommsMessage = exports.throwRetryBlock = exports.evaluateRetryBlock = void 0;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const luxon_1 = require("luxon");
|
|
6
|
+
const COMMS_RETRY_NOT_RETRYABLE = "comms-retry-not-retryable";
|
|
7
|
+
const COMMS_RETRY_PAYLOAD_EXPIRED = "comms-retry-payload-expired";
|
|
8
|
+
const COMMS_RETRY_CHANNEL_UNCONFIGURED = "comms-retry-channel-unconfigured";
|
|
9
|
+
const RETRYABLE_STATUSES = new Set(["bounced", "failed"]);
|
|
10
|
+
const isPayloadPresent = (message) => {
|
|
22
11
|
if (message.payload === undefined || message.payload === null) {
|
|
23
12
|
return false;
|
|
24
13
|
}
|
|
@@ -27,8 +16,7 @@ var isPayloadPresent = function (message) {
|
|
|
27
16
|
}
|
|
28
17
|
return true;
|
|
29
18
|
};
|
|
30
|
-
|
|
31
|
-
var isChannelConfigured = _a.isChannelConfigured, message = _a.message;
|
|
19
|
+
const evaluateRetryBlock = ({ isChannelConfigured, message, }) => {
|
|
32
20
|
if (message.channel === "verification") {
|
|
33
21
|
return {
|
|
34
22
|
code: COMMS_RETRY_NOT_RETRYABLE,
|
|
@@ -62,7 +50,7 @@ var evaluateRetryBlock = function (_a) {
|
|
|
62
50
|
return undefined;
|
|
63
51
|
};
|
|
64
52
|
exports.evaluateRetryBlock = evaluateRetryBlock;
|
|
65
|
-
|
|
53
|
+
const throwRetryBlock = (block) => {
|
|
66
54
|
throw new api_1.APIError({
|
|
67
55
|
code: block.code,
|
|
68
56
|
status: 400,
|
|
@@ -70,9 +58,13 @@ var throwRetryBlock = function (block) {
|
|
|
70
58
|
});
|
|
71
59
|
};
|
|
72
60
|
exports.throwRetryBlock = throwRetryBlock;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return
|
|
61
|
+
const serializeCommsMessage = (message, isChannelConfigured) => {
|
|
62
|
+
const json = message.toJSON();
|
|
63
|
+
const block = (0, exports.evaluateRetryBlock)({ isChannelConfigured, message });
|
|
64
|
+
return {
|
|
65
|
+
...json,
|
|
66
|
+
retryable: !block,
|
|
67
|
+
retryDisabledReason: block?.title,
|
|
68
|
+
};
|
|
77
69
|
};
|
|
78
70
|
exports.serializeCommsMessage = serializeCommsMessage;
|