@terreno/comms 57.4.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/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,123 +1,37 @@
|
|
|
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
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
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);
|
|
24
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
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;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
-
if (!m) return o;
|
|
52
|
-
var i = m.call(o), r, ar = [], e;
|
|
53
|
-
try {
|
|
54
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
-
}
|
|
56
|
-
catch (error) { e = { error: error }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e) throw e.error; }
|
|
62
|
-
}
|
|
63
|
-
return ar;
|
|
64
|
-
};
|
|
65
|
-
var __values = (this && this.__values) || function(o) {
|
|
66
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
67
|
-
if (m) return m.call(o);
|
|
68
|
-
if (o && typeof o.length === "number") return {
|
|
69
|
-
next: function () {
|
|
70
|
-
if (o && i >= o.length) o = void 0;
|
|
71
|
-
return { value: o && o[i++], done: !o };
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
75
|
-
};
|
|
76
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
77
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
78
|
-
if (ar || !(i in from)) {
|
|
79
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
80
|
-
ar[i] = from[i];
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
84
|
-
};
|
|
85
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
86
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
87
4
|
};
|
|
88
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
89
6
|
exports.addCommsDashboardRoutes = void 0;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (!(user === null || user === void 0 ? void 0 : user.admin)) {
|
|
7
|
+
const api_1 = require("@terreno/api");
|
|
8
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
const commsQuery_1 = require("../commsQuery");
|
|
10
|
+
const commsRetry_1 = require("../commsRetry");
|
|
11
|
+
const commsMessage_1 = require("../models/commsMessage");
|
|
12
|
+
const requireAdmin = (req) => {
|
|
13
|
+
const user = req.user;
|
|
14
|
+
if (!user?.admin) {
|
|
99
15
|
throw new api_1.APIError({ status: 403, title: "Admin access required" });
|
|
100
16
|
}
|
|
101
|
-
return { id:
|
|
102
|
-
};
|
|
103
|
-
var listQueryParameters = function (builder) {
|
|
104
|
-
return builder
|
|
105
|
-
.withQueryParameter("page", { type: "number" }, { required: false })
|
|
106
|
-
.withQueryParameter("limit", { type: "number" }, { required: false })
|
|
107
|
-
.withQueryParameter("channel", { type: "string" }, { required: false })
|
|
108
|
-
.withQueryParameter("provider", { type: "string" }, { required: false })
|
|
109
|
-
.withQueryParameter("status", { type: "string" }, { required: false })
|
|
110
|
-
.withQueryParameter("errorClass", { type: "string" }, { required: false })
|
|
111
|
-
.withQueryParameter("errorCode", { type: "string" }, { required: false })
|
|
112
|
-
.withQueryParameter("userId", { type: "string" }, { required: false })
|
|
113
|
-
.withQueryParameter("to", { type: "string" }, { required: false })
|
|
114
|
-
.withQueryParameter("templateId", { type: "string" }, { required: false })
|
|
115
|
-
.withQueryParameter("retriedFromId", { type: "string" }, { required: false })
|
|
116
|
-
.withQueryParameter("startDate", { type: "string" }, { required: false })
|
|
117
|
-
.withQueryParameter("endDate", { type: "string" }, { required: false })
|
|
118
|
-
.withQueryParameter("q", { type: "string" }, { required: false });
|
|
17
|
+
return { id: user.id ?? user._id?.toString() };
|
|
119
18
|
};
|
|
120
|
-
|
|
19
|
+
const listQueryParameters = (builder) => builder
|
|
20
|
+
.withQueryParameter("page", { type: "number" }, { required: false })
|
|
21
|
+
.withQueryParameter("limit", { type: "number" }, { required: false })
|
|
22
|
+
.withQueryParameter("channel", { type: "string" }, { required: false })
|
|
23
|
+
.withQueryParameter("provider", { type: "string" }, { required: false })
|
|
24
|
+
.withQueryParameter("status", { type: "string" }, { required: false })
|
|
25
|
+
.withQueryParameter("errorClass", { type: "string" }, { required: false })
|
|
26
|
+
.withQueryParameter("errorCode", { type: "string" }, { required: false })
|
|
27
|
+
.withQueryParameter("userId", { type: "string" }, { required: false })
|
|
28
|
+
.withQueryParameter("to", { type: "string" }, { required: false })
|
|
29
|
+
.withQueryParameter("templateId", { type: "string" }, { required: false })
|
|
30
|
+
.withQueryParameter("retriedFromId", { type: "string" }, { required: false })
|
|
31
|
+
.withQueryParameter("startDate", { type: "string" }, { required: false })
|
|
32
|
+
.withQueryParameter("endDate", { type: "string" }, { required: false })
|
|
33
|
+
.withQueryParameter("q", { type: "string" }, { required: false });
|
|
34
|
+
const filterBodyFields = {
|
|
121
35
|
channel: { type: "string" },
|
|
122
36
|
endDate: { type: "string" },
|
|
123
37
|
errorClass: { type: "string" },
|
|
@@ -132,25 +46,24 @@ var filterBodyFields = {
|
|
|
132
46
|
to: { type: "string" },
|
|
133
47
|
userId: { type: "string" },
|
|
134
48
|
};
|
|
135
|
-
|
|
49
|
+
const pathId = (value) => {
|
|
136
50
|
if (Array.isArray(value)) {
|
|
137
51
|
return value[0];
|
|
138
52
|
}
|
|
139
53
|
return value;
|
|
140
54
|
};
|
|
141
|
-
|
|
55
|
+
const failureRate = (failed, total) => {
|
|
142
56
|
if (total === 0) {
|
|
143
57
|
return 0;
|
|
144
58
|
}
|
|
145
59
|
return failed / total;
|
|
146
60
|
};
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
app.get("".concat(basePath, "/messages"), [
|
|
61
|
+
const addCommsDashboardRoutes = (app, options) => {
|
|
62
|
+
const basePath = options.basePath ?? "/comms";
|
|
63
|
+
const routeOpenApi = options.openApi ? { openApi: options.openApi } : {};
|
|
64
|
+
const { service } = options;
|
|
65
|
+
const isConfigured = (channel) => service.isChannelConfigured(channel);
|
|
66
|
+
app.get(`${basePath}/messages`, [
|
|
154
67
|
(0, api_1.authenticateMiddleware)(),
|
|
155
68
|
listQueryParameters((0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
156
69
|
.withTags(["admin", "comms"])
|
|
@@ -163,32 +76,24 @@ var addCommsDashboardRoutes = function (app, options) {
|
|
|
163
76
|
total: { type: "number" },
|
|
164
77
|
})
|
|
165
78
|
.build(),
|
|
166
|
-
], (0, api_1.asyncHandler)(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return [2 /*return*/, res.json({
|
|
182
|
-
data: messages.map(function (message) { return (0, commsRetry_1.serializeCommsMessage)(message, isConfigured); }),
|
|
183
|
-
limit: limit,
|
|
184
|
-
more: skip + limit < total,
|
|
185
|
-
page: page,
|
|
186
|
-
total: total,
|
|
187
|
-
})];
|
|
188
|
-
}
|
|
79
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
80
|
+
requireAdmin(req);
|
|
81
|
+
const filters = (0, commsQuery_1.parseCommsListFilters)(req.query);
|
|
82
|
+
const { limit, page, skip } = (0, commsQuery_1.parseCommsListPagination)(req.query);
|
|
83
|
+
const match = (0, commsQuery_1.buildCommsMessageMatch)(filters);
|
|
84
|
+
const [messages, total] = await Promise.all([
|
|
85
|
+
commsMessage_1.CommsMessage.find(match).sort("-created").skip(skip).limit(limit),
|
|
86
|
+
commsMessage_1.CommsMessage.countDocuments(match),
|
|
87
|
+
]);
|
|
88
|
+
return res.json({
|
|
89
|
+
data: messages.map((message) => (0, commsRetry_1.serializeCommsMessage)(message, isConfigured)),
|
|
90
|
+
limit,
|
|
91
|
+
more: skip + limit < total,
|
|
92
|
+
page,
|
|
93
|
+
total,
|
|
189
94
|
});
|
|
190
|
-
})
|
|
191
|
-
app.post(
|
|
95
|
+
}));
|
|
96
|
+
app.post(`${basePath}/messages/retryMany`, [
|
|
192
97
|
(0, api_1.authenticateMiddleware)(),
|
|
193
98
|
(0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
194
99
|
.withTags(["admin", "comms"])
|
|
@@ -199,65 +104,30 @@ var addCommsDashboardRoutes = function (app, options) {
|
|
|
199
104
|
skipped: { items: { type: "object" }, type: "array" },
|
|
200
105
|
})
|
|
201
106
|
.build(),
|
|
202
|
-
], (0, api_1.asyncHandler)(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
skipped = [];
|
|
218
|
-
_c.label = 2;
|
|
219
|
-
case 2:
|
|
220
|
-
_c.trys.push([2, 9, 10, 11]);
|
|
221
|
-
candidates_1 = __values(candidates), candidates_1_1 = candidates_1.next();
|
|
222
|
-
_c.label = 3;
|
|
223
|
-
case 3:
|
|
224
|
-
if (!!candidates_1_1.done) return [3 /*break*/, 8];
|
|
225
|
-
candidate = candidates_1_1.value;
|
|
226
|
-
_c.label = 4;
|
|
227
|
-
case 4:
|
|
228
|
-
_c.trys.push([4, 6, , 7]);
|
|
229
|
-
return [4 /*yield*/, service.retryMessage({
|
|
230
|
-
messageId: String(candidate._id),
|
|
231
|
-
retriedByUserId: admin.id,
|
|
232
|
-
})];
|
|
233
|
-
case 5:
|
|
234
|
-
created = _c.sent();
|
|
235
|
-
retried.push((0, commsRetry_1.serializeCommsMessage)(created, isConfigured));
|
|
236
|
-
return [3 /*break*/, 7];
|
|
237
|
-
case 6:
|
|
238
|
-
error_1 = _c.sent();
|
|
239
|
-
title = (0, api_1.isAPIError)(error_1) ? error_1.title : "Message could not be retried";
|
|
240
|
-
skipped.push({ id: String(candidate._id), reason: title });
|
|
241
|
-
return [3 /*break*/, 7];
|
|
242
|
-
case 7:
|
|
243
|
-
candidates_1_1 = candidates_1.next();
|
|
244
|
-
return [3 /*break*/, 3];
|
|
245
|
-
case 8: return [3 /*break*/, 11];
|
|
246
|
-
case 9:
|
|
247
|
-
e_1_1 = _c.sent();
|
|
248
|
-
e_1 = { error: e_1_1 };
|
|
249
|
-
return [3 /*break*/, 11];
|
|
250
|
-
case 10:
|
|
251
|
-
try {
|
|
252
|
-
if (candidates_1_1 && !candidates_1_1.done && (_a = candidates_1.return)) _a.call(candidates_1);
|
|
253
|
-
}
|
|
254
|
-
finally { if (e_1) throw e_1.error; }
|
|
255
|
-
return [7 /*endfinally*/];
|
|
256
|
-
case 11: return [2 /*return*/, res.json({ retried: retried, skipped: skipped })];
|
|
107
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
108
|
+
const admin = requireAdmin(req);
|
|
109
|
+
const body = (req.body ?? {});
|
|
110
|
+
const limit = (0, commsQuery_1.parseRetryManyLimit)(body.limit);
|
|
111
|
+
const match = (0, commsQuery_1.buildCommsMessageMatch)((0, commsQuery_1.parseCommsListFilters)(body));
|
|
112
|
+
const candidates = await commsMessage_1.CommsMessage.find(match).sort("-created").limit(limit);
|
|
113
|
+
const retried = [];
|
|
114
|
+
const skipped = [];
|
|
115
|
+
for (const candidate of candidates) {
|
|
116
|
+
try {
|
|
117
|
+
const created = await service.retryMessage({
|
|
118
|
+
messageId: String(candidate._id),
|
|
119
|
+
retriedByUserId: admin.id,
|
|
120
|
+
});
|
|
121
|
+
retried.push((0, commsRetry_1.serializeCommsMessage)(created, isConfigured));
|
|
257
122
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
123
|
+
catch (error) {
|
|
124
|
+
const title = (0, api_1.isAPIError)(error) ? error.title : "Message could not be retried";
|
|
125
|
+
skipped.push({ id: String(candidate._id), reason: title });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return res.json({ retried, skipped });
|
|
129
|
+
}));
|
|
130
|
+
app.get(`${basePath}/messages/:id`, [
|
|
261
131
|
(0, api_1.authenticateMiddleware)(),
|
|
262
132
|
(0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
263
133
|
.withTags(["admin", "comms"])
|
|
@@ -265,32 +135,25 @@ var addCommsDashboardRoutes = function (app, options) {
|
|
|
265
135
|
.withPathParameter("id", { type: "string" })
|
|
266
136
|
.withResponse(200, { data: { type: "object" } })
|
|
267
137
|
.build(),
|
|
268
|
-
], (0, api_1.asyncHandler)(
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return [4 /*yield*/, commsMessage_1.CommsMessage.find({ retriedFromId: message._id }).sort("-created")];
|
|
285
|
-
case 2:
|
|
286
|
-
retries = _a.sent();
|
|
287
|
-
return [2 /*return*/, res.json({
|
|
288
|
-
data: __assign(__assign({}, (0, commsRetry_1.serializeCommsMessage)(message, isConfigured)), { retries: retries.map(function (row) { return (0, commsRetry_1.serializeCommsMessage)(row, isConfigured); }) }),
|
|
289
|
-
})];
|
|
290
|
-
}
|
|
138
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
139
|
+
requireAdmin(req);
|
|
140
|
+
const id = pathId(req.params.id);
|
|
141
|
+
if (!id || !mongoose_1.default.isValidObjectId(id)) {
|
|
142
|
+
throw new api_1.APIError({ status: 404, title: "Comms message not found" });
|
|
143
|
+
}
|
|
144
|
+
const message = await commsMessage_1.CommsMessage.findOneOrNone({ _id: id });
|
|
145
|
+
if (!message) {
|
|
146
|
+
throw new api_1.APIError({ status: 404, title: "Comms message not found" });
|
|
147
|
+
}
|
|
148
|
+
const retries = await commsMessage_1.CommsMessage.find({ retriedFromId: message._id }).sort("-created");
|
|
149
|
+
return res.json({
|
|
150
|
+
data: {
|
|
151
|
+
...(0, commsRetry_1.serializeCommsMessage)(message, isConfigured),
|
|
152
|
+
retries: retries.map((row) => (0, commsRetry_1.serializeCommsMessage)(row, isConfigured)),
|
|
153
|
+
},
|
|
291
154
|
});
|
|
292
|
-
})
|
|
293
|
-
app.post(
|
|
155
|
+
}));
|
|
156
|
+
app.post(`${basePath}/messages/:id/retry`, [
|
|
294
157
|
(0, api_1.authenticateMiddleware)(),
|
|
295
158
|
(0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
296
159
|
.withTags(["admin", "comms"])
|
|
@@ -298,29 +161,21 @@ var addCommsDashboardRoutes = function (app, options) {
|
|
|
298
161
|
.withPathParameter("id", { type: "string" })
|
|
299
162
|
.withResponse(200, { data: { type: "object" } })
|
|
300
163
|
.build(),
|
|
301
|
-
], (0, api_1.asyncHandler)(
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
return [4 /*yield*/, service.retryMessage({
|
|
312
|
-
messageId: id,
|
|
313
|
-
retriedByUserId: admin.id,
|
|
314
|
-
})];
|
|
315
|
-
case 1:
|
|
316
|
-
created = _a.sent();
|
|
317
|
-
return [2 /*return*/, res.json({
|
|
318
|
-
data: (0, commsRetry_1.serializeCommsMessage)(created, isConfigured),
|
|
319
|
-
})];
|
|
320
|
-
}
|
|
164
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
165
|
+
const admin = requireAdmin(req);
|
|
166
|
+
const id = pathId(req.params.id);
|
|
167
|
+
if (!id || !mongoose_1.default.isValidObjectId(id)) {
|
|
168
|
+
throw new api_1.APIError({ status: 404, title: "Comms message not found" });
|
|
169
|
+
}
|
|
170
|
+
const created = await service.retryMessage({
|
|
171
|
+
messageId: id,
|
|
172
|
+
retriedByUserId: admin.id,
|
|
321
173
|
});
|
|
322
|
-
|
|
323
|
-
|
|
174
|
+
return res.json({
|
|
175
|
+
data: (0, commsRetry_1.serializeCommsMessage)(created, isConfigured),
|
|
176
|
+
});
|
|
177
|
+
}));
|
|
178
|
+
app.get(`${basePath}/stats`, [
|
|
324
179
|
(0, api_1.authenticateMiddleware)(),
|
|
325
180
|
listQueryParameters((0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
326
181
|
.withTags(["admin", "comms"])
|
|
@@ -331,82 +186,76 @@ var addCommsDashboardRoutes = function (app, options) {
|
|
|
331
186
|
totals: { type: "object" },
|
|
332
187
|
})
|
|
333
188
|
.build(),
|
|
334
|
-
], (0, api_1.asyncHandler)(
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
total: 0,
|
|
382
|
-
};
|
|
383
|
-
existing.total += row.count;
|
|
384
|
-
if (status === "bounced" ||
|
|
385
|
-
status === "delivered" ||
|
|
386
|
-
status === "failed" ||
|
|
387
|
-
status === "sent") {
|
|
388
|
-
existing[status] += row.count;
|
|
389
|
-
}
|
|
390
|
-
providerTotals.set(provider, existing);
|
|
391
|
-
return {
|
|
392
|
-
channel: row._id.channel,
|
|
393
|
-
count: row.count,
|
|
394
|
-
day: row._id.day,
|
|
395
|
-
provider: provider,
|
|
396
|
-
status: status,
|
|
397
|
-
};
|
|
398
|
-
});
|
|
399
|
-
failedTotal = ((_a = totals.failed) !== null && _a !== void 0 ? _a : 0) + ((_b = totals.bounced) !== null && _b !== void 0 ? _b : 0);
|
|
400
|
-
return [2 /*return*/, res.json({
|
|
401
|
-
buckets: buckets.sort(function (left, right) { return left.day.localeCompare(right.day); }),
|
|
402
|
-
byProvider: __spreadArray([], __read(providerTotals.entries()), false).map(function (_a) {
|
|
403
|
-
var _b = __read(_a, 2), provider = _b[0], counts = _b[1];
|
|
404
|
-
return (__assign(__assign({}, counts), { failureRate: failureRate(counts.failed + counts.bounced, counts.total), provider: provider }));
|
|
405
|
-
}),
|
|
406
|
-
totals: __assign(__assign({}, totals), { failureRate: failureRate(failedTotal, totals.total) }),
|
|
407
|
-
})];
|
|
189
|
+
], (0, api_1.asyncHandler)(async (req, res) => {
|
|
190
|
+
requireAdmin(req);
|
|
191
|
+
const filters = (0, commsQuery_1.parseCommsListFilters)(req.query);
|
|
192
|
+
const match = (0, commsQuery_1.buildCommsMessageMatch)(filters, { applyDefaultStatsRange: true });
|
|
193
|
+
const grouped = await commsMessage_1.CommsMessage.aggregate([
|
|
194
|
+
{ $match: match },
|
|
195
|
+
{
|
|
196
|
+
$group: {
|
|
197
|
+
_id: {
|
|
198
|
+
channel: "$channel",
|
|
199
|
+
day: { $dateToString: { date: "$created", format: "%Y-%m-%d" } },
|
|
200
|
+
provider: "$provider",
|
|
201
|
+
status: "$status",
|
|
202
|
+
},
|
|
203
|
+
count: { $sum: 1 },
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
]);
|
|
207
|
+
const totals = {
|
|
208
|
+
bounced: 0,
|
|
209
|
+
cancelled: 0,
|
|
210
|
+
delivered: 0,
|
|
211
|
+
failed: 0,
|
|
212
|
+
sent: 0,
|
|
213
|
+
total: 0,
|
|
214
|
+
};
|
|
215
|
+
const providerTotals = new Map();
|
|
216
|
+
const buckets = grouped.map((row) => {
|
|
217
|
+
const status = row._id.status;
|
|
218
|
+
const provider = row._id.provider;
|
|
219
|
+
totals.total += row.count;
|
|
220
|
+
if (status in totals) {
|
|
221
|
+
totals[status] = (totals[status] ?? 0) + row.count;
|
|
222
|
+
}
|
|
223
|
+
const existing = providerTotals.get(provider) ?? {
|
|
224
|
+
bounced: 0,
|
|
225
|
+
delivered: 0,
|
|
226
|
+
failed: 0,
|
|
227
|
+
sent: 0,
|
|
228
|
+
total: 0,
|
|
229
|
+
};
|
|
230
|
+
existing.total += row.count;
|
|
231
|
+
if (status === "bounced" ||
|
|
232
|
+
status === "delivered" ||
|
|
233
|
+
status === "failed" ||
|
|
234
|
+
status === "sent") {
|
|
235
|
+
existing[status] += row.count;
|
|
408
236
|
}
|
|
237
|
+
providerTotals.set(provider, existing);
|
|
238
|
+
return {
|
|
239
|
+
channel: row._id.channel,
|
|
240
|
+
count: row.count,
|
|
241
|
+
day: row._id.day,
|
|
242
|
+
provider,
|
|
243
|
+
status,
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
const failedTotal = (totals.failed ?? 0) + (totals.bounced ?? 0);
|
|
247
|
+
return res.json({
|
|
248
|
+
buckets: buckets.sort((left, right) => left.day.localeCompare(right.day)),
|
|
249
|
+
byProvider: [...providerTotals.entries()].map(([provider, counts]) => ({
|
|
250
|
+
...counts,
|
|
251
|
+
failureRate: failureRate(counts.failed + counts.bounced, counts.total),
|
|
252
|
+
provider,
|
|
253
|
+
})),
|
|
254
|
+
totals: {
|
|
255
|
+
...totals,
|
|
256
|
+
failureRate: failureRate(failedTotal, totals.total),
|
|
257
|
+
},
|
|
409
258
|
});
|
|
410
|
-
})
|
|
259
|
+
}));
|
|
411
260
|
};
|
|
412
261
|
exports.addCommsDashboardRoutes = addCommsDashboardRoutes;
|