@jskit-ai/workspaces-core 0.2.15 → 0.2.16
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/CHANGELOG.md +25 -0
- package/locales/en.json +0 -5
- package/package.json +26 -16
- package/patterns/workspace-server/PATTERN.md +9 -9
- package/src/server/WorkspacesFeature.js +28 -16
- package/src/server/WorkspacesIntegrationsProvider.js +5 -6
- package/src/server/common/repositories/workspaceInvitesRepository.js +69 -123
- package/src/server/common/repositories/workspaceMembershipsRepository.js +106 -146
- package/src/server/common/repositories/workspacesRepository.js +105 -113
- package/src/server/common/services/workspaceDirectoryService.js +39 -19
- package/src/server/common/support/realtimeServiceEvents.js +19 -17
- package/src/server/support/resolveWorkspace.js +1 -1
- package/src/server/support/workspaceCrud.js +1 -1
- package/src/server/types.js +14 -3
- package/src/server/workspaceDirectory/workspaceDirectoryActions.js +7 -7
- package/src/server/workspaceDirectory/workspaceDirectoryRoutes.js +6 -6
- package/src/server/workspaceMembers/workspaceMembersActions.js +31 -30
- package/src/server/workspaceMembers/workspaceMembersRoutes.js +10 -10
- package/src/server/workspaceMembers/workspaceMembersService.js +35 -24
- package/src/server/workspacePendingInvitations/workspacePendingInvitationsActions.js +4 -4
- package/src/server/workspacePendingInvitations/workspacePendingInvitationsRoutes.js +7 -7
- package/src/server/workspacePendingInvitations/workspacePendingInvitationsService.js +8 -11
- package/src/server/workspaceSettings/workspaceSettingsActions.js +4 -4
- package/src/server/workspaceSettings/workspaceSettingsRepository.js +19 -50
- package/src/server/workspaceSettings/workspaceSettingsRoutes.js +4 -4
- package/src/server/workspacesRuntime.js +40 -47
- package/src/shared/resources/workspaceAssistantOutputs.js +1 -1
- package/src/shared/resources/workspaceInvitesResource.js +12 -73
- package/src/shared/resources/workspaceMembersResource.js +3 -3
- package/src/shared/resources/workspaceMembershipsResource.js +10 -48
- package/src/shared/resources/workspacePendingInvitationsResource.js +3 -3
- package/src/shared/resources/workspaceResource.js +20 -17
- package/src/shared/resources/workspaceSettingsResource.js +16 -24
- package/src/test/WorkspaceRecordsFeature.js +27 -0
- package/test/exportsContract.test.js +2 -0
- package/test/featureRuntime.test.js +34 -41
- package/test/repositories.integration.test.js +264 -41
- package/test/repositoryContracts.test.js +52 -40
- package/test/resourcesCanonical.test.js +41 -38
- package/test/roleLabels.test.js +10 -10
- package/test/support/invitationTransactions.js +49 -47
- package/test/support/mysqlWorkspaces.js +129 -0
- package/test/usersRouteResources.test.js +17 -17
- package/test/workspaceDirectoryService.mysql.test.js +255 -0
- package/test/workspaceInvitesRepository.mysql.test.js +355 -0
- package/test/workspaceMembersResource.test.js +2 -2
- package/test/workspaceMembersService.mysql.test.js +223 -0
- package/test/workspaceMembersService.test.js +10 -10
- package/test/workspacePendingInvitationsResource.test.js +6 -6
- package/test/workspacePendingInvitationsService.mysql.test.js +292 -0
- package/test/workspacePendingInvitationsService.test.js +2 -2
- package/test/workspaceRecords.test.js +142 -0
- package/test/workspaceRecordsFeature.test.js +42 -0
- package/test/workspaceService.test.js +2 -2
- package/test/workspaceSettingsActions.test.js +26 -21
- package/test/workspaceSettingsRepository.test.js +66 -86
- package/test/workspaceSettingsResource.test.js +5 -5
- package/test/workspaceInternalCrudResources.test.js +0 -98
- package/test/workspaceInvitesRepository.test.js +0 -318
- package/test/workspaceMembershipsRepository.test.js +0 -329
- package/test/workspacesRepository.test.js +0 -410
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { joinOrOpenTransaction } from "@jskit-ai/database-runtime/shared";
|
|
2
1
|
import { normalizeLowerText, normalizeRecordId, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
2
|
import { encodeInviteTokenHash } from "@jskit-ai/auth-core/shared/inviteTokens";
|
|
4
3
|
import { resolveInviteTokenHash, buildInviteToken, hashInviteToken } from "@jskit-ai/auth-core/server/inviteTokens";
|
|
@@ -293,10 +292,6 @@ function createService({
|
|
|
293
292
|
|
|
294
293
|
async function prepareInvite(workspace, user, payload, options = {}) {
|
|
295
294
|
ensureWorkspaceInvitationsEnabled();
|
|
296
|
-
// readable-check: allow required-collaborators - platform object: a Knex transaction, which prepareInvite writes in
|
|
297
|
-
if (!options.trx) {
|
|
298
|
-
throw new TypeError("prepareInvite requires a managed transaction.");
|
|
299
|
-
}
|
|
300
295
|
const email = normalizeLowerText(payload.email);
|
|
301
296
|
const roleSid = normalizeLowerText(payload.roleSid || "member") || "member";
|
|
302
297
|
if (!assignableRoleIds.includes(roleSid)) {
|
|
@@ -306,6 +301,7 @@ function createService({
|
|
|
306
301
|
const token = buildInviteToken();
|
|
307
302
|
const tokenHash = hashInviteToken(token);
|
|
308
303
|
const expiresAt = new Date(Date.now() + inviteExpiresInMs).toISOString();
|
|
304
|
+
// The caller's managed transaction: the lock refuses a call outside one.
|
|
309
305
|
await workspaceInvitesRepository.lockWorkspaceForInvitations(workspace.id, options);
|
|
310
306
|
await workspaceInvitesRepository.expirePendingByWorkspaceIdAndEmail(workspace.id, email, options);
|
|
311
307
|
const createdInvite = await workspaceInvitesRepository.insert(
|
|
@@ -328,17 +324,20 @@ function createService({
|
|
|
328
324
|
return { createdInviteId, inviteTokenPreview: token };
|
|
329
325
|
}
|
|
330
326
|
|
|
327
|
+
// The committed pending invitation, read in a transaction of its own: one inside the caller's is refused, since
|
|
328
|
+
// delivery runs after the invitation's transaction commits.
|
|
329
|
+
function readInviteToDeliver(workspace, prepared, options) {
|
|
330
|
+
return workspaceInvitesRepository.withTransaction({ ...options, outermost: true }, (txOptions) =>
|
|
331
|
+
workspaceInvitesRepository.findPendingByIdForWorkspace(
|
|
332
|
+
prepared.createdInviteId,
|
|
333
|
+
workspace.id,
|
|
334
|
+
{ ...txOptions, includeWorkspace: true, lock: false }
|
|
335
|
+
));
|
|
336
|
+
}
|
|
337
|
+
|
|
331
338
|
async function sendInvite(workspace, user, prepared, options = {}) {
|
|
332
339
|
ensureWorkspaceInvitationsEnabled();
|
|
333
|
-
|
|
334
|
-
if (options.trx) {
|
|
335
|
-
throw new TypeError("sendInvite must run after the invitation transaction commits.");
|
|
336
|
-
}
|
|
337
|
-
const invite = await workspaceInvitesRepository.findPendingByIdForWorkspace(
|
|
338
|
-
prepared.createdInviteId,
|
|
339
|
-
workspace.id,
|
|
340
|
-
{ ...options, includeWorkspace: true, lock: false }
|
|
341
|
-
);
|
|
340
|
+
const invite = await readInviteToDeliver(workspace, prepared, options);
|
|
342
341
|
if (!invite) {
|
|
343
342
|
throw new AppError(404, "Invite not found or already handled.", { code: "workspaces.invite.notFoundOrHandled" });
|
|
344
343
|
}
|
|
@@ -374,12 +373,10 @@ function createService({
|
|
|
374
373
|
|
|
375
374
|
async function createInvite(workspace, user, payload, options = {}) {
|
|
376
375
|
ensureWorkspaceInvitationsEnabled();
|
|
377
|
-
//
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const prepared = await workspaceInvitesRepository.withTransaction((trx) =>
|
|
382
|
-
prepareInvite(workspace, user, payload, { ...options, trx })
|
|
376
|
+
// Preparation commits before delivery, so a call inside the caller's transaction is refused: use prepareInvite
|
|
377
|
+
// inside it, then sendInvite after its commit.
|
|
378
|
+
const prepared = await workspaceInvitesRepository.withTransaction({ ...options, outermost: true }, (txOptions) =>
|
|
379
|
+
prepareInvite(workspace, user, payload, txOptions)
|
|
383
380
|
);
|
|
384
381
|
return returnJsonApiData(await sendInvite(workspace, user, prepared, options));
|
|
385
382
|
}
|
|
@@ -391,8 +388,8 @@ function createService({
|
|
|
391
388
|
throw inviteIdRequiredError();
|
|
392
389
|
}
|
|
393
390
|
|
|
394
|
-
const revoke = async (
|
|
395
|
-
const transactionOptions = { ...
|
|
391
|
+
const revoke = async (txOptions) => {
|
|
392
|
+
const transactionOptions = { ...txOptions, lock: true, workspaceId: workspace.id };
|
|
396
393
|
const invite = await workspaceInvitesRepository.findPendingByIdForWorkspace(
|
|
397
394
|
normalizedInviteId,
|
|
398
395
|
workspace.id,
|
|
@@ -404,7 +401,7 @@ function createService({
|
|
|
404
401
|
await workspaceInvitesRepository.revokeById(normalizedInviteId, transactionOptions);
|
|
405
402
|
return normalizeRecordId(invite.id, { fallback: null });
|
|
406
403
|
};
|
|
407
|
-
const revokedInviteId = await
|
|
404
|
+
const revokedInviteId = await workspaceInvitesRepository.withTransaction(options, revoke);
|
|
408
405
|
|
|
409
406
|
const response = await listInvitesPayload(workspace, options);
|
|
410
407
|
return returnJsonApiData({
|
|
@@ -413,6 +410,19 @@ function createService({
|
|
|
413
410
|
});
|
|
414
411
|
}
|
|
415
412
|
|
|
413
|
+
/**
|
|
414
|
+
* The user an invite's email belongs to, as a list of their id, or none
|
|
415
|
+
* before that person signs up: the users an invite's realtime change reaches.
|
|
416
|
+
* @param {unknown} inviteId
|
|
417
|
+
* @param {object} [options]
|
|
418
|
+
* @returns {Promise<string[]>}
|
|
419
|
+
*/
|
|
420
|
+
async function inviteRecipientUserIds(inviteId, options = {}) {
|
|
421
|
+
const invite = await workspaceInvitesRepository.findById(inviteId, options);
|
|
422
|
+
const recipient = invite?.email ? await userProfilesRepository.findByEmail(invite.email, options) : null;
|
|
423
|
+
return recipient?.id ? [recipient.id] : [];
|
|
424
|
+
}
|
|
425
|
+
|
|
416
426
|
return Object.freeze({
|
|
417
427
|
listRoles,
|
|
418
428
|
listRolesResponse,
|
|
@@ -425,7 +435,8 @@ function createService({
|
|
|
425
435
|
createInvite,
|
|
426
436
|
prepareInvite,
|
|
427
437
|
sendInvite,
|
|
428
|
-
revokeInvite
|
|
438
|
+
revokeInvite,
|
|
439
|
+
inviteRecipientUserIds
|
|
429
440
|
});
|
|
430
441
|
}
|
|
431
442
|
|
|
@@ -2,8 +2,8 @@ import { pendingInvitationsAssistantOutput } from "../../shared/resources/worksp
|
|
|
2
2
|
import {
|
|
3
3
|
emptyInputValidator
|
|
4
4
|
} from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { workspaceMembersContract } from "../../shared/resources/workspaceMembersResource.js";
|
|
6
|
+
import { workspacePendingInvitationsContract } from "../../shared/resources/workspacePendingInvitationsResource.js";
|
|
7
7
|
import { createInviteDecisionEvents } from "../common/support/realtimeServiceEvents.js";
|
|
8
8
|
import { resolveActionUser } from "@jskit-ai/users-core/server/resolveActionUser";
|
|
9
9
|
|
|
@@ -21,7 +21,7 @@ function createWorkspacePendingInvitationsActions({ workspacePendingInvitationsS
|
|
|
21
21
|
permission: {
|
|
22
22
|
require: "none"
|
|
23
23
|
},
|
|
24
|
-
input: { schema:
|
|
24
|
+
input: { schema: workspacePendingInvitationsContract.operations.resolve.query.schema },
|
|
25
25
|
output: null,
|
|
26
26
|
extensions: {
|
|
27
27
|
assistant: {
|
|
@@ -70,7 +70,7 @@ function createWorkspacePendingInvitationsActions({ workspacePendingInvitationsS
|
|
|
70
70
|
permission: {
|
|
71
71
|
require: "authenticated"
|
|
72
72
|
},
|
|
73
|
-
input:
|
|
73
|
+
input: workspaceMembersContract.operations.redeemInvite.body,
|
|
74
74
|
output: null,
|
|
75
75
|
extensions: {
|
|
76
76
|
assistant: {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createJsonApiResourceRouteContract } from "@jskit-ai/http-runtime/shared/validators/jsonApiRouteTransport";
|
|
2
2
|
import { resolveActorId } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { workspaceMembersContract } from "../../shared/resources/workspaceMembersResource.js";
|
|
4
|
+
import { workspacePendingInvitationsContract } from "../../shared/resources/workspacePendingInvitationsResource.js";
|
|
5
5
|
import {
|
|
6
6
|
WORKSPACE_INVITATION_RESOLVE_TRANSPORT,
|
|
7
7
|
WORKSPACE_INVITE_REDEEM_TRANSPORT,
|
|
@@ -43,8 +43,8 @@ function registerWorkspacePendingInvitationsRoutes(router) {
|
|
|
43
43
|
},
|
|
44
44
|
...createJsonApiResourceRouteContract({
|
|
45
45
|
...WORKSPACE_INVITATION_RESOLVE_TRANSPORT,
|
|
46
|
-
query:
|
|
47
|
-
output:
|
|
46
|
+
query: workspacePendingInvitationsContract.operations.resolve.query,
|
|
47
|
+
output: workspacePendingInvitationsContract.operations.resolve.output,
|
|
48
48
|
outputKind: "record",
|
|
49
49
|
getRecordId: resolveInviteResolutionRecordId,
|
|
50
50
|
includeValidation400: true
|
|
@@ -70,7 +70,7 @@ function registerWorkspacePendingInvitationsRoutes(router) {
|
|
|
70
70
|
},
|
|
71
71
|
...createJsonApiResourceRouteContract({
|
|
72
72
|
...WORKSPACE_PENDING_INVITATIONS_TRANSPORT,
|
|
73
|
-
output:
|
|
73
|
+
output: workspacePendingInvitationsContract.operations.list.output,
|
|
74
74
|
outputKind: "record",
|
|
75
75
|
getRecordId: resolveAuthenticatedUserRecordId
|
|
76
76
|
})
|
|
@@ -94,8 +94,8 @@ function registerWorkspacePendingInvitationsRoutes(router) {
|
|
|
94
94
|
},
|
|
95
95
|
...createJsonApiResourceRouteContract({
|
|
96
96
|
...WORKSPACE_INVITE_REDEEM_TRANSPORT,
|
|
97
|
-
body:
|
|
98
|
-
output:
|
|
97
|
+
body: workspaceMembersContract.operations.redeemInvite.body,
|
|
98
|
+
output: workspaceMembersContract.operations.redeemInvite.output,
|
|
99
99
|
outputKind: "record",
|
|
100
100
|
getRecordId: resolveAuthenticatedUserRecordId,
|
|
101
101
|
includeValidation400: true
|
|
@@ -18,21 +18,18 @@ function createService({ workspaceInvitesRepository, workspaceMembershipsReposit
|
|
|
18
18
|
return label ? { roleLabel: label } : {};
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
// Acceptance and refusal own their transaction (outermost): related writes belong in the acceptance participant.
|
|
21
22
|
async function withInviteTransaction(user, token, options, work) {
|
|
22
|
-
// readable-check: allow required-collaborators - platform object: a Knex transaction, which acceptance and refusal open themselves
|
|
23
|
-
if (options.trx) {
|
|
24
|
-
throw new TypeError("Invitation acceptance/refusal owns its transaction; use the acceptance participant for related writes.");
|
|
25
|
-
}
|
|
26
23
|
// Locate outside the transaction to avoid a stale MySQL repeatable-read
|
|
27
24
|
// snapshot. The callback validates again under the workspace/invite locks.
|
|
28
25
|
const initial = await requirePendingInviteForUserByToken(user, token, options);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
// work receives the locking options its repository reads take, and the transaction's own options.
|
|
27
|
+
const result = await workspaceInvitesRepository.withTransaction({ ...options, outermost: true }, (txOptions) => work({
|
|
28
|
+
...txOptions,
|
|
32
29
|
lock: true,
|
|
33
30
|
workspaceId: initial.invite.workspaceId,
|
|
34
31
|
inviteId: initial.invite.id
|
|
35
|
-
}));
|
|
32
|
+
}, txOptions));
|
|
36
33
|
// Commit expiry revocation before reporting the rejection.
|
|
37
34
|
if (result.expired) {
|
|
38
35
|
throw Object.assign(new AppError(409, "Invitation has expired.", { code: "workspaces.invite.expired" }), { transactionOutcome: "committed" });
|
|
@@ -242,7 +239,7 @@ function createService({ workspaceInvitesRepository, workspaceMembershipsReposit
|
|
|
242
239
|
}
|
|
243
240
|
|
|
244
241
|
async function acceptInviteByToken({ user, token }, options = {}) {
|
|
245
|
-
return withInviteTransaction(user, token, options, async (transactionOptions) => {
|
|
242
|
+
return withInviteTransaction(user, token, options, async (transactionOptions, txOptions) => {
|
|
246
243
|
const resolved = await resolveInviteActionInput(
|
|
247
244
|
user,
|
|
248
245
|
token,
|
|
@@ -259,11 +256,11 @@ function createService({ workspaceInvitesRepository, workspaceMembershipsReposit
|
|
|
259
256
|
{ roleSid: resolvedInvite.invite.roleSid, status: "active" },
|
|
260
257
|
transactionOptions
|
|
261
258
|
);
|
|
262
|
-
// Repository locking options stay internal to this service.
|
|
259
|
+
// Repository locking options stay internal to this service: the participant gets the transaction's own options.
|
|
263
260
|
await acceptanceParticipant({
|
|
264
261
|
invite: Object.freeze({ ...resolvedInvite.invite }),
|
|
265
262
|
user: Object.freeze({ ...resolvedInvite.user })
|
|
266
|
-
},
|
|
263
|
+
}, txOptions);
|
|
267
264
|
await workspaceInvitesRepository.markAcceptedById(resolvedInvite.invite.id, transactionOptions);
|
|
268
265
|
return { decision: "accepted", workspaceId };
|
|
269
266
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { composeSchemaDefinitions } from "@jskit-ai/kernel/shared/validators";
|
|
2
|
-
import {
|
|
2
|
+
import { workspaceSettingsCrud } from "../../shared/resources/workspaceSettingsResource.js";
|
|
3
3
|
import { workspaceSlugParamsValidator } from "../common/validators/routeParamsValidator.js";
|
|
4
4
|
import { createWorkspaceEntityAndBootstrapEvents } from "../common/support/realtimeServiceEvents.js";
|
|
5
5
|
import { resolveWorkspace } from "../support/resolveWorkspace.js";
|
|
@@ -12,7 +12,7 @@ const WORKSPACE_SETTINGS_CHANGED_EVENTS = createWorkspaceEntityAndBootstrapEvent
|
|
|
12
12
|
|
|
13
13
|
const workspaceSettingsUpdateInputValidator = composeSchemaDefinitions([
|
|
14
14
|
workspaceSlugParamsValidator,
|
|
15
|
-
|
|
15
|
+
workspaceSettingsCrud.operations.patch.body
|
|
16
16
|
], {
|
|
17
17
|
mode: "patch",
|
|
18
18
|
context: "workspaceSettingsActions.workspaceSettingsUpdateInputValidator"
|
|
@@ -36,7 +36,7 @@ function createWorkspaceSettingsActions({ workspaceSettingsService }) {
|
|
|
36
36
|
extensions: {
|
|
37
37
|
assistant: {
|
|
38
38
|
description: "Read the active workspace appearance and invitation settings.",
|
|
39
|
-
output:
|
|
39
|
+
output: workspaceSettingsCrud.operations.view.output,
|
|
40
40
|
transformResult: (result) => result.value
|
|
41
41
|
}
|
|
42
42
|
},
|
|
@@ -60,7 +60,7 @@ function createWorkspaceSettingsActions({ workspaceSettingsService }) {
|
|
|
60
60
|
extensions: {
|
|
61
61
|
assistant: {
|
|
62
62
|
description: "Update the active workspace appearance and invitation settings.",
|
|
63
|
-
output:
|
|
63
|
+
output: workspaceSettingsCrud.operations.patch.output,
|
|
64
64
|
transformResult: (result) => result.value
|
|
65
65
|
}
|
|
66
66
|
},
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import { isLostDuplicateInsertRace
|
|
2
|
-
import {
|
|
3
|
-
createJsonRestContext,
|
|
4
|
-
extractJsonRestCollectionRows
|
|
5
|
-
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
1
|
+
import { isLostDuplicateInsertRace } from "@jskit-ai/database-runtime/shared";
|
|
6
2
|
import { pickOwnProperties } from "@jskit-ai/kernel/shared/support";
|
|
7
3
|
import { normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
8
4
|
import { WORKSPACE_SETTINGS_FIELD_KEYS } from "../../shared/resources/workspaceSettingsResource.js";
|
|
@@ -26,38 +22,24 @@ function buildDefaultWorkspaceSettingsCreatePayload(workspaceId, defaultInvitesE
|
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
/**
|
|
25
|
+
* The workspaces' settings records, `repositories.workspaceSettings` of
|
|
26
|
+
* `workspaces.core`. Every method takes the caller's options last and joins
|
|
27
|
+
* their transaction.
|
|
29
28
|
* @param {object} dependencies
|
|
30
|
-
* @param {import("@jskit-ai/
|
|
29
|
+
* @param {import("@jskit-ai/resource-runtime/server/types").ResourceHandle} dependencies.workspaceSettings
|
|
30
|
+
* The workspace_settings table's handle.
|
|
31
31
|
* @param {boolean} dependencies.defaultInvitesEnabled
|
|
32
32
|
* @param {object} dependencies.defaultColorSettings The configured colors that replace the theme's.
|
|
33
33
|
*/
|
|
34
|
-
function createRepository({
|
|
35
|
-
const withTransaction = (work) => retryDeadlocks(() => api.transaction(work));
|
|
36
|
-
|
|
37
|
-
async function queryFirst(filters = {}, options = {}) {
|
|
38
|
-
const rows = extractJsonRestCollectionRows(
|
|
39
|
-
await api.resources.workspaceSettings.query(
|
|
40
|
-
{
|
|
41
|
-
queryParams: {
|
|
42
|
-
filters
|
|
43
|
-
},
|
|
44
|
-
transaction: resolveTransaction(options),
|
|
45
|
-
format: "plain"
|
|
46
|
-
},
|
|
47
|
-
createJsonRestContext(options.context)
|
|
48
|
-
)
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
return rows[0] || null;
|
|
52
|
-
}
|
|
53
|
-
|
|
34
|
+
function createRepository({ workspaceSettings, defaultInvitesEnabled, defaultColorSettings }) {
|
|
54
35
|
async function findByWorkspaceId(workspaceId, options = {}) {
|
|
55
36
|
const normalizedWorkspaceId = normalizeRecordId(workspaceId, { fallback: null });
|
|
56
37
|
if (!normalizedWorkspaceId) {
|
|
57
38
|
return null;
|
|
58
39
|
}
|
|
59
40
|
|
|
60
|
-
|
|
41
|
+
const { records } = await workspaceSettings.query({ filters: { id: normalizedWorkspaceId }, page: { size: 1 } }, options);
|
|
42
|
+
return records[0] || null;
|
|
61
43
|
}
|
|
62
44
|
|
|
63
45
|
async function ensureForWorkspaceId(workspaceId, options = {}) {
|
|
@@ -72,20 +54,12 @@ function createRepository({ api, defaultInvitesEnabled, defaultColorSettings })
|
|
|
72
54
|
}
|
|
73
55
|
|
|
74
56
|
try {
|
|
75
|
-
await
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
normalizedWorkspaceId,
|
|
79
|
-
defaultInvitesEnabled,
|
|
80
|
-
defaultColorSettings
|
|
81
|
-
),
|
|
82
|
-
format: "plain",
|
|
83
|
-
returning: "full",
|
|
84
|
-
transaction: resolveTransaction(options)
|
|
85
|
-
},
|
|
86
|
-
createJsonRestContext(options.context)
|
|
57
|
+
await workspaceSettings.create(
|
|
58
|
+
buildDefaultWorkspaceSettingsCreatePayload(normalizedWorkspaceId, defaultInvitesEnabled, defaultColorSettings),
|
|
59
|
+
{ ...options, returning: "none" }
|
|
87
60
|
);
|
|
88
61
|
} catch (error) {
|
|
62
|
+
// A standalone insert that lost to another one reads the winner; inside the caller's transaction it fails it.
|
|
89
63
|
if (!isLostDuplicateInsertRace(error, options)) {
|
|
90
64
|
throw error;
|
|
91
65
|
}
|
|
@@ -108,25 +82,20 @@ function createRepository({ api, defaultInvitesEnabled, defaultColorSettings })
|
|
|
108
82
|
return findByWorkspaceId(normalizedWorkspaceId, options);
|
|
109
83
|
}
|
|
110
84
|
|
|
111
|
-
await
|
|
85
|
+
await workspaceSettings.patch(
|
|
86
|
+
normalizedWorkspaceId,
|
|
112
87
|
{
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
...updatePayload,
|
|
116
|
-
updatedAt: new Date().toISOString()
|
|
117
|
-
},
|
|
118
|
-
format: "plain",
|
|
119
|
-
returning: "full",
|
|
120
|
-
transaction: resolveTransaction(options)
|
|
88
|
+
...updatePayload,
|
|
89
|
+
updatedAt: new Date().toISOString()
|
|
121
90
|
},
|
|
122
|
-
|
|
91
|
+
{ ...options, returning: "none" }
|
|
123
92
|
);
|
|
124
93
|
|
|
125
94
|
return findByWorkspaceId(normalizedWorkspaceId, options);
|
|
126
95
|
}
|
|
127
96
|
|
|
128
97
|
return Object.freeze({
|
|
129
|
-
withTransaction,
|
|
98
|
+
withTransaction: workspaceSettings.withTransaction,
|
|
130
99
|
findByWorkspaceId,
|
|
131
100
|
ensureForWorkspaceId,
|
|
132
101
|
updateSettingsByWorkspaceId
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createJsonApiResourceRouteContract } from "@jskit-ai/http-runtime/shared/validators/jsonApiRouteTransport";
|
|
2
2
|
import { WORKSPACE_SETTINGS_TRANSPORT } from "../../shared/jsonApiTransports.js";
|
|
3
|
-
import {
|
|
3
|
+
import { workspaceSettingsCrud } from "../../shared/resources/workspaceSettingsResource.js";
|
|
4
4
|
import { resolveWorkspaceRecordId } from "../common/support/workspaceRecordId.js";
|
|
5
5
|
import { resolveWorkspaceRoutePath } from "../support/workspaceRoutePaths.js";
|
|
6
6
|
import { workspaceSlugParamsValidator } from "../common/validators/routeParamsValidator.js";
|
|
@@ -27,7 +27,7 @@ function registerWorkspaceSettingsRoutes(router, { config }) {
|
|
|
27
27
|
params: workspaceSlugParamsValidator,
|
|
28
28
|
...createJsonApiResourceRouteContract({
|
|
29
29
|
...WORKSPACE_SETTINGS_TRANSPORT,
|
|
30
|
-
output:
|
|
30
|
+
output: workspaceSettingsCrud.operations.view.output,
|
|
31
31
|
outputKind: "record",
|
|
32
32
|
getRecordId: resolveWorkspaceRecordId
|
|
33
33
|
})
|
|
@@ -57,8 +57,8 @@ function registerWorkspaceSettingsRoutes(router, { config }) {
|
|
|
57
57
|
params: workspaceSlugParamsValidator,
|
|
58
58
|
...createJsonApiResourceRouteContract({
|
|
59
59
|
...WORKSPACE_SETTINGS_TRANSPORT,
|
|
60
|
-
body:
|
|
61
|
-
output:
|
|
60
|
+
body: workspaceSettingsCrud.operations.patch.body,
|
|
61
|
+
output: workspaceSettingsCrud.operations.patch.output,
|
|
62
62
|
outputKind: "record",
|
|
63
63
|
getRecordId: resolveWorkspaceRecordId,
|
|
64
64
|
includeValidation400: true
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
createJsonRestResourceScopeOptions
|
|
5
|
-
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
1
|
+
import { createWorkspaceRoleCatalog } from "../shared/roles.js";
|
|
2
|
+
import { WORKSPACE_SETTINGS_COLOR_FIELDS, normalizeWorkspaceHexColor } from "../shared/settings.js";
|
|
3
|
+
import { resolveTenancyProfile } from "../shared/tenancyProfile.js";
|
|
6
4
|
import { workspaceInvitesResource } from "../shared/resources/workspaceInvitesResource.js";
|
|
7
5
|
import { workspaceMembershipsResource } from "../shared/resources/workspaceMembershipsResource.js";
|
|
8
6
|
import { workspaceResource } from "../shared/resources/workspaceResource.js";
|
|
9
7
|
import { workspaceSettingsResource } from "../shared/resources/workspaceSettingsResource.js";
|
|
10
|
-
import { createWorkspaceRoleCatalog } from "../shared/roles.js";
|
|
11
|
-
import { WORKSPACE_SETTINGS_COLOR_FIELDS, normalizeWorkspaceHexColor } from "../shared/settings.js";
|
|
12
|
-
import { resolveTenancyProfile } from "../shared/tenancyProfile.js";
|
|
13
8
|
import { createRepository as createWorkspaceInvitesRepository } from "./common/repositories/workspaceInvitesRepository.js";
|
|
14
9
|
import { createRepository as createWorkspaceMembershipsRepository } from "./common/repositories/workspaceMembershipsRepository.js";
|
|
15
10
|
import { createRepository as createWorkspacesRepository } from "./common/repositories/workspacesRepository.js";
|
|
@@ -55,34 +50,27 @@ function requireWorkspaceColorDefaults(defaults) {
|
|
|
55
50
|
return colors;
|
|
56
51
|
}
|
|
57
52
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
createJsonRestResourceScopeOptions(workspaceInvitesResource, scopeOptions)
|
|
75
|
-
);
|
|
76
|
-
await addResourceIfMissing(
|
|
77
|
-
jsonRestApi,
|
|
78
|
-
"workspaceSettings",
|
|
79
|
-
createJsonRestResourceScopeOptions(workspaceSettingsResource, scopeOptions)
|
|
80
|
-
);
|
|
53
|
+
// The workspaces, workspace_memberships, workspace_invites and workspace_settings tables, which workspaces.core owns.
|
|
54
|
+
const WORKSPACES_TABLES = Object.freeze([workspaceResource, workspaceMembershipsResource, workspaceInvitesResource, workspaceSettingsResource]);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The workspaces and their memberships, the `workspaces` and
|
|
58
|
+
* `workspaces.memberships` capabilities. The workspace services are built on them.
|
|
59
|
+
* @param {import("./types.js").WorkspacesResources} resources The handles of the tables `workspaces.core` owns.
|
|
60
|
+
*/
|
|
61
|
+
function createWorkspaceRecords(resources) {
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
workspaces: createWorkspacesRepository({
|
|
64
|
+
workspaces: resources.workspaces,
|
|
65
|
+
workspaceMemberships: resources.workspaceMemberships
|
|
66
|
+
}),
|
|
67
|
+
memberships: createWorkspaceMembershipsRepository({ workspaceMemberships: resources.workspaceMemberships })
|
|
68
|
+
});
|
|
81
69
|
}
|
|
82
70
|
|
|
83
71
|
/**
|
|
84
|
-
* The `workspaces.core` capability: the workspace
|
|
85
|
-
* built from the runtime config.
|
|
72
|
+
* The `workspaces.core` capability: the workspace services and the invite and
|
|
73
|
+
* settings repositories, built from the runtime config.
|
|
86
74
|
* @param {object} dependencies
|
|
87
75
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeAppRoot} dependencies.appRoot
|
|
88
76
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeConfig} dependencies.config
|
|
@@ -91,8 +79,11 @@ async function installWorkspaceResources(jsonRestApi) {
|
|
|
91
79
|
* @param {import("./types.js").WorkspaceInviteEmailTemplate} dependencies.inviteEmailTemplate
|
|
92
80
|
* @param {import("./types.js").WorkspaceInviteAcceptanceParticipant} dependencies.inviteAcceptanceParticipant
|
|
93
81
|
* @param {import("./types.js").PersonalWorkspaceName} dependencies.personalWorkspaceName
|
|
94
|
-
* @param {import("
|
|
95
|
-
* @param {import("@jskit-ai/users-core/server/types").
|
|
82
|
+
* @param {import("./types.js").WorkspacesResources} dependencies.resources The handles of the tables `workspaces.core` owns.
|
|
83
|
+
* @param {import("@jskit-ai/users-core/server/types").UsersProfiles} dependencies.userProfiles
|
|
84
|
+
* @param {import("@jskit-ai/users-core/server/types").UsersSettings} dependencies.userSettings
|
|
85
|
+
* @param {import("./types.js").Workspaces} dependencies.workspaces
|
|
86
|
+
* @param {import("./types.js").WorkspacesMemberships} dependencies.workspaceMemberships
|
|
96
87
|
*/
|
|
97
88
|
function createWorkspacesRuntime({
|
|
98
89
|
appRoot,
|
|
@@ -102,24 +93,28 @@ function createWorkspacesRuntime({
|
|
|
102
93
|
inviteEmailTemplate,
|
|
103
94
|
inviteAcceptanceParticipant,
|
|
104
95
|
personalWorkspaceName,
|
|
105
|
-
|
|
106
|
-
|
|
96
|
+
resources,
|
|
97
|
+
userProfiles,
|
|
98
|
+
userSettings,
|
|
99
|
+
workspaces: workspacesRepository,
|
|
100
|
+
workspaceMemberships: workspaceMembershipsRepository
|
|
107
101
|
}) {
|
|
108
102
|
const tenancyProfile = resolveTenancyProfile(config);
|
|
109
103
|
const invitationsPolicy = resolveWorkspaceInvitationsPolicy({ appConfig: config, tenancyProfile });
|
|
110
104
|
const invitationsEnabled = invitationsPolicy.enabled === true;
|
|
111
105
|
const roleCatalog = createWorkspaceRoleCatalog(config);
|
|
112
106
|
const workspaceSettingsRepository = createWorkspaceSettingsRepository({
|
|
113
|
-
|
|
107
|
+
workspaceSettings: resources.workspaceSettings,
|
|
114
108
|
defaultInvitesEnabled: requireBoolean(
|
|
115
109
|
config?.workspaceSettings?.defaults?.invitesEnabled,
|
|
116
110
|
"runtime.config.workspaceSettings.defaults.invitesEnabled"
|
|
117
111
|
),
|
|
118
112
|
defaultColorSettings: requireWorkspaceColorDefaults(config?.workspaceSettings?.defaults)
|
|
119
113
|
});
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
114
|
+
const workspaceInvitesRepository = createWorkspaceInvitesRepository({
|
|
115
|
+
workspaceInvites: resources.workspaceInvites,
|
|
116
|
+
workspaces: workspacesRepository
|
|
117
|
+
});
|
|
123
118
|
const workspaceService = createWorkspaceService({
|
|
124
119
|
tenancyProfile,
|
|
125
120
|
roleCatalog,
|
|
@@ -147,8 +142,8 @@ function createWorkspacesRuntime({
|
|
|
147
142
|
workspaceInviteMailer: inviteMailer,
|
|
148
143
|
workspaceInviteEmailTemplate: inviteEmailTemplate,
|
|
149
144
|
i18n: createWorkspacesServerI18n({ appRoot }),
|
|
150
|
-
userProfilesRepository:
|
|
151
|
-
userSettingsRepository:
|
|
145
|
+
userProfilesRepository: userProfiles,
|
|
146
|
+
userSettingsRepository: userSettings
|
|
152
147
|
});
|
|
153
148
|
const workspaceSettingsService = createWorkspaceSettingsService({
|
|
154
149
|
workspaceSettingsRepository,
|
|
@@ -169,9 +164,7 @@ function createWorkspacesRuntime({
|
|
|
169
164
|
tenancyProfile,
|
|
170
165
|
repositories: Object.freeze({
|
|
171
166
|
workspaceInvites: workspaceInvitesRepository,
|
|
172
|
-
|
|
173
|
-
workspaceSettings: workspaceSettingsRepository,
|
|
174
|
-
workspaces: workspacesRepository
|
|
167
|
+
workspaceSettings: workspaceSettingsRepository
|
|
175
168
|
}),
|
|
176
169
|
services: Object.freeze({
|
|
177
170
|
directory: workspaceService,
|
|
@@ -182,4 +175,4 @@ function createWorkspacesRuntime({
|
|
|
182
175
|
});
|
|
183
176
|
}
|
|
184
177
|
|
|
185
|
-
export {
|
|
178
|
+
export { WORKSPACES_TABLES, createWorkspaceRecords, createWorkspacesRuntime };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createSchema } from "json-rest-schema";
|
|
2
|
-
import { createSchemaDefinition } from "@jskit-ai/resource-core/shared/
|
|
2
|
+
import { createSchemaDefinition } from "@jskit-ai/resource-core/shared/apiContract";
|
|
3
3
|
|
|
4
4
|
// Deliberate assistant allowlists: HTTP invitation responses also contain credentials.
|
|
5
5
|
const workspaceInviteAssistantOutput = createSchemaDefinition(createSchema({
|