@terreno/api 57.4.0 → 57.6.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/README.md +5 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/api.js +598 -852
- package/dist/audit/auditApp.js +48 -89
- package/dist/audit/auditEventModel.js +15 -16
- package/dist/audit/cloudTasksEnqueue.js +56 -149
- package/dist/audit/diff.js +70 -160
- package/dist/audit/processQueue.js +25 -70
- package/dist/audit/rbacSink.js +25 -66
- package/dist/audit/record.js +129 -251
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.js +32 -47
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.js +56 -150
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +2 -42
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +2 -42
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +1 -39
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +2 -42
- package/dist/migrations/generate.js +107 -229
- package/dist/migrations/load.js +75 -201
- package/dist/migrations/lock.js +54 -126
- package/dist/migrations/runner.js +192 -446
- package/dist/migrations/schemaCatalog.js +122 -263
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.js +6 -6
- package/dist/models/notificationPreference.js +6 -6
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.js +133 -273
- package/dist/notifications/notificationsApp.js +85 -179
- package/dist/notifications/notificationsBeforeSend.js +19 -64
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.js +107 -122
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.js +177 -286
- package/dist/orgs/orgContext.js +87 -157
- package/dist/orgs/orgPermissions.js +29 -68
- package/dist/orgs/orgPlugin.js +9 -27
- package/dist/orgs/organizationModel.js +65 -166
- package/dist/orgs/organizationSettings.js +29 -36
- package/dist/orgs/orgsApp.js +346 -608
- package/dist/permissions.js +88 -181
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.js +48 -99
- package/dist/rateLimit/mongoStore.js +54 -112
- package/dist/rateLimit/policies.js +41 -75
- package/dist/rateLimit/redisStore.js +39 -133
- package/dist/rbac/access.js +119 -267
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.js +28 -99
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.js +130 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +148 -342
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.js +44 -171
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.js +23 -91
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.js +489 -800
- package/dist/realtime/deprecation.js +8 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.js +247 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +179 -274
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.js +58 -170
- package/dist/sync/adminWindowAccess.js +15 -86
- package/dist/sync/adminWindowMutation.js +110 -181
- package/dist/sync/executors.js +425 -629
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.js +533 -801
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +29 -49
- package/dist/sync/routes.js +484 -817
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.js +253 -478
- package/dist/sync/streams.js +49 -117
- package/dist/sync/syncApp.js +12 -15
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.js +166 -276
- package/dist/tests/createTestData.js +146 -256
- package/dist/tests/models.js +14 -54
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.js +31 -93
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.js +9 -9
- package/dist/webhooks/rawBody.js +1 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +6 -6
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/audit/auditApp.test.ts +5 -5
- package/src/audit/auditEventModel.ts +7 -3
- package/src/audit/auditRouter.test.ts +5 -5
- package/src/audit/rbacSink.test.ts +2 -2
- package/src/audit/record.test.ts +2 -2
- package/src/audit/record.ts +3 -2
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/configurationPlugin.test.ts +1 -11
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +7 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/migrations/cli.test.ts +0 -23
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +3 -2
- package/src/models/notificationPreference.ts +1 -1
- package/src/notifications/notificationsApp.test.ts +24 -1
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/orgs/orgPlugin.ts +1 -1
- package/src/orgs/organizationModel.ts +11 -6
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/realtime/realtimeApp.ts +1 -1
- package/src/sync/integration.test.ts +2 -2
- package/src/terrenoApp.ts +3 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +4 -3
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +2 -0
package/dist/orgs/orgsApp.js
CHANGED
|
@@ -1,208 +1,127 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.OrgsApp = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
4
|
+
const express_1 = require("express");
|
|
5
|
+
const api_1 = require("../api");
|
|
6
|
+
const auth_1 = require("../auth");
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
const logger_1 = require("../logger");
|
|
9
|
+
const openApiBuilder_1 = require("../openApiBuilder");
|
|
10
|
+
const plugins_1 = require("../plugins");
|
|
11
|
+
const organizationModel_1 = require("./organizationModel");
|
|
12
|
+
const organizationSettings_1 = require("./organizationSettings");
|
|
13
|
+
const orgContext_1 = require("./orgContext");
|
|
14
|
+
const escapeRegularExpression = (value) => {
|
|
51
15
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
52
16
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}); };
|
|
66
|
-
var pathParam = function (value, title) {
|
|
17
|
+
const findUserForAttach = async (userModel, args) => {
|
|
18
|
+
if (args.email) {
|
|
19
|
+
return (0, plugins_1.findOneOrNoneFor)(userModel, {
|
|
20
|
+
email: { $options: "i", $regex: `^${escapeRegularExpression(args.email)}$` },
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (args.userId) {
|
|
24
|
+
return (0, plugins_1.findOneOrNoneFor)(userModel, { _id: args.userId });
|
|
25
|
+
}
|
|
26
|
+
throw new errors_1.APIError({ status: 400, title: "email or userId is required" });
|
|
27
|
+
};
|
|
28
|
+
const pathParam = (value, title) => {
|
|
67
29
|
if (typeof value !== "string" || value.length === 0) {
|
|
68
|
-
throw new errors_1.APIError({ status: 400, title
|
|
30
|
+
throw new errors_1.APIError({ status: 400, title });
|
|
69
31
|
}
|
|
70
32
|
return value;
|
|
71
33
|
};
|
|
72
|
-
|
|
34
|
+
const userIdOf = (user) => {
|
|
73
35
|
if (user._id) {
|
|
74
36
|
return String(user._id);
|
|
75
37
|
}
|
|
76
38
|
return user.id;
|
|
77
39
|
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (!
|
|
40
|
+
const requireUser = (req) => {
|
|
41
|
+
const user = req.user;
|
|
42
|
+
if (!user?.id) {
|
|
81
43
|
throw new errors_1.APIError({ status: 401, title: "Unauthorized" });
|
|
82
44
|
}
|
|
83
45
|
return user;
|
|
84
46
|
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
})];
|
|
47
|
+
const emitOrgAudit = async (args) => {
|
|
48
|
+
if (!args.onOrgAudit) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
await args.onOrgAudit(args.event, args.req);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
56
|
+
logger_1.logger.error(`onOrgAudit failed after ${args.event.verb}: ${detail}`);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const loadOrganization = async (id) => {
|
|
60
|
+
const organization = await organizationModel_1.Organization.findOneOrNone({ _id: id });
|
|
61
|
+
if (!organization) {
|
|
62
|
+
throw new errors_1.NotFoundError("Organization not found");
|
|
63
|
+
}
|
|
64
|
+
return organization;
|
|
65
|
+
};
|
|
66
|
+
const loadActiveMembership = async (user, organizationId) => {
|
|
67
|
+
const membership = await organizationModel_1.Membership.findOneOrNone({
|
|
68
|
+
organizationId,
|
|
69
|
+
status: "active",
|
|
70
|
+
userId: userIdOf(user),
|
|
145
71
|
});
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
return
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
}
|
|
72
|
+
return membership ?? undefined;
|
|
73
|
+
};
|
|
74
|
+
const countActiveOrgAdmins = async (organizationId) => {
|
|
75
|
+
return organizationModel_1.Membership.countDocuments({
|
|
76
|
+
organizationId,
|
|
77
|
+
roleName: "org-admin",
|
|
78
|
+
status: "active",
|
|
168
79
|
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
80
|
+
};
|
|
81
|
+
const assertNotLastOrgAdmin = async (membership, nextRoleName, nextStatus) => {
|
|
82
|
+
const remainsOrgAdmin = (nextRoleName ?? membership.roleName) === "org-admin" &&
|
|
83
|
+
(nextStatus ?? membership.status) === "active";
|
|
84
|
+
if (remainsOrgAdmin) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (membership.roleName !== "org-admin" || membership.status !== "active") {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const activeAdmins = await countActiveOrgAdmins(membership.organizationId);
|
|
91
|
+
if (activeAdmins <= 1) {
|
|
92
|
+
throw new errors_1.APIError({ status: 400, title: "Cannot remove the last org-admin" });
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const assertCan = async (args) => {
|
|
96
|
+
const result = await args.access.can({
|
|
97
|
+
doc: args.organization,
|
|
98
|
+
permissions: args.permissions,
|
|
99
|
+
user: args.user,
|
|
187
100
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
101
|
+
if (result.allowed) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
throw new errors_1.ForbiddenError(result.reason ?? "Access denied");
|
|
105
|
+
};
|
|
106
|
+
class OrgsApp {
|
|
107
|
+
access;
|
|
108
|
+
basePath;
|
|
109
|
+
onOrgAudit;
|
|
110
|
+
userModel;
|
|
111
|
+
constructor(options) {
|
|
192
112
|
this.access = options.access;
|
|
193
|
-
this.basePath =
|
|
113
|
+
this.basePath = options.basePath ?? "/orgs";
|
|
194
114
|
this.onOrgAudit = options.onOrgAudit;
|
|
195
115
|
this.userModel = options.userModel;
|
|
196
116
|
if (options.settingsSchema) {
|
|
197
117
|
(0, organizationSettings_1.registerOrganizationSettings)(options.settingsSchema);
|
|
198
118
|
}
|
|
199
119
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
var builder = (0, openApiBuilder_1.createOpenApiBuilder)({ openApi: openApiMiddleware })
|
|
120
|
+
register(app, openApi) {
|
|
121
|
+
const router = (0, express_1.Router)();
|
|
122
|
+
const openApiMiddleware = openApi;
|
|
123
|
+
const docs = (summary, requestBody) => {
|
|
124
|
+
const builder = (0, openApiBuilder_1.createOpenApiBuilder)({ openApi: openApiMiddleware })
|
|
206
125
|
.withTags(["organizations"])
|
|
207
126
|
.withSummary(summary);
|
|
208
127
|
if (requestBody) {
|
|
@@ -213,263 +132,154 @@ var OrgsApp = /** @class */ (function () {
|
|
|
213
132
|
router.post("/", (0, auth_1.authenticateMiddleware)(), this.access.middleware({ organization: ["create"] }), docs("Create an organization", {
|
|
214
133
|
name: { description: "Organization name", required: true, type: "string" },
|
|
215
134
|
settings: { description: "App-defined organization settings", type: "object" },
|
|
216
|
-
}), (0, api_1.asyncHandler)(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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
|
-
}
|
|
135
|
+
}), (0, api_1.asyncHandler)(async (req, res) => {
|
|
136
|
+
const user = requireUser(req);
|
|
137
|
+
const name = typeof req.body?.name === "string" ? req.body.name.trim() : "";
|
|
138
|
+
if (!name) {
|
|
139
|
+
throw new errors_1.APIError({ status: 400, title: "name is required" });
|
|
140
|
+
}
|
|
141
|
+
await (0, organizationModel_1.assertOrganizationSlugAvailable)((0, organizationModel_1.organizationSlugFromName)(name));
|
|
142
|
+
const organization = await organizationModel_1.Organization.create({
|
|
143
|
+
name,
|
|
144
|
+
ownerId: userIdOf(user),
|
|
145
|
+
settings: req.body?.settings,
|
|
262
146
|
});
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
}
|
|
147
|
+
await emitOrgAudit({
|
|
148
|
+
event: {
|
|
149
|
+
actorId: user.id,
|
|
150
|
+
organizationId: String(organization._id),
|
|
151
|
+
organizationLabel: organization.name,
|
|
152
|
+
verb: "created",
|
|
153
|
+
},
|
|
154
|
+
onOrgAudit: this.onOrgAudit,
|
|
155
|
+
req,
|
|
293
156
|
});
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
return
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
-
}
|
|
157
|
+
return res.status(201).json({ data: organization });
|
|
158
|
+
}));
|
|
159
|
+
router.get("/", (0, auth_1.authenticateMiddleware)(), this.access.middleware({ organization: ["list"] }), docs("List all organizations"), (0, api_1.asyncHandler)(async (_req, res) => {
|
|
160
|
+
const organizations = await organizationModel_1.Organization.find({}).sort({ name: 1 });
|
|
161
|
+
return res.json({ data: organizations });
|
|
162
|
+
}));
|
|
163
|
+
router.get("/mine", (0, auth_1.authenticateMiddleware)(), docs("List organizations the caller may switch into"), (0, api_1.asyncHandler)(async (req, res) => {
|
|
164
|
+
const user = requireUser(req);
|
|
165
|
+
if ((0, orgContext_1.isPlatformOrgActor)(user)) {
|
|
166
|
+
const organizations = await organizationModel_1.Organization.find({ disabled: { $ne: true } }).sort({ name: 1 });
|
|
167
|
+
return res.json({ data: organizations });
|
|
168
|
+
}
|
|
169
|
+
const memberships = await organizationModel_1.Membership.find({
|
|
170
|
+
roleName: "org-admin",
|
|
171
|
+
status: "active",
|
|
172
|
+
userId: userIdOf(user),
|
|
320
173
|
});
|
|
321
|
-
|
|
174
|
+
if (memberships.length === 0) {
|
|
175
|
+
throw new errors_1.ForbiddenError("Access denied");
|
|
176
|
+
}
|
|
177
|
+
const organizations = await organizationModel_1.Organization.find({
|
|
178
|
+
_id: { $in: memberships.map((membership) => membership.organizationId) },
|
|
179
|
+
disabled: { $ne: true },
|
|
180
|
+
}).sort({ name: 1 });
|
|
181
|
+
return res.json({ data: organizations });
|
|
182
|
+
}));
|
|
183
|
+
router.get("/:id", (0, auth_1.authenticateMiddleware)(), docs("Read an organization"), (0, api_1.asyncHandler)(async (req, res) => {
|
|
184
|
+
const user = requireUser(req);
|
|
185
|
+
const organization = await loadOrganization(pathParam(req.params.id, "id is required"));
|
|
186
|
+
const membership = await loadActiveMembership(user, organization._id);
|
|
187
|
+
await (0, orgContext_1.runWithOrgContext)({ membership, organization }, () => assertCan({
|
|
188
|
+
access: this.access,
|
|
189
|
+
organization,
|
|
190
|
+
permissions: { organization: ["read"] },
|
|
191
|
+
user,
|
|
192
|
+
}));
|
|
193
|
+
return res.json({ data: organization });
|
|
194
|
+
}));
|
|
322
195
|
router.patch("/:id", (0, auth_1.authenticateMiddleware)(), docs("Update an organization", {
|
|
323
196
|
disabled: { description: "Disable the organization", type: "boolean" },
|
|
324
197
|
name: { description: "Organization name", type: "string" },
|
|
325
198
|
settings: { description: "App-defined organization settings", type: "object" },
|
|
326
|
-
}), (0, api_1.asyncHandler)(
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
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 })];
|
|
199
|
+
}), (0, api_1.asyncHandler)(async (req, res) => {
|
|
200
|
+
const user = requireUser(req);
|
|
201
|
+
const organization = await loadOrganization(pathParam(req.params.id, "id is required"));
|
|
202
|
+
const membership = await loadActiveMembership(user, organization._id);
|
|
203
|
+
const body = req.body ?? {};
|
|
204
|
+
const wantsDisable = "disabled" in body && Boolean(body.disabled) && !organization.disabled;
|
|
205
|
+
await (0, orgContext_1.runWithOrgContext)({ membership, organization }, async () => {
|
|
206
|
+
await assertCan({
|
|
207
|
+
access: this.access,
|
|
208
|
+
organization,
|
|
209
|
+
permissions: { organization: ["update"] },
|
|
210
|
+
user,
|
|
211
|
+
});
|
|
212
|
+
if (wantsDisable) {
|
|
213
|
+
await assertCan({
|
|
214
|
+
access: this.access,
|
|
215
|
+
organization,
|
|
216
|
+
permissions: { organization: ["disable"] },
|
|
217
|
+
user,
|
|
218
|
+
});
|
|
406
219
|
}
|
|
407
220
|
});
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
})];
|
|
435
|
-
case 4:
|
|
436
|
-
_a.sent();
|
|
437
|
-
return [2 /*return*/, res.status(204).send()];
|
|
438
|
-
}
|
|
221
|
+
if (typeof body.name === "string" && body.name.trim()) {
|
|
222
|
+
const trimmedName = body.name.trim();
|
|
223
|
+
const slug = (0, organizationModel_1.organizationSlugFromName)(trimmedName);
|
|
224
|
+
await (0, organizationModel_1.assertOrganizationSlugAvailable)(slug, organization._id);
|
|
225
|
+
organization.name = trimmedName;
|
|
226
|
+
organization.slug = slug;
|
|
227
|
+
}
|
|
228
|
+
if ("settings" in body) {
|
|
229
|
+
organization.settings = body.settings;
|
|
230
|
+
}
|
|
231
|
+
if ("disabled" in body) {
|
|
232
|
+
organization.disabled = Boolean(body.disabled);
|
|
233
|
+
}
|
|
234
|
+
await organization.save();
|
|
235
|
+
if (wantsDisable) {
|
|
236
|
+
await organizationModel_1.Membership.updateMany({ organizationId: organization._id }, { status: "suspended" });
|
|
237
|
+
}
|
|
238
|
+
await emitOrgAudit({
|
|
239
|
+
event: {
|
|
240
|
+
actorId: user.id,
|
|
241
|
+
organizationId: String(organization._id),
|
|
242
|
+
organizationLabel: organization.name,
|
|
243
|
+
verb: wantsDisable ? "disabled" : "updated",
|
|
244
|
+
},
|
|
245
|
+
onOrgAudit: this.onOrgAudit,
|
|
246
|
+
req,
|
|
439
247
|
});
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
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
|
-
}
|
|
248
|
+
return res.json({ data: organization });
|
|
249
|
+
}));
|
|
250
|
+
router.delete("/:id", (0, auth_1.authenticateMiddleware)(), this.access.middleware({ organization: ["delete"] }), docs("Soft-delete an organization"), (0, api_1.asyncHandler)(async (req, res) => {
|
|
251
|
+
const user = requireUser(req);
|
|
252
|
+
const organization = await loadOrganization(pathParam(req.params.id, "id is required"));
|
|
253
|
+
organization.deleted = true;
|
|
254
|
+
await organization.save();
|
|
255
|
+
await organizationModel_1.Membership.updateMany({ organizationId: organization._id }, { status: "suspended" });
|
|
256
|
+
await emitOrgAudit({
|
|
257
|
+
event: {
|
|
258
|
+
actorId: user.id,
|
|
259
|
+
organizationId: String(organization._id),
|
|
260
|
+
organizationLabel: organization.name,
|
|
261
|
+
verb: "deleted",
|
|
262
|
+
},
|
|
263
|
+
onOrgAudit: this.onOrgAudit,
|
|
264
|
+
req,
|
|
471
265
|
});
|
|
472
|
-
|
|
266
|
+
return res.status(204).send();
|
|
267
|
+
}));
|
|
268
|
+
router.get("/:id/members", (0, auth_1.authenticateMiddleware)(), docs("List organization members"), (0, api_1.asyncHandler)(async (req, res) => {
|
|
269
|
+
const user = requireUser(req);
|
|
270
|
+
const organization = await loadOrganization(pathParam(req.params.id, "id is required"));
|
|
271
|
+
const membership = await loadActiveMembership(user, organization._id);
|
|
272
|
+
await (0, orgContext_1.runWithOrgContext)({ membership, organization }, () => assertCan({
|
|
273
|
+
access: this.access,
|
|
274
|
+
organization,
|
|
275
|
+
permissions: { organization: ["manageMembers"] },
|
|
276
|
+
user,
|
|
277
|
+
}));
|
|
278
|
+
const members = await organizationModel_1.Membership.find({ organizationId: organization._id })
|
|
279
|
+
.sort({ created: 1 })
|
|
280
|
+
.populate({ path: "userId", select: "email name" });
|
|
281
|
+
return res.json({ data: members });
|
|
282
|
+
}));
|
|
473
283
|
router.post("/:id/members", (0, auth_1.authenticateMiddleware)(), docs("Attach an existing user as a member", {
|
|
474
284
|
email: { description: "Existing user email", type: "string" },
|
|
475
285
|
roleName: {
|
|
@@ -477,72 +287,48 @@ var OrgsApp = /** @class */ (function () {
|
|
|
477
287
|
type: "string",
|
|
478
288
|
},
|
|
479
289
|
userId: { description: "Existing user id", type: "string" },
|
|
480
|
-
}), (0, api_1.asyncHandler)(
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
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
|
-
}
|
|
290
|
+
}), (0, api_1.asyncHandler)(async (req, res) => {
|
|
291
|
+
const user = requireUser(req);
|
|
292
|
+
const organization = await loadOrganization(pathParam(req.params.id, "id is required"));
|
|
293
|
+
(0, orgContext_1.assertOrganizationEnabled)(organization);
|
|
294
|
+
const actorMembership = await loadActiveMembership(user, organization._id);
|
|
295
|
+
await (0, orgContext_1.runWithOrgContext)({ membership: actorMembership, organization }, () => assertCan({
|
|
296
|
+
access: this.access,
|
|
297
|
+
organization,
|
|
298
|
+
permissions: { organization: ["manageMembers"] },
|
|
299
|
+
user,
|
|
300
|
+
}));
|
|
301
|
+
const email = typeof req.body?.email === "string" ? req.body.email.trim() : "";
|
|
302
|
+
const userId = typeof req.body?.userId === "string" ? req.body.userId.trim() : "";
|
|
303
|
+
const target = await findUserForAttach(this.userModel, { email, userId });
|
|
304
|
+
if (!target) {
|
|
305
|
+
throw new errors_1.NotFoundError("User not found");
|
|
306
|
+
}
|
|
307
|
+
const existing = await organizationModel_1.Membership.findOneOrNone({
|
|
308
|
+
organizationId: organization._id,
|
|
309
|
+
userId: userIdOf(target),
|
|
544
310
|
});
|
|
545
|
-
|
|
311
|
+
if (existing) {
|
|
312
|
+
throw new errors_1.ConflictError("User is already a member");
|
|
313
|
+
}
|
|
314
|
+
const roleName = req.body?.roleName === "org-admin" ? "org-admin" : "member";
|
|
315
|
+
const created = await organizationModel_1.Membership.create({
|
|
316
|
+
organizationId: organization._id,
|
|
317
|
+
roleName,
|
|
318
|
+
userId: userIdOf(target),
|
|
319
|
+
});
|
|
320
|
+
await emitOrgAudit({
|
|
321
|
+
event: {
|
|
322
|
+
actorId: user.id,
|
|
323
|
+
organizationId: String(organization._id),
|
|
324
|
+
organizationLabel: organization.name,
|
|
325
|
+
verb: "memberAttached",
|
|
326
|
+
},
|
|
327
|
+
onOrgAudit: this.onOrgAudit,
|
|
328
|
+
req,
|
|
329
|
+
});
|
|
330
|
+
return res.status(201).json({ data: created });
|
|
331
|
+
}));
|
|
546
332
|
router.patch("/:id/members/:memberId", (0, auth_1.authenticateMiddleware)(), docs("Update a membership", {
|
|
547
333
|
roleName: {
|
|
548
334
|
description: "Membership role",
|
|
@@ -552,127 +338,79 @@ var OrgsApp = /** @class */ (function () {
|
|
|
552
338
|
description: "Membership status",
|
|
553
339
|
type: "string",
|
|
554
340
|
},
|
|
555
|
-
}), (0, api_1.asyncHandler)(
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
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
|
-
}
|
|
341
|
+
}), (0, api_1.asyncHandler)(async (req, res) => {
|
|
342
|
+
const user = requireUser(req);
|
|
343
|
+
const organization = await loadOrganization(pathParam(req.params.id, "id is required"));
|
|
344
|
+
(0, orgContext_1.assertOrganizationEnabled)(organization);
|
|
345
|
+
const actorMembership = await loadActiveMembership(user, organization._id);
|
|
346
|
+
await (0, orgContext_1.runWithOrgContext)({ membership: actorMembership, organization }, () => assertCan({
|
|
347
|
+
access: this.access,
|
|
348
|
+
organization,
|
|
349
|
+
permissions: { organization: ["manageMembers"] },
|
|
350
|
+
user,
|
|
351
|
+
}));
|
|
352
|
+
const member = await organizationModel_1.Membership.findOneOrNone({
|
|
353
|
+
_id: pathParam(req.params.memberId, "memberId is required"),
|
|
354
|
+
organizationId: organization._id,
|
|
617
355
|
});
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
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
|
-
}
|
|
356
|
+
if (!member) {
|
|
357
|
+
throw new errors_1.NotFoundError("Membership not found");
|
|
358
|
+
}
|
|
359
|
+
const nextRoleName = typeof req.body?.roleName === "string" ? req.body.roleName : member.roleName;
|
|
360
|
+
const nextStatus = typeof req.body?.status === "string" ? req.body.status : member.status;
|
|
361
|
+
await assertNotLastOrgAdmin(member, nextRoleName, nextStatus);
|
|
362
|
+
if (typeof req.body?.roleName === "string") {
|
|
363
|
+
member.roleName = req.body.roleName === "org-admin" ? "org-admin" : "member";
|
|
364
|
+
}
|
|
365
|
+
if (typeof req.body?.status === "string") {
|
|
366
|
+
member.status = req.body.status === "suspended" ? "suspended" : "active";
|
|
367
|
+
}
|
|
368
|
+
await member.save();
|
|
369
|
+
await emitOrgAudit({
|
|
370
|
+
event: {
|
|
371
|
+
actorId: user.id,
|
|
372
|
+
organizationId: String(organization._id),
|
|
373
|
+
organizationLabel: organization.name,
|
|
374
|
+
verb: "memberUpdated",
|
|
375
|
+
},
|
|
376
|
+
onOrgAudit: this.onOrgAudit,
|
|
377
|
+
req,
|
|
672
378
|
});
|
|
673
|
-
|
|
379
|
+
return res.json({ data: member });
|
|
380
|
+
}));
|
|
381
|
+
router.delete("/:id/members/:memberId", (0, auth_1.authenticateMiddleware)(), docs("Remove a membership"), (0, api_1.asyncHandler)(async (req, res) => {
|
|
382
|
+
const user = requireUser(req);
|
|
383
|
+
const organization = await loadOrganization(pathParam(req.params.id, "id is required"));
|
|
384
|
+
(0, orgContext_1.assertOrganizationEnabled)(organization);
|
|
385
|
+
const actorMembership = await loadActiveMembership(user, organization._id);
|
|
386
|
+
await (0, orgContext_1.runWithOrgContext)({ membership: actorMembership, organization }, () => assertCan({
|
|
387
|
+
access: this.access,
|
|
388
|
+
organization,
|
|
389
|
+
permissions: { organization: ["manageMembers"] },
|
|
390
|
+
user,
|
|
391
|
+
}));
|
|
392
|
+
const member = await organizationModel_1.Membership.findOneOrNone({
|
|
393
|
+
_id: pathParam(req.params.memberId, "memberId is required"),
|
|
394
|
+
organizationId: organization._id,
|
|
395
|
+
});
|
|
396
|
+
if (!member) {
|
|
397
|
+
throw new errors_1.NotFoundError("Membership not found");
|
|
398
|
+
}
|
|
399
|
+
await assertNotLastOrgAdmin(member, "member", "suspended");
|
|
400
|
+
await member.deleteOne();
|
|
401
|
+
await emitOrgAudit({
|
|
402
|
+
event: {
|
|
403
|
+
actorId: user.id,
|
|
404
|
+
organizationId: String(organization._id),
|
|
405
|
+
organizationLabel: organization.name,
|
|
406
|
+
verb: "memberRemoved",
|
|
407
|
+
},
|
|
408
|
+
onOrgAudit: this.onOrgAudit,
|
|
409
|
+
req,
|
|
410
|
+
});
|
|
411
|
+
return res.status(204).send();
|
|
412
|
+
}));
|
|
674
413
|
app.use(this.basePath, router);
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
}());
|
|
414
|
+
}
|
|
415
|
+
}
|
|
678
416
|
exports.OrgsApp = OrgsApp;
|