@jskit-ai/workspaces-core 0.2.15 → 0.2.17
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 +27 -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 +92 -146
- package/src/server/common/repositories/workspacesRepository.js +90 -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 +16 -73
- package/src/shared/resources/workspaceMembersResource.js +3 -3
- package/src/shared/resources/workspaceMembershipsResource.js +14 -48
- package/src/shared/resources/workspacePendingInvitationsResource.js +3 -3
- package/src/shared/resources/workspaceResource.js +21 -17
- package/src/shared/resources/workspaceSettingsResource.js +17 -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 +272 -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,15 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isLostDuplicateInsertRace,
|
|
3
|
-
normalizeDbRecordId,
|
|
4
|
-
resolveTransaction,
|
|
5
|
-
retryDeadlocks,
|
|
6
|
-
toIsoString
|
|
7
|
-
} from "@jskit-ai/database-runtime/shared";
|
|
1
|
+
import { isLostDuplicateInsertRace, normalizeDbRecordId, toIsoString } from "@jskit-ai/database-runtime/shared";
|
|
8
2
|
import { normalizeLowerText, normalizeRecordId, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
9
|
-
import {
|
|
10
|
-
createJsonRestContext,
|
|
11
|
-
queryAllJsonRestCollectionRows
|
|
12
|
-
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
13
3
|
import { OWNER_ROLE_ID } from "../../../shared/roles.js";
|
|
14
4
|
|
|
15
5
|
// A bulk read's users per query, so its "in" list stays well inside every database's limits.
|
|
@@ -60,30 +50,31 @@ function memberSummaryFromMembership(row) {
|
|
|
60
50
|
});
|
|
61
51
|
}
|
|
62
52
|
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
53
|
+
/**
|
|
54
|
+
* The `workspaces.memberships` capability: the workspace memberships table's
|
|
55
|
+
* records. Every method takes the caller's options last and joins their transaction.
|
|
56
|
+
* @param {{ workspaceMemberships: import("@jskit-ai/resource-runtime/server/types").ResourceHandle }} dependencies
|
|
57
|
+
* The workspace_memberships table's handle.
|
|
58
|
+
*/
|
|
59
|
+
function createRepository({ workspaceMemberships }) {
|
|
60
|
+
async function queryMemberships(filters, options, { include = [] } = {}) {
|
|
61
|
+
return workspaceMemberships.queryAll({ filters, ...(include.length > 0 ? { include } : {}) }, options);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// A new membership, or nothing when a standalone insert lost to another one (the caller reads the winner).
|
|
65
|
+
async function createMembership(attributes, options) {
|
|
66
|
+
const createdAt = new Date().toISOString();
|
|
67
|
+
try {
|
|
68
|
+
await workspaceMemberships.create({ ...attributes, createdAt, updatedAt: createdAt }, { ...options, returning: "none" });
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (!isLostDuplicateInsertRace(error, options)) {
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function patchMembership(id, changes, options) {
|
|
77
|
+
await workspaceMemberships.patch(id, { ...changes, updatedAt: new Date().toISOString() }, { ...options, returning: "none" });
|
|
87
78
|
}
|
|
88
79
|
|
|
89
80
|
async function findByWorkspaceIdAndUserId(workspaceId, userId, options = {}) {
|
|
@@ -93,15 +84,7 @@ function createRepository({ api }) {
|
|
|
93
84
|
return null;
|
|
94
85
|
}
|
|
95
86
|
|
|
96
|
-
const rows = await queryMemberships(
|
|
97
|
-
{
|
|
98
|
-
workspace: normalizedWorkspaceId,
|
|
99
|
-
user: normalizedUserId
|
|
100
|
-
},
|
|
101
|
-
options,
|
|
102
|
-
{ include: ["workspace", "user"] }
|
|
103
|
-
);
|
|
104
|
-
|
|
87
|
+
const rows = await queryMemberships({ workspace: normalizedWorkspaceId, user: normalizedUserId }, options);
|
|
105
88
|
return normalizeMembershipRecord(rows[0] || null);
|
|
106
89
|
}
|
|
107
90
|
|
|
@@ -115,49 +98,15 @@ function createRepository({ api }) {
|
|
|
115
98
|
const existing = await findByWorkspaceIdAndUserId(normalizedWorkspaceId, normalizedUserId, options);
|
|
116
99
|
if (existing) {
|
|
117
100
|
if (existing.roleSid !== OWNER_ROLE_ID || existing.status !== "active") {
|
|
118
|
-
|
|
119
|
-
await api.resources.workspaceMemberships.patch(
|
|
120
|
-
{
|
|
121
|
-
id: existing.id,
|
|
122
|
-
data: {
|
|
123
|
-
roleSid: OWNER_ROLE_ID,
|
|
124
|
-
status: "active",
|
|
125
|
-
updatedAt
|
|
126
|
-
},
|
|
127
|
-
format: "plain",
|
|
128
|
-
returning: "full",
|
|
129
|
-
transaction: resolveTransaction(options)
|
|
130
|
-
},
|
|
131
|
-
createJsonRestContext(options.context)
|
|
132
|
-
);
|
|
101
|
+
await patchMembership(existing.id, { roleSid: OWNER_ROLE_ID, status: "active" }, options);
|
|
133
102
|
}
|
|
134
103
|
return findByWorkspaceIdAndUserId(normalizedWorkspaceId, normalizedUserId, options);
|
|
135
104
|
}
|
|
136
105
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
data: {
|
|
142
|
-
roleSid: OWNER_ROLE_ID,
|
|
143
|
-
status: "active",
|
|
144
|
-
createdAt,
|
|
145
|
-
updatedAt: createdAt,
|
|
146
|
-
workspace: normalizedWorkspaceId,
|
|
147
|
-
user: normalizedUserId
|
|
148
|
-
},
|
|
149
|
-
format: "plain",
|
|
150
|
-
returning: "full",
|
|
151
|
-
transaction: resolveTransaction(options)
|
|
152
|
-
},
|
|
153
|
-
createJsonRestContext(options.context)
|
|
154
|
-
);
|
|
155
|
-
} catch (error) {
|
|
156
|
-
if (!isLostDuplicateInsertRace(error, options)) {
|
|
157
|
-
throw error;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
106
|
+
await createMembership(
|
|
107
|
+
{ roleSid: OWNER_ROLE_ID, status: "active", workspaceId: normalizedWorkspaceId, userId: normalizedUserId },
|
|
108
|
+
options
|
|
109
|
+
);
|
|
161
110
|
return findByWorkspaceIdAndUserId(normalizedWorkspaceId, normalizedUserId, options);
|
|
162
111
|
}
|
|
163
112
|
|
|
@@ -173,72 +122,71 @@ function createRepository({ api }) {
|
|
|
173
122
|
const status = normalizeLowerText(patch?.status ?? existing?.status) || "active";
|
|
174
123
|
|
|
175
124
|
if (!existing) {
|
|
176
|
-
|
|
177
|
-
const createdAt = new Date().toISOString();
|
|
178
|
-
await api.resources.workspaceMemberships.post(
|
|
179
|
-
{
|
|
180
|
-
data: {
|
|
181
|
-
roleSid,
|
|
182
|
-
status,
|
|
183
|
-
createdAt,
|
|
184
|
-
updatedAt: createdAt,
|
|
185
|
-
workspace: normalizedWorkspaceId,
|
|
186
|
-
user: normalizedUserId
|
|
187
|
-
},
|
|
188
|
-
format: "plain",
|
|
189
|
-
returning: "full",
|
|
190
|
-
transaction: resolveTransaction(options)
|
|
191
|
-
},
|
|
192
|
-
createJsonRestContext(options.context)
|
|
193
|
-
);
|
|
194
|
-
} catch (error) {
|
|
195
|
-
if (!isLostDuplicateInsertRace(error, options)) {
|
|
196
|
-
throw error;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
125
|
+
await createMembership({ roleSid, status, workspaceId: normalizedWorkspaceId, userId: normalizedUserId }, options);
|
|
199
126
|
return findByWorkspaceIdAndUserId(normalizedWorkspaceId, normalizedUserId, options);
|
|
200
127
|
}
|
|
201
128
|
|
|
202
|
-
|
|
203
|
-
await api.resources.workspaceMemberships.patch(
|
|
204
|
-
{
|
|
205
|
-
id: existing.id,
|
|
206
|
-
data: {
|
|
207
|
-
roleSid,
|
|
208
|
-
status,
|
|
209
|
-
updatedAt
|
|
210
|
-
},
|
|
211
|
-
format: "plain",
|
|
212
|
-
returning: "full",
|
|
213
|
-
transaction: resolveTransaction(options)
|
|
214
|
-
},
|
|
215
|
-
createJsonRestContext(options.context)
|
|
216
|
-
);
|
|
217
|
-
|
|
129
|
+
await patchMembership(existing.id, { roleSid, status }, options);
|
|
218
130
|
return findByWorkspaceIdAndUserId(normalizedWorkspaceId, normalizedUserId, options);
|
|
219
131
|
}
|
|
220
132
|
|
|
221
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Makes the user an active member of the workspace with the role (`member`
|
|
135
|
+
* when none is given), adding the membership or reactivating it.
|
|
136
|
+
* @param {{ workspaceId: unknown, userId: unknown, roleSid?: string }} member
|
|
137
|
+
* @param {object} [options]
|
|
138
|
+
*/
|
|
139
|
+
async function addMember({ workspaceId, userId, roleSid = "member" }, options = {}) {
|
|
140
|
+
return upsertMembership(workspaceId, userId, { roleSid, status: "active" }, options);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The ids of the workspace's active members, in one read without their profiles.
|
|
145
|
+
* @param {unknown} workspaceId
|
|
146
|
+
* @param {object} [options]
|
|
147
|
+
* @returns {Promise<string[]>}
|
|
148
|
+
*/
|
|
149
|
+
async function activeMemberIds(workspaceId, options = {}) {
|
|
222
150
|
const normalizedWorkspaceId = normalizeRecordId(workspaceId, { fallback: null });
|
|
223
151
|
if (!normalizedWorkspaceId) {
|
|
224
152
|
return [];
|
|
225
153
|
}
|
|
226
154
|
|
|
227
|
-
const rows = await queryMemberships(
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
155
|
+
const rows = await queryMemberships({ workspace: normalizedWorkspaceId, status: "active" }, options);
|
|
156
|
+
return rows
|
|
157
|
+
.map((row) => normalizeDbRecordId(row.userId, { fallback: null }))
|
|
158
|
+
.filter(Boolean);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The member summaries of a workspace's memberships the filters select, by display name, with their users in the same read.
|
|
162
|
+
async function listWorkspaceMembers(workspaceId, filters, options) {
|
|
163
|
+
const normalizedWorkspaceId = normalizeRecordId(workspaceId, { fallback: null });
|
|
164
|
+
if (!normalizedWorkspaceId) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
235
167
|
|
|
168
|
+
const rows = await queryMemberships({ workspace: normalizedWorkspaceId, ...filters }, options, { include: ["user"] });
|
|
236
169
|
const members = rows.map(memberSummaryFromMembership).filter(Boolean);
|
|
237
170
|
|
|
238
171
|
members.sort((left, right) => String(left.displayName || "").localeCompare(String(right.displayName || "")));
|
|
239
172
|
return members;
|
|
240
173
|
}
|
|
241
174
|
|
|
175
|
+
async function listActiveByWorkspaceId(workspaceId, options = {}) {
|
|
176
|
+
return listWorkspaceMembers(workspaceId, { status: "active" }, options);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Every member of a workspace whatever their membership's status, such as
|
|
181
|
+
* one just removed ("revoked"), as listActiveByWorkspaceId lists the active
|
|
182
|
+
* ones; each summary carries its status.
|
|
183
|
+
* @param {unknown} workspaceId
|
|
184
|
+
* @param {object} [options]
|
|
185
|
+
*/
|
|
186
|
+
async function listByWorkspaceId(workspaceId, options = {}) {
|
|
187
|
+
return listWorkspaceMembers(workspaceId, {}, options);
|
|
188
|
+
}
|
|
189
|
+
|
|
242
190
|
// One active member's summary, as listActiveByWorkspaceId lists it, or null. Its cost does not grow
|
|
243
191
|
// with the workspace's members.
|
|
244
192
|
async function findActiveByWorkspaceIdAndUserId(workspaceId, userId, options = {}) {
|
|
@@ -267,24 +215,15 @@ function createRepository({ api }) {
|
|
|
267
215
|
return [];
|
|
268
216
|
}
|
|
269
217
|
|
|
270
|
-
const rows = await queryMemberships(
|
|
271
|
-
{
|
|
272
|
-
user: normalizedUserId,
|
|
273
|
-
status: "active"
|
|
274
|
-
},
|
|
275
|
-
options,
|
|
276
|
-
{ include: ["workspace"] }
|
|
277
|
-
);
|
|
278
|
-
|
|
218
|
+
const rows = await queryMemberships({ user: normalizedUserId, status: "active" }, options);
|
|
279
219
|
return rows
|
|
280
|
-
.map((row) => normalizeDbRecordId(row
|
|
220
|
+
.map((row) => normalizeDbRecordId(row.workspaceId, { fallback: null }))
|
|
281
221
|
.filter(Boolean);
|
|
282
222
|
}
|
|
283
223
|
|
|
284
224
|
/**
|
|
285
225
|
* Each user's active workspace ids, as listActiveWorkspaceIdsByUserId lists them, in one
|
|
286
|
-
* query per 1,000 users: its cost does not grow with the users asked about.
|
|
287
|
-
* memberships' workspace_id column instead of including each workspace.
|
|
226
|
+
* query per 1,000 users: its cost does not grow with the users asked about.
|
|
288
227
|
* @param {Iterable<string | number>} userIds
|
|
289
228
|
* @param {object} [options]
|
|
290
229
|
* @returns {Promise<Map<string, string[]>>} Every asked user, with no workspaces when they have none.
|
|
@@ -306,8 +245,8 @@ function createRepository({ api }) {
|
|
|
306
245
|
options
|
|
307
246
|
);
|
|
308
247
|
for (const row of rows) {
|
|
309
|
-
const userId = normalizeDbRecordId(row
|
|
310
|
-
const workspaceId = normalizeDbRecordId(row
|
|
248
|
+
const userId = normalizeDbRecordId(row.userId, { fallback: null });
|
|
249
|
+
const workspaceId = normalizeDbRecordId(row.workspaceId, { fallback: null });
|
|
311
250
|
if (workspaceId) workspaceIdsByUserId.get(userId)?.push(workspaceId);
|
|
312
251
|
}
|
|
313
252
|
}
|
|
@@ -315,11 +254,18 @@ function createRepository({ api }) {
|
|
|
315
254
|
}
|
|
316
255
|
|
|
317
256
|
return Object.freeze({
|
|
318
|
-
withTransaction,
|
|
257
|
+
withTransaction: workspaceMemberships.withTransaction,
|
|
258
|
+
// The memberships as a join source for another owner's report, which reads them in its own statement: the
|
|
259
|
+
// handle's joinSource, the scoped memberships under `alias` matching `filters` (such as `{ workspace, status:
|
|
260
|
+
// "active" }`).
|
|
261
|
+
joinSource: workspaceMemberships.joinSource,
|
|
319
262
|
findByWorkspaceIdAndUserId,
|
|
320
263
|
ensureOwnerMembership,
|
|
321
264
|
upsertMembership,
|
|
265
|
+
addMember,
|
|
266
|
+
activeMemberIds,
|
|
322
267
|
listActiveByWorkspaceId,
|
|
268
|
+
listByWorkspaceId,
|
|
323
269
|
findActiveByWorkspaceIdAndUserId,
|
|
324
270
|
listActiveWorkspaceIdsByUserId,
|
|
325
271
|
listActiveWorkspaceIdsByUserIds
|
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import { isLostDuplicateInsertRace,
|
|
1
|
+
import { isLostDuplicateInsertRace, toIsoString } from "@jskit-ai/database-runtime/shared";
|
|
2
2
|
import { normalizeLowerText, normalizeRecordId, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
-
import {
|
|
4
|
-
createJsonRestContext,
|
|
5
|
-
extractJsonRestCollectionRows,
|
|
6
|
-
queryAllJsonRestCollectionRows
|
|
7
|
-
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
8
3
|
|
|
9
4
|
function normalizeWorkspaceRecord(payload = null) {
|
|
10
5
|
if (!payload || typeof payload !== "object") {
|
|
@@ -24,25 +19,28 @@ function normalizeWorkspaceRecord(payload = null) {
|
|
|
24
19
|
};
|
|
25
20
|
}
|
|
26
21
|
|
|
27
|
-
|
|
28
|
-
function
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
filters
|
|
37
|
-
},
|
|
38
|
-
transaction: resolveTransaction(options),
|
|
39
|
-
format: "plain"
|
|
40
|
-
},
|
|
41
|
-
createJsonRestContext(options.context)
|
|
42
|
-
)
|
|
43
|
-
);
|
|
22
|
+
// Lower ids first; ids that are not numbers in text order.
|
|
23
|
+
function byId(left, right) {
|
|
24
|
+
const leftId = Number(left?.id);
|
|
25
|
+
const rightId = Number(right?.id);
|
|
26
|
+
if (Number.isFinite(leftId) && Number.isFinite(rightId)) {
|
|
27
|
+
return leftId - rightId;
|
|
28
|
+
}
|
|
29
|
+
return String(left?.id || "").localeCompare(String(right?.id || ""));
|
|
30
|
+
}
|
|
44
31
|
|
|
45
|
-
|
|
32
|
+
/**
|
|
33
|
+
* The `workspaces` capability: the workspaces table's records. Every method
|
|
34
|
+
* takes the caller's options last and joins their transaction.
|
|
35
|
+
* @param {object} dependencies
|
|
36
|
+
* @param {import("@jskit-ai/resource-runtime/server/types").ResourceHandle} dependencies.workspaces The workspaces table's handle.
|
|
37
|
+
* @param {import("@jskit-ai/resource-runtime/server/types").ResourceHandle} dependencies.workspaceMemberships
|
|
38
|
+
* The workspace_memberships table's handle, for a user's workspaces.
|
|
39
|
+
*/
|
|
40
|
+
function createRepository({ workspaces, workspaceMemberships }) {
|
|
41
|
+
async function queryFirst(filters, options) {
|
|
42
|
+
const { records } = await workspaces.query({ filters, page: { size: 1 } }, options);
|
|
43
|
+
return normalizeWorkspaceRecord(records[0] || null);
|
|
46
44
|
}
|
|
47
45
|
|
|
48
46
|
async function findById(workspaceId, options = {}) {
|
|
@@ -54,13 +52,14 @@ function createRepository({ api }) {
|
|
|
54
52
|
return queryFirst({ id: normalizedWorkspaceId }, options);
|
|
55
53
|
}
|
|
56
54
|
|
|
55
|
+
// A deleted workspace keeps its slug, so the slug stays taken, but it is not found by it.
|
|
57
56
|
async function findBySlug(slug, options = {}) {
|
|
58
57
|
const normalizedSlug = typeof slug === "string" ? slug.trim().toLowerCase() : "";
|
|
59
58
|
if (!normalizedSlug) {
|
|
60
59
|
return null;
|
|
61
60
|
}
|
|
62
61
|
|
|
63
|
-
return queryFirst({ slug: normalizedSlug }, options);
|
|
62
|
+
return queryFirst({ slug: normalizedSlug, deletedAt: null }, options);
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
/**
|
|
@@ -77,53 +76,37 @@ function createRepository({ api }) {
|
|
|
77
76
|
return new Set();
|
|
78
77
|
}
|
|
79
78
|
|
|
80
|
-
const rows = await
|
|
81
|
-
api.resources.workspaces,
|
|
82
|
-
{
|
|
83
|
-
queryParams: {
|
|
84
|
-
filters: {
|
|
85
|
-
slugs: normalizedSlugs
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
transaction: resolveTransaction(options),
|
|
89
|
-
format: "plain"
|
|
90
|
-
},
|
|
91
|
-
options.context
|
|
92
|
-
);
|
|
79
|
+
const rows = await workspaces.queryAll({ filters: { slugs: normalizedSlugs } }, options);
|
|
93
80
|
return new Set(rows.map((row) => normalizeLowerText(row.slug)));
|
|
94
81
|
}
|
|
95
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The workspaces with these ids, in one read however many, sorted by id. An id naming no workspace is left out; a
|
|
85
|
+
* deleted workspace is kept, as findById keeps it.
|
|
86
|
+
* @param {unknown[]} workspaceIds
|
|
87
|
+
* @param {import("@jskit-ai/resource-runtime/server/types").ResourceCallOptions} [options]
|
|
88
|
+
*/
|
|
89
|
+
async function listByIds(workspaceIds, options = {}) {
|
|
90
|
+
if (!Array.isArray(workspaceIds)) {
|
|
91
|
+
throw new TypeError("workspaces.listByIds() takes a list of workspace ids.");
|
|
92
|
+
}
|
|
93
|
+
const ids = [...new Set(workspaceIds.map((id) => normalizeRecordId(id, { fallback: null })).filter(Boolean))];
|
|
94
|
+
if (ids.length < 1) {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
const rows = await workspaces.queryAll({ filters: { id: ids } }, options);
|
|
98
|
+
return rows.map((row) => normalizeWorkspaceRecord(row)).filter(Boolean).sort(byId);
|
|
99
|
+
}
|
|
100
|
+
|
|
96
101
|
async function findPersonalByOwnerUserId(userId, options = {}) {
|
|
97
102
|
const normalizedUserId = normalizeRecordId(userId, { fallback: null });
|
|
98
103
|
if (!normalizedUserId) {
|
|
99
104
|
return null;
|
|
100
105
|
}
|
|
101
106
|
|
|
102
|
-
const rows =
|
|
103
|
-
await api.resources.workspaces.query(
|
|
104
|
-
{
|
|
105
|
-
queryParams: {
|
|
106
|
-
filters: {
|
|
107
|
-
owner: normalizedUserId,
|
|
108
|
-
isPersonal: true
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
transaction: resolveTransaction(options),
|
|
112
|
-
format: "plain"
|
|
113
|
-
},
|
|
114
|
-
createJsonRestContext(options.context)
|
|
115
|
-
)
|
|
116
|
-
);
|
|
117
|
-
|
|
107
|
+
const rows = await workspaces.queryAll({ filters: { owner: normalizedUserId, isPersonal: true } }, options);
|
|
118
108
|
const normalizedRows = rows.map((row) => normalizeWorkspaceRecord(row)).filter(Boolean);
|
|
119
|
-
normalizedRows.sort(
|
|
120
|
-
const leftId = Number(left?.id);
|
|
121
|
-
const rightId = Number(right?.id);
|
|
122
|
-
if (Number.isFinite(leftId) && Number.isFinite(rightId)) {
|
|
123
|
-
return leftId - rightId;
|
|
124
|
-
}
|
|
125
|
-
return String(left?.id || "").localeCompare(String(right?.id || ""));
|
|
126
|
-
});
|
|
109
|
+
normalizedRows.sort(byId);
|
|
127
110
|
return normalizedRows[0] || null;
|
|
128
111
|
}
|
|
129
112
|
|
|
@@ -143,19 +126,14 @@ function createRepository({ api }) {
|
|
|
143
126
|
|
|
144
127
|
try {
|
|
145
128
|
const createdAt = new Date().toISOString();
|
|
146
|
-
const created = await
|
|
129
|
+
const created = await workspaces.create(
|
|
147
130
|
{
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
owner: ownerUserId
|
|
153
|
-
},
|
|
154
|
-
format: "plain",
|
|
155
|
-
returning: "full",
|
|
156
|
-
transaction: resolveTransaction(options)
|
|
131
|
+
...createPayload,
|
|
132
|
+
createdAt,
|
|
133
|
+
updatedAt: createdAt,
|
|
134
|
+
ownerUserId
|
|
157
135
|
},
|
|
158
|
-
|
|
136
|
+
options
|
|
159
137
|
);
|
|
160
138
|
|
|
161
139
|
return normalizeWorkspaceRecord(created);
|
|
@@ -167,8 +145,10 @@ function createRepository({ api }) {
|
|
|
167
145
|
throw error;
|
|
168
146
|
}
|
|
169
147
|
|
|
148
|
+
// A create lost to the same owner's identical create, such as a personal workspace made twice at a first
|
|
149
|
+
// sign-in, answers that workspace. Another owner's workspace is never answered: the caller picks another slug.
|
|
170
150
|
const bySlug = await findBySlug(createPayload.slug, options);
|
|
171
|
-
if (bySlug) {
|
|
151
|
+
if (bySlug && bySlug.ownerUserId === ownerUserId && bySlug.isPersonal === (createPayload.isPersonal === true)) {
|
|
172
152
|
return bySlug;
|
|
173
153
|
}
|
|
174
154
|
throw error;
|
|
@@ -186,23 +166,11 @@ function createRepository({ api }) {
|
|
|
186
166
|
...sourcePatch,
|
|
187
167
|
updatedAt: new Date().toISOString()
|
|
188
168
|
};
|
|
189
|
-
delete workspacePatch.ownerUserId;
|
|
190
169
|
if (Object.hasOwn(sourcePatch, "ownerUserId")) {
|
|
191
|
-
workspacePatch.
|
|
170
|
+
workspacePatch.ownerUserId = normalizeRecordId(sourcePatch.ownerUserId, { fallback: null });
|
|
192
171
|
}
|
|
193
172
|
|
|
194
|
-
|
|
195
|
-
{
|
|
196
|
-
id: normalizedWorkspaceId,
|
|
197
|
-
data: workspacePatch,
|
|
198
|
-
format: "plain",
|
|
199
|
-
returning: "full",
|
|
200
|
-
transaction: resolveTransaction(options)
|
|
201
|
-
},
|
|
202
|
-
createJsonRestContext(options.context)
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
return normalizeWorkspaceRecord(updated);
|
|
173
|
+
return normalizeWorkspaceRecord(await workspaces.patch(normalizedWorkspaceId, workspacePatch, options));
|
|
206
174
|
}
|
|
207
175
|
|
|
208
176
|
async function listForUserId(userId, options = {}) {
|
|
@@ -211,23 +179,12 @@ function createRepository({ api }) {
|
|
|
211
179
|
return [];
|
|
212
180
|
}
|
|
213
181
|
|
|
214
|
-
const rows = await
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
queryParams: {
|
|
218
|
-
filters: {
|
|
219
|
-
user: normalizedUserId,
|
|
220
|
-
status: "active"
|
|
221
|
-
},
|
|
222
|
-
include: ["workspace"]
|
|
223
|
-
},
|
|
224
|
-
transaction: resolveTransaction(options),
|
|
225
|
-
format: "plain"
|
|
226
|
-
},
|
|
227
|
-
options.context
|
|
182
|
+
const rows = await workspaceMemberships.queryAll(
|
|
183
|
+
{ filters: { user: normalizedUserId, status: "active" }, include: ["workspace"] },
|
|
184
|
+
options
|
|
228
185
|
);
|
|
229
186
|
|
|
230
|
-
const
|
|
187
|
+
const userWorkspaces = rows
|
|
231
188
|
.map((row) => {
|
|
232
189
|
const workspace = normalizeWorkspaceRecord(row?.workspace);
|
|
233
190
|
if (!workspace || workspace.deletedAt != null) {
|
|
@@ -242,30 +199,50 @@ function createRepository({ api }) {
|
|
|
242
199
|
})
|
|
243
200
|
.filter(Boolean);
|
|
244
201
|
|
|
245
|
-
|
|
202
|
+
userWorkspaces.sort((left, right) => {
|
|
246
203
|
if (left.isPersonal !== right.isPersonal) {
|
|
247
204
|
return left.isPersonal ? -1 : 1;
|
|
248
205
|
}
|
|
249
|
-
|
|
250
|
-
const rightId = Number(right?.id);
|
|
251
|
-
if (Number.isFinite(leftId) && Number.isFinite(rightId)) {
|
|
252
|
-
return leftId - rightId;
|
|
253
|
-
}
|
|
254
|
-
return String(left?.id || "").localeCompare(String(right?.id || ""));
|
|
206
|
+
return byId(left, right);
|
|
255
207
|
});
|
|
256
208
|
|
|
257
|
-
return
|
|
209
|
+
return userWorkspaces;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Locks a workspace's row until the caller's transaction ends, so work that
|
|
214
|
+
* must not interleave for one workspace takes turns. It needs an active
|
|
215
|
+
* transaction: pass the txOptions of withTransaction. A missing workspace
|
|
216
|
+
* throws json-rest-api's not-found error, or answers null with
|
|
217
|
+
* `missing: "skip"`.
|
|
218
|
+
* @param {unknown} workspaceId
|
|
219
|
+
* @param {import("@jskit-ai/resource-runtime/server/types").ResourceCallOptions & { missing?: "throw" | "skip" }} options
|
|
220
|
+
* @returns {Promise<{ id: string } | null>} The locked workspace.
|
|
221
|
+
*/
|
|
222
|
+
async function lock(workspaceId, options = {}) {
|
|
223
|
+
const id = normalizeRecordId(workspaceId, { fallback: null });
|
|
224
|
+
if (!id) {
|
|
225
|
+
throw new TypeError("Locking a workspace requires workspaceId.");
|
|
226
|
+
}
|
|
227
|
+
const row = await workspaces.lock(id, options);
|
|
228
|
+
return row ? { id: normalizeRecordId(row.id, { fallback: null }) } : null;
|
|
258
229
|
}
|
|
259
230
|
|
|
260
231
|
return Object.freeze({
|
|
261
|
-
withTransaction,
|
|
232
|
+
withTransaction: workspaces.withTransaction,
|
|
233
|
+
// The workspaces as a join source for another owner's report, which reads them in its own statement: the handle's
|
|
234
|
+
// joinSource, the scoped workspaces under `alias` matching `filters` (a deleted workspace is in it unless the
|
|
235
|
+
// report filters on `column("deletedAt")`).
|
|
236
|
+
joinSource: workspaces.joinSource,
|
|
262
237
|
findById,
|
|
238
|
+
listByIds,
|
|
263
239
|
findBySlug,
|
|
264
240
|
listSlugsIn,
|
|
265
241
|
findPersonalByOwnerUserId,
|
|
266
242
|
insert,
|
|
267
243
|
updateById,
|
|
268
|
-
listForUserId
|
|
244
|
+
listForUserId,
|
|
245
|
+
lock
|
|
269
246
|
});
|
|
270
247
|
}
|
|
271
248
|
|