@goweekdays/core 2.1.0 → 2.1.2
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 +12 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.js +737 -184
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +696 -137
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6547,7 +6547,8 @@ import Joi19 from "joi";
|
|
|
6547
6547
|
var schemaApp = Joi19.object({
|
|
6548
6548
|
code: Joi19.string().alphanum().max(20).required(),
|
|
6549
6549
|
name: Joi19.string().max(255).required(),
|
|
6550
|
-
description: Joi19.string().max(1024).optional().allow("", null)
|
|
6550
|
+
description: Joi19.string().max(1024).optional().allow("", null),
|
|
6551
|
+
type: Joi19.string().allow("default", "standard").optional().allow("", null)
|
|
6551
6552
|
});
|
|
6552
6553
|
var schemaAppUpdate = Joi19.object({
|
|
6553
6554
|
code: Joi19.string().alphanum().max(20).optional().allow("", null),
|
|
@@ -6564,6 +6565,7 @@ function modelApp(value) {
|
|
|
6564
6565
|
code: value.code,
|
|
6565
6566
|
name: value.name,
|
|
6566
6567
|
description: value.description,
|
|
6568
|
+
type: value.type ?? "standard",
|
|
6567
6569
|
status: value.status ?? "active",
|
|
6568
6570
|
createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
|
|
6569
6571
|
updatedAt: "",
|
|
@@ -6583,6 +6585,8 @@ import {
|
|
|
6583
6585
|
useCache as useCache14
|
|
6584
6586
|
} from "@goweekdays/utils";
|
|
6585
6587
|
import { ObjectId as ObjectId19 } from "mongodb";
|
|
6588
|
+
import Joi20 from "joi";
|
|
6589
|
+
var init = false;
|
|
6586
6590
|
function useAppRepo() {
|
|
6587
6591
|
const db = useAtlas18.getDb();
|
|
6588
6592
|
if (!db) {
|
|
@@ -6617,9 +6621,12 @@ function useAppRepo() {
|
|
|
6617
6621
|
throw new Error("Failed to create index on apps.");
|
|
6618
6622
|
}
|
|
6619
6623
|
}
|
|
6620
|
-
|
|
6621
|
-
|
|
6622
|
-
|
|
6624
|
+
if (!init) {
|
|
6625
|
+
createIndexes().catch((error) => {
|
|
6626
|
+
logger19.log({ level: "error", message: `Index creation error: ${error}` });
|
|
6627
|
+
});
|
|
6628
|
+
init = true;
|
|
6629
|
+
}
|
|
6623
6630
|
async function add(value, session) {
|
|
6624
6631
|
try {
|
|
6625
6632
|
value = modelApp(value);
|
|
@@ -6673,35 +6680,32 @@ function useAppRepo() {
|
|
|
6673
6680
|
page = 1,
|
|
6674
6681
|
limit = 10,
|
|
6675
6682
|
sort = {},
|
|
6676
|
-
|
|
6677
|
-
|
|
6683
|
+
status = "active",
|
|
6684
|
+
type = "standard"
|
|
6678
6685
|
} = {}) {
|
|
6679
6686
|
page = page > 0 ? page - 1 : 0;
|
|
6680
6687
|
const query = {
|
|
6681
6688
|
status
|
|
6682
6689
|
};
|
|
6683
6690
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
6684
|
-
if (search) {
|
|
6685
|
-
query.$text = { $search: search };
|
|
6686
|
-
}
|
|
6687
|
-
if (school) {
|
|
6688
|
-
try {
|
|
6689
|
-
query.school = new ObjectId19(school);
|
|
6690
|
-
} catch (error) {
|
|
6691
|
-
throw new BadRequestError35("Invalid school ID.");
|
|
6692
|
-
}
|
|
6693
|
-
}
|
|
6694
6691
|
const cacheParams = {
|
|
6692
|
+
status,
|
|
6695
6693
|
page,
|
|
6696
6694
|
limit,
|
|
6697
6695
|
sort: JSON.stringify(sort)
|
|
6698
6696
|
};
|
|
6699
|
-
if (search)
|
|
6697
|
+
if (search) {
|
|
6698
|
+
query.$text = { $search: search };
|
|
6700
6699
|
cacheParams.search = search;
|
|
6701
|
-
|
|
6702
|
-
|
|
6703
|
-
|
|
6704
|
-
|
|
6700
|
+
}
|
|
6701
|
+
if (type) {
|
|
6702
|
+
if (Array.isArray(type)) {
|
|
6703
|
+
query.type = { $in: type };
|
|
6704
|
+
} else {
|
|
6705
|
+
query.type = type;
|
|
6706
|
+
}
|
|
6707
|
+
cacheParams.type = type;
|
|
6708
|
+
}
|
|
6705
6709
|
const cacheKey = makeCacheKey13(namespace_collection, cacheParams);
|
|
6706
6710
|
logger19.log({
|
|
6707
6711
|
level: "info",
|
|
@@ -6780,6 +6784,48 @@ function useAppRepo() {
|
|
|
6780
6784
|
}
|
|
6781
6785
|
}
|
|
6782
6786
|
}
|
|
6787
|
+
async function getByCode(code) {
|
|
6788
|
+
const validate = Joi20.string().required();
|
|
6789
|
+
const { error } = validate.validate(code);
|
|
6790
|
+
if (error) {
|
|
6791
|
+
throw new BadRequestError35("Invalid code.");
|
|
6792
|
+
}
|
|
6793
|
+
const cacheKey = makeCacheKey13(namespace_collection, {
|
|
6794
|
+
code,
|
|
6795
|
+
tag: "byCode"
|
|
6796
|
+
});
|
|
6797
|
+
try {
|
|
6798
|
+
const cached = await getCache(cacheKey);
|
|
6799
|
+
if (cached) {
|
|
6800
|
+
logger19.log({
|
|
6801
|
+
level: "info",
|
|
6802
|
+
message: `Cache hit for getByCode app: ${cacheKey}`
|
|
6803
|
+
});
|
|
6804
|
+
return cached;
|
|
6805
|
+
}
|
|
6806
|
+
const result = await collection.findOne({
|
|
6807
|
+
code
|
|
6808
|
+
});
|
|
6809
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
6810
|
+
logger19.log({
|
|
6811
|
+
level: "info",
|
|
6812
|
+
message: `Cache set for app by code: ${cacheKey}`
|
|
6813
|
+
});
|
|
6814
|
+
}).catch((err) => {
|
|
6815
|
+
logger19.log({
|
|
6816
|
+
level: "error",
|
|
6817
|
+
message: `Failed to set cache for app by code: ${err.message}`
|
|
6818
|
+
});
|
|
6819
|
+
});
|
|
6820
|
+
return result;
|
|
6821
|
+
} catch (error2) {
|
|
6822
|
+
if (error2 instanceof AppError13) {
|
|
6823
|
+
throw error2;
|
|
6824
|
+
} else {
|
|
6825
|
+
throw new InternalServerError19("Failed to get app.");
|
|
6826
|
+
}
|
|
6827
|
+
}
|
|
6828
|
+
}
|
|
6783
6829
|
async function deleteById(_id, session) {
|
|
6784
6830
|
try {
|
|
6785
6831
|
_id = new ObjectId19(_id);
|
|
@@ -6823,18 +6869,96 @@ function useAppRepo() {
|
|
|
6823
6869
|
add,
|
|
6824
6870
|
getAll,
|
|
6825
6871
|
getById,
|
|
6872
|
+
getByCode,
|
|
6826
6873
|
updateById,
|
|
6827
6874
|
deleteById
|
|
6828
6875
|
};
|
|
6829
6876
|
}
|
|
6830
6877
|
|
|
6831
6878
|
// src/resources/app/app.service.ts
|
|
6879
|
+
import { logger as logger20, useAtlas as useAtlas19 } from "@goweekdays/utils";
|
|
6880
|
+
var init2 = false;
|
|
6832
6881
|
function useAppService() {
|
|
6833
6882
|
const {
|
|
6834
6883
|
updateById: _updateById,
|
|
6835
6884
|
getById: _getById,
|
|
6836
|
-
deleteById: _deleteById
|
|
6885
|
+
deleteById: _deleteById,
|
|
6886
|
+
getByCode: _getByCode,
|
|
6887
|
+
add: _add
|
|
6837
6888
|
} = useAppRepo();
|
|
6889
|
+
async function addDefaultApps() {
|
|
6890
|
+
const apps = [
|
|
6891
|
+
{
|
|
6892
|
+
code: "admin",
|
|
6893
|
+
name: "Admin",
|
|
6894
|
+
description: "Administrative application.",
|
|
6895
|
+
type: "default"
|
|
6896
|
+
},
|
|
6897
|
+
{
|
|
6898
|
+
code: "marketplace",
|
|
6899
|
+
name: "Marketplace",
|
|
6900
|
+
description: "Marketplace for product listings."
|
|
6901
|
+
},
|
|
6902
|
+
{
|
|
6903
|
+
code: "service",
|
|
6904
|
+
name: "Services",
|
|
6905
|
+
description: "Service offerings and management."
|
|
6906
|
+
},
|
|
6907
|
+
{
|
|
6908
|
+
code: "stay",
|
|
6909
|
+
name: "Stay",
|
|
6910
|
+
description: "Accommodation and lodging services."
|
|
6911
|
+
},
|
|
6912
|
+
{ code: "eat", name: "Eat", description: "Food and dining services." },
|
|
6913
|
+
{
|
|
6914
|
+
code: "experience",
|
|
6915
|
+
name: "Experience",
|
|
6916
|
+
description: "Experiential touring, travel, activities and events."
|
|
6917
|
+
},
|
|
6918
|
+
{
|
|
6919
|
+
code: "ride",
|
|
6920
|
+
name: "Ride",
|
|
6921
|
+
description: "Transportation and ride services."
|
|
6922
|
+
}
|
|
6923
|
+
];
|
|
6924
|
+
const session = useAtlas19.getClient()?.startSession();
|
|
6925
|
+
if (!session) {
|
|
6926
|
+
throw new Error("Failed to start database session.");
|
|
6927
|
+
}
|
|
6928
|
+
try {
|
|
6929
|
+
session?.startTransaction();
|
|
6930
|
+
for (const app of apps) {
|
|
6931
|
+
const existingApp = await _getByCode(app.code);
|
|
6932
|
+
if (!existingApp) {
|
|
6933
|
+
await _add(app, session);
|
|
6934
|
+
}
|
|
6935
|
+
}
|
|
6936
|
+
await session.commitTransaction();
|
|
6937
|
+
logger20.log({
|
|
6938
|
+
level: "info",
|
|
6939
|
+
message: "Default apps added successfully."
|
|
6940
|
+
});
|
|
6941
|
+
return;
|
|
6942
|
+
} catch (error) {
|
|
6943
|
+
await session.abortTransaction();
|
|
6944
|
+
logger20.log({
|
|
6945
|
+
level: "error",
|
|
6946
|
+
message: `Failed to add default apps: ${error}`
|
|
6947
|
+
});
|
|
6948
|
+
throw error;
|
|
6949
|
+
} finally {
|
|
6950
|
+
await session.endSession();
|
|
6951
|
+
}
|
|
6952
|
+
}
|
|
6953
|
+
if (!init2) {
|
|
6954
|
+
addDefaultApps().catch((error) => {
|
|
6955
|
+
logger20.log({
|
|
6956
|
+
level: "error",
|
|
6957
|
+
message: `Error in addDefaultApps: ${error}`
|
|
6958
|
+
});
|
|
6959
|
+
});
|
|
6960
|
+
init2 = true;
|
|
6961
|
+
}
|
|
6838
6962
|
async function deleteById(id) {
|
|
6839
6963
|
try {
|
|
6840
6964
|
await _deleteById(id);
|
|
@@ -6850,7 +6974,7 @@ function useAppService() {
|
|
|
6850
6974
|
|
|
6851
6975
|
// src/resources/app/app.controller.ts
|
|
6852
6976
|
import { BadRequestError as BadRequestError36 } from "@goweekdays/utils";
|
|
6853
|
-
import
|
|
6977
|
+
import Joi21 from "joi";
|
|
6854
6978
|
function useAppController() {
|
|
6855
6979
|
const {
|
|
6856
6980
|
getAll: _getAll,
|
|
@@ -6876,7 +7000,7 @@ function useAppController() {
|
|
|
6876
7000
|
}
|
|
6877
7001
|
async function updateById(req, res, next) {
|
|
6878
7002
|
const id = req.params.id ?? "";
|
|
6879
|
-
const { error: errorId } =
|
|
7003
|
+
const { error: errorId } = Joi21.string().hex().required().validate(id);
|
|
6880
7004
|
if (errorId) {
|
|
6881
7005
|
next(new BadRequestError36(errorId.message));
|
|
6882
7006
|
return;
|
|
@@ -6897,12 +7021,12 @@ function useAppController() {
|
|
|
6897
7021
|
}
|
|
6898
7022
|
async function getAll(req, res, next) {
|
|
6899
7023
|
const query = req.query;
|
|
6900
|
-
const validation =
|
|
6901
|
-
page:
|
|
6902
|
-
limit:
|
|
6903
|
-
search:
|
|
6904
|
-
|
|
6905
|
-
|
|
7024
|
+
const validation = Joi21.object({
|
|
7025
|
+
page: Joi21.number().min(1).optional().allow("", null),
|
|
7026
|
+
limit: Joi21.number().min(1).optional().allow("", null),
|
|
7027
|
+
search: Joi21.string().optional().allow("", null),
|
|
7028
|
+
status: Joi21.string().optional().allow("", null),
|
|
7029
|
+
type: Joi21.string().optional().allow("", null)
|
|
6906
7030
|
});
|
|
6907
7031
|
const { error } = validation.validate(query);
|
|
6908
7032
|
if (error) {
|
|
@@ -6921,16 +7045,16 @@ function useAppController() {
|
|
|
6921
7045
|
});
|
|
6922
7046
|
}
|
|
6923
7047
|
const status = req.query.status ?? "active";
|
|
6924
|
-
const school = req.query.school ?? "";
|
|
6925
7048
|
const search = req.query.search ?? "";
|
|
7049
|
+
let type = req.query.type ? req.query.type.split(",") : "standard";
|
|
6926
7050
|
try {
|
|
6927
7051
|
const buildings = await _getAll({
|
|
6928
7052
|
page,
|
|
6929
7053
|
limit,
|
|
6930
7054
|
sort: sortObj,
|
|
6931
7055
|
status,
|
|
6932
|
-
|
|
6933
|
-
|
|
7056
|
+
search,
|
|
7057
|
+
type
|
|
6934
7058
|
});
|
|
6935
7059
|
res.json(buildings);
|
|
6936
7060
|
return;
|
|
@@ -6940,8 +7064,8 @@ function useAppController() {
|
|
|
6940
7064
|
}
|
|
6941
7065
|
async function getById(req, res, next) {
|
|
6942
7066
|
const id = req.params.id;
|
|
6943
|
-
const validation =
|
|
6944
|
-
id:
|
|
7067
|
+
const validation = Joi21.object({
|
|
7068
|
+
id: Joi21.string().hex().required()
|
|
6945
7069
|
});
|
|
6946
7070
|
const { error } = validation.validate({ id });
|
|
6947
7071
|
if (error) {
|
|
@@ -6961,8 +7085,8 @@ function useAppController() {
|
|
|
6961
7085
|
}
|
|
6962
7086
|
async function deleteById(req, res, next) {
|
|
6963
7087
|
const id = req.params.id;
|
|
6964
|
-
const validation =
|
|
6965
|
-
id:
|
|
7088
|
+
const validation = Joi21.object({
|
|
7089
|
+
id: Joi21.string().hex().required()
|
|
6966
7090
|
});
|
|
6967
7091
|
const { error } = validation.validate({ id });
|
|
6968
7092
|
if (error) {
|
|
@@ -6988,18 +7112,18 @@ function useAppController() {
|
|
|
6988
7112
|
|
|
6989
7113
|
// src/resources/permission/permission.model.ts
|
|
6990
7114
|
import { BadRequestError as BadRequestError37 } from "@goweekdays/utils";
|
|
6991
|
-
import
|
|
6992
|
-
var schemaPermission =
|
|
6993
|
-
app:
|
|
6994
|
-
key:
|
|
6995
|
-
group:
|
|
6996
|
-
description:
|
|
6997
|
-
deprecated:
|
|
7115
|
+
import Joi22 from "joi";
|
|
7116
|
+
var schemaPermission = Joi22.object({
|
|
7117
|
+
app: Joi22.string().required(),
|
|
7118
|
+
key: Joi22.string().required(),
|
|
7119
|
+
group: Joi22.string().required(),
|
|
7120
|
+
description: Joi22.string().required(),
|
|
7121
|
+
deprecated: Joi22.boolean().optional().allow(null)
|
|
6998
7122
|
});
|
|
6999
|
-
var schemaPermissionUpdate =
|
|
7000
|
-
key:
|
|
7001
|
-
group:
|
|
7002
|
-
description:
|
|
7123
|
+
var schemaPermissionUpdate = Joi22.object({
|
|
7124
|
+
key: Joi22.string().optional().allow("", null),
|
|
7125
|
+
group: Joi22.string().optional().allow("", null),
|
|
7126
|
+
description: Joi22.string().max(1024).optional().allow("", null)
|
|
7003
7127
|
});
|
|
7004
7128
|
function modelPermission(value) {
|
|
7005
7129
|
const { error } = schemaPermission.validate(value);
|
|
@@ -7025,15 +7149,16 @@ import {
|
|
|
7025
7149
|
AppError as AppError14,
|
|
7026
7150
|
BadRequestError as BadRequestError38,
|
|
7027
7151
|
InternalServerError as InternalServerError20,
|
|
7028
|
-
logger as
|
|
7152
|
+
logger as logger22,
|
|
7029
7153
|
makeCacheKey as makeCacheKey14,
|
|
7030
7154
|
paginate as paginate10,
|
|
7031
|
-
useAtlas as
|
|
7155
|
+
useAtlas as useAtlas20,
|
|
7032
7156
|
useCache as useCache15
|
|
7033
7157
|
} from "@goweekdays/utils";
|
|
7034
7158
|
import { ObjectId as ObjectId20 } from "mongodb";
|
|
7159
|
+
import Joi23 from "joi";
|
|
7035
7160
|
function usePermissionRepo() {
|
|
7036
|
-
const db =
|
|
7161
|
+
const db = useAtlas20.getDb();
|
|
7037
7162
|
if (!db) {
|
|
7038
7163
|
throw new Error("Unable to connect to server.");
|
|
7039
7164
|
}
|
|
@@ -7061,7 +7186,7 @@ function usePermissionRepo() {
|
|
|
7061
7186
|
}
|
|
7062
7187
|
}
|
|
7063
7188
|
createIndexes().catch((error) => {
|
|
7064
|
-
|
|
7189
|
+
logger22.log({ level: "error", message: `Index creation error: ${error}` });
|
|
7065
7190
|
});
|
|
7066
7191
|
async function add(value, session) {
|
|
7067
7192
|
try {
|
|
@@ -7070,7 +7195,7 @@ function usePermissionRepo() {
|
|
|
7070
7195
|
delCachedData();
|
|
7071
7196
|
return res.insertedId;
|
|
7072
7197
|
} catch (error) {
|
|
7073
|
-
|
|
7198
|
+
logger22.log({
|
|
7074
7199
|
level: "error",
|
|
7075
7200
|
message: error.message
|
|
7076
7201
|
});
|
|
@@ -7104,7 +7229,7 @@ function usePermissionRepo() {
|
|
|
7104
7229
|
delCachedData();
|
|
7105
7230
|
return res;
|
|
7106
7231
|
} catch (error2) {
|
|
7107
|
-
|
|
7232
|
+
logger22.log({
|
|
7108
7233
|
level: "error",
|
|
7109
7234
|
message: error2.message
|
|
7110
7235
|
});
|
|
@@ -7142,14 +7267,14 @@ function usePermissionRepo() {
|
|
|
7142
7267
|
cacheParams.app = app;
|
|
7143
7268
|
}
|
|
7144
7269
|
const cacheKey = makeCacheKey14(namespace_collection, cacheParams);
|
|
7145
|
-
|
|
7270
|
+
logger22.log({
|
|
7146
7271
|
level: "info",
|
|
7147
7272
|
message: `Cache key for getAll permissions: ${cacheKey}`
|
|
7148
7273
|
});
|
|
7149
7274
|
try {
|
|
7150
7275
|
const cached = await getCache(cacheKey);
|
|
7151
7276
|
if (cached) {
|
|
7152
|
-
|
|
7277
|
+
logger22.log({
|
|
7153
7278
|
level: "info",
|
|
7154
7279
|
message: `Cache hit for getAll permissions: ${cacheKey}`
|
|
7155
7280
|
});
|
|
@@ -7164,19 +7289,19 @@ function usePermissionRepo() {
|
|
|
7164
7289
|
const length = await collection.countDocuments(query);
|
|
7165
7290
|
const data = paginate10(items, page, limit, length);
|
|
7166
7291
|
setCache(cacheKey, data, 600).then(() => {
|
|
7167
|
-
|
|
7292
|
+
logger22.log({
|
|
7168
7293
|
level: "info",
|
|
7169
7294
|
message: `Cache set for getAll permissions: ${cacheKey}`
|
|
7170
7295
|
});
|
|
7171
7296
|
}).catch((err) => {
|
|
7172
|
-
|
|
7297
|
+
logger22.log({
|
|
7173
7298
|
level: "error",
|
|
7174
7299
|
message: `Failed to set cache for getAll permissions: ${err.message}`
|
|
7175
7300
|
});
|
|
7176
7301
|
});
|
|
7177
7302
|
return data;
|
|
7178
7303
|
} catch (error) {
|
|
7179
|
-
|
|
7304
|
+
logger22.log({ level: "error", message: `${error}` });
|
|
7180
7305
|
throw error;
|
|
7181
7306
|
}
|
|
7182
7307
|
}
|
|
@@ -7190,7 +7315,7 @@ function usePermissionRepo() {
|
|
|
7190
7315
|
try {
|
|
7191
7316
|
const cached = await getCache(cacheKey);
|
|
7192
7317
|
if (cached) {
|
|
7193
|
-
|
|
7318
|
+
logger22.log({
|
|
7194
7319
|
level: "info",
|
|
7195
7320
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
7196
7321
|
});
|
|
@@ -7200,12 +7325,12 @@ function usePermissionRepo() {
|
|
|
7200
7325
|
_id
|
|
7201
7326
|
});
|
|
7202
7327
|
setCache(cacheKey, result, 300).then(() => {
|
|
7203
|
-
|
|
7328
|
+
logger22.log({
|
|
7204
7329
|
level: "info",
|
|
7205
7330
|
message: `Cache set for permission by id: ${cacheKey}`
|
|
7206
7331
|
});
|
|
7207
7332
|
}).catch((err) => {
|
|
7208
|
-
|
|
7333
|
+
logger22.log({
|
|
7209
7334
|
level: "error",
|
|
7210
7335
|
message: `Failed to set cache for permission by id: ${err.message}`
|
|
7211
7336
|
});
|
|
@@ -7219,6 +7344,54 @@ function usePermissionRepo() {
|
|
|
7219
7344
|
}
|
|
7220
7345
|
}
|
|
7221
7346
|
}
|
|
7347
|
+
async function getByKey(key, group) {
|
|
7348
|
+
const validation = Joi23.object({
|
|
7349
|
+
key: Joi23.string().required(),
|
|
7350
|
+
group: Joi23.string().optional().allow("", null)
|
|
7351
|
+
});
|
|
7352
|
+
const { error } = validation.validate({ key, group });
|
|
7353
|
+
if (error) {
|
|
7354
|
+
throw new BadRequestError38(`Invalid data: ${error.message}`);
|
|
7355
|
+
}
|
|
7356
|
+
const query = {};
|
|
7357
|
+
const cacheKeyOptions = {};
|
|
7358
|
+
query.key = key;
|
|
7359
|
+
cacheKeyOptions.key = key;
|
|
7360
|
+
if (group) {
|
|
7361
|
+
query.group = group;
|
|
7362
|
+
cacheKeyOptions.group = group;
|
|
7363
|
+
}
|
|
7364
|
+
const cacheKey = makeCacheKey14(namespace_collection, cacheKeyOptions);
|
|
7365
|
+
try {
|
|
7366
|
+
const cached = await getCache(cacheKey);
|
|
7367
|
+
if (cached) {
|
|
7368
|
+
logger22.log({
|
|
7369
|
+
level: "info",
|
|
7370
|
+
message: `Cache hit for getById permission: ${cacheKey}`
|
|
7371
|
+
});
|
|
7372
|
+
return cached;
|
|
7373
|
+
}
|
|
7374
|
+
const result = await collection.findOne(query);
|
|
7375
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
7376
|
+
logger22.log({
|
|
7377
|
+
level: "info",
|
|
7378
|
+
message: `Cache set for permission by key: ${cacheKey}`
|
|
7379
|
+
});
|
|
7380
|
+
}).catch((err) => {
|
|
7381
|
+
logger22.log({
|
|
7382
|
+
level: "error",
|
|
7383
|
+
message: `Failed to set cache for permission by key: ${err.message}`
|
|
7384
|
+
});
|
|
7385
|
+
});
|
|
7386
|
+
return result;
|
|
7387
|
+
} catch (error2) {
|
|
7388
|
+
if (error2 instanceof AppError14) {
|
|
7389
|
+
throw error2;
|
|
7390
|
+
} else {
|
|
7391
|
+
throw new InternalServerError20("Failed to get permission.");
|
|
7392
|
+
}
|
|
7393
|
+
}
|
|
7394
|
+
}
|
|
7222
7395
|
async function countByGroup(group) {
|
|
7223
7396
|
const cacheKey = makeCacheKey14(namespace_collection, {
|
|
7224
7397
|
group,
|
|
@@ -7227,22 +7400,22 @@ function usePermissionRepo() {
|
|
|
7227
7400
|
try {
|
|
7228
7401
|
const cached = await getCache(cacheKey);
|
|
7229
7402
|
if (cached) {
|
|
7230
|
-
|
|
7403
|
+
logger22.log({
|
|
7231
7404
|
level: "info",
|
|
7232
7405
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
7233
7406
|
});
|
|
7234
7407
|
return cached;
|
|
7235
7408
|
}
|
|
7236
7409
|
const result = await collection.countDocuments({
|
|
7237
|
-
|
|
7410
|
+
group
|
|
7238
7411
|
});
|
|
7239
7412
|
setCache(cacheKey, result, 300).then(() => {
|
|
7240
|
-
|
|
7413
|
+
logger22.log({
|
|
7241
7414
|
level: "info",
|
|
7242
7415
|
message: `Cache set for permission count by group: ${cacheKey}`
|
|
7243
7416
|
});
|
|
7244
7417
|
}).catch((err) => {
|
|
7245
|
-
|
|
7418
|
+
logger22.log({
|
|
7246
7419
|
level: "error",
|
|
7247
7420
|
message: `Failed to set cache for permission by group: ${err.message}`
|
|
7248
7421
|
});
|
|
@@ -7270,7 +7443,7 @@ function usePermissionRepo() {
|
|
|
7270
7443
|
delCachedData();
|
|
7271
7444
|
return res;
|
|
7272
7445
|
} catch (error) {
|
|
7273
|
-
|
|
7446
|
+
logger22.log({
|
|
7274
7447
|
level: "error",
|
|
7275
7448
|
message: error.message
|
|
7276
7449
|
});
|
|
@@ -7283,12 +7456,12 @@ function usePermissionRepo() {
|
|
|
7283
7456
|
}
|
|
7284
7457
|
function delCachedData() {
|
|
7285
7458
|
delNamespace().then(() => {
|
|
7286
|
-
|
|
7459
|
+
logger22.log({
|
|
7287
7460
|
level: "info",
|
|
7288
7461
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
7289
7462
|
});
|
|
7290
7463
|
}).catch((err) => {
|
|
7291
|
-
|
|
7464
|
+
logger22.log({
|
|
7292
7465
|
level: "error",
|
|
7293
7466
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
7294
7467
|
});
|
|
@@ -7299,6 +7472,7 @@ function usePermissionRepo() {
|
|
|
7299
7472
|
add,
|
|
7300
7473
|
getAll,
|
|
7301
7474
|
getById,
|
|
7475
|
+
getByKey,
|
|
7302
7476
|
updateById,
|
|
7303
7477
|
deleteById,
|
|
7304
7478
|
countByGroup
|
|
@@ -7326,8 +7500,8 @@ function usePermissionService() {
|
|
|
7326
7500
|
}
|
|
7327
7501
|
|
|
7328
7502
|
// src/resources/permission/permission.controller.ts
|
|
7329
|
-
import { BadRequestError as BadRequestError39, logger as
|
|
7330
|
-
import
|
|
7503
|
+
import { BadRequestError as BadRequestError39, logger as logger23 } from "@goweekdays/utils";
|
|
7504
|
+
import Joi24 from "joi";
|
|
7331
7505
|
function usePermissionController() {
|
|
7332
7506
|
const {
|
|
7333
7507
|
getAll: _getAll,
|
|
@@ -7341,7 +7515,7 @@ function usePermissionController() {
|
|
|
7341
7515
|
const { error } = schemaPermission.validate(value);
|
|
7342
7516
|
if (error) {
|
|
7343
7517
|
next(new BadRequestError39(error.message));
|
|
7344
|
-
|
|
7518
|
+
logger23.info(`Controller: ${error.message}`);
|
|
7345
7519
|
return;
|
|
7346
7520
|
}
|
|
7347
7521
|
try {
|
|
@@ -7354,12 +7528,12 @@ function usePermissionController() {
|
|
|
7354
7528
|
}
|
|
7355
7529
|
async function getAll(req, res, next) {
|
|
7356
7530
|
const query = req.query;
|
|
7357
|
-
const validation =
|
|
7358
|
-
page:
|
|
7359
|
-
limit:
|
|
7360
|
-
search:
|
|
7361
|
-
app:
|
|
7362
|
-
status:
|
|
7531
|
+
const validation = Joi24.object({
|
|
7532
|
+
page: Joi24.number().min(1).optional().allow("", null),
|
|
7533
|
+
limit: Joi24.number().min(1).optional().allow("", null),
|
|
7534
|
+
search: Joi24.string().optional().allow("", null),
|
|
7535
|
+
app: Joi24.string().optional().allow("", null),
|
|
7536
|
+
status: Joi24.string().optional().allow("", null)
|
|
7363
7537
|
});
|
|
7364
7538
|
const { error } = validation.validate(query);
|
|
7365
7539
|
if (error) {
|
|
@@ -7397,8 +7571,8 @@ function usePermissionController() {
|
|
|
7397
7571
|
}
|
|
7398
7572
|
async function getById(req, res, next) {
|
|
7399
7573
|
const id = req.params.id;
|
|
7400
|
-
const validation =
|
|
7401
|
-
id:
|
|
7574
|
+
const validation = Joi24.object({
|
|
7575
|
+
id: Joi24.string().hex().required()
|
|
7402
7576
|
});
|
|
7403
7577
|
const { error } = validation.validate({ id });
|
|
7404
7578
|
if (error) {
|
|
@@ -7418,8 +7592,8 @@ function usePermissionController() {
|
|
|
7418
7592
|
}
|
|
7419
7593
|
async function deleteById(req, res, next) {
|
|
7420
7594
|
const id = req.params.id;
|
|
7421
|
-
const validation =
|
|
7422
|
-
id:
|
|
7595
|
+
const validation = Joi24.object({
|
|
7596
|
+
id: Joi24.string().hex().required()
|
|
7423
7597
|
});
|
|
7424
7598
|
const { error } = validation.validate({ id });
|
|
7425
7599
|
if (error) {
|
|
@@ -7436,7 +7610,7 @@ function usePermissionController() {
|
|
|
7436
7610
|
}
|
|
7437
7611
|
async function updateById(req, res, next) {
|
|
7438
7612
|
const id = req.params.id;
|
|
7439
|
-
const { error: errorId } =
|
|
7613
|
+
const { error: errorId } = Joi24.string().hex().required().validate(id);
|
|
7440
7614
|
if (errorId) {
|
|
7441
7615
|
next(new BadRequestError39(errorId.message));
|
|
7442
7616
|
return;
|
|
@@ -7466,17 +7640,17 @@ function usePermissionController() {
|
|
|
7466
7640
|
|
|
7467
7641
|
// src/resources/permission/permission.group.model.ts
|
|
7468
7642
|
import { BadRequestError as BadRequestError40 } from "@goweekdays/utils";
|
|
7469
|
-
import
|
|
7470
|
-
var schemaPermissionGroup =
|
|
7471
|
-
app:
|
|
7472
|
-
key:
|
|
7473
|
-
label:
|
|
7474
|
-
order:
|
|
7643
|
+
import Joi25 from "joi";
|
|
7644
|
+
var schemaPermissionGroup = Joi25.object({
|
|
7645
|
+
app: Joi25.string().required(),
|
|
7646
|
+
key: Joi25.string().required(),
|
|
7647
|
+
label: Joi25.string().required(),
|
|
7648
|
+
order: Joi25.number().integer().optional().allow("", null)
|
|
7475
7649
|
});
|
|
7476
|
-
var schemaPermissionGroupUpdate =
|
|
7477
|
-
key:
|
|
7478
|
-
label:
|
|
7479
|
-
order:
|
|
7650
|
+
var schemaPermissionGroupUpdate = Joi25.object({
|
|
7651
|
+
key: Joi25.string().optional().allow("", null),
|
|
7652
|
+
label: Joi25.string().optional().allow("", null),
|
|
7653
|
+
order: Joi25.number().integer().optional().allow("", null)
|
|
7480
7654
|
});
|
|
7481
7655
|
function modelPermissionGroup(value) {
|
|
7482
7656
|
const { error } = schemaPermissionGroup.validate(value);
|
|
@@ -7501,15 +7675,17 @@ import {
|
|
|
7501
7675
|
AppError as AppError15,
|
|
7502
7676
|
BadRequestError as BadRequestError41,
|
|
7503
7677
|
InternalServerError as InternalServerError21,
|
|
7504
|
-
logger as
|
|
7678
|
+
logger as logger24,
|
|
7505
7679
|
makeCacheKey as makeCacheKey15,
|
|
7506
7680
|
paginate as paginate11,
|
|
7507
|
-
useAtlas as
|
|
7681
|
+
useAtlas as useAtlas21,
|
|
7508
7682
|
useCache as useCache16
|
|
7509
7683
|
} from "@goweekdays/utils";
|
|
7510
7684
|
import { ObjectId as ObjectId21 } from "mongodb";
|
|
7685
|
+
import Joi26 from "joi";
|
|
7686
|
+
var init3 = false;
|
|
7511
7687
|
function usePermissionGroupRepo() {
|
|
7512
|
-
const db =
|
|
7688
|
+
const db = useAtlas21.getDb();
|
|
7513
7689
|
if (!db) {
|
|
7514
7690
|
throw new Error("Unable to connect to server.");
|
|
7515
7691
|
}
|
|
@@ -7536,17 +7712,24 @@ function usePermissionGroupRepo() {
|
|
|
7536
7712
|
throw new Error("Failed to create index on permission groups.");
|
|
7537
7713
|
}
|
|
7538
7714
|
}
|
|
7539
|
-
|
|
7540
|
-
|
|
7541
|
-
|
|
7715
|
+
if (!init3) {
|
|
7716
|
+
createIndexes().catch((error) => {
|
|
7717
|
+
logger24.log({
|
|
7718
|
+
level: "error",
|
|
7719
|
+
message: `Index creation error: ${error}`
|
|
7720
|
+
});
|
|
7721
|
+
});
|
|
7722
|
+
init3 = true;
|
|
7723
|
+
}
|
|
7542
7724
|
async function add(value, session) {
|
|
7543
7725
|
try {
|
|
7544
7726
|
value = modelPermissionGroup(value);
|
|
7727
|
+
console.log(value);
|
|
7545
7728
|
const res = await collection.insertOne(value, { session });
|
|
7546
7729
|
delCachedData();
|
|
7547
7730
|
return res.insertedId;
|
|
7548
7731
|
} catch (error) {
|
|
7549
|
-
|
|
7732
|
+
logger24.log({
|
|
7550
7733
|
level: "error",
|
|
7551
7734
|
message: error.message
|
|
7552
7735
|
});
|
|
@@ -7555,7 +7738,7 @@ function usePermissionGroupRepo() {
|
|
|
7555
7738
|
} else {
|
|
7556
7739
|
const isDuplicated = error.message.includes("duplicate");
|
|
7557
7740
|
if (isDuplicated) {
|
|
7558
|
-
throw new BadRequestError41("
|
|
7741
|
+
throw new BadRequestError41("Permission group already exists.");
|
|
7559
7742
|
}
|
|
7560
7743
|
throw new Error("Failed to create permission group.");
|
|
7561
7744
|
}
|
|
@@ -7580,7 +7763,7 @@ function usePermissionGroupRepo() {
|
|
|
7580
7763
|
delCachedData();
|
|
7581
7764
|
return res;
|
|
7582
7765
|
} catch (error2) {
|
|
7583
|
-
|
|
7766
|
+
logger24.log({
|
|
7584
7767
|
level: "error",
|
|
7585
7768
|
message: error2.message
|
|
7586
7769
|
});
|
|
@@ -7618,14 +7801,14 @@ function usePermissionGroupRepo() {
|
|
|
7618
7801
|
cacheParams.app = app;
|
|
7619
7802
|
}
|
|
7620
7803
|
const cacheKey = makeCacheKey15(namespace_collection, cacheParams);
|
|
7621
|
-
|
|
7804
|
+
logger24.log({
|
|
7622
7805
|
level: "info",
|
|
7623
7806
|
message: `Cache key for getAll permission groups: ${cacheKey}`
|
|
7624
7807
|
});
|
|
7625
7808
|
try {
|
|
7626
7809
|
const cached = await getCache(cacheKey);
|
|
7627
7810
|
if (cached) {
|
|
7628
|
-
|
|
7811
|
+
logger24.log({
|
|
7629
7812
|
level: "info",
|
|
7630
7813
|
message: `Cache hit for getAll permission groups: ${cacheKey}`
|
|
7631
7814
|
});
|
|
@@ -7640,19 +7823,19 @@ function usePermissionGroupRepo() {
|
|
|
7640
7823
|
const length = await collection.countDocuments(query);
|
|
7641
7824
|
const data = paginate11(items, page, limit, length);
|
|
7642
7825
|
setCache(cacheKey, data, 600).then(() => {
|
|
7643
|
-
|
|
7826
|
+
logger24.log({
|
|
7644
7827
|
level: "info",
|
|
7645
7828
|
message: `Cache set for getAll permission groups: ${cacheKey}`
|
|
7646
7829
|
});
|
|
7647
7830
|
}).catch((err) => {
|
|
7648
|
-
|
|
7831
|
+
logger24.log({
|
|
7649
7832
|
level: "error",
|
|
7650
7833
|
message: `Failed to set cache for getAll permission groups: ${err.message}`
|
|
7651
7834
|
});
|
|
7652
7835
|
});
|
|
7653
7836
|
return data;
|
|
7654
7837
|
} catch (error) {
|
|
7655
|
-
|
|
7838
|
+
logger24.log({ level: "error", message: `${error}` });
|
|
7656
7839
|
throw error;
|
|
7657
7840
|
}
|
|
7658
7841
|
}
|
|
@@ -7666,7 +7849,7 @@ function usePermissionGroupRepo() {
|
|
|
7666
7849
|
try {
|
|
7667
7850
|
const cached = await getCache(cacheKey);
|
|
7668
7851
|
if (cached) {
|
|
7669
|
-
|
|
7852
|
+
logger24.log({
|
|
7670
7853
|
level: "info",
|
|
7671
7854
|
message: `Cache hit for getById permission group: ${cacheKey}`
|
|
7672
7855
|
});
|
|
@@ -7676,12 +7859,12 @@ function usePermissionGroupRepo() {
|
|
|
7676
7859
|
_id
|
|
7677
7860
|
});
|
|
7678
7861
|
setCache(cacheKey, result, 300).then(() => {
|
|
7679
|
-
|
|
7862
|
+
logger24.log({
|
|
7680
7863
|
level: "info",
|
|
7681
7864
|
message: `Cache set for permission group by id: ${cacheKey}`
|
|
7682
7865
|
});
|
|
7683
7866
|
}).catch((err) => {
|
|
7684
|
-
|
|
7867
|
+
logger24.log({
|
|
7685
7868
|
level: "error",
|
|
7686
7869
|
message: `Failed to set cache for permission group by id: ${err.message}`
|
|
7687
7870
|
});
|
|
@@ -7695,6 +7878,52 @@ function usePermissionGroupRepo() {
|
|
|
7695
7878
|
}
|
|
7696
7879
|
}
|
|
7697
7880
|
}
|
|
7881
|
+
async function getByKey(key, app) {
|
|
7882
|
+
const validation = Joi26.object({
|
|
7883
|
+
key: Joi26.string().required(),
|
|
7884
|
+
app: Joi26.string().optional().allow(null, "")
|
|
7885
|
+
});
|
|
7886
|
+
const { error } = validation.validate({ key, app });
|
|
7887
|
+
if (error) {
|
|
7888
|
+
throw new BadRequestError41("Invalid key.");
|
|
7889
|
+
}
|
|
7890
|
+
const query = { key };
|
|
7891
|
+
const cacheKeyOptions = { key, tag: "byKey" };
|
|
7892
|
+
if (app) {
|
|
7893
|
+
query.app = app;
|
|
7894
|
+
cacheKeyOptions.app = app;
|
|
7895
|
+
}
|
|
7896
|
+
const cacheKey = makeCacheKey15(namespace_collection, cacheKeyOptions);
|
|
7897
|
+
try {
|
|
7898
|
+
const cached = await getCache(cacheKey);
|
|
7899
|
+
if (cached) {
|
|
7900
|
+
logger24.log({
|
|
7901
|
+
level: "info",
|
|
7902
|
+
message: `Cache hit for getById permission group: ${cacheKey}`
|
|
7903
|
+
});
|
|
7904
|
+
return cached;
|
|
7905
|
+
}
|
|
7906
|
+
const result = await collection.findOne(query);
|
|
7907
|
+
setCache(cacheKey, result, 300).then(() => {
|
|
7908
|
+
logger24.log({
|
|
7909
|
+
level: "info",
|
|
7910
|
+
message: `Cache set for permission group by key: ${cacheKey}`
|
|
7911
|
+
});
|
|
7912
|
+
}).catch((err) => {
|
|
7913
|
+
logger24.log({
|
|
7914
|
+
level: "error",
|
|
7915
|
+
message: `Failed to set cache for permission group by key: ${err.message}`
|
|
7916
|
+
});
|
|
7917
|
+
});
|
|
7918
|
+
return result;
|
|
7919
|
+
} catch (error2) {
|
|
7920
|
+
if (error2 instanceof AppError15) {
|
|
7921
|
+
throw error2;
|
|
7922
|
+
} else {
|
|
7923
|
+
throw new InternalServerError21("Failed to get permission group.");
|
|
7924
|
+
}
|
|
7925
|
+
}
|
|
7926
|
+
}
|
|
7698
7927
|
async function deleteById(_id, session) {
|
|
7699
7928
|
try {
|
|
7700
7929
|
_id = new ObjectId21(_id);
|
|
@@ -7709,7 +7938,7 @@ function usePermissionGroupRepo() {
|
|
|
7709
7938
|
delCachedData();
|
|
7710
7939
|
return res;
|
|
7711
7940
|
} catch (error) {
|
|
7712
|
-
|
|
7941
|
+
logger24.log({
|
|
7713
7942
|
level: "error",
|
|
7714
7943
|
message: error.message
|
|
7715
7944
|
});
|
|
@@ -7722,12 +7951,12 @@ function usePermissionGroupRepo() {
|
|
|
7722
7951
|
}
|
|
7723
7952
|
function delCachedData() {
|
|
7724
7953
|
delNamespace().then(() => {
|
|
7725
|
-
|
|
7954
|
+
logger24.log({
|
|
7726
7955
|
level: "info",
|
|
7727
7956
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
7728
7957
|
});
|
|
7729
7958
|
}).catch((err) => {
|
|
7730
|
-
|
|
7959
|
+
logger24.log({
|
|
7731
7960
|
level: "error",
|
|
7732
7961
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
7733
7962
|
});
|
|
@@ -7738,19 +7967,345 @@ function usePermissionGroupRepo() {
|
|
|
7738
7967
|
add,
|
|
7739
7968
|
getAll,
|
|
7740
7969
|
getById,
|
|
7970
|
+
getByKey,
|
|
7741
7971
|
updateById,
|
|
7742
7972
|
deleteById
|
|
7743
7973
|
};
|
|
7744
7974
|
}
|
|
7745
7975
|
|
|
7746
7976
|
// src/resources/permission/permission.group.service.ts
|
|
7977
|
+
import {
|
|
7978
|
+
AppError as AppError16,
|
|
7979
|
+
BadRequestError as BadRequestError42,
|
|
7980
|
+
InternalServerError as InternalServerError22,
|
|
7981
|
+
logger as logger25,
|
|
7982
|
+
useAtlas as useAtlas22
|
|
7983
|
+
} from "@goweekdays/utils";
|
|
7984
|
+
var init4 = false;
|
|
7747
7985
|
function usePermissionGroupService() {
|
|
7748
7986
|
const {
|
|
7749
7987
|
updateById: _updateById,
|
|
7750
7988
|
getById: _getById,
|
|
7751
|
-
deleteById: _deleteById
|
|
7989
|
+
deleteById: _deleteById,
|
|
7990
|
+
getByKey: _getByKey,
|
|
7991
|
+
add: _add
|
|
7752
7992
|
} = usePermissionGroupRepo();
|
|
7753
|
-
const {
|
|
7993
|
+
const { getAll: getAllApps } = useAppRepo();
|
|
7994
|
+
const {
|
|
7995
|
+
countByGroup,
|
|
7996
|
+
getByKey: getPermissionByKey,
|
|
7997
|
+
add: addPermission
|
|
7998
|
+
} = usePermissionRepo();
|
|
7999
|
+
async function addDefaultModule() {
|
|
8000
|
+
const session = useAtlas22.getClient()?.startSession();
|
|
8001
|
+
if (!session) {
|
|
8002
|
+
throw new Error("Failed to start database session.");
|
|
8003
|
+
}
|
|
8004
|
+
try {
|
|
8005
|
+
session?.startTransaction();
|
|
8006
|
+
const apps = await getAllApps({ limit: 20 });
|
|
8007
|
+
if (apps && apps.items && apps.items.length) {
|
|
8008
|
+
const modules = [
|
|
8009
|
+
{
|
|
8010
|
+
key: "members",
|
|
8011
|
+
label: "Members",
|
|
8012
|
+
permissions: [
|
|
8013
|
+
{
|
|
8014
|
+
key: "members.view",
|
|
8015
|
+
description: "View members"
|
|
8016
|
+
},
|
|
8017
|
+
{
|
|
8018
|
+
key: "members.view.details",
|
|
8019
|
+
description: "View member details"
|
|
8020
|
+
},
|
|
8021
|
+
{
|
|
8022
|
+
key: "members.add",
|
|
8023
|
+
description: "Add new members"
|
|
8024
|
+
},
|
|
8025
|
+
{
|
|
8026
|
+
key: "members.edit.details",
|
|
8027
|
+
description: "Edit member details"
|
|
8028
|
+
},
|
|
8029
|
+
{
|
|
8030
|
+
key: "members.delete",
|
|
8031
|
+
description: "Delete members"
|
|
8032
|
+
}
|
|
8033
|
+
]
|
|
8034
|
+
},
|
|
8035
|
+
{
|
|
8036
|
+
key: "roles",
|
|
8037
|
+
label: "Roles",
|
|
8038
|
+
permissions: [
|
|
8039
|
+
{
|
|
8040
|
+
key: "roles.view",
|
|
8041
|
+
description: "View roles"
|
|
8042
|
+
},
|
|
8043
|
+
{ key: "roles.view.details", description: "View role details" },
|
|
8044
|
+
{
|
|
8045
|
+
key: "roles.add",
|
|
8046
|
+
description: "Add new roles"
|
|
8047
|
+
},
|
|
8048
|
+
{
|
|
8049
|
+
key: "roles.edit.details",
|
|
8050
|
+
description: "Edit role details"
|
|
8051
|
+
},
|
|
8052
|
+
{
|
|
8053
|
+
key: "roles.delete",
|
|
8054
|
+
description: "Delete roles"
|
|
8055
|
+
}
|
|
8056
|
+
]
|
|
8057
|
+
},
|
|
8058
|
+
{
|
|
8059
|
+
key: "invitations",
|
|
8060
|
+
label: "Invitations",
|
|
8061
|
+
permissions: [
|
|
8062
|
+
{
|
|
8063
|
+
key: "invitations.view",
|
|
8064
|
+
description: "View invitations"
|
|
8065
|
+
},
|
|
8066
|
+
{
|
|
8067
|
+
key: "invitations.view.details",
|
|
8068
|
+
description: "View invitation details"
|
|
8069
|
+
},
|
|
8070
|
+
{ key: "invitations.send", description: "Send invitations" },
|
|
8071
|
+
{ key: "invitations.revoke", description: "Revoke invitations" }
|
|
8072
|
+
]
|
|
8073
|
+
}
|
|
8074
|
+
];
|
|
8075
|
+
for (const app of apps.items) {
|
|
8076
|
+
for (const module of modules) {
|
|
8077
|
+
const existingGroup = await _getByKey(module.key);
|
|
8078
|
+
if (!existingGroup) {
|
|
8079
|
+
await _add({
|
|
8080
|
+
app: app.code,
|
|
8081
|
+
key: module.key,
|
|
8082
|
+
label: module.label
|
|
8083
|
+
});
|
|
8084
|
+
logger25.log({
|
|
8085
|
+
level: "info",
|
|
8086
|
+
message: `Default permission group added: ${app.code} - ${module.key}`
|
|
8087
|
+
});
|
|
8088
|
+
}
|
|
8089
|
+
for (const permission of module.permissions) {
|
|
8090
|
+
const existingPermission = await getPermissionByKey(
|
|
8091
|
+
permission.key,
|
|
8092
|
+
module.key
|
|
8093
|
+
);
|
|
8094
|
+
if (!existingPermission) {
|
|
8095
|
+
await addPermission({
|
|
8096
|
+
app: app.code,
|
|
8097
|
+
group: module.key,
|
|
8098
|
+
key: permission.key,
|
|
8099
|
+
description: permission.description
|
|
8100
|
+
});
|
|
8101
|
+
}
|
|
8102
|
+
}
|
|
8103
|
+
}
|
|
8104
|
+
if (app.code === "admin") {
|
|
8105
|
+
const modules2 = [
|
|
8106
|
+
{
|
|
8107
|
+
key: "applications",
|
|
8108
|
+
label: "Applications",
|
|
8109
|
+
permissions: [
|
|
8110
|
+
{
|
|
8111
|
+
key: "applications.add",
|
|
8112
|
+
description: "Add new applications"
|
|
8113
|
+
},
|
|
8114
|
+
{
|
|
8115
|
+
key: "applications.view",
|
|
8116
|
+
description: "View applications"
|
|
8117
|
+
},
|
|
8118
|
+
{
|
|
8119
|
+
key: "applications.view.details",
|
|
8120
|
+
description: "View application details"
|
|
8121
|
+
},
|
|
8122
|
+
{
|
|
8123
|
+
key: "applications.edit",
|
|
8124
|
+
description: "Edit existing applications"
|
|
8125
|
+
},
|
|
8126
|
+
{
|
|
8127
|
+
key: "applications.update.details",
|
|
8128
|
+
description: "Update application details"
|
|
8129
|
+
},
|
|
8130
|
+
{
|
|
8131
|
+
key: "applications.delete",
|
|
8132
|
+
description: "Delete applications"
|
|
8133
|
+
}
|
|
8134
|
+
]
|
|
8135
|
+
},
|
|
8136
|
+
{
|
|
8137
|
+
key: "organizations",
|
|
8138
|
+
label: "Organizations",
|
|
8139
|
+
permissions: [
|
|
8140
|
+
{
|
|
8141
|
+
key: "organizations.add",
|
|
8142
|
+
description: "Add new organizations"
|
|
8143
|
+
},
|
|
8144
|
+
{
|
|
8145
|
+
key: "organizations.view",
|
|
8146
|
+
description: "View organizations"
|
|
8147
|
+
},
|
|
8148
|
+
{
|
|
8149
|
+
key: "organizations.view.details",
|
|
8150
|
+
description: "View organization details"
|
|
8151
|
+
},
|
|
8152
|
+
{
|
|
8153
|
+
key: "organizations.edit.status",
|
|
8154
|
+
description: "Edit organization status"
|
|
8155
|
+
},
|
|
8156
|
+
{
|
|
8157
|
+
key: "organizations.delete",
|
|
8158
|
+
description: "Delete organizations"
|
|
8159
|
+
}
|
|
8160
|
+
]
|
|
8161
|
+
},
|
|
8162
|
+
{
|
|
8163
|
+
key: "users",
|
|
8164
|
+
label: "Users",
|
|
8165
|
+
permissions: [
|
|
8166
|
+
{
|
|
8167
|
+
key: "users.add",
|
|
8168
|
+
description: "Add new users"
|
|
8169
|
+
},
|
|
8170
|
+
{
|
|
8171
|
+
key: "users.view",
|
|
8172
|
+
description: "View users"
|
|
8173
|
+
},
|
|
8174
|
+
{
|
|
8175
|
+
key: "users.view.details",
|
|
8176
|
+
description: "View user details"
|
|
8177
|
+
},
|
|
8178
|
+
{
|
|
8179
|
+
key: "users.edit.status",
|
|
8180
|
+
description: "Edit user status"
|
|
8181
|
+
},
|
|
8182
|
+
{
|
|
8183
|
+
key: "users.delete",
|
|
8184
|
+
description: "Delete users"
|
|
8185
|
+
}
|
|
8186
|
+
]
|
|
8187
|
+
},
|
|
8188
|
+
{
|
|
8189
|
+
key: "subscriptions",
|
|
8190
|
+
label: "Subscriptions",
|
|
8191
|
+
permissions: [
|
|
8192
|
+
{
|
|
8193
|
+
key: "subscriptions.view",
|
|
8194
|
+
description: "View subscriptions"
|
|
8195
|
+
},
|
|
8196
|
+
{
|
|
8197
|
+
key: "subscriptions.view.details",
|
|
8198
|
+
description: "View subscription details"
|
|
8199
|
+
},
|
|
8200
|
+
{
|
|
8201
|
+
key: "subscriptions.edit.details",
|
|
8202
|
+
description: "Edit subscription details"
|
|
8203
|
+
}
|
|
8204
|
+
]
|
|
8205
|
+
},
|
|
8206
|
+
{
|
|
8207
|
+
key: "promo.codes",
|
|
8208
|
+
label: "Promo Codes",
|
|
8209
|
+
permissions: [
|
|
8210
|
+
{
|
|
8211
|
+
key: "promo.codes.view",
|
|
8212
|
+
description: "View promo codes"
|
|
8213
|
+
},
|
|
8214
|
+
{
|
|
8215
|
+
key: "promo.codes.view.details",
|
|
8216
|
+
description: "View promo code details"
|
|
8217
|
+
},
|
|
8218
|
+
{
|
|
8219
|
+
key: "promo.codes.add",
|
|
8220
|
+
description: "Add new promo codes"
|
|
8221
|
+
},
|
|
8222
|
+
{
|
|
8223
|
+
key: "promo.codes.edit.details",
|
|
8224
|
+
description: "Edit promo code details"
|
|
8225
|
+
},
|
|
8226
|
+
{
|
|
8227
|
+
key: "promo.codes.update.status",
|
|
8228
|
+
description: "Update promo code status"
|
|
8229
|
+
},
|
|
8230
|
+
{
|
|
8231
|
+
key: "promo.codes.delete",
|
|
8232
|
+
description: "Delete promo codes"
|
|
8233
|
+
}
|
|
8234
|
+
]
|
|
8235
|
+
},
|
|
8236
|
+
{
|
|
8237
|
+
key: "affiliates",
|
|
8238
|
+
label: "Affiliates",
|
|
8239
|
+
permissions: [
|
|
8240
|
+
{
|
|
8241
|
+
key: "affiliates.view",
|
|
8242
|
+
description: "View affiliates"
|
|
8243
|
+
},
|
|
8244
|
+
{
|
|
8245
|
+
key: "affiliates.view.details",
|
|
8246
|
+
description: "View affiliate details"
|
|
8247
|
+
},
|
|
8248
|
+
{
|
|
8249
|
+
key: "affiliates.edit.status",
|
|
8250
|
+
description: "Edit affiliate status"
|
|
8251
|
+
}
|
|
8252
|
+
]
|
|
8253
|
+
}
|
|
8254
|
+
];
|
|
8255
|
+
for (const module of modules2) {
|
|
8256
|
+
const existingGroup = await _getByKey(module.key, app.code);
|
|
8257
|
+
if (!existingGroup) {
|
|
8258
|
+
await _add({
|
|
8259
|
+
app: app.code,
|
|
8260
|
+
key: module.key,
|
|
8261
|
+
label: module.label
|
|
8262
|
+
});
|
|
8263
|
+
logger25.log({
|
|
8264
|
+
level: "info",
|
|
8265
|
+
message: `Default permission group added: ${app.code} - ${module.key}`
|
|
8266
|
+
});
|
|
8267
|
+
}
|
|
8268
|
+
for (const permission of module.permissions) {
|
|
8269
|
+
const existingPermission = await getPermissionByKey(
|
|
8270
|
+
permission.key,
|
|
8271
|
+
module.key
|
|
8272
|
+
);
|
|
8273
|
+
if (!existingPermission) {
|
|
8274
|
+
await addPermission({
|
|
8275
|
+
app: app.code,
|
|
8276
|
+
group: module.key,
|
|
8277
|
+
key: permission.key,
|
|
8278
|
+
description: permission.description
|
|
8279
|
+
});
|
|
8280
|
+
}
|
|
8281
|
+
}
|
|
8282
|
+
}
|
|
8283
|
+
}
|
|
8284
|
+
}
|
|
8285
|
+
await session.commitTransaction();
|
|
8286
|
+
}
|
|
8287
|
+
logger25.log({
|
|
8288
|
+
level: "info",
|
|
8289
|
+
message: "Default permission groups added successfully."
|
|
8290
|
+
});
|
|
8291
|
+
return;
|
|
8292
|
+
} catch (error) {
|
|
8293
|
+
console.log(error);
|
|
8294
|
+
await session.abortTransaction();
|
|
8295
|
+
throw error;
|
|
8296
|
+
} finally {
|
|
8297
|
+
await session.endSession();
|
|
8298
|
+
}
|
|
8299
|
+
}
|
|
8300
|
+
if (!init4) {
|
|
8301
|
+
addDefaultModule().catch((error) => {
|
|
8302
|
+
logger25.log({
|
|
8303
|
+
level: "error",
|
|
8304
|
+
message: `Error in addDefaultModule: ${error}`
|
|
8305
|
+
});
|
|
8306
|
+
});
|
|
8307
|
+
init4 = true;
|
|
8308
|
+
}
|
|
7754
8309
|
async function deleteById(id) {
|
|
7755
8310
|
const permission = await _getById(id);
|
|
7756
8311
|
if (!permission) {
|
|
@@ -7758,7 +8313,7 @@ function usePermissionGroupService() {
|
|
|
7758
8313
|
}
|
|
7759
8314
|
const associatedPermissionsCount = await countByGroup(permission.key);
|
|
7760
8315
|
if (associatedPermissionsCount > 0) {
|
|
7761
|
-
throw new
|
|
8316
|
+
throw new BadRequestError42(
|
|
7762
8317
|
"Cannot delete Permission Group with associated Permissions."
|
|
7763
8318
|
);
|
|
7764
8319
|
}
|
|
@@ -7766,7 +8321,11 @@ function usePermissionGroupService() {
|
|
|
7766
8321
|
await _deleteById(id);
|
|
7767
8322
|
return "Permission deleted successfully.";
|
|
7768
8323
|
} catch (error) {
|
|
7769
|
-
|
|
8324
|
+
if (error instanceof AppError16) {
|
|
8325
|
+
throw error;
|
|
8326
|
+
} else {
|
|
8327
|
+
throw new InternalServerError22("Failed to delete Permission Group.");
|
|
8328
|
+
}
|
|
7770
8329
|
}
|
|
7771
8330
|
}
|
|
7772
8331
|
return {
|
|
@@ -7775,8 +8334,8 @@ function usePermissionGroupService() {
|
|
|
7775
8334
|
}
|
|
7776
8335
|
|
|
7777
8336
|
// src/resources/permission/permission.group.controller.ts
|
|
7778
|
-
import { BadRequestError as
|
|
7779
|
-
import
|
|
8337
|
+
import { BadRequestError as BadRequestError43, logger as logger26 } from "@goweekdays/utils";
|
|
8338
|
+
import Joi27 from "joi";
|
|
7780
8339
|
function usePermissionGroupController() {
|
|
7781
8340
|
const {
|
|
7782
8341
|
getAll: _getAll,
|
|
@@ -7789,8 +8348,8 @@ function usePermissionGroupController() {
|
|
|
7789
8348
|
const value = req.body;
|
|
7790
8349
|
const { error } = schemaPermissionGroup.validate(value);
|
|
7791
8350
|
if (error) {
|
|
7792
|
-
next(new
|
|
7793
|
-
|
|
8351
|
+
next(new BadRequestError43(error.message));
|
|
8352
|
+
logger26.info(`Controller: ${error.message}`);
|
|
7794
8353
|
return;
|
|
7795
8354
|
}
|
|
7796
8355
|
try {
|
|
@@ -7803,16 +8362,16 @@ function usePermissionGroupController() {
|
|
|
7803
8362
|
}
|
|
7804
8363
|
async function getAll(req, res, next) {
|
|
7805
8364
|
const query = req.query;
|
|
7806
|
-
const validation =
|
|
7807
|
-
page:
|
|
7808
|
-
limit:
|
|
7809
|
-
search:
|
|
7810
|
-
app:
|
|
7811
|
-
status:
|
|
8365
|
+
const validation = Joi27.object({
|
|
8366
|
+
page: Joi27.number().min(1).optional().allow("", null),
|
|
8367
|
+
limit: Joi27.number().min(1).optional().allow("", null),
|
|
8368
|
+
search: Joi27.string().optional().allow("", null),
|
|
8369
|
+
app: Joi27.string().optional().allow("", null),
|
|
8370
|
+
status: Joi27.string().optional().allow("", null)
|
|
7812
8371
|
});
|
|
7813
8372
|
const { error } = validation.validate(query);
|
|
7814
8373
|
if (error) {
|
|
7815
|
-
next(new
|
|
8374
|
+
next(new BadRequestError43(error.message));
|
|
7816
8375
|
return;
|
|
7817
8376
|
}
|
|
7818
8377
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -7846,12 +8405,12 @@ function usePermissionGroupController() {
|
|
|
7846
8405
|
}
|
|
7847
8406
|
async function getById(req, res, next) {
|
|
7848
8407
|
const id = req.params.id;
|
|
7849
|
-
const validation =
|
|
7850
|
-
id:
|
|
8408
|
+
const validation = Joi27.object({
|
|
8409
|
+
id: Joi27.string().hex().required()
|
|
7851
8410
|
});
|
|
7852
8411
|
const { error } = validation.validate({ id });
|
|
7853
8412
|
if (error) {
|
|
7854
|
-
next(new
|
|
8413
|
+
next(new BadRequestError43(error.message));
|
|
7855
8414
|
return;
|
|
7856
8415
|
}
|
|
7857
8416
|
try {
|
|
@@ -7867,12 +8426,12 @@ function usePermissionGroupController() {
|
|
|
7867
8426
|
}
|
|
7868
8427
|
async function deleteById(req, res, next) {
|
|
7869
8428
|
const id = req.params.id;
|
|
7870
|
-
const validation =
|
|
7871
|
-
id:
|
|
8429
|
+
const validation = Joi27.object({
|
|
8430
|
+
id: Joi27.string().hex().required()
|
|
7872
8431
|
});
|
|
7873
8432
|
const { error } = validation.validate({ id });
|
|
7874
8433
|
if (error) {
|
|
7875
|
-
next(new
|
|
8434
|
+
next(new BadRequestError43(error.message));
|
|
7876
8435
|
return;
|
|
7877
8436
|
}
|
|
7878
8437
|
try {
|
|
@@ -7885,15 +8444,15 @@ function usePermissionGroupController() {
|
|
|
7885
8444
|
}
|
|
7886
8445
|
async function updateById(req, res, next) {
|
|
7887
8446
|
const id = req.params.id;
|
|
7888
|
-
const { error: errorId } =
|
|
8447
|
+
const { error: errorId } = Joi27.string().hex().required().validate(id);
|
|
7889
8448
|
if (errorId) {
|
|
7890
|
-
next(new
|
|
8449
|
+
next(new BadRequestError43(errorId.message));
|
|
7891
8450
|
return;
|
|
7892
8451
|
}
|
|
7893
8452
|
const payload = req.body;
|
|
7894
8453
|
const { error } = schemaPermissionGroupUpdate.validate(payload);
|
|
7895
8454
|
if (error) {
|
|
7896
|
-
next(new
|
|
8455
|
+
next(new BadRequestError43(error.message));
|
|
7897
8456
|
return;
|
|
7898
8457
|
}
|
|
7899
8458
|
try {
|