@loomcore/api 0.2.4 → 0.2.6
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/__tests__/common-test.utils.d.ts +12 -12
- package/dist/__tests__/common-test.utils.js +94 -101
- package/dist/__tests__/postgres.test-database.d.ts +2 -2
- package/dist/__tests__/postgres.test-database.js +16 -15
- package/dist/__tests__/test-objects.d.ts +1 -6
- package/dist/__tests__/test-objects.js +34 -73
- package/dist/config/base-api-config.d.ts +2 -2
- package/dist/config/base-api-config.js +17 -10
- package/dist/controllers/auth.controller.d.ts +3 -5
- package/dist/controllers/auth.controller.js +22 -26
- package/dist/controllers/index.d.ts +10 -11
- package/dist/controllers/index.js +10 -11
- package/dist/databases/mongo-db/migrations/mongo-initial-schema.d.ts +2 -2
- package/dist/databases/mongo-db/migrations/mongo-initial-schema.js +185 -128
- package/dist/services/auth.service.d.ts +8 -9
- package/dist/services/auth.service.js +66 -82
- package/dist/services/email.service.js +5 -5
- package/dist/services/jwt.service.js +2 -2
- package/dist/services/multi-tenant-api.service.d.ts +4 -4
- package/dist/services/multi-tenant-api.service.js +8 -8
- package/dist/services/organization.service.d.ts +4 -4
- package/dist/services/organization.service.js +12 -8
- package/dist/services/password-reset-token.service.d.ts +4 -4
- package/dist/services/password-reset-token.service.js +13 -9
- package/dist/services/tenant-query-decorator.d.ts +1 -1
- package/dist/services/tenant-query-decorator.js +17 -11
- package/dist/services/user.service.d.ts +6 -6
- package/dist/services/user.service.js +7 -7
- package/dist/services/utils/audit-for-create.util.d.ts +1 -1
- package/dist/services/utils/audit-for-update.util.d.ts +1 -1
- package/dist/services/utils/audit-for-update.util.js +0 -1
- package/dist/services/utils/getUserContextAuthorizations.util.d.ts +2 -2
- package/dist/services/utils/strip-sender-provided-system-properties.util.d.ts +1 -1
- package/dist/services/utils/strip-sender-provided-system-properties.util.js +5 -3
- package/package.json +5 -2
- package/dist/controllers/persons.controller.d.ts +0 -8
- package/dist/controllers/persons.controller.js +0 -18
- package/dist/services/person.service.d.ts +0 -6
- package/dist/services/person.service.js +0 -7
|
@@ -1,169 +1,205 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { EmptyUserContext, getSystemUserContext, initializeSystemUserContext, isSystemUserContextInitialized, } from "@loomcore/common/models";
|
|
2
|
+
import { OrganizationService } from "../../../services/index.js";
|
|
3
|
+
import { passwordUtils } from "../../../utils/index.js";
|
|
4
|
+
import { MongoDBDatabase } from "../mongo-db.database.js";
|
|
5
5
|
export const getMongoInitialSchema = (dbConfig) => {
|
|
6
6
|
const migrations = [];
|
|
7
7
|
const isMultiTenant = dbConfig.app.isMultiTenant;
|
|
8
8
|
if (isMultiTenant && !dbConfig.multiTenant) {
|
|
9
|
-
throw new Error(
|
|
9
|
+
throw new Error("Multi-tenant configuration is enabled but multi-tenant configuration is not provided");
|
|
10
10
|
}
|
|
11
11
|
const isAuthEnabled = dbConfig.app.isAuthEnabled;
|
|
12
12
|
if (isMultiTenant)
|
|
13
13
|
migrations.push({
|
|
14
|
-
name:
|
|
14
|
+
name: "00000000000001_schema-organizations",
|
|
15
15
|
up: async ({ context: db }) => {
|
|
16
|
-
await db.createCollection(
|
|
17
|
-
await db
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
await db.createCollection("organizations");
|
|
17
|
+
await db
|
|
18
|
+
.collection("organizations")
|
|
19
|
+
.createIndex({ name: 1 }, { unique: true });
|
|
20
|
+
await db
|
|
21
|
+
.collection("organizations")
|
|
22
|
+
.createIndex({ code: 1 }, { unique: true });
|
|
23
|
+
await db.collection("organizations").createIndex({ isMetaOrg: 1 });
|
|
20
24
|
},
|
|
21
25
|
down: async ({ context: db }) => {
|
|
22
|
-
await db.collection(
|
|
23
|
-
}
|
|
26
|
+
await db.collection("organizations").drop();
|
|
27
|
+
},
|
|
24
28
|
});
|
|
25
29
|
if (isAuthEnabled)
|
|
26
30
|
migrations.push({
|
|
27
|
-
name:
|
|
31
|
+
name: "00000000000002_schema-persons",
|
|
28
32
|
up: async ({ context: db }) => {
|
|
29
|
-
await db.createCollection(
|
|
33
|
+
await db.createCollection("persons");
|
|
30
34
|
if (isMultiTenant) {
|
|
31
|
-
await db.collection(
|
|
32
|
-
await db
|
|
35
|
+
await db.collection("persons").createIndex({ _orgId: 1 });
|
|
36
|
+
await db
|
|
37
|
+
.collection("persons")
|
|
38
|
+
.createIndex({ _orgId: 1, externalId: 1 }, { unique: true, sparse: true });
|
|
33
39
|
}
|
|
34
40
|
else {
|
|
35
|
-
await db
|
|
41
|
+
await db
|
|
42
|
+
.collection("persons")
|
|
43
|
+
.createIndex({ externalId: 1 }, { unique: true, sparse: true });
|
|
36
44
|
}
|
|
37
45
|
},
|
|
38
46
|
down: async ({ context: db }) => {
|
|
39
|
-
await db.collection(
|
|
40
|
-
}
|
|
47
|
+
await db.collection("persons").drop();
|
|
48
|
+
},
|
|
41
49
|
});
|
|
42
50
|
if (isAuthEnabled)
|
|
43
51
|
migrations.push({
|
|
44
|
-
name:
|
|
52
|
+
name: "00000000000003_schema-users",
|
|
45
53
|
up: async ({ context: db }) => {
|
|
46
|
-
await db.createCollection(
|
|
54
|
+
await db.createCollection("users");
|
|
47
55
|
if (dbConfig.app.isMultiTenant) {
|
|
48
|
-
await db
|
|
49
|
-
|
|
56
|
+
await db
|
|
57
|
+
.collection("users")
|
|
58
|
+
.createIndex({ _orgId: 1, email: 1 }, { unique: true });
|
|
59
|
+
await db.collection("users").createIndex({ _orgId: 1 });
|
|
50
60
|
}
|
|
51
61
|
else {
|
|
52
|
-
await db
|
|
62
|
+
await db
|
|
63
|
+
.collection("users")
|
|
64
|
+
.createIndex({ email: 1 }, { unique: true });
|
|
53
65
|
}
|
|
54
66
|
},
|
|
55
67
|
down: async ({ context: db }) => {
|
|
56
|
-
await db.collection(
|
|
57
|
-
}
|
|
68
|
+
await db.collection("users").drop();
|
|
69
|
+
},
|
|
58
70
|
});
|
|
59
71
|
if (isAuthEnabled)
|
|
60
72
|
migrations.push({
|
|
61
|
-
name:
|
|
73
|
+
name: "00000000000004_schema-refresh-tokens",
|
|
62
74
|
up: async ({ context: db }) => {
|
|
63
|
-
await db.createCollection(
|
|
64
|
-
await db
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
await db.createCollection("refresh_tokens");
|
|
76
|
+
await db
|
|
77
|
+
.collection("refresh_tokens")
|
|
78
|
+
.createIndex({ token: 1 }, { unique: true });
|
|
79
|
+
await db.collection("refresh_tokens").createIndex({ userId: 1 });
|
|
80
|
+
await db.collection("refresh_tokens").createIndex({ deviceId: 1 });
|
|
67
81
|
if (isMultiTenant) {
|
|
68
|
-
await db.collection(
|
|
82
|
+
await db.collection("refresh_tokens").createIndex({ _orgId: 1 });
|
|
69
83
|
}
|
|
70
84
|
},
|
|
71
85
|
down: async ({ context: db }) => {
|
|
72
|
-
await db.collection(
|
|
73
|
-
}
|
|
86
|
+
await db.collection("refresh_tokens").drop();
|
|
87
|
+
},
|
|
74
88
|
});
|
|
75
89
|
if (isAuthEnabled)
|
|
76
90
|
migrations.push({
|
|
77
|
-
name:
|
|
91
|
+
name: "00000000000005_schema-password-reset-tokens",
|
|
78
92
|
up: async ({ context: db }) => {
|
|
79
|
-
await db.createCollection(
|
|
93
|
+
await db.createCollection("password_reset_tokens");
|
|
80
94
|
if (isMultiTenant) {
|
|
81
|
-
await db
|
|
82
|
-
|
|
95
|
+
await db
|
|
96
|
+
.collection("password_reset_tokens")
|
|
97
|
+
.createIndex({ _orgId: 1, email: 1 }, { unique: true });
|
|
98
|
+
await db
|
|
99
|
+
.collection("password_reset_tokens")
|
|
100
|
+
.createIndex({ _orgId: 1 });
|
|
83
101
|
}
|
|
84
102
|
else {
|
|
85
|
-
await db
|
|
103
|
+
await db
|
|
104
|
+
.collection("password_reset_tokens")
|
|
105
|
+
.createIndex({ email: 1 }, { unique: true });
|
|
86
106
|
}
|
|
87
107
|
},
|
|
88
108
|
down: async ({ context: db }) => {
|
|
89
|
-
await db.collection(
|
|
90
|
-
}
|
|
109
|
+
await db.collection("password_reset_tokens").drop();
|
|
110
|
+
},
|
|
91
111
|
});
|
|
92
112
|
if (isAuthEnabled)
|
|
93
113
|
migrations.push({
|
|
94
|
-
name:
|
|
114
|
+
name: "00000000000006_schema-roles",
|
|
95
115
|
up: async ({ context: db }) => {
|
|
96
|
-
await db.createCollection(
|
|
116
|
+
await db.createCollection("roles");
|
|
97
117
|
if (isMultiTenant) {
|
|
98
|
-
await db
|
|
99
|
-
|
|
118
|
+
await db
|
|
119
|
+
.collection("roles")
|
|
120
|
+
.createIndex({ _orgId: 1, name: 1 }, { unique: true });
|
|
121
|
+
await db.collection("roles").createIndex({ _orgId: 1 });
|
|
100
122
|
}
|
|
101
123
|
else {
|
|
102
|
-
await db
|
|
124
|
+
await db
|
|
125
|
+
.collection("roles")
|
|
126
|
+
.createIndex({ name: 1 }, { unique: true });
|
|
103
127
|
}
|
|
104
128
|
},
|
|
105
129
|
down: async ({ context: db }) => {
|
|
106
|
-
await db.collection(
|
|
107
|
-
}
|
|
130
|
+
await db.collection("roles").drop();
|
|
131
|
+
},
|
|
108
132
|
});
|
|
109
133
|
if (isAuthEnabled)
|
|
110
134
|
migrations.push({
|
|
111
|
-
name:
|
|
135
|
+
name: "00000000000007_schema-user-roles",
|
|
112
136
|
up: async ({ context: db }) => {
|
|
113
|
-
await db.createCollection(
|
|
137
|
+
await db.createCollection("user_roles");
|
|
114
138
|
if (isMultiTenant) {
|
|
115
|
-
await db
|
|
116
|
-
|
|
139
|
+
await db
|
|
140
|
+
.collection("user_roles")
|
|
141
|
+
.createIndex({ _orgId: 1, userId: 1, roleId: 1 }, { unique: true });
|
|
142
|
+
await db.collection("user_roles").createIndex({ _orgId: 1 });
|
|
117
143
|
}
|
|
118
144
|
else {
|
|
119
|
-
await db
|
|
145
|
+
await db
|
|
146
|
+
.collection("user_roles")
|
|
147
|
+
.createIndex({ userId: 1, roleId: 1 }, { unique: true });
|
|
120
148
|
}
|
|
121
|
-
await db.collection(
|
|
122
|
-
await db.collection(
|
|
149
|
+
await db.collection("user_roles").createIndex({ userId: 1 });
|
|
150
|
+
await db.collection("user_roles").createIndex({ roleId: 1 });
|
|
123
151
|
},
|
|
124
152
|
down: async ({ context: db }) => {
|
|
125
|
-
await db.collection(
|
|
126
|
-
}
|
|
153
|
+
await db.collection("user_roles").drop();
|
|
154
|
+
},
|
|
127
155
|
});
|
|
128
156
|
if (isAuthEnabled)
|
|
129
157
|
migrations.push({
|
|
130
|
-
name:
|
|
158
|
+
name: "00000000000008_schema-features",
|
|
131
159
|
up: async ({ context: db }) => {
|
|
132
|
-
await db.createCollection(
|
|
160
|
+
await db.createCollection("features");
|
|
133
161
|
if (isMultiTenant) {
|
|
134
|
-
await db
|
|
135
|
-
|
|
162
|
+
await db
|
|
163
|
+
.collection("features")
|
|
164
|
+
.createIndex({ _orgId: 1, name: 1 }, { unique: true });
|
|
165
|
+
await db.collection("features").createIndex({ _orgId: 1 });
|
|
136
166
|
}
|
|
137
167
|
else {
|
|
138
|
-
await db
|
|
168
|
+
await db
|
|
169
|
+
.collection("features")
|
|
170
|
+
.createIndex({ name: 1 }, { unique: true });
|
|
139
171
|
}
|
|
140
172
|
},
|
|
141
173
|
down: async ({ context: db }) => {
|
|
142
|
-
await db.collection(
|
|
143
|
-
}
|
|
174
|
+
await db.collection("features").drop();
|
|
175
|
+
},
|
|
144
176
|
});
|
|
145
177
|
if (isAuthEnabled)
|
|
146
178
|
migrations.push({
|
|
147
|
-
name:
|
|
179
|
+
name: "00000000000009_schema-authorizations",
|
|
148
180
|
up: async ({ context: db }) => {
|
|
149
|
-
await db.createCollection(
|
|
181
|
+
await db.createCollection("authorizations");
|
|
150
182
|
if (isMultiTenant) {
|
|
151
|
-
await db
|
|
152
|
-
|
|
183
|
+
await db
|
|
184
|
+
.collection("authorizations")
|
|
185
|
+
.createIndex({ _orgId: 1, roleId: 1, featureId: 1 }, { unique: true });
|
|
186
|
+
await db.collection("authorizations").createIndex({ _orgId: 1 });
|
|
153
187
|
}
|
|
154
188
|
else {
|
|
155
|
-
await db
|
|
189
|
+
await db
|
|
190
|
+
.collection("authorizations")
|
|
191
|
+
.createIndex({ roleId: 1, featureId: 1 }, { unique: true });
|
|
156
192
|
}
|
|
157
|
-
await db.collection(
|
|
158
|
-
await db.collection(
|
|
193
|
+
await db.collection("authorizations").createIndex({ roleId: 1 });
|
|
194
|
+
await db.collection("authorizations").createIndex({ featureId: 1 });
|
|
159
195
|
},
|
|
160
196
|
down: async ({ context: db }) => {
|
|
161
|
-
await db.collection(
|
|
162
|
-
}
|
|
197
|
+
await db.collection("authorizations").drop();
|
|
198
|
+
},
|
|
163
199
|
});
|
|
164
200
|
if (isMultiTenant) {
|
|
165
201
|
migrations.push({
|
|
166
|
-
name:
|
|
202
|
+
name: "00000000000010_data-meta-org",
|
|
167
203
|
up: async ({ context: db }) => {
|
|
168
204
|
const metaOrgDoc = {
|
|
169
205
|
name: dbConfig.multiTenant.metaOrgName,
|
|
@@ -173,106 +209,117 @@ export const getMongoInitialSchema = (dbConfig) => {
|
|
|
173
209
|
isMetaOrg: true,
|
|
174
210
|
authToken: undefined,
|
|
175
211
|
_created: new Date(),
|
|
176
|
-
_createdBy:
|
|
212
|
+
_createdBy: "system",
|
|
177
213
|
_updated: new Date(),
|
|
178
|
-
_updatedBy:
|
|
214
|
+
_updatedBy: "system",
|
|
179
215
|
_deleted: undefined,
|
|
180
|
-
_deletedBy: undefined
|
|
216
|
+
_deletedBy: undefined,
|
|
181
217
|
};
|
|
182
|
-
const result = await db
|
|
183
|
-
|
|
184
|
-
|
|
218
|
+
const result = await db
|
|
219
|
+
.collection("organizations")
|
|
220
|
+
.insertOne(metaOrgDoc);
|
|
221
|
+
const metaOrg = {
|
|
222
|
+
...metaOrgDoc,
|
|
223
|
+
_id: result.insertedId,
|
|
224
|
+
};
|
|
225
|
+
initializeSystemUserContext(dbConfig.email?.systemEmailAddress || "system@example.com", metaOrg);
|
|
185
226
|
},
|
|
186
227
|
down: async ({ context: db }) => {
|
|
187
|
-
await db.collection(
|
|
188
|
-
}
|
|
228
|
+
await db.collection("organizations").deleteMany({ isMetaOrg: true });
|
|
229
|
+
},
|
|
189
230
|
});
|
|
190
231
|
}
|
|
191
232
|
if (isAuthEnabled && dbConfig.adminUser) {
|
|
192
233
|
migrations.push({
|
|
193
|
-
name:
|
|
234
|
+
name: "00000000000011_data-admin-user",
|
|
194
235
|
up: async ({ context: db }) => {
|
|
195
236
|
if (!isSystemUserContextInitialized()) {
|
|
196
237
|
const errorMessage = isMultiTenant
|
|
197
|
-
?
|
|
198
|
-
|
|
199
|
-
:
|
|
200
|
-
console.error(
|
|
238
|
+
? "SystemUserContext has not been initialized. The meta-org migration (00000000000010_data-meta-org) should have run before this migration. " +
|
|
239
|
+
"Please ensure metaOrgName and metaOrgCode are provided in your dbConfig."
|
|
240
|
+
: "BUG: SystemUserContext has not been initialized. For non-multi-tenant setups, SystemUserContext should be initialized before migrations run.";
|
|
241
|
+
console.error("❌ Migration Error:", errorMessage);
|
|
201
242
|
throw new Error(errorMessage);
|
|
202
243
|
}
|
|
203
244
|
const systemUserContext = getSystemUserContext();
|
|
204
|
-
const orgDoc = isMultiTenant && systemUserContext.organization?._id
|
|
245
|
+
const orgDoc = isMultiTenant && systemUserContext.organization?._id
|
|
246
|
+
? { _orgId: systemUserContext.organization._id }
|
|
247
|
+
: {};
|
|
205
248
|
const hashedPassword = await passwordUtils.hashPassword(dbConfig.adminUser.password);
|
|
206
249
|
const email = dbConfig.adminUser.email.toLowerCase();
|
|
207
|
-
const personResult = await db.collection(
|
|
250
|
+
const personResult = await db.collection("persons").insertOne({
|
|
208
251
|
...orgDoc,
|
|
209
|
-
externalId:
|
|
210
|
-
firstName:
|
|
211
|
-
lastName:
|
|
252
|
+
externalId: "admin-user-person-external-id",
|
|
253
|
+
firstName: "Admin",
|
|
254
|
+
lastName: "User",
|
|
212
255
|
isAgent: false,
|
|
213
256
|
isClient: false,
|
|
214
257
|
isEmployee: false,
|
|
215
258
|
_created: new Date(),
|
|
216
|
-
_createdBy:
|
|
259
|
+
_createdBy: "system",
|
|
217
260
|
_updated: new Date(),
|
|
218
|
-
_updatedBy:
|
|
261
|
+
_updatedBy: "system",
|
|
219
262
|
});
|
|
220
|
-
await db.collection(
|
|
263
|
+
await db.collection("users").insertOne({
|
|
221
264
|
...orgDoc,
|
|
222
|
-
externalId:
|
|
265
|
+
externalId: "admin-user-external-id",
|
|
223
266
|
email,
|
|
224
267
|
password: hashedPassword,
|
|
225
|
-
displayName:
|
|
268
|
+
displayName: "Admin User",
|
|
226
269
|
personId: personResult.insertedId,
|
|
227
270
|
_created: new Date(),
|
|
228
|
-
_createdBy:
|
|
271
|
+
_createdBy: "system",
|
|
229
272
|
_updated: new Date(),
|
|
230
|
-
_updatedBy:
|
|
273
|
+
_updatedBy: "system",
|
|
231
274
|
});
|
|
232
275
|
},
|
|
233
276
|
down: async ({ context: db }) => {
|
|
234
277
|
if (!dbConfig?.adminUser?.email)
|
|
235
278
|
return;
|
|
236
|
-
await db
|
|
237
|
-
|
|
279
|
+
await db
|
|
280
|
+
.collection("users")
|
|
281
|
+
.deleteOne({ email: dbConfig.adminUser.email.toLowerCase() });
|
|
282
|
+
},
|
|
238
283
|
});
|
|
239
284
|
}
|
|
240
285
|
if (isAuthEnabled && dbConfig.adminUser) {
|
|
241
286
|
migrations.push({
|
|
242
|
-
name:
|
|
287
|
+
name: "00000000000012_data-admin-authorizations",
|
|
243
288
|
up: async ({ context: db }) => {
|
|
244
289
|
const database = new MongoDBDatabase(db);
|
|
245
290
|
const organizationService = new OrganizationService(database);
|
|
246
|
-
const metaOrg = isMultiTenant
|
|
291
|
+
const metaOrg = isMultiTenant
|
|
292
|
+
? await organizationService.getMetaOrg(EmptyUserContext)
|
|
293
|
+
: undefined;
|
|
247
294
|
if (isMultiTenant && !metaOrg) {
|
|
248
|
-
throw new Error(
|
|
295
|
+
throw new Error("Meta organization not found. Ensure meta-org migration ran successfully.");
|
|
249
296
|
}
|
|
250
297
|
const email = dbConfig.adminUser.email.toLowerCase();
|
|
251
|
-
const adminUser = await db.collection(
|
|
298
|
+
const adminUser = await db.collection("users").findOne({ email });
|
|
252
299
|
if (!adminUser) {
|
|
253
|
-
throw new Error(
|
|
300
|
+
throw new Error("Admin user not found. Ensure admin-user migration ran successfully.");
|
|
254
301
|
}
|
|
255
302
|
const orgDoc = isMultiTenant && metaOrg ? { _orgId: metaOrg._id } : {};
|
|
256
|
-
const roleResult = await db.collection(
|
|
303
|
+
const roleResult = await db.collection("roles").insertOne({
|
|
257
304
|
...orgDoc,
|
|
258
|
-
name:
|
|
305
|
+
name: "admin",
|
|
259
306
|
});
|
|
260
|
-
await db.collection(
|
|
307
|
+
await db.collection("user_roles").insertOne({
|
|
261
308
|
...orgDoc,
|
|
262
309
|
userId: adminUser._id,
|
|
263
310
|
roleId: roleResult.insertedId,
|
|
264
311
|
_created: new Date(),
|
|
265
|
-
_createdBy:
|
|
312
|
+
_createdBy: "system",
|
|
266
313
|
_updated: new Date(),
|
|
267
|
-
_updatedBy:
|
|
314
|
+
_updatedBy: "system",
|
|
268
315
|
_deleted: undefined,
|
|
269
|
-
_deletedBy: undefined
|
|
316
|
+
_deletedBy: undefined,
|
|
270
317
|
});
|
|
271
|
-
const featureResult = await db.collection(
|
|
318
|
+
const featureResult = await db.collection("features").insertOne({
|
|
272
319
|
...orgDoc,
|
|
273
|
-
name:
|
|
320
|
+
name: "admin",
|
|
274
321
|
});
|
|
275
|
-
await db.collection(
|
|
322
|
+
await db.collection("authorizations").insertOne({
|
|
276
323
|
...orgDoc,
|
|
277
324
|
roleId: roleResult.insertedId,
|
|
278
325
|
featureId: featureResult.insertedId,
|
|
@@ -280,38 +327,48 @@ export const getMongoInitialSchema = (dbConfig) => {
|
|
|
280
327
|
endDate: undefined,
|
|
281
328
|
config: undefined,
|
|
282
329
|
_created: new Date(),
|
|
283
|
-
_createdBy:
|
|
330
|
+
_createdBy: "system",
|
|
284
331
|
_updated: new Date(),
|
|
285
|
-
_updatedBy:
|
|
332
|
+
_updatedBy: "system",
|
|
286
333
|
_deleted: undefined,
|
|
287
|
-
_deletedBy: undefined
|
|
334
|
+
_deletedBy: undefined,
|
|
288
335
|
});
|
|
289
336
|
},
|
|
290
337
|
down: async ({ context: db }) => {
|
|
291
338
|
const database = new MongoDBDatabase(db);
|
|
292
339
|
const organizationService = new OrganizationService(database);
|
|
293
|
-
const metaOrg = isMultiTenant
|
|
340
|
+
const metaOrg = isMultiTenant
|
|
341
|
+
? await organizationService.getMetaOrg(EmptyUserContext)
|
|
342
|
+
: undefined;
|
|
294
343
|
if (isMultiTenant && !metaOrg)
|
|
295
344
|
return;
|
|
296
345
|
const orgFilter = isMultiTenant && metaOrg ? { _orgId: metaOrg._id } : {};
|
|
297
|
-
const adminRole = await db
|
|
298
|
-
|
|
346
|
+
const adminRole = await db
|
|
347
|
+
.collection("roles")
|
|
348
|
+
.findOne({ ...orgFilter, name: "admin" });
|
|
349
|
+
const adminFeature = await db
|
|
350
|
+
.collection("features")
|
|
351
|
+
.findOne({ ...orgFilter, name: "admin" });
|
|
299
352
|
if (adminRole && adminFeature) {
|
|
300
|
-
await db.collection(
|
|
353
|
+
await db.collection("authorizations").deleteMany({
|
|
301
354
|
...orgFilter,
|
|
302
355
|
roleId: adminRole._id,
|
|
303
|
-
featureId: adminFeature._id
|
|
356
|
+
featureId: adminFeature._id,
|
|
304
357
|
});
|
|
305
358
|
}
|
|
306
|
-
await db
|
|
359
|
+
await db
|
|
360
|
+
.collection("features")
|
|
361
|
+
.deleteMany({ ...orgFilter, name: "admin" });
|
|
307
362
|
if (adminRole) {
|
|
308
|
-
await db.collection(
|
|
363
|
+
await db.collection("user_roles").deleteMany({
|
|
309
364
|
...orgFilter,
|
|
310
|
-
roleId: adminRole._id
|
|
365
|
+
roleId: adminRole._id,
|
|
311
366
|
});
|
|
312
367
|
}
|
|
313
|
-
await db
|
|
314
|
-
|
|
368
|
+
await db
|
|
369
|
+
.collection("roles")
|
|
370
|
+
.deleteMany({ ...orgFilter, name: "admin" });
|
|
371
|
+
},
|
|
315
372
|
});
|
|
316
373
|
}
|
|
317
374
|
return migrations;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
import { UpdateResult } from
|
|
6
|
-
import { IRefreshToken } from
|
|
7
|
-
import {
|
|
1
|
+
import { type ILoginResponse, type ITokenResponse, type IUser, type IUserContext } from "@loomcore/common/models";
|
|
2
|
+
import type { AppIdType } from "@loomcore/common/types";
|
|
3
|
+
import type { Request, Response } from "express";
|
|
4
|
+
import type { IDatabase } from "../databases/models/index.js";
|
|
5
|
+
import type { UpdateResult } from "../databases/models/update-result.js";
|
|
6
|
+
import { type IRefreshToken } from "../models/refresh-token.model.js";
|
|
7
|
+
import { MultiTenantApiService } from "./multi-tenant-api.service.js";
|
|
8
8
|
export declare class AuthService extends MultiTenantApiService<IUser> {
|
|
9
9
|
private refreshTokenService;
|
|
10
10
|
private passwordResetTokenService;
|
|
11
11
|
private emailService;
|
|
12
12
|
private organizationService;
|
|
13
13
|
private authConfig;
|
|
14
|
-
private personService;
|
|
15
14
|
constructor(database: IDatabase);
|
|
16
15
|
attemptLogin(req: Request, res: Response, email: string, password: string): Promise<ILoginResponse | null>;
|
|
17
16
|
logUserIn(userContext: IUserContext, deviceId: string): Promise<{
|
|
@@ -23,7 +22,7 @@ export declare class AuthService extends MultiTenantApiService<IUser> {
|
|
|
23
22
|
userContext: IUserContext;
|
|
24
23
|
} | null>;
|
|
25
24
|
getUserByEmail(email: string): Promise<IUser | null>;
|
|
26
|
-
createUser(userContext: IUserContext, user: Partial<IUser
|
|
25
|
+
createUser(userContext: IUserContext, user: Partial<IUser>): Promise<IUser | null>;
|
|
27
26
|
requestTokenUsingRefreshToken(refreshToken: string, deviceId: string): Promise<ITokenResponse | null>;
|
|
28
27
|
changeLoggedInUsersPassword(userContext: IUserContext, body: any): Promise<UpdateResult>;
|
|
29
28
|
changePassword(userContext: IUserContext, queryObject: any, password: string): Promise<UpdateResult>;
|