@loomcore/api 0.1.25 → 0.1.26
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/databases/postgres/migrations/008-create-user-roles-table.migration.js +7 -1
- package/dist/databases/postgres/migrations/011-create-admin-authorization.migration.js +6 -6
- package/dist/databases/postgres/migrations/setup-for-auth.migration.js +42 -9
- package/dist/databases/postgres/postgres.database.js +0 -2
- package/dist/models/user-role.model.d.ts +2 -2
- package/dist/models/user-role.model.js +1 -1
- package/package.json +1 -1
|
@@ -18,7 +18,13 @@ export class CreateUserRolesTableMigration {
|
|
|
18
18
|
"_orgId" VARCHAR(255),
|
|
19
19
|
"_userId" VARCHAR(255) NOT NULL,
|
|
20
20
|
"_roleId" VARCHAR(255) NOT NULL,
|
|
21
|
-
|
|
21
|
+
"_created" TIMESTAMP NOT NULL,
|
|
22
|
+
"_createdBy" VARCHAR(255) NOT NULL,
|
|
23
|
+
"_updated" TIMESTAMP NOT NULL,
|
|
24
|
+
"_updatedBy" VARCHAR(255) NOT NULL,
|
|
25
|
+
"_deleted" TIMESTAMP,
|
|
26
|
+
"_deletedBy" VARCHAR(255),
|
|
27
|
+
CONSTRAINT "fk_user_roles_organization" FOREIGN KEY ("_orgId") REFERENCES "organizations"("_id") ON DELETE CASCADE,
|
|
22
28
|
CONSTRAINT "fk_user_roles_user" FOREIGN KEY ("_userId") REFERENCES "users"("_id") ON DELETE CASCADE,
|
|
23
29
|
CONSTRAINT "fk_user_roles_role" FOREIGN KEY ("_roleId") REFERENCES "roles"("_id") ON DELETE CASCADE,
|
|
24
30
|
CONSTRAINT "uk_user_roles" UNIQUE ("_orgId", "_userId", "_roleId")
|
|
@@ -18,16 +18,16 @@ export class CreateAdminAuthorizationMigration {
|
|
|
18
18
|
INSERT INTO "roles" ("_id", "_orgId", "name")
|
|
19
19
|
VALUES ($1, $2, 'admin')
|
|
20
20
|
`, [roleId, this.metaOrgId]);
|
|
21
|
-
if (roleResult.
|
|
21
|
+
if (roleResult.rowCount === 0) {
|
|
22
22
|
await this.client.query('ROLLBACK');
|
|
23
23
|
return { success: false, error: new Error('Failed to create admin role') };
|
|
24
24
|
}
|
|
25
25
|
const userRoleId = randomUUID().toString();
|
|
26
26
|
const userRoleResult = await this.client.query(`
|
|
27
|
-
INSERT INTO "user_roles" ("_id", "_orgId", "_userId", "_roleId")
|
|
28
|
-
VALUES ($1, $2, $3, $4)
|
|
27
|
+
INSERT INTO "user_roles" ("_id", "_orgId", "_userId", "_roleId", "_created", "_createdBy", "_updated", "_updatedBy")
|
|
28
|
+
VALUES ($1, $2, $3, $4, NOW(), 'system', NOW(), 'system')
|
|
29
29
|
`, [userRoleId, this.metaOrgId, this.adminUserId, roleId]);
|
|
30
|
-
if (userRoleResult.
|
|
30
|
+
if (userRoleResult.rowCount === 0) {
|
|
31
31
|
await this.client.query('ROLLBACK');
|
|
32
32
|
return { success: false, error: new Error('Failed to create user role') };
|
|
33
33
|
}
|
|
@@ -36,7 +36,7 @@ export class CreateAdminAuthorizationMigration {
|
|
|
36
36
|
INSERT INTO "features" ("_id", "_orgId", "name")
|
|
37
37
|
VALUES ($1, $2, 'admin')
|
|
38
38
|
`, [featureId, this.metaOrgId]);
|
|
39
|
-
if (featureResult.
|
|
39
|
+
if (featureResult.rowCount === 0) {
|
|
40
40
|
await this.client.query('ROLLBACK');
|
|
41
41
|
return { success: false, error: new Error('Failed to create admin feature') };
|
|
42
42
|
}
|
|
@@ -48,7 +48,7 @@ export class CreateAdminAuthorizationMigration {
|
|
|
48
48
|
)
|
|
49
49
|
VALUES ($1, $2, $3, $4, NOW(), 'system', NOW(), 'system')
|
|
50
50
|
`, [authorizationId, this.metaOrgId, roleId, featureId]);
|
|
51
|
-
if (authorizationResult.
|
|
51
|
+
if (authorizationResult.rowCount === 0) {
|
|
52
52
|
await this.client.query('ROLLBACK');
|
|
53
53
|
return { success: false, error: new Error('Failed to create admin authorization') };
|
|
54
54
|
}
|
|
@@ -23,15 +23,27 @@ export async function setupDatabaseForAuth(client, adminUsername, adminPassword,
|
|
|
23
23
|
let adminUserId;
|
|
24
24
|
if (!runMigrations.includes(1)) {
|
|
25
25
|
const migration = new CreateMigrationTableMigration(client);
|
|
26
|
-
await migration.execute();
|
|
26
|
+
const result = await migration.execute();
|
|
27
|
+
if (!result.success) {
|
|
28
|
+
console.error('setupDatabaseForAuth: error creating migrations table', result.error);
|
|
29
|
+
return { success: false, error: result.error };
|
|
30
|
+
}
|
|
27
31
|
}
|
|
28
32
|
if (!runMigrations.includes(3)) {
|
|
29
33
|
const migration = new CreateUsersTableMigration(client);
|
|
30
|
-
await migration.execute();
|
|
34
|
+
const result = await migration.execute();
|
|
35
|
+
if (!result.success) {
|
|
36
|
+
console.error('setupDatabaseForAuth: error creating users table', result.error);
|
|
37
|
+
return { success: false, error: result.error };
|
|
38
|
+
}
|
|
31
39
|
}
|
|
32
40
|
if (!runMigrations.includes(4)) {
|
|
33
41
|
const migration = new CreateRefreshTokenTableMigration(client);
|
|
34
|
-
await migration.execute();
|
|
42
|
+
const result = await migration.execute();
|
|
43
|
+
if (!result.success) {
|
|
44
|
+
console.error('setupDatabaseForAuth: error creating refresh_tokens table', result.error);
|
|
45
|
+
return { success: false, error: result.error };
|
|
46
|
+
}
|
|
35
47
|
}
|
|
36
48
|
if (!runMigrations.includes(6)) {
|
|
37
49
|
const migration = new CreateAdminUserMigration(client);
|
|
@@ -40,26 +52,47 @@ export async function setupDatabaseForAuth(client, adminUsername, adminPassword,
|
|
|
40
52
|
}
|
|
41
53
|
if (!runMigrations.includes(7)) {
|
|
42
54
|
const migration = new CreateRoleTableMigration(client);
|
|
43
|
-
await migration.execute();
|
|
55
|
+
const result = await migration.execute();
|
|
56
|
+
if (!result.success) {
|
|
57
|
+
console.error('setupDatabaseForAuth: error creating roles table', result.error);
|
|
58
|
+
return { success: false, error: result.error };
|
|
59
|
+
}
|
|
44
60
|
}
|
|
45
61
|
if (!runMigrations.includes(8)) {
|
|
46
62
|
const migration = new CreateUserRolesTableMigration(client);
|
|
47
|
-
await migration.execute();
|
|
63
|
+
const result = await migration.execute();
|
|
64
|
+
if (!result.success) {
|
|
65
|
+
console.error('setupDatabaseForAuth: error creating user_roles table', result.error);
|
|
66
|
+
return { success: false, error: result.error };
|
|
67
|
+
}
|
|
48
68
|
}
|
|
49
69
|
if (!runMigrations.includes(9)) {
|
|
50
70
|
const migration = new CreateFeaturesTableMigration(client);
|
|
51
|
-
await migration.execute();
|
|
71
|
+
const result = await migration.execute();
|
|
72
|
+
if (!result.success) {
|
|
73
|
+
console.error('setupDatabaseForAuth: error creating features table', result.error);
|
|
74
|
+
return { success: false, error: result.error };
|
|
75
|
+
}
|
|
52
76
|
}
|
|
53
77
|
if (!runMigrations.includes(10)) {
|
|
54
78
|
const migration = new CreateAuthorizationsTableMigration(client);
|
|
55
|
-
await migration.execute();
|
|
79
|
+
const result = await migration.execute();
|
|
80
|
+
if (!result.success) {
|
|
81
|
+
console.error('setupDatabaseForAuth: error creating authorizations table', result.error);
|
|
82
|
+
return { success: false, error: result.error };
|
|
83
|
+
}
|
|
56
84
|
}
|
|
57
85
|
if (!runMigrations.includes(11)) {
|
|
58
86
|
if (!adminUserId) {
|
|
59
|
-
|
|
87
|
+
console.error('setupDatabaseForAuth: Admin user ID is required');
|
|
88
|
+
return { success: true, error: null };
|
|
60
89
|
}
|
|
61
90
|
const migration = new CreateAdminAuthorizationMigration(client, adminUserId, metaOrgId);
|
|
62
|
-
await migration.execute();
|
|
91
|
+
const result = await migration.execute();
|
|
92
|
+
if (!result.success) {
|
|
93
|
+
console.error('setupDatabaseForAuth: error creating admin authorization', result.error);
|
|
94
|
+
return { success: false, error: result.error };
|
|
95
|
+
}
|
|
63
96
|
}
|
|
64
97
|
return { success: true, error: null };
|
|
65
98
|
}
|
|
@@ -90,9 +90,7 @@ export class PostgresDatabase {
|
|
|
90
90
|
INNER JOIN "features" f ON a."_featureId" = f."_id"
|
|
91
91
|
WHERE ur."_userId" IN (${placeholders})
|
|
92
92
|
AND ur."_deleted" IS NULL
|
|
93
|
-
AND r."_deleted" IS NULL
|
|
94
93
|
AND a."_deleted" IS NULL
|
|
95
|
-
AND f."_deleted" IS NULL
|
|
96
94
|
AND (a."startDate" IS NULL OR a."startDate" <= $${userIds.length + 1})
|
|
97
95
|
AND (a."endDate" IS NULL OR a."endDate" >= $${userIds.length + 1})
|
|
98
96
|
`;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IEntity } from "@loomcore/common/models";
|
|
1
|
+
import { IAuditable, IEntity } from "@loomcore/common/models";
|
|
2
2
|
import { TSchema } from "@sinclair/typebox";
|
|
3
|
-
export interface IUserRole extends IEntity {
|
|
3
|
+
export interface IUserRole extends IEntity, IAuditable {
|
|
4
4
|
userId: string;
|
|
5
5
|
roleId: string;
|
|
6
6
|
}
|
|
@@ -4,4 +4,4 @@ export const UserRoleSchema = Type.Object({
|
|
|
4
4
|
userId: Type.String({ minLength: 1 }),
|
|
5
5
|
roleId: Type.String({ minLength: 1 }),
|
|
6
6
|
});
|
|
7
|
-
export const UserRoleModelSpec = entityUtils.getModelSpec(UserRoleSchema);
|
|
7
|
+
export const UserRoleModelSpec = entityUtils.getModelSpec(UserRoleSchema, { isAuditable: true });
|