@loomcore/api 0.1.140 → 0.1.142
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.
|
@@ -83,11 +83,17 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
83
83
|
"_deleted" TIMESTAMPTZ,
|
|
84
84
|
"_deletedBy" INTEGER,
|
|
85
85
|
${personsUniqueConstraints}
|
|
86
|
-
)
|
|
86
|
+
);
|
|
87
|
+
CREATE INDEX IF NOT EXISTS "idx_persons_external_id" ON "persons" ("external_id");
|
|
88
|
+
CREATE INDEX IF NOT EXISTS "idx_persons_ssn" ON "persons" ("ssn");
|
|
87
89
|
`);
|
|
88
90
|
},
|
|
89
91
|
down: async ({ context: pool }) => {
|
|
90
|
-
await pool.query(
|
|
92
|
+
await pool.query(`
|
|
93
|
+
DROP INDEX IF EXISTS "idx_persons_external_id";
|
|
94
|
+
DROP INDEX IF EXISTS "idx_persons_ssn";
|
|
95
|
+
DROP TABLE IF EXISTS "persons"
|
|
96
|
+
`);
|
|
91
97
|
}
|
|
92
98
|
});
|
|
93
99
|
if (isAuthEnabled)
|
|
@@ -101,28 +107,36 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
101
107
|
uniqueConstraint += `,
|
|
102
108
|
CONSTRAINT "fk_users_person_id" FOREIGN KEY("person_id") REFERENCES "persons"("_id") ON DELETE CASCADE`;
|
|
103
109
|
await pool.query(`
|
|
104
|
-
CREATE TABLE IF NOT EXISTS "users"
|
|
105
|
-
|
|
106
|
-
|
|
110
|
+
CREATE TABLE IF NOT EXISTS "users"(
|
|
111
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
112
|
+
${orgColumnDef}
|
|
107
113
|
"external_id" VARCHAR(255) UNIQUE,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
114
|
+
"email" VARCHAR(255) NOT NULL,
|
|
115
|
+
"display_name" VARCHAR(255),
|
|
116
|
+
"password" VARCHAR(255) NOT NULL,
|
|
117
|
+
"person_id" INTEGER UNIQUE,
|
|
118
|
+
"_lastLoggedIn" TIMESTAMPTZ,
|
|
119
|
+
"_lastPasswordChange" TIMESTAMPTZ,
|
|
120
|
+
"_created" TIMESTAMPTZ NOT NULL,
|
|
121
|
+
"_createdBy" INTEGER NOT NULL,
|
|
122
|
+
"_updated" TIMESTAMPTZ NOT NULL,
|
|
123
|
+
"_updatedBy" INTEGER NOT NULL,
|
|
124
|
+
"_deleted" TIMESTAMPTZ,
|
|
125
|
+
"_deletedBy" INTEGER,
|
|
126
|
+
${uniqueConstraint}
|
|
127
|
+
)
|
|
128
|
+
CREATE INDEX IF NOT EXISTS "idx_users_external_id" ON "users"("external_id");
|
|
129
|
+
CREATE INDEX IF NOT EXISTS "idx_users_email" ON "users"("email");
|
|
130
|
+
CREATE INDEX IF NOT EXISTS "idx_users_person_id" ON "users"("person_id");
|
|
131
|
+
`);
|
|
123
132
|
},
|
|
124
133
|
down: async ({ context: pool }) => {
|
|
125
|
-
await pool.query(
|
|
134
|
+
await pool.query(`
|
|
135
|
+
DROP INDEX IF EXISTS "idx_users_external_id";
|
|
136
|
+
DROP INDEX IF EXISTS "idx_users_email";
|
|
137
|
+
DROP INDEX IF EXISTS "idx_users_person_id";
|
|
138
|
+
DROP TABLE IF EXISTS "users"
|
|
139
|
+
`);
|
|
126
140
|
}
|
|
127
141
|
});
|
|
128
142
|
if (isAuthEnabled)
|
|
@@ -131,18 +145,18 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
131
145
|
up: async ({ context: pool }) => {
|
|
132
146
|
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER,' : '';
|
|
133
147
|
await pool.query(`
|
|
134
|
-
CREATE TABLE IF NOT EXISTS "refresh_tokens"
|
|
135
|
-
|
|
136
|
-
|
|
148
|
+
CREATE TABLE IF NOT EXISTS "refresh_tokens"(
|
|
149
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
150
|
+
${orgColumnDef}
|
|
137
151
|
"token" VARCHAR(255) NOT NULL,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
152
|
+
"device_id" VARCHAR(255) NOT NULL,
|
|
153
|
+
"user_id" INTEGER NOT NULL,
|
|
154
|
+
"expires_on" BIGINT NOT NULL,
|
|
155
|
+
"created" TIMESTAMPTZ NOT NULL,
|
|
156
|
+
"created_by" INTEGER NOT NULL,
|
|
157
|
+
CONSTRAINT "fk_refresh_tokens_user" FOREIGN KEY("user_id") REFERENCES "users"("_id") ON DELETE CASCADE
|
|
158
|
+
)
|
|
159
|
+
`);
|
|
146
160
|
},
|
|
147
161
|
down: async ({ context: pool }) => {
|
|
148
162
|
await pool.query('DROP TABLE IF EXISTS "refresh_tokens"');
|
|
@@ -157,21 +171,21 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
157
171
|
? 'CONSTRAINT "uk_passwordResetTokens_email" UNIQUE ("_orgId", "email")'
|
|
158
172
|
: 'CONSTRAINT "uk_passwordResetTokens_email" UNIQUE ("email")';
|
|
159
173
|
await pool.query(`
|
|
160
|
-
CREATE TABLE IF NOT EXISTS "password_reset_tokens"
|
|
161
|
-
|
|
162
|
-
|
|
174
|
+
CREATE TABLE IF NOT EXISTS "password_reset_tokens"(
|
|
175
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
176
|
+
${orgColumnDef}
|
|
163
177
|
"email" VARCHAR(255) NOT NULL,
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
178
|
+
"token" VARCHAR(255) NOT NULL,
|
|
179
|
+
"expires_on" BIGINT NOT NULL,
|
|
180
|
+
"_created" TIMESTAMPTZ NOT NULL,
|
|
181
|
+
"_createdBy" INTEGER NOT NULL,
|
|
182
|
+
"_updated" TIMESTAMPTZ NOT NULL,
|
|
183
|
+
"_updatedBy" INTEGER NOT NULL,
|
|
184
|
+
"_deleted" TIMESTAMPTZ,
|
|
185
|
+
"_deletedBy" INTEGER,
|
|
186
|
+
${uniqueConstraint}
|
|
187
|
+
)
|
|
188
|
+
`);
|
|
175
189
|
},
|
|
176
190
|
down: async ({ context: pool }) => {
|
|
177
191
|
await pool.query('DROP TABLE IF EXISTS "password_reset_tokens"');
|
|
@@ -186,14 +200,14 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
186
200
|
? 'CONSTRAINT "uk_roles_name" UNIQUE ("_orgId", "name")'
|
|
187
201
|
: 'CONSTRAINT "uk_roles_name" UNIQUE ("name")';
|
|
188
202
|
await pool.query(`
|
|
189
|
-
CREATE TABLE IF NOT EXISTS "roles"
|
|
190
|
-
|
|
191
|
-
|
|
203
|
+
CREATE TABLE IF NOT EXISTS "roles"(
|
|
204
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
205
|
+
${orgColumnDef}
|
|
192
206
|
"name" VARCHAR(255) NOT NULL,
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
207
|
+
"description" TEXT,
|
|
208
|
+
${uniqueConstraint}
|
|
209
|
+
)
|
|
210
|
+
`);
|
|
197
211
|
},
|
|
198
212
|
down: async ({ context: pool }) => {
|
|
199
213
|
await pool.query('DROP TABLE IF EXISTS "roles"');
|
|
@@ -208,22 +222,22 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
208
222
|
? 'CONSTRAINT "uk_user_roles" UNIQUE ("_orgId", "user_id", "role_id")'
|
|
209
223
|
: 'CONSTRAINT "uk_user_roles" UNIQUE ("user_id", "role_id")';
|
|
210
224
|
await pool.query(`
|
|
211
|
-
CREATE TABLE IF NOT EXISTS "user_roles"
|
|
212
|
-
|
|
213
|
-
|
|
225
|
+
CREATE TABLE IF NOT EXISTS "user_roles"(
|
|
226
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
227
|
+
${orgColumnDef}
|
|
214
228
|
"user_id" INTEGER NOT NULL,
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
229
|
+
"role_id" INTEGER NOT NULL,
|
|
230
|
+
"_created" TIMESTAMPTZ NOT NULL,
|
|
231
|
+
"_createdBy" INTEGER NOT NULL,
|
|
232
|
+
"_updated" TIMESTAMPTZ NOT NULL,
|
|
233
|
+
"_updatedBy" INTEGER NOT NULL,
|
|
234
|
+
"_deleted" TIMESTAMPTZ,
|
|
235
|
+
"_deletedBy" INTEGER,
|
|
236
|
+
CONSTRAINT "fk_user_roles_user" FOREIGN KEY("user_id") REFERENCES "users"("_id") ON DELETE CASCADE,
|
|
237
|
+
CONSTRAINT "fk_user_roles_role" FOREIGN KEY("role_id") REFERENCES "roles"("_id") ON DELETE CASCADE,
|
|
238
|
+
${uniqueConstraint}
|
|
239
|
+
)
|
|
240
|
+
`);
|
|
227
241
|
},
|
|
228
242
|
down: async ({ context: pool }) => {
|
|
229
243
|
await pool.query('DROP TABLE IF EXISTS "user_roles"');
|
|
@@ -238,14 +252,14 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
238
252
|
? 'CONSTRAINT "uk_features" UNIQUE ("_orgId", "name")'
|
|
239
253
|
: 'CONSTRAINT "uk_features" UNIQUE ("name")';
|
|
240
254
|
await pool.query(`
|
|
241
|
-
CREATE TABLE IF NOT EXISTS "features"
|
|
242
|
-
|
|
243
|
-
|
|
255
|
+
CREATE TABLE IF NOT EXISTS "features"(
|
|
256
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
257
|
+
${orgColumnDef}
|
|
244
258
|
"name" VARCHAR(255) NOT NULL,
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
259
|
+
"description" TEXT,
|
|
260
|
+
${uniqueConstraint}
|
|
261
|
+
)
|
|
262
|
+
`);
|
|
249
263
|
},
|
|
250
264
|
down: async ({ context: pool }) => {
|
|
251
265
|
await pool.query('DROP TABLE IF EXISTS "features"');
|
|
@@ -260,25 +274,25 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
260
274
|
? 'CONSTRAINT "uk_authorizations" UNIQUE ("_orgId", "role_id", "feature_id")'
|
|
261
275
|
: 'CONSTRAINT "uk_authorizations" UNIQUE ("role_id", "feature_id")';
|
|
262
276
|
await pool.query(`
|
|
263
|
-
CREATE TABLE IF NOT EXISTS "authorizations"
|
|
264
|
-
|
|
265
|
-
|
|
277
|
+
CREATE TABLE IF NOT EXISTS "authorizations"(
|
|
278
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
279
|
+
${orgColumnDef}
|
|
266
280
|
"role_id" INTEGER NOT NULL,
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
281
|
+
"feature_id" INTEGER NOT NULL,
|
|
282
|
+
"start_date" TIMESTAMPTZ,
|
|
283
|
+
"end_date" TIMESTAMPTZ,
|
|
284
|
+
"config" JSONB,
|
|
285
|
+
"_created" TIMESTAMPTZ NOT NULL,
|
|
286
|
+
"_createdBy" INTEGER NOT NULL,
|
|
287
|
+
"_updated" TIMESTAMPTZ NOT NULL,
|
|
288
|
+
"_updatedBy" INTEGER NOT NULL,
|
|
289
|
+
"_deleted" TIMESTAMPTZ,
|
|
290
|
+
"_deletedBy" INTEGER,
|
|
291
|
+
CONSTRAINT "fk_authorizations_role" FOREIGN KEY("role_id") REFERENCES "roles"("_id") ON DELETE CASCADE,
|
|
292
|
+
CONSTRAINT "fk_authorizations_feature" FOREIGN KEY("feature_id") REFERENCES "features"("_id") ON DELETE CASCADE,
|
|
293
|
+
${uniqueConstraint}
|
|
294
|
+
)
|
|
295
|
+
`);
|
|
282
296
|
},
|
|
283
297
|
down: async ({ context: pool }) => {
|
|
284
298
|
await pool.query('DROP TABLE IF EXISTS "authorizations"');
|
|
@@ -289,10 +303,10 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
289
303
|
name: '00000000000011_data-meta-org',
|
|
290
304
|
up: async ({ context: pool }) => {
|
|
291
305
|
const result = await pool.query(`
|
|
292
|
-
INSERT INTO "organizations"
|
|
293
|
-
|
|
306
|
+
INSERT INTO "organizations"("name", "code", "status", "is_meta_org", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
307
|
+
VALUES($1, $2, 1, true, NOW(), 0, NOW(), 0)
|
|
294
308
|
RETURNING "_id", "name", "code", "status", "is_meta_org", "_created", "_createdBy", "_updated", "_updatedBy"
|
|
295
|
-
|
|
309
|
+
`, [dbConfig.multiTenant?.metaOrgName, dbConfig.multiTenant?.metaOrgCode]);
|
|
296
310
|
if (result.rowCount === 0) {
|
|
297
311
|
throw new Error('Failed to create meta organization');
|
|
298
312
|
}
|
|
@@ -322,20 +336,20 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
322
336
|
const client = await pool.connect();
|
|
323
337
|
try {
|
|
324
338
|
const personResult = isMultiTenant && orgId
|
|
325
|
-
? await client.query(`INSERT INTO "persons"
|
|
326
|
-
|
|
339
|
+
? await client.query(`INSERT INTO "persons"("_orgId", "first_name", "last_name", "is_agent", "is_client", "is_employee", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
340
|
+
VALUES($1, 'Admin', 'User', false, false, false, NOW(), 0, NOW(), 0)
|
|
327
341
|
RETURNING "_id"`, [orgId])
|
|
328
|
-
: await client.query(`INSERT INTO "persons"
|
|
329
|
-
|
|
342
|
+
: await client.query(`INSERT INTO "persons"("first_name", "last_name", "is_agent", "is_client", "is_employee", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
343
|
+
VALUES('Admin', 'User', false, false, false, NOW(), 0, NOW(), 0)
|
|
330
344
|
RETURNING "_id"`);
|
|
331
345
|
const personId = personResult.rows[0]._id;
|
|
332
346
|
if (isMultiTenant && orgId) {
|
|
333
|
-
await client.query(`INSERT INTO "users"
|
|
334
|
-
|
|
347
|
+
await client.query(`INSERT INTO "users"("_orgId", "email", "display_name", "password", "person_id", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
348
|
+
VALUES($1, $2, 'Admin User', $3, $4, NOW(), 0, NOW(), 0)`, [orgId, email, hashedPassword, personId]);
|
|
335
349
|
}
|
|
336
350
|
else {
|
|
337
|
-
await client.query(`INSERT INTO "users"
|
|
338
|
-
|
|
351
|
+
await client.query(`INSERT INTO "users"("email", "display_name", "password", "person_id", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
352
|
+
VALUES($1, 'Admin User', $2, $3, NOW(), 0, NOW(), 0)`, [email, hashedPassword, personId]);
|
|
339
353
|
}
|
|
340
354
|
}
|
|
341
355
|
finally {
|
|
@@ -372,61 +386,61 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
372
386
|
try {
|
|
373
387
|
const roleResult = isMultiTenant
|
|
374
388
|
? await client.query(`
|
|
375
|
-
INSERT INTO "roles"
|
|
376
|
-
|
|
389
|
+
INSERT INTO "roles"("_orgId", "name")
|
|
390
|
+
VALUES($1, 'admin')
|
|
377
391
|
RETURNING "_id"
|
|
378
|
-
|
|
392
|
+
`, [metaOrg._id])
|
|
379
393
|
: await client.query(`
|
|
380
|
-
INSERT INTO "roles"
|
|
381
|
-
|
|
394
|
+
INSERT INTO "roles"("name")
|
|
395
|
+
VALUES('admin')
|
|
382
396
|
RETURNING "_id"
|
|
383
|
-
|
|
397
|
+
`);
|
|
384
398
|
if (roleResult.rowCount === 0) {
|
|
385
399
|
throw new Error('Failed to create admin role');
|
|
386
400
|
}
|
|
387
401
|
const roleId = roleResult.rows[0]._id;
|
|
388
402
|
const userRoleResult = isMultiTenant
|
|
389
403
|
? await client.query(`
|
|
390
|
-
INSERT INTO "user_roles"
|
|
391
|
-
|
|
392
|
-
|
|
404
|
+
INSERT INTO "user_roles"("_orgId", "user_id", "role_id", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
405
|
+
VALUES($1, $2, $3, NOW(), 0, NOW(), 0)
|
|
406
|
+
`, [metaOrg._id, adminUserId, roleId])
|
|
393
407
|
: await client.query(`
|
|
394
|
-
INSERT INTO "user_roles"
|
|
395
|
-
|
|
396
|
-
|
|
408
|
+
INSERT INTO "user_roles"("user_id", "role_id", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
409
|
+
VALUES($1, $2, NOW(), 0, NOW(), 0)
|
|
410
|
+
`, [adminUserId, roleId]);
|
|
397
411
|
if (userRoleResult.rowCount === 0) {
|
|
398
412
|
throw new Error('Failed to create user role');
|
|
399
413
|
}
|
|
400
414
|
const featureResult = isMultiTenant
|
|
401
415
|
? await client.query(`
|
|
402
|
-
INSERT INTO "features"
|
|
403
|
-
|
|
416
|
+
INSERT INTO "features"("_orgId", "name")
|
|
417
|
+
VALUES($1, 'admin')
|
|
404
418
|
RETURNING "_id"
|
|
405
|
-
|
|
419
|
+
`, [metaOrg._id])
|
|
406
420
|
: await client.query(`
|
|
407
|
-
INSERT INTO "features"
|
|
408
|
-
|
|
421
|
+
INSERT INTO "features"("name")
|
|
422
|
+
VALUES('admin')
|
|
409
423
|
RETURNING "_id"
|
|
410
|
-
|
|
424
|
+
`);
|
|
411
425
|
if (featureResult.rowCount === 0) {
|
|
412
426
|
throw new Error('Failed to create admin feature');
|
|
413
427
|
}
|
|
414
428
|
const featureId = featureResult.rows[0]._id;
|
|
415
429
|
const authorizationResult = isMultiTenant
|
|
416
430
|
? await client.query(`
|
|
417
|
-
INSERT INTO "authorizations"
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
431
|
+
INSERT INTO "authorizations"(
|
|
432
|
+
"_orgId", "role_id", "feature_id",
|
|
433
|
+
"_created", "_createdBy", "_updated", "_updatedBy"
|
|
434
|
+
)
|
|
435
|
+
VALUES($1, $2, $3, NOW(), 0, NOW(), 0)
|
|
436
|
+
`, [metaOrg._id, roleId, featureId])
|
|
423
437
|
: await client.query(`
|
|
424
|
-
INSERT INTO "authorizations"
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
438
|
+
INSERT INTO "authorizations"(
|
|
439
|
+
"role_id", "feature_id",
|
|
440
|
+
"_created", "_createdBy", "_updated", "_updatedBy"
|
|
441
|
+
)
|
|
442
|
+
VALUES($1, $2, NOW(), 0, NOW(), 0)
|
|
443
|
+
`, [roleId, featureId]);
|
|
430
444
|
if (authorizationResult.rowCount === 0) {
|
|
431
445
|
throw new Error('Failed to create admin authorization');
|
|
432
446
|
}
|
|
@@ -455,59 +469,59 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
455
469
|
await client.query(`
|
|
456
470
|
DELETE FROM "authorizations"
|
|
457
471
|
WHERE "_orgId" = $1
|
|
458
|
-
AND "feature_id" IN
|
|
459
|
-
|
|
472
|
+
AND "feature_id" IN(
|
|
473
|
+
SELECT "_id" FROM "features"
|
|
460
474
|
WHERE "_orgId" = $1 AND "name" = 'admin'
|
|
461
|
-
|
|
462
|
-
AND "role_id" IN
|
|
463
|
-
|
|
475
|
+
)
|
|
476
|
+
AND "role_id" IN(
|
|
477
|
+
SELECT "_id" FROM "roles"
|
|
464
478
|
WHERE "_orgId" = $1 AND "name" = 'admin'
|
|
465
|
-
|
|
466
|
-
|
|
479
|
+
)
|
|
480
|
+
`, [metaOrg._id]);
|
|
467
481
|
await client.query(`
|
|
468
482
|
DELETE FROM "features"
|
|
469
483
|
WHERE "_orgId" = $1 AND "name" = 'admin'
|
|
470
|
-
|
|
484
|
+
`, [metaOrg._id]);
|
|
471
485
|
await client.query(`
|
|
472
486
|
DELETE FROM "user_roles"
|
|
473
487
|
WHERE "_orgId" = $1
|
|
474
|
-
AND "role_id" IN
|
|
475
|
-
|
|
488
|
+
AND "role_id" IN(
|
|
489
|
+
SELECT "_id" FROM "roles"
|
|
476
490
|
WHERE "_orgId" = $1 AND "name" = 'admin'
|
|
477
|
-
|
|
478
|
-
|
|
491
|
+
)
|
|
492
|
+
`, [metaOrg._id]);
|
|
479
493
|
await client.query(`
|
|
480
494
|
DELETE FROM "roles"
|
|
481
495
|
WHERE "_orgId" = $1 AND "name" = 'admin'
|
|
482
|
-
|
|
496
|
+
`, [metaOrg._id]);
|
|
483
497
|
}
|
|
484
498
|
else {
|
|
485
499
|
await client.query(`
|
|
486
500
|
DELETE FROM "authorizations"
|
|
487
|
-
WHERE "feature_id" IN
|
|
488
|
-
|
|
501
|
+
WHERE "feature_id" IN(
|
|
502
|
+
SELECT "_id" FROM "features"
|
|
489
503
|
WHERE "name" = 'admin'
|
|
490
|
-
|
|
491
|
-
AND "role_id" IN
|
|
492
|
-
|
|
504
|
+
)
|
|
505
|
+
AND "role_id" IN(
|
|
506
|
+
SELECT "_id" FROM "roles"
|
|
493
507
|
WHERE "name" = 'admin'
|
|
494
|
-
|
|
495
|
-
|
|
508
|
+
)
|
|
509
|
+
`);
|
|
496
510
|
await client.query(`
|
|
497
511
|
DELETE FROM "features"
|
|
498
512
|
WHERE "name" = 'admin'
|
|
499
|
-
|
|
513
|
+
`);
|
|
500
514
|
await client.query(`
|
|
501
515
|
DELETE FROM "user_roles"
|
|
502
|
-
WHERE "role_id" IN
|
|
503
|
-
|
|
516
|
+
WHERE "role_id" IN(
|
|
517
|
+
SELECT "_id" FROM "roles"
|
|
504
518
|
WHERE "name" = 'admin'
|
|
505
|
-
|
|
506
|
-
|
|
519
|
+
)
|
|
520
|
+
`);
|
|
507
521
|
await client.query(`
|
|
508
522
|
DELETE FROM "roles"
|
|
509
523
|
WHERE "name" = 'admin'
|
|
510
|
-
|
|
524
|
+
`);
|
|
511
525
|
}
|
|
512
526
|
await client.query('COMMIT');
|
|
513
527
|
}
|
|
@@ -33,12 +33,9 @@ export class AuthService extends MultiTenantApiService {
|
|
|
33
33
|
this.authConfig = config.auth;
|
|
34
34
|
}
|
|
35
35
|
async attemptLogin(req, res, email, password) {
|
|
36
|
-
console.log('auth service attemptLogin', email, password);
|
|
37
36
|
const lowerCaseEmail = email.toLowerCase();
|
|
38
37
|
const user = await this.getUserByEmail(lowerCaseEmail);
|
|
39
|
-
console.log('auth service attemptLogin user', user);
|
|
40
38
|
const organization = await this.organizationService.findOne(EmptyUserContext, { filters: { _id: { eq: user?._orgId } } });
|
|
41
|
-
console.log('auth service attemptLogin organization', organization);
|
|
42
39
|
if (!user) {
|
|
43
40
|
throw new BadRequestError('Invalid Credentials');
|
|
44
41
|
}
|
|
@@ -47,20 +44,15 @@ export class AuthService extends MultiTenantApiService {
|
|
|
47
44
|
throw new BadRequestError('Invalid Credentials');
|
|
48
45
|
}
|
|
49
46
|
const person = await this.personService.findOne(EmptyUserContext, { filters: { _id: { eq: user.personId } } });
|
|
50
|
-
console.log('auth service attemptLogin person', person);
|
|
51
47
|
const authorizations = await getUserContextAuthorizations(this.database, user);
|
|
52
|
-
console.log('auth service attemptLogin authorizations', authorizations);
|
|
53
48
|
const userContext = {
|
|
54
49
|
user: user,
|
|
55
50
|
person: person ?? undefined,
|
|
56
51
|
organization: organization ?? undefined,
|
|
57
52
|
authorizations: authorizations
|
|
58
53
|
};
|
|
59
|
-
console.log('auth service attemptLogin userContext', userContext);
|
|
60
54
|
const deviceId = this.getAndSetDeviceIdCookie(req, res);
|
|
61
|
-
console.log('auth service attemptLogin deviceId', deviceId);
|
|
62
55
|
const loginResponse = await this.logUserIn(userContext, deviceId);
|
|
63
|
-
console.log('auth service attemptLogin loginResponse', loginResponse);
|
|
64
56
|
return loginResponse;
|
|
65
57
|
}
|
|
66
58
|
async logUserIn(userContext, deviceId) {
|