@terreno/api 57.3.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.
- package/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
- package/README.md +6 -0
- package/dist/adminTypes.d.ts +7 -0
- package/dist/adminTypes.js +6 -1
- package/dist/api.d.ts +10 -0
- package/dist/api.js +99 -36
- package/dist/audit/auditApp.d.ts +30 -0
- package/dist/audit/auditApp.js +155 -0
- package/dist/audit/auditEventModel.d.ts +29 -0
- package/dist/audit/auditEventModel.js +102 -0
- package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
- package/dist/audit/cloudTasksEnqueue.js +162 -0
- package/dist/audit/diff.d.ts +10 -0
- package/dist/audit/diff.js +230 -0
- package/dist/audit/processQueue.d.ts +6 -0
- package/dist/audit/processQueue.js +101 -0
- package/dist/audit/rbacSink.d.ts +2 -0
- package/dist/audit/rbacSink.js +82 -0
- package/dist/audit/record.d.ts +56 -0
- package/dist/audit/record.js +373 -0
- package/dist/betterAuth.d.ts +6 -0
- package/dist/betterAuthSetup.js +1 -1
- package/dist/collectionRegistry.d.ts +3 -1
- package/dist/collectionRegistry.js +25 -0
- package/dist/index.d.ts +26 -1
- package/dist/index.js +48 -2
- package/dist/mcp/query.d.ts +0 -13
- package/dist/mcp/query.js +8 -8
- package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +49 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +49 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +45 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +45 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +45 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +49 -0
- package/dist/migrations/generate.d.ts +17 -0
- package/dist/migrations/generate.js +277 -0
- package/dist/migrations/load.d.ts +20 -0
- package/dist/migrations/load.js +236 -0
- package/dist/migrations/lock.d.ts +13 -0
- package/dist/migrations/lock.js +145 -0
- package/dist/migrations/runner.d.ts +42 -0
- package/dist/migrations/runner.js +503 -0
- package/dist/migrations/schemaCatalog.d.ts +44 -0
- package/dist/migrations/schemaCatalog.js +326 -0
- package/dist/migrations/types.d.ts +87 -0
- package/dist/migrations/types.js +7 -0
- package/dist/models/notification.d.ts +2 -0
- package/dist/models/notification.js +62 -0
- package/dist/models/notificationPreference.d.ts +2 -0
- package/dist/models/notificationPreference.js +49 -0
- package/dist/notifications/notificationService.d.ts +40 -0
- package/dist/notifications/notificationService.js +323 -0
- package/dist/notifications/notificationsApp.d.ts +18 -0
- package/dist/notifications/notificationsApp.js +250 -0
- package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
- package/dist/notifications/notificationsBeforeSend.js +77 -0
- package/dist/openApi.d.ts +1 -7
- package/dist/openApi.js +7 -6
- package/dist/openApiBuilder.d.ts +14 -3
- package/dist/openApiValidator.d.ts +13 -1
- package/dist/openApiValidator.js +200 -36
- package/dist/orgs/orgContext.d.ts +23 -0
- package/dist/orgs/orgContext.js +198 -0
- package/dist/orgs/orgPermissions.d.ts +4 -0
- package/dist/orgs/orgPermissions.js +77 -0
- package/dist/orgs/orgPlugin.d.ts +13 -0
- package/dist/orgs/orgPlugin.js +84 -0
- package/dist/orgs/organizationModel.d.ts +13 -0
- package/dist/orgs/organizationModel.js +301 -0
- package/dist/orgs/organizationSettings.d.ts +19 -0
- package/dist/orgs/organizationSettings.js +120 -0
- package/dist/orgs/orgsApp.d.ts +28 -0
- package/dist/orgs/orgsApp.js +678 -0
- package/dist/permissions.d.ts +7 -13
- package/dist/permissions.js +8 -31
- package/dist/rateLimit/middleware.d.ts +0 -8
- package/dist/rateLimit/middleware.js +2 -3
- package/dist/rateLimit/mongoStore.d.ts +1 -0
- package/dist/rateLimit/mongoStore.js +1 -0
- package/dist/rateLimit/policies.d.ts +0 -4
- package/dist/rateLimit/policies.js +5 -7
- package/dist/rateLimit/redisStore.d.ts +1 -0
- package/dist/rateLimit/redisStore.js +1 -0
- package/dist/rbac/access.js +30 -13
- package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
- package/dist/rbac/membershipOrgAdminSource.js +122 -0
- package/dist/rbac/modelRouterAccess.d.ts +3 -0
- package/dist/rbac/modelRouterAccess.js +3 -0
- package/dist/rbac/resolve.js +60 -31
- package/dist/rbac/roleModel.d.ts +2 -5
- package/dist/rbac/roleModel.js +18 -4
- package/dist/rbac/statements.d.ts +10 -1
- package/dist/rbac/statements.js +19 -2
- package/dist/rbac/types.d.ts +5 -0
- package/dist/realtime/changeStreamWatcher.d.ts +10 -4
- package/dist/realtime/changeStreamWatcher.js +110 -64
- package/dist/realtime/deprecation.d.ts +1 -0
- package/dist/realtime/deprecation.js +1 -0
- package/dist/realtime/realtimeApp.d.ts +1 -0
- package/dist/realtime/realtimeApp.js +1 -0
- package/dist/schemaMetadata.js +16 -5
- package/dist/sync/adminBroadcastScope.d.ts +40 -0
- package/dist/sync/adminBroadcastScope.js +194 -0
- package/dist/sync/adminWindowAccess.d.ts +13 -0
- package/dist/sync/adminWindowAccess.js +96 -0
- package/dist/sync/adminWindowMutation.d.ts +88 -0
- package/dist/sync/adminWindowMutation.js +247 -0
- package/dist/sync/executors.d.ts +9 -3
- package/dist/sync/executors.js +190 -159
- package/dist/sync/mutationHandler.d.ts +5 -2
- package/dist/sync/mutationHandler.js +191 -71
- package/dist/sync/registry.js +4 -0
- package/dist/sync/routes.d.ts +13 -0
- package/dist/sync/routes.js +113 -56
- package/dist/sync/socketHandlers.d.ts +8 -2
- package/dist/sync/socketHandlers.js +144 -50
- package/dist/sync/streams.d.ts +2 -0
- package/dist/sync/streams.js +7 -1
- package/dist/sync/syncApp.d.ts +2 -1
- package/dist/sync/syncApp.js +2 -1
- package/dist/sync/syncBatch.d.ts +5 -2
- package/dist/sync/syncBatch.js +4 -4
- package/dist/sync/types.d.ts +18 -0
- package/dist/terrenoApp.d.ts +29 -0
- package/dist/terrenoApp.js +91 -54
- package/dist/tests/createTestData.d.ts +1 -3
- package/dist/tests/createTestData.js +3 -5
- package/dist/tests/models.d.ts +1 -0
- package/dist/tests/models.js +9 -2
- package/dist/tests/testHelper.d.ts +0 -5
- package/dist/tests/testHelper.js +2 -9
- package/dist/types/authToken.d.ts +3 -3
- package/dist/types/mcpServiceToken.d.ts +2 -2
- package/dist/types/membership.d.ts +23 -0
- package/dist/types/membership.js +2 -0
- package/dist/types/notification.d.ts +28 -0
- package/dist/types/notification.js +2 -0
- package/dist/types/notificationPreference.d.ts +20 -0
- package/dist/types/notificationPreference.js +2 -0
- package/dist/types/organization.d.ts +36 -0
- package/dist/types/organization.js +2 -0
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +4 -49
- package/dist/webhooks/idempotency/webhookReceipt.js +7 -7
- package/dist/webhooks/rawBody.d.ts +1 -0
- package/dist/webhooks/rawBody.js +1 -0
- package/package.json +31 -25
- package/src/__snapshots__/openApi.test.ts.snap +192 -54
- package/src/__snapshots__/openApiBuilder.test.ts.snap +0 -18
- package/src/__snapshots__/schemaMetadata.test.ts.snap +0 -1
- package/src/adminTypes.ts +8 -0
- package/src/api.test.ts +39 -2
- package/src/api.ts +68 -0
- package/src/audit/auditApp.test.ts +243 -0
- package/src/audit/auditApp.ts +139 -0
- package/src/audit/auditEventModel.ts +156 -0
- package/src/audit/auditRouter.test.ts +456 -0
- package/src/audit/cloudTasksEnqueue.test.ts +124 -0
- package/src/audit/cloudTasksEnqueue.ts +104 -0
- package/src/audit/diff.ts +163 -0
- package/src/audit/processQueue.ts +69 -0
- package/src/audit/rbacSink.test.ts +86 -0
- package/src/audit/rbacSink.ts +42 -0
- package/src/audit/record.test.ts +402 -0
- package/src/audit/record.ts +328 -0
- package/src/auth.test.ts +1 -1
- package/src/betterAuth.ts +7 -0
- package/src/betterAuthSetup.test.ts +27 -0
- package/src/betterAuthSetup.ts +1 -0
- package/src/collectionRegistry.test.ts +8 -9
- package/src/collectionRegistry.ts +21 -1
- package/src/express.d.ts +2 -0
- package/src/expressServer.test.ts +50 -2
- package/src/index.ts +69 -0
- package/src/mcp/handlers.test.ts +275 -0
- package/src/mcp/query.ts +3 -3
- package/src/mcp/toolGenerator.test.ts +81 -0
- package/src/migrations/cli.test.ts +376 -0
- package/src/migrations/cli.ts +283 -0
- package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
- package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
- package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
- package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
- package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
- package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
- package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
- package/src/migrations/generate.test.ts +333 -0
- package/src/migrations/generate.ts +216 -0
- package/src/migrations/load.test.ts +103 -0
- package/src/migrations/load.ts +124 -0
- package/src/migrations/lock.test.ts +133 -0
- package/src/migrations/lock.ts +118 -0
- package/src/migrations/runner.test.ts +504 -0
- package/src/migrations/runner.ts +358 -0
- package/src/migrations/schemaCatalog.test.ts +159 -0
- package/src/migrations/schemaCatalog.ts +262 -0
- package/src/migrations/types.ts +94 -0
- package/src/models/notification.ts +68 -0
- package/src/models/notificationPreference.ts +62 -0
- package/src/notifications/notificationService.ts +254 -0
- package/src/notifications/notificationsApp.test.ts +505 -0
- package/src/notifications/notificationsApp.ts +200 -0
- package/src/notifications/notificationsBeforeSend.ts +51 -0
- package/src/openApi.test.ts +0 -1
- package/src/openApi.ts +2 -1
- package/src/openApiBuilder.ts +14 -3
- package/src/openApiValidator.test.ts +221 -2
- package/src/openApiValidator.ts +218 -33
- package/src/orgs/orgAccessOptIn.test.ts +87 -0
- package/src/orgs/orgContext.test.ts +259 -0
- package/src/orgs/orgContext.ts +151 -0
- package/src/orgs/orgPermissions.test.ts +105 -0
- package/src/orgs/orgPermissions.ts +40 -0
- package/src/orgs/orgPlugin.test.ts +130 -0
- package/src/orgs/orgPlugin.ts +70 -0
- package/src/orgs/orgRbac.test.ts +267 -0
- package/src/orgs/orgSecurityDefects.test.ts +415 -0
- package/src/orgs/organizationModel.test.ts +205 -0
- package/src/orgs/organizationModel.ts +257 -0
- package/src/orgs/organizationSettings.test.ts +113 -0
- package/src/orgs/organizationSettings.ts +126 -0
- package/src/orgs/orgsApp.test.ts +382 -0
- package/src/orgs/orgsApp.ts +563 -0
- package/src/permissions.test.ts +62 -36
- package/src/permissions.ts +8 -30
- package/src/rateLimit/middleware.ts +1 -1
- package/src/rateLimit/mongoStore.ts +1 -0
- package/src/rateLimit/policies.ts +2 -2
- package/src/rateLimit/redisStore.ts +1 -0
- package/src/rbac/access.ts +11 -3
- package/src/rbac/membershipOrgAdminSource.ts +59 -0
- package/src/rbac/modelRouterAccess.ts +3 -0
- package/src/rbac/permissionUtils.test.ts +26 -5
- package/src/rbac/resolve.test.ts +34 -2
- package/src/rbac/resolve.ts +26 -7
- package/src/rbac/roleModel.test.ts +34 -1
- package/src/rbac/roleModel.ts +17 -1
- package/src/rbac/statements.test.ts +19 -0
- package/src/rbac/statements.ts +19 -1
- package/src/rbac/types.ts +5 -0
- package/src/realtime/changeStreamWatcher.ts +73 -33
- package/src/realtime/deprecation.ts +1 -0
- package/src/realtime/realtime.test.ts +357 -185
- package/src/realtime/realtimeApp.ts +1 -0
- package/src/schemaMetadata.test.ts +14 -0
- package/src/schemaMetadata.ts +5 -5
- package/src/sync/adminBroadcastConfig.test.ts +96 -0
- package/src/sync/adminBroadcastScope.test.ts +155 -0
- package/src/sync/adminBroadcastScope.ts +123 -0
- package/src/sync/adminWindowAccess.test.ts +57 -0
- package/src/sync/adminWindowAccess.ts +32 -0
- package/src/sync/adminWindowMutation.test.ts +450 -0
- package/src/sync/adminWindowMutation.ts +283 -0
- package/src/sync/executors.ts +33 -6
- package/src/sync/mutationHandler.test.ts +277 -1
- package/src/sync/mutationHandler.ts +171 -29
- package/src/sync/registry.ts +4 -0
- package/src/sync/routes.ts +52 -8
- package/src/sync/socketHandlers.ts +154 -58
- package/src/sync/streams.ts +6 -0
- package/src/sync/sync.test.ts +3 -2
- package/src/sync/syncApp.ts +2 -1
- package/src/sync/syncBatch.ts +7 -2
- package/src/sync/syncRoutes.test.ts +297 -3
- package/src/sync/syncSocket.test.ts +229 -0
- package/src/sync/types.ts +20 -0
- package/src/terrenoApp.migrations.test.ts +114 -0
- package/src/terrenoApp.test.ts +41 -0
- package/src/terrenoApp.ts +94 -31
- package/src/tests/createTestData.ts +2 -2
- package/src/tests/models.ts +10 -2
- package/src/tests/testHelper.ts +1 -7
- package/src/types/authToken.ts +2 -4
- package/src/types/mcpServiceToken.ts +1 -7
- package/src/types/membership.ts +48 -0
- package/src/types/notification.ts +34 -0
- package/src/types/notificationPreference.ts +28 -0
- package/src/types/organization.ts +49 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +4 -7
- package/src/webhooks/rawBody.test.ts +33 -0
- package/src/webhooks/rawBody.ts +1 -0
- package/tsconfig.json +1 -1
- package/tsconfig.typedoc.json +2 -1
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
- package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
|
@@ -0,0 +1,323 @@
|
|
|
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 __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.getNotificationService = exports.configureNotificationService = exports.createNotificationService = exports.assertValidNotificationArchivedAt = exports.assertValidNotificationReadAt = void 0;
|
|
51
|
+
var luxon_1 = require("luxon");
|
|
52
|
+
var errors_1 = require("../errors");
|
|
53
|
+
var logger_1 = require("../logger");
|
|
54
|
+
var notification_1 = require("../models/notification");
|
|
55
|
+
var notificationPreference_1 = require("../models/notificationPreference");
|
|
56
|
+
var escapeHtml = function (value) {
|
|
57
|
+
return value.replace(/[&<>"']/g, function (character) {
|
|
58
|
+
var _a;
|
|
59
|
+
return (_a = ({
|
|
60
|
+
"'": "'",
|
|
61
|
+
'"': """,
|
|
62
|
+
"&": "&",
|
|
63
|
+
"<": "<",
|
|
64
|
+
">": ">",
|
|
65
|
+
})[character]) !== null && _a !== void 0 ? _a : character;
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
var DEFAULT_PREFERENCES = {
|
|
69
|
+
inapp: true,
|
|
70
|
+
mail: true,
|
|
71
|
+
push: true,
|
|
72
|
+
sms: true,
|
|
73
|
+
};
|
|
74
|
+
var loadPreferences = function (userId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
75
|
+
var preference;
|
|
76
|
+
return __generator(this, function (_a) {
|
|
77
|
+
switch (_a.label) {
|
|
78
|
+
case 0: return [4 /*yield*/, notificationPreference_1.NotificationPreference.findOneOrNone({ ownerId: userId })];
|
|
79
|
+
case 1:
|
|
80
|
+
preference = _a.sent();
|
|
81
|
+
if (!preference) {
|
|
82
|
+
return [2 /*return*/, DEFAULT_PREFERENCES];
|
|
83
|
+
}
|
|
84
|
+
return [2 /*return*/, {
|
|
85
|
+
inapp: preference.inapp,
|
|
86
|
+
mail: preference.mail,
|
|
87
|
+
push: preference.push,
|
|
88
|
+
sms: preference.sms,
|
|
89
|
+
}];
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}); };
|
|
93
|
+
var loadUser = function (userModel, userId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
94
|
+
var user;
|
|
95
|
+
return __generator(this, function (_a) {
|
|
96
|
+
switch (_a.label) {
|
|
97
|
+
case 0:
|
|
98
|
+
if (!userModel) {
|
|
99
|
+
return [2 /*return*/, undefined];
|
|
100
|
+
}
|
|
101
|
+
return [4 /*yield*/, userModel.findById(userId).select("email phone").lean()];
|
|
102
|
+
case 1:
|
|
103
|
+
user = _a.sent();
|
|
104
|
+
if (!user) {
|
|
105
|
+
return [2 /*return*/, undefined];
|
|
106
|
+
}
|
|
107
|
+
return [2 /*return*/, { email: user.email, phone: user.phone }];
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}); };
|
|
111
|
+
var isValidReadAt = function (value) {
|
|
112
|
+
if (value === null || value === undefined) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
if (value instanceof Date && !Number.isNaN(value.getTime())) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
if (typeof value === "string") {
|
|
119
|
+
var parsed = luxon_1.DateTime.fromISO(value);
|
|
120
|
+
return parsed.isValid;
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
};
|
|
124
|
+
var assertValidNotificationReadAt = function (readAt) {
|
|
125
|
+
if (!isValidReadAt(readAt)) {
|
|
126
|
+
throw new errors_1.APIError({ status: 400, title: "readAt must be a date or null" });
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
exports.assertValidNotificationReadAt = assertValidNotificationReadAt;
|
|
130
|
+
var assertValidNotificationArchivedAt = function (archivedAt) {
|
|
131
|
+
if (!isValidReadAt(archivedAt)) {
|
|
132
|
+
throw new errors_1.APIError({ status: 400, title: "archivedAt must be a date or null" });
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
exports.assertValidNotificationArchivedAt = assertValidNotificationArchivedAt;
|
|
136
|
+
var createNotificationService = function (options) {
|
|
137
|
+
var _a;
|
|
138
|
+
if (options === void 0) { options = {}; }
|
|
139
|
+
var retainDays = (_a = options.retainDays) !== null && _a !== void 0 ? _a : 0;
|
|
140
|
+
var sweepExpired = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
141
|
+
var cutoff, stale, tombstoned, stale_1, stale_1_1, row, e_1_1;
|
|
142
|
+
var e_1, _a;
|
|
143
|
+
return __generator(this, function (_b) {
|
|
144
|
+
switch (_b.label) {
|
|
145
|
+
case 0:
|
|
146
|
+
if (retainDays <= 0) {
|
|
147
|
+
return [2 /*return*/, 0];
|
|
148
|
+
}
|
|
149
|
+
cutoff = luxon_1.DateTime.now().minus({ days: retainDays }).toJSDate();
|
|
150
|
+
return [4 /*yield*/, notification_1.Notification.find({
|
|
151
|
+
created: { $lt: cutoff },
|
|
152
|
+
deleted: { $ne: true },
|
|
153
|
+
})];
|
|
154
|
+
case 1:
|
|
155
|
+
stale = _b.sent();
|
|
156
|
+
tombstoned = 0;
|
|
157
|
+
_b.label = 2;
|
|
158
|
+
case 2:
|
|
159
|
+
_b.trys.push([2, 7, 8, 9]);
|
|
160
|
+
stale_1 = __values(stale), stale_1_1 = stale_1.next();
|
|
161
|
+
_b.label = 3;
|
|
162
|
+
case 3:
|
|
163
|
+
if (!!stale_1_1.done) return [3 /*break*/, 6];
|
|
164
|
+
row = stale_1_1.value;
|
|
165
|
+
row.deleted = true;
|
|
166
|
+
return [4 /*yield*/, row.save()];
|
|
167
|
+
case 4:
|
|
168
|
+
_b.sent();
|
|
169
|
+
tombstoned += 1;
|
|
170
|
+
_b.label = 5;
|
|
171
|
+
case 5:
|
|
172
|
+
stale_1_1 = stale_1.next();
|
|
173
|
+
return [3 /*break*/, 3];
|
|
174
|
+
case 6: return [3 /*break*/, 9];
|
|
175
|
+
case 7:
|
|
176
|
+
e_1_1 = _b.sent();
|
|
177
|
+
e_1 = { error: e_1_1 };
|
|
178
|
+
return [3 /*break*/, 9];
|
|
179
|
+
case 8:
|
|
180
|
+
try {
|
|
181
|
+
if (stale_1_1 && !stale_1_1.done && (_a = stale_1.return)) _a.call(stale_1);
|
|
182
|
+
}
|
|
183
|
+
finally { if (e_1) throw e_1.error; }
|
|
184
|
+
return [7 /*endfinally*/];
|
|
185
|
+
case 9: return [2 /*return*/, tombstoned];
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}); };
|
|
189
|
+
var fanOutComms = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
190
|
+
var getComms, comms, error_1, error_2, error_3, error_4;
|
|
191
|
+
var body = _b.body, input = _b.input, preferences = _b.preferences, title = _b.title, user = _b.user;
|
|
192
|
+
return __generator(this, function (_c) {
|
|
193
|
+
switch (_c.label) {
|
|
194
|
+
case 0:
|
|
195
|
+
getComms = options.getComms;
|
|
196
|
+
if (!getComms) {
|
|
197
|
+
return [2 /*return*/];
|
|
198
|
+
}
|
|
199
|
+
_c.label = 1;
|
|
200
|
+
case 1:
|
|
201
|
+
_c.trys.push([1, 14, , 15]);
|
|
202
|
+
comms = getComms();
|
|
203
|
+
if (!(preferences.mail && (user === null || user === void 0 ? void 0 : user.email))) return [3 /*break*/, 5];
|
|
204
|
+
_c.label = 2;
|
|
205
|
+
case 2:
|
|
206
|
+
_c.trys.push([2, 4, , 5]);
|
|
207
|
+
return [4 /*yield*/, comms.sendMail({
|
|
208
|
+
html: "<p>".concat(escapeHtml(body), "</p>"),
|
|
209
|
+
subject: title,
|
|
210
|
+
text: body,
|
|
211
|
+
to: user.email,
|
|
212
|
+
}, { userId: input.userId })];
|
|
213
|
+
case 3:
|
|
214
|
+
_c.sent();
|
|
215
|
+
return [3 /*break*/, 5];
|
|
216
|
+
case 4:
|
|
217
|
+
error_1 = _c.sent();
|
|
218
|
+
logger_1.logger.error("[notifications] comms fan-out channel failed after inbox write", {
|
|
219
|
+
channel: "mail",
|
|
220
|
+
error: error_1,
|
|
221
|
+
userId: input.userId,
|
|
222
|
+
});
|
|
223
|
+
return [3 /*break*/, 5];
|
|
224
|
+
case 5:
|
|
225
|
+
if (!(preferences.sms && (user === null || user === void 0 ? void 0 : user.phone))) return [3 /*break*/, 9];
|
|
226
|
+
_c.label = 6;
|
|
227
|
+
case 6:
|
|
228
|
+
_c.trys.push([6, 8, , 9]);
|
|
229
|
+
return [4 /*yield*/, comms.sendSms({ body: "".concat(title, ": ").concat(body), to: user.phone }, { userId: input.userId })];
|
|
230
|
+
case 7:
|
|
231
|
+
_c.sent();
|
|
232
|
+
return [3 /*break*/, 9];
|
|
233
|
+
case 8:
|
|
234
|
+
error_2 = _c.sent();
|
|
235
|
+
logger_1.logger.error("[notifications] comms fan-out channel failed after inbox write", {
|
|
236
|
+
channel: "sms",
|
|
237
|
+
error: error_2,
|
|
238
|
+
userId: input.userId,
|
|
239
|
+
});
|
|
240
|
+
return [3 /*break*/, 9];
|
|
241
|
+
case 9:
|
|
242
|
+
if (!preferences.push) return [3 /*break*/, 13];
|
|
243
|
+
_c.label = 10;
|
|
244
|
+
case 10:
|
|
245
|
+
_c.trys.push([10, 12, , 13]);
|
|
246
|
+
return [4 /*yield*/, comms.sendPushToUser({
|
|
247
|
+
body: body,
|
|
248
|
+
title: title,
|
|
249
|
+
userId: input.userId,
|
|
250
|
+
})];
|
|
251
|
+
case 11:
|
|
252
|
+
_c.sent();
|
|
253
|
+
return [3 /*break*/, 13];
|
|
254
|
+
case 12:
|
|
255
|
+
error_3 = _c.sent();
|
|
256
|
+
logger_1.logger.error("[notifications] comms fan-out channel failed after inbox write", {
|
|
257
|
+
channel: "push",
|
|
258
|
+
error: error_3,
|
|
259
|
+
userId: input.userId,
|
|
260
|
+
});
|
|
261
|
+
return [3 /*break*/, 13];
|
|
262
|
+
case 13: return [3 /*break*/, 15];
|
|
263
|
+
case 14:
|
|
264
|
+
error_4 = _c.sent();
|
|
265
|
+
logger_1.logger.error("[notifications] comms fan-out failed after inbox write", {
|
|
266
|
+
error: error_4,
|
|
267
|
+
userId: input.userId,
|
|
268
|
+
});
|
|
269
|
+
return [3 /*break*/, 15];
|
|
270
|
+
case 15: return [2 /*return*/];
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}); };
|
|
274
|
+
var notify = function (input) { return __awaiter(void 0, void 0, void 0, function () {
|
|
275
|
+
var preferences, user, notificationId, row;
|
|
276
|
+
return __generator(this, function (_a) {
|
|
277
|
+
switch (_a.label) {
|
|
278
|
+
case 0: return [4 /*yield*/, loadPreferences(input.userId)];
|
|
279
|
+
case 1:
|
|
280
|
+
preferences = _a.sent();
|
|
281
|
+
return [4 /*yield*/, loadUser(options.userModel, input.userId)];
|
|
282
|
+
case 2:
|
|
283
|
+
user = _a.sent();
|
|
284
|
+
if (!preferences.inapp) return [3 /*break*/, 4];
|
|
285
|
+
return [4 /*yield*/, notification_1.Notification.create({
|
|
286
|
+
body: input.body,
|
|
287
|
+
href: input.href,
|
|
288
|
+
kind: input.kind,
|
|
289
|
+
ownerId: input.userId,
|
|
290
|
+
title: input.title,
|
|
291
|
+
})];
|
|
292
|
+
case 3:
|
|
293
|
+
row = _a.sent();
|
|
294
|
+
notificationId = String(row._id);
|
|
295
|
+
_a.label = 4;
|
|
296
|
+
case 4: return [4 /*yield*/, fanOutComms({
|
|
297
|
+
body: input.body,
|
|
298
|
+
input: input,
|
|
299
|
+
preferences: preferences,
|
|
300
|
+
title: input.title,
|
|
301
|
+
user: user,
|
|
302
|
+
})];
|
|
303
|
+
case 5:
|
|
304
|
+
_a.sent();
|
|
305
|
+
if (!(retainDays > 0)) return [3 /*break*/, 7];
|
|
306
|
+
return [4 /*yield*/, sweepExpired()];
|
|
307
|
+
case 6:
|
|
308
|
+
_a.sent();
|
|
309
|
+
_a.label = 7;
|
|
310
|
+
case 7: return [2 /*return*/, notificationId !== null && notificationId !== void 0 ? notificationId : ""];
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}); };
|
|
314
|
+
return { notify: notify, sweepExpired: sweepExpired };
|
|
315
|
+
};
|
|
316
|
+
exports.createNotificationService = createNotificationService;
|
|
317
|
+
var notificationService = (0, exports.createNotificationService)();
|
|
318
|
+
var configureNotificationService = function (options) {
|
|
319
|
+
notificationService = (0, exports.createNotificationService)(options);
|
|
320
|
+
};
|
|
321
|
+
exports.configureNotificationService = configureNotificationService;
|
|
322
|
+
var getNotificationService = function () { return notificationService; };
|
|
323
|
+
exports.getNotificationService = getNotificationService;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Application } from "express";
|
|
2
|
+
import type { Model } from "mongoose";
|
|
3
|
+
import type { TerrenoPlugin } from "../terrenoPlugin";
|
|
4
|
+
import { type NotificationServiceOptions, type NotificationsCommsService } from "./notificationService";
|
|
5
|
+
export interface NotificationsAppOptions extends NotificationServiceOptions {
|
|
6
|
+
getComms?: () => NotificationsCommsService;
|
|
7
|
+
retainDays?: number;
|
|
8
|
+
userModel?: Model<{
|
|
9
|
+
email?: string;
|
|
10
|
+
phone?: string;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
export declare class NotificationsApp implements TerrenoPlugin {
|
|
14
|
+
private readonly options;
|
|
15
|
+
private routers?;
|
|
16
|
+
constructor(options?: NotificationsAppOptions);
|
|
17
|
+
register(app: Application, openApi?: unknown): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
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 __values = (this && this.__values) || function(o) {
|
|
50
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
51
|
+
if (m) return m.call(o);
|
|
52
|
+
if (o && typeof o.length === "number") return {
|
|
53
|
+
next: function () {
|
|
54
|
+
if (o && i >= o.length) o = void 0;
|
|
55
|
+
return { value: o && o[i++], done: !o };
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
59
|
+
};
|
|
60
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
61
|
+
exports.NotificationsApp = void 0;
|
|
62
|
+
var luxon_1 = require("luxon");
|
|
63
|
+
var api_1 = require("../api");
|
|
64
|
+
var auth_1 = require("../auth");
|
|
65
|
+
var errors_1 = require("../errors");
|
|
66
|
+
var logger_1 = require("../logger");
|
|
67
|
+
var notification_1 = require("../models/notification");
|
|
68
|
+
var notificationPreference_1 = require("../models/notificationPreference");
|
|
69
|
+
var openApiBuilder_1 = require("../openApiBuilder");
|
|
70
|
+
var permissions_1 = require("../permissions");
|
|
71
|
+
var notificationService_1 = require("./notificationService");
|
|
72
|
+
var PREFERENCE_CHANNELS = ["inapp", "mail", "push", "sms"];
|
|
73
|
+
var parseNotificationDate = function (value, fieldName) {
|
|
74
|
+
if (value === null || value === undefined) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (value instanceof Date) {
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
if (typeof value === "string") {
|
|
81
|
+
var parsed = luxon_1.DateTime.fromISO(value);
|
|
82
|
+
if (!parsed.isValid) {
|
|
83
|
+
throw new errors_1.APIError({ status: 400, title: "".concat(fieldName, " must be a date or null") });
|
|
84
|
+
}
|
|
85
|
+
return parsed.toJSDate();
|
|
86
|
+
}
|
|
87
|
+
throw new errors_1.APIError({ status: 400, title: "".concat(fieldName, " must be a date or null") });
|
|
88
|
+
};
|
|
89
|
+
var sanitizeNotificationUpdate = function (body) {
|
|
90
|
+
var payload = body;
|
|
91
|
+
var update = {};
|
|
92
|
+
if (payload.readAt !== undefined) {
|
|
93
|
+
(0, notificationService_1.assertValidNotificationReadAt)(payload.readAt);
|
|
94
|
+
update.readAt = parseNotificationDate(payload.readAt, "readAt");
|
|
95
|
+
}
|
|
96
|
+
if (payload.archivedAt !== undefined) {
|
|
97
|
+
(0, notificationService_1.assertValidNotificationArchivedAt)(payload.archivedAt);
|
|
98
|
+
update.archivedAt = parseNotificationDate(payload.archivedAt, "archivedAt");
|
|
99
|
+
}
|
|
100
|
+
return update;
|
|
101
|
+
};
|
|
102
|
+
var sanitizePreferenceUpdate = function (body) {
|
|
103
|
+
var e_1, _a;
|
|
104
|
+
var payload = body;
|
|
105
|
+
var update = {};
|
|
106
|
+
try {
|
|
107
|
+
for (var PREFERENCE_CHANNELS_1 = __values(PREFERENCE_CHANNELS), PREFERENCE_CHANNELS_1_1 = PREFERENCE_CHANNELS_1.next(); !PREFERENCE_CHANNELS_1_1.done; PREFERENCE_CHANNELS_1_1 = PREFERENCE_CHANNELS_1.next()) {
|
|
108
|
+
var channel = PREFERENCE_CHANNELS_1_1.value;
|
|
109
|
+
if (channel in payload) {
|
|
110
|
+
update[channel] = payload[channel];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
115
|
+
finally {
|
|
116
|
+
try {
|
|
117
|
+
if (PREFERENCE_CHANNELS_1_1 && !PREFERENCE_CHANNELS_1_1.done && (_a = PREFERENCE_CHANNELS_1.return)) _a.call(PREFERENCE_CHANNELS_1);
|
|
118
|
+
}
|
|
119
|
+
finally { if (e_1) throw e_1.error; }
|
|
120
|
+
}
|
|
121
|
+
return update;
|
|
122
|
+
};
|
|
123
|
+
var NotificationsApp = /** @class */ (function () {
|
|
124
|
+
function NotificationsApp(options) {
|
|
125
|
+
if (options === void 0) { options = {}; }
|
|
126
|
+
this.options = options;
|
|
127
|
+
}
|
|
128
|
+
NotificationsApp.prototype.register = function (app, openApi) {
|
|
129
|
+
var e_2, _a;
|
|
130
|
+
var _this = this;
|
|
131
|
+
var _b;
|
|
132
|
+
(0, notificationService_1.configureNotificationService)({
|
|
133
|
+
getComms: this.options.getComms,
|
|
134
|
+
retainDays: this.options.retainDays,
|
|
135
|
+
userModel: this.options.userModel,
|
|
136
|
+
});
|
|
137
|
+
if (this.routers) {
|
|
138
|
+
try {
|
|
139
|
+
for (var _c = __values(this.routers), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
140
|
+
var router = _d.value;
|
|
141
|
+
app.use(router.path, router.router);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
145
|
+
finally {
|
|
146
|
+
try {
|
|
147
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
148
|
+
}
|
|
149
|
+
finally { if (e_2) throw e_2.error; }
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
var openApiMiddleware = openApi;
|
|
154
|
+
var sharedRouterOptions = openApiMiddleware
|
|
155
|
+
? { openApi: openApiMiddleware }
|
|
156
|
+
: {};
|
|
157
|
+
var notificationsRouter = (0, api_1.modelRouter)("/notifications", notification_1.Notification, __assign(__assign({}, sharedRouterOptions), { endpoints: function (router) {
|
|
158
|
+
router.post("/mark-all-read", [
|
|
159
|
+
(0, auth_1.authenticateMiddleware)(),
|
|
160
|
+
(0, openApiBuilder_1.createOpenApiBuilder)(sharedRouterOptions)
|
|
161
|
+
.withTags(["notifications"])
|
|
162
|
+
.withSummary("Mark all of the caller's unread notifications as read")
|
|
163
|
+
.withResponse(200, {
|
|
164
|
+
data: {
|
|
165
|
+
properties: { modified: { type: "number" } },
|
|
166
|
+
type: "object",
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
.build(),
|
|
170
|
+
], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
171
|
+
var user, unread, now, modified, unread_1, unread_1_1, row, e_3_1;
|
|
172
|
+
var e_3, _a;
|
|
173
|
+
return __generator(this, function (_b) {
|
|
174
|
+
switch (_b.label) {
|
|
175
|
+
case 0:
|
|
176
|
+
user = req.user;
|
|
177
|
+
if (!(user === null || user === void 0 ? void 0 : user.id)) {
|
|
178
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
179
|
+
}
|
|
180
|
+
return [4 /*yield*/, notification_1.Notification.find({
|
|
181
|
+
$or: [{ readAt: null }, { readAt: { $exists: false } }],
|
|
182
|
+
ownerId: user.id,
|
|
183
|
+
})];
|
|
184
|
+
case 1:
|
|
185
|
+
unread = _b.sent();
|
|
186
|
+
now = luxon_1.DateTime.now().toJSDate();
|
|
187
|
+
modified = 0;
|
|
188
|
+
_b.label = 2;
|
|
189
|
+
case 2:
|
|
190
|
+
_b.trys.push([2, 7, 8, 9]);
|
|
191
|
+
unread_1 = __values(unread), unread_1_1 = unread_1.next();
|
|
192
|
+
_b.label = 3;
|
|
193
|
+
case 3:
|
|
194
|
+
if (!!unread_1_1.done) return [3 /*break*/, 6];
|
|
195
|
+
row = unread_1_1.value;
|
|
196
|
+
row.readAt = now;
|
|
197
|
+
return [4 /*yield*/, row.save()];
|
|
198
|
+
case 4:
|
|
199
|
+
_b.sent();
|
|
200
|
+
modified += 1;
|
|
201
|
+
_b.label = 5;
|
|
202
|
+
case 5:
|
|
203
|
+
unread_1_1 = unread_1.next();
|
|
204
|
+
return [3 /*break*/, 3];
|
|
205
|
+
case 6: return [3 /*break*/, 9];
|
|
206
|
+
case 7:
|
|
207
|
+
e_3_1 = _b.sent();
|
|
208
|
+
e_3 = { error: e_3_1 };
|
|
209
|
+
return [3 /*break*/, 9];
|
|
210
|
+
case 8:
|
|
211
|
+
try {
|
|
212
|
+
if (unread_1_1 && !unread_1_1.done && (_a = unread_1.return)) _a.call(unread_1);
|
|
213
|
+
}
|
|
214
|
+
finally { if (e_3) throw e_3.error; }
|
|
215
|
+
return [7 /*endfinally*/];
|
|
216
|
+
case 9:
|
|
217
|
+
logger_1.logger.info("[notifications] mark-all-read", { modified: modified, userId: user.id });
|
|
218
|
+
return [2 /*return*/, res.json({ data: { modified: modified } })];
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}); }));
|
|
222
|
+
}, permissions: {
|
|
223
|
+
create: [],
|
|
224
|
+
delete: [permissions_1.Permissions.IsOwner],
|
|
225
|
+
list: [permissions_1.Permissions.IsAuthenticated],
|
|
226
|
+
read: [permissions_1.Permissions.IsOwner],
|
|
227
|
+
update: [permissions_1.Permissions.IsOwner],
|
|
228
|
+
}, preUpdate: sanitizeNotificationUpdate, queryFields: ["_id", "ownerId", "readAt", "archivedAt", "kind"], queryFilter: permissions_1.OwnerQueryFilter, sort: "-created", sync: { scope: { type: "owner" } } }));
|
|
229
|
+
app.use(notificationsRouter.path, notificationsRouter.router);
|
|
230
|
+
var preferencesRouter = (0, api_1.modelRouter)("/notification-preferences", notificationPreference_1.NotificationPreference, __assign(__assign({}, (openApiMiddleware ? { openApi: openApiMiddleware } : {})), { permissions: {
|
|
231
|
+
create: [permissions_1.Permissions.IsAuthenticated],
|
|
232
|
+
delete: [permissions_1.Permissions.IsOwner],
|
|
233
|
+
list: [permissions_1.Permissions.IsAuthenticated],
|
|
234
|
+
read: [permissions_1.Permissions.IsOwner],
|
|
235
|
+
update: [permissions_1.Permissions.IsOwner],
|
|
236
|
+
}, preCreate: function (body, req) {
|
|
237
|
+
var user = req.user;
|
|
238
|
+
if (!(user === null || user === void 0 ? void 0 : user.id)) {
|
|
239
|
+
throw new errors_1.APIError({ status: 401, title: "Authentication required" });
|
|
240
|
+
}
|
|
241
|
+
var payload = body;
|
|
242
|
+
return __assign(__assign({}, payload), { ownerId: user.id });
|
|
243
|
+
}, preUpdate: sanitizePreferenceUpdate, queryFields: ["ownerId"], queryFilter: permissions_1.OwnerQueryFilter, sort: "-created", sync: { scope: { type: "owner" } } }));
|
|
244
|
+
app.use(preferencesRouter.path, preferencesRouter.router);
|
|
245
|
+
this.routers = [notificationsRouter, preferencesRouter];
|
|
246
|
+
logger_1.logger.info("NotificationsApp registered", { retainDays: (_b = this.options.retainDays) !== null && _b !== void 0 ? _b : 0 });
|
|
247
|
+
};
|
|
248
|
+
return NotificationsApp;
|
|
249
|
+
}());
|
|
250
|
+
exports.NotificationsApp = NotificationsApp;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type NotificationsBeforeSendChannel = "mail" | "push" | "sms" | "verification";
|
|
2
|
+
export interface NotificationsBeforeSendContext {
|
|
3
|
+
channel: NotificationsBeforeSendChannel;
|
|
4
|
+
userId?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface NotificationsBeforeSendResult {
|
|
7
|
+
cancel?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Duck-typed CommsApp `beforeSend` hook that cancels outbound delivery when the user
|
|
11
|
+
* disabled that channel in NotificationPreference. Missing preference rows default to
|
|
12
|
+
* all channels on. The `verification` channel is never cancelled here.
|
|
13
|
+
*/
|
|
14
|
+
export declare const notificationsBeforeSend: ({ channel, userId, }: NotificationsBeforeSendContext) => Promise<NotificationsBeforeSendResult | undefined>;
|