@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,678 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.OrgsApp = void 0;
|
|
40
|
+
var express_1 = require("express");
|
|
41
|
+
var api_1 = require("../api");
|
|
42
|
+
var auth_1 = require("../auth");
|
|
43
|
+
var errors_1 = require("../errors");
|
|
44
|
+
var logger_1 = require("../logger");
|
|
45
|
+
var openApiBuilder_1 = require("../openApiBuilder");
|
|
46
|
+
var plugins_1 = require("../plugins");
|
|
47
|
+
var organizationModel_1 = require("./organizationModel");
|
|
48
|
+
var organizationSettings_1 = require("./organizationSettings");
|
|
49
|
+
var orgContext_1 = require("./orgContext");
|
|
50
|
+
var escapeRegularExpression = function (value) {
|
|
51
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
52
|
+
};
|
|
53
|
+
var findUserForAttach = function (userModel, args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
54
|
+
return __generator(this, function (_a) {
|
|
55
|
+
if (args.email) {
|
|
56
|
+
return [2 /*return*/, (0, plugins_1.findOneOrNoneFor)(userModel, {
|
|
57
|
+
email: { $options: "i", $regex: "^".concat(escapeRegularExpression(args.email), "$") },
|
|
58
|
+
})];
|
|
59
|
+
}
|
|
60
|
+
if (args.userId) {
|
|
61
|
+
return [2 /*return*/, (0, plugins_1.findOneOrNoneFor)(userModel, { _id: args.userId })];
|
|
62
|
+
}
|
|
63
|
+
throw new errors_1.APIError({ status: 400, title: "email or userId is required" });
|
|
64
|
+
});
|
|
65
|
+
}); };
|
|
66
|
+
var pathParam = function (value, title) {
|
|
67
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
68
|
+
throw new errors_1.APIError({ status: 400, title: title });
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
};
|
|
72
|
+
var userIdOf = function (user) {
|
|
73
|
+
if (user._id) {
|
|
74
|
+
return String(user._id);
|
|
75
|
+
}
|
|
76
|
+
return user.id;
|
|
77
|
+
};
|
|
78
|
+
var requireUser = function (req) {
|
|
79
|
+
var user = req.user;
|
|
80
|
+
if (!(user === null || user === void 0 ? void 0 : user.id)) {
|
|
81
|
+
throw new errors_1.APIError({ status: 401, title: "Unauthorized" });
|
|
82
|
+
}
|
|
83
|
+
return user;
|
|
84
|
+
};
|
|
85
|
+
var emitOrgAudit = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
86
|
+
var error_1, detail;
|
|
87
|
+
return __generator(this, function (_a) {
|
|
88
|
+
switch (_a.label) {
|
|
89
|
+
case 0:
|
|
90
|
+
if (!args.onOrgAudit) {
|
|
91
|
+
return [2 /*return*/];
|
|
92
|
+
}
|
|
93
|
+
_a.label = 1;
|
|
94
|
+
case 1:
|
|
95
|
+
_a.trys.push([1, 3, , 4]);
|
|
96
|
+
return [4 /*yield*/, args.onOrgAudit(args.event, args.req)];
|
|
97
|
+
case 2:
|
|
98
|
+
_a.sent();
|
|
99
|
+
return [3 /*break*/, 4];
|
|
100
|
+
case 3:
|
|
101
|
+
error_1 = _a.sent();
|
|
102
|
+
detail = error_1 instanceof Error ? error_1.message : String(error_1);
|
|
103
|
+
logger_1.logger.error("onOrgAudit failed after ".concat(args.event.verb, ": ").concat(detail));
|
|
104
|
+
return [3 /*break*/, 4];
|
|
105
|
+
case 4: return [2 /*return*/];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}); };
|
|
109
|
+
var loadOrganization = function (id) { return __awaiter(void 0, void 0, void 0, function () {
|
|
110
|
+
var organization;
|
|
111
|
+
return __generator(this, function (_a) {
|
|
112
|
+
switch (_a.label) {
|
|
113
|
+
case 0: return [4 /*yield*/, organizationModel_1.Organization.findOneOrNone({ _id: id })];
|
|
114
|
+
case 1:
|
|
115
|
+
organization = _a.sent();
|
|
116
|
+
if (!organization) {
|
|
117
|
+
throw new errors_1.NotFoundError("Organization not found");
|
|
118
|
+
}
|
|
119
|
+
return [2 /*return*/, organization];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}); };
|
|
123
|
+
var loadActiveMembership = function (user, organizationId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
124
|
+
var membership;
|
|
125
|
+
return __generator(this, function (_a) {
|
|
126
|
+
switch (_a.label) {
|
|
127
|
+
case 0: return [4 /*yield*/, organizationModel_1.Membership.findOneOrNone({
|
|
128
|
+
organizationId: organizationId,
|
|
129
|
+
status: "active",
|
|
130
|
+
userId: userIdOf(user),
|
|
131
|
+
})];
|
|
132
|
+
case 1:
|
|
133
|
+
membership = _a.sent();
|
|
134
|
+
return [2 /*return*/, membership !== null && membership !== void 0 ? membership : undefined];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}); };
|
|
138
|
+
var countActiveOrgAdmins = function (organizationId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
139
|
+
return __generator(this, function (_a) {
|
|
140
|
+
return [2 /*return*/, organizationModel_1.Membership.countDocuments({
|
|
141
|
+
organizationId: organizationId,
|
|
142
|
+
roleName: "org-admin",
|
|
143
|
+
status: "active",
|
|
144
|
+
})];
|
|
145
|
+
});
|
|
146
|
+
}); };
|
|
147
|
+
var assertNotLastOrgAdmin = function (membership, nextRoleName, nextStatus) { return __awaiter(void 0, void 0, void 0, function () {
|
|
148
|
+
var remainsOrgAdmin, activeAdmins;
|
|
149
|
+
return __generator(this, function (_a) {
|
|
150
|
+
switch (_a.label) {
|
|
151
|
+
case 0:
|
|
152
|
+
remainsOrgAdmin = (nextRoleName !== null && nextRoleName !== void 0 ? nextRoleName : membership.roleName) === "org-admin" &&
|
|
153
|
+
(nextStatus !== null && nextStatus !== void 0 ? nextStatus : membership.status) === "active";
|
|
154
|
+
if (remainsOrgAdmin) {
|
|
155
|
+
return [2 /*return*/];
|
|
156
|
+
}
|
|
157
|
+
if (membership.roleName !== "org-admin" || membership.status !== "active") {
|
|
158
|
+
return [2 /*return*/];
|
|
159
|
+
}
|
|
160
|
+
return [4 /*yield*/, countActiveOrgAdmins(membership.organizationId)];
|
|
161
|
+
case 1:
|
|
162
|
+
activeAdmins = _a.sent();
|
|
163
|
+
if (activeAdmins <= 1) {
|
|
164
|
+
throw new errors_1.APIError({ status: 400, title: "Cannot remove the last org-admin" });
|
|
165
|
+
}
|
|
166
|
+
return [2 /*return*/];
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}); };
|
|
170
|
+
var assertCan = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
171
|
+
var result;
|
|
172
|
+
var _a;
|
|
173
|
+
return __generator(this, function (_b) {
|
|
174
|
+
switch (_b.label) {
|
|
175
|
+
case 0: return [4 /*yield*/, args.access.can({
|
|
176
|
+
doc: args.organization,
|
|
177
|
+
permissions: args.permissions,
|
|
178
|
+
user: args.user,
|
|
179
|
+
})];
|
|
180
|
+
case 1:
|
|
181
|
+
result = _b.sent();
|
|
182
|
+
if (result.allowed) {
|
|
183
|
+
return [2 /*return*/];
|
|
184
|
+
}
|
|
185
|
+
throw new errors_1.ForbiddenError((_a = result.reason) !== null && _a !== void 0 ? _a : "Access denied");
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}); };
|
|
189
|
+
var OrgsApp = /** @class */ (function () {
|
|
190
|
+
function OrgsApp(options) {
|
|
191
|
+
var _a;
|
|
192
|
+
this.access = options.access;
|
|
193
|
+
this.basePath = (_a = options.basePath) !== null && _a !== void 0 ? _a : "/orgs";
|
|
194
|
+
this.onOrgAudit = options.onOrgAudit;
|
|
195
|
+
this.userModel = options.userModel;
|
|
196
|
+
if (options.settingsSchema) {
|
|
197
|
+
(0, organizationSettings_1.registerOrganizationSettings)(options.settingsSchema);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
OrgsApp.prototype.register = function (app, openApi) {
|
|
201
|
+
var _this = this;
|
|
202
|
+
var router = (0, express_1.Router)();
|
|
203
|
+
var openApiMiddleware = openApi;
|
|
204
|
+
var docs = function (summary, requestBody) {
|
|
205
|
+
var builder = (0, openApiBuilder_1.createOpenApiBuilder)({ openApi: openApiMiddleware })
|
|
206
|
+
.withTags(["organizations"])
|
|
207
|
+
.withSummary(summary);
|
|
208
|
+
if (requestBody) {
|
|
209
|
+
builder.withRequestBody(requestBody);
|
|
210
|
+
}
|
|
211
|
+
return builder.build();
|
|
212
|
+
};
|
|
213
|
+
router.post("/", (0, auth_1.authenticateMiddleware)(), this.access.middleware({ organization: ["create"] }), docs("Create an organization", {
|
|
214
|
+
name: { description: "Organization name", required: true, type: "string" },
|
|
215
|
+
settings: { description: "App-defined organization settings", type: "object" },
|
|
216
|
+
}), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
217
|
+
var user, name, organization;
|
|
218
|
+
var _a, _b;
|
|
219
|
+
return __generator(this, function (_c) {
|
|
220
|
+
switch (_c.label) {
|
|
221
|
+
case 0:
|
|
222
|
+
user = requireUser(req);
|
|
223
|
+
name = typeof ((_a = req.body) === null || _a === void 0 ? void 0 : _a.name) === "string" ? req.body.name.trim() : "";
|
|
224
|
+
if (!name) {
|
|
225
|
+
throw new errors_1.APIError({ status: 400, title: "name is required" });
|
|
226
|
+
}
|
|
227
|
+
return [4 /*yield*/, (0, organizationModel_1.assertOrganizationSlugAvailable)((0, organizationModel_1.organizationSlugFromName)(name))];
|
|
228
|
+
case 1:
|
|
229
|
+
_c.sent();
|
|
230
|
+
return [4 /*yield*/, organizationModel_1.Organization.create({
|
|
231
|
+
name: name,
|
|
232
|
+
ownerId: userIdOf(user),
|
|
233
|
+
settings: (_b = req.body) === null || _b === void 0 ? void 0 : _b.settings,
|
|
234
|
+
})];
|
|
235
|
+
case 2:
|
|
236
|
+
organization = _c.sent();
|
|
237
|
+
return [4 /*yield*/, emitOrgAudit({
|
|
238
|
+
event: {
|
|
239
|
+
actorId: user.id,
|
|
240
|
+
organizationId: String(organization._id),
|
|
241
|
+
organizationLabel: organization.name,
|
|
242
|
+
verb: "created",
|
|
243
|
+
},
|
|
244
|
+
onOrgAudit: this.onOrgAudit,
|
|
245
|
+
req: req,
|
|
246
|
+
})];
|
|
247
|
+
case 3:
|
|
248
|
+
_c.sent();
|
|
249
|
+
return [2 /*return*/, res.status(201).json({ data: organization })];
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}); }));
|
|
253
|
+
router.get("/", (0, auth_1.authenticateMiddleware)(), this.access.middleware({ organization: ["list"] }), docs("List all organizations"), (0, api_1.asyncHandler)(function (_req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
254
|
+
var organizations;
|
|
255
|
+
return __generator(this, function (_a) {
|
|
256
|
+
switch (_a.label) {
|
|
257
|
+
case 0: return [4 /*yield*/, organizationModel_1.Organization.find({}).sort({ name: 1 })];
|
|
258
|
+
case 1:
|
|
259
|
+
organizations = _a.sent();
|
|
260
|
+
return [2 /*return*/, res.json({ data: organizations })];
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}); }));
|
|
264
|
+
router.get("/mine", (0, auth_1.authenticateMiddleware)(), docs("List organizations the caller may switch into"), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
265
|
+
var user, organizations_1, memberships, organizations;
|
|
266
|
+
return __generator(this, function (_a) {
|
|
267
|
+
switch (_a.label) {
|
|
268
|
+
case 0:
|
|
269
|
+
user = requireUser(req);
|
|
270
|
+
if (!(0, orgContext_1.isPlatformOrgActor)(user)) return [3 /*break*/, 2];
|
|
271
|
+
return [4 /*yield*/, organizationModel_1.Organization.find({ disabled: { $ne: true } }).sort({ name: 1 })];
|
|
272
|
+
case 1:
|
|
273
|
+
organizations_1 = _a.sent();
|
|
274
|
+
return [2 /*return*/, res.json({ data: organizations_1 })];
|
|
275
|
+
case 2: return [4 /*yield*/, organizationModel_1.Membership.find({
|
|
276
|
+
roleName: "org-admin",
|
|
277
|
+
status: "active",
|
|
278
|
+
userId: userIdOf(user),
|
|
279
|
+
})];
|
|
280
|
+
case 3:
|
|
281
|
+
memberships = _a.sent();
|
|
282
|
+
if (memberships.length === 0) {
|
|
283
|
+
throw new errors_1.ForbiddenError("Access denied");
|
|
284
|
+
}
|
|
285
|
+
return [4 /*yield*/, organizationModel_1.Organization.find({
|
|
286
|
+
_id: { $in: memberships.map(function (membership) { return membership.organizationId; }) },
|
|
287
|
+
disabled: { $ne: true },
|
|
288
|
+
}).sort({ name: 1 })];
|
|
289
|
+
case 4:
|
|
290
|
+
organizations = _a.sent();
|
|
291
|
+
return [2 /*return*/, res.json({ data: organizations })];
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}); }));
|
|
295
|
+
router.get("/:id", (0, auth_1.authenticateMiddleware)(), docs("Read an organization"), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
296
|
+
var user, organization, membership;
|
|
297
|
+
var _this = this;
|
|
298
|
+
return __generator(this, function (_a) {
|
|
299
|
+
switch (_a.label) {
|
|
300
|
+
case 0:
|
|
301
|
+
user = requireUser(req);
|
|
302
|
+
return [4 /*yield*/, loadOrganization(pathParam(req.params.id, "id is required"))];
|
|
303
|
+
case 1:
|
|
304
|
+
organization = _a.sent();
|
|
305
|
+
return [4 /*yield*/, loadActiveMembership(user, organization._id)];
|
|
306
|
+
case 2:
|
|
307
|
+
membership = _a.sent();
|
|
308
|
+
return [4 /*yield*/, (0, orgContext_1.runWithOrgContext)({ membership: membership, organization: organization }, function () {
|
|
309
|
+
return assertCan({
|
|
310
|
+
access: _this.access,
|
|
311
|
+
organization: organization,
|
|
312
|
+
permissions: { organization: ["read"] },
|
|
313
|
+
user: user,
|
|
314
|
+
});
|
|
315
|
+
})];
|
|
316
|
+
case 3:
|
|
317
|
+
_a.sent();
|
|
318
|
+
return [2 /*return*/, res.json({ data: organization })];
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
}); }));
|
|
322
|
+
router.patch("/:id", (0, auth_1.authenticateMiddleware)(), docs("Update an organization", {
|
|
323
|
+
disabled: { description: "Disable the organization", type: "boolean" },
|
|
324
|
+
name: { description: "Organization name", type: "string" },
|
|
325
|
+
settings: { description: "App-defined organization settings", type: "object" },
|
|
326
|
+
}), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
327
|
+
var user, organization, membership, body, wantsDisable, trimmedName, slug;
|
|
328
|
+
var _this = this;
|
|
329
|
+
var _a;
|
|
330
|
+
return __generator(this, function (_b) {
|
|
331
|
+
switch (_b.label) {
|
|
332
|
+
case 0:
|
|
333
|
+
user = requireUser(req);
|
|
334
|
+
return [4 /*yield*/, loadOrganization(pathParam(req.params.id, "id is required"))];
|
|
335
|
+
case 1:
|
|
336
|
+
organization = _b.sent();
|
|
337
|
+
return [4 /*yield*/, loadActiveMembership(user, organization._id)];
|
|
338
|
+
case 2:
|
|
339
|
+
membership = _b.sent();
|
|
340
|
+
body = (_a = req.body) !== null && _a !== void 0 ? _a : {};
|
|
341
|
+
wantsDisable = "disabled" in body && Boolean(body.disabled) && !organization.disabled;
|
|
342
|
+
return [4 /*yield*/, (0, orgContext_1.runWithOrgContext)({ membership: membership, organization: organization }, function () { return __awaiter(_this, void 0, void 0, function () {
|
|
343
|
+
return __generator(this, function (_a) {
|
|
344
|
+
switch (_a.label) {
|
|
345
|
+
case 0: return [4 /*yield*/, assertCan({
|
|
346
|
+
access: this.access,
|
|
347
|
+
organization: organization,
|
|
348
|
+
permissions: { organization: ["update"] },
|
|
349
|
+
user: user,
|
|
350
|
+
})];
|
|
351
|
+
case 1:
|
|
352
|
+
_a.sent();
|
|
353
|
+
if (!wantsDisable) return [3 /*break*/, 3];
|
|
354
|
+
return [4 /*yield*/, assertCan({
|
|
355
|
+
access: this.access,
|
|
356
|
+
organization: organization,
|
|
357
|
+
permissions: { organization: ["disable"] },
|
|
358
|
+
user: user,
|
|
359
|
+
})];
|
|
360
|
+
case 2:
|
|
361
|
+
_a.sent();
|
|
362
|
+
_a.label = 3;
|
|
363
|
+
case 3: return [2 /*return*/];
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}); })];
|
|
367
|
+
case 3:
|
|
368
|
+
_b.sent();
|
|
369
|
+
if (!(typeof body.name === "string" && body.name.trim())) return [3 /*break*/, 5];
|
|
370
|
+
trimmedName = body.name.trim();
|
|
371
|
+
slug = (0, organizationModel_1.organizationSlugFromName)(trimmedName);
|
|
372
|
+
return [4 /*yield*/, (0, organizationModel_1.assertOrganizationSlugAvailable)(slug, organization._id)];
|
|
373
|
+
case 4:
|
|
374
|
+
_b.sent();
|
|
375
|
+
organization.name = trimmedName;
|
|
376
|
+
organization.slug = slug;
|
|
377
|
+
_b.label = 5;
|
|
378
|
+
case 5:
|
|
379
|
+
if ("settings" in body) {
|
|
380
|
+
organization.settings = body.settings;
|
|
381
|
+
}
|
|
382
|
+
if ("disabled" in body) {
|
|
383
|
+
organization.disabled = Boolean(body.disabled);
|
|
384
|
+
}
|
|
385
|
+
return [4 /*yield*/, organization.save()];
|
|
386
|
+
case 6:
|
|
387
|
+
_b.sent();
|
|
388
|
+
if (!wantsDisable) return [3 /*break*/, 8];
|
|
389
|
+
return [4 /*yield*/, organizationModel_1.Membership.updateMany({ organizationId: organization._id }, { status: "suspended" })];
|
|
390
|
+
case 7:
|
|
391
|
+
_b.sent();
|
|
392
|
+
_b.label = 8;
|
|
393
|
+
case 8: return [4 /*yield*/, emitOrgAudit({
|
|
394
|
+
event: {
|
|
395
|
+
actorId: user.id,
|
|
396
|
+
organizationId: String(organization._id),
|
|
397
|
+
organizationLabel: organization.name,
|
|
398
|
+
verb: wantsDisable ? "disabled" : "updated",
|
|
399
|
+
},
|
|
400
|
+
onOrgAudit: this.onOrgAudit,
|
|
401
|
+
req: req,
|
|
402
|
+
})];
|
|
403
|
+
case 9:
|
|
404
|
+
_b.sent();
|
|
405
|
+
return [2 /*return*/, res.json({ data: organization })];
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
}); }));
|
|
409
|
+
router.delete("/:id", (0, auth_1.authenticateMiddleware)(), this.access.middleware({ organization: ["delete"] }), docs("Soft-delete an organization"), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
410
|
+
var user, organization;
|
|
411
|
+
return __generator(this, function (_a) {
|
|
412
|
+
switch (_a.label) {
|
|
413
|
+
case 0:
|
|
414
|
+
user = requireUser(req);
|
|
415
|
+
return [4 /*yield*/, loadOrganization(pathParam(req.params.id, "id is required"))];
|
|
416
|
+
case 1:
|
|
417
|
+
organization = _a.sent();
|
|
418
|
+
organization.deleted = true;
|
|
419
|
+
return [4 /*yield*/, organization.save()];
|
|
420
|
+
case 2:
|
|
421
|
+
_a.sent();
|
|
422
|
+
return [4 /*yield*/, organizationModel_1.Membership.updateMany({ organizationId: organization._id }, { status: "suspended" })];
|
|
423
|
+
case 3:
|
|
424
|
+
_a.sent();
|
|
425
|
+
return [4 /*yield*/, emitOrgAudit({
|
|
426
|
+
event: {
|
|
427
|
+
actorId: user.id,
|
|
428
|
+
organizationId: String(organization._id),
|
|
429
|
+
organizationLabel: organization.name,
|
|
430
|
+
verb: "deleted",
|
|
431
|
+
},
|
|
432
|
+
onOrgAudit: this.onOrgAudit,
|
|
433
|
+
req: req,
|
|
434
|
+
})];
|
|
435
|
+
case 4:
|
|
436
|
+
_a.sent();
|
|
437
|
+
return [2 /*return*/, res.status(204).send()];
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}); }));
|
|
441
|
+
router.get("/:id/members", (0, auth_1.authenticateMiddleware)(), docs("List organization members"), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
442
|
+
var user, organization, membership, members;
|
|
443
|
+
var _this = this;
|
|
444
|
+
return __generator(this, function (_a) {
|
|
445
|
+
switch (_a.label) {
|
|
446
|
+
case 0:
|
|
447
|
+
user = requireUser(req);
|
|
448
|
+
return [4 /*yield*/, loadOrganization(pathParam(req.params.id, "id is required"))];
|
|
449
|
+
case 1:
|
|
450
|
+
organization = _a.sent();
|
|
451
|
+
return [4 /*yield*/, loadActiveMembership(user, organization._id)];
|
|
452
|
+
case 2:
|
|
453
|
+
membership = _a.sent();
|
|
454
|
+
return [4 /*yield*/, (0, orgContext_1.runWithOrgContext)({ membership: membership, organization: organization }, function () {
|
|
455
|
+
return assertCan({
|
|
456
|
+
access: _this.access,
|
|
457
|
+
organization: organization,
|
|
458
|
+
permissions: { organization: ["manageMembers"] },
|
|
459
|
+
user: user,
|
|
460
|
+
});
|
|
461
|
+
})];
|
|
462
|
+
case 3:
|
|
463
|
+
_a.sent();
|
|
464
|
+
return [4 /*yield*/, organizationModel_1.Membership.find({ organizationId: organization._id })
|
|
465
|
+
.sort({ created: 1 })
|
|
466
|
+
.populate({ path: "userId", select: "email name" })];
|
|
467
|
+
case 4:
|
|
468
|
+
members = _a.sent();
|
|
469
|
+
return [2 /*return*/, res.json({ data: members })];
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
}); }));
|
|
473
|
+
router.post("/:id/members", (0, auth_1.authenticateMiddleware)(), docs("Attach an existing user as a member", {
|
|
474
|
+
email: { description: "Existing user email", type: "string" },
|
|
475
|
+
roleName: {
|
|
476
|
+
description: "Membership role",
|
|
477
|
+
type: "string",
|
|
478
|
+
},
|
|
479
|
+
userId: { description: "Existing user id", type: "string" },
|
|
480
|
+
}), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
481
|
+
var user, organization, actorMembership, email, userId, target, existing, roleName, created;
|
|
482
|
+
var _this = this;
|
|
483
|
+
var _a, _b, _c;
|
|
484
|
+
return __generator(this, function (_d) {
|
|
485
|
+
switch (_d.label) {
|
|
486
|
+
case 0:
|
|
487
|
+
user = requireUser(req);
|
|
488
|
+
return [4 /*yield*/, loadOrganization(pathParam(req.params.id, "id is required"))];
|
|
489
|
+
case 1:
|
|
490
|
+
organization = _d.sent();
|
|
491
|
+
(0, orgContext_1.assertOrganizationEnabled)(organization);
|
|
492
|
+
return [4 /*yield*/, loadActiveMembership(user, organization._id)];
|
|
493
|
+
case 2:
|
|
494
|
+
actorMembership = _d.sent();
|
|
495
|
+
return [4 /*yield*/, (0, orgContext_1.runWithOrgContext)({ membership: actorMembership, organization: organization }, function () {
|
|
496
|
+
return assertCan({
|
|
497
|
+
access: _this.access,
|
|
498
|
+
organization: organization,
|
|
499
|
+
permissions: { organization: ["manageMembers"] },
|
|
500
|
+
user: user,
|
|
501
|
+
});
|
|
502
|
+
})];
|
|
503
|
+
case 3:
|
|
504
|
+
_d.sent();
|
|
505
|
+
email = typeof ((_a = req.body) === null || _a === void 0 ? void 0 : _a.email) === "string" ? req.body.email.trim() : "";
|
|
506
|
+
userId = typeof ((_b = req.body) === null || _b === void 0 ? void 0 : _b.userId) === "string" ? req.body.userId.trim() : "";
|
|
507
|
+
return [4 /*yield*/, findUserForAttach(this.userModel, { email: email, userId: userId })];
|
|
508
|
+
case 4:
|
|
509
|
+
target = _d.sent();
|
|
510
|
+
if (!target) {
|
|
511
|
+
throw new errors_1.NotFoundError("User not found");
|
|
512
|
+
}
|
|
513
|
+
return [4 /*yield*/, organizationModel_1.Membership.findOneOrNone({
|
|
514
|
+
organizationId: organization._id,
|
|
515
|
+
userId: userIdOf(target),
|
|
516
|
+
})];
|
|
517
|
+
case 5:
|
|
518
|
+
existing = _d.sent();
|
|
519
|
+
if (existing) {
|
|
520
|
+
throw new errors_1.ConflictError("User is already a member");
|
|
521
|
+
}
|
|
522
|
+
roleName = ((_c = req.body) === null || _c === void 0 ? void 0 : _c.roleName) === "org-admin" ? "org-admin" : "member";
|
|
523
|
+
return [4 /*yield*/, organizationModel_1.Membership.create({
|
|
524
|
+
organizationId: organization._id,
|
|
525
|
+
roleName: roleName,
|
|
526
|
+
userId: userIdOf(target),
|
|
527
|
+
})];
|
|
528
|
+
case 6:
|
|
529
|
+
created = _d.sent();
|
|
530
|
+
return [4 /*yield*/, emitOrgAudit({
|
|
531
|
+
event: {
|
|
532
|
+
actorId: user.id,
|
|
533
|
+
organizationId: String(organization._id),
|
|
534
|
+
organizationLabel: organization.name,
|
|
535
|
+
verb: "memberAttached",
|
|
536
|
+
},
|
|
537
|
+
onOrgAudit: this.onOrgAudit,
|
|
538
|
+
req: req,
|
|
539
|
+
})];
|
|
540
|
+
case 7:
|
|
541
|
+
_d.sent();
|
|
542
|
+
return [2 /*return*/, res.status(201).json({ data: created })];
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
}); }));
|
|
546
|
+
router.patch("/:id/members/:memberId", (0, auth_1.authenticateMiddleware)(), docs("Update a membership", {
|
|
547
|
+
roleName: {
|
|
548
|
+
description: "Membership role",
|
|
549
|
+
type: "string",
|
|
550
|
+
},
|
|
551
|
+
status: {
|
|
552
|
+
description: "Membership status",
|
|
553
|
+
type: "string",
|
|
554
|
+
},
|
|
555
|
+
}), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
556
|
+
var user, organization, actorMembership, member, nextRoleName, nextStatus;
|
|
557
|
+
var _this = this;
|
|
558
|
+
var _a, _b, _c, _d;
|
|
559
|
+
return __generator(this, function (_e) {
|
|
560
|
+
switch (_e.label) {
|
|
561
|
+
case 0:
|
|
562
|
+
user = requireUser(req);
|
|
563
|
+
return [4 /*yield*/, loadOrganization(pathParam(req.params.id, "id is required"))];
|
|
564
|
+
case 1:
|
|
565
|
+
organization = _e.sent();
|
|
566
|
+
(0, orgContext_1.assertOrganizationEnabled)(organization);
|
|
567
|
+
return [4 /*yield*/, loadActiveMembership(user, organization._id)];
|
|
568
|
+
case 2:
|
|
569
|
+
actorMembership = _e.sent();
|
|
570
|
+
return [4 /*yield*/, (0, orgContext_1.runWithOrgContext)({ membership: actorMembership, organization: organization }, function () {
|
|
571
|
+
return assertCan({
|
|
572
|
+
access: _this.access,
|
|
573
|
+
organization: organization,
|
|
574
|
+
permissions: { organization: ["manageMembers"] },
|
|
575
|
+
user: user,
|
|
576
|
+
});
|
|
577
|
+
})];
|
|
578
|
+
case 3:
|
|
579
|
+
_e.sent();
|
|
580
|
+
return [4 /*yield*/, organizationModel_1.Membership.findOneOrNone({
|
|
581
|
+
_id: pathParam(req.params.memberId, "memberId is required"),
|
|
582
|
+
organizationId: organization._id,
|
|
583
|
+
})];
|
|
584
|
+
case 4:
|
|
585
|
+
member = _e.sent();
|
|
586
|
+
if (!member) {
|
|
587
|
+
throw new errors_1.NotFoundError("Membership not found");
|
|
588
|
+
}
|
|
589
|
+
nextRoleName = typeof ((_a = req.body) === null || _a === void 0 ? void 0 : _a.roleName) === "string" ? req.body.roleName : member.roleName;
|
|
590
|
+
nextStatus = typeof ((_b = req.body) === null || _b === void 0 ? void 0 : _b.status) === "string" ? req.body.status : member.status;
|
|
591
|
+
return [4 /*yield*/, assertNotLastOrgAdmin(member, nextRoleName, nextStatus)];
|
|
592
|
+
case 5:
|
|
593
|
+
_e.sent();
|
|
594
|
+
if (typeof ((_c = req.body) === null || _c === void 0 ? void 0 : _c.roleName) === "string") {
|
|
595
|
+
member.roleName = req.body.roleName === "org-admin" ? "org-admin" : "member";
|
|
596
|
+
}
|
|
597
|
+
if (typeof ((_d = req.body) === null || _d === void 0 ? void 0 : _d.status) === "string") {
|
|
598
|
+
member.status = req.body.status === "suspended" ? "suspended" : "active";
|
|
599
|
+
}
|
|
600
|
+
return [4 /*yield*/, member.save()];
|
|
601
|
+
case 6:
|
|
602
|
+
_e.sent();
|
|
603
|
+
return [4 /*yield*/, emitOrgAudit({
|
|
604
|
+
event: {
|
|
605
|
+
actorId: user.id,
|
|
606
|
+
organizationId: String(organization._id),
|
|
607
|
+
organizationLabel: organization.name,
|
|
608
|
+
verb: "memberUpdated",
|
|
609
|
+
},
|
|
610
|
+
onOrgAudit: this.onOrgAudit,
|
|
611
|
+
req: req,
|
|
612
|
+
})];
|
|
613
|
+
case 7:
|
|
614
|
+
_e.sent();
|
|
615
|
+
return [2 /*return*/, res.json({ data: member })];
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}); }));
|
|
619
|
+
router.delete("/:id/members/:memberId", (0, auth_1.authenticateMiddleware)(), docs("Remove a membership"), (0, api_1.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
|
|
620
|
+
var user, organization, actorMembership, member;
|
|
621
|
+
var _this = this;
|
|
622
|
+
return __generator(this, function (_a) {
|
|
623
|
+
switch (_a.label) {
|
|
624
|
+
case 0:
|
|
625
|
+
user = requireUser(req);
|
|
626
|
+
return [4 /*yield*/, loadOrganization(pathParam(req.params.id, "id is required"))];
|
|
627
|
+
case 1:
|
|
628
|
+
organization = _a.sent();
|
|
629
|
+
(0, orgContext_1.assertOrganizationEnabled)(organization);
|
|
630
|
+
return [4 /*yield*/, loadActiveMembership(user, organization._id)];
|
|
631
|
+
case 2:
|
|
632
|
+
actorMembership = _a.sent();
|
|
633
|
+
return [4 /*yield*/, (0, orgContext_1.runWithOrgContext)({ membership: actorMembership, organization: organization }, function () {
|
|
634
|
+
return assertCan({
|
|
635
|
+
access: _this.access,
|
|
636
|
+
organization: organization,
|
|
637
|
+
permissions: { organization: ["manageMembers"] },
|
|
638
|
+
user: user,
|
|
639
|
+
});
|
|
640
|
+
})];
|
|
641
|
+
case 3:
|
|
642
|
+
_a.sent();
|
|
643
|
+
return [4 /*yield*/, organizationModel_1.Membership.findOneOrNone({
|
|
644
|
+
_id: pathParam(req.params.memberId, "memberId is required"),
|
|
645
|
+
organizationId: organization._id,
|
|
646
|
+
})];
|
|
647
|
+
case 4:
|
|
648
|
+
member = _a.sent();
|
|
649
|
+
if (!member) {
|
|
650
|
+
throw new errors_1.NotFoundError("Membership not found");
|
|
651
|
+
}
|
|
652
|
+
return [4 /*yield*/, assertNotLastOrgAdmin(member, "member", "suspended")];
|
|
653
|
+
case 5:
|
|
654
|
+
_a.sent();
|
|
655
|
+
return [4 /*yield*/, member.deleteOne()];
|
|
656
|
+
case 6:
|
|
657
|
+
_a.sent();
|
|
658
|
+
return [4 /*yield*/, emitOrgAudit({
|
|
659
|
+
event: {
|
|
660
|
+
actorId: user.id,
|
|
661
|
+
organizationId: String(organization._id),
|
|
662
|
+
organizationLabel: organization.name,
|
|
663
|
+
verb: "memberRemoved",
|
|
664
|
+
},
|
|
665
|
+
onOrgAudit: this.onOrgAudit,
|
|
666
|
+
req: req,
|
|
667
|
+
})];
|
|
668
|
+
case 7:
|
|
669
|
+
_a.sent();
|
|
670
|
+
return [2 /*return*/, res.status(204).send()];
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
}); }));
|
|
674
|
+
app.use(this.basePath, router);
|
|
675
|
+
};
|
|
676
|
+
return OrgsApp;
|
|
677
|
+
}());
|
|
678
|
+
exports.OrgsApp = OrgsApp;
|