@goweekdays/core 0.0.15 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +69 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -340,12 +340,13 @@ declare function useRoleRepo(): {
|
|
|
340
340
|
createTextIndex: () => Promise<void>;
|
|
341
341
|
createUniqueIndex: () => Promise<void>;
|
|
342
342
|
addRole: (value: TRole, session?: ClientSession) => Promise<ObjectId>;
|
|
343
|
-
getRoles: ({ search, page, limit, sort, type, }?: {
|
|
343
|
+
getRoles: ({ search, page, limit, sort, type, org, }?: {
|
|
344
344
|
search?: string | undefined;
|
|
345
345
|
page?: number | undefined;
|
|
346
346
|
limit?: number | undefined;
|
|
347
|
-
sort?:
|
|
347
|
+
sort?: any;
|
|
348
348
|
type?: string | undefined;
|
|
349
|
+
org?: string | ObjectId | undefined;
|
|
349
350
|
}) => Promise<{
|
|
350
351
|
items: any[];
|
|
351
352
|
pages: number;
|
|
@@ -356,6 +357,7 @@ declare function useRoleRepo(): {
|
|
|
356
357
|
getRoleByName: (name: string) => Promise<mongodb.WithId<bson.Document> | null>;
|
|
357
358
|
updateRole: (_id: string | ObjectId, value: TMiniRole, session?: ClientSession) => Promise<string>;
|
|
358
359
|
deleteRole: (_id: ObjectId | string, session?: ClientSession) => Promise<string>;
|
|
360
|
+
updatePermissionsById: (_id: string | ObjectId, permissions: TRole["permissions"], session?: ClientSession) => Promise<string>;
|
|
359
361
|
};
|
|
360
362
|
|
|
361
363
|
type TFile = {
|
|
@@ -393,11 +395,12 @@ declare function useFileController(): {
|
|
|
393
395
|
|
|
394
396
|
declare function useRoleService(): {
|
|
395
397
|
createRole: (value: Pick<TRole, "type" | "name" | "permissions">) => Promise<bson.ObjectId>;
|
|
396
|
-
getRoles: ({ search, page, limit, type, }?: {
|
|
398
|
+
getRoles: ({ search, page, limit, type, org, }?: {
|
|
397
399
|
search?: string | undefined;
|
|
398
400
|
page?: number | undefined;
|
|
399
401
|
limit?: number | undefined;
|
|
400
402
|
type?: string | undefined;
|
|
403
|
+
org?: string | undefined;
|
|
401
404
|
}) => Promise<{
|
|
402
405
|
items: any[];
|
|
403
406
|
pages: number;
|
|
@@ -417,6 +420,7 @@ declare function useRoleController(): {
|
|
|
417
420
|
getRoleById: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
418
421
|
updateRole: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
419
422
|
deleteRole: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
423
|
+
updatePermissionsById: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
420
424
|
};
|
|
421
425
|
|
|
422
426
|
type TEntity = {
|
package/dist/index.js
CHANGED
|
@@ -17323,10 +17323,18 @@ function useRoleRepo() {
|
|
|
17323
17323
|
page = 1,
|
|
17324
17324
|
limit = 10,
|
|
17325
17325
|
sort = {},
|
|
17326
|
-
type = ""
|
|
17326
|
+
type = "",
|
|
17327
|
+
org = ""
|
|
17327
17328
|
} = {}) {
|
|
17328
17329
|
page = page > 0 ? page - 1 : 0;
|
|
17329
|
-
|
|
17330
|
+
if (org && typeof org === "string" && org.length === 24) {
|
|
17331
|
+
try {
|
|
17332
|
+
org = new import_mongodb13.ObjectId(org);
|
|
17333
|
+
} catch (error) {
|
|
17334
|
+
throw new import_utils41.BadRequestError("Invalid organization ID.");
|
|
17335
|
+
}
|
|
17336
|
+
}
|
|
17337
|
+
const query = { status: "active", org };
|
|
17330
17338
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
17331
17339
|
if (search) {
|
|
17332
17340
|
query.$text = { $search: search };
|
|
@@ -17374,6 +17382,32 @@ function useRoleRepo() {
|
|
|
17374
17382
|
throw new import_utils41.BadRequestError("No fields to update.");
|
|
17375
17383
|
}
|
|
17376
17384
|
}
|
|
17385
|
+
async function updatePermissionsById(_id, permissions, session) {
|
|
17386
|
+
if (!_id) {
|
|
17387
|
+
throw new import_utils41.BadRequestError("Role ID is required.");
|
|
17388
|
+
}
|
|
17389
|
+
try {
|
|
17390
|
+
_id = new import_mongodb13.ObjectId(_id);
|
|
17391
|
+
} catch (error) {
|
|
17392
|
+
throw new import_utils41.BadRequestError("Invalid role ID.");
|
|
17393
|
+
}
|
|
17394
|
+
if (!permissions) {
|
|
17395
|
+
throw new import_utils41.BadRequestError("Permissions are required.");
|
|
17396
|
+
}
|
|
17397
|
+
if (permissions.length === 0) {
|
|
17398
|
+
throw new import_utils41.BadRequestError("Permissions cannot be empty.");
|
|
17399
|
+
}
|
|
17400
|
+
try {
|
|
17401
|
+
await collection.updateOne(
|
|
17402
|
+
{ _id },
|
|
17403
|
+
{ $set: { permissions } },
|
|
17404
|
+
{ session }
|
|
17405
|
+
);
|
|
17406
|
+
return "Successfully updated role permissions.";
|
|
17407
|
+
} catch (error) {
|
|
17408
|
+
throw new import_utils41.InternalServerError("Failed to update role permissions.");
|
|
17409
|
+
}
|
|
17410
|
+
}
|
|
17377
17411
|
async function deleteRole(_id, session) {
|
|
17378
17412
|
try {
|
|
17379
17413
|
_id = new import_mongodb13.ObjectId(_id);
|
|
@@ -17402,7 +17436,8 @@ function useRoleRepo() {
|
|
|
17402
17436
|
getRoleById,
|
|
17403
17437
|
getRoleByName,
|
|
17404
17438
|
updateRole,
|
|
17405
|
-
deleteRole
|
|
17439
|
+
deleteRole,
|
|
17440
|
+
updatePermissionsById
|
|
17406
17441
|
};
|
|
17407
17442
|
}
|
|
17408
17443
|
|
|
@@ -17558,10 +17593,11 @@ function useRoleService() {
|
|
|
17558
17593
|
search = "",
|
|
17559
17594
|
page = 1,
|
|
17560
17595
|
limit = 10,
|
|
17561
|
-
type = ""
|
|
17596
|
+
type = "",
|
|
17597
|
+
org = ""
|
|
17562
17598
|
} = {}) {
|
|
17563
17599
|
try {
|
|
17564
|
-
return await _getRoles({ search, page, limit, type });
|
|
17600
|
+
return await _getRoles({ search, page, limit, type, org });
|
|
17565
17601
|
} catch (error) {
|
|
17566
17602
|
throw error;
|
|
17567
17603
|
}
|
|
@@ -17624,6 +17660,7 @@ function useRoleController() {
|
|
|
17624
17660
|
updateRole: _updateRole,
|
|
17625
17661
|
deleteRole: _deleteRole
|
|
17626
17662
|
} = useRoleService();
|
|
17663
|
+
const { updatePermissionsById: _updatePermissionsById } = useRoleRepo();
|
|
17627
17664
|
async function createRole(req, res, next) {
|
|
17628
17665
|
const name = req.body.name ?? "";
|
|
17629
17666
|
const permissions = req.body.permissions ?? [];
|
|
@@ -17651,19 +17688,21 @@ function useRoleController() {
|
|
|
17651
17688
|
const page = parseInt(req.query.page ?? "1");
|
|
17652
17689
|
const limit = parseInt(req.query.limit ?? "10");
|
|
17653
17690
|
const type = req.query.type ?? "";
|
|
17691
|
+
const org = req.query.org ?? "";
|
|
17654
17692
|
const validation = import_joi7.default.object({
|
|
17655
17693
|
search: import_joi7.default.string().optional().allow("", null),
|
|
17656
17694
|
page: import_joi7.default.number().required(),
|
|
17657
17695
|
limit: import_joi7.default.number().required(),
|
|
17658
|
-
type: import_joi7.default.string().optional().allow("", null)
|
|
17696
|
+
type: import_joi7.default.string().optional().allow("", null),
|
|
17697
|
+
org: import_joi7.default.string().hex().optional().allow("", null)
|
|
17659
17698
|
});
|
|
17660
|
-
const { error } = validation.validate({ search, page, limit, type });
|
|
17699
|
+
const { error } = validation.validate({ search, page, limit, type, org });
|
|
17661
17700
|
if (error) {
|
|
17662
17701
|
next(new import_utils44.BadRequestError(error.message));
|
|
17663
17702
|
return;
|
|
17664
17703
|
}
|
|
17665
17704
|
try {
|
|
17666
|
-
const data = await _getRoles({ search, page, limit, type });
|
|
17705
|
+
const data = await _getRoles({ search, page, limit, type, org });
|
|
17667
17706
|
res.json(data);
|
|
17668
17707
|
return;
|
|
17669
17708
|
} catch (error2) {
|
|
@@ -17729,6 +17768,26 @@ function useRoleController() {
|
|
|
17729
17768
|
next(error2);
|
|
17730
17769
|
}
|
|
17731
17770
|
}
|
|
17771
|
+
async function updatePermissionsById(req, res, next) {
|
|
17772
|
+
const _id = req.params.id;
|
|
17773
|
+
const permissions = req.body.permissions ?? [];
|
|
17774
|
+
const validation = import_joi7.default.object({
|
|
17775
|
+
_id: import_joi7.default.string().required(),
|
|
17776
|
+
permissions: import_joi7.default.array().items(import_joi7.default.string()).required()
|
|
17777
|
+
});
|
|
17778
|
+
const { error } = validation.validate({ _id, permissions });
|
|
17779
|
+
if (error) {
|
|
17780
|
+
next(new import_utils44.BadRequestError(error.message));
|
|
17781
|
+
return;
|
|
17782
|
+
}
|
|
17783
|
+
try {
|
|
17784
|
+
await _updatePermissionsById(_id, permissions);
|
|
17785
|
+
res.json({ message: "Successfully updated role permissions." });
|
|
17786
|
+
return;
|
|
17787
|
+
} catch (error2) {
|
|
17788
|
+
next(error2);
|
|
17789
|
+
}
|
|
17790
|
+
}
|
|
17732
17791
|
async function deleteRole(req, res, next) {
|
|
17733
17792
|
const _id = req.params.id;
|
|
17734
17793
|
const validation = import_joi7.default.object({
|
|
@@ -17753,7 +17812,8 @@ function useRoleController() {
|
|
|
17753
17812
|
getRoleByUserId,
|
|
17754
17813
|
getRoleById,
|
|
17755
17814
|
updateRole,
|
|
17756
|
-
deleteRole
|
|
17815
|
+
deleteRole,
|
|
17816
|
+
updatePermissionsById
|
|
17757
17817
|
};
|
|
17758
17818
|
}
|
|
17759
17819
|
|