@terreno/comms 57.2.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.
Files changed (57) hide show
  1. package/README.md +58 -0
  2. package/dist/adapters/expoPush.d.ts +54 -0
  3. package/dist/adapters/expoPush.js +400 -0
  4. package/dist/adapters/sendgrid.d.ts +3 -0
  5. package/dist/adapters/sendgrid.js +7 -1
  6. package/dist/adapters/twilioSms.d.ts +36 -0
  7. package/dist/adapters/twilioSms.js +232 -0
  8. package/dist/adapters/twilioVerify.d.ts +44 -0
  9. package/dist/adapters/twilioVerify.js +212 -0
  10. package/dist/commsApp.d.ts +4 -1
  11. package/dist/commsApp.js +28 -2
  12. package/dist/commsQuery.d.ts +31 -0
  13. package/dist/commsQuery.js +135 -0
  14. package/dist/commsRetry.d.ts +12 -0
  15. package/dist/commsRetry.js +78 -0
  16. package/dist/commsService.d.ts +11 -4
  17. package/dist/commsService.js +224 -47
  18. package/dist/index.d.ts +3 -3
  19. package/dist/index.js +3 -1
  20. package/dist/routes/commsDashboard.d.ts +9 -0
  21. package/dist/routes/commsDashboard.js +412 -0
  22. package/dist/templates.d.ts +9 -0
  23. package/dist/templates.js +34 -1
  24. package/dist/types.d.ts +12 -0
  25. package/dist/webhooks/sendgridWebhooks.d.ts +10 -0
  26. package/dist/webhooks/sendgridWebhooks.js +250 -0
  27. package/dist/webhooks/twilioWebhooks.d.ts +11 -0
  28. package/dist/webhooks/twilioWebhooks.js +174 -0
  29. package/package.json +30 -9
  30. package/src/adapters/expoPush.test.ts +585 -0
  31. package/src/adapters/expoPush.ts +304 -0
  32. package/src/adapters/sendgrid.ts +8 -0
  33. package/src/adapters/twilioSms.test.ts +374 -0
  34. package/src/adapters/twilioSms.ts +237 -0
  35. package/src/adapters/twilioVerify.test.ts +207 -0
  36. package/src/adapters/twilioVerify.ts +227 -0
  37. package/src/commsApp.test.ts +13 -0
  38. package/src/commsApp.ts +38 -3
  39. package/src/commsQuery.test.ts +165 -0
  40. package/src/commsQuery.ts +163 -0
  41. package/src/commsRetry.ts +87 -0
  42. package/src/commsService.test.ts +128 -0
  43. package/src/commsService.ts +184 -20
  44. package/src/index.ts +9 -2
  45. package/src/routes/commsDashboard.test.ts +310 -0
  46. package/src/routes/commsDashboard.ts +324 -0
  47. package/src/templates.test.ts +63 -1
  48. package/src/templates.ts +47 -0
  49. package/src/tests/bunSetup.ts +8 -0
  50. package/src/types.ts +14 -0
  51. package/src/webhooks/sendgridWebhooks.test.ts +520 -0
  52. package/src/webhooks/sendgridWebhooks.ts +166 -0
  53. package/src/webhooks/twilioWebhooks.test.ts +352 -0
  54. package/src/webhooks/twilioWebhooks.ts +154 -0
  55. package/dist/routes/commsExplorer.d.ts +0 -7
  56. package/dist/routes/commsExplorer.js +0 -147
  57. package/src/routes/commsExplorer.ts +0 -114
@@ -1,147 +0,0 @@
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
- var __read = (this && this.__read) || function (o, n) {
39
- var m = typeof Symbol === "function" && o[Symbol.iterator];
40
- if (!m) return o;
41
- var i = m.call(o), r, ar = [], e;
42
- try {
43
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
- }
45
- catch (error) { e = { error: error }; }
46
- finally {
47
- try {
48
- if (r && !r.done && (m = i["return"])) m.call(i);
49
- }
50
- finally { if (e) throw e.error; }
51
- }
52
- return ar;
53
- };
54
- Object.defineProperty(exports, "__esModule", { value: true });
55
- exports.addCommsExplorerRoute = void 0;
56
- var api_1 = require("@terreno/api");
57
- var luxon_1 = require("luxon");
58
- var commsMessage_1 = require("../models/commsMessage");
59
- var addCommsExplorerRoute = function (app, options) {
60
- var _a;
61
- var routeOpenApi = (options === null || options === void 0 ? void 0 : options.openApi) ? { openApi: options.openApi } : {};
62
- app.get((_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : "/comms/messages", [
63
- (0, api_1.authenticateMiddleware)(),
64
- (0, api_1.createOpenApiBuilder)(routeOpenApi)
65
- .withTags(["admin", "comms"])
66
- .withSummary("List communication delivery attempts")
67
- .withQueryParameter("page", { type: "number" }, { required: false })
68
- .withQueryParameter("limit", { type: "number" }, { required: false })
69
- .withQueryParameter("channel", { type: "string" }, { required: false })
70
- .withQueryParameter("status", { type: "string" }, { required: false })
71
- .withQueryParameter("userId", { type: "string" }, { required: false })
72
- .withQueryParameter("startDate", { type: "string" }, { required: false })
73
- .withQueryParameter("endDate", { type: "string" }, { required: false })
74
- .withResponse(200, {
75
- data: { items: { type: "object" }, type: "array" },
76
- limit: { type: "number" },
77
- more: { type: "boolean" },
78
- page: { type: "number" },
79
- total: { type: "number" },
80
- })
81
- .build(),
82
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
83
- var user, page, limit, skip, match, startTimestamp, endTimestamp, start, end, _a, messages, total;
84
- return __generator(this, function (_b) {
85
- switch (_b.label) {
86
- case 0:
87
- user = req.user;
88
- if (!(user === null || user === void 0 ? void 0 : user.admin)) {
89
- throw new api_1.APIError({ status: 403, title: "Admin access required" });
90
- }
91
- page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
92
- limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
93
- skip = (page - 1) * limit;
94
- match = {
95
- deleted: { $ne: true },
96
- };
97
- if (req.query.channel) {
98
- match.channel = req.query.channel;
99
- }
100
- if (req.query.status) {
101
- match.status = req.query.status;
102
- }
103
- if (req.query.userId) {
104
- match.userId = req.query.userId;
105
- }
106
- if (req.query.startDate || req.query.endDate) {
107
- match.created = {};
108
- }
109
- if (req.query.startDate && match.created) {
110
- start = luxon_1.DateTime.fromISO(req.query.startDate, { zone: "utc" });
111
- if (!start.isValid) {
112
- throw new api_1.APIError({ status: 400, title: "Invalid startDate format" });
113
- }
114
- startTimestamp = start.toMillis();
115
- match.created.$gte = start.toJSDate();
116
- }
117
- if (req.query.endDate && match.created) {
118
- end = luxon_1.DateTime.fromISO(req.query.endDate, { zone: "utc" });
119
- if (!end.isValid) {
120
- throw new api_1.APIError({ status: 400, title: "Invalid endDate format" });
121
- }
122
- endTimestamp = end.toMillis();
123
- match.created.$lte = end.toJSDate();
124
- }
125
- if (startTimestamp !== undefined &&
126
- endTimestamp !== undefined &&
127
- startTimestamp > endTimestamp) {
128
- throw new api_1.APIError({ status: 400, title: "startDate must not be after endDate" });
129
- }
130
- return [4 /*yield*/, Promise.all([
131
- commsMessage_1.CommsMessage.find(match).sort("-created").skip(skip).limit(limit),
132
- commsMessage_1.CommsMessage.countDocuments(match),
133
- ])];
134
- case 1:
135
- _a = __read.apply(void 0, [_b.sent(), 2]), messages = _a[0], total = _a[1];
136
- return [2 /*return*/, res.json({
137
- data: messages,
138
- limit: limit,
139
- more: skip + limit < total,
140
- page: page,
141
- total: total,
142
- })];
143
- }
144
- });
145
- }); }));
146
- };
147
- exports.addCommsExplorerRoute = addCommsExplorerRoute;
@@ -1,114 +0,0 @@
1
- import {
2
- APIError,
3
- asyncHandler,
4
- authenticateMiddleware,
5
- createOpenApiBuilder,
6
- type OpenApiMiddleware,
7
- } from "@terreno/api";
8
- import type express from "express";
9
- import {DateTime} from "luxon";
10
-
11
- import {CommsMessage} from "../models/commsMessage";
12
-
13
- interface CommsExplorerRouteOptions {
14
- path?: string;
15
- openApi?: unknown;
16
- }
17
-
18
- export const addCommsExplorerRoute = (
19
- app: express.Application,
20
- options?: CommsExplorerRouteOptions
21
- ): void => {
22
- const routeOpenApi = options?.openApi ? {openApi: options.openApi as OpenApiMiddleware} : {};
23
-
24
- app.get(
25
- options?.path ?? "/comms/messages",
26
- [
27
- authenticateMiddleware(),
28
- createOpenApiBuilder(routeOpenApi)
29
- .withTags(["admin", "comms"])
30
- .withSummary("List communication delivery attempts")
31
- .withQueryParameter("page", {type: "number"}, {required: false})
32
- .withQueryParameter("limit", {type: "number"}, {required: false})
33
- .withQueryParameter("channel", {type: "string"}, {required: false})
34
- .withQueryParameter("status", {type: "string"}, {required: false})
35
- .withQueryParameter("userId", {type: "string"}, {required: false})
36
- .withQueryParameter("startDate", {type: "string"}, {required: false})
37
- .withQueryParameter("endDate", {type: "string"}, {required: false})
38
- .withResponse(200, {
39
- data: {items: {type: "object"}, type: "array"},
40
- limit: {type: "number"},
41
- more: {type: "boolean"},
42
- page: {type: "number"},
43
- total: {type: "number"},
44
- })
45
- .build(),
46
- ],
47
- asyncHandler(async (req: express.Request, res: express.Response) => {
48
- const user = req.user as {admin?: boolean} | undefined;
49
- if (!user?.admin) {
50
- throw new APIError({status: 403, title: "Admin access required"});
51
- }
52
-
53
- const page = Math.max(1, Number.parseInt(req.query.page as string, 10) || 1);
54
- const limit = Math.min(
55
- 100,
56
- Math.max(1, Number.parseInt(req.query.limit as string, 10) || 20)
57
- );
58
- const skip = (page - 1) * limit;
59
- const match: Record<string, unknown> & {created?: {$gte?: Date; $lte?: Date}} = {
60
- deleted: {$ne: true},
61
- };
62
- let startTimestamp: number | undefined;
63
- let endTimestamp: number | undefined;
64
-
65
- if (req.query.channel) {
66
- match.channel = req.query.channel;
67
- }
68
- if (req.query.status) {
69
- match.status = req.query.status;
70
- }
71
- if (req.query.userId) {
72
- match.userId = req.query.userId;
73
- }
74
- if (req.query.startDate || req.query.endDate) {
75
- match.created = {};
76
- }
77
- if (req.query.startDate && match.created) {
78
- const start = DateTime.fromISO(req.query.startDate as string, {zone: "utc"});
79
- if (!start.isValid) {
80
- throw new APIError({status: 400, title: "Invalid startDate format"});
81
- }
82
- startTimestamp = start.toMillis();
83
- match.created.$gte = start.toJSDate();
84
- }
85
- if (req.query.endDate && match.created) {
86
- const end = DateTime.fromISO(req.query.endDate as string, {zone: "utc"});
87
- if (!end.isValid) {
88
- throw new APIError({status: 400, title: "Invalid endDate format"});
89
- }
90
- endTimestamp = end.toMillis();
91
- match.created.$lte = end.toJSDate();
92
- }
93
- if (
94
- startTimestamp !== undefined &&
95
- endTimestamp !== undefined &&
96
- startTimestamp > endTimestamp
97
- ) {
98
- throw new APIError({status: 400, title: "startDate must not be after endDate"});
99
- }
100
-
101
- const [messages, total] = await Promise.all([
102
- CommsMessage.find(match).sort("-created").skip(skip).limit(limit),
103
- CommsMessage.countDocuments(match),
104
- ]);
105
- return res.json({
106
- data: messages,
107
- limit,
108
- more: skip + limit < total,
109
- page,
110
- total,
111
- });
112
- })
113
- );
114
- };