@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/dist/index.js CHANGED
@@ -6557,7 +6557,8 @@ var import_joi19 = __toESM(require("joi"));
6557
6557
  var schemaApp = import_joi19.default.object({
6558
6558
  code: import_joi19.default.string().alphanum().max(20).required(),
6559
6559
  name: import_joi19.default.string().max(255).required(),
6560
- description: import_joi19.default.string().max(1024).optional().allow("", null)
6560
+ description: import_joi19.default.string().max(1024).optional().allow("", null),
6561
+ type: import_joi19.default.string().allow("default", "standard").optional().allow("", null)
6561
6562
  });
6562
6563
  var schemaAppUpdate = import_joi19.default.object({
6563
6564
  code: import_joi19.default.string().alphanum().max(20).optional().allow("", null),
@@ -6574,6 +6575,7 @@ function modelApp(value) {
6574
6575
  code: value.code,
6575
6576
  name: value.name,
6576
6577
  description: value.description,
6578
+ type: value.type ?? "standard",
6577
6579
  status: value.status ?? "active",
6578
6580
  createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
6579
6581
  updatedAt: "",
@@ -6584,6 +6586,8 @@ function modelApp(value) {
6584
6586
  // src/resources/app/app.repository.ts
6585
6587
  var import_utils39 = require("@goweekdays/utils");
6586
6588
  var import_mongodb19 = require("mongodb");
6589
+ var import_joi20 = __toESM(require("joi"));
6590
+ var init = false;
6587
6591
  function useAppRepo() {
6588
6592
  const db = import_utils39.useAtlas.getDb();
6589
6593
  if (!db) {
@@ -6618,9 +6622,12 @@ function useAppRepo() {
6618
6622
  throw new Error("Failed to create index on apps.");
6619
6623
  }
6620
6624
  }
6621
- createIndexes().catch((error) => {
6622
- import_utils39.logger.log({ level: "error", message: `Index creation error: ${error}` });
6623
- });
6625
+ if (!init) {
6626
+ createIndexes().catch((error) => {
6627
+ import_utils39.logger.log({ level: "error", message: `Index creation error: ${error}` });
6628
+ });
6629
+ init = true;
6630
+ }
6624
6631
  async function add(value, session) {
6625
6632
  try {
6626
6633
  value = modelApp(value);
@@ -6674,35 +6681,32 @@ function useAppRepo() {
6674
6681
  page = 1,
6675
6682
  limit = 10,
6676
6683
  sort = {},
6677
- school = "",
6678
- status = "active"
6684
+ status = "active",
6685
+ type = "standard"
6679
6686
  } = {}) {
6680
6687
  page = page > 0 ? page - 1 : 0;
6681
6688
  const query = {
6682
6689
  status
6683
6690
  };
6684
6691
  sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
6685
- if (search) {
6686
- query.$text = { $search: search };
6687
- }
6688
- if (school) {
6689
- try {
6690
- query.school = new import_mongodb19.ObjectId(school);
6691
- } catch (error) {
6692
- throw new import_utils39.BadRequestError("Invalid school ID.");
6693
- }
6694
- }
6695
6692
  const cacheParams = {
6693
+ status,
6696
6694
  page,
6697
6695
  limit,
6698
6696
  sort: JSON.stringify(sort)
6699
6697
  };
6700
- if (search)
6698
+ if (search) {
6699
+ query.$text = { $search: search };
6701
6700
  cacheParams.search = search;
6702
- if (school)
6703
- cacheParams.school = school;
6704
- if (status !== "active")
6705
- cacheParams.status = status;
6701
+ }
6702
+ if (type) {
6703
+ if (Array.isArray(type)) {
6704
+ query.type = { $in: type };
6705
+ } else {
6706
+ query.type = type;
6707
+ }
6708
+ cacheParams.type = type;
6709
+ }
6706
6710
  const cacheKey = (0, import_utils39.makeCacheKey)(namespace_collection, cacheParams);
6707
6711
  import_utils39.logger.log({
6708
6712
  level: "info",
@@ -6781,6 +6785,48 @@ function useAppRepo() {
6781
6785
  }
6782
6786
  }
6783
6787
  }
6788
+ async function getByCode(code) {
6789
+ const validate = import_joi20.default.string().required();
6790
+ const { error } = validate.validate(code);
6791
+ if (error) {
6792
+ throw new import_utils39.BadRequestError("Invalid code.");
6793
+ }
6794
+ const cacheKey = (0, import_utils39.makeCacheKey)(namespace_collection, {
6795
+ code,
6796
+ tag: "byCode"
6797
+ });
6798
+ try {
6799
+ const cached = await getCache(cacheKey);
6800
+ if (cached) {
6801
+ import_utils39.logger.log({
6802
+ level: "info",
6803
+ message: `Cache hit for getByCode app: ${cacheKey}`
6804
+ });
6805
+ return cached;
6806
+ }
6807
+ const result = await collection.findOne({
6808
+ code
6809
+ });
6810
+ setCache(cacheKey, result, 300).then(() => {
6811
+ import_utils39.logger.log({
6812
+ level: "info",
6813
+ message: `Cache set for app by code: ${cacheKey}`
6814
+ });
6815
+ }).catch((err) => {
6816
+ import_utils39.logger.log({
6817
+ level: "error",
6818
+ message: `Failed to set cache for app by code: ${err.message}`
6819
+ });
6820
+ });
6821
+ return result;
6822
+ } catch (error2) {
6823
+ if (error2 instanceof import_utils39.AppError) {
6824
+ throw error2;
6825
+ } else {
6826
+ throw new import_utils39.InternalServerError("Failed to get app.");
6827
+ }
6828
+ }
6829
+ }
6784
6830
  async function deleteById(_id, session) {
6785
6831
  try {
6786
6832
  _id = new import_mongodb19.ObjectId(_id);
@@ -6824,18 +6870,96 @@ function useAppRepo() {
6824
6870
  add,
6825
6871
  getAll,
6826
6872
  getById,
6873
+ getByCode,
6827
6874
  updateById,
6828
6875
  deleteById
6829
6876
  };
6830
6877
  }
6831
6878
 
6832
6879
  // src/resources/app/app.service.ts
6880
+ var import_utils40 = require("@goweekdays/utils");
6881
+ var init2 = false;
6833
6882
  function useAppService() {
6834
6883
  const {
6835
6884
  updateById: _updateById,
6836
6885
  getById: _getById,
6837
- deleteById: _deleteById
6886
+ deleteById: _deleteById,
6887
+ getByCode: _getByCode,
6888
+ add: _add
6838
6889
  } = useAppRepo();
6890
+ async function addDefaultApps() {
6891
+ const apps = [
6892
+ {
6893
+ code: "admin",
6894
+ name: "Admin",
6895
+ description: "Administrative application.",
6896
+ type: "default"
6897
+ },
6898
+ {
6899
+ code: "marketplace",
6900
+ name: "Marketplace",
6901
+ description: "Marketplace for product listings."
6902
+ },
6903
+ {
6904
+ code: "service",
6905
+ name: "Services",
6906
+ description: "Service offerings and management."
6907
+ },
6908
+ {
6909
+ code: "stay",
6910
+ name: "Stay",
6911
+ description: "Accommodation and lodging services."
6912
+ },
6913
+ { code: "eat", name: "Eat", description: "Food and dining services." },
6914
+ {
6915
+ code: "experience",
6916
+ name: "Experience",
6917
+ description: "Experiential touring, travel, activities and events."
6918
+ },
6919
+ {
6920
+ code: "ride",
6921
+ name: "Ride",
6922
+ description: "Transportation and ride services."
6923
+ }
6924
+ ];
6925
+ const session = import_utils40.useAtlas.getClient()?.startSession();
6926
+ if (!session) {
6927
+ throw new Error("Failed to start database session.");
6928
+ }
6929
+ try {
6930
+ session?.startTransaction();
6931
+ for (const app of apps) {
6932
+ const existingApp = await _getByCode(app.code);
6933
+ if (!existingApp) {
6934
+ await _add(app, session);
6935
+ }
6936
+ }
6937
+ await session.commitTransaction();
6938
+ import_utils40.logger.log({
6939
+ level: "info",
6940
+ message: "Default apps added successfully."
6941
+ });
6942
+ return;
6943
+ } catch (error) {
6944
+ await session.abortTransaction();
6945
+ import_utils40.logger.log({
6946
+ level: "error",
6947
+ message: `Failed to add default apps: ${error}`
6948
+ });
6949
+ throw error;
6950
+ } finally {
6951
+ await session.endSession();
6952
+ }
6953
+ }
6954
+ if (!init2) {
6955
+ addDefaultApps().catch((error) => {
6956
+ import_utils40.logger.log({
6957
+ level: "error",
6958
+ message: `Error in addDefaultApps: ${error}`
6959
+ });
6960
+ });
6961
+ init2 = true;
6962
+ }
6839
6963
  async function deleteById(id) {
6840
6964
  try {
6841
6965
  await _deleteById(id);
@@ -6850,8 +6974,8 @@ function useAppService() {
6850
6974
  }
6851
6975
 
6852
6976
  // src/resources/app/app.controller.ts
6853
- var import_utils40 = require("@goweekdays/utils");
6854
- var import_joi20 = __toESM(require("joi"));
6977
+ var import_utils41 = require("@goweekdays/utils");
6978
+ var import_joi21 = __toESM(require("joi"));
6855
6979
  function useAppController() {
6856
6980
  const {
6857
6981
  getAll: _getAll,
@@ -6864,7 +6988,7 @@ function useAppController() {
6864
6988
  const value = req.body;
6865
6989
  const { error } = schemaApp.validate(value);
6866
6990
  if (error) {
6867
- next(new import_utils40.BadRequestError(error.message));
6991
+ next(new import_utils41.BadRequestError(error.message));
6868
6992
  return;
6869
6993
  }
6870
6994
  try {
@@ -6877,15 +7001,15 @@ function useAppController() {
6877
7001
  }
6878
7002
  async function updateById(req, res, next) {
6879
7003
  const id = req.params.id ?? "";
6880
- const { error: errorId } = import_joi20.default.string().hex().required().validate(id);
7004
+ const { error: errorId } = import_joi21.default.string().hex().required().validate(id);
6881
7005
  if (errorId) {
6882
- next(new import_utils40.BadRequestError(errorId.message));
7006
+ next(new import_utils41.BadRequestError(errorId.message));
6883
7007
  return;
6884
7008
  }
6885
7009
  const value = req.body;
6886
7010
  const { error } = schemaAppUpdate.validate(value);
6887
7011
  if (error) {
6888
- next(new import_utils40.BadRequestError(error.message));
7012
+ next(new import_utils41.BadRequestError(error.message));
6889
7013
  return;
6890
7014
  }
6891
7015
  try {
@@ -6898,16 +7022,16 @@ function useAppController() {
6898
7022
  }
6899
7023
  async function getAll(req, res, next) {
6900
7024
  const query = req.query;
6901
- const validation = import_joi20.default.object({
6902
- page: import_joi20.default.number().min(1).optional().allow("", null),
6903
- limit: import_joi20.default.number().min(1).optional().allow("", null),
6904
- search: import_joi20.default.string().optional().allow("", null),
6905
- school: import_joi20.default.string().hex().optional().allow("", null),
6906
- status: import_joi20.default.string().optional().allow("", null)
7025
+ const validation = import_joi21.default.object({
7026
+ page: import_joi21.default.number().min(1).optional().allow("", null),
7027
+ limit: import_joi21.default.number().min(1).optional().allow("", null),
7028
+ search: import_joi21.default.string().optional().allow("", null),
7029
+ status: import_joi21.default.string().optional().allow("", null),
7030
+ type: import_joi21.default.string().optional().allow("", null)
6907
7031
  });
6908
7032
  const { error } = validation.validate(query);
6909
7033
  if (error) {
6910
- next(new import_utils40.BadRequestError(error.message));
7034
+ next(new import_utils41.BadRequestError(error.message));
6911
7035
  return;
6912
7036
  }
6913
7037
  const page = parseInt(req.query.page) ?? 1;
@@ -6922,16 +7046,16 @@ function useAppController() {
6922
7046
  });
6923
7047
  }
6924
7048
  const status = req.query.status ?? "active";
6925
- const school = req.query.school ?? "";
6926
7049
  const search = req.query.search ?? "";
7050
+ let type = req.query.type ? req.query.type.split(",") : "standard";
6927
7051
  try {
6928
7052
  const buildings = await _getAll({
6929
7053
  page,
6930
7054
  limit,
6931
7055
  sort: sortObj,
6932
7056
  status,
6933
- school,
6934
- search
7057
+ search,
7058
+ type
6935
7059
  });
6936
7060
  res.json(buildings);
6937
7061
  return;
@@ -6941,12 +7065,12 @@ function useAppController() {
6941
7065
  }
6942
7066
  async function getById(req, res, next) {
6943
7067
  const id = req.params.id;
6944
- const validation = import_joi20.default.object({
6945
- id: import_joi20.default.string().hex().required()
7068
+ const validation = import_joi21.default.object({
7069
+ id: import_joi21.default.string().hex().required()
6946
7070
  });
6947
7071
  const { error } = validation.validate({ id });
6948
7072
  if (error) {
6949
- next(new import_utils40.BadRequestError(error.message));
7073
+ next(new import_utils41.BadRequestError(error.message));
6950
7074
  return;
6951
7075
  }
6952
7076
  try {
@@ -6962,12 +7086,12 @@ function useAppController() {
6962
7086
  }
6963
7087
  async function deleteById(req, res, next) {
6964
7088
  const id = req.params.id;
6965
- const validation = import_joi20.default.object({
6966
- id: import_joi20.default.string().hex().required()
7089
+ const validation = import_joi21.default.object({
7090
+ id: import_joi21.default.string().hex().required()
6967
7091
  });
6968
7092
  const { error } = validation.validate({ id });
6969
7093
  if (error) {
6970
- next(new import_utils40.BadRequestError(error.message));
7094
+ next(new import_utils41.BadRequestError(error.message));
6971
7095
  return;
6972
7096
  }
6973
7097
  try {
@@ -6988,24 +7112,24 @@ function useAppController() {
6988
7112
  }
6989
7113
 
6990
7114
  // src/resources/permission/permission.model.ts
6991
- var import_utils41 = require("@goweekdays/utils");
6992
- var import_joi21 = __toESM(require("joi"));
6993
- var schemaPermission = import_joi21.default.object({
6994
- app: import_joi21.default.string().required(),
6995
- key: import_joi21.default.string().required(),
6996
- group: import_joi21.default.string().required(),
6997
- description: import_joi21.default.string().max(1024).required(),
6998
- deprecated: import_joi21.default.boolean().optional().allow(null)
7115
+ var import_utils42 = require("@goweekdays/utils");
7116
+ var import_joi22 = __toESM(require("joi"));
7117
+ var schemaPermission = import_joi22.default.object({
7118
+ app: import_joi22.default.string().required(),
7119
+ key: import_joi22.default.string().required(),
7120
+ group: import_joi22.default.string().required(),
7121
+ description: import_joi22.default.string().required(),
7122
+ deprecated: import_joi22.default.boolean().optional().allow(null)
6999
7123
  });
7000
- var schemaPermissionUpdate = import_joi21.default.object({
7001
- key: import_joi21.default.string().optional().allow("", null),
7002
- group: import_joi21.default.string().optional().allow("", null),
7003
- description: import_joi21.default.string().max(1024).optional().allow("", null)
7124
+ var schemaPermissionUpdate = import_joi22.default.object({
7125
+ key: import_joi22.default.string().optional().allow("", null),
7126
+ group: import_joi22.default.string().optional().allow("", null),
7127
+ description: import_joi22.default.string().max(1024).optional().allow("", null)
7004
7128
  });
7005
7129
  function modelPermission(value) {
7006
7130
  const { error } = schemaPermission.validate(value);
7007
7131
  if (error) {
7008
- throw new import_utils41.BadRequestError(error.message);
7132
+ throw new import_utils42.BadRequestError(error.message);
7009
7133
  }
7010
7134
  return {
7011
7135
  _id: value._id,
@@ -7022,16 +7146,17 @@ function modelPermission(value) {
7022
7146
  }
7023
7147
 
7024
7148
  // src/resources/permission/permission.repository.ts
7025
- var import_utils42 = require("@goweekdays/utils");
7149
+ var import_utils43 = require("@goweekdays/utils");
7026
7150
  var import_mongodb20 = require("mongodb");
7151
+ var import_joi23 = __toESM(require("joi"));
7027
7152
  function usePermissionRepo() {
7028
- const db = import_utils42.useAtlas.getDb();
7153
+ const db = import_utils43.useAtlas.getDb();
7029
7154
  if (!db) {
7030
7155
  throw new Error("Unable to connect to server.");
7031
7156
  }
7032
7157
  const namespace_collection = "permissions";
7033
7158
  const collection = db.collection(namespace_collection);
7034
- const { getCache, setCache, delNamespace } = (0, import_utils42.useCache)(namespace_collection);
7159
+ const { getCache, setCache, delNamespace } = (0, import_utils43.useCache)(namespace_collection);
7035
7160
  async function createIndexes() {
7036
7161
  try {
7037
7162
  await collection.createIndexes([
@@ -7053,7 +7178,7 @@ function usePermissionRepo() {
7053
7178
  }
7054
7179
  }
7055
7180
  createIndexes().catch((error) => {
7056
- import_utils42.logger.log({ level: "error", message: `Index creation error: ${error}` });
7181
+ import_utils43.logger.log({ level: "error", message: `Index creation error: ${error}` });
7057
7182
  });
7058
7183
  async function add(value, session) {
7059
7184
  try {
@@ -7062,16 +7187,16 @@ function usePermissionRepo() {
7062
7187
  delCachedData();
7063
7188
  return res.insertedId;
7064
7189
  } catch (error) {
7065
- import_utils42.logger.log({
7190
+ import_utils43.logger.log({
7066
7191
  level: "error",
7067
7192
  message: error.message
7068
7193
  });
7069
- if (error instanceof import_utils42.AppError) {
7194
+ if (error instanceof import_utils43.AppError) {
7070
7195
  throw error;
7071
7196
  } else {
7072
7197
  const isDuplicated = error.message.includes("duplicate");
7073
7198
  if (isDuplicated) {
7074
- throw new import_utils42.BadRequestError("Permission already exists.");
7199
+ throw new import_utils43.BadRequestError("Permission already exists.");
7075
7200
  }
7076
7201
  throw new Error("Failed to create permission.");
7077
7202
  }
@@ -7081,11 +7206,11 @@ function usePermissionRepo() {
7081
7206
  try {
7082
7207
  _id = new import_mongodb20.ObjectId(_id);
7083
7208
  } catch (error2) {
7084
- throw new import_utils42.BadRequestError("Invalid ID.");
7209
+ throw new import_utils43.BadRequestError("Invalid ID.");
7085
7210
  }
7086
7211
  const { error } = schemaPermissionUpdate.validate(value);
7087
7212
  if (error) {
7088
- throw new import_utils42.BadRequestError(`Invalid data: ${error.message}`);
7213
+ throw new import_utils43.BadRequestError(`Invalid data: ${error.message}`);
7089
7214
  }
7090
7215
  try {
7091
7216
  const res = await collection.updateOne(
@@ -7096,11 +7221,11 @@ function usePermissionRepo() {
7096
7221
  delCachedData();
7097
7222
  return res;
7098
7223
  } catch (error2) {
7099
- import_utils42.logger.log({
7224
+ import_utils43.logger.log({
7100
7225
  level: "error",
7101
7226
  message: error2.message
7102
7227
  });
7103
- if (error2 instanceof import_utils42.AppError) {
7228
+ if (error2 instanceof import_utils43.AppError) {
7104
7229
  throw error2;
7105
7230
  } else {
7106
7231
  throw new Error("Failed to update permission.");
@@ -7133,15 +7258,15 @@ function usePermissionRepo() {
7133
7258
  query.app = app;
7134
7259
  cacheParams.app = app;
7135
7260
  }
7136
- const cacheKey = (0, import_utils42.makeCacheKey)(namespace_collection, cacheParams);
7137
- import_utils42.logger.log({
7261
+ const cacheKey = (0, import_utils43.makeCacheKey)(namespace_collection, cacheParams);
7262
+ import_utils43.logger.log({
7138
7263
  level: "info",
7139
7264
  message: `Cache key for getAll permissions: ${cacheKey}`
7140
7265
  });
7141
7266
  try {
7142
7267
  const cached = await getCache(cacheKey);
7143
7268
  if (cached) {
7144
- import_utils42.logger.log({
7269
+ import_utils43.logger.log({
7145
7270
  level: "info",
7146
7271
  message: `Cache hit for getAll permissions: ${cacheKey}`
7147
7272
  });
@@ -7154,21 +7279,21 @@ function usePermissionRepo() {
7154
7279
  { $limit: limit }
7155
7280
  ]).toArray();
7156
7281
  const length = await collection.countDocuments(query);
7157
- const data = (0, import_utils42.paginate)(items, page, limit, length);
7282
+ const data = (0, import_utils43.paginate)(items, page, limit, length);
7158
7283
  setCache(cacheKey, data, 600).then(() => {
7159
- import_utils42.logger.log({
7284
+ import_utils43.logger.log({
7160
7285
  level: "info",
7161
7286
  message: `Cache set for getAll permissions: ${cacheKey}`
7162
7287
  });
7163
7288
  }).catch((err) => {
7164
- import_utils42.logger.log({
7289
+ import_utils43.logger.log({
7165
7290
  level: "error",
7166
7291
  message: `Failed to set cache for getAll permissions: ${err.message}`
7167
7292
  });
7168
7293
  });
7169
7294
  return data;
7170
7295
  } catch (error) {
7171
- import_utils42.logger.log({ level: "error", message: `${error}` });
7296
+ import_utils43.logger.log({ level: "error", message: `${error}` });
7172
7297
  throw error;
7173
7298
  }
7174
7299
  }
@@ -7176,13 +7301,13 @@ function usePermissionRepo() {
7176
7301
  try {
7177
7302
  _id = new import_mongodb20.ObjectId(_id);
7178
7303
  } catch (error) {
7179
- throw new import_utils42.BadRequestError("Invalid ID.");
7304
+ throw new import_utils43.BadRequestError("Invalid ID.");
7180
7305
  }
7181
- const cacheKey = (0, import_utils42.makeCacheKey)(namespace_collection, { _id: String(_id) });
7306
+ const cacheKey = (0, import_utils43.makeCacheKey)(namespace_collection, { _id: String(_id) });
7182
7307
  try {
7183
7308
  const cached = await getCache(cacheKey);
7184
7309
  if (cached) {
7185
- import_utils42.logger.log({
7310
+ import_utils43.logger.log({
7186
7311
  level: "info",
7187
7312
  message: `Cache hit for getById permission: ${cacheKey}`
7188
7313
  });
@@ -7192,59 +7317,107 @@ function usePermissionRepo() {
7192
7317
  _id
7193
7318
  });
7194
7319
  setCache(cacheKey, result, 300).then(() => {
7195
- import_utils42.logger.log({
7320
+ import_utils43.logger.log({
7196
7321
  level: "info",
7197
7322
  message: `Cache set for permission by id: ${cacheKey}`
7198
7323
  });
7199
7324
  }).catch((err) => {
7200
- import_utils42.logger.log({
7325
+ import_utils43.logger.log({
7201
7326
  level: "error",
7202
7327
  message: `Failed to set cache for permission by id: ${err.message}`
7203
7328
  });
7204
7329
  });
7205
7330
  return result;
7206
7331
  } catch (error) {
7207
- if (error instanceof import_utils42.AppError) {
7332
+ if (error instanceof import_utils43.AppError) {
7208
7333
  throw error;
7209
7334
  } else {
7210
- throw new import_utils42.InternalServerError("Failed to get permission.");
7335
+ throw new import_utils43.InternalServerError("Failed to get permission.");
7336
+ }
7337
+ }
7338
+ }
7339
+ async function getByKey(key, group) {
7340
+ const validation = import_joi23.default.object({
7341
+ key: import_joi23.default.string().required(),
7342
+ group: import_joi23.default.string().optional().allow("", null)
7343
+ });
7344
+ const { error } = validation.validate({ key, group });
7345
+ if (error) {
7346
+ throw new import_utils43.BadRequestError(`Invalid data: ${error.message}`);
7347
+ }
7348
+ const query = {};
7349
+ const cacheKeyOptions = {};
7350
+ query.key = key;
7351
+ cacheKeyOptions.key = key;
7352
+ if (group) {
7353
+ query.group = group;
7354
+ cacheKeyOptions.group = group;
7355
+ }
7356
+ const cacheKey = (0, import_utils43.makeCacheKey)(namespace_collection, cacheKeyOptions);
7357
+ try {
7358
+ const cached = await getCache(cacheKey);
7359
+ if (cached) {
7360
+ import_utils43.logger.log({
7361
+ level: "info",
7362
+ message: `Cache hit for getById permission: ${cacheKey}`
7363
+ });
7364
+ return cached;
7365
+ }
7366
+ const result = await collection.findOne(query);
7367
+ setCache(cacheKey, result, 300).then(() => {
7368
+ import_utils43.logger.log({
7369
+ level: "info",
7370
+ message: `Cache set for permission by key: ${cacheKey}`
7371
+ });
7372
+ }).catch((err) => {
7373
+ import_utils43.logger.log({
7374
+ level: "error",
7375
+ message: `Failed to set cache for permission by key: ${err.message}`
7376
+ });
7377
+ });
7378
+ return result;
7379
+ } catch (error2) {
7380
+ if (error2 instanceof import_utils43.AppError) {
7381
+ throw error2;
7382
+ } else {
7383
+ throw new import_utils43.InternalServerError("Failed to get permission.");
7211
7384
  }
7212
7385
  }
7213
7386
  }
7214
7387
  async function countByGroup(group) {
7215
- const cacheKey = (0, import_utils42.makeCacheKey)(namespace_collection, {
7388
+ const cacheKey = (0, import_utils43.makeCacheKey)(namespace_collection, {
7216
7389
  group,
7217
7390
  tag: "countByGroup"
7218
7391
  });
7219
7392
  try {
7220
7393
  const cached = await getCache(cacheKey);
7221
7394
  if (cached) {
7222
- import_utils42.logger.log({
7395
+ import_utils43.logger.log({
7223
7396
  level: "info",
7224
7397
  message: `Cache hit for getById permission: ${cacheKey}`
7225
7398
  });
7226
7399
  return cached;
7227
7400
  }
7228
7401
  const result = await collection.countDocuments({
7229
- key: group
7402
+ group
7230
7403
  });
7231
7404
  setCache(cacheKey, result, 300).then(() => {
7232
- import_utils42.logger.log({
7405
+ import_utils43.logger.log({
7233
7406
  level: "info",
7234
7407
  message: `Cache set for permission count by group: ${cacheKey}`
7235
7408
  });
7236
7409
  }).catch((err) => {
7237
- import_utils42.logger.log({
7410
+ import_utils43.logger.log({
7238
7411
  level: "error",
7239
7412
  message: `Failed to set cache for permission by group: ${err.message}`
7240
7413
  });
7241
7414
  });
7242
7415
  return result;
7243
7416
  } catch (error) {
7244
- if (error instanceof import_utils42.AppError) {
7417
+ if (error instanceof import_utils43.AppError) {
7245
7418
  throw error;
7246
7419
  } else {
7247
- throw new import_utils42.InternalServerError("Failed to count permission by group.");
7420
+ throw new import_utils43.InternalServerError("Failed to count permission by group.");
7248
7421
  }
7249
7422
  }
7250
7423
  }
@@ -7252,7 +7425,7 @@ function usePermissionRepo() {
7252
7425
  try {
7253
7426
  _id = new import_mongodb20.ObjectId(_id);
7254
7427
  } catch (error) {
7255
- throw new import_utils42.BadRequestError("Invalid ID.");
7428
+ throw new import_utils43.BadRequestError("Invalid ID.");
7256
7429
  }
7257
7430
  try {
7258
7431
  const res = await collection.updateOne(
@@ -7262,25 +7435,25 @@ function usePermissionRepo() {
7262
7435
  delCachedData();
7263
7436
  return res;
7264
7437
  } catch (error) {
7265
- import_utils42.logger.log({
7438
+ import_utils43.logger.log({
7266
7439
  level: "error",
7267
7440
  message: error.message
7268
7441
  });
7269
- if (error instanceof import_utils42.AppError) {
7442
+ if (error instanceof import_utils43.AppError) {
7270
7443
  throw error;
7271
7444
  } else {
7272
- throw new import_utils42.InternalServerError("Failed to delete permission.");
7445
+ throw new import_utils43.InternalServerError("Failed to delete permission.");
7273
7446
  }
7274
7447
  }
7275
7448
  }
7276
7449
  function delCachedData() {
7277
7450
  delNamespace().then(() => {
7278
- import_utils42.logger.log({
7451
+ import_utils43.logger.log({
7279
7452
  level: "info",
7280
7453
  message: `Cache namespace cleared for ${namespace_collection}`
7281
7454
  });
7282
7455
  }).catch((err) => {
7283
- import_utils42.logger.log({
7456
+ import_utils43.logger.log({
7284
7457
  level: "error",
7285
7458
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
7286
7459
  });
@@ -7291,6 +7464,7 @@ function usePermissionRepo() {
7291
7464
  add,
7292
7465
  getAll,
7293
7466
  getById,
7467
+ getByKey,
7294
7468
  updateById,
7295
7469
  deleteById,
7296
7470
  countByGroup
@@ -7318,8 +7492,8 @@ function usePermissionService() {
7318
7492
  }
7319
7493
 
7320
7494
  // src/resources/permission/permission.controller.ts
7321
- var import_utils43 = require("@goweekdays/utils");
7322
- var import_joi22 = __toESM(require("joi"));
7495
+ var import_utils44 = require("@goweekdays/utils");
7496
+ var import_joi24 = __toESM(require("joi"));
7323
7497
  function usePermissionController() {
7324
7498
  const {
7325
7499
  getAll: _getAll,
@@ -7332,8 +7506,8 @@ function usePermissionController() {
7332
7506
  const value = req.body;
7333
7507
  const { error } = schemaPermission.validate(value);
7334
7508
  if (error) {
7335
- next(new import_utils43.BadRequestError(error.message));
7336
- import_utils43.logger.info(`Controller: ${error.message}`);
7509
+ next(new import_utils44.BadRequestError(error.message));
7510
+ import_utils44.logger.info(`Controller: ${error.message}`);
7337
7511
  return;
7338
7512
  }
7339
7513
  try {
@@ -7346,16 +7520,16 @@ function usePermissionController() {
7346
7520
  }
7347
7521
  async function getAll(req, res, next) {
7348
7522
  const query = req.query;
7349
- const validation = import_joi22.default.object({
7350
- page: import_joi22.default.number().min(1).optional().allow("", null),
7351
- limit: import_joi22.default.number().min(1).optional().allow("", null),
7352
- search: import_joi22.default.string().optional().allow("", null),
7353
- app: import_joi22.default.string().optional().allow("", null),
7354
- status: import_joi22.default.string().optional().allow("", null)
7523
+ const validation = import_joi24.default.object({
7524
+ page: import_joi24.default.number().min(1).optional().allow("", null),
7525
+ limit: import_joi24.default.number().min(1).optional().allow("", null),
7526
+ search: import_joi24.default.string().optional().allow("", null),
7527
+ app: import_joi24.default.string().optional().allow("", null),
7528
+ status: import_joi24.default.string().optional().allow("", null)
7355
7529
  });
7356
7530
  const { error } = validation.validate(query);
7357
7531
  if (error) {
7358
- next(new import_utils43.BadRequestError(error.message));
7532
+ next(new import_utils44.BadRequestError(error.message));
7359
7533
  return;
7360
7534
  }
7361
7535
  const page = parseInt(req.query.page) ?? 1;
@@ -7389,12 +7563,12 @@ function usePermissionController() {
7389
7563
  }
7390
7564
  async function getById(req, res, next) {
7391
7565
  const id = req.params.id;
7392
- const validation = import_joi22.default.object({
7393
- id: import_joi22.default.string().hex().required()
7566
+ const validation = import_joi24.default.object({
7567
+ id: import_joi24.default.string().hex().required()
7394
7568
  });
7395
7569
  const { error } = validation.validate({ id });
7396
7570
  if (error) {
7397
- next(new import_utils43.BadRequestError(error.message));
7571
+ next(new import_utils44.BadRequestError(error.message));
7398
7572
  return;
7399
7573
  }
7400
7574
  try {
@@ -7410,12 +7584,12 @@ function usePermissionController() {
7410
7584
  }
7411
7585
  async function deleteById(req, res, next) {
7412
7586
  const id = req.params.id;
7413
- const validation = import_joi22.default.object({
7414
- id: import_joi22.default.string().hex().required()
7587
+ const validation = import_joi24.default.object({
7588
+ id: import_joi24.default.string().hex().required()
7415
7589
  });
7416
7590
  const { error } = validation.validate({ id });
7417
7591
  if (error) {
7418
- next(new import_utils43.BadRequestError(error.message));
7592
+ next(new import_utils44.BadRequestError(error.message));
7419
7593
  return;
7420
7594
  }
7421
7595
  try {
@@ -7428,15 +7602,15 @@ function usePermissionController() {
7428
7602
  }
7429
7603
  async function updateById(req, res, next) {
7430
7604
  const id = req.params.id;
7431
- const { error: errorId } = import_joi22.default.string().hex().required().validate(id);
7605
+ const { error: errorId } = import_joi24.default.string().hex().required().validate(id);
7432
7606
  if (errorId) {
7433
- next(new import_utils43.BadRequestError(errorId.message));
7607
+ next(new import_utils44.BadRequestError(errorId.message));
7434
7608
  return;
7435
7609
  }
7436
7610
  const payload = req.body;
7437
7611
  const { error } = schemaPermissionUpdate.validate(payload);
7438
7612
  if (error) {
7439
- next(new import_utils43.BadRequestError(error.message));
7613
+ next(new import_utils44.BadRequestError(error.message));
7440
7614
  return;
7441
7615
  }
7442
7616
  try {
@@ -7457,23 +7631,23 @@ function usePermissionController() {
7457
7631
  }
7458
7632
 
7459
7633
  // src/resources/permission/permission.group.model.ts
7460
- var import_utils44 = require("@goweekdays/utils");
7461
- var import_joi23 = __toESM(require("joi"));
7462
- var schemaPermissionGroup = import_joi23.default.object({
7463
- app: import_joi23.default.string().required(),
7464
- key: import_joi23.default.string().required(),
7465
- label: import_joi23.default.string().required(),
7466
- order: import_joi23.default.number().integer().optional().allow("", null)
7634
+ var import_utils45 = require("@goweekdays/utils");
7635
+ var import_joi25 = __toESM(require("joi"));
7636
+ var schemaPermissionGroup = import_joi25.default.object({
7637
+ app: import_joi25.default.string().required(),
7638
+ key: import_joi25.default.string().required(),
7639
+ label: import_joi25.default.string().required(),
7640
+ order: import_joi25.default.number().integer().optional().allow("", null)
7467
7641
  });
7468
- var schemaPermissionGroupUpdate = import_joi23.default.object({
7469
- key: import_joi23.default.string().optional().allow("", null),
7470
- label: import_joi23.default.string().optional().allow("", null),
7471
- order: import_joi23.default.number().integer().optional().allow("", null)
7642
+ var schemaPermissionGroupUpdate = import_joi25.default.object({
7643
+ key: import_joi25.default.string().optional().allow("", null),
7644
+ label: import_joi25.default.string().optional().allow("", null),
7645
+ order: import_joi25.default.number().integer().optional().allow("", null)
7472
7646
  });
7473
7647
  function modelPermissionGroup(value) {
7474
7648
  const { error } = schemaPermissionGroup.validate(value);
7475
7649
  if (error) {
7476
- throw new import_utils44.BadRequestError(error.message);
7650
+ throw new import_utils45.BadRequestError(error.message);
7477
7651
  }
7478
7652
  return {
7479
7653
  _id: value._id,
@@ -7489,16 +7663,18 @@ function modelPermissionGroup(value) {
7489
7663
  }
7490
7664
 
7491
7665
  // src/resources/permission/permission.group.repository.ts
7492
- var import_utils45 = require("@goweekdays/utils");
7666
+ var import_utils46 = require("@goweekdays/utils");
7493
7667
  var import_mongodb21 = require("mongodb");
7668
+ var import_joi26 = __toESM(require("joi"));
7669
+ var init3 = false;
7494
7670
  function usePermissionGroupRepo() {
7495
- const db = import_utils45.useAtlas.getDb();
7671
+ const db = import_utils46.useAtlas.getDb();
7496
7672
  if (!db) {
7497
7673
  throw new Error("Unable to connect to server.");
7498
7674
  }
7499
7675
  const namespace_collection = "permission.groups";
7500
7676
  const collection = db.collection(namespace_collection);
7501
- const { getCache, setCache, delNamespace } = (0, import_utils45.useCache)(namespace_collection);
7677
+ const { getCache, setCache, delNamespace } = (0, import_utils46.useCache)(namespace_collection);
7502
7678
  async function createIndexes() {
7503
7679
  try {
7504
7680
  await collection.createIndexes([
@@ -7519,26 +7695,33 @@ function usePermissionGroupRepo() {
7519
7695
  throw new Error("Failed to create index on permission groups.");
7520
7696
  }
7521
7697
  }
7522
- createIndexes().catch((error) => {
7523
- import_utils45.logger.log({ level: "error", message: `Index creation error: ${error}` });
7524
- });
7698
+ if (!init3) {
7699
+ createIndexes().catch((error) => {
7700
+ import_utils46.logger.log({
7701
+ level: "error",
7702
+ message: `Index creation error: ${error}`
7703
+ });
7704
+ });
7705
+ init3 = true;
7706
+ }
7525
7707
  async function add(value, session) {
7526
7708
  try {
7527
7709
  value = modelPermissionGroup(value);
7710
+ console.log(value);
7528
7711
  const res = await collection.insertOne(value, { session });
7529
7712
  delCachedData();
7530
7713
  return res.insertedId;
7531
7714
  } catch (error) {
7532
- import_utils45.logger.log({
7715
+ import_utils46.logger.log({
7533
7716
  level: "error",
7534
7717
  message: error.message
7535
7718
  });
7536
- if (error instanceof import_utils45.AppError) {
7719
+ if (error instanceof import_utils46.AppError) {
7537
7720
  throw error;
7538
7721
  } else {
7539
7722
  const isDuplicated = error.message.includes("duplicate");
7540
7723
  if (isDuplicated) {
7541
- throw new import_utils45.BadRequestError("App already exists.");
7724
+ throw new import_utils46.BadRequestError("Permission group already exists.");
7542
7725
  }
7543
7726
  throw new Error("Failed to create permission group.");
7544
7727
  }
@@ -7548,11 +7731,11 @@ function usePermissionGroupRepo() {
7548
7731
  try {
7549
7732
  _id = new import_mongodb21.ObjectId(_id);
7550
7733
  } catch (error2) {
7551
- throw new import_utils45.BadRequestError("Invalid ID.");
7734
+ throw new import_utils46.BadRequestError("Invalid ID.");
7552
7735
  }
7553
7736
  const { error } = schemaPermissionGroupUpdate.validate(value);
7554
7737
  if (error) {
7555
- throw new import_utils45.BadRequestError(`Invalid data: ${error.message}`);
7738
+ throw new import_utils46.BadRequestError(`Invalid data: ${error.message}`);
7556
7739
  }
7557
7740
  try {
7558
7741
  const res = await collection.updateOne(
@@ -7563,11 +7746,11 @@ function usePermissionGroupRepo() {
7563
7746
  delCachedData();
7564
7747
  return res;
7565
7748
  } catch (error2) {
7566
- import_utils45.logger.log({
7749
+ import_utils46.logger.log({
7567
7750
  level: "error",
7568
7751
  message: error2.message
7569
7752
  });
7570
- if (error2 instanceof import_utils45.AppError) {
7753
+ if (error2 instanceof import_utils46.AppError) {
7571
7754
  throw error2;
7572
7755
  } else {
7573
7756
  throw new Error("Failed to update permission group.");
@@ -7600,15 +7783,15 @@ function usePermissionGroupRepo() {
7600
7783
  query.app = app;
7601
7784
  cacheParams.app = app;
7602
7785
  }
7603
- const cacheKey = (0, import_utils45.makeCacheKey)(namespace_collection, cacheParams);
7604
- import_utils45.logger.log({
7786
+ const cacheKey = (0, import_utils46.makeCacheKey)(namespace_collection, cacheParams);
7787
+ import_utils46.logger.log({
7605
7788
  level: "info",
7606
7789
  message: `Cache key for getAll permission groups: ${cacheKey}`
7607
7790
  });
7608
7791
  try {
7609
7792
  const cached = await getCache(cacheKey);
7610
7793
  if (cached) {
7611
- import_utils45.logger.log({
7794
+ import_utils46.logger.log({
7612
7795
  level: "info",
7613
7796
  message: `Cache hit for getAll permission groups: ${cacheKey}`
7614
7797
  });
@@ -7621,21 +7804,21 @@ function usePermissionGroupRepo() {
7621
7804
  { $limit: limit }
7622
7805
  ]).toArray();
7623
7806
  const length = await collection.countDocuments(query);
7624
- const data = (0, import_utils45.paginate)(items, page, limit, length);
7807
+ const data = (0, import_utils46.paginate)(items, page, limit, length);
7625
7808
  setCache(cacheKey, data, 600).then(() => {
7626
- import_utils45.logger.log({
7809
+ import_utils46.logger.log({
7627
7810
  level: "info",
7628
7811
  message: `Cache set for getAll permission groups: ${cacheKey}`
7629
7812
  });
7630
7813
  }).catch((err) => {
7631
- import_utils45.logger.log({
7814
+ import_utils46.logger.log({
7632
7815
  level: "error",
7633
7816
  message: `Failed to set cache for getAll permission groups: ${err.message}`
7634
7817
  });
7635
7818
  });
7636
7819
  return data;
7637
7820
  } catch (error) {
7638
- import_utils45.logger.log({ level: "error", message: `${error}` });
7821
+ import_utils46.logger.log({ level: "error", message: `${error}` });
7639
7822
  throw error;
7640
7823
  }
7641
7824
  }
@@ -7643,13 +7826,13 @@ function usePermissionGroupRepo() {
7643
7826
  try {
7644
7827
  _id = new import_mongodb21.ObjectId(_id);
7645
7828
  } catch (error) {
7646
- throw new import_utils45.BadRequestError("Invalid ID.");
7829
+ throw new import_utils46.BadRequestError("Invalid ID.");
7647
7830
  }
7648
- const cacheKey = (0, import_utils45.makeCacheKey)(namespace_collection, { _id: String(_id) });
7831
+ const cacheKey = (0, import_utils46.makeCacheKey)(namespace_collection, { _id: String(_id) });
7649
7832
  try {
7650
7833
  const cached = await getCache(cacheKey);
7651
7834
  if (cached) {
7652
- import_utils45.logger.log({
7835
+ import_utils46.logger.log({
7653
7836
  level: "info",
7654
7837
  message: `Cache hit for getById permission group: ${cacheKey}`
7655
7838
  });
@@ -7659,22 +7842,68 @@ function usePermissionGroupRepo() {
7659
7842
  _id
7660
7843
  });
7661
7844
  setCache(cacheKey, result, 300).then(() => {
7662
- import_utils45.logger.log({
7845
+ import_utils46.logger.log({
7663
7846
  level: "info",
7664
7847
  message: `Cache set for permission group by id: ${cacheKey}`
7665
7848
  });
7666
7849
  }).catch((err) => {
7667
- import_utils45.logger.log({
7850
+ import_utils46.logger.log({
7668
7851
  level: "error",
7669
7852
  message: `Failed to set cache for permission group by id: ${err.message}`
7670
7853
  });
7671
7854
  });
7672
7855
  return result;
7673
7856
  } catch (error) {
7674
- if (error instanceof import_utils45.AppError) {
7857
+ if (error instanceof import_utils46.AppError) {
7675
7858
  throw error;
7676
7859
  } else {
7677
- throw new import_utils45.InternalServerError("Failed to get permission group.");
7860
+ throw new import_utils46.InternalServerError("Failed to get permission group.");
7861
+ }
7862
+ }
7863
+ }
7864
+ async function getByKey(key, app) {
7865
+ const validation = import_joi26.default.object({
7866
+ key: import_joi26.default.string().required(),
7867
+ app: import_joi26.default.string().optional().allow(null, "")
7868
+ });
7869
+ const { error } = validation.validate({ key, app });
7870
+ if (error) {
7871
+ throw new import_utils46.BadRequestError("Invalid key.");
7872
+ }
7873
+ const query = { key };
7874
+ const cacheKeyOptions = { key, tag: "byKey" };
7875
+ if (app) {
7876
+ query.app = app;
7877
+ cacheKeyOptions.app = app;
7878
+ }
7879
+ const cacheKey = (0, import_utils46.makeCacheKey)(namespace_collection, cacheKeyOptions);
7880
+ try {
7881
+ const cached = await getCache(cacheKey);
7882
+ if (cached) {
7883
+ import_utils46.logger.log({
7884
+ level: "info",
7885
+ message: `Cache hit for getById permission group: ${cacheKey}`
7886
+ });
7887
+ return cached;
7888
+ }
7889
+ const result = await collection.findOne(query);
7890
+ setCache(cacheKey, result, 300).then(() => {
7891
+ import_utils46.logger.log({
7892
+ level: "info",
7893
+ message: `Cache set for permission group by key: ${cacheKey}`
7894
+ });
7895
+ }).catch((err) => {
7896
+ import_utils46.logger.log({
7897
+ level: "error",
7898
+ message: `Failed to set cache for permission group by key: ${err.message}`
7899
+ });
7900
+ });
7901
+ return result;
7902
+ } catch (error2) {
7903
+ if (error2 instanceof import_utils46.AppError) {
7904
+ throw error2;
7905
+ } else {
7906
+ throw new import_utils46.InternalServerError("Failed to get permission group.");
7678
7907
  }
7679
7908
  }
7680
7909
  }
@@ -7682,7 +7911,7 @@ function usePermissionGroupRepo() {
7682
7911
  try {
7683
7912
  _id = new import_mongodb21.ObjectId(_id);
7684
7913
  } catch (error) {
7685
- throw new import_utils45.BadRequestError("Invalid ID.");
7914
+ throw new import_utils46.BadRequestError("Invalid ID.");
7686
7915
  }
7687
7916
  try {
7688
7917
  const res = await collection.updateOne(
@@ -7692,25 +7921,25 @@ function usePermissionGroupRepo() {
7692
7921
  delCachedData();
7693
7922
  return res;
7694
7923
  } catch (error) {
7695
- import_utils45.logger.log({
7924
+ import_utils46.logger.log({
7696
7925
  level: "error",
7697
7926
  message: error.message
7698
7927
  });
7699
- if (error instanceof import_utils45.AppError) {
7928
+ if (error instanceof import_utils46.AppError) {
7700
7929
  throw error;
7701
7930
  } else {
7702
- throw new import_utils45.InternalServerError("Failed to delete permission group.");
7931
+ throw new import_utils46.InternalServerError("Failed to delete permission group.");
7703
7932
  }
7704
7933
  }
7705
7934
  }
7706
7935
  function delCachedData() {
7707
7936
  delNamespace().then(() => {
7708
- import_utils45.logger.log({
7937
+ import_utils46.logger.log({
7709
7938
  level: "info",
7710
7939
  message: `Cache namespace cleared for ${namespace_collection}`
7711
7940
  });
7712
7941
  }).catch((err) => {
7713
- import_utils45.logger.log({
7942
+ import_utils46.logger.log({
7714
7943
  level: "error",
7715
7944
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
7716
7945
  });
@@ -7721,19 +7950,339 @@ function usePermissionGroupRepo() {
7721
7950
  add,
7722
7951
  getAll,
7723
7952
  getById,
7953
+ getByKey,
7724
7954
  updateById,
7725
7955
  deleteById
7726
7956
  };
7727
7957
  }
7728
7958
 
7729
7959
  // src/resources/permission/permission.group.service.ts
7960
+ var import_utils47 = require("@goweekdays/utils");
7961
+ var init4 = false;
7730
7962
  function usePermissionGroupService() {
7731
7963
  const {
7732
7964
  updateById: _updateById,
7733
7965
  getById: _getById,
7734
- deleteById: _deleteById
7966
+ deleteById: _deleteById,
7967
+ getByKey: _getByKey,
7968
+ add: _add
7735
7969
  } = usePermissionGroupRepo();
7736
- const { countByGroup } = usePermissionRepo();
7970
+ const { getAll: getAllApps } = useAppRepo();
7971
+ const {
7972
+ countByGroup,
7973
+ getByKey: getPermissionByKey,
7974
+ add: addPermission
7975
+ } = usePermissionRepo();
7976
+ async function addDefaultModule() {
7977
+ const session = import_utils47.useAtlas.getClient()?.startSession();
7978
+ if (!session) {
7979
+ throw new Error("Failed to start database session.");
7980
+ }
7981
+ try {
7982
+ session?.startTransaction();
7983
+ const apps = await getAllApps({ limit: 20 });
7984
+ if (apps && apps.items && apps.items.length) {
7985
+ const modules = [
7986
+ {
7987
+ key: "members",
7988
+ label: "Members",
7989
+ permissions: [
7990
+ {
7991
+ key: "members.view",
7992
+ description: "View members"
7993
+ },
7994
+ {
7995
+ key: "members.view.details",
7996
+ description: "View member details"
7997
+ },
7998
+ {
7999
+ key: "members.add",
8000
+ description: "Add new members"
8001
+ },
8002
+ {
8003
+ key: "members.edit.details",
8004
+ description: "Edit member details"
8005
+ },
8006
+ {
8007
+ key: "members.delete",
8008
+ description: "Delete members"
8009
+ }
8010
+ ]
8011
+ },
8012
+ {
8013
+ key: "roles",
8014
+ label: "Roles",
8015
+ permissions: [
8016
+ {
8017
+ key: "roles.view",
8018
+ description: "View roles"
8019
+ },
8020
+ { key: "roles.view.details", description: "View role details" },
8021
+ {
8022
+ key: "roles.add",
8023
+ description: "Add new roles"
8024
+ },
8025
+ {
8026
+ key: "roles.edit.details",
8027
+ description: "Edit role details"
8028
+ },
8029
+ {
8030
+ key: "roles.delete",
8031
+ description: "Delete roles"
8032
+ }
8033
+ ]
8034
+ },
8035
+ {
8036
+ key: "invitations",
8037
+ label: "Invitations",
8038
+ permissions: [
8039
+ {
8040
+ key: "invitations.view",
8041
+ description: "View invitations"
8042
+ },
8043
+ {
8044
+ key: "invitations.view.details",
8045
+ description: "View invitation details"
8046
+ },
8047
+ { key: "invitations.send", description: "Send invitations" },
8048
+ { key: "invitations.revoke", description: "Revoke invitations" }
8049
+ ]
8050
+ }
8051
+ ];
8052
+ for (const app of apps.items) {
8053
+ for (const module2 of modules) {
8054
+ const existingGroup = await _getByKey(module2.key);
8055
+ if (!existingGroup) {
8056
+ await _add({
8057
+ app: app.code,
8058
+ key: module2.key,
8059
+ label: module2.label
8060
+ });
8061
+ import_utils47.logger.log({
8062
+ level: "info",
8063
+ message: `Default permission group added: ${app.code} - ${module2.key}`
8064
+ });
8065
+ }
8066
+ for (const permission of module2.permissions) {
8067
+ const existingPermission = await getPermissionByKey(
8068
+ permission.key,
8069
+ module2.key
8070
+ );
8071
+ if (!existingPermission) {
8072
+ await addPermission({
8073
+ app: app.code,
8074
+ group: module2.key,
8075
+ key: permission.key,
8076
+ description: permission.description
8077
+ });
8078
+ }
8079
+ }
8080
+ }
8081
+ if (app.code === "admin") {
8082
+ const modules2 = [
8083
+ {
8084
+ key: "applications",
8085
+ label: "Applications",
8086
+ permissions: [
8087
+ {
8088
+ key: "applications.add",
8089
+ description: "Add new applications"
8090
+ },
8091
+ {
8092
+ key: "applications.view",
8093
+ description: "View applications"
8094
+ },
8095
+ {
8096
+ key: "applications.view.details",
8097
+ description: "View application details"
8098
+ },
8099
+ {
8100
+ key: "applications.edit",
8101
+ description: "Edit existing applications"
8102
+ },
8103
+ {
8104
+ key: "applications.update.details",
8105
+ description: "Update application details"
8106
+ },
8107
+ {
8108
+ key: "applications.delete",
8109
+ description: "Delete applications"
8110
+ }
8111
+ ]
8112
+ },
8113
+ {
8114
+ key: "organizations",
8115
+ label: "Organizations",
8116
+ permissions: [
8117
+ {
8118
+ key: "organizations.add",
8119
+ description: "Add new organizations"
8120
+ },
8121
+ {
8122
+ key: "organizations.view",
8123
+ description: "View organizations"
8124
+ },
8125
+ {
8126
+ key: "organizations.view.details",
8127
+ description: "View organization details"
8128
+ },
8129
+ {
8130
+ key: "organizations.edit.status",
8131
+ description: "Edit organization status"
8132
+ },
8133
+ {
8134
+ key: "organizations.delete",
8135
+ description: "Delete organizations"
8136
+ }
8137
+ ]
8138
+ },
8139
+ {
8140
+ key: "users",
8141
+ label: "Users",
8142
+ permissions: [
8143
+ {
8144
+ key: "users.add",
8145
+ description: "Add new users"
8146
+ },
8147
+ {
8148
+ key: "users.view",
8149
+ description: "View users"
8150
+ },
8151
+ {
8152
+ key: "users.view.details",
8153
+ description: "View user details"
8154
+ },
8155
+ {
8156
+ key: "users.edit.status",
8157
+ description: "Edit user status"
8158
+ },
8159
+ {
8160
+ key: "users.delete",
8161
+ description: "Delete users"
8162
+ }
8163
+ ]
8164
+ },
8165
+ {
8166
+ key: "subscriptions",
8167
+ label: "Subscriptions",
8168
+ permissions: [
8169
+ {
8170
+ key: "subscriptions.view",
8171
+ description: "View subscriptions"
8172
+ },
8173
+ {
8174
+ key: "subscriptions.view.details",
8175
+ description: "View subscription details"
8176
+ },
8177
+ {
8178
+ key: "subscriptions.edit.details",
8179
+ description: "Edit subscription details"
8180
+ }
8181
+ ]
8182
+ },
8183
+ {
8184
+ key: "promo.codes",
8185
+ label: "Promo Codes",
8186
+ permissions: [
8187
+ {
8188
+ key: "promo.codes.view",
8189
+ description: "View promo codes"
8190
+ },
8191
+ {
8192
+ key: "promo.codes.view.details",
8193
+ description: "View promo code details"
8194
+ },
8195
+ {
8196
+ key: "promo.codes.add",
8197
+ description: "Add new promo codes"
8198
+ },
8199
+ {
8200
+ key: "promo.codes.edit.details",
8201
+ description: "Edit promo code details"
8202
+ },
8203
+ {
8204
+ key: "promo.codes.update.status",
8205
+ description: "Update promo code status"
8206
+ },
8207
+ {
8208
+ key: "promo.codes.delete",
8209
+ description: "Delete promo codes"
8210
+ }
8211
+ ]
8212
+ },
8213
+ {
8214
+ key: "affiliates",
8215
+ label: "Affiliates",
8216
+ permissions: [
8217
+ {
8218
+ key: "affiliates.view",
8219
+ description: "View affiliates"
8220
+ },
8221
+ {
8222
+ key: "affiliates.view.details",
8223
+ description: "View affiliate details"
8224
+ },
8225
+ {
8226
+ key: "affiliates.edit.status",
8227
+ description: "Edit affiliate status"
8228
+ }
8229
+ ]
8230
+ }
8231
+ ];
8232
+ for (const module2 of modules2) {
8233
+ const existingGroup = await _getByKey(module2.key, app.code);
8234
+ if (!existingGroup) {
8235
+ await _add({
8236
+ app: app.code,
8237
+ key: module2.key,
8238
+ label: module2.label
8239
+ });
8240
+ import_utils47.logger.log({
8241
+ level: "info",
8242
+ message: `Default permission group added: ${app.code} - ${module2.key}`
8243
+ });
8244
+ }
8245
+ for (const permission of module2.permissions) {
8246
+ const existingPermission = await getPermissionByKey(
8247
+ permission.key,
8248
+ module2.key
8249
+ );
8250
+ if (!existingPermission) {
8251
+ await addPermission({
8252
+ app: app.code,
8253
+ group: module2.key,
8254
+ key: permission.key,
8255
+ description: permission.description
8256
+ });
8257
+ }
8258
+ }
8259
+ }
8260
+ }
8261
+ }
8262
+ await session.commitTransaction();
8263
+ }
8264
+ import_utils47.logger.log({
8265
+ level: "info",
8266
+ message: "Default permission groups added successfully."
8267
+ });
8268
+ return;
8269
+ } catch (error) {
8270
+ console.log(error);
8271
+ await session.abortTransaction();
8272
+ throw error;
8273
+ } finally {
8274
+ await session.endSession();
8275
+ }
8276
+ }
8277
+ if (!init4) {
8278
+ addDefaultModule().catch((error) => {
8279
+ import_utils47.logger.log({
8280
+ level: "error",
8281
+ message: `Error in addDefaultModule: ${error}`
8282
+ });
8283
+ });
8284
+ init4 = true;
8285
+ }
7737
8286
  async function deleteById(id) {
7738
8287
  const permission = await _getById(id);
7739
8288
  if (!permission) {
@@ -7741,7 +8290,7 @@ function usePermissionGroupService() {
7741
8290
  }
7742
8291
  const associatedPermissionsCount = await countByGroup(permission.key);
7743
8292
  if (associatedPermissionsCount > 0) {
7744
- throw new Error(
8293
+ throw new import_utils47.BadRequestError(
7745
8294
  "Cannot delete Permission Group with associated Permissions."
7746
8295
  );
7747
8296
  }
@@ -7749,7 +8298,11 @@ function usePermissionGroupService() {
7749
8298
  await _deleteById(id);
7750
8299
  return "Permission deleted successfully.";
7751
8300
  } catch (error) {
7752
- throw error;
8301
+ if (error instanceof import_utils47.AppError) {
8302
+ throw error;
8303
+ } else {
8304
+ throw new import_utils47.InternalServerError("Failed to delete Permission Group.");
8305
+ }
7753
8306
  }
7754
8307
  }
7755
8308
  return {
@@ -7758,8 +8311,8 @@ function usePermissionGroupService() {
7758
8311
  }
7759
8312
 
7760
8313
  // src/resources/permission/permission.group.controller.ts
7761
- var import_utils46 = require("@goweekdays/utils");
7762
- var import_joi24 = __toESM(require("joi"));
8314
+ var import_utils48 = require("@goweekdays/utils");
8315
+ var import_joi27 = __toESM(require("joi"));
7763
8316
  function usePermissionGroupController() {
7764
8317
  const {
7765
8318
  getAll: _getAll,
@@ -7772,8 +8325,8 @@ function usePermissionGroupController() {
7772
8325
  const value = req.body;
7773
8326
  const { error } = schemaPermissionGroup.validate(value);
7774
8327
  if (error) {
7775
- next(new import_utils46.BadRequestError(error.message));
7776
- import_utils46.logger.info(`Controller: ${error.message}`);
8328
+ next(new import_utils48.BadRequestError(error.message));
8329
+ import_utils48.logger.info(`Controller: ${error.message}`);
7777
8330
  return;
7778
8331
  }
7779
8332
  try {
@@ -7786,16 +8339,16 @@ function usePermissionGroupController() {
7786
8339
  }
7787
8340
  async function getAll(req, res, next) {
7788
8341
  const query = req.query;
7789
- const validation = import_joi24.default.object({
7790
- page: import_joi24.default.number().min(1).optional().allow("", null),
7791
- limit: import_joi24.default.number().min(1).optional().allow("", null),
7792
- search: import_joi24.default.string().optional().allow("", null),
7793
- app: import_joi24.default.string().optional().allow("", null),
7794
- status: import_joi24.default.string().optional().allow("", null)
8342
+ const validation = import_joi27.default.object({
8343
+ page: import_joi27.default.number().min(1).optional().allow("", null),
8344
+ limit: import_joi27.default.number().min(1).optional().allow("", null),
8345
+ search: import_joi27.default.string().optional().allow("", null),
8346
+ app: import_joi27.default.string().optional().allow("", null),
8347
+ status: import_joi27.default.string().optional().allow("", null)
7795
8348
  });
7796
8349
  const { error } = validation.validate(query);
7797
8350
  if (error) {
7798
- next(new import_utils46.BadRequestError(error.message));
8351
+ next(new import_utils48.BadRequestError(error.message));
7799
8352
  return;
7800
8353
  }
7801
8354
  const page = parseInt(req.query.page) ?? 1;
@@ -7829,12 +8382,12 @@ function usePermissionGroupController() {
7829
8382
  }
7830
8383
  async function getById(req, res, next) {
7831
8384
  const id = req.params.id;
7832
- const validation = import_joi24.default.object({
7833
- id: import_joi24.default.string().hex().required()
8385
+ const validation = import_joi27.default.object({
8386
+ id: import_joi27.default.string().hex().required()
7834
8387
  });
7835
8388
  const { error } = validation.validate({ id });
7836
8389
  if (error) {
7837
- next(new import_utils46.BadRequestError(error.message));
8390
+ next(new import_utils48.BadRequestError(error.message));
7838
8391
  return;
7839
8392
  }
7840
8393
  try {
@@ -7850,12 +8403,12 @@ function usePermissionGroupController() {
7850
8403
  }
7851
8404
  async function deleteById(req, res, next) {
7852
8405
  const id = req.params.id;
7853
- const validation = import_joi24.default.object({
7854
- id: import_joi24.default.string().hex().required()
8406
+ const validation = import_joi27.default.object({
8407
+ id: import_joi27.default.string().hex().required()
7855
8408
  });
7856
8409
  const { error } = validation.validate({ id });
7857
8410
  if (error) {
7858
- next(new import_utils46.BadRequestError(error.message));
8411
+ next(new import_utils48.BadRequestError(error.message));
7859
8412
  return;
7860
8413
  }
7861
8414
  try {
@@ -7868,15 +8421,15 @@ function usePermissionGroupController() {
7868
8421
  }
7869
8422
  async function updateById(req, res, next) {
7870
8423
  const id = req.params.id;
7871
- const { error: errorId } = import_joi24.default.string().hex().required().validate(id);
8424
+ const { error: errorId } = import_joi27.default.string().hex().required().validate(id);
7872
8425
  if (errorId) {
7873
- next(new import_utils46.BadRequestError(errorId.message));
8426
+ next(new import_utils48.BadRequestError(errorId.message));
7874
8427
  return;
7875
8428
  }
7876
8429
  const payload = req.body;
7877
8430
  const { error } = schemaPermissionGroupUpdate.validate(payload);
7878
8431
  if (error) {
7879
- next(new import_utils46.BadRequestError(error.message));
8432
+ next(new import_utils48.BadRequestError(error.message));
7880
8433
  return;
7881
8434
  }
7882
8435
  try {