@jskit-ai/workspaces-core 0.1.14 → 0.1.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.
Files changed (83) hide show
  1. package/package.descriptor.mjs +2 -2
  2. package/package.json +18 -3
  3. package/src/server/WorkspacesCoreServiceProvider.js +41 -2
  4. package/src/server/common/contributors/workspaceActionContextContributor.js +88 -0
  5. package/src/server/common/contributors/workspaceAuthPolicyContextResolver.js +34 -0
  6. package/src/server/common/contributors/workspaceRouteVisibilityResolver.js +78 -0
  7. package/src/server/common/formatters/workspaceFormatter.js +53 -0
  8. package/src/server/common/repositories/repositoryUtils.js +59 -0
  9. package/src/server/common/repositories/workspaceInvitesRepository.js +208 -0
  10. package/src/server/common/repositories/workspaceMembershipsRepository.js +190 -0
  11. package/src/server/common/repositories/workspacesRepository.js +202 -0
  12. package/src/server/common/services/workspaceContextService.js +281 -0
  13. package/src/server/common/support/deepFreeze.js +1 -0
  14. package/src/server/common/support/realtimeServiceEvents.js +91 -0
  15. package/src/server/common/support/resolveActionUser.js +9 -0
  16. package/src/server/common/support/workspaceRoutePaths.js +18 -0
  17. package/src/server/common/validators/authenticatedUserValidator.js +43 -0
  18. package/src/server/common/validators/routeParamsValidator.js +62 -0
  19. package/src/server/registerWorkspaceBootstrap.js +27 -0
  20. package/src/server/registerWorkspaceCore.js +100 -0
  21. package/src/server/registerWorkspaceRepositories.js +26 -0
  22. package/src/server/support/resolveWorkspace.js +16 -0
  23. package/src/server/support/workspaceActionSurfaces.js +118 -0
  24. package/src/server/support/workspaceInvitationsPolicy.js +45 -0
  25. package/src/server/support/workspaceRouteInput.js +22 -0
  26. package/src/server/workspaceBootstrapContributor.js +233 -0
  27. package/src/server/workspaceDirectory/bootWorkspaceDirectoryRoutes.js +133 -0
  28. package/src/server/workspaceDirectory/registerWorkspaceDirectory.js +19 -0
  29. package/src/server/workspaceDirectory/workspaceDirectoryActions.js +133 -0
  30. package/src/server/workspaceMembers/bootWorkspaceMembers.js +236 -0
  31. package/src/server/workspaceMembers/registerWorkspaceMembers.js +108 -0
  32. package/src/server/workspaceMembers/workspaceMembersActions.js +186 -0
  33. package/src/server/workspaceMembers/workspaceMembersService.js +222 -0
  34. package/src/server/workspacePendingInvitations/bootWorkspacePendingInvitations.js +62 -0
  35. package/src/server/workspacePendingInvitations/registerWorkspacePendingInvitations.js +119 -0
  36. package/src/server/workspacePendingInvitations/workspacePendingInvitationsActions.js +74 -0
  37. package/src/server/workspacePendingInvitations/workspacePendingInvitationsService.js +138 -0
  38. package/src/server/workspaceSettings/bootWorkspaceSettings.js +76 -0
  39. package/src/server/workspaceSettings/registerWorkspaceSettings.js +62 -0
  40. package/src/server/workspaceSettings/workspaceSettingsActions.js +72 -0
  41. package/src/server/workspaceSettings/workspaceSettingsRepository.js +154 -0
  42. package/src/server/workspaceSettings/workspaceSettingsService.js +66 -0
  43. package/src/shared/operationMessages.js +16 -0
  44. package/src/shared/resources/resolveGlobalArrayRegistry.js +6 -0
  45. package/src/shared/resources/workspaceMembersResource.js +354 -0
  46. package/src/shared/resources/workspacePendingInvitationsResource.js +82 -0
  47. package/src/shared/resources/workspaceResource.js +176 -0
  48. package/src/shared/resources/workspaceSettingsFields.js +59 -0
  49. package/src/shared/resources/workspaceSettingsResource.js +169 -0
  50. package/src/shared/roles.js +161 -0
  51. package/src/shared/settings.js +119 -0
  52. package/src/shared/support/workspacePathModel.js +145 -0
  53. package/src/shared/tenancyMode.js +35 -0
  54. package/src/shared/tenancyProfile.js +73 -0
  55. package/templates/packages/main/src/shared/resources/workspaceSettingsFields.js +2 -2
  56. package/test/registerServiceRealtimeEvents.test.js +116 -0
  57. package/test/registerWorkspaceDirectory.test.js +31 -0
  58. package/test/registerWorkspaceSettings.test.js +40 -0
  59. package/test/repositoryContracts.test.js +34 -0
  60. package/test/resourcesCanonical.test.js +74 -0
  61. package/test/roles.test.js +159 -0
  62. package/test/routeParamsValidator.test.js +49 -0
  63. package/test/settingsFieldRegistriesSingleton.test.js +14 -0
  64. package/test/tenancyProfile.test.js +67 -0
  65. package/test/usersRouteResources.test.js +97 -0
  66. package/test/workspaceActionContextContributor.test.js +344 -0
  67. package/test/workspaceActionSurfaces.test.js +85 -0
  68. package/test/workspaceAuthPolicyContextResolver.test.js +119 -0
  69. package/test/workspaceBootstrapContributor.test.js +169 -0
  70. package/test/workspaceInvitationsPolicy.test.js +71 -0
  71. package/test/workspaceInvitesRepository.test.js +111 -0
  72. package/test/workspaceMembersService.test.js +398 -0
  73. package/test/workspacePathModel.test.js +93 -0
  74. package/test/workspacePendingInvitationsResource.test.js +38 -0
  75. package/test/workspacePendingInvitationsService.test.js +151 -0
  76. package/test/workspaceRouteVisibilityResolver.test.js +83 -0
  77. package/test/workspaceService.test.js +546 -0
  78. package/test/workspaceSettingsActions.test.js +52 -0
  79. package/test/workspaceSettingsRepository.test.js +202 -0
  80. package/test/workspaceSettingsResource.test.js +169 -0
  81. package/test/workspaceSettingsService.test.js +140 -0
  82. package/test/workspacesRouteRequestInputValidator.test.js +5 -5
  83. package/test-support/registerDefaultSettingsFields.js +1 -0
@@ -0,0 +1,62 @@
1
+ import { withActionDefaults } from "@jskit-ai/kernel/shared/actions";
2
+ import { resolveAppConfig } from "@jskit-ai/kernel/server/support";
3
+ import { deepFreeze } from "../common/support/deepFreeze.js";
4
+ import { createRepository as createWorkspaceSettingsRepository } from "./workspaceSettingsRepository.js";
5
+ import { createService as createWorkspaceSettingsService } from "./workspaceSettingsService.js";
6
+ import { workspaceSettingsActions } from "./workspaceSettingsActions.js";
7
+ import { createWorkspaceRoleCatalog } from "../../shared/roles.js";
8
+ import { createWorkspaceEntityAndBootstrapEvents } from "../common/support/realtimeServiceEvents.js";
9
+
10
+ function resolveWorkspaceSettingsDefaultInvitesEnabled(appConfig = {}) {
11
+ const defaultInvitesEnabled = appConfig?.workspaceSettings?.defaults?.invitesEnabled;
12
+
13
+ if (typeof defaultInvitesEnabled !== "boolean") {
14
+ throw new TypeError("users.core requires appConfig.workspaceSettings.defaults.invitesEnabled.");
15
+ }
16
+
17
+ return defaultInvitesEnabled;
18
+ }
19
+
20
+ function registerWorkspaceSettings(app) {
21
+ if (!app || typeof app.singleton !== "function" || typeof app.actions !== "function" || typeof app.service !== "function") {
22
+ throw new Error("registerWorkspaceSettings requires application singleton()/service()/actions().");
23
+ }
24
+
25
+ app.singleton("workspaceSettingsRepository", (scope) => {
26
+ const knex = scope.make("jskit.database.knex");
27
+ const appConfig = resolveAppConfig(scope);
28
+ return createWorkspaceSettingsRepository(knex, {
29
+ defaultInvitesEnabled: resolveWorkspaceSettingsDefaultInvitesEnabled(appConfig)
30
+ });
31
+ });
32
+
33
+ app.service(
34
+ "workspaces.settings.service",
35
+ (scope) =>
36
+ createWorkspaceSettingsService({
37
+ workspaceSettingsRepository: scope.make("workspaceSettingsRepository"),
38
+ workspaceInvitationsEnabled: scope.make("workspaces.invitations.enabled"),
39
+ roleCatalog: createWorkspaceRoleCatalog(resolveAppConfig(scope))
40
+ }),
41
+ {
42
+ events: deepFreeze({
43
+ updateWorkspaceSettings: createWorkspaceEntityAndBootstrapEvents({
44
+ workspaceEntity: "settings",
45
+ workspaceOperation: "updated",
46
+ workspaceRealtimeEvent: "workspace.settings.changed"
47
+ })
48
+ })
49
+ }
50
+ );
51
+
52
+ app.actions(
53
+ withActionDefaults(workspaceSettingsActions, {
54
+ domain: "workspace",
55
+ dependencies: {
56
+ workspaceSettingsService: "workspaces.settings.service"
57
+ }
58
+ })
59
+ );
60
+ }
61
+
62
+ export { registerWorkspaceSettings };
@@ -0,0 +1,72 @@
1
+ import { workspaceSettingsResource } from "../../shared/resources/workspaceSettingsResource.js";
2
+ import { workspaceSlugParamsValidator } from "../common/validators/routeParamsValidator.js";
3
+ import { resolveWorkspace } from "../support/resolveWorkspace.js";
4
+
5
+ const workspaceSettingsActions = Object.freeze([
6
+ {
7
+ id: "workspace.settings.read",
8
+ version: 1,
9
+ kind: "query",
10
+ channels: ["api", "automation", "internal"],
11
+ surfacesFrom: "workspace",
12
+ permission: {
13
+ require: "any",
14
+ permissions: ["workspace.settings.view", "workspace.settings.update"]
15
+ },
16
+ inputValidator: workspaceSlugParamsValidator,
17
+ outputValidator: workspaceSettingsResource.operations.view.outputValidator,
18
+ idempotency: "none",
19
+ audit: {
20
+ actionName: "workspace.settings.read"
21
+ },
22
+ observability: {},
23
+ async execute(input, context, deps) {
24
+ const response = await deps.workspaceSettingsService.getWorkspaceSettings(resolveWorkspace(context, input), {
25
+ context
26
+ });
27
+
28
+ return response;
29
+ }
30
+ },
31
+ {
32
+ id: "workspace.settings.update",
33
+ version: 1,
34
+ kind: "command",
35
+ channels: ["api", "assistant_tool", "automation", "internal"],
36
+ surfacesFrom: "workspace",
37
+ permission: {
38
+ require: "all",
39
+ permissions: ["workspace.settings.update"]
40
+ },
41
+ inputValidator: [
42
+ workspaceSlugParamsValidator,
43
+ {
44
+ patch: workspaceSettingsResource.operations.patch.bodyValidator
45
+ }
46
+ ],
47
+ outputValidator: workspaceSettingsResource.operations.patch.outputValidator,
48
+ idempotency: "optional",
49
+ audit: {
50
+ actionName: "workspace.settings.update"
51
+ },
52
+ observability: {},
53
+ extensions: {
54
+ assistant: {
55
+ description: "Update workspace settings."
56
+ }
57
+ },
58
+ async execute(input, context, deps) {
59
+ const response = await deps.workspaceSettingsService.updateWorkspaceSettings(
60
+ resolveWorkspace(context, input),
61
+ input.patch,
62
+ {
63
+ context
64
+ }
65
+ );
66
+
67
+ return response;
68
+ }
69
+ }
70
+ ]);
71
+
72
+ export { workspaceSettingsActions };
@@ -0,0 +1,154 @@
1
+ import {
2
+ normalizeDbRecordId,
3
+ normalizeRecordId,
4
+ toIsoString,
5
+ nowDb,
6
+ isDuplicateEntryError,
7
+ createWithTransaction
8
+ } from "../common/repositories/repositoryUtils.js";
9
+ import { normalizeObjectInput } from "@jskit-ai/kernel/shared/validators/inputNormalization";
10
+ import { pickOwnProperties } from "@jskit-ai/kernel/shared/support";
11
+ import {
12
+ workspaceSettingsFields,
13
+ resolveWorkspaceSettingsFieldKeys
14
+ } from "../../shared/resources/workspaceSettingsFields.js";
15
+
16
+ function resolveWorkspaceSettingsSeed(workspace = {}, { defaultInvitesEnabled = true } = {}) {
17
+ const source = normalizeObjectInput(workspace);
18
+ const seed = {};
19
+ for (const field of workspaceSettingsFields) {
20
+ const rawValue = Object.hasOwn(source, field.key)
21
+ ? source[field.key]
22
+ : field.resolveDefault({
23
+ workspace: source,
24
+ defaultInvitesEnabled
25
+ });
26
+ seed[field.key] = field.normalizeOutput(rawValue, {
27
+ workspace: source,
28
+ defaultInvitesEnabled
29
+ });
30
+ }
31
+ return seed;
32
+ }
33
+
34
+ function createRepository(knex, { defaultInvitesEnabled } = {}) {
35
+ if (typeof knex !== "function") {
36
+ throw new TypeError("workspaceSettingsRepository requires knex.");
37
+ }
38
+ const withTransaction = createWithTransaction(knex);
39
+
40
+ function mapRow(row) {
41
+ if (!row) {
42
+ return null;
43
+ }
44
+
45
+ const settings = {
46
+ workspaceId: normalizeDbRecordId(row.workspace_id, { fallback: "" })
47
+ };
48
+ for (const field of workspaceSettingsFields) {
49
+ const rawValue = Object.hasOwn(row, field.dbColumn)
50
+ ? row[field.dbColumn]
51
+ : field.resolveDefault({
52
+ defaultInvitesEnabled
53
+ });
54
+ settings[field.key] = field.normalizeOutput(rawValue, {
55
+ defaultInvitesEnabled
56
+ });
57
+ }
58
+
59
+ settings.createdAt = toIsoString(row.created_at);
60
+ settings.updatedAt = toIsoString(row.updated_at);
61
+ return settings;
62
+ }
63
+
64
+ async function findByWorkspaceId(workspaceId, options = {}) {
65
+ const client = options?.trx || knex;
66
+ const normalizedWorkspaceId = normalizeRecordId(workspaceId, { fallback: null });
67
+ if (!normalizedWorkspaceId) {
68
+ return null;
69
+ }
70
+
71
+ const row = await client("workspace_settings").where({ workspace_id: normalizedWorkspaceId }).first();
72
+ return mapRow(row);
73
+ }
74
+
75
+ async function ensureForWorkspaceId(workspaceId, options = {}) {
76
+ const client = options?.trx || knex;
77
+ const normalizedWorkspaceId = normalizeRecordId(workspaceId, { fallback: null });
78
+ if (!normalizedWorkspaceId) {
79
+ throw new TypeError("workspaceSettingsRepository.ensureForWorkspaceId requires a valid workspace id.");
80
+ }
81
+
82
+ const seed = resolveWorkspaceSettingsSeed(options?.workspace, {
83
+ defaultInvitesEnabled
84
+ });
85
+ const existing = await findByWorkspaceId(normalizedWorkspaceId, { trx: client });
86
+ if (existing) {
87
+ return existing;
88
+ }
89
+
90
+ try {
91
+ const insertPayload = {
92
+ workspace_id: normalizedWorkspaceId,
93
+ created_at: nowDb(),
94
+ updated_at: nowDb()
95
+ };
96
+ for (const field of workspaceSettingsFields) {
97
+ insertPayload[field.dbColumn] = seed[field.key];
98
+ }
99
+ await client("workspace_settings").insert(insertPayload);
100
+ } catch (error) {
101
+ if (!isDuplicateEntryError(error)) {
102
+ throw error;
103
+ }
104
+ }
105
+
106
+ return findByWorkspaceId(normalizedWorkspaceId, { trx: client });
107
+ }
108
+
109
+ async function updateSettingsByWorkspaceId(workspaceId, patch = {}, options = {}) {
110
+ const client = options?.trx || knex;
111
+ const normalizedWorkspaceId = normalizeRecordId(workspaceId, { fallback: null });
112
+ if (!normalizedWorkspaceId) {
113
+ throw new TypeError("workspaceSettingsRepository.updateSettingsByWorkspaceId requires a valid workspace id.");
114
+ }
115
+
116
+ const ensured = await ensureForWorkspaceId(normalizedWorkspaceId, {
117
+ trx: client,
118
+ workspace: options?.workspace
119
+ });
120
+ const source = normalizeObjectInput(patch);
121
+ const settingsPatch = pickOwnProperties(source, resolveWorkspaceSettingsFieldKeys());
122
+
123
+ if (Object.keys(settingsPatch).length === 0) {
124
+ return ensured;
125
+ }
126
+
127
+ const dbPatch = {
128
+ updated_at: nowDb()
129
+ };
130
+
131
+ for (const field of workspaceSettingsFields) {
132
+ if (!Object.hasOwn(settingsPatch, field.key)) {
133
+ continue;
134
+ }
135
+ dbPatch[field.dbColumn] = field.normalizeInput(settingsPatch[field.key], {
136
+ payload: source
137
+ });
138
+ }
139
+
140
+ await client("workspace_settings").where({ workspace_id: normalizedWorkspaceId }).update({
141
+ ...dbPatch
142
+ });
143
+ return findByWorkspaceId(normalizedWorkspaceId, { trx: client });
144
+ }
145
+
146
+ return Object.freeze({
147
+ withTransaction,
148
+ findByWorkspaceId,
149
+ ensureForWorkspaceId,
150
+ updateSettingsByWorkspaceId
151
+ });
152
+ }
153
+
154
+ export { createRepository };
@@ -0,0 +1,66 @@
1
+ import { normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
2
+ import { normalizeObjectInput } from "@jskit-ai/kernel/shared/validators/inputNormalization";
3
+ import { pickOwnProperties } from "@jskit-ai/kernel/shared/support";
4
+ import {
5
+ workspaceSettingsFields,
6
+ resolveWorkspaceSettingsFieldKeys
7
+ } from "../../shared/resources/workspaceSettingsFields.js";
8
+ import { createWorkspaceRoleCatalog, cloneWorkspaceRoleCatalog } from "../../shared/roles.js";
9
+
10
+ function createService({
11
+ workspaceSettingsRepository,
12
+ workspaceInvitationsEnabled = true,
13
+ roleCatalog = null
14
+ } = {}) {
15
+ if (!workspaceSettingsRepository) {
16
+ throw new Error("workspaceSettingsService requires workspaceSettingsRepository.");
17
+ }
18
+ const resolvedRoleCatalog = roleCatalog && typeof roleCatalog === "object" ? roleCatalog : createWorkspaceRoleCatalog();
19
+ const invitesAvailable = workspaceInvitationsEnabled === true;
20
+
21
+ async function getWorkspaceSettings(workspace, options = {}) {
22
+ const settingsRecord = await workspaceSettingsRepository.ensureForWorkspaceId(workspace.id, {
23
+ ...options,
24
+ workspace
25
+ });
26
+ const settings = {};
27
+ for (const field of workspaceSettingsFields) {
28
+ settings[field.key] = settingsRecord[field.key];
29
+ }
30
+ const invitesEnabled = invitesAvailable && settings.invitesEnabled !== false;
31
+ settings.invitesEnabled = invitesEnabled;
32
+ settings.invitesAvailable = invitesAvailable;
33
+ settings.invitesEffective = invitesAvailable && invitesEnabled;
34
+
35
+ return {
36
+ workspace: {
37
+ id: normalizeRecordId(workspace.id, { fallback: "" }),
38
+ slug: String(workspace.slug || ""),
39
+ ownerUserId: normalizeRecordId(workspace.ownerUserId, { fallback: "" })
40
+ },
41
+ settings,
42
+ roleCatalog: cloneWorkspaceRoleCatalog(resolvedRoleCatalog)
43
+ };
44
+ }
45
+
46
+ async function updateWorkspaceSettings(workspace, payload = {}, options = {}) {
47
+ const source = normalizeObjectInput(payload);
48
+ const settingsPatch = pickOwnProperties(source, resolveWorkspaceSettingsFieldKeys());
49
+
50
+ if (Object.keys(settingsPatch).length > 0) {
51
+ await workspaceSettingsRepository.updateSettingsByWorkspaceId(workspace.id, settingsPatch, {
52
+ ...options,
53
+ workspace
54
+ });
55
+ }
56
+
57
+ return getWorkspaceSettings(workspace, options);
58
+ }
59
+
60
+ return Object.freeze({
61
+ getWorkspaceSettings,
62
+ updateWorkspaceSettings
63
+ });
64
+ }
65
+
66
+ export { createService };
@@ -0,0 +1,16 @@
1
+ function createOperationMessages({
2
+ validationMessage = "Validation failed.",
3
+ apiValidationMessage = validationMessage
4
+ } = {}) {
5
+ const validation = String(validationMessage || "Validation failed.");
6
+ const apiValidation = String(apiValidationMessage || validation || "Validation failed.");
7
+
8
+ return Object.freeze({
9
+ validation,
10
+ apiValidation
11
+ });
12
+ }
13
+
14
+ export {
15
+ createOperationMessages
16
+ };
@@ -0,0 +1,6 @@
1
+ function resolveGlobalArrayRegistry(symbolKey) {
2
+ globalThis[symbolKey] = Array.isArray(globalThis[symbolKey]) ? globalThis[symbolKey] : [];
3
+ return globalThis[symbolKey];
4
+ }
5
+
6
+ export { resolveGlobalArrayRegistry };