weave-typescript 0.30.0 → 0.32.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/dist/google/api/annotations.pb.js +1 -1
- package/dist/google/api/http.pb.js +1 -1
- package/dist/google/protobuf/descriptor.pb.js +1 -1
- package/dist/google/protobuf/struct.pb.js +1 -1
- package/dist/google/protobuf/timestamp.pb.js +1 -1
- package/dist/weaveapi/atc/v1/event.pb.js +1 -1
- package/dist/weaveapi/atc/v1/message.pb.js +1 -1
- package/dist/weaveapi/atc/v1/process.pb.js +1 -1
- package/dist/weaveapi/atc/v1/run.pb.js +1 -1
- package/dist/weaveapi/atc/v1/service.pb.js +1 -1
- package/dist/weaveapi/auth/v1/auth.pb.d.ts +260 -0
- package/dist/weaveapi/auth/v1/auth.pb.js +3175 -0
- package/dist/weaveapi/auth/v1/service.pb.d.ts +761 -0
- package/dist/weaveapi/auth/v1/service.pb.js +6196 -0
- package/dist/weaveapi/chat/v1/chat.pb.js +1 -1
- package/dist/weaveapi/chat/v1/service.pb.js +1 -1
- package/dist/weaveapi/consolidation/v1/consolidation.pb.js +1 -1
- package/dist/weaveapi/consolidation/v1/service.pb.js +1 -1
- package/dist/weaveapi/inference/v1/inference.pb.js +1 -1
- package/dist/weaveapi/inference/v1/service.pb.js +1 -1
- package/dist/weaveapi/ingestion/v1/ingestion.pb.js +1 -1
- package/dist/weaveapi/ingestion/v1/service.pb.js +1 -1
- package/dist/weaveapi/model/v1/model.pb.js +1 -1
- package/dist/weaveapi/model/v1/service.pb.js +1 -1
- package/dist/weaveapi/organization/v1/organization.pb.js +1 -1
- package/dist/weaveapi/organization/v1/service.pb.js +1 -1
- package/dist/weaveapi/project/v1/project.pb.js +1 -1
- package/dist/weaveapi/project/v1/service.pb.js +1 -1
- package/dist/weaveapi/provider/v1/provider.pb.js +1 -1
- package/dist/weaveapi/provider/v1/service.pb.js +1 -1
- package/dist/weaveapi/retrieval/v1/retrieval.pb.js +1 -1
- package/dist/weaveapi/retrieval/v1/service.pb.js +1 -1
- package/dist/weaveapi/task/v1/service.pb.js +1 -1
- package/dist/weaveapi/task/v1/task.pb.js +1 -1
- package/dist/weavesql/weavedb/auth_identity_sql.d.ts +525 -0
- package/dist/weavesql/weavedb/auth_identity_sql.js +1233 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deleteExpiredApplicationSessionsQuery = exports.markApplicationSessionOrganizationSessionStateQuery = exports.listApplicationSessionOrganizationSessionStatesQuery = exports.getApplicationSessionOrganizationSessionQuery = exports.upsertApplicationSessionOrganizationSessionQuery = exports.revokeApplicationSessionQuery = exports.setApplicationSessionActiveOrganizationQuery = exports.getApplicationSessionQuery = exports.createApplicationSessionQuery = exports.insertAuthAuditEventQuery = exports.endSupportAccessSessionQuery = exports.createSupportAccessSessionQuery = exports.createPlatformAdminGrantQuery = exports.listActivePlatformAdminGrantsForUserQuery = exports.listOrganizationMembersQuery = exports.listOrganizationMembershipsForUserQuery = exports.getOrganizationMembershipQuery = exports.upsertOrganizationMembershipQuery = exports.upsertOrganizationAuthLinkQuery = exports.getOrganizationAuthLinkByOrganizationIDQuery = exports.getOrganizationAuthLinkByStytchIDQuery = exports.upsertIdentityLinkQuery = exports.getIdentityLinkByStytchMemberQuery = exports.getUserProfileQuery = exports.upsertUserProfileQuery = exports.deleteOrganizationAuthDomainQuery = exports.listOrganizationAuthDomainsQuery = exports.upsertOrganizationAuthDomainQuery = exports.resolvePrimaryOrganizationAuthLinkByDomainQuery = exports.getUserPrimaryOrganizationAuthLinkQuery = exports.setUserPrimaryOrganizationQuery = exports.getUserByIDQuery = exports.createUserQuery = void 0;
|
|
4
|
+
exports.createUser = createUser;
|
|
5
|
+
exports.getUserByID = getUserByID;
|
|
6
|
+
exports.setUserPrimaryOrganization = setUserPrimaryOrganization;
|
|
7
|
+
exports.getUserPrimaryOrganizationAuthLink = getUserPrimaryOrganizationAuthLink;
|
|
8
|
+
exports.resolvePrimaryOrganizationAuthLinkByDomain = resolvePrimaryOrganizationAuthLinkByDomain;
|
|
9
|
+
exports.upsertOrganizationAuthDomain = upsertOrganizationAuthDomain;
|
|
10
|
+
exports.listOrganizationAuthDomains = listOrganizationAuthDomains;
|
|
11
|
+
exports.upsertUserProfile = upsertUserProfile;
|
|
12
|
+
exports.getUserProfile = getUserProfile;
|
|
13
|
+
exports.getIdentityLinkByStytchMember = getIdentityLinkByStytchMember;
|
|
14
|
+
exports.upsertIdentityLink = upsertIdentityLink;
|
|
15
|
+
exports.getOrganizationAuthLinkByStytchID = getOrganizationAuthLinkByStytchID;
|
|
16
|
+
exports.getOrganizationAuthLinkByOrganizationID = getOrganizationAuthLinkByOrganizationID;
|
|
17
|
+
exports.upsertOrganizationAuthLink = upsertOrganizationAuthLink;
|
|
18
|
+
exports.upsertOrganizationMembership = upsertOrganizationMembership;
|
|
19
|
+
exports.getOrganizationMembership = getOrganizationMembership;
|
|
20
|
+
exports.listOrganizationMembershipsForUser = listOrganizationMembershipsForUser;
|
|
21
|
+
exports.listOrganizationMembers = listOrganizationMembers;
|
|
22
|
+
exports.listActivePlatformAdminGrantsForUser = listActivePlatformAdminGrantsForUser;
|
|
23
|
+
exports.createPlatformAdminGrant = createPlatformAdminGrant;
|
|
24
|
+
exports.createSupportAccessSession = createSupportAccessSession;
|
|
25
|
+
exports.insertAuthAuditEvent = insertAuthAuditEvent;
|
|
26
|
+
exports.createApplicationSession = createApplicationSession;
|
|
27
|
+
exports.getApplicationSession = getApplicationSession;
|
|
28
|
+
exports.setApplicationSessionActiveOrganization = setApplicationSessionActiveOrganization;
|
|
29
|
+
exports.upsertApplicationSessionOrganizationSession = upsertApplicationSessionOrganizationSession;
|
|
30
|
+
exports.getApplicationSessionOrganizationSession = getApplicationSessionOrganizationSession;
|
|
31
|
+
exports.listApplicationSessionOrganizationSessionStates = listApplicationSessionOrganizationSessionStates;
|
|
32
|
+
exports.markApplicationSessionOrganizationSessionState = markApplicationSessionOrganizationSessionState;
|
|
33
|
+
exports.createUserQuery = `-- name: CreateUser :one
|
|
34
|
+
INSERT INTO weave.users (status)
|
|
35
|
+
VALUES ($1)
|
|
36
|
+
RETURNING id, status, created_at, updated_at`;
|
|
37
|
+
async function createUser(client, args) {
|
|
38
|
+
const result = await client.query({
|
|
39
|
+
text: exports.createUserQuery,
|
|
40
|
+
values: [args.status],
|
|
41
|
+
rowMode: "array"
|
|
42
|
+
});
|
|
43
|
+
if (result.rows.length !== 1) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const row = result.rows[0];
|
|
47
|
+
return {
|
|
48
|
+
id: row[0],
|
|
49
|
+
status: row[1],
|
|
50
|
+
createdAt: row[2],
|
|
51
|
+
updatedAt: row[3]
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
exports.getUserByIDQuery = `-- name: GetUserByID :one
|
|
55
|
+
SELECT id, status, created_at, updated_at
|
|
56
|
+
FROM weave.users
|
|
57
|
+
WHERE id = $1`;
|
|
58
|
+
async function getUserByID(client, args) {
|
|
59
|
+
const result = await client.query({
|
|
60
|
+
text: exports.getUserByIDQuery,
|
|
61
|
+
values: [args.id],
|
|
62
|
+
rowMode: "array"
|
|
63
|
+
});
|
|
64
|
+
if (result.rows.length !== 1) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const row = result.rows[0];
|
|
68
|
+
return {
|
|
69
|
+
id: row[0],
|
|
70
|
+
status: row[1],
|
|
71
|
+
createdAt: row[2],
|
|
72
|
+
updatedAt: row[3]
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
exports.setUserPrimaryOrganizationQuery = `-- name: SetUserPrimaryOrganization :one
|
|
76
|
+
UPDATE weave.users
|
|
77
|
+
SET
|
|
78
|
+
primary_organization_id = $1,
|
|
79
|
+
updated_at = now()
|
|
80
|
+
WHERE id = $2
|
|
81
|
+
RETURNING
|
|
82
|
+
id,
|
|
83
|
+
primary_organization_id,
|
|
84
|
+
status,
|
|
85
|
+
created_at,
|
|
86
|
+
updated_at`;
|
|
87
|
+
async function setUserPrimaryOrganization(client, args) {
|
|
88
|
+
const result = await client.query({
|
|
89
|
+
text: exports.setUserPrimaryOrganizationQuery,
|
|
90
|
+
values: [args.organizationId, args.userId],
|
|
91
|
+
rowMode: "array"
|
|
92
|
+
});
|
|
93
|
+
if (result.rows.length !== 1) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const row = result.rows[0];
|
|
97
|
+
return {
|
|
98
|
+
id: row[0],
|
|
99
|
+
primaryOrganizationId: row[1],
|
|
100
|
+
status: row[2],
|
|
101
|
+
createdAt: row[3],
|
|
102
|
+
updatedAt: row[4]
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
exports.getUserPrimaryOrganizationAuthLinkQuery = `-- name: GetUserPrimaryOrganizationAuthLink :one
|
|
106
|
+
SELECT
|
|
107
|
+
o.id AS organization_id,
|
|
108
|
+
o.slug AS organization_slug,
|
|
109
|
+
o.name AS organization_name,
|
|
110
|
+
l.stytch_organization_id,
|
|
111
|
+
l.stytch_organization_slug,
|
|
112
|
+
l.stytch_organization_kind,
|
|
113
|
+
l.allowed_domains,
|
|
114
|
+
l.auth_policy_summary,
|
|
115
|
+
l.created_at,
|
|
116
|
+
l.updated_at
|
|
117
|
+
FROM weave.users AS u
|
|
118
|
+
JOIN weave.organizations AS o
|
|
119
|
+
ON o.id = u.primary_organization_id
|
|
120
|
+
JOIN weave.organization_auth_links AS l
|
|
121
|
+
ON l.organization_id = o.id
|
|
122
|
+
WHERE u.id = $1
|
|
123
|
+
AND u.status = 'active'`;
|
|
124
|
+
async function getUserPrimaryOrganizationAuthLink(client, args) {
|
|
125
|
+
const result = await client.query({
|
|
126
|
+
text: exports.getUserPrimaryOrganizationAuthLinkQuery,
|
|
127
|
+
values: [args.userId],
|
|
128
|
+
rowMode: "array"
|
|
129
|
+
});
|
|
130
|
+
if (result.rows.length !== 1) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const row = result.rows[0];
|
|
134
|
+
return {
|
|
135
|
+
organizationId: row[0],
|
|
136
|
+
organizationSlug: row[1],
|
|
137
|
+
organizationName: row[2],
|
|
138
|
+
stytchOrganizationId: row[3],
|
|
139
|
+
stytchOrganizationSlug: row[4],
|
|
140
|
+
stytchOrganizationKind: row[5],
|
|
141
|
+
allowedDomains: row[6],
|
|
142
|
+
authPolicySummary: row[7],
|
|
143
|
+
createdAt: row[8],
|
|
144
|
+
updatedAt: row[9]
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
exports.resolvePrimaryOrganizationAuthLinkByDomainQuery = `-- name: ResolvePrimaryOrganizationAuthLinkByDomain :one
|
|
148
|
+
SELECT
|
|
149
|
+
o.id AS organization_id,
|
|
150
|
+
o.slug AS organization_slug,
|
|
151
|
+
o.name AS organization_name,
|
|
152
|
+
l.stytch_organization_id,
|
|
153
|
+
l.stytch_organization_slug,
|
|
154
|
+
l.stytch_organization_kind,
|
|
155
|
+
d.domain,
|
|
156
|
+
l.allowed_domains,
|
|
157
|
+
l.auth_policy_summary,
|
|
158
|
+
l.created_at,
|
|
159
|
+
l.updated_at
|
|
160
|
+
FROM weave.organization_auth_domains AS d
|
|
161
|
+
JOIN weave.organization_auth_links AS l
|
|
162
|
+
ON l.organization_id = d.organization_id
|
|
163
|
+
JOIN weave.organizations AS o
|
|
164
|
+
ON o.id = d.organization_id
|
|
165
|
+
WHERE d.domain = lower($1)
|
|
166
|
+
AND d.is_primary_login
|
|
167
|
+
AND d.status = 'active'`;
|
|
168
|
+
async function resolvePrimaryOrganizationAuthLinkByDomain(client, args) {
|
|
169
|
+
const result = await client.query({
|
|
170
|
+
text: exports.resolvePrimaryOrganizationAuthLinkByDomainQuery,
|
|
171
|
+
values: [args.domain],
|
|
172
|
+
rowMode: "array"
|
|
173
|
+
});
|
|
174
|
+
if (result.rows.length !== 1) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
const row = result.rows[0];
|
|
178
|
+
return {
|
|
179
|
+
organizationId: row[0],
|
|
180
|
+
organizationSlug: row[1],
|
|
181
|
+
organizationName: row[2],
|
|
182
|
+
stytchOrganizationId: row[3],
|
|
183
|
+
stytchOrganizationSlug: row[4],
|
|
184
|
+
stytchOrganizationKind: row[5],
|
|
185
|
+
domain: row[6],
|
|
186
|
+
allowedDomains: row[7],
|
|
187
|
+
authPolicySummary: row[8],
|
|
188
|
+
createdAt: row[9],
|
|
189
|
+
updatedAt: row[10]
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
exports.upsertOrganizationAuthDomainQuery = `-- name: UpsertOrganizationAuthDomain :one
|
|
193
|
+
INSERT INTO weave.organization_auth_domains (
|
|
194
|
+
organization_id,
|
|
195
|
+
domain,
|
|
196
|
+
is_primary_login,
|
|
197
|
+
status
|
|
198
|
+
) VALUES (
|
|
199
|
+
$1,
|
|
200
|
+
lower($2),
|
|
201
|
+
$3,
|
|
202
|
+
$4
|
|
203
|
+
)
|
|
204
|
+
ON CONFLICT (organization_id, domain) DO UPDATE
|
|
205
|
+
SET
|
|
206
|
+
is_primary_login = EXCLUDED.is_primary_login,
|
|
207
|
+
status = EXCLUDED.status,
|
|
208
|
+
updated_at = now()
|
|
209
|
+
RETURNING
|
|
210
|
+
id,
|
|
211
|
+
organization_id,
|
|
212
|
+
domain,
|
|
213
|
+
is_primary_login,
|
|
214
|
+
status,
|
|
215
|
+
created_at,
|
|
216
|
+
updated_at`;
|
|
217
|
+
async function upsertOrganizationAuthDomain(client, args) {
|
|
218
|
+
const result = await client.query({
|
|
219
|
+
text: exports.upsertOrganizationAuthDomainQuery,
|
|
220
|
+
values: [args.organizationId, args.domain, args.isPrimaryLogin, args.status],
|
|
221
|
+
rowMode: "array"
|
|
222
|
+
});
|
|
223
|
+
if (result.rows.length !== 1) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
const row = result.rows[0];
|
|
227
|
+
return {
|
|
228
|
+
id: row[0],
|
|
229
|
+
organizationId: row[1],
|
|
230
|
+
domain: row[2],
|
|
231
|
+
isPrimaryLogin: row[3],
|
|
232
|
+
status: row[4],
|
|
233
|
+
createdAt: row[5],
|
|
234
|
+
updatedAt: row[6]
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
exports.listOrganizationAuthDomainsQuery = `-- name: ListOrganizationAuthDomains :many
|
|
238
|
+
SELECT
|
|
239
|
+
id,
|
|
240
|
+
organization_id,
|
|
241
|
+
domain,
|
|
242
|
+
is_primary_login,
|
|
243
|
+
status,
|
|
244
|
+
created_at,
|
|
245
|
+
updated_at
|
|
246
|
+
FROM weave.organization_auth_domains
|
|
247
|
+
WHERE organization_id = $1
|
|
248
|
+
ORDER BY is_primary_login DESC, domain ASC`;
|
|
249
|
+
async function listOrganizationAuthDomains(client, args) {
|
|
250
|
+
const result = await client.query({
|
|
251
|
+
text: exports.listOrganizationAuthDomainsQuery,
|
|
252
|
+
values: [args.organizationId],
|
|
253
|
+
rowMode: "array"
|
|
254
|
+
});
|
|
255
|
+
return result.rows.map(row => {
|
|
256
|
+
return {
|
|
257
|
+
id: row[0],
|
|
258
|
+
organizationId: row[1],
|
|
259
|
+
domain: row[2],
|
|
260
|
+
isPrimaryLogin: row[3],
|
|
261
|
+
status: row[4],
|
|
262
|
+
createdAt: row[5],
|
|
263
|
+
updatedAt: row[6]
|
|
264
|
+
};
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
exports.deleteOrganizationAuthDomainQuery = `-- name: DeleteOrganizationAuthDomain :execrows
|
|
268
|
+
DELETE FROM weave.organization_auth_domains
|
|
269
|
+
WHERE organization_id = $1
|
|
270
|
+
AND domain = lower($2)`;
|
|
271
|
+
exports.upsertUserProfileQuery = `-- name: UpsertUserProfile :one
|
|
272
|
+
INSERT INTO weave.user_profiles (
|
|
273
|
+
user_id,
|
|
274
|
+
display_name,
|
|
275
|
+
job_title,
|
|
276
|
+
email,
|
|
277
|
+
avatar_url,
|
|
278
|
+
theme
|
|
279
|
+
) VALUES (
|
|
280
|
+
$1,
|
|
281
|
+
$2,
|
|
282
|
+
$3,
|
|
283
|
+
$4,
|
|
284
|
+
$5,
|
|
285
|
+
$6
|
|
286
|
+
)
|
|
287
|
+
ON CONFLICT (user_id) DO UPDATE
|
|
288
|
+
SET
|
|
289
|
+
display_name = EXCLUDED.display_name,
|
|
290
|
+
job_title = EXCLUDED.job_title,
|
|
291
|
+
email = EXCLUDED.email,
|
|
292
|
+
avatar_url = EXCLUDED.avatar_url,
|
|
293
|
+
theme = EXCLUDED.theme,
|
|
294
|
+
updated_at = now()
|
|
295
|
+
RETURNING
|
|
296
|
+
user_id,
|
|
297
|
+
display_name,
|
|
298
|
+
job_title,
|
|
299
|
+
email,
|
|
300
|
+
avatar_url,
|
|
301
|
+
theme,
|
|
302
|
+
created_at,
|
|
303
|
+
updated_at`;
|
|
304
|
+
async function upsertUserProfile(client, args) {
|
|
305
|
+
const result = await client.query({
|
|
306
|
+
text: exports.upsertUserProfileQuery,
|
|
307
|
+
values: [args.userId, args.displayName, args.jobTitle, args.email, args.avatarUrl, args.theme],
|
|
308
|
+
rowMode: "array"
|
|
309
|
+
});
|
|
310
|
+
if (result.rows.length !== 1) {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
const row = result.rows[0];
|
|
314
|
+
return {
|
|
315
|
+
userId: row[0],
|
|
316
|
+
displayName: row[1],
|
|
317
|
+
jobTitle: row[2],
|
|
318
|
+
email: row[3],
|
|
319
|
+
avatarUrl: row[4],
|
|
320
|
+
theme: row[5],
|
|
321
|
+
createdAt: row[6],
|
|
322
|
+
updatedAt: row[7]
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
exports.getUserProfileQuery = `-- name: GetUserProfile :one
|
|
326
|
+
SELECT
|
|
327
|
+
user_id,
|
|
328
|
+
display_name,
|
|
329
|
+
job_title,
|
|
330
|
+
email,
|
|
331
|
+
avatar_url,
|
|
332
|
+
theme,
|
|
333
|
+
created_at,
|
|
334
|
+
updated_at
|
|
335
|
+
FROM weave.user_profiles
|
|
336
|
+
WHERE user_id = $1`;
|
|
337
|
+
async function getUserProfile(client, args) {
|
|
338
|
+
const result = await client.query({
|
|
339
|
+
text: exports.getUserProfileQuery,
|
|
340
|
+
values: [args.userId],
|
|
341
|
+
rowMode: "array"
|
|
342
|
+
});
|
|
343
|
+
if (result.rows.length !== 1) {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
const row = result.rows[0];
|
|
347
|
+
return {
|
|
348
|
+
userId: row[0],
|
|
349
|
+
displayName: row[1],
|
|
350
|
+
jobTitle: row[2],
|
|
351
|
+
email: row[3],
|
|
352
|
+
avatarUrl: row[4],
|
|
353
|
+
theme: row[5],
|
|
354
|
+
createdAt: row[6],
|
|
355
|
+
updatedAt: row[7]
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
exports.getIdentityLinkByStytchMemberQuery = `-- name: GetIdentityLinkByStytchMember :one
|
|
359
|
+
SELECT
|
|
360
|
+
id,
|
|
361
|
+
user_id,
|
|
362
|
+
organization_id,
|
|
363
|
+
provider,
|
|
364
|
+
stytch_organization_id,
|
|
365
|
+
stytch_member_id,
|
|
366
|
+
stytch_member_email,
|
|
367
|
+
last_seen_at,
|
|
368
|
+
created_at,
|
|
369
|
+
updated_at
|
|
370
|
+
FROM weave.identity_links
|
|
371
|
+
WHERE provider = 'stytch_b2b'
|
|
372
|
+
AND stytch_organization_id = $1
|
|
373
|
+
AND stytch_member_id = $2`;
|
|
374
|
+
async function getIdentityLinkByStytchMember(client, args) {
|
|
375
|
+
const result = await client.query({
|
|
376
|
+
text: exports.getIdentityLinkByStytchMemberQuery,
|
|
377
|
+
values: [args.stytchOrganizationId, args.stytchMemberId],
|
|
378
|
+
rowMode: "array"
|
|
379
|
+
});
|
|
380
|
+
if (result.rows.length !== 1) {
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
const row = result.rows[0];
|
|
384
|
+
return {
|
|
385
|
+
id: row[0],
|
|
386
|
+
userId: row[1],
|
|
387
|
+
organizationId: row[2],
|
|
388
|
+
provider: row[3],
|
|
389
|
+
stytchOrganizationId: row[4],
|
|
390
|
+
stytchMemberId: row[5],
|
|
391
|
+
stytchMemberEmail: row[6],
|
|
392
|
+
lastSeenAt: row[7],
|
|
393
|
+
createdAt: row[8],
|
|
394
|
+
updatedAt: row[9]
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
exports.upsertIdentityLinkQuery = `-- name: UpsertIdentityLink :one
|
|
398
|
+
INSERT INTO weave.identity_links (
|
|
399
|
+
user_id,
|
|
400
|
+
organization_id,
|
|
401
|
+
provider,
|
|
402
|
+
stytch_organization_id,
|
|
403
|
+
stytch_member_id,
|
|
404
|
+
stytch_member_email,
|
|
405
|
+
last_seen_at
|
|
406
|
+
) VALUES (
|
|
407
|
+
$1,
|
|
408
|
+
$2,
|
|
409
|
+
'stytch_b2b',
|
|
410
|
+
$3,
|
|
411
|
+
$4,
|
|
412
|
+
$5,
|
|
413
|
+
now()
|
|
414
|
+
)
|
|
415
|
+
ON CONFLICT (provider, stytch_organization_id, stytch_member_id) DO UPDATE
|
|
416
|
+
SET
|
|
417
|
+
user_id = EXCLUDED.user_id,
|
|
418
|
+
organization_id = EXCLUDED.organization_id,
|
|
419
|
+
stytch_member_email = EXCLUDED.stytch_member_email,
|
|
420
|
+
last_seen_at = now(),
|
|
421
|
+
updated_at = now()
|
|
422
|
+
RETURNING
|
|
423
|
+
id,
|
|
424
|
+
user_id,
|
|
425
|
+
organization_id,
|
|
426
|
+
provider,
|
|
427
|
+
stytch_organization_id,
|
|
428
|
+
stytch_member_id,
|
|
429
|
+
stytch_member_email,
|
|
430
|
+
last_seen_at,
|
|
431
|
+
created_at,
|
|
432
|
+
updated_at`;
|
|
433
|
+
async function upsertIdentityLink(client, args) {
|
|
434
|
+
const result = await client.query({
|
|
435
|
+
text: exports.upsertIdentityLinkQuery,
|
|
436
|
+
values: [args.userId, args.organizationId, args.stytchOrganizationId, args.stytchMemberId, args.stytchMemberEmail],
|
|
437
|
+
rowMode: "array"
|
|
438
|
+
});
|
|
439
|
+
if (result.rows.length !== 1) {
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
const row = result.rows[0];
|
|
443
|
+
return {
|
|
444
|
+
id: row[0],
|
|
445
|
+
userId: row[1],
|
|
446
|
+
organizationId: row[2],
|
|
447
|
+
provider: row[3],
|
|
448
|
+
stytchOrganizationId: row[4],
|
|
449
|
+
stytchMemberId: row[5],
|
|
450
|
+
stytchMemberEmail: row[6],
|
|
451
|
+
lastSeenAt: row[7],
|
|
452
|
+
createdAt: row[8],
|
|
453
|
+
updatedAt: row[9]
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
exports.getOrganizationAuthLinkByStytchIDQuery = `-- name: GetOrganizationAuthLinkByStytchID :one
|
|
457
|
+
SELECT
|
|
458
|
+
organization_id,
|
|
459
|
+
stytch_organization_id,
|
|
460
|
+
stytch_organization_slug,
|
|
461
|
+
stytch_organization_kind,
|
|
462
|
+
allowed_domains,
|
|
463
|
+
auth_policy_summary,
|
|
464
|
+
created_at,
|
|
465
|
+
updated_at
|
|
466
|
+
FROM weave.organization_auth_links
|
|
467
|
+
WHERE stytch_organization_id = $1`;
|
|
468
|
+
async function getOrganizationAuthLinkByStytchID(client, args) {
|
|
469
|
+
const result = await client.query({
|
|
470
|
+
text: exports.getOrganizationAuthLinkByStytchIDQuery,
|
|
471
|
+
values: [args.stytchOrganizationId],
|
|
472
|
+
rowMode: "array"
|
|
473
|
+
});
|
|
474
|
+
if (result.rows.length !== 1) {
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
const row = result.rows[0];
|
|
478
|
+
return {
|
|
479
|
+
organizationId: row[0],
|
|
480
|
+
stytchOrganizationId: row[1],
|
|
481
|
+
stytchOrganizationSlug: row[2],
|
|
482
|
+
stytchOrganizationKind: row[3],
|
|
483
|
+
allowedDomains: row[4],
|
|
484
|
+
authPolicySummary: row[5],
|
|
485
|
+
createdAt: row[6],
|
|
486
|
+
updatedAt: row[7]
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
exports.getOrganizationAuthLinkByOrganizationIDQuery = `-- name: GetOrganizationAuthLinkByOrganizationID :one
|
|
490
|
+
SELECT
|
|
491
|
+
organization_id,
|
|
492
|
+
stytch_organization_id,
|
|
493
|
+
stytch_organization_slug,
|
|
494
|
+
stytch_organization_kind,
|
|
495
|
+
allowed_domains,
|
|
496
|
+
auth_policy_summary,
|
|
497
|
+
created_at,
|
|
498
|
+
updated_at
|
|
499
|
+
FROM weave.organization_auth_links
|
|
500
|
+
WHERE organization_id = $1`;
|
|
501
|
+
async function getOrganizationAuthLinkByOrganizationID(client, args) {
|
|
502
|
+
const result = await client.query({
|
|
503
|
+
text: exports.getOrganizationAuthLinkByOrganizationIDQuery,
|
|
504
|
+
values: [args.organizationId],
|
|
505
|
+
rowMode: "array"
|
|
506
|
+
});
|
|
507
|
+
if (result.rows.length !== 1) {
|
|
508
|
+
return null;
|
|
509
|
+
}
|
|
510
|
+
const row = result.rows[0];
|
|
511
|
+
return {
|
|
512
|
+
organizationId: row[0],
|
|
513
|
+
stytchOrganizationId: row[1],
|
|
514
|
+
stytchOrganizationSlug: row[2],
|
|
515
|
+
stytchOrganizationKind: row[3],
|
|
516
|
+
allowedDomains: row[4],
|
|
517
|
+
authPolicySummary: row[5],
|
|
518
|
+
createdAt: row[6],
|
|
519
|
+
updatedAt: row[7]
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
exports.upsertOrganizationAuthLinkQuery = `-- name: UpsertOrganizationAuthLink :one
|
|
523
|
+
INSERT INTO weave.organization_auth_links (
|
|
524
|
+
organization_id,
|
|
525
|
+
stytch_organization_id,
|
|
526
|
+
stytch_organization_slug,
|
|
527
|
+
stytch_organization_kind,
|
|
528
|
+
allowed_domains,
|
|
529
|
+
auth_policy_summary
|
|
530
|
+
) VALUES (
|
|
531
|
+
$1,
|
|
532
|
+
$2,
|
|
533
|
+
$3,
|
|
534
|
+
$4,
|
|
535
|
+
$5,
|
|
536
|
+
$6
|
|
537
|
+
)
|
|
538
|
+
ON CONFLICT (organization_id) DO UPDATE
|
|
539
|
+
SET
|
|
540
|
+
stytch_organization_id = EXCLUDED.stytch_organization_id,
|
|
541
|
+
stytch_organization_slug = EXCLUDED.stytch_organization_slug,
|
|
542
|
+
stytch_organization_kind = EXCLUDED.stytch_organization_kind,
|
|
543
|
+
allowed_domains = EXCLUDED.allowed_domains,
|
|
544
|
+
auth_policy_summary = EXCLUDED.auth_policy_summary,
|
|
545
|
+
updated_at = now()
|
|
546
|
+
RETURNING
|
|
547
|
+
organization_id,
|
|
548
|
+
stytch_organization_id,
|
|
549
|
+
stytch_organization_slug,
|
|
550
|
+
stytch_organization_kind,
|
|
551
|
+
allowed_domains,
|
|
552
|
+
auth_policy_summary,
|
|
553
|
+
created_at,
|
|
554
|
+
updated_at`;
|
|
555
|
+
async function upsertOrganizationAuthLink(client, args) {
|
|
556
|
+
const result = await client.query({
|
|
557
|
+
text: exports.upsertOrganizationAuthLinkQuery,
|
|
558
|
+
values: [args.organizationId, args.stytchOrganizationId, args.stytchOrganizationSlug, args.stytchOrganizationKind, args.allowedDomains, args.authPolicySummary],
|
|
559
|
+
rowMode: "array"
|
|
560
|
+
});
|
|
561
|
+
if (result.rows.length !== 1) {
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
const row = result.rows[0];
|
|
565
|
+
return {
|
|
566
|
+
organizationId: row[0],
|
|
567
|
+
stytchOrganizationId: row[1],
|
|
568
|
+
stytchOrganizationSlug: row[2],
|
|
569
|
+
stytchOrganizationKind: row[3],
|
|
570
|
+
allowedDomains: row[4],
|
|
571
|
+
authPolicySummary: row[5],
|
|
572
|
+
createdAt: row[6],
|
|
573
|
+
updatedAt: row[7]
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
exports.upsertOrganizationMembershipQuery = `-- name: UpsertOrganizationMembership :one
|
|
577
|
+
INSERT INTO weave.organization_memberships (
|
|
578
|
+
user_id,
|
|
579
|
+
organization_id,
|
|
580
|
+
identity_link_id,
|
|
581
|
+
status,
|
|
582
|
+
role_ids
|
|
583
|
+
) VALUES (
|
|
584
|
+
$1,
|
|
585
|
+
$2,
|
|
586
|
+
$3,
|
|
587
|
+
$4,
|
|
588
|
+
$5
|
|
589
|
+
)
|
|
590
|
+
ON CONFLICT (user_id, organization_id) DO UPDATE
|
|
591
|
+
SET
|
|
592
|
+
identity_link_id = EXCLUDED.identity_link_id,
|
|
593
|
+
status = EXCLUDED.status,
|
|
594
|
+
role_ids = EXCLUDED.role_ids,
|
|
595
|
+
updated_at = now()
|
|
596
|
+
RETURNING
|
|
597
|
+
id,
|
|
598
|
+
user_id,
|
|
599
|
+
organization_id,
|
|
600
|
+
identity_link_id,
|
|
601
|
+
status,
|
|
602
|
+
role_ids,
|
|
603
|
+
created_at,
|
|
604
|
+
updated_at`;
|
|
605
|
+
async function upsertOrganizationMembership(client, args) {
|
|
606
|
+
const result = await client.query({
|
|
607
|
+
text: exports.upsertOrganizationMembershipQuery,
|
|
608
|
+
values: [args.userId, args.organizationId, args.identityLinkId, args.status, args.roleIds],
|
|
609
|
+
rowMode: "array"
|
|
610
|
+
});
|
|
611
|
+
if (result.rows.length !== 1) {
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
const row = result.rows[0];
|
|
615
|
+
return {
|
|
616
|
+
id: row[0],
|
|
617
|
+
userId: row[1],
|
|
618
|
+
organizationId: row[2],
|
|
619
|
+
identityLinkId: row[3],
|
|
620
|
+
status: row[4],
|
|
621
|
+
roleIds: row[5],
|
|
622
|
+
createdAt: row[6],
|
|
623
|
+
updatedAt: row[7]
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
exports.getOrganizationMembershipQuery = `-- name: GetOrganizationMembership :one
|
|
627
|
+
SELECT
|
|
628
|
+
id,
|
|
629
|
+
user_id,
|
|
630
|
+
organization_id,
|
|
631
|
+
identity_link_id,
|
|
632
|
+
status,
|
|
633
|
+
role_ids,
|
|
634
|
+
created_at,
|
|
635
|
+
updated_at
|
|
636
|
+
FROM weave.organization_memberships
|
|
637
|
+
WHERE user_id = $1
|
|
638
|
+
AND organization_id = $2`;
|
|
639
|
+
async function getOrganizationMembership(client, args) {
|
|
640
|
+
const result = await client.query({
|
|
641
|
+
text: exports.getOrganizationMembershipQuery,
|
|
642
|
+
values: [args.userId, args.organizationId],
|
|
643
|
+
rowMode: "array"
|
|
644
|
+
});
|
|
645
|
+
if (result.rows.length !== 1) {
|
|
646
|
+
return null;
|
|
647
|
+
}
|
|
648
|
+
const row = result.rows[0];
|
|
649
|
+
return {
|
|
650
|
+
id: row[0],
|
|
651
|
+
userId: row[1],
|
|
652
|
+
organizationId: row[2],
|
|
653
|
+
identityLinkId: row[3],
|
|
654
|
+
status: row[4],
|
|
655
|
+
roleIds: row[5],
|
|
656
|
+
createdAt: row[6],
|
|
657
|
+
updatedAt: row[7]
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
exports.listOrganizationMembershipsForUserQuery = `-- name: ListOrganizationMembershipsForUser :many
|
|
661
|
+
SELECT
|
|
662
|
+
id,
|
|
663
|
+
user_id,
|
|
664
|
+
organization_id,
|
|
665
|
+
identity_link_id,
|
|
666
|
+
status,
|
|
667
|
+
role_ids,
|
|
668
|
+
created_at,
|
|
669
|
+
updated_at
|
|
670
|
+
FROM weave.organization_memberships
|
|
671
|
+
WHERE user_id = $1
|
|
672
|
+
ORDER BY created_at ASC`;
|
|
673
|
+
async function listOrganizationMembershipsForUser(client, args) {
|
|
674
|
+
const result = await client.query({
|
|
675
|
+
text: exports.listOrganizationMembershipsForUserQuery,
|
|
676
|
+
values: [args.userId],
|
|
677
|
+
rowMode: "array"
|
|
678
|
+
});
|
|
679
|
+
return result.rows.map(row => {
|
|
680
|
+
return {
|
|
681
|
+
id: row[0],
|
|
682
|
+
userId: row[1],
|
|
683
|
+
organizationId: row[2],
|
|
684
|
+
identityLinkId: row[3],
|
|
685
|
+
status: row[4],
|
|
686
|
+
roleIds: row[5],
|
|
687
|
+
createdAt: row[6],
|
|
688
|
+
updatedAt: row[7]
|
|
689
|
+
};
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
exports.listOrganizationMembersQuery = `-- name: ListOrganizationMembers :many
|
|
693
|
+
SELECT
|
|
694
|
+
m.id,
|
|
695
|
+
m.user_id,
|
|
696
|
+
m.organization_id,
|
|
697
|
+
m.identity_link_id,
|
|
698
|
+
m.status,
|
|
699
|
+
m.role_ids,
|
|
700
|
+
p.display_name,
|
|
701
|
+
p.email,
|
|
702
|
+
p.avatar_url,
|
|
703
|
+
m.created_at,
|
|
704
|
+
m.updated_at
|
|
705
|
+
FROM weave.organization_memberships AS m
|
|
706
|
+
JOIN weave.user_profiles AS p
|
|
707
|
+
ON p.user_id = m.user_id
|
|
708
|
+
WHERE m.organization_id = $1
|
|
709
|
+
ORDER BY p.display_name ASC, p.email ASC`;
|
|
710
|
+
async function listOrganizationMembers(client, args) {
|
|
711
|
+
const result = await client.query({
|
|
712
|
+
text: exports.listOrganizationMembersQuery,
|
|
713
|
+
values: [args.organizationId],
|
|
714
|
+
rowMode: "array"
|
|
715
|
+
});
|
|
716
|
+
return result.rows.map(row => {
|
|
717
|
+
return {
|
|
718
|
+
id: row[0],
|
|
719
|
+
userId: row[1],
|
|
720
|
+
organizationId: row[2],
|
|
721
|
+
identityLinkId: row[3],
|
|
722
|
+
status: row[4],
|
|
723
|
+
roleIds: row[5],
|
|
724
|
+
displayName: row[6],
|
|
725
|
+
email: row[7],
|
|
726
|
+
avatarUrl: row[8],
|
|
727
|
+
createdAt: row[9],
|
|
728
|
+
updatedAt: row[10]
|
|
729
|
+
};
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
exports.listActivePlatformAdminGrantsForUserQuery = `-- name: ListActivePlatformAdminGrantsForUser :many
|
|
733
|
+
SELECT
|
|
734
|
+
id,
|
|
735
|
+
user_id,
|
|
736
|
+
platform_role,
|
|
737
|
+
organization_id,
|
|
738
|
+
all_organizations,
|
|
739
|
+
allowed_actions,
|
|
740
|
+
expires_at,
|
|
741
|
+
status,
|
|
742
|
+
created_at,
|
|
743
|
+
updated_at
|
|
744
|
+
FROM weave.platform_admin_grants
|
|
745
|
+
WHERE user_id = $1
|
|
746
|
+
AND status = 'active'
|
|
747
|
+
AND (expires_at IS NULL OR expires_at > now())`;
|
|
748
|
+
async function listActivePlatformAdminGrantsForUser(client, args) {
|
|
749
|
+
const result = await client.query({
|
|
750
|
+
text: exports.listActivePlatformAdminGrantsForUserQuery,
|
|
751
|
+
values: [args.userId],
|
|
752
|
+
rowMode: "array"
|
|
753
|
+
});
|
|
754
|
+
return result.rows.map(row => {
|
|
755
|
+
return {
|
|
756
|
+
id: row[0],
|
|
757
|
+
userId: row[1],
|
|
758
|
+
platformRole: row[2],
|
|
759
|
+
organizationId: row[3],
|
|
760
|
+
allOrganizations: row[4],
|
|
761
|
+
allowedActions: row[5],
|
|
762
|
+
expiresAt: row[6],
|
|
763
|
+
status: row[7],
|
|
764
|
+
createdAt: row[8],
|
|
765
|
+
updatedAt: row[9]
|
|
766
|
+
};
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
exports.createPlatformAdminGrantQuery = `-- name: CreatePlatformAdminGrant :one
|
|
770
|
+
INSERT INTO weave.platform_admin_grants (
|
|
771
|
+
user_id,
|
|
772
|
+
platform_role,
|
|
773
|
+
organization_id,
|
|
774
|
+
all_organizations,
|
|
775
|
+
allowed_actions,
|
|
776
|
+
expires_at,
|
|
777
|
+
status
|
|
778
|
+
) VALUES (
|
|
779
|
+
$1,
|
|
780
|
+
$2,
|
|
781
|
+
$3,
|
|
782
|
+
$4,
|
|
783
|
+
$5,
|
|
784
|
+
$6,
|
|
785
|
+
$7
|
|
786
|
+
)
|
|
787
|
+
RETURNING
|
|
788
|
+
id,
|
|
789
|
+
user_id,
|
|
790
|
+
platform_role,
|
|
791
|
+
organization_id,
|
|
792
|
+
all_organizations,
|
|
793
|
+
allowed_actions,
|
|
794
|
+
expires_at,
|
|
795
|
+
status,
|
|
796
|
+
created_at,
|
|
797
|
+
updated_at`;
|
|
798
|
+
async function createPlatformAdminGrant(client, args) {
|
|
799
|
+
const result = await client.query({
|
|
800
|
+
text: exports.createPlatformAdminGrantQuery,
|
|
801
|
+
values: [args.userId, args.platformRole, args.organizationId, args.allOrganizations, args.allowedActions, args.expiresAt, args.status],
|
|
802
|
+
rowMode: "array"
|
|
803
|
+
});
|
|
804
|
+
if (result.rows.length !== 1) {
|
|
805
|
+
return null;
|
|
806
|
+
}
|
|
807
|
+
const row = result.rows[0];
|
|
808
|
+
return {
|
|
809
|
+
id: row[0],
|
|
810
|
+
userId: row[1],
|
|
811
|
+
platformRole: row[2],
|
|
812
|
+
organizationId: row[3],
|
|
813
|
+
allOrganizations: row[4],
|
|
814
|
+
allowedActions: row[5],
|
|
815
|
+
expiresAt: row[6],
|
|
816
|
+
status: row[7],
|
|
817
|
+
createdAt: row[8],
|
|
818
|
+
updatedAt: row[9]
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
exports.createSupportAccessSessionQuery = `-- name: CreateSupportAccessSession :one
|
|
822
|
+
INSERT INTO weave.support_access_sessions (
|
|
823
|
+
acting_user_id,
|
|
824
|
+
target_organization_id,
|
|
825
|
+
reason,
|
|
826
|
+
allowed_actions
|
|
827
|
+
) VALUES (
|
|
828
|
+
$1,
|
|
829
|
+
$2,
|
|
830
|
+
$3,
|
|
831
|
+
$4
|
|
832
|
+
)
|
|
833
|
+
RETURNING
|
|
834
|
+
id,
|
|
835
|
+
acting_user_id,
|
|
836
|
+
target_organization_id,
|
|
837
|
+
reason,
|
|
838
|
+
allowed_actions,
|
|
839
|
+
started_at,
|
|
840
|
+
ended_at,
|
|
841
|
+
created_at`;
|
|
842
|
+
async function createSupportAccessSession(client, args) {
|
|
843
|
+
const result = await client.query({
|
|
844
|
+
text: exports.createSupportAccessSessionQuery,
|
|
845
|
+
values: [args.actingUserId, args.targetOrganizationId, args.reason, args.allowedActions],
|
|
846
|
+
rowMode: "array"
|
|
847
|
+
});
|
|
848
|
+
if (result.rows.length !== 1) {
|
|
849
|
+
return null;
|
|
850
|
+
}
|
|
851
|
+
const row = result.rows[0];
|
|
852
|
+
return {
|
|
853
|
+
id: row[0],
|
|
854
|
+
actingUserId: row[1],
|
|
855
|
+
targetOrganizationId: row[2],
|
|
856
|
+
reason: row[3],
|
|
857
|
+
allowedActions: row[4],
|
|
858
|
+
startedAt: row[5],
|
|
859
|
+
endedAt: row[6],
|
|
860
|
+
createdAt: row[7]
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
exports.endSupportAccessSessionQuery = `-- name: EndSupportAccessSession :execrows
|
|
864
|
+
UPDATE weave.support_access_sessions
|
|
865
|
+
SET ended_at = now()
|
|
866
|
+
WHERE id = $1
|
|
867
|
+
AND ended_at IS NULL`;
|
|
868
|
+
exports.insertAuthAuditEventQuery = `-- name: InsertAuthAuditEvent :exec
|
|
869
|
+
INSERT INTO weave.auth_audit_events (
|
|
870
|
+
organization_id,
|
|
871
|
+
acting_user_id,
|
|
872
|
+
target_user_id,
|
|
873
|
+
event_type,
|
|
874
|
+
auth_method,
|
|
875
|
+
stytch_request_id,
|
|
876
|
+
stytch_organization_id,
|
|
877
|
+
stytch_member_id,
|
|
878
|
+
support_access_session_id,
|
|
879
|
+
metadata
|
|
880
|
+
) VALUES (
|
|
881
|
+
$1,
|
|
882
|
+
$2,
|
|
883
|
+
$3,
|
|
884
|
+
$4,
|
|
885
|
+
$5,
|
|
886
|
+
$6,
|
|
887
|
+
$7,
|
|
888
|
+
$8,
|
|
889
|
+
$9,
|
|
890
|
+
$10
|
|
891
|
+
)`;
|
|
892
|
+
async function insertAuthAuditEvent(client, args) {
|
|
893
|
+
await client.query({
|
|
894
|
+
text: exports.insertAuthAuditEventQuery,
|
|
895
|
+
values: [args.organizationId, args.actingUserId, args.targetUserId, args.eventType, args.authMethod, args.stytchRequestId, args.stytchOrganizationId, args.stytchMemberId, args.supportAccessSessionId, args.metadata],
|
|
896
|
+
rowMode: "array"
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
exports.createApplicationSessionQuery = `-- name: CreateApplicationSession :one
|
|
900
|
+
INSERT INTO weave.application_sessions (
|
|
901
|
+
user_id,
|
|
902
|
+
active_organization_id,
|
|
903
|
+
expires_at
|
|
904
|
+
) VALUES (
|
|
905
|
+
$1,
|
|
906
|
+
$2,
|
|
907
|
+
$3
|
|
908
|
+
)
|
|
909
|
+
RETURNING
|
|
910
|
+
id,
|
|
911
|
+
user_id,
|
|
912
|
+
active_organization_id,
|
|
913
|
+
status,
|
|
914
|
+
expires_at,
|
|
915
|
+
revoked_at,
|
|
916
|
+
created_at,
|
|
917
|
+
updated_at`;
|
|
918
|
+
async function createApplicationSession(client, args) {
|
|
919
|
+
const result = await client.query({
|
|
920
|
+
text: exports.createApplicationSessionQuery,
|
|
921
|
+
values: [args.userId, args.activeOrganizationId, args.expiresAt],
|
|
922
|
+
rowMode: "array"
|
|
923
|
+
});
|
|
924
|
+
if (result.rows.length !== 1) {
|
|
925
|
+
return null;
|
|
926
|
+
}
|
|
927
|
+
const row = result.rows[0];
|
|
928
|
+
return {
|
|
929
|
+
id: row[0],
|
|
930
|
+
userId: row[1],
|
|
931
|
+
activeOrganizationId: row[2],
|
|
932
|
+
status: row[3],
|
|
933
|
+
expiresAt: row[4],
|
|
934
|
+
revokedAt: row[5],
|
|
935
|
+
createdAt: row[6],
|
|
936
|
+
updatedAt: row[7]
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
exports.getApplicationSessionQuery = `-- name: GetApplicationSession :one
|
|
940
|
+
SELECT
|
|
941
|
+
id,
|
|
942
|
+
user_id,
|
|
943
|
+
active_organization_id,
|
|
944
|
+
status,
|
|
945
|
+
expires_at,
|
|
946
|
+
revoked_at,
|
|
947
|
+
created_at,
|
|
948
|
+
updated_at
|
|
949
|
+
FROM weave.application_sessions
|
|
950
|
+
WHERE id = $1`;
|
|
951
|
+
async function getApplicationSession(client, args) {
|
|
952
|
+
const result = await client.query({
|
|
953
|
+
text: exports.getApplicationSessionQuery,
|
|
954
|
+
values: [args.id],
|
|
955
|
+
rowMode: "array"
|
|
956
|
+
});
|
|
957
|
+
if (result.rows.length !== 1) {
|
|
958
|
+
return null;
|
|
959
|
+
}
|
|
960
|
+
const row = result.rows[0];
|
|
961
|
+
return {
|
|
962
|
+
id: row[0],
|
|
963
|
+
userId: row[1],
|
|
964
|
+
activeOrganizationId: row[2],
|
|
965
|
+
status: row[3],
|
|
966
|
+
expiresAt: row[4],
|
|
967
|
+
revokedAt: row[5],
|
|
968
|
+
createdAt: row[6],
|
|
969
|
+
updatedAt: row[7]
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
exports.setApplicationSessionActiveOrganizationQuery = `-- name: SetApplicationSessionActiveOrganization :one
|
|
973
|
+
UPDATE weave.application_sessions
|
|
974
|
+
SET
|
|
975
|
+
active_organization_id = $1,
|
|
976
|
+
updated_at = now()
|
|
977
|
+
WHERE id = $2
|
|
978
|
+
AND status = 'active'
|
|
979
|
+
RETURNING
|
|
980
|
+
id,
|
|
981
|
+
user_id,
|
|
982
|
+
active_organization_id,
|
|
983
|
+
status,
|
|
984
|
+
expires_at,
|
|
985
|
+
revoked_at,
|
|
986
|
+
created_at,
|
|
987
|
+
updated_at`;
|
|
988
|
+
async function setApplicationSessionActiveOrganization(client, args) {
|
|
989
|
+
const result = await client.query({
|
|
990
|
+
text: exports.setApplicationSessionActiveOrganizationQuery,
|
|
991
|
+
values: [args.organizationId, args.id],
|
|
992
|
+
rowMode: "array"
|
|
993
|
+
});
|
|
994
|
+
if (result.rows.length !== 1) {
|
|
995
|
+
return null;
|
|
996
|
+
}
|
|
997
|
+
const row = result.rows[0];
|
|
998
|
+
return {
|
|
999
|
+
id: row[0],
|
|
1000
|
+
userId: row[1],
|
|
1001
|
+
activeOrganizationId: row[2],
|
|
1002
|
+
status: row[3],
|
|
1003
|
+
expiresAt: row[4],
|
|
1004
|
+
revokedAt: row[5],
|
|
1005
|
+
createdAt: row[6],
|
|
1006
|
+
updatedAt: row[7]
|
|
1007
|
+
};
|
|
1008
|
+
}
|
|
1009
|
+
exports.revokeApplicationSessionQuery = `-- name: RevokeApplicationSession :execrows
|
|
1010
|
+
UPDATE weave.application_sessions
|
|
1011
|
+
SET
|
|
1012
|
+
status = 'revoked',
|
|
1013
|
+
revoked_at = now(),
|
|
1014
|
+
updated_at = now()
|
|
1015
|
+
WHERE id = $1
|
|
1016
|
+
AND status = 'active'`;
|
|
1017
|
+
exports.upsertApplicationSessionOrganizationSessionQuery = `-- name: UpsertApplicationSessionOrganizationSession :one
|
|
1018
|
+
INSERT INTO weave.application_session_organization_sessions (
|
|
1019
|
+
application_session_id,
|
|
1020
|
+
organization_id,
|
|
1021
|
+
identity_link_id,
|
|
1022
|
+
stytch_organization_id,
|
|
1023
|
+
stytch_member_id,
|
|
1024
|
+
stytch_member_session_id,
|
|
1025
|
+
stytch_session_token_ciphertext,
|
|
1026
|
+
auth_state,
|
|
1027
|
+
expires_at,
|
|
1028
|
+
last_validated_at
|
|
1029
|
+
) VALUES (
|
|
1030
|
+
$1,
|
|
1031
|
+
$2,
|
|
1032
|
+
$3,
|
|
1033
|
+
$4,
|
|
1034
|
+
$5,
|
|
1035
|
+
$6,
|
|
1036
|
+
$7,
|
|
1037
|
+
$8,
|
|
1038
|
+
$9,
|
|
1039
|
+
$10
|
|
1040
|
+
)
|
|
1041
|
+
ON CONFLICT (application_session_id, organization_id) DO UPDATE
|
|
1042
|
+
SET
|
|
1043
|
+
identity_link_id = EXCLUDED.identity_link_id,
|
|
1044
|
+
stytch_organization_id = EXCLUDED.stytch_organization_id,
|
|
1045
|
+
stytch_member_id = EXCLUDED.stytch_member_id,
|
|
1046
|
+
stytch_member_session_id = EXCLUDED.stytch_member_session_id,
|
|
1047
|
+
stytch_session_token_ciphertext = EXCLUDED.stytch_session_token_ciphertext,
|
|
1048
|
+
auth_state = EXCLUDED.auth_state,
|
|
1049
|
+
expires_at = EXCLUDED.expires_at,
|
|
1050
|
+
last_validated_at = EXCLUDED.last_validated_at,
|
|
1051
|
+
updated_at = now()
|
|
1052
|
+
RETURNING
|
|
1053
|
+
id,
|
|
1054
|
+
application_session_id,
|
|
1055
|
+
organization_id,
|
|
1056
|
+
identity_link_id,
|
|
1057
|
+
stytch_organization_id,
|
|
1058
|
+
stytch_member_id,
|
|
1059
|
+
stytch_member_session_id,
|
|
1060
|
+
stytch_session_token_ciphertext,
|
|
1061
|
+
auth_state,
|
|
1062
|
+
expires_at,
|
|
1063
|
+
last_validated_at,
|
|
1064
|
+
created_at,
|
|
1065
|
+
updated_at`;
|
|
1066
|
+
async function upsertApplicationSessionOrganizationSession(client, args) {
|
|
1067
|
+
const result = await client.query({
|
|
1068
|
+
text: exports.upsertApplicationSessionOrganizationSessionQuery,
|
|
1069
|
+
values: [args.applicationSessionId, args.organizationId, args.identityLinkId, args.stytchOrganizationId, args.stytchMemberId, args.stytchMemberSessionId, args.stytchSessionTokenCiphertext, args.authState, args.expiresAt, args.lastValidatedAt],
|
|
1070
|
+
rowMode: "array"
|
|
1071
|
+
});
|
|
1072
|
+
if (result.rows.length !== 1) {
|
|
1073
|
+
return null;
|
|
1074
|
+
}
|
|
1075
|
+
const row = result.rows[0];
|
|
1076
|
+
return {
|
|
1077
|
+
id: row[0],
|
|
1078
|
+
applicationSessionId: row[1],
|
|
1079
|
+
organizationId: row[2],
|
|
1080
|
+
identityLinkId: row[3],
|
|
1081
|
+
stytchOrganizationId: row[4],
|
|
1082
|
+
stytchMemberId: row[5],
|
|
1083
|
+
stytchMemberSessionId: row[6],
|
|
1084
|
+
stytchSessionTokenCiphertext: row[7],
|
|
1085
|
+
authState: row[8],
|
|
1086
|
+
expiresAt: row[9],
|
|
1087
|
+
lastValidatedAt: row[10],
|
|
1088
|
+
createdAt: row[11],
|
|
1089
|
+
updatedAt: row[12]
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
exports.getApplicationSessionOrganizationSessionQuery = `-- name: GetApplicationSessionOrganizationSession :one
|
|
1093
|
+
SELECT
|
|
1094
|
+
id,
|
|
1095
|
+
application_session_id,
|
|
1096
|
+
organization_id,
|
|
1097
|
+
identity_link_id,
|
|
1098
|
+
stytch_organization_id,
|
|
1099
|
+
stytch_member_id,
|
|
1100
|
+
stytch_member_session_id,
|
|
1101
|
+
stytch_session_token_ciphertext,
|
|
1102
|
+
auth_state,
|
|
1103
|
+
expires_at,
|
|
1104
|
+
last_validated_at,
|
|
1105
|
+
created_at,
|
|
1106
|
+
updated_at
|
|
1107
|
+
FROM weave.application_session_organization_sessions
|
|
1108
|
+
WHERE application_session_id = $1
|
|
1109
|
+
AND organization_id = $2`;
|
|
1110
|
+
async function getApplicationSessionOrganizationSession(client, args) {
|
|
1111
|
+
const result = await client.query({
|
|
1112
|
+
text: exports.getApplicationSessionOrganizationSessionQuery,
|
|
1113
|
+
values: [args.applicationSessionId, args.organizationId],
|
|
1114
|
+
rowMode: "array"
|
|
1115
|
+
});
|
|
1116
|
+
if (result.rows.length !== 1) {
|
|
1117
|
+
return null;
|
|
1118
|
+
}
|
|
1119
|
+
const row = result.rows[0];
|
|
1120
|
+
return {
|
|
1121
|
+
id: row[0],
|
|
1122
|
+
applicationSessionId: row[1],
|
|
1123
|
+
organizationId: row[2],
|
|
1124
|
+
identityLinkId: row[3],
|
|
1125
|
+
stytchOrganizationId: row[4],
|
|
1126
|
+
stytchMemberId: row[5],
|
|
1127
|
+
stytchMemberSessionId: row[6],
|
|
1128
|
+
stytchSessionTokenCiphertext: row[7],
|
|
1129
|
+
authState: row[8],
|
|
1130
|
+
expiresAt: row[9],
|
|
1131
|
+
lastValidatedAt: row[10],
|
|
1132
|
+
createdAt: row[11],
|
|
1133
|
+
updatedAt: row[12]
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
exports.listApplicationSessionOrganizationSessionStatesQuery = `-- name: ListApplicationSessionOrganizationSessionStates :many
|
|
1137
|
+
SELECT
|
|
1138
|
+
s.id,
|
|
1139
|
+
s.application_session_id,
|
|
1140
|
+
s.organization_id,
|
|
1141
|
+
o.slug AS organization_slug,
|
|
1142
|
+
o.name AS organization_name,
|
|
1143
|
+
s.identity_link_id,
|
|
1144
|
+
s.stytch_organization_id,
|
|
1145
|
+
s.stytch_member_id,
|
|
1146
|
+
s.stytch_member_session_id,
|
|
1147
|
+
s.auth_state,
|
|
1148
|
+
s.expires_at,
|
|
1149
|
+
s.last_validated_at,
|
|
1150
|
+
s.created_at,
|
|
1151
|
+
s.updated_at
|
|
1152
|
+
FROM weave.application_session_organization_sessions AS s
|
|
1153
|
+
JOIN weave.organizations AS o
|
|
1154
|
+
ON o.id = s.organization_id
|
|
1155
|
+
WHERE s.application_session_id = $1
|
|
1156
|
+
ORDER BY o.slug ASC`;
|
|
1157
|
+
async function listApplicationSessionOrganizationSessionStates(client, args) {
|
|
1158
|
+
const result = await client.query({
|
|
1159
|
+
text: exports.listApplicationSessionOrganizationSessionStatesQuery,
|
|
1160
|
+
values: [args.applicationSessionId],
|
|
1161
|
+
rowMode: "array"
|
|
1162
|
+
});
|
|
1163
|
+
return result.rows.map(row => {
|
|
1164
|
+
return {
|
|
1165
|
+
id: row[0],
|
|
1166
|
+
applicationSessionId: row[1],
|
|
1167
|
+
organizationId: row[2],
|
|
1168
|
+
organizationSlug: row[3],
|
|
1169
|
+
organizationName: row[4],
|
|
1170
|
+
identityLinkId: row[5],
|
|
1171
|
+
stytchOrganizationId: row[6],
|
|
1172
|
+
stytchMemberId: row[7],
|
|
1173
|
+
stytchMemberSessionId: row[8],
|
|
1174
|
+
authState: row[9],
|
|
1175
|
+
expiresAt: row[10],
|
|
1176
|
+
lastValidatedAt: row[11],
|
|
1177
|
+
createdAt: row[12],
|
|
1178
|
+
updatedAt: row[13]
|
|
1179
|
+
};
|
|
1180
|
+
});
|
|
1181
|
+
}
|
|
1182
|
+
exports.markApplicationSessionOrganizationSessionStateQuery = `-- name: MarkApplicationSessionOrganizationSessionState :one
|
|
1183
|
+
UPDATE weave.application_session_organization_sessions
|
|
1184
|
+
SET
|
|
1185
|
+
auth_state = $1,
|
|
1186
|
+
last_validated_at = now(),
|
|
1187
|
+
updated_at = now()
|
|
1188
|
+
WHERE application_session_id = $2
|
|
1189
|
+
AND organization_id = $3
|
|
1190
|
+
RETURNING
|
|
1191
|
+
id,
|
|
1192
|
+
application_session_id,
|
|
1193
|
+
organization_id,
|
|
1194
|
+
identity_link_id,
|
|
1195
|
+
stytch_organization_id,
|
|
1196
|
+
stytch_member_id,
|
|
1197
|
+
stytch_member_session_id,
|
|
1198
|
+
stytch_session_token_ciphertext,
|
|
1199
|
+
auth_state,
|
|
1200
|
+
expires_at,
|
|
1201
|
+
last_validated_at,
|
|
1202
|
+
created_at,
|
|
1203
|
+
updated_at`;
|
|
1204
|
+
async function markApplicationSessionOrganizationSessionState(client, args) {
|
|
1205
|
+
const result = await client.query({
|
|
1206
|
+
text: exports.markApplicationSessionOrganizationSessionStateQuery,
|
|
1207
|
+
values: [args.authState, args.applicationSessionId, args.organizationId],
|
|
1208
|
+
rowMode: "array"
|
|
1209
|
+
});
|
|
1210
|
+
if (result.rows.length !== 1) {
|
|
1211
|
+
return null;
|
|
1212
|
+
}
|
|
1213
|
+
const row = result.rows[0];
|
|
1214
|
+
return {
|
|
1215
|
+
id: row[0],
|
|
1216
|
+
applicationSessionId: row[1],
|
|
1217
|
+
organizationId: row[2],
|
|
1218
|
+
identityLinkId: row[3],
|
|
1219
|
+
stytchOrganizationId: row[4],
|
|
1220
|
+
stytchMemberId: row[5],
|
|
1221
|
+
stytchMemberSessionId: row[6],
|
|
1222
|
+
stytchSessionTokenCiphertext: row[7],
|
|
1223
|
+
authState: row[8],
|
|
1224
|
+
expiresAt: row[9],
|
|
1225
|
+
lastValidatedAt: row[10],
|
|
1226
|
+
createdAt: row[11],
|
|
1227
|
+
updatedAt: row[12]
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
exports.deleteExpiredApplicationSessionsQuery = `-- name: DeleteExpiredApplicationSessions :execrows
|
|
1231
|
+
DELETE FROM weave.application_sessions
|
|
1232
|
+
WHERE expires_at <= now()
|
|
1233
|
+
OR status IN ('revoked', 'expired')`;
|