@terreno/comms 56.0.0-beta.2
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/LICENSE +21 -0
- package/biome.jsonc +17 -0
- package/bunfig.toml +5 -0
- package/dist/adapters/console.d.ts +30 -0
- package/dist/adapters/console.js +122 -0
- package/dist/commsApp.d.ts +14 -0
- package/dist/commsApp.js +37 -0
- package/dist/commsService.d.ts +20 -0
- package/dist/commsService.js +386 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +19 -0
- package/dist/modelTypes.d.ts +53 -0
- package/dist/modelTypes.js +2 -0
- package/dist/models/commsMessage.d.ts +2 -0
- package/dist/models/commsMessage.js +119 -0
- package/dist/models/pushToken.d.ts +2 -0
- package/dist/models/pushToken.js +56 -0
- package/dist/routes/commsExplorer.d.ts +7 -0
- package/dist/routes/commsExplorer.js +147 -0
- package/dist/routes/pushTokens.d.ts +7 -0
- package/dist/routes/pushTokens.js +292 -0
- package/dist/templates.d.ts +10 -0
- package/dist/templates.js +32 -0
- package/dist/types.d.ts +78 -0
- package/dist/types.js +2 -0
- package/package.json +55 -0
- package/src/adapters/console.test.ts +227 -0
- package/src/adapters/console.ts +87 -0
- package/src/commsApp.test.ts +448 -0
- package/src/commsApp.ts +48 -0
- package/src/commsService.test.ts +570 -0
- package/src/commsService.ts +293 -0
- package/src/index.ts +45 -0
- package/src/modelTypes.ts +78 -0
- package/src/models/commsMessage.test.ts +53 -0
- package/src/models/commsMessage.ts +97 -0
- package/src/models/pushToken.test.ts +45 -0
- package/src/models/pushToken.ts +69 -0
- package/src/routes/commsExplorer.ts +114 -0
- package/src/routes/pushTokens.ts +265 -0
- package/src/templates.test.ts +37 -0
- package/src/templates.ts +31 -0
- package/src/tests/bunSetup.ts +14 -0
- package/src/types.ts +93 -0
- package/tsconfig.json +32 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import {beforeEach, describe, it} from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
addAuthRoutes,
|
|
4
|
+
apiErrorMiddleware,
|
|
5
|
+
apiUnauthorizedMiddleware,
|
|
6
|
+
setupAuth,
|
|
7
|
+
TerrenoApp,
|
|
8
|
+
type UserModel as UserModelType,
|
|
9
|
+
} from "@terreno/api";
|
|
10
|
+
import {authAsUser, getBaseServer, setupDb, UserModel} from "@terreno/api/testing";
|
|
11
|
+
import {assert} from "chai";
|
|
12
|
+
import type express from "express";
|
|
13
|
+
import {DateTime} from "luxon";
|
|
14
|
+
import supertest from "supertest";
|
|
15
|
+
import type TestAgent from "supertest/lib/agent";
|
|
16
|
+
|
|
17
|
+
import {CommsApp, getCommsService} from "./commsApp";
|
|
18
|
+
import {CommsMessage} from "./models/commsMessage";
|
|
19
|
+
import {PushToken} from "./models/pushToken";
|
|
20
|
+
|
|
21
|
+
const buildApp = (): express.Application => {
|
|
22
|
+
const app = getBaseServer();
|
|
23
|
+
setupAuth(app, UserModel as unknown as UserModelType);
|
|
24
|
+
addAuthRoutes(app, UserModel as unknown as UserModelType);
|
|
25
|
+
new CommsApp().register(app);
|
|
26
|
+
app.use(apiUnauthorizedMiddleware);
|
|
27
|
+
app.use(apiErrorMiddleware);
|
|
28
|
+
return app;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const signUpAgent = async (
|
|
32
|
+
app: express.Application,
|
|
33
|
+
email: string
|
|
34
|
+
): Promise<{agent: TestAgent; userId: string}> => {
|
|
35
|
+
const agent = supertest.agent(app);
|
|
36
|
+
const signup = await agent.post("/auth/signup").send({email, password: "racer-password"});
|
|
37
|
+
await agent.set("authorization", `Bearer ${signup.body.data.token}`);
|
|
38
|
+
return {agent, userId: signup.body.data.userId};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
describe("CommsApp", () => {
|
|
42
|
+
beforeEach(async (): Promise<void> => {
|
|
43
|
+
await setupDb();
|
|
44
|
+
// The atomic token claim relies on the unique index, so make sure it is built.
|
|
45
|
+
await PushToken.init();
|
|
46
|
+
await Promise.all([CommsMessage.deleteMany({}), PushToken.deleteMany({})]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("registers the configured service accessor", (): void => {
|
|
50
|
+
const app = getBaseServer();
|
|
51
|
+
const plugin = new CommsApp({logMessages: false});
|
|
52
|
+
|
|
53
|
+
plugin.register(app);
|
|
54
|
+
|
|
55
|
+
assert.strictEqual(getCommsService(), plugin.service);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("upserts push tokens and isolates list and delete operations by owner", async (): Promise<void> => {
|
|
59
|
+
const app = buildApp();
|
|
60
|
+
const owner = await authAsUser(app, "notAdmin");
|
|
61
|
+
const admin = await authAsUser(app, "admin");
|
|
62
|
+
const other = supertest.agent(app);
|
|
63
|
+
const signup = await other
|
|
64
|
+
.post("/auth/signup")
|
|
65
|
+
.send({email: "other@example.com", password: "other-password"})
|
|
66
|
+
.expect(200);
|
|
67
|
+
await other.set("authorization", `Bearer ${signup.body.data.token}`);
|
|
68
|
+
|
|
69
|
+
const created = await owner
|
|
70
|
+
.post("/comms/pushTokens")
|
|
71
|
+
.send({deviceId: "device-1", platform: "ios", token: "ExponentPushToken[test]"})
|
|
72
|
+
.expect(201);
|
|
73
|
+
const updated = await owner
|
|
74
|
+
.post("/comms/pushTokens")
|
|
75
|
+
.send({deviceId: "device-1", platform: "android", token: "ExponentPushToken[test]"})
|
|
76
|
+
.expect(200);
|
|
77
|
+
|
|
78
|
+
assert.equal(created.body.data._id, updated.body.data._id);
|
|
79
|
+
assert.equal(updated.body.data.platform, "android");
|
|
80
|
+
assert.equal(updated.body.data.deviceId, "device-1");
|
|
81
|
+
assert.equal(await PushToken.countDocuments(), 1);
|
|
82
|
+
|
|
83
|
+
// The atomic claim bypasses save() middleware, so it maintains timestamps itself.
|
|
84
|
+
const stored = await PushToken.findExactlyOne({token: "ExponentPushToken[test]"});
|
|
85
|
+
assert.instanceOf(stored.created, Date);
|
|
86
|
+
assert.instanceOf(stored.updated, Date);
|
|
87
|
+
assert.isFalse(stored.deleted);
|
|
88
|
+
assert.equal(
|
|
89
|
+
stored.created.toISOString(),
|
|
90
|
+
new Date(created.body.data.created).toISOString(),
|
|
91
|
+
"created must not change on refresh"
|
|
92
|
+
);
|
|
93
|
+
assert.isAtLeast(stored.updated.getTime(), stored.created.getTime());
|
|
94
|
+
|
|
95
|
+
const refreshed = await owner
|
|
96
|
+
.post("/comms/pushTokens")
|
|
97
|
+
.send({platform: "android", token: "ExponentPushToken[test]"})
|
|
98
|
+
.expect(200);
|
|
99
|
+
assert.equal(refreshed.body.data.deviceId, "device-1");
|
|
100
|
+
|
|
101
|
+
const ownerList = await owner.get("/comms/pushTokens").expect(200);
|
|
102
|
+
const otherList = await other.get("/comms/pushTokens").expect(200);
|
|
103
|
+
assert.lengthOf(ownerList.body.data, 1);
|
|
104
|
+
assert.lengthOf(otherList.body.data, 0);
|
|
105
|
+
|
|
106
|
+
await other.delete(`/comms/pushTokens/${created.body.data._id}`).expect(403);
|
|
107
|
+
await admin.delete(`/comms/pushTokens/${created.body.data._id}`).expect(403);
|
|
108
|
+
await owner.delete(`/comms/pushTokens/${created.body.data._id}`).expect(204);
|
|
109
|
+
|
|
110
|
+
const token = await PushToken.findExactlyOne({_id: created.body.data._id});
|
|
111
|
+
assert.isFalse(token.active);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("requires authentication for push-token registration", async (): Promise<void> => {
|
|
115
|
+
const app = buildApp();
|
|
116
|
+
|
|
117
|
+
await supertest(app)
|
|
118
|
+
.post("/comms/pushTokens")
|
|
119
|
+
.send({platform: "ios", token: "ExponentPushToken[test]"})
|
|
120
|
+
.expect(401);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("transfers an inactive device token without allowing active-token takeover", async (): Promise<void> => {
|
|
124
|
+
const app = buildApp();
|
|
125
|
+
const first = await authAsUser(app, "notAdmin");
|
|
126
|
+
const second = supertest.agent(app);
|
|
127
|
+
const signup = await second
|
|
128
|
+
.post("/auth/signup")
|
|
129
|
+
.send({email: "second@example.com", password: "second-password"})
|
|
130
|
+
.expect(200);
|
|
131
|
+
await second.set("authorization", `Bearer ${signup.body.data.token}`);
|
|
132
|
+
|
|
133
|
+
const created = await first
|
|
134
|
+
.post("/comms/pushTokens")
|
|
135
|
+
.send({platform: "ios", token: "ExponentPushToken[transfer]"})
|
|
136
|
+
.expect(201);
|
|
137
|
+
await second
|
|
138
|
+
.post("/comms/pushTokens")
|
|
139
|
+
.send({platform: "android", token: "ExponentPushToken[transfer]"})
|
|
140
|
+
.expect(409);
|
|
141
|
+
assert.lengthOf((await first.get("/comms/pushTokens").expect(200)).body.data, 1);
|
|
142
|
+
assert.lengthOf((await second.get("/comms/pushTokens").expect(200)).body.data, 0);
|
|
143
|
+
|
|
144
|
+
await first.delete(`/comms/pushTokens/${created.body.data._id}`).expect(204);
|
|
145
|
+
const transferred = await second
|
|
146
|
+
.post("/comms/pushTokens")
|
|
147
|
+
.send({platform: "android", token: "ExponentPushToken[transfer]"})
|
|
148
|
+
.expect(200);
|
|
149
|
+
|
|
150
|
+
assert.equal(await PushToken.countDocuments(), 1);
|
|
151
|
+
assert.equal(transferred.body.data.userId, signup.body.data.userId);
|
|
152
|
+
assert.lengthOf((await first.get("/comms/pushTokens").expect(200)).body.data, 0);
|
|
153
|
+
assert.lengthOf((await second.get("/comms/pushTokens").expect(200)).body.data, 1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("claims a new token for exactly one user under concurrent registration", async (): Promise<void> => {
|
|
157
|
+
const app = buildApp();
|
|
158
|
+
const [first, second] = await Promise.all([
|
|
159
|
+
signUpAgent(app, "racer-one@example.com"),
|
|
160
|
+
signUpAgent(app, "racer-two@example.com"),
|
|
161
|
+
]);
|
|
162
|
+
|
|
163
|
+
const responses = await Promise.all([
|
|
164
|
+
first.agent
|
|
165
|
+
.post("/comms/pushTokens")
|
|
166
|
+
.send({platform: "ios", token: "ExponentPushToken[race]"}),
|
|
167
|
+
second.agent
|
|
168
|
+
.post("/comms/pushTokens")
|
|
169
|
+
.send({platform: "android", token: "ExponentPushToken[race]"}),
|
|
170
|
+
]);
|
|
171
|
+
|
|
172
|
+
const accepted = responses.filter((response) => response.status < 300);
|
|
173
|
+
const conflicted = responses.filter((response) => response.status === 409);
|
|
174
|
+
assert.lengthOf(accepted, 1);
|
|
175
|
+
assert.lengthOf(conflicted, 1);
|
|
176
|
+
assert.equal(await PushToken.countDocuments({token: "ExponentPushToken[race]"}), 1);
|
|
177
|
+
|
|
178
|
+
const stored = await PushToken.findExactlyOne({token: "ExponentPushToken[race]"});
|
|
179
|
+
assert.equal(stored.userId.toString(), accepted[0]?.body.data.userId);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("claims a released token for exactly one user under concurrent registration", async (): Promise<void> => {
|
|
183
|
+
const app = buildApp();
|
|
184
|
+
const owner = await authAsUser(app, "notAdmin");
|
|
185
|
+
const [first, second] = await Promise.all([
|
|
186
|
+
signUpAgent(app, "claimer-one@example.com"),
|
|
187
|
+
signUpAgent(app, "claimer-two@example.com"),
|
|
188
|
+
]);
|
|
189
|
+
|
|
190
|
+
const created = await owner
|
|
191
|
+
.post("/comms/pushTokens")
|
|
192
|
+
.send({platform: "ios", token: "ExponentPushToken[released]"})
|
|
193
|
+
.expect(201);
|
|
194
|
+
await owner.delete(`/comms/pushTokens/${created.body.data._id}`).expect(204);
|
|
195
|
+
|
|
196
|
+
const responses = await Promise.all([
|
|
197
|
+
first.agent
|
|
198
|
+
.post("/comms/pushTokens")
|
|
199
|
+
.send({platform: "ios", token: "ExponentPushToken[released]"}),
|
|
200
|
+
second.agent
|
|
201
|
+
.post("/comms/pushTokens")
|
|
202
|
+
.send({platform: "android", token: "ExponentPushToken[released]"}),
|
|
203
|
+
]);
|
|
204
|
+
|
|
205
|
+
const accepted = responses.filter((response) => response.status < 300);
|
|
206
|
+
assert.lengthOf(accepted, 1);
|
|
207
|
+
assert.lengthOf(
|
|
208
|
+
responses.filter((response) => response.status === 409),
|
|
209
|
+
1
|
|
210
|
+
);
|
|
211
|
+
assert.equal(await PushToken.countDocuments({token: "ExponentPushToken[released]"}), 1);
|
|
212
|
+
|
|
213
|
+
const stored = await PushToken.findExactlyOne({token: "ExponentPushToken[released]"});
|
|
214
|
+
assert.isTrue(stored.active);
|
|
215
|
+
assert.equal(stored.userId.toString(), accepted[0]?.body.data.userId);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("keeps an active token with its owner while a foreign claim races a refresh", async (): Promise<void> => {
|
|
219
|
+
const app = buildApp();
|
|
220
|
+
const {agent: owner, userId: ownerId} = await signUpAgent(app, "holder@example.com");
|
|
221
|
+
const {agent: intruder} = await signUpAgent(app, "intruder@example.com");
|
|
222
|
+
await owner
|
|
223
|
+
.post("/comms/pushTokens")
|
|
224
|
+
.send({platform: "ios", token: "ExponentPushToken[held]"})
|
|
225
|
+
.expect(201);
|
|
226
|
+
|
|
227
|
+
const [refresh, takeover] = await Promise.all([
|
|
228
|
+
owner.post("/comms/pushTokens").send({platform: "ios", token: "ExponentPushToken[held]"}),
|
|
229
|
+
intruder
|
|
230
|
+
.post("/comms/pushTokens")
|
|
231
|
+
.send({platform: "android", token: "ExponentPushToken[held]"}),
|
|
232
|
+
]);
|
|
233
|
+
|
|
234
|
+
assert.isBelow(refresh.status, 300);
|
|
235
|
+
assert.equal(takeover.status, 409);
|
|
236
|
+
assert.equal(await PushToken.countDocuments({token: "ExponentPushToken[held]"}), 1);
|
|
237
|
+
const stored = await PushToken.findExactlyOne({token: "ExponentPushToken[held]"});
|
|
238
|
+
assert.equal(stored.userId.toString(), ownerId);
|
|
239
|
+
assert.equal(stored.platform, "ios");
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("revives a soft-deleted token row on re-registration", async (): Promise<void> => {
|
|
243
|
+
const app = buildApp();
|
|
244
|
+
const {agent, userId} = await signUpAgent(app, "revive@example.com");
|
|
245
|
+
const created = await agent
|
|
246
|
+
.post("/comms/pushTokens")
|
|
247
|
+
.send({platform: "ios", token: "ExponentPushToken[revive]"})
|
|
248
|
+
.expect(201);
|
|
249
|
+
await PushToken.updateOne({_id: created.body.data._id}, {$set: {active: false, deleted: true}});
|
|
250
|
+
|
|
251
|
+
const revived = await agent
|
|
252
|
+
.post("/comms/pushTokens")
|
|
253
|
+
.send({platform: "android", token: "ExponentPushToken[revive]"})
|
|
254
|
+
.expect(200);
|
|
255
|
+
|
|
256
|
+
assert.equal(revived.body.data._id, created.body.data._id);
|
|
257
|
+
assert.equal(await PushToken.countDocuments({token: "ExponentPushToken[revive]"}), 1);
|
|
258
|
+
const stored = await PushToken.findExactlyOne({token: "ExponentPushToken[revive]"});
|
|
259
|
+
assert.isTrue(stored.active);
|
|
260
|
+
assert.isFalse(stored.deleted);
|
|
261
|
+
assert.equal(stored.userId.toString(), userId);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("keeps refreshes idempotent for the owner under concurrent registration", async (): Promise<void> => {
|
|
265
|
+
const app = buildApp();
|
|
266
|
+
const {agent, userId} = await signUpAgent(app, "self-racer@example.com");
|
|
267
|
+
|
|
268
|
+
const responses = await Promise.all([
|
|
269
|
+
agent.post("/comms/pushTokens").send({platform: "ios", token: "ExponentPushToken[self]"}),
|
|
270
|
+
agent.post("/comms/pushTokens").send({platform: "ios", token: "ExponentPushToken[self]"}),
|
|
271
|
+
]);
|
|
272
|
+
|
|
273
|
+
for (const response of responses) {
|
|
274
|
+
assert.isBelow(response.status, 300);
|
|
275
|
+
}
|
|
276
|
+
assert.equal(await PushToken.countDocuments({token: "ExponentPushToken[self]"}), 1);
|
|
277
|
+
const stored = await PushToken.findExactlyOne({token: "ExponentPushToken[self]"});
|
|
278
|
+
assert.equal(stored.userId.toString(), userId);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("rejects unauthenticated reads, deletes, and explorer requests", async (): Promise<void> => {
|
|
282
|
+
const app = buildApp();
|
|
283
|
+
const anonymous = supertest(app);
|
|
284
|
+
|
|
285
|
+
await anonymous.get("/comms/pushTokens").expect(401);
|
|
286
|
+
await anonymous.delete("/comms/pushTokens/507f1f77bcf86cd799439011").expect(401);
|
|
287
|
+
await anonymous.get("/comms/messages").expect(401);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("validates push-token registration fields", async (): Promise<void> => {
|
|
291
|
+
const app = buildApp();
|
|
292
|
+
const owner = await authAsUser(app, "notAdmin");
|
|
293
|
+
|
|
294
|
+
await owner.post("/comms/pushTokens").send({platform: "ios", token: " "}).expect(400);
|
|
295
|
+
await owner.post("/comms/pushTokens").send({platform: "ios"}).expect(400);
|
|
296
|
+
await owner
|
|
297
|
+
.post("/comms/pushTokens")
|
|
298
|
+
.send({token: "ExponentPushToken[missing-platform]"})
|
|
299
|
+
.expect(400);
|
|
300
|
+
await owner
|
|
301
|
+
.post("/comms/pushTokens")
|
|
302
|
+
.send({platform: "desktop", token: "ExponentPushToken[test]"})
|
|
303
|
+
.expect(400);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("exposes a paginated, filterable delivery explorer to admins only", async (): Promise<void> => {
|
|
307
|
+
const app = buildApp();
|
|
308
|
+
const admin = await authAsUser(app, "admin");
|
|
309
|
+
const user = await authAsUser(app, "notAdmin");
|
|
310
|
+
await Promise.all([
|
|
311
|
+
CommsMessage.logSend({
|
|
312
|
+
channel: "mail",
|
|
313
|
+
provider: "console",
|
|
314
|
+
status: "sent",
|
|
315
|
+
to: "[redacted]",
|
|
316
|
+
}),
|
|
317
|
+
CommsMessage.logSend({
|
|
318
|
+
channel: "sms",
|
|
319
|
+
provider: "console",
|
|
320
|
+
status: "failed",
|
|
321
|
+
to: "[redacted]",
|
|
322
|
+
}),
|
|
323
|
+
]);
|
|
324
|
+
|
|
325
|
+
await user.get("/comms/messages").expect(403);
|
|
326
|
+
const response = await admin
|
|
327
|
+
.get("/comms/messages")
|
|
328
|
+
.query({channel: "sms", limit: 1, page: 1, status: "failed"})
|
|
329
|
+
.expect(200);
|
|
330
|
+
|
|
331
|
+
assert.lengthOf(response.body.data, 1);
|
|
332
|
+
assert.equal(response.body.data[0].channel, "sms");
|
|
333
|
+
assert.equal(response.body.data[0].status, "failed");
|
|
334
|
+
assert.equal(response.body.limit, 1);
|
|
335
|
+
assert.equal(response.body.page, 1);
|
|
336
|
+
assert.equal(response.body.total, 1);
|
|
337
|
+
assert.isFalse(response.body.more);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("filters the delivery explorer by user and date and validates date input", async (): Promise<void> => {
|
|
341
|
+
const app = buildApp();
|
|
342
|
+
const admin = await authAsUser(app, "admin");
|
|
343
|
+
const userId = "507f1f77bcf86cd799439011";
|
|
344
|
+
await CommsMessage.logSend({
|
|
345
|
+
channel: "mail",
|
|
346
|
+
provider: "console",
|
|
347
|
+
status: "sent",
|
|
348
|
+
to: "[redacted]",
|
|
349
|
+
userId,
|
|
350
|
+
});
|
|
351
|
+
const startDate = DateTime.utc().minus({minutes: 1}).toISO();
|
|
352
|
+
const endDate = DateTime.utc().plus({minutes: 1}).toISO();
|
|
353
|
+
|
|
354
|
+
const response = await admin
|
|
355
|
+
.get("/comms/messages")
|
|
356
|
+
.query({endDate, startDate, userId})
|
|
357
|
+
.expect(200);
|
|
358
|
+
|
|
359
|
+
assert.lengthOf(response.body.data, 1);
|
|
360
|
+
assert.equal(response.body.data[0].userId, userId);
|
|
361
|
+
assert.equal(
|
|
362
|
+
(await admin.get("/comms/messages").query({userId: "507f191e810c19729de860ea"})).body.total,
|
|
363
|
+
0
|
|
364
|
+
);
|
|
365
|
+
assert.equal(
|
|
366
|
+
(await admin.get("/comms/messages").query({startDate: "not-a-date"}).expect(400)).body.title,
|
|
367
|
+
"Invalid startDate format"
|
|
368
|
+
);
|
|
369
|
+
assert.equal(
|
|
370
|
+
(await admin.get("/comms/messages").query({endDate: "not-a-date"}).expect(400)).body.title,
|
|
371
|
+
"Invalid endDate format"
|
|
372
|
+
);
|
|
373
|
+
assert.equal(
|
|
374
|
+
(
|
|
375
|
+
await admin
|
|
376
|
+
.get("/comms/messages")
|
|
377
|
+
.query({
|
|
378
|
+
endDate: DateTime.utc().minus({minutes: 1}).toISO(),
|
|
379
|
+
startDate: DateTime.utc().plus({minutes: 1}).toISO(),
|
|
380
|
+
})
|
|
381
|
+
.expect(400)
|
|
382
|
+
).body.title,
|
|
383
|
+
"startDate must not be after endDate"
|
|
384
|
+
);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("clamps explorer pagination and reports additional pages", async (): Promise<void> => {
|
|
388
|
+
const app = buildApp();
|
|
389
|
+
const admin = await authAsUser(app, "admin");
|
|
390
|
+
await Promise.all(
|
|
391
|
+
["mail", "sms"].map(
|
|
392
|
+
async (channel): Promise<unknown> =>
|
|
393
|
+
CommsMessage.logSend({
|
|
394
|
+
channel: channel as "mail" | "sms",
|
|
395
|
+
provider: "console",
|
|
396
|
+
status: "sent",
|
|
397
|
+
to: "[redacted]",
|
|
398
|
+
})
|
|
399
|
+
)
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
const first = await admin.get("/comms/messages").query({limit: 1, page: 0}).expect(200);
|
|
403
|
+
const second = await admin.get("/comms/messages").query({limit: 1, page: 2}).expect(200);
|
|
404
|
+
const clamped = await admin.get("/comms/messages").query({limit: 999}).expect(200);
|
|
405
|
+
const negative = await admin.get("/comms/messages").query({limit: -10, page: -2}).expect(200);
|
|
406
|
+
const nonNumeric = await admin
|
|
407
|
+
.get("/comms/messages")
|
|
408
|
+
.query({limit: "invalid", page: "invalid"})
|
|
409
|
+
.expect(200);
|
|
410
|
+
|
|
411
|
+
assert.equal(first.body.page, 1);
|
|
412
|
+
assert.isTrue(first.body.more);
|
|
413
|
+
assert.equal(second.body.page, 2);
|
|
414
|
+
assert.isFalse(second.body.more);
|
|
415
|
+
assert.equal(clamped.body.limit, 100);
|
|
416
|
+
assert.equal(negative.body.limit, 1);
|
|
417
|
+
assert.equal(negative.body.page, 1);
|
|
418
|
+
assert.equal(nonNumeric.body.limit, 20);
|
|
419
|
+
assert.equal(nonNumeric.body.page, 1);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("publishes push-token and delivery-explorer routes in OpenAPI", async (): Promise<void> => {
|
|
423
|
+
const app = new TerrenoApp({
|
|
424
|
+
skipListen: true,
|
|
425
|
+
userModel: UserModel as unknown as UserModelType,
|
|
426
|
+
})
|
|
427
|
+
.register(new CommsApp())
|
|
428
|
+
.build();
|
|
429
|
+
|
|
430
|
+
const response = await supertest(app).get("/openapi.json").expect(200);
|
|
431
|
+
|
|
432
|
+
assert.property(response.body.paths, "/comms/pushTokens");
|
|
433
|
+
assert.property(response.body.paths["/comms/pushTokens"], "post");
|
|
434
|
+
assert.property(response.body.paths, "/comms/messages");
|
|
435
|
+
assert.property(response.body.paths["/comms/messages"], "get");
|
|
436
|
+
assert.notProperty(response.body.paths, "/comms/pushTokens/");
|
|
437
|
+
|
|
438
|
+
// A shared tag keeps generated SDK mutations invalidating the by-id read.
|
|
439
|
+
for (const operation of [
|
|
440
|
+
response.body.paths["/comms/pushTokens"].post,
|
|
441
|
+
response.body.paths["/comms/pushTokens"].get,
|
|
442
|
+
response.body.paths["/comms/pushTokens/{id}"].get,
|
|
443
|
+
response.body.paths["/comms/pushTokens/{id}"].delete,
|
|
444
|
+
]) {
|
|
445
|
+
assert.includeMembers(operation.tags, ["comms"]);
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
});
|
package/src/commsApp.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {APIError, logger, type TerrenoPlugin} from "@terreno/api";
|
|
2
|
+
import type express from "express";
|
|
3
|
+
|
|
4
|
+
import {CommsService} from "./commsService";
|
|
5
|
+
import {PushToken} from "./models/pushToken";
|
|
6
|
+
import {addCommsExplorerRoute} from "./routes/commsExplorer";
|
|
7
|
+
import {addPushTokenRoutes} from "./routes/pushTokens";
|
|
8
|
+
import type {CommsOptions} from "./types";
|
|
9
|
+
|
|
10
|
+
export interface CommsAppOptions extends CommsOptions {
|
|
11
|
+
basePath?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let registeredCommsService: CommsService | undefined;
|
|
15
|
+
|
|
16
|
+
export const getCommsService = (): CommsService => {
|
|
17
|
+
if (!registeredCommsService) {
|
|
18
|
+
throw new APIError({status: 500, title: "CommsApp is not registered"});
|
|
19
|
+
}
|
|
20
|
+
return registeredCommsService;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export class CommsApp implements TerrenoPlugin {
|
|
24
|
+
readonly service: CommsService;
|
|
25
|
+
private readonly options: CommsAppOptions;
|
|
26
|
+
|
|
27
|
+
constructor(options?: CommsAppOptions) {
|
|
28
|
+
this.options = options ?? {};
|
|
29
|
+
this.service = new CommsService(this.options);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
register(app: express.Application, openApi?: unknown): void {
|
|
33
|
+
const basePath = this.options.basePath ?? "/comms";
|
|
34
|
+
registeredCommsService = this.service;
|
|
35
|
+
|
|
36
|
+
// Atomic push-token registration depends on the unique token index to reject concurrent
|
|
37
|
+
// claims, so build it explicitly instead of relying on Mongoose autoIndex being enabled.
|
|
38
|
+
void PushToken.init().catch((error: unknown) => {
|
|
39
|
+
logger.error(
|
|
40
|
+
`[comms] Failed to build PushToken indexes; concurrent token registration cannot be ` +
|
|
41
|
+
`rejected safely until the unique token index exists: ${String(error)}`
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
addPushTokenRoutes(app, {basePath: `${basePath}/pushTokens`, openApi});
|
|
46
|
+
addCommsExplorerRoute(app, {openApi, path: `${basePath}/messages`});
|
|
47
|
+
}
|
|
48
|
+
}
|