@rpcbase/server 0.524.0 → 0.526.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 (46) hide show
  1. package/dist/email-Dzauaq11.js +12449 -0
  2. package/dist/email-Dzauaq11.js.map +1 -0
  3. package/dist/handler-BLwgdQv-.js +544 -0
  4. package/dist/handler-BLwgdQv-.js.map +1 -0
  5. package/dist/handler-Cq-OJ0Rf.js +182 -0
  6. package/dist/handler-Cq-OJ0Rf.js.map +1 -0
  7. package/dist/handler-Cq6MsoD4.js +124 -0
  8. package/dist/handler-Cq6MsoD4.js.map +1 -0
  9. package/dist/handler-Cx_ZP_NB.js +749 -0
  10. package/dist/handler-Cx_ZP_NB.js.map +1 -0
  11. package/dist/index.js +4783 -4935
  12. package/dist/index.js.map +1 -1
  13. package/dist/notifications.js +134 -199
  14. package/dist/notifications.js.map +1 -1
  15. package/dist/queryExecutor-DYVlCvns.js +295 -0
  16. package/dist/queryExecutor-DYVlCvns.js.map +1 -0
  17. package/dist/render_resend-CQb8_8G7.js +7 -0
  18. package/dist/render_resend-CQb8_8G7.js.map +1 -0
  19. package/dist/rts/index.js +581 -725
  20. package/dist/rts/index.js.map +1 -1
  21. package/dist/schemas-BR3K5Luo.js +3824 -0
  22. package/dist/schemas-BR3K5Luo.js.map +1 -0
  23. package/dist/shared-BfMSZm2P.js +87 -0
  24. package/dist/shared-BfMSZm2P.js.map +1 -0
  25. package/dist/ssrMiddleware.d.ts +1 -1
  26. package/dist/uploads.js +71 -84
  27. package/dist/uploads.js.map +1 -1
  28. package/package.json +9 -9
  29. package/dist/email-DK8uUU4X.js +0 -8045
  30. package/dist/email-DK8uUU4X.js.map +0 -1
  31. package/dist/handler-0rPClEv4.js +0 -663
  32. package/dist/handler-0rPClEv4.js.map +0 -1
  33. package/dist/handler-3uwH4f67.js +0 -924
  34. package/dist/handler-3uwH4f67.js.map +0 -1
  35. package/dist/handler-BsauvgjA.js +0 -153
  36. package/dist/handler-BsauvgjA.js.map +0 -1
  37. package/dist/handler-DnSJAQ_B.js +0 -203
  38. package/dist/handler-DnSJAQ_B.js.map +0 -1
  39. package/dist/queryExecutor-Bg1GGL3j.js +0 -407
  40. package/dist/queryExecutor-Bg1GGL3j.js.map +0 -1
  41. package/dist/render_resend_false-MiC__Smr.js +0 -6
  42. package/dist/render_resend_false-MiC__Smr.js.map +0 -1
  43. package/dist/schemas-Cjdjgehl.js +0 -4225
  44. package/dist/schemas-Cjdjgehl.js.map +0 -1
  45. package/dist/shared-BqZiSOmf.js +0 -111
  46. package/dist/shared-BqZiSOmf.js.map +0 -1
@@ -0,0 +1,544 @@
1
+ import { createNotification, sendNotificationsDigestForUser } from "./notifications.js";
2
+ import { a as object, c as unknown, i as number, n as array, o as record, r as boolean, s as string, t as _enum } from "./schemas-BR3K5Luo.js";
3
+ import { models } from "@rpcbase/db";
4
+ import { buildAbilityFromSession, getAccessibleByQuery } from "@rpcbase/db/acl";
5
+ //#region src/notifications/api/notifications/shared.ts
6
+ var getSessionUser = (ctx) => {
7
+ const rawSessionUser = ctx.req.session?.user;
8
+ const userId = typeof rawSessionUser?.id === "string" ? rawSessionUser.id.trim() : "";
9
+ const tenantId = typeof rawSessionUser?.currentTenantId === "string" ? rawSessionUser.currentTenantId.trim() : "";
10
+ if (!userId) {
11
+ ctx.res.status(401);
12
+ return null;
13
+ }
14
+ if (!tenantId) {
15
+ ctx.res.status(400);
16
+ return null;
17
+ }
18
+ return {
19
+ userId,
20
+ tenantId
21
+ };
22
+ };
23
+ //#endregion
24
+ //#region src/notifications/api/notifications/index.ts
25
+ var ListRoute = "/api/rb/notifications";
26
+ var CreateRoute = "/api/rb/notifications/create";
27
+ var MarkReadRoute = "/api/rb/notifications/:notificationId/read";
28
+ var ArchiveRoute = "/api/rb/notifications/:notificationId/archive";
29
+ var MarkAllReadRoute = "/api/rb/notifications/mark-all-read";
30
+ var SettingsRoute = "/api/rb/notifications/settings";
31
+ var DigestRunRoute = "/api/rb/notifications/digest/run";
32
+ var listRequestSchema = object({
33
+ includeArchived: boolean().optional(),
34
+ unreadOnly: boolean().optional(),
35
+ limit: number().int().min(1).max(200).optional(),
36
+ markSeen: boolean().optional()
37
+ });
38
+ var createRequestSchema = object({
39
+ topic: string().trim().min(1).optional(),
40
+ title: string().trim().min(1),
41
+ body: string().trim().optional(),
42
+ url: string().trim().optional(),
43
+ metadata: record(string(), unknown()).optional()
44
+ });
45
+ var notificationSchema = object({
46
+ id: string(),
47
+ topic: string().optional(),
48
+ title: string(),
49
+ body: string().optional(),
50
+ url: string().optional(),
51
+ createdAt: string(),
52
+ seenAt: string().optional(),
53
+ readAt: string().optional(),
54
+ archivedAt: string().optional(),
55
+ metadata: record(string(), unknown()).optional()
56
+ });
57
+ var listResponseSchema = object({
58
+ ok: boolean(),
59
+ error: string().optional(),
60
+ notifications: array(notificationSchema).optional(),
61
+ unreadCount: number().int().min(0).optional(),
62
+ unseenCount: number().int().min(0).optional()
63
+ });
64
+ var createResponseSchema = object({
65
+ ok: boolean(),
66
+ error: string().optional(),
67
+ id: string().optional()
68
+ });
69
+ object({
70
+ ok: boolean(),
71
+ error: string().optional()
72
+ });
73
+ object({
74
+ ok: boolean(),
75
+ error: string().optional()
76
+ });
77
+ object({
78
+ ok: boolean(),
79
+ error: string().optional()
80
+ });
81
+ var digestFrequencySchema = _enum([
82
+ "off",
83
+ "daily",
84
+ "weekly"
85
+ ]);
86
+ var topicPreferenceSchema = object({
87
+ topic: string(),
88
+ inApp: boolean(),
89
+ emailDigest: boolean(),
90
+ push: boolean()
91
+ });
92
+ var settingsSchema = object({
93
+ digestFrequency: digestFrequencySchema,
94
+ topicPreferences: array(topicPreferenceSchema),
95
+ lastDigestSentAt: string().optional()
96
+ });
97
+ var settingsResponseSchema = object({
98
+ ok: boolean(),
99
+ error: string().optional(),
100
+ settings: settingsSchema.optional()
101
+ });
102
+ var updateSettingsRequestSchema = object({
103
+ digestFrequency: digestFrequencySchema.optional(),
104
+ topicPreferences: array(topicPreferenceSchema).optional()
105
+ });
106
+ var updateSettingsResponseSchema = object({
107
+ ok: boolean(),
108
+ error: string().optional(),
109
+ settings: settingsSchema.optional()
110
+ });
111
+ var digestRunRequestSchema = object({ force: boolean().optional() });
112
+ object({
113
+ ok: boolean(),
114
+ error: string().optional(),
115
+ sent: boolean().optional(),
116
+ skippedReason: string().optional()
117
+ });
118
+ //#endregion
119
+ //#region src/notifications/api/notifications/handler.ts
120
+ var toIso = (value) => value instanceof Date ? value.toISOString() : void 0;
121
+ var buildDisabledTopics = (settings, key) => {
122
+ const raw = settings?.topicPreferences;
123
+ if (!Array.isArray(raw) || raw.length === 0) return [];
124
+ return raw.map((pref) => {
125
+ if (!pref || typeof pref !== "object") return null;
126
+ const topic = typeof pref.topic === "string" ? pref.topic.trim() : "";
127
+ if (!topic) return null;
128
+ return pref[key] === true ? null : topic;
129
+ }).filter((topic) => Boolean(topic));
130
+ };
131
+ var listNotifications = async (payload, ctx) => {
132
+ const session = getSessionUser(ctx);
133
+ if (!session) return {
134
+ ok: false,
135
+ error: "unauthorized"
136
+ };
137
+ const ability = buildAbilityFromSession({
138
+ tenantId: session.tenantId,
139
+ session: ctx.req.session
140
+ });
141
+ if (!ability.can("read", "RBNotification")) {
142
+ ctx.res.status(403);
143
+ return {
144
+ ok: false,
145
+ error: "forbidden"
146
+ };
147
+ }
148
+ const parsed = listRequestSchema.safeParse(payload);
149
+ if (!parsed.success) {
150
+ ctx.res.status(400);
151
+ return {
152
+ ok: false,
153
+ error: "invalid_payload"
154
+ };
155
+ }
156
+ const { userId } = session;
157
+ const includeArchived = parsed.data.includeArchived === true;
158
+ const unreadOnly = parsed.data.unreadOnly === true;
159
+ const limit = parsed.data.limit ?? 50;
160
+ const markSeen = parsed.data.markSeen === true;
161
+ const disabledTopics = buildDisabledTopics(await (await models.get("RBNotificationSettings", {
162
+ req: ctx.req,
163
+ ability
164
+ })).findOne({ userId }).lean(), "inApp");
165
+ const NotificationModel = await models.get("RBNotification", {
166
+ req: ctx.req,
167
+ ability
168
+ });
169
+ const queryFilters = [{ userId }, getAccessibleByQuery(ability, "read", "RBNotification")];
170
+ if (!includeArchived) queryFilters.push({ archivedAt: { $exists: false } });
171
+ if (unreadOnly) queryFilters.push({ readAt: { $exists: false } });
172
+ if (disabledTopics.length > 0) queryFilters.push({ topic: { $nin: disabledTopics } });
173
+ const query = { $and: queryFilters };
174
+ const notifications = await NotificationModel.find(query).sort({ createdAt: -1 }).limit(limit).lean();
175
+ const unseenQueryFilters = [
176
+ { userId },
177
+ { archivedAt: { $exists: false } },
178
+ { seenAt: { $exists: false } },
179
+ getAccessibleByQuery(ability, "read", "RBNotification")
180
+ ];
181
+ if (disabledTopics.length > 0) unseenQueryFilters.push({ topic: { $nin: disabledTopics } });
182
+ const unseenQuery = { $and: unseenQueryFilters };
183
+ const unreadQueryFilters = [
184
+ { userId },
185
+ { archivedAt: { $exists: false } },
186
+ { readAt: { $exists: false } },
187
+ getAccessibleByQuery(ability, "read", "RBNotification")
188
+ ];
189
+ if (disabledTopics.length > 0) unreadQueryFilters.push({ topic: { $nin: disabledTopics } });
190
+ const unreadQuery = { $and: unreadQueryFilters };
191
+ const [unreadCount, unseenCount] = await Promise.all([NotificationModel.countDocuments(unreadQuery), NotificationModel.countDocuments(unseenQuery)]);
192
+ const now = markSeen ? /* @__PURE__ */ new Date() : null;
193
+ if (now && unseenCount > 0) await NotificationModel.updateMany(unseenQuery, { $set: { seenAt: now } });
194
+ return listResponseSchema.parse({
195
+ ok: true,
196
+ notifications: notifications.map((n) => ({
197
+ id: String(n._id),
198
+ topic: typeof n.topic === "string" ? n.topic : void 0,
199
+ title: typeof n.title === "string" ? n.title : "",
200
+ body: typeof n.body === "string" ? n.body : void 0,
201
+ url: typeof n.url === "string" ? n.url : void 0,
202
+ createdAt: toIso(n.createdAt) ?? (/* @__PURE__ */ new Date()).toISOString(),
203
+ seenAt: toIso(n.seenAt) ?? (now && !n.archivedAt && !n.seenAt ? now.toISOString() : void 0),
204
+ readAt: toIso(n.readAt),
205
+ archivedAt: toIso(n.archivedAt),
206
+ metadata: typeof n.metadata === "object" && n.metadata !== null ? n.metadata : void 0
207
+ })),
208
+ unreadCount,
209
+ unseenCount: now ? 0 : unseenCount
210
+ });
211
+ };
212
+ var createNotificationForCurrentUser = async (payload, ctx) => {
213
+ const session = getSessionUser(ctx);
214
+ if (!session) return {
215
+ ok: false,
216
+ error: "unauthorized"
217
+ };
218
+ const ability = buildAbilityFromSession({
219
+ tenantId: session.tenantId,
220
+ session: ctx.req.session
221
+ });
222
+ if (!ability.can("create", "RBNotification")) {
223
+ ctx.res.status(403);
224
+ return {
225
+ ok: false,
226
+ error: "forbidden"
227
+ };
228
+ }
229
+ const parsed = createRequestSchema.safeParse(payload);
230
+ if (!parsed.success) {
231
+ ctx.res.status(400);
232
+ return {
233
+ ok: false,
234
+ error: "invalid_payload"
235
+ };
236
+ }
237
+ const created = await createNotification(ctx, {
238
+ userId: session.userId,
239
+ topic: parsed.data.topic,
240
+ title: parsed.data.title,
241
+ body: parsed.data.body,
242
+ url: parsed.data.url,
243
+ metadata: parsed.data.metadata
244
+ }, ability);
245
+ return createResponseSchema.parse({
246
+ ok: true,
247
+ id: created.id
248
+ });
249
+ };
250
+ var markRead = async (_payload, ctx) => {
251
+ const session = getSessionUser(ctx);
252
+ if (!session) return {
253
+ ok: false,
254
+ error: "unauthorized"
255
+ };
256
+ const ability = buildAbilityFromSession({
257
+ tenantId: session.tenantId,
258
+ session: ctx.req.session
259
+ });
260
+ if (!ability.can("update", "RBNotification")) {
261
+ ctx.res.status(403);
262
+ return {
263
+ ok: false,
264
+ error: "forbidden"
265
+ };
266
+ }
267
+ const notificationId = typeof ctx.req.params.notificationId === "string" ? ctx.req.params.notificationId.trim() : "";
268
+ if (!notificationId) {
269
+ ctx.res.status(400);
270
+ return {
271
+ ok: false,
272
+ error: "missing_notification_id"
273
+ };
274
+ }
275
+ const NotificationModel = await models.get("RBNotification", {
276
+ req: ctx.req,
277
+ ability
278
+ });
279
+ const now = /* @__PURE__ */ new Date();
280
+ try {
281
+ await NotificationModel.updateOne({ $and: [
282
+ { _id: notificationId },
283
+ { archivedAt: { $exists: false } },
284
+ getAccessibleByQuery(ability, "update", "RBNotification")
285
+ ] }, { $set: {
286
+ readAt: now,
287
+ seenAt: now
288
+ } });
289
+ } catch {
290
+ ctx.res.status(400);
291
+ return {
292
+ ok: false,
293
+ error: "invalid_notification_id"
294
+ };
295
+ }
296
+ return { ok: true };
297
+ };
298
+ var markAllRead = async (_payload, ctx) => {
299
+ const session = getSessionUser(ctx);
300
+ if (!session) return {
301
+ ok: false,
302
+ error: "unauthorized"
303
+ };
304
+ const ability = buildAbilityFromSession({
305
+ tenantId: session.tenantId,
306
+ session: ctx.req.session
307
+ });
308
+ if (!ability.can("update", "RBNotification")) {
309
+ ctx.res.status(403);
310
+ return {
311
+ ok: false,
312
+ error: "forbidden"
313
+ };
314
+ }
315
+ const disabledTopics = buildDisabledTopics(await (await models.get("RBNotificationSettings", {
316
+ req: ctx.req,
317
+ ability
318
+ })).findOne({ userId: session.userId }).lean(), "inApp");
319
+ const NotificationModel = await models.get("RBNotification", {
320
+ req: ctx.req,
321
+ ability
322
+ });
323
+ const queryFilters = [
324
+ { userId: session.userId },
325
+ { archivedAt: { $exists: false } },
326
+ { readAt: { $exists: false } },
327
+ getAccessibleByQuery(ability, "update", "RBNotification")
328
+ ];
329
+ if (disabledTopics.length > 0) queryFilters.push({ topic: { $nin: disabledTopics } });
330
+ const query = { $and: queryFilters };
331
+ const now = /* @__PURE__ */ new Date();
332
+ await NotificationModel.updateMany(query, { $set: {
333
+ readAt: now,
334
+ seenAt: now
335
+ } });
336
+ return { ok: true };
337
+ };
338
+ var archiveNotification = async (_payload, ctx) => {
339
+ const session = getSessionUser(ctx);
340
+ if (!session) return {
341
+ ok: false,
342
+ error: "unauthorized"
343
+ };
344
+ const ability = buildAbilityFromSession({
345
+ tenantId: session.tenantId,
346
+ session: ctx.req.session
347
+ });
348
+ if (!ability.can("update", "RBNotification")) {
349
+ ctx.res.status(403);
350
+ return {
351
+ ok: false,
352
+ error: "forbidden"
353
+ };
354
+ }
355
+ const notificationId = typeof ctx.req.params.notificationId === "string" ? ctx.req.params.notificationId.trim() : "";
356
+ if (!notificationId) {
357
+ ctx.res.status(400);
358
+ return {
359
+ ok: false,
360
+ error: "missing_notification_id"
361
+ };
362
+ }
363
+ const NotificationModel = await models.get("RBNotification", {
364
+ req: ctx.req,
365
+ ability
366
+ });
367
+ try {
368
+ await NotificationModel.updateOne({ $and: [
369
+ { _id: notificationId },
370
+ { archivedAt: { $exists: false } },
371
+ getAccessibleByQuery(ability, "update", "RBNotification")
372
+ ] }, { $set: { archivedAt: /* @__PURE__ */ new Date() } });
373
+ } catch {
374
+ ctx.res.status(400);
375
+ return {
376
+ ok: false,
377
+ error: "invalid_notification_id"
378
+ };
379
+ }
380
+ return { ok: true };
381
+ };
382
+ var getSettings = async (_payload, ctx) => {
383
+ const session = getSessionUser(ctx);
384
+ if (!session) return {
385
+ ok: false,
386
+ error: "unauthorized"
387
+ };
388
+ const ability = buildAbilityFromSession({
389
+ tenantId: session.tenantId,
390
+ session: ctx.req.session
391
+ });
392
+ if (!ability.can("read", "RBNotificationSettings")) {
393
+ ctx.res.status(403);
394
+ return {
395
+ ok: false,
396
+ error: "forbidden"
397
+ };
398
+ }
399
+ const settings = await (await models.get("RBNotificationSettings", {
400
+ req: ctx.req,
401
+ ability
402
+ })).findOne({ $and: [{ userId: session.userId }, getAccessibleByQuery(ability, "read", "RBNotificationSettings")] }).lean();
403
+ const digestFrequencyRaw = typeof settings?.digestFrequency === "string" ? settings.digestFrequency : "weekly";
404
+ const digestFrequency = digestFrequencyRaw === "off" || digestFrequencyRaw === "daily" || digestFrequencyRaw === "weekly" ? digestFrequencyRaw : "weekly";
405
+ const topicPreferences = Array.isArray(settings?.topicPreferences) ? settings.topicPreferences.map((pref) => ({
406
+ topic: typeof pref.topic === "string" ? pref.topic : "",
407
+ inApp: pref.inApp === true,
408
+ emailDigest: pref.emailDigest === true,
409
+ push: pref.push === true
410
+ })).filter((pref) => pref.topic.length > 0) : [];
411
+ return settingsResponseSchema.parse({
412
+ ok: true,
413
+ settings: {
414
+ digestFrequency,
415
+ topicPreferences,
416
+ lastDigestSentAt: toIso(settings?.lastDigestSentAt)
417
+ }
418
+ });
419
+ };
420
+ var updateSettings = async (payload, ctx) => {
421
+ const session = getSessionUser(ctx);
422
+ if (!session) return {
423
+ ok: false,
424
+ error: "unauthorized"
425
+ };
426
+ const ability = buildAbilityFromSession({
427
+ tenantId: session.tenantId,
428
+ session: ctx.req.session
429
+ });
430
+ if (!ability.can("update", "RBNotificationSettings")) {
431
+ ctx.res.status(403);
432
+ return {
433
+ ok: false,
434
+ error: "forbidden"
435
+ };
436
+ }
437
+ const parsed = updateSettingsRequestSchema.safeParse(payload);
438
+ if (!parsed.success) {
439
+ ctx.res.status(400);
440
+ return {
441
+ ok: false,
442
+ error: "invalid_payload"
443
+ };
444
+ }
445
+ const SettingsModel = await models.get("RBNotificationSettings", {
446
+ req: ctx.req,
447
+ ability
448
+ });
449
+ const nextValues = {};
450
+ if (parsed.data.digestFrequency) nextValues.digestFrequency = parsed.data.digestFrequency;
451
+ if (parsed.data.topicPreferences) {
452
+ const seen = /* @__PURE__ */ new Set();
453
+ nextValues.topicPreferences = parsed.data.topicPreferences.map((pref) => ({
454
+ topic: pref.topic.trim(),
455
+ inApp: pref.inApp,
456
+ emailDigest: pref.emailDigest,
457
+ push: pref.push
458
+ })).filter((pref) => pref.topic.length > 0).filter((pref) => {
459
+ if (seen.has(pref.topic)) return false;
460
+ seen.add(pref.topic);
461
+ return true;
462
+ });
463
+ }
464
+ const ops = { $setOnInsert: { userId: session.userId } };
465
+ if (Object.keys(nextValues).length > 0) ops.$set = nextValues;
466
+ const settings = await SettingsModel.findOneAndUpdate({ $and: [{ userId: session.userId }, getAccessibleByQuery(ability, "update", "RBNotificationSettings")] }, ops, {
467
+ upsert: true,
468
+ returnDocument: "after",
469
+ setDefaultsOnInsert: true
470
+ }).lean();
471
+ const digestFrequencyRaw = typeof settings?.digestFrequency === "string" ? settings.digestFrequency : "weekly";
472
+ const digestFrequency = digestFrequencyRaw === "off" || digestFrequencyRaw === "daily" || digestFrequencyRaw === "weekly" ? digestFrequencyRaw : "weekly";
473
+ const topicPreferences = Array.isArray(settings?.topicPreferences) ? settings.topicPreferences.map((pref) => ({
474
+ topic: typeof pref.topic === "string" ? pref.topic : "",
475
+ inApp: pref.inApp === true,
476
+ emailDigest: pref.emailDigest === true,
477
+ push: pref.push === true
478
+ })).filter((pref) => pref.topic.length > 0) : [];
479
+ return updateSettingsResponseSchema.parse({
480
+ ok: true,
481
+ settings: {
482
+ digestFrequency,
483
+ topicPreferences,
484
+ lastDigestSentAt: toIso(settings?.lastDigestSentAt)
485
+ }
486
+ });
487
+ };
488
+ var runDigest = async (payload, ctx) => {
489
+ const session = getSessionUser(ctx);
490
+ if (!session) return {
491
+ ok: false,
492
+ error: "unauthorized"
493
+ };
494
+ const ability = buildAbilityFromSession({
495
+ tenantId: session.tenantId,
496
+ session: ctx.req.session
497
+ });
498
+ if (!ability.can("read", "RBNotification")) {
499
+ ctx.res.status(403);
500
+ return {
501
+ ok: false,
502
+ error: "forbidden"
503
+ };
504
+ }
505
+ const parsed = digestRunRequestSchema.safeParse(payload);
506
+ if (!parsed.success) {
507
+ ctx.res.status(400);
508
+ return {
509
+ ok: false,
510
+ error: "invalid_payload"
511
+ };
512
+ }
513
+ const result = await sendNotificationsDigestForUser(ctx, {
514
+ userId: session.userId,
515
+ ability,
516
+ force: parsed.data.force === true
517
+ });
518
+ if (!result.ok) {
519
+ ctx.res.status(500);
520
+ return {
521
+ ok: false,
522
+ error: result.error
523
+ };
524
+ }
525
+ return {
526
+ ok: true,
527
+ sent: result.sent,
528
+ ...result.skippedReason ? { skippedReason: result.skippedReason } : {}
529
+ };
530
+ };
531
+ var handler_default = (api) => {
532
+ api.post(ListRoute, listNotifications);
533
+ api.post(CreateRoute, createNotificationForCurrentUser);
534
+ api.post(MarkReadRoute, markRead);
535
+ api.post(MarkAllReadRoute, markAllRead);
536
+ api.post(ArchiveRoute, archiveNotification);
537
+ api.get(SettingsRoute, getSettings);
538
+ api.put(SettingsRoute, updateSettings);
539
+ api.post(DigestRunRoute, runDigest);
540
+ };
541
+ //#endregion
542
+ export { handler_default as default };
543
+
544
+ //# sourceMappingURL=handler-BLwgdQv-.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler-BLwgdQv-.js","names":["Ctx","NotificationSessionUser","id","currentTenantId","getSessionUser","ctx","rawSessionUser","req","session","user","userId","trim","tenantId","res","status","z","ListRoute","CreateRoute","MarkReadRoute","ArchiveRoute","MarkAllReadRoute","SettingsRoute","DigestRunRoute","listRequestSchema","object","includeArchived","boolean","optional","unreadOnly","limit","number","int","min","max","markSeen","ListRequestPayload","infer","createRequestSchema","topic","string","trim","title","body","url","metadata","record","unknown","CreateRequestPayload","notificationSchema","id","createdAt","seenAt","readAt","archivedAt","NotificationPayload","listResponseSchema","ok","error","notifications","array","unreadCount","unseenCount","ListResponsePayload","createResponseSchema","CreateResponsePayload","markReadResponseSchema","MarkReadResponsePayload","archiveResponseSchema","ArchiveResponsePayload","markAllReadResponseSchema","MarkAllReadResponsePayload","digestFrequencySchema","enum","DigestFrequency","topicPreferenceSchema","inApp","emailDigest","push","TopicPreferencePayload","settingsSchema","digestFrequency","topicPreferences","lastDigestSentAt","SettingsPayload","settingsResponseSchema","settings","SettingsResponsePayload","updateSettingsRequestSchema","UpdateSettingsRequestPayload","updateSettingsResponseSchema","UpdateSettingsResponsePayload","digestRunRequestSchema","force","DigestRunRequestPayload","digestRunResponseSchema","sent","skippedReason","DigestRunResponsePayload","Api","ApiHandler","models","IRBNotification","IRBNotificationSettings","buildAbilityFromSession","getAccessibleByQuery","createNotification","sendNotificationsDigestForUser","getSessionUser","Notifications","NotificationDoc","_id","SettingsDoc","toIso","value","Date","toISOString","undefined","buildDisabledTopics","settings","key","raw","topicPreferences","Array","isArray","length","map","pref","topic","trim","enabled","Record","filter","Boolean","listNotifications","ListRequestPayload","ListResponsePayload","payload","ctx","session","ok","error","ability","tenantId","req","can","res","status","parsed","listRequestSchema","safeParse","success","userId","includeArchived","data","unreadOnly","limit","markSeen","SettingsModel","get","findOne","lean","disabledTopics","NotificationModel","queryFilters","push","archivedAt","$exists","readAt","$nin","query","$and","notifications","find","sort","createdAt","unseenQueryFilters","seenAt","unseenQuery","unreadQueryFilters","unreadQuery","unreadCount","unseenCount","Promise","all","countDocuments","now","updateMany","$set","listResponseSchema","parse","n","id","String","title","body","url","metadata","createNotificationForCurrentUser","CreateRequestPayload","CreateResponsePayload","createRequestSchema","created","createResponseSchema","markRead","MarkReadResponsePayload","_payload","notificationId","params","updateOne","markAllRead","MarkAllReadResponsePayload","archiveNotification","ArchiveResponsePayload","getSettings","SettingsResponsePayload","digestFrequencyRaw","digestFrequency","DigestFrequency","inApp","emailDigest","settingsResponseSchema","lastDigestSentAt","updateSettings","UpdateSettingsRequestPayload","UpdateSettingsResponsePayload","updateSettingsRequestSchema","nextValues","seen","Set","next","has","add","ops","$setOnInsert","Object","keys","findOneAndUpdate","upsert","returnDocument","setDefaultsOnInsert","updateSettingsResponseSchema","runDigest","DigestRunRequestPayload","DigestRunResponsePayload","digestRunRequestSchema","result","force","sent","skippedReason","api","post","ListRoute","CreateRoute","MarkReadRoute","MarkAllReadRoute","ArchiveRoute","SettingsRoute","put","DigestRunRoute"],"sources":["../src/notifications/api/notifications/shared.ts","../src/notifications/api/notifications/index.ts","../src/notifications/api/notifications/handler.ts"],"sourcesContent":["import type { Ctx } from \"@rpcbase/api\"\n\n\ntype NotificationSessionUser = {\n id?: unknown\n currentTenantId?: unknown\n}\n\nexport const getSessionUser = (ctx: Ctx) => {\n const rawSessionUser = (ctx.req.session as { user?: NotificationSessionUser } | null | undefined)?.user\n const userId = typeof rawSessionUser?.id === \"string\" ? rawSessionUser.id.trim() : \"\"\n const tenantId = typeof rawSessionUser?.currentTenantId === \"string\" ? rawSessionUser.currentTenantId.trim() : \"\"\n\n if (!userId) {\n ctx.res.status(401)\n return null\n }\n\n if (!tenantId) {\n ctx.res.status(400)\n return null\n }\n\n return { userId, tenantId }\n}\n\n","import { z } from \"zod\"\n\n\nexport const ListRoute = \"/api/rb/notifications\"\nexport const CreateRoute = \"/api/rb/notifications/create\"\nexport const MarkReadRoute = \"/api/rb/notifications/:notificationId/read\"\nexport const ArchiveRoute = \"/api/rb/notifications/:notificationId/archive\"\nexport const MarkAllReadRoute = \"/api/rb/notifications/mark-all-read\"\nexport const SettingsRoute = \"/api/rb/notifications/settings\"\nexport const DigestRunRoute = \"/api/rb/notifications/digest/run\"\n\nexport const listRequestSchema = z.object({\n includeArchived: z.boolean().optional(),\n unreadOnly: z.boolean().optional(),\n limit: z.number().int().min(1).max(200).optional(),\n markSeen: z.boolean().optional(),\n})\n\nexport type ListRequestPayload = z.infer<typeof listRequestSchema>\n\nexport const createRequestSchema = z.object({\n topic: z.string().trim().min(1).optional(),\n title: z.string().trim().min(1),\n body: z.string().trim().optional(),\n url: z.string().trim().optional(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n})\n\nexport type CreateRequestPayload = z.infer<typeof createRequestSchema>\n\nexport const notificationSchema = z.object({\n id: z.string(),\n topic: z.string().optional(),\n title: z.string(),\n body: z.string().optional(),\n url: z.string().optional(),\n createdAt: z.string(),\n seenAt: z.string().optional(),\n readAt: z.string().optional(),\n archivedAt: z.string().optional(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n})\n\nexport type NotificationPayload = z.infer<typeof notificationSchema>\n\nexport const listResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n notifications: z.array(notificationSchema).optional(),\n unreadCount: z.number().int().min(0).optional(),\n unseenCount: z.number().int().min(0).optional(),\n})\n\nexport type ListResponsePayload = z.infer<typeof listResponseSchema>\n\nexport const createResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n id: z.string().optional(),\n})\n\nexport type CreateResponsePayload = z.infer<typeof createResponseSchema>\n\nexport const markReadResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n})\n\nexport type MarkReadResponsePayload = z.infer<typeof markReadResponseSchema>\n\nexport const archiveResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n})\n\nexport type ArchiveResponsePayload = z.infer<typeof archiveResponseSchema>\n\nexport const markAllReadResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n})\n\nexport type MarkAllReadResponsePayload = z.infer<typeof markAllReadResponseSchema>\n\nexport const digestFrequencySchema = z.enum([\"off\", \"daily\", \"weekly\"])\n\nexport type DigestFrequency = z.infer<typeof digestFrequencySchema>\n\nexport const topicPreferenceSchema = z.object({\n topic: z.string(),\n inApp: z.boolean(),\n emailDigest: z.boolean(),\n push: z.boolean(),\n})\n\nexport type TopicPreferencePayload = z.infer<typeof topicPreferenceSchema>\n\nexport const settingsSchema = z.object({\n digestFrequency: digestFrequencySchema,\n topicPreferences: z.array(topicPreferenceSchema),\n lastDigestSentAt: z.string().optional(),\n})\n\nexport type SettingsPayload = z.infer<typeof settingsSchema>\n\nexport const settingsResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n settings: settingsSchema.optional(),\n})\n\nexport type SettingsResponsePayload = z.infer<typeof settingsResponseSchema>\n\nexport const updateSettingsRequestSchema = z.object({\n digestFrequency: digestFrequencySchema.optional(),\n topicPreferences: z.array(topicPreferenceSchema).optional(),\n})\n\nexport type UpdateSettingsRequestPayload = z.infer<typeof updateSettingsRequestSchema>\n\nexport const updateSettingsResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n settings: settingsSchema.optional(),\n})\n\nexport type UpdateSettingsResponsePayload = z.infer<typeof updateSettingsResponseSchema>\n\nexport const digestRunRequestSchema = z.object({\n force: z.boolean().optional(),\n})\n\nexport type DigestRunRequestPayload = z.infer<typeof digestRunRequestSchema>\n\nexport const digestRunResponseSchema = z.object({\n ok: z.boolean(),\n error: z.string().optional(),\n sent: z.boolean().optional(),\n skippedReason: z.string().optional(),\n})\n\nexport type DigestRunResponsePayload = z.infer<typeof digestRunResponseSchema>\n","import { Api, type ApiHandler } from \"@rpcbase/api\"\nimport { models, type IRBNotification, type IRBNotificationSettings } from \"@rpcbase/db\"\nimport { buildAbilityFromSession, getAccessibleByQuery } from \"@rpcbase/db/acl\"\n\nimport { createNotification } from \"../../createNotification\"\nimport { sendNotificationsDigestForUser } from \"../../digest\"\nimport { getSessionUser } from \"./shared\"\n\nimport * as Notifications from \"./index\"\n\n\ntype NotificationDoc = IRBNotification & { _id: unknown }\ntype SettingsDoc = IRBNotificationSettings & { _id: unknown }\n\nconst toIso = (value: unknown): string | undefined => (value instanceof Date ? value.toISOString() : undefined)\n\nconst buildDisabledTopics = (\n settings: SettingsDoc | null,\n key: \"inApp\" | \"emailDigest\",\n): string[] => {\n const raw = settings?.topicPreferences\n if (!Array.isArray(raw) || raw.length === 0) return []\n\n return raw\n .map((pref) => {\n if (!pref || typeof pref !== \"object\") return null\n const topic = typeof (pref as { topic?: unknown }).topic === \"string\" ? (pref as { topic: string }).topic.trim() : \"\"\n if (!topic) return null\n const enabled = (pref as Record<string, unknown>)[key] === true\n return enabled ? null : topic\n })\n .filter((topic): topic is string => Boolean(topic))\n}\n\nconst listNotifications: ApiHandler<Notifications.ListRequestPayload, Notifications.ListResponsePayload> = async(\n payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"read\", \"RBNotification\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const parsed = Notifications.listRequestSchema.safeParse(payload)\n if (!parsed.success) {\n ctx.res.status(400)\n return { ok: false, error: \"invalid_payload\" }\n }\n\n const { userId } = session\n const includeArchived = parsed.data.includeArchived === true\n const unreadOnly = parsed.data.unreadOnly === true\n const limit = parsed.data.limit ?? 50\n const markSeen = parsed.data.markSeen === true\n\n const SettingsModel = await models.get(\"RBNotificationSettings\", { req: ctx.req, ability })\n const settings = (await SettingsModel.findOne({ userId }).lean()) as SettingsDoc | null\n const disabledTopics = buildDisabledTopics(settings, \"inApp\")\n\n const NotificationModel = await models.get(\"RBNotification\", { req: ctx.req, ability })\n\n const queryFilters: Record<string, unknown>[] = [\n { userId },\n getAccessibleByQuery(ability, \"read\", \"RBNotification\"),\n ]\n if (!includeArchived) queryFilters.push({ archivedAt: { $exists: false } })\n if (unreadOnly) queryFilters.push({ readAt: { $exists: false } })\n if (disabledTopics.length > 0) queryFilters.push({ topic: { $nin: disabledTopics } })\n const query: Record<string, unknown> = { $and: queryFilters }\n\n const notifications = (await NotificationModel.find(query)\n .sort({ createdAt: -1 })\n .limit(limit)\n .lean()) as NotificationDoc[]\n\n const unseenQueryFilters: Record<string, unknown>[] = [\n { userId },\n { archivedAt: { $exists: false } },\n { seenAt: { $exists: false } },\n getAccessibleByQuery(ability, \"read\", \"RBNotification\"),\n ]\n if (disabledTopics.length > 0) unseenQueryFilters.push({ topic: { $nin: disabledTopics } })\n const unseenQuery: Record<string, unknown> = { $and: unseenQueryFilters }\n\n const unreadQueryFilters: Record<string, unknown>[] = [\n { userId },\n { archivedAt: { $exists: false } },\n { readAt: { $exists: false } },\n getAccessibleByQuery(ability, \"read\", \"RBNotification\"),\n ]\n if (disabledTopics.length > 0) unreadQueryFilters.push({ topic: { $nin: disabledTopics } })\n const unreadQuery: Record<string, unknown> = { $and: unreadQueryFilters }\n\n const [unreadCount, unseenCount] = await Promise.all([\n NotificationModel.countDocuments(unreadQuery),\n NotificationModel.countDocuments(unseenQuery),\n ])\n\n const now = markSeen ? new Date() : null\n if (now && unseenCount > 0) {\n await NotificationModel.updateMany(unseenQuery, { $set: { seenAt: now } })\n }\n\n return Notifications.listResponseSchema.parse({\n ok: true,\n notifications: notifications.map((n) => ({\n id: String(n._id),\n topic: typeof n.topic === \"string\" ? n.topic : undefined,\n title: typeof n.title === \"string\" ? n.title : \"\",\n body: typeof n.body === \"string\" ? n.body : undefined,\n url: typeof n.url === \"string\" ? n.url : undefined,\n createdAt: toIso(n.createdAt) ?? new Date().toISOString(),\n seenAt: toIso(n.seenAt) ?? (now && !n.archivedAt && !n.seenAt ? now.toISOString() : undefined),\n readAt: toIso(n.readAt),\n archivedAt: toIso(n.archivedAt),\n metadata: typeof n.metadata === \"object\" && n.metadata !== null ? (n.metadata as Record<string, unknown>) : undefined,\n })),\n unreadCount,\n unseenCount: now ? 0 : unseenCount,\n })\n}\n\nconst createNotificationForCurrentUser: ApiHandler<Notifications.CreateRequestPayload, Notifications.CreateResponsePayload> = async(\n payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"create\", \"RBNotification\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const parsed = Notifications.createRequestSchema.safeParse(payload)\n if (!parsed.success) {\n ctx.res.status(400)\n return { ok: false, error: \"invalid_payload\" }\n }\n\n const created = await createNotification(ctx, {\n userId: session.userId,\n topic: parsed.data.topic,\n title: parsed.data.title,\n body: parsed.data.body,\n url: parsed.data.url,\n metadata: parsed.data.metadata,\n }, ability)\n\n return Notifications.createResponseSchema.parse({ ok: true, id: created.id })\n}\n\nconst markRead: ApiHandler<unknown, Notifications.MarkReadResponsePayload> = async(\n _payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"update\", \"RBNotification\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const notificationId = typeof ctx.req.params.notificationId === \"string\" ? ctx.req.params.notificationId.trim() : \"\"\n if (!notificationId) {\n ctx.res.status(400)\n return { ok: false, error: \"missing_notification_id\" }\n }\n\n const NotificationModel = await models.get(\"RBNotification\", { req: ctx.req, ability })\n const now = new Date()\n\n try {\n await NotificationModel.updateOne(\n { $and: [{ _id: notificationId }, { archivedAt: { $exists: false } }, getAccessibleByQuery(ability, \"update\", \"RBNotification\")] },\n { $set: { readAt: now, seenAt: now } },\n )\n } catch {\n ctx.res.status(400)\n return { ok: false, error: \"invalid_notification_id\" }\n }\n\n return { ok: true }\n}\n\nconst markAllRead: ApiHandler<unknown, Notifications.MarkAllReadResponsePayload> = async(\n _payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"update\", \"RBNotification\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const SettingsModel = await models.get(\"RBNotificationSettings\", { req: ctx.req, ability })\n const settings = (await SettingsModel.findOne({ userId: session.userId }).lean()) as SettingsDoc | null\n const disabledTopics = buildDisabledTopics(settings, \"inApp\")\n\n const NotificationModel = await models.get(\"RBNotification\", { req: ctx.req, ability })\n\n const queryFilters: Record<string, unknown>[] = [\n { userId: session.userId },\n { archivedAt: { $exists: false } },\n { readAt: { $exists: false } },\n getAccessibleByQuery(ability, \"update\", \"RBNotification\"),\n ]\n if (disabledTopics.length > 0) queryFilters.push({ topic: { $nin: disabledTopics } })\n const query: Record<string, unknown> = { $and: queryFilters }\n\n const now = new Date()\n await NotificationModel.updateMany(query, { $set: { readAt: now, seenAt: now } })\n\n return { ok: true }\n}\n\nconst archiveNotification: ApiHandler<unknown, Notifications.ArchiveResponsePayload> = async(\n _payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"update\", \"RBNotification\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const notificationId = typeof ctx.req.params.notificationId === \"string\" ? ctx.req.params.notificationId.trim() : \"\"\n if (!notificationId) {\n ctx.res.status(400)\n return { ok: false, error: \"missing_notification_id\" }\n }\n\n const NotificationModel = await models.get(\"RBNotification\", { req: ctx.req, ability })\n\n try {\n await NotificationModel.updateOne(\n { $and: [{ _id: notificationId }, { archivedAt: { $exists: false } }, getAccessibleByQuery(ability, \"update\", \"RBNotification\")] },\n { $set: { archivedAt: new Date() } },\n )\n } catch {\n ctx.res.status(400)\n return { ok: false, error: \"invalid_notification_id\" }\n }\n\n return { ok: true }\n}\n\nconst getSettings: ApiHandler<unknown, Notifications.SettingsResponsePayload> = async(\n _payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"read\", \"RBNotificationSettings\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const SettingsModel = await models.get(\"RBNotificationSettings\", { req: ctx.req, ability })\n const settings = (await SettingsModel.findOne(\n { $and: [{ userId: session.userId }, getAccessibleByQuery(ability, \"read\", \"RBNotificationSettings\")] },\n ).lean()) as SettingsDoc | null\n\n const digestFrequencyRaw = typeof settings?.digestFrequency === \"string\" ? settings.digestFrequency : \"weekly\"\n const digestFrequency: Notifications.DigestFrequency =\n digestFrequencyRaw === \"off\" || digestFrequencyRaw === \"daily\" || digestFrequencyRaw === \"weekly\"\n ? digestFrequencyRaw\n : \"weekly\"\n\n const topicPreferences = Array.isArray(settings?.topicPreferences)\n ? settings!.topicPreferences.map((pref) => ({\n topic: typeof pref.topic === \"string\" ? pref.topic : \"\",\n inApp: pref.inApp === true,\n emailDigest: pref.emailDigest === true,\n push: pref.push === true,\n })).filter((pref) => pref.topic.length > 0)\n : []\n\n return Notifications.settingsResponseSchema.parse({\n ok: true,\n settings: {\n digestFrequency,\n topicPreferences,\n lastDigestSentAt: toIso(settings?.lastDigestSentAt),\n },\n })\n}\n\nconst updateSettings: ApiHandler<Notifications.UpdateSettingsRequestPayload, Notifications.UpdateSettingsResponsePayload> = async(\n payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"update\", \"RBNotificationSettings\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const parsed = Notifications.updateSettingsRequestSchema.safeParse(payload)\n if (!parsed.success) {\n ctx.res.status(400)\n return { ok: false, error: \"invalid_payload\" }\n }\n\n const SettingsModel = await models.get(\"RBNotificationSettings\", { req: ctx.req, ability })\n const nextValues: Record<string, unknown> = {}\n\n if (parsed.data.digestFrequency) {\n nextValues.digestFrequency = parsed.data.digestFrequency\n }\n\n if (parsed.data.topicPreferences) {\n const seen = new Set<string>()\n const next = parsed.data.topicPreferences\n .map((pref) => ({\n topic: pref.topic.trim(),\n inApp: pref.inApp,\n emailDigest: pref.emailDigest,\n push: pref.push,\n }))\n .filter((pref) => pref.topic.length > 0)\n .filter((pref) => {\n if (seen.has(pref.topic)) return false\n seen.add(pref.topic)\n return true\n })\n\n nextValues.topicPreferences = next\n }\n\n const ops: Record<string, unknown> = {\n $setOnInsert: { userId: session.userId },\n }\n\n if (Object.keys(nextValues).length > 0) {\n ops.$set = nextValues\n }\n\n const settings = (await SettingsModel.findOneAndUpdate(\n { $and: [{ userId: session.userId }, getAccessibleByQuery(ability, \"update\", \"RBNotificationSettings\")] },\n ops,\n { upsert: true, returnDocument: \"after\", setDefaultsOnInsert: true },\n ).lean()) as SettingsDoc | null\n\n const digestFrequencyRaw = typeof settings?.digestFrequency === \"string\" ? settings.digestFrequency : \"weekly\"\n const digestFrequency: Notifications.DigestFrequency =\n digestFrequencyRaw === \"off\" || digestFrequencyRaw === \"daily\" || digestFrequencyRaw === \"weekly\"\n ? digestFrequencyRaw\n : \"weekly\"\n\n const topicPreferences = Array.isArray(settings?.topicPreferences)\n ? settings!.topicPreferences.map((pref) => ({\n topic: typeof pref.topic === \"string\" ? pref.topic : \"\",\n inApp: pref.inApp === true,\n emailDigest: pref.emailDigest === true,\n push: pref.push === true,\n })).filter((pref) => pref.topic.length > 0)\n : []\n\n return Notifications.updateSettingsResponseSchema.parse({\n ok: true,\n settings: {\n digestFrequency,\n topicPreferences,\n lastDigestSentAt: toIso(settings?.lastDigestSentAt),\n },\n })\n}\n\nconst runDigest: ApiHandler<Notifications.DigestRunRequestPayload, Notifications.DigestRunResponsePayload> = async(\n payload,\n ctx,\n) => {\n const session = getSessionUser(ctx)\n if (!session) {\n return { ok: false, error: \"unauthorized\" }\n }\n\n const ability = buildAbilityFromSession({ tenantId: session.tenantId, session: ctx.req.session })\n if (!ability.can(\"read\", \"RBNotification\")) {\n ctx.res.status(403)\n return { ok: false, error: \"forbidden\" }\n }\n\n const parsed = Notifications.digestRunRequestSchema.safeParse(payload)\n if (!parsed.success) {\n ctx.res.status(400)\n return { ok: false, error: \"invalid_payload\" }\n }\n\n const result = await sendNotificationsDigestForUser(ctx, {\n userId: session.userId,\n ability,\n force: parsed.data.force === true,\n })\n\n if (!result.ok) {\n ctx.res.status(500)\n return { ok: false, error: result.error }\n }\n\n return {\n ok: true,\n sent: result.sent,\n ...(result.skippedReason ? { skippedReason: result.skippedReason } : {}),\n }\n}\n\nexport default (api: Api) => {\n api.post(Notifications.ListRoute, listNotifications)\n api.post(Notifications.CreateRoute, createNotificationForCurrentUser)\n api.post(Notifications.MarkReadRoute, markRead)\n api.post(Notifications.MarkAllReadRoute, markAllRead)\n api.post(Notifications.ArchiveRoute, archiveNotification)\n api.get(Notifications.SettingsRoute, getSettings)\n api.put(Notifications.SettingsRoute, updateSettings)\n api.post(Notifications.DigestRunRoute, runDigest)\n}\n"],"mapp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