@goweekdays/core 2.1.1 → 2.1.3

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.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
- createIndexes().catch((error) => {
6621
- logger19.log({ level: "error", message: `Index creation error: ${error}` });
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
- school = "",
6677
- status = "active"
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
- if (school)
6702
- cacheParams.school = school;
6703
- if (status !== "active")
6704
- cacheParams.status = status;
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,177 +6869,23 @@ 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
6832
- function useAppService() {
6833
- const {
6834
- updateById: _updateById,
6835
- getById: _getById,
6836
- deleteById: _deleteById
6837
- } = useAppRepo();
6838
- async function deleteById(id) {
6839
- try {
6840
- await _deleteById(id);
6841
- return "App deleted successfully.";
6842
- } catch (error) {
6843
- throw error;
6844
- }
6845
- }
6846
- return {
6847
- deleteById
6848
- };
6849
- }
6850
-
6851
- // src/resources/app/app.controller.ts
6852
- import { BadRequestError as BadRequestError36 } from "@goweekdays/utils";
6853
- import Joi20 from "joi";
6854
- function useAppController() {
6855
- const {
6856
- getAll: _getAll,
6857
- getById: _getById,
6858
- add: _add,
6859
- updateById: _updateById
6860
- } = useAppRepo();
6861
- const { deleteById: _deleteById } = useAppService();
6862
- async function add(req, res, next) {
6863
- const value = req.body;
6864
- const { error } = schemaApp.validate(value);
6865
- if (error) {
6866
- next(new BadRequestError36(error.message));
6867
- return;
6868
- }
6869
- try {
6870
- const result = await _add(value);
6871
- res.json(result);
6872
- return;
6873
- } catch (error2) {
6874
- next(error2);
6875
- }
6876
- }
6877
- async function updateById(req, res, next) {
6878
- const id = req.params.id ?? "";
6879
- const { error: errorId } = Joi20.string().hex().required().validate(id);
6880
- if (errorId) {
6881
- next(new BadRequestError36(errorId.message));
6882
- return;
6883
- }
6884
- const value = req.body;
6885
- const { error } = schemaAppUpdate.validate(value);
6886
- if (error) {
6887
- next(new BadRequestError36(error.message));
6888
- return;
6889
- }
6890
- try {
6891
- const result = await _updateById(req.params.id, value);
6892
- res.json(result);
6893
- return;
6894
- } catch (error2) {
6895
- next(error2);
6896
- }
6897
- }
6898
- async function getAll(req, res, next) {
6899
- const query = req.query;
6900
- const validation = Joi20.object({
6901
- page: Joi20.number().min(1).optional().allow("", null),
6902
- limit: Joi20.number().min(1).optional().allow("", null),
6903
- search: Joi20.string().optional().allow("", null),
6904
- school: Joi20.string().hex().optional().allow("", null),
6905
- status: Joi20.string().optional().allow("", null)
6906
- });
6907
- const { error } = validation.validate(query);
6908
- if (error) {
6909
- next(new BadRequestError36(error.message));
6910
- return;
6911
- }
6912
- const page = parseInt(req.query.page) ?? 1;
6913
- let limit = parseInt(req.query.limit) ?? 20;
6914
- limit = isNaN(limit) ? 20 : limit;
6915
- const sort = req.query.sort ? String(req.query.sort).split(",") : "";
6916
- const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
6917
- const sortObj = {};
6918
- if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
6919
- sort.forEach((field, index) => {
6920
- sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
6921
- });
6922
- }
6923
- const status = req.query.status ?? "active";
6924
- const school = req.query.school ?? "";
6925
- const search = req.query.search ?? "";
6926
- try {
6927
- const buildings = await _getAll({
6928
- page,
6929
- limit,
6930
- sort: sortObj,
6931
- status,
6932
- school,
6933
- search
6934
- });
6935
- res.json(buildings);
6936
- return;
6937
- } catch (error2) {
6938
- next(error2);
6939
- }
6940
- }
6941
- async function getById(req, res, next) {
6942
- const id = req.params.id;
6943
- const validation = Joi20.object({
6944
- id: Joi20.string().hex().required()
6945
- });
6946
- const { error } = validation.validate({ id });
6947
- if (error) {
6948
- next(new BadRequestError36(error.message));
6949
- return;
6950
- }
6951
- try {
6952
- const data = await _getById(id);
6953
- res.json({
6954
- message: "Successfully retrieved app.",
6955
- data
6956
- });
6957
- return;
6958
- } catch (error2) {
6959
- next(error2);
6960
- }
6961
- }
6962
- async function deleteById(req, res, next) {
6963
- const id = req.params.id;
6964
- const validation = Joi20.object({
6965
- id: Joi20.string().hex().required()
6966
- });
6967
- const { error } = validation.validate({ id });
6968
- if (error) {
6969
- next(new BadRequestError36(error.message));
6970
- return;
6971
- }
6972
- try {
6973
- const message = await _deleteById(id);
6974
- res.json(message);
6975
- return;
6976
- } catch (error2) {
6977
- next(error2);
6978
- }
6979
- }
6980
- return {
6981
- add,
6982
- updateById,
6983
- getAll,
6984
- getById,
6985
- deleteById
6986
- };
6987
- }
6879
+ import { logger as logger25, useAtlas as useAtlas22 } from "@goweekdays/utils";
6988
6880
 
6989
6881
  // src/resources/permission/permission.model.ts
6990
- import { BadRequestError as BadRequestError37 } from "@goweekdays/utils";
6882
+ import { BadRequestError as BadRequestError36 } from "@goweekdays/utils";
6991
6883
  import Joi21 from "joi";
6992
6884
  var schemaPermission = Joi21.object({
6993
6885
  app: Joi21.string().required(),
6994
6886
  key: Joi21.string().required(),
6995
6887
  group: Joi21.string().required(),
6996
- description: Joi21.string().max(1024).required(),
6888
+ description: Joi21.string().required(),
6997
6889
  deprecated: Joi21.boolean().optional().allow(null)
6998
6890
  });
6999
6891
  var schemaPermissionUpdate = Joi21.object({
@@ -7004,7 +6896,7 @@ var schemaPermissionUpdate = Joi21.object({
7004
6896
  function modelPermission(value) {
7005
6897
  const { error } = schemaPermission.validate(value);
7006
6898
  if (error) {
7007
- throw new BadRequestError37(error.message);
6899
+ throw new BadRequestError36(error.message);
7008
6900
  }
7009
6901
  return {
7010
6902
  _id: value._id,
@@ -7023,15 +6915,16 @@ function modelPermission(value) {
7023
6915
  // src/resources/permission/permission.repository.ts
7024
6916
  import {
7025
6917
  AppError as AppError14,
7026
- BadRequestError as BadRequestError38,
6918
+ BadRequestError as BadRequestError37,
7027
6919
  InternalServerError as InternalServerError20,
7028
- logger as logger21,
6920
+ logger as logger20,
7029
6921
  makeCacheKey as makeCacheKey14,
7030
6922
  paginate as paginate10,
7031
6923
  useAtlas as useAtlas19,
7032
6924
  useCache as useCache15
7033
6925
  } from "@goweekdays/utils";
7034
6926
  import { ObjectId as ObjectId20 } from "mongodb";
6927
+ import Joi22 from "joi";
7035
6928
  function usePermissionRepo() {
7036
6929
  const db = useAtlas19.getDb();
7037
6930
  if (!db) {
@@ -7061,7 +6954,7 @@ function usePermissionRepo() {
7061
6954
  }
7062
6955
  }
7063
6956
  createIndexes().catch((error) => {
7064
- logger21.log({ level: "error", message: `Index creation error: ${error}` });
6957
+ logger20.log({ level: "error", message: `Index creation error: ${error}` });
7065
6958
  });
7066
6959
  async function add(value, session) {
7067
6960
  try {
@@ -7070,7 +6963,7 @@ function usePermissionRepo() {
7070
6963
  delCachedData();
7071
6964
  return res.insertedId;
7072
6965
  } catch (error) {
7073
- logger21.log({
6966
+ logger20.log({
7074
6967
  level: "error",
7075
6968
  message: error.message
7076
6969
  });
@@ -7079,7 +6972,7 @@ function usePermissionRepo() {
7079
6972
  } else {
7080
6973
  const isDuplicated = error.message.includes("duplicate");
7081
6974
  if (isDuplicated) {
7082
- throw new BadRequestError38("Permission already exists.");
6975
+ throw new BadRequestError37("Permission already exists.");
7083
6976
  }
7084
6977
  throw new Error("Failed to create permission.");
7085
6978
  }
@@ -7089,11 +6982,11 @@ function usePermissionRepo() {
7089
6982
  try {
7090
6983
  _id = new ObjectId20(_id);
7091
6984
  } catch (error2) {
7092
- throw new BadRequestError38("Invalid ID.");
6985
+ throw new BadRequestError37("Invalid ID.");
7093
6986
  }
7094
6987
  const { error } = schemaPermissionUpdate.validate(value);
7095
6988
  if (error) {
7096
- throw new BadRequestError38(`Invalid data: ${error.message}`);
6989
+ throw new BadRequestError37(`Invalid data: ${error.message}`);
7097
6990
  }
7098
6991
  try {
7099
6992
  const res = await collection.updateOne(
@@ -7104,7 +6997,7 @@ function usePermissionRepo() {
7104
6997
  delCachedData();
7105
6998
  return res;
7106
6999
  } catch (error2) {
7107
- logger21.log({
7000
+ logger20.log({
7108
7001
  level: "error",
7109
7002
  message: error2.message
7110
7003
  });
@@ -7142,14 +7035,14 @@ function usePermissionRepo() {
7142
7035
  cacheParams.app = app;
7143
7036
  }
7144
7037
  const cacheKey = makeCacheKey14(namespace_collection, cacheParams);
7145
- logger21.log({
7038
+ logger20.log({
7146
7039
  level: "info",
7147
7040
  message: `Cache key for getAll permissions: ${cacheKey}`
7148
7041
  });
7149
7042
  try {
7150
7043
  const cached = await getCache(cacheKey);
7151
7044
  if (cached) {
7152
- logger21.log({
7045
+ logger20.log({
7153
7046
  level: "info",
7154
7047
  message: `Cache hit for getAll permissions: ${cacheKey}`
7155
7048
  });
@@ -7164,19 +7057,19 @@ function usePermissionRepo() {
7164
7057
  const length = await collection.countDocuments(query);
7165
7058
  const data = paginate10(items, page, limit, length);
7166
7059
  setCache(cacheKey, data, 600).then(() => {
7167
- logger21.log({
7060
+ logger20.log({
7168
7061
  level: "info",
7169
7062
  message: `Cache set for getAll permissions: ${cacheKey}`
7170
7063
  });
7171
7064
  }).catch((err) => {
7172
- logger21.log({
7065
+ logger20.log({
7173
7066
  level: "error",
7174
7067
  message: `Failed to set cache for getAll permissions: ${err.message}`
7175
7068
  });
7176
7069
  });
7177
7070
  return data;
7178
7071
  } catch (error) {
7179
- logger21.log({ level: "error", message: `${error}` });
7072
+ logger20.log({ level: "error", message: `${error}` });
7180
7073
  throw error;
7181
7074
  }
7182
7075
  }
@@ -7184,13 +7077,13 @@ function usePermissionRepo() {
7184
7077
  try {
7185
7078
  _id = new ObjectId20(_id);
7186
7079
  } catch (error) {
7187
- throw new BadRequestError38("Invalid ID.");
7080
+ throw new BadRequestError37("Invalid ID.");
7188
7081
  }
7189
7082
  const cacheKey = makeCacheKey14(namespace_collection, { _id: String(_id) });
7190
7083
  try {
7191
7084
  const cached = await getCache(cacheKey);
7192
7085
  if (cached) {
7193
- logger21.log({
7086
+ logger20.log({
7194
7087
  level: "info",
7195
7088
  message: `Cache hit for getById permission: ${cacheKey}`
7196
7089
  });
@@ -7200,12 +7093,12 @@ function usePermissionRepo() {
7200
7093
  _id
7201
7094
  });
7202
7095
  setCache(cacheKey, result, 300).then(() => {
7203
- logger21.log({
7096
+ logger20.log({
7204
7097
  level: "info",
7205
7098
  message: `Cache set for permission by id: ${cacheKey}`
7206
7099
  });
7207
7100
  }).catch((err) => {
7208
- logger21.log({
7101
+ logger20.log({
7209
7102
  level: "error",
7210
7103
  message: `Failed to set cache for permission by id: ${err.message}`
7211
7104
  });
@@ -7219,6 +7112,59 @@ function usePermissionRepo() {
7219
7112
  }
7220
7113
  }
7221
7114
  }
7115
+ async function getByKey(key, group, app) {
7116
+ const validation = Joi22.object({
7117
+ key: Joi22.string().required(),
7118
+ group: Joi22.string().optional().allow("", null),
7119
+ app: Joi22.string().optional().allow("", null)
7120
+ });
7121
+ const { error } = validation.validate({ key, group });
7122
+ if (error) {
7123
+ throw new BadRequestError37(`Invalid data: ${error.message}`);
7124
+ }
7125
+ const query = {};
7126
+ const cacheKeyOptions = {};
7127
+ query.key = key;
7128
+ cacheKeyOptions.key = key;
7129
+ if (group) {
7130
+ query.group = group;
7131
+ cacheKeyOptions.group = group;
7132
+ }
7133
+ if (app) {
7134
+ query.app = app;
7135
+ cacheKeyOptions.app = app;
7136
+ }
7137
+ const cacheKey = makeCacheKey14(namespace_collection, cacheKeyOptions);
7138
+ try {
7139
+ const cached = await getCache(cacheKey);
7140
+ if (cached) {
7141
+ logger20.log({
7142
+ level: "info",
7143
+ message: `Cache hit for getById permission: ${cacheKey}`
7144
+ });
7145
+ return cached;
7146
+ }
7147
+ const result = await collection.findOne(query);
7148
+ setCache(cacheKey, result, 300).then(() => {
7149
+ logger20.log({
7150
+ level: "info",
7151
+ message: `Cache set for permission by key: ${cacheKey}`
7152
+ });
7153
+ }).catch((err) => {
7154
+ logger20.log({
7155
+ level: "error",
7156
+ message: `Failed to set cache for permission by key: ${err.message}`
7157
+ });
7158
+ });
7159
+ return result;
7160
+ } catch (error2) {
7161
+ if (error2 instanceof AppError14) {
7162
+ throw error2;
7163
+ } else {
7164
+ throw new InternalServerError20("Failed to get permission.");
7165
+ }
7166
+ }
7167
+ }
7222
7168
  async function countByGroup(group) {
7223
7169
  const cacheKey = makeCacheKey14(namespace_collection, {
7224
7170
  group,
@@ -7227,7 +7173,7 @@ function usePermissionRepo() {
7227
7173
  try {
7228
7174
  const cached = await getCache(cacheKey);
7229
7175
  if (cached) {
7230
- logger21.log({
7176
+ logger20.log({
7231
7177
  level: "info",
7232
7178
  message: `Cache hit for getById permission: ${cacheKey}`
7233
7179
  });
@@ -7237,12 +7183,12 @@ function usePermissionRepo() {
7237
7183
  group
7238
7184
  });
7239
7185
  setCache(cacheKey, result, 300).then(() => {
7240
- logger21.log({
7186
+ logger20.log({
7241
7187
  level: "info",
7242
7188
  message: `Cache set for permission count by group: ${cacheKey}`
7243
7189
  });
7244
7190
  }).catch((err) => {
7245
- logger21.log({
7191
+ logger20.log({
7246
7192
  level: "error",
7247
7193
  message: `Failed to set cache for permission by group: ${err.message}`
7248
7194
  });
@@ -7260,7 +7206,7 @@ function usePermissionRepo() {
7260
7206
  try {
7261
7207
  _id = new ObjectId20(_id);
7262
7208
  } catch (error) {
7263
- throw new BadRequestError38("Invalid ID.");
7209
+ throw new BadRequestError37("Invalid ID.");
7264
7210
  }
7265
7211
  try {
7266
7212
  const res = await collection.updateOne(
@@ -7270,7 +7216,7 @@ function usePermissionRepo() {
7270
7216
  delCachedData();
7271
7217
  return res;
7272
7218
  } catch (error) {
7273
- logger21.log({
7219
+ logger20.log({
7274
7220
  level: "error",
7275
7221
  message: error.message
7276
7222
  });
@@ -7283,12 +7229,12 @@ function usePermissionRepo() {
7283
7229
  }
7284
7230
  function delCachedData() {
7285
7231
  delNamespace().then(() => {
7286
- logger21.log({
7232
+ logger20.log({
7287
7233
  level: "info",
7288
7234
  message: `Cache namespace cleared for ${namespace_collection}`
7289
7235
  });
7290
7236
  }).catch((err) => {
7291
- logger21.log({
7237
+ logger20.log({
7292
7238
  level: "error",
7293
7239
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
7294
7240
  });
@@ -7299,6 +7245,7 @@ function usePermissionRepo() {
7299
7245
  add,
7300
7246
  getAll,
7301
7247
  getById,
7248
+ getByKey,
7302
7249
  updateById,
7303
7250
  deleteById,
7304
7251
  countByGroup
@@ -7326,8 +7273,8 @@ function usePermissionService() {
7326
7273
  }
7327
7274
 
7328
7275
  // src/resources/permission/permission.controller.ts
7329
- import { BadRequestError as BadRequestError39, logger as logger22 } from "@goweekdays/utils";
7330
- import Joi22 from "joi";
7276
+ import { BadRequestError as BadRequestError38, logger as logger21 } from "@goweekdays/utils";
7277
+ import Joi23 from "joi";
7331
7278
  function usePermissionController() {
7332
7279
  const {
7333
7280
  getAll: _getAll,
@@ -7340,8 +7287,8 @@ function usePermissionController() {
7340
7287
  const value = req.body;
7341
7288
  const { error } = schemaPermission.validate(value);
7342
7289
  if (error) {
7343
- next(new BadRequestError39(error.message));
7344
- logger22.info(`Controller: ${error.message}`);
7290
+ next(new BadRequestError38(error.message));
7291
+ logger21.info(`Controller: ${error.message}`);
7345
7292
  return;
7346
7293
  }
7347
7294
  try {
@@ -7354,16 +7301,16 @@ function usePermissionController() {
7354
7301
  }
7355
7302
  async function getAll(req, res, next) {
7356
7303
  const query = req.query;
7357
- const validation = Joi22.object({
7358
- page: Joi22.number().min(1).optional().allow("", null),
7359
- limit: Joi22.number().min(1).optional().allow("", null),
7360
- search: Joi22.string().optional().allow("", null),
7361
- app: Joi22.string().optional().allow("", null),
7362
- status: Joi22.string().optional().allow("", null)
7304
+ const validation = Joi23.object({
7305
+ page: Joi23.number().min(1).optional().allow("", null),
7306
+ limit: Joi23.number().min(1).optional().allow("", null),
7307
+ search: Joi23.string().optional().allow("", null),
7308
+ app: Joi23.string().optional().allow("", null),
7309
+ status: Joi23.string().optional().allow("", null)
7363
7310
  });
7364
7311
  const { error } = validation.validate(query);
7365
7312
  if (error) {
7366
- next(new BadRequestError39(error.message));
7313
+ next(new BadRequestError38(error.message));
7367
7314
  return;
7368
7315
  }
7369
7316
  const page = parseInt(req.query.page) ?? 1;
@@ -7397,12 +7344,12 @@ function usePermissionController() {
7397
7344
  }
7398
7345
  async function getById(req, res, next) {
7399
7346
  const id = req.params.id;
7400
- const validation = Joi22.object({
7401
- id: Joi22.string().hex().required()
7347
+ const validation = Joi23.object({
7348
+ id: Joi23.string().hex().required()
7402
7349
  });
7403
7350
  const { error } = validation.validate({ id });
7404
7351
  if (error) {
7405
- next(new BadRequestError39(error.message));
7352
+ next(new BadRequestError38(error.message));
7406
7353
  return;
7407
7354
  }
7408
7355
  try {
@@ -7418,12 +7365,12 @@ function usePermissionController() {
7418
7365
  }
7419
7366
  async function deleteById(req, res, next) {
7420
7367
  const id = req.params.id;
7421
- const validation = Joi22.object({
7422
- id: Joi22.string().hex().required()
7368
+ const validation = Joi23.object({
7369
+ id: Joi23.string().hex().required()
7423
7370
  });
7424
7371
  const { error } = validation.validate({ id });
7425
7372
  if (error) {
7426
- next(new BadRequestError39(error.message));
7373
+ next(new BadRequestError38(error.message));
7427
7374
  return;
7428
7375
  }
7429
7376
  try {
@@ -7436,15 +7383,15 @@ function usePermissionController() {
7436
7383
  }
7437
7384
  async function updateById(req, res, next) {
7438
7385
  const id = req.params.id;
7439
- const { error: errorId } = Joi22.string().hex().required().validate(id);
7386
+ const { error: errorId } = Joi23.string().hex().required().validate(id);
7440
7387
  if (errorId) {
7441
- next(new BadRequestError39(errorId.message));
7388
+ next(new BadRequestError38(errorId.message));
7442
7389
  return;
7443
7390
  }
7444
7391
  const payload = req.body;
7445
7392
  const { error } = schemaPermissionUpdate.validate(payload);
7446
7393
  if (error) {
7447
- next(new BadRequestError39(error.message));
7394
+ next(new BadRequestError38(error.message));
7448
7395
  return;
7449
7396
  }
7450
7397
  try {
@@ -7465,23 +7412,23 @@ function usePermissionController() {
7465
7412
  }
7466
7413
 
7467
7414
  // src/resources/permission/permission.group.model.ts
7468
- import { BadRequestError as BadRequestError40 } from "@goweekdays/utils";
7469
- import Joi23 from "joi";
7470
- var schemaPermissionGroup = Joi23.object({
7471
- app: Joi23.string().required(),
7472
- key: Joi23.string().required(),
7473
- label: Joi23.string().required(),
7474
- order: Joi23.number().integer().optional().allow("", null)
7415
+ import { BadRequestError as BadRequestError39 } from "@goweekdays/utils";
7416
+ import Joi24 from "joi";
7417
+ var schemaPermissionGroup = Joi24.object({
7418
+ app: Joi24.string().required(),
7419
+ key: Joi24.string().required(),
7420
+ label: Joi24.string().required(),
7421
+ order: Joi24.number().integer().optional().allow("", null)
7475
7422
  });
7476
- var schemaPermissionGroupUpdate = Joi23.object({
7477
- key: Joi23.string().optional().allow("", null),
7478
- label: Joi23.string().optional().allow("", null),
7479
- order: Joi23.number().integer().optional().allow("", null)
7423
+ var schemaPermissionGroupUpdate = Joi24.object({
7424
+ key: Joi24.string().optional().allow("", null),
7425
+ label: Joi24.string().optional().allow("", null),
7426
+ order: Joi24.number().integer().optional().allow("", null)
7480
7427
  });
7481
7428
  function modelPermissionGroup(value) {
7482
7429
  const { error } = schemaPermissionGroup.validate(value);
7483
7430
  if (error) {
7484
- throw new BadRequestError40(error.message);
7431
+ throw new BadRequestError39(error.message);
7485
7432
  }
7486
7433
  return {
7487
7434
  _id: value._id,
@@ -7499,15 +7446,17 @@ function modelPermissionGroup(value) {
7499
7446
  // src/resources/permission/permission.group.repository.ts
7500
7447
  import {
7501
7448
  AppError as AppError15,
7502
- BadRequestError as BadRequestError41,
7449
+ BadRequestError as BadRequestError40,
7503
7450
  InternalServerError as InternalServerError21,
7504
- logger as logger23,
7451
+ logger as logger22,
7505
7452
  makeCacheKey as makeCacheKey15,
7506
7453
  paginate as paginate11,
7507
7454
  useAtlas as useAtlas20,
7508
7455
  useCache as useCache16
7509
7456
  } from "@goweekdays/utils";
7510
7457
  import { ObjectId as ObjectId21 } from "mongodb";
7458
+ import Joi25 from "joi";
7459
+ var init2 = false;
7511
7460
  function usePermissionGroupRepo() {
7512
7461
  const db = useAtlas20.getDb();
7513
7462
  if (!db) {
@@ -7536,9 +7485,15 @@ function usePermissionGroupRepo() {
7536
7485
  throw new Error("Failed to create index on permission groups.");
7537
7486
  }
7538
7487
  }
7539
- createIndexes().catch((error) => {
7540
- logger23.log({ level: "error", message: `Index creation error: ${error}` });
7541
- });
7488
+ if (!init2) {
7489
+ createIndexes().catch((error) => {
7490
+ logger22.log({
7491
+ level: "error",
7492
+ message: `Index creation error: ${error}`
7493
+ });
7494
+ });
7495
+ init2 = true;
7496
+ }
7542
7497
  async function add(value, session) {
7543
7498
  try {
7544
7499
  value = modelPermissionGroup(value);
@@ -7546,7 +7501,7 @@ function usePermissionGroupRepo() {
7546
7501
  delCachedData();
7547
7502
  return res.insertedId;
7548
7503
  } catch (error) {
7549
- logger23.log({
7504
+ logger22.log({
7550
7505
  level: "error",
7551
7506
  message: error.message
7552
7507
  });
@@ -7555,7 +7510,7 @@ function usePermissionGroupRepo() {
7555
7510
  } else {
7556
7511
  const isDuplicated = error.message.includes("duplicate");
7557
7512
  if (isDuplicated) {
7558
- throw new BadRequestError41("App already exists.");
7513
+ throw new BadRequestError40("Permission group already exists.");
7559
7514
  }
7560
7515
  throw new Error("Failed to create permission group.");
7561
7516
  }
@@ -7565,11 +7520,11 @@ function usePermissionGroupRepo() {
7565
7520
  try {
7566
7521
  _id = new ObjectId21(_id);
7567
7522
  } catch (error2) {
7568
- throw new BadRequestError41("Invalid ID.");
7523
+ throw new BadRequestError40("Invalid ID.");
7569
7524
  }
7570
7525
  const { error } = schemaPermissionGroupUpdate.validate(value);
7571
7526
  if (error) {
7572
- throw new BadRequestError41(`Invalid data: ${error.message}`);
7527
+ throw new BadRequestError40(`Invalid data: ${error.message}`);
7573
7528
  }
7574
7529
  try {
7575
7530
  const res = await collection.updateOne(
@@ -7580,7 +7535,7 @@ function usePermissionGroupRepo() {
7580
7535
  delCachedData();
7581
7536
  return res;
7582
7537
  } catch (error2) {
7583
- logger23.log({
7538
+ logger22.log({
7584
7539
  level: "error",
7585
7540
  message: error2.message
7586
7541
  });
@@ -7618,14 +7573,14 @@ function usePermissionGroupRepo() {
7618
7573
  cacheParams.app = app;
7619
7574
  }
7620
7575
  const cacheKey = makeCacheKey15(namespace_collection, cacheParams);
7621
- logger23.log({
7576
+ logger22.log({
7622
7577
  level: "info",
7623
7578
  message: `Cache key for getAll permission groups: ${cacheKey}`
7624
7579
  });
7625
7580
  try {
7626
7581
  const cached = await getCache(cacheKey);
7627
7582
  if (cached) {
7628
- logger23.log({
7583
+ logger22.log({
7629
7584
  level: "info",
7630
7585
  message: `Cache hit for getAll permission groups: ${cacheKey}`
7631
7586
  });
@@ -7640,19 +7595,19 @@ function usePermissionGroupRepo() {
7640
7595
  const length = await collection.countDocuments(query);
7641
7596
  const data = paginate11(items, page, limit, length);
7642
7597
  setCache(cacheKey, data, 600).then(() => {
7643
- logger23.log({
7598
+ logger22.log({
7644
7599
  level: "info",
7645
7600
  message: `Cache set for getAll permission groups: ${cacheKey}`
7646
7601
  });
7647
7602
  }).catch((err) => {
7648
- logger23.log({
7603
+ logger22.log({
7649
7604
  level: "error",
7650
7605
  message: `Failed to set cache for getAll permission groups: ${err.message}`
7651
7606
  });
7652
7607
  });
7653
7608
  return data;
7654
7609
  } catch (error) {
7655
- logger23.log({ level: "error", message: `${error}` });
7610
+ logger22.log({ level: "error", message: `${error}` });
7656
7611
  throw error;
7657
7612
  }
7658
7613
  }
@@ -7660,13 +7615,13 @@ function usePermissionGroupRepo() {
7660
7615
  try {
7661
7616
  _id = new ObjectId21(_id);
7662
7617
  } catch (error) {
7663
- throw new BadRequestError41("Invalid ID.");
7618
+ throw new BadRequestError40("Invalid ID.");
7664
7619
  }
7665
7620
  const cacheKey = makeCacheKey15(namespace_collection, { _id: String(_id) });
7666
7621
  try {
7667
7622
  const cached = await getCache(cacheKey);
7668
7623
  if (cached) {
7669
- logger23.log({
7624
+ logger22.log({
7670
7625
  level: "info",
7671
7626
  message: `Cache hit for getById permission group: ${cacheKey}`
7672
7627
  });
@@ -7676,12 +7631,12 @@ function usePermissionGroupRepo() {
7676
7631
  _id
7677
7632
  });
7678
7633
  setCache(cacheKey, result, 300).then(() => {
7679
- logger23.log({
7634
+ logger22.log({
7680
7635
  level: "info",
7681
7636
  message: `Cache set for permission group by id: ${cacheKey}`
7682
7637
  });
7683
7638
  }).catch((err) => {
7684
- logger23.log({
7639
+ logger22.log({
7685
7640
  level: "error",
7686
7641
  message: `Failed to set cache for permission group by id: ${err.message}`
7687
7642
  });
@@ -7695,11 +7650,57 @@ function usePermissionGroupRepo() {
7695
7650
  }
7696
7651
  }
7697
7652
  }
7653
+ async function getByKey(key, app) {
7654
+ const validation = Joi25.object({
7655
+ key: Joi25.string().required(),
7656
+ app: Joi25.string().optional().allow(null, "")
7657
+ });
7658
+ const { error } = validation.validate({ key, app });
7659
+ if (error) {
7660
+ throw new BadRequestError40("Invalid key.");
7661
+ }
7662
+ const query = { key };
7663
+ const cacheKeyOptions = { key, tag: "byKey" };
7664
+ if (app) {
7665
+ query.app = app;
7666
+ cacheKeyOptions.app = app;
7667
+ }
7668
+ const cacheKey = makeCacheKey15(namespace_collection, cacheKeyOptions);
7669
+ try {
7670
+ const cached = await getCache(cacheKey);
7671
+ if (cached) {
7672
+ logger22.log({
7673
+ level: "info",
7674
+ message: `Cache hit for getById permission group: ${cacheKey}`
7675
+ });
7676
+ return cached;
7677
+ }
7678
+ const result = await collection.findOne(query);
7679
+ setCache(cacheKey, result, 300).then(() => {
7680
+ logger22.log({
7681
+ level: "info",
7682
+ message: `Cache set for permission group by key: ${cacheKey}`
7683
+ });
7684
+ }).catch((err) => {
7685
+ logger22.log({
7686
+ level: "error",
7687
+ message: `Failed to set cache for permission group by key: ${err.message}`
7688
+ });
7689
+ });
7690
+ return result;
7691
+ } catch (error2) {
7692
+ if (error2 instanceof AppError15) {
7693
+ throw error2;
7694
+ } else {
7695
+ throw new InternalServerError21("Failed to get permission group.");
7696
+ }
7697
+ }
7698
+ }
7698
7699
  async function deleteById(_id, session) {
7699
7700
  try {
7700
7701
  _id = new ObjectId21(_id);
7701
7702
  } catch (error) {
7702
- throw new BadRequestError41("Invalid ID.");
7703
+ throw new BadRequestError40("Invalid ID.");
7703
7704
  }
7704
7705
  try {
7705
7706
  const res = await collection.updateOne(
@@ -7709,7 +7710,7 @@ function usePermissionGroupRepo() {
7709
7710
  delCachedData();
7710
7711
  return res;
7711
7712
  } catch (error) {
7712
- logger23.log({
7713
+ logger22.log({
7713
7714
  level: "error",
7714
7715
  message: error.message
7715
7716
  });
@@ -7722,12 +7723,12 @@ function usePermissionGroupRepo() {
7722
7723
  }
7723
7724
  function delCachedData() {
7724
7725
  delNamespace().then(() => {
7725
- logger23.log({
7726
+ logger22.log({
7726
7727
  level: "info",
7727
7728
  message: `Cache namespace cleared for ${namespace_collection}`
7728
7729
  });
7729
7730
  }).catch((err) => {
7730
- logger23.log({
7731
+ logger22.log({
7731
7732
  level: "error",
7732
7733
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
7733
7734
  });
@@ -7738,6 +7739,7 @@ function usePermissionGroupRepo() {
7738
7739
  add,
7739
7740
  getAll,
7740
7741
  getById,
7742
+ getByKey,
7741
7743
  updateById,
7742
7744
  deleteById
7743
7745
  };
@@ -7746,16 +7748,327 @@ function usePermissionGroupRepo() {
7746
7748
  // src/resources/permission/permission.group.service.ts
7747
7749
  import {
7748
7750
  AppError as AppError16,
7749
- BadRequestError as BadRequestError42,
7750
- InternalServerError as InternalServerError22
7751
+ BadRequestError as BadRequestError41,
7752
+ InternalServerError as InternalServerError22,
7753
+ logger as logger23,
7754
+ useAtlas as useAtlas21
7751
7755
  } from "@goweekdays/utils";
7752
7756
  function usePermissionGroupService() {
7753
7757
  const {
7754
7758
  updateById: _updateById,
7755
7759
  getById: _getById,
7756
- deleteById: _deleteById
7760
+ deleteById: _deleteById,
7761
+ getByKey: _getByKey,
7762
+ add: _add
7757
7763
  } = usePermissionGroupRepo();
7758
- const { countByGroup } = usePermissionRepo();
7764
+ const { getAll: getAllApps } = useAppRepo();
7765
+ const {
7766
+ countByGroup,
7767
+ getByKey: getPermissionByKey,
7768
+ add: addPermission
7769
+ } = usePermissionRepo();
7770
+ async function addDefaultModule() {
7771
+ const session = useAtlas21.getClient()?.startSession();
7772
+ if (!session) {
7773
+ throw new Error("Failed to start database session.");
7774
+ }
7775
+ try {
7776
+ session?.startTransaction();
7777
+ const apps = await getAllApps({ limit: 20 });
7778
+ if (apps && apps.items && apps.items.length) {
7779
+ const modules = [
7780
+ {
7781
+ key: "members",
7782
+ label: "Members",
7783
+ permissions: [
7784
+ {
7785
+ key: "members.view",
7786
+ description: "View members"
7787
+ },
7788
+ {
7789
+ key: "members.view.details",
7790
+ description: "View member details"
7791
+ },
7792
+ {
7793
+ key: "members.add",
7794
+ description: "Add new members"
7795
+ },
7796
+ {
7797
+ key: "members.edit.details",
7798
+ description: "Edit member details"
7799
+ },
7800
+ {
7801
+ key: "members.delete",
7802
+ description: "Delete members"
7803
+ }
7804
+ ]
7805
+ },
7806
+ {
7807
+ key: "roles",
7808
+ label: "Roles",
7809
+ permissions: [
7810
+ {
7811
+ key: "roles.view",
7812
+ description: "View roles"
7813
+ },
7814
+ { key: "roles.view.details", description: "View role details" },
7815
+ {
7816
+ key: "roles.add",
7817
+ description: "Add new roles"
7818
+ },
7819
+ {
7820
+ key: "roles.edit.details",
7821
+ description: "Edit role details"
7822
+ },
7823
+ {
7824
+ key: "roles.delete",
7825
+ description: "Delete roles"
7826
+ }
7827
+ ]
7828
+ },
7829
+ {
7830
+ key: "invitations",
7831
+ label: "Invitations",
7832
+ permissions: [
7833
+ {
7834
+ key: "invitations.view",
7835
+ description: "View invitations"
7836
+ },
7837
+ {
7838
+ key: "invitations.view.details",
7839
+ description: "View invitation details"
7840
+ },
7841
+ { key: "invitations.send", description: "Send invitations" },
7842
+ { key: "invitations.revoke", description: "Revoke invitations" }
7843
+ ]
7844
+ }
7845
+ ];
7846
+ for (const app of apps.items) {
7847
+ for (const module of modules) {
7848
+ const existingGroup = await _getByKey(module.key, app.code);
7849
+ if (!existingGroup) {
7850
+ await _add({
7851
+ app: app.code,
7852
+ key: module.key,
7853
+ label: module.label
7854
+ });
7855
+ logger23.log({
7856
+ level: "info",
7857
+ message: `Default permission group added: ${app.code} - ${module.key}`
7858
+ });
7859
+ }
7860
+ for (const permission of module.permissions) {
7861
+ const existingPermission = await getPermissionByKey(
7862
+ permission.key,
7863
+ module.key,
7864
+ app.code
7865
+ );
7866
+ if (!existingPermission) {
7867
+ await addPermission({
7868
+ app: app.code,
7869
+ group: module.key,
7870
+ key: permission.key,
7871
+ description: permission.description
7872
+ });
7873
+ }
7874
+ }
7875
+ }
7876
+ if (app.code === "admin") {
7877
+ const modules2 = [
7878
+ {
7879
+ key: "applications",
7880
+ label: "Applications",
7881
+ permissions: [
7882
+ {
7883
+ key: "applications.add",
7884
+ description: "Add new applications"
7885
+ },
7886
+ {
7887
+ key: "applications.view",
7888
+ description: "View applications"
7889
+ },
7890
+ {
7891
+ key: "applications.view.details",
7892
+ description: "View application details"
7893
+ },
7894
+ {
7895
+ key: "applications.edit",
7896
+ description: "Edit existing applications"
7897
+ },
7898
+ {
7899
+ key: "applications.update.details",
7900
+ description: "Update application details"
7901
+ },
7902
+ {
7903
+ key: "applications.delete",
7904
+ description: "Delete applications"
7905
+ }
7906
+ ]
7907
+ },
7908
+ {
7909
+ key: "organizations",
7910
+ label: "Organizations",
7911
+ permissions: [
7912
+ {
7913
+ key: "organizations.add",
7914
+ description: "Add new organizations"
7915
+ },
7916
+ {
7917
+ key: "organizations.view",
7918
+ description: "View organizations"
7919
+ },
7920
+ {
7921
+ key: "organizations.view.details",
7922
+ description: "View organization details"
7923
+ },
7924
+ {
7925
+ key: "organizations.edit.status",
7926
+ description: "Edit organization status"
7927
+ },
7928
+ {
7929
+ key: "organizations.delete",
7930
+ description: "Delete organizations"
7931
+ }
7932
+ ]
7933
+ },
7934
+ {
7935
+ key: "users",
7936
+ label: "Users",
7937
+ permissions: [
7938
+ {
7939
+ key: "users.add",
7940
+ description: "Add new users"
7941
+ },
7942
+ {
7943
+ key: "users.view",
7944
+ description: "View users"
7945
+ },
7946
+ {
7947
+ key: "users.view.details",
7948
+ description: "View user details"
7949
+ },
7950
+ {
7951
+ key: "users.edit.status",
7952
+ description: "Edit user status"
7953
+ },
7954
+ {
7955
+ key: "users.delete",
7956
+ description: "Delete users"
7957
+ }
7958
+ ]
7959
+ },
7960
+ {
7961
+ key: "subscriptions",
7962
+ label: "Subscriptions",
7963
+ permissions: [
7964
+ {
7965
+ key: "subscriptions.view",
7966
+ description: "View subscriptions"
7967
+ },
7968
+ {
7969
+ key: "subscriptions.view.details",
7970
+ description: "View subscription details"
7971
+ },
7972
+ {
7973
+ key: "subscriptions.edit.details",
7974
+ description: "Edit subscription details"
7975
+ }
7976
+ ]
7977
+ },
7978
+ {
7979
+ key: "promo.codes",
7980
+ label: "Promo Codes",
7981
+ permissions: [
7982
+ {
7983
+ key: "promo.codes.view",
7984
+ description: "View promo codes"
7985
+ },
7986
+ {
7987
+ key: "promo.codes.view.details",
7988
+ description: "View promo code details"
7989
+ },
7990
+ {
7991
+ key: "promo.codes.add",
7992
+ description: "Add new promo codes"
7993
+ },
7994
+ {
7995
+ key: "promo.codes.edit.details",
7996
+ description: "Edit promo code details"
7997
+ },
7998
+ {
7999
+ key: "promo.codes.update.status",
8000
+ description: "Update promo code status"
8001
+ },
8002
+ {
8003
+ key: "promo.codes.delete",
8004
+ description: "Delete promo codes"
8005
+ }
8006
+ ]
8007
+ },
8008
+ {
8009
+ key: "affiliates",
8010
+ label: "Affiliates",
8011
+ permissions: [
8012
+ {
8013
+ key: "affiliates.view",
8014
+ description: "View affiliates"
8015
+ },
8016
+ {
8017
+ key: "affiliates.view.details",
8018
+ description: "View affiliate details"
8019
+ },
8020
+ {
8021
+ key: "affiliates.edit.status",
8022
+ description: "Edit affiliate status"
8023
+ }
8024
+ ]
8025
+ }
8026
+ ];
8027
+ for (const module of modules2) {
8028
+ const existingGroup = await _getByKey(module.key, app.code);
8029
+ if (!existingGroup) {
8030
+ await _add({
8031
+ app: app.code,
8032
+ key: module.key,
8033
+ label: module.label
8034
+ });
8035
+ logger23.log({
8036
+ level: "info",
8037
+ message: `Default permission group added: ${app.code} - ${module.key}`
8038
+ });
8039
+ }
8040
+ for (const permission of module.permissions) {
8041
+ const existingPermission = await getPermissionByKey(
8042
+ permission.key,
8043
+ module.key
8044
+ );
8045
+ if (!existingPermission) {
8046
+ await addPermission({
8047
+ app: app.code,
8048
+ group: module.key,
8049
+ key: permission.key,
8050
+ description: permission.description
8051
+ });
8052
+ }
8053
+ }
8054
+ }
8055
+ }
8056
+ }
8057
+ await session.commitTransaction();
8058
+ }
8059
+ logger23.log({
8060
+ level: "info",
8061
+ message: "Default permission groups added successfully."
8062
+ });
8063
+ return;
8064
+ } catch (error) {
8065
+ console.log(error);
8066
+ await session.abortTransaction();
8067
+ throw error;
8068
+ } finally {
8069
+ await session.endSession();
8070
+ }
8071
+ }
7759
8072
  async function deleteById(id) {
7760
8073
  const permission = await _getById(id);
7761
8074
  if (!permission) {
@@ -7763,7 +8076,7 @@ function usePermissionGroupService() {
7763
8076
  }
7764
8077
  const associatedPermissionsCount = await countByGroup(permission.key);
7765
8078
  if (associatedPermissionsCount > 0) {
7766
- throw new BadRequestError42(
8079
+ throw new BadRequestError41(
7767
8080
  "Cannot delete Permission Group with associated Permissions."
7768
8081
  );
7769
8082
  }
@@ -7779,13 +8092,14 @@ function usePermissionGroupService() {
7779
8092
  }
7780
8093
  }
7781
8094
  return {
8095
+ addDefaultModule,
7782
8096
  deleteById
7783
8097
  };
7784
8098
  }
7785
8099
 
7786
8100
  // src/resources/permission/permission.group.controller.ts
7787
- import { BadRequestError as BadRequestError43, logger as logger24 } from "@goweekdays/utils";
7788
- import Joi24 from "joi";
8101
+ import { BadRequestError as BadRequestError42, logger as logger24 } from "@goweekdays/utils";
8102
+ import Joi26 from "joi";
7789
8103
  function usePermissionGroupController() {
7790
8104
  const {
7791
8105
  getAll: _getAll,
@@ -7798,7 +8112,7 @@ function usePermissionGroupController() {
7798
8112
  const value = req.body;
7799
8113
  const { error } = schemaPermissionGroup.validate(value);
7800
8114
  if (error) {
7801
- next(new BadRequestError43(error.message));
8115
+ next(new BadRequestError42(error.message));
7802
8116
  logger24.info(`Controller: ${error.message}`);
7803
8117
  return;
7804
8118
  }
@@ -7812,16 +8126,16 @@ function usePermissionGroupController() {
7812
8126
  }
7813
8127
  async function getAll(req, res, next) {
7814
8128
  const query = req.query;
7815
- const validation = Joi24.object({
7816
- page: Joi24.number().min(1).optional().allow("", null),
7817
- limit: Joi24.number().min(1).optional().allow("", null),
7818
- search: Joi24.string().optional().allow("", null),
7819
- app: Joi24.string().optional().allow("", null),
7820
- status: Joi24.string().optional().allow("", null)
8129
+ const validation = Joi26.object({
8130
+ page: Joi26.number().min(1).optional().allow("", null),
8131
+ limit: Joi26.number().min(1).optional().allow("", null),
8132
+ search: Joi26.string().optional().allow("", null),
8133
+ app: Joi26.string().optional().allow("", null),
8134
+ status: Joi26.string().optional().allow("", null)
7821
8135
  });
7822
8136
  const { error } = validation.validate(query);
7823
8137
  if (error) {
7824
- next(new BadRequestError43(error.message));
8138
+ next(new BadRequestError42(error.message));
7825
8139
  return;
7826
8140
  }
7827
8141
  const page = parseInt(req.query.page) ?? 1;
@@ -7855,12 +8169,12 @@ function usePermissionGroupController() {
7855
8169
  }
7856
8170
  async function getById(req, res, next) {
7857
8171
  const id = req.params.id;
7858
- const validation = Joi24.object({
7859
- id: Joi24.string().hex().required()
8172
+ const validation = Joi26.object({
8173
+ id: Joi26.string().hex().required()
7860
8174
  });
7861
8175
  const { error } = validation.validate({ id });
7862
8176
  if (error) {
7863
- next(new BadRequestError43(error.message));
8177
+ next(new BadRequestError42(error.message));
7864
8178
  return;
7865
8179
  }
7866
8180
  try {
@@ -7876,12 +8190,12 @@ function usePermissionGroupController() {
7876
8190
  }
7877
8191
  async function deleteById(req, res, next) {
7878
8192
  const id = req.params.id;
7879
- const validation = Joi24.object({
7880
- id: Joi24.string().hex().required()
8193
+ const validation = Joi26.object({
8194
+ id: Joi26.string().hex().required()
7881
8195
  });
7882
8196
  const { error } = validation.validate({ id });
7883
8197
  if (error) {
7884
- next(new BadRequestError43(error.message));
8198
+ next(new BadRequestError42(error.message));
7885
8199
  return;
7886
8200
  }
7887
8201
  try {
@@ -7894,15 +8208,15 @@ function usePermissionGroupController() {
7894
8208
  }
7895
8209
  async function updateById(req, res, next) {
7896
8210
  const id = req.params.id;
7897
- const { error: errorId } = Joi24.string().hex().required().validate(id);
8211
+ const { error: errorId } = Joi26.string().hex().required().validate(id);
7898
8212
  if (errorId) {
7899
- next(new BadRequestError43(errorId.message));
8213
+ next(new BadRequestError42(errorId.message));
7900
8214
  return;
7901
8215
  }
7902
8216
  const payload = req.body;
7903
8217
  const { error } = schemaPermissionGroupUpdate.validate(payload);
7904
8218
  if (error) {
7905
- next(new BadRequestError43(error.message));
8219
+ next(new BadRequestError42(error.message));
7906
8220
  return;
7907
8221
  }
7908
8222
  try {
@@ -7921,6 +8235,248 @@ function usePermissionGroupController() {
7921
8235
  updateById
7922
8236
  };
7923
8237
  }
8238
+
8239
+ // src/resources/app/app.service.ts
8240
+ var init3 = false;
8241
+ function useAppService() {
8242
+ const {
8243
+ updateById: _updateById,
8244
+ getById: _getById,
8245
+ deleteById: _deleteById,
8246
+ getByCode: _getByCode,
8247
+ add: _add
8248
+ } = useAppRepo();
8249
+ const { addDefaultModule } = usePermissionGroupService();
8250
+ async function addDefaultApps() {
8251
+ const apps = [
8252
+ {
8253
+ code: "admin",
8254
+ name: "Admin",
8255
+ description: "Administrative application.",
8256
+ type: "default"
8257
+ },
8258
+ {
8259
+ code: "marketplace",
8260
+ name: "Marketplace",
8261
+ description: "Marketplace for product listings."
8262
+ },
8263
+ {
8264
+ code: "service",
8265
+ name: "Services",
8266
+ description: "Service offerings and management."
8267
+ },
8268
+ {
8269
+ code: "stay",
8270
+ name: "Stay",
8271
+ description: "Accommodation and lodging services."
8272
+ },
8273
+ { code: "eat", name: "Eat", description: "Food and dining services." },
8274
+ {
8275
+ code: "experience",
8276
+ name: "Experience",
8277
+ description: "Experiential touring, travel, activities and events."
8278
+ },
8279
+ {
8280
+ code: "ride",
8281
+ name: "Ride",
8282
+ description: "Transportation and ride services."
8283
+ }
8284
+ ];
8285
+ const session = useAtlas22.getClient()?.startSession();
8286
+ if (!session) {
8287
+ throw new Error("Failed to start database session.");
8288
+ }
8289
+ try {
8290
+ session?.startTransaction();
8291
+ for (const app of apps) {
8292
+ const existingApp = await _getByCode(app.code);
8293
+ if (!existingApp) {
8294
+ await _add(app, session);
8295
+ }
8296
+ }
8297
+ await session.commitTransaction();
8298
+ logger25.log({
8299
+ level: "info",
8300
+ message: "Default apps added successfully."
8301
+ });
8302
+ return;
8303
+ } catch (error) {
8304
+ await session.abortTransaction();
8305
+ logger25.log({
8306
+ level: "error",
8307
+ message: `Failed to add default apps: ${error}`
8308
+ });
8309
+ throw error;
8310
+ } finally {
8311
+ await session.endSession();
8312
+ }
8313
+ }
8314
+ if (!init3) {
8315
+ addDefaultApps().catch((error) => {
8316
+ logger25.log({
8317
+ level: "error",
8318
+ message: `Error in addDefaultApps: ${error}`
8319
+ });
8320
+ }).then(() => {
8321
+ addDefaultModule().catch((error) => {
8322
+ logger25.log({
8323
+ level: "error",
8324
+ message: `Error in addDefaultModule: ${error}`
8325
+ });
8326
+ });
8327
+ });
8328
+ init3 = true;
8329
+ }
8330
+ async function deleteById(id) {
8331
+ try {
8332
+ await _deleteById(id);
8333
+ return "App deleted successfully.";
8334
+ } catch (error) {
8335
+ throw error;
8336
+ }
8337
+ }
8338
+ return {
8339
+ deleteById
8340
+ };
8341
+ }
8342
+
8343
+ // src/resources/app/app.controller.ts
8344
+ import { BadRequestError as BadRequestError43 } from "@goweekdays/utils";
8345
+ import Joi27 from "joi";
8346
+ function useAppController() {
8347
+ const {
8348
+ getAll: _getAll,
8349
+ getById: _getById,
8350
+ add: _add,
8351
+ updateById: _updateById
8352
+ } = useAppRepo();
8353
+ const { deleteById: _deleteById } = useAppService();
8354
+ async function add(req, res, next) {
8355
+ const value = req.body;
8356
+ const { error } = schemaApp.validate(value);
8357
+ if (error) {
8358
+ next(new BadRequestError43(error.message));
8359
+ return;
8360
+ }
8361
+ try {
8362
+ const result = await _add(value);
8363
+ res.json(result);
8364
+ return;
8365
+ } catch (error2) {
8366
+ next(error2);
8367
+ }
8368
+ }
8369
+ async function updateById(req, res, next) {
8370
+ const id = req.params.id ?? "";
8371
+ const { error: errorId } = Joi27.string().hex().required().validate(id);
8372
+ if (errorId) {
8373
+ next(new BadRequestError43(errorId.message));
8374
+ return;
8375
+ }
8376
+ const value = req.body;
8377
+ const { error } = schemaAppUpdate.validate(value);
8378
+ if (error) {
8379
+ next(new BadRequestError43(error.message));
8380
+ return;
8381
+ }
8382
+ try {
8383
+ const result = await _updateById(req.params.id, value);
8384
+ res.json(result);
8385
+ return;
8386
+ } catch (error2) {
8387
+ next(error2);
8388
+ }
8389
+ }
8390
+ async function getAll(req, res, next) {
8391
+ const query = req.query;
8392
+ const validation = Joi27.object({
8393
+ page: Joi27.number().min(1).optional().allow("", null),
8394
+ limit: Joi27.number().min(1).optional().allow("", null),
8395
+ search: Joi27.string().optional().allow("", null),
8396
+ status: Joi27.string().optional().allow("", null),
8397
+ type: Joi27.string().optional().allow("", null)
8398
+ });
8399
+ const { error } = validation.validate(query);
8400
+ if (error) {
8401
+ next(new BadRequestError43(error.message));
8402
+ return;
8403
+ }
8404
+ const page = parseInt(req.query.page) ?? 1;
8405
+ let limit = parseInt(req.query.limit) ?? 20;
8406
+ limit = isNaN(limit) ? 20 : limit;
8407
+ const sort = req.query.sort ? String(req.query.sort).split(",") : "";
8408
+ const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
8409
+ const sortObj = {};
8410
+ if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
8411
+ sort.forEach((field, index) => {
8412
+ sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
8413
+ });
8414
+ }
8415
+ const status = req.query.status ?? "active";
8416
+ const search = req.query.search ?? "";
8417
+ let type = req.query.type ? req.query.type.split(",") : "standard";
8418
+ try {
8419
+ const buildings = await _getAll({
8420
+ page,
8421
+ limit,
8422
+ sort: sortObj,
8423
+ status,
8424
+ search,
8425
+ type
8426
+ });
8427
+ res.json(buildings);
8428
+ return;
8429
+ } catch (error2) {
8430
+ next(error2);
8431
+ }
8432
+ }
8433
+ async function getById(req, res, next) {
8434
+ const id = req.params.id;
8435
+ const validation = Joi27.object({
8436
+ id: Joi27.string().hex().required()
8437
+ });
8438
+ const { error } = validation.validate({ id });
8439
+ if (error) {
8440
+ next(new BadRequestError43(error.message));
8441
+ return;
8442
+ }
8443
+ try {
8444
+ const data = await _getById(id);
8445
+ res.json({
8446
+ message: "Successfully retrieved app.",
8447
+ data
8448
+ });
8449
+ return;
8450
+ } catch (error2) {
8451
+ next(error2);
8452
+ }
8453
+ }
8454
+ async function deleteById(req, res, next) {
8455
+ const id = req.params.id;
8456
+ const validation = Joi27.object({
8457
+ id: Joi27.string().hex().required()
8458
+ });
8459
+ const { error } = validation.validate({ id });
8460
+ if (error) {
8461
+ next(new BadRequestError43(error.message));
8462
+ return;
8463
+ }
8464
+ try {
8465
+ const message = await _deleteById(id);
8466
+ res.json(message);
8467
+ return;
8468
+ } catch (error2) {
8469
+ next(error2);
8470
+ }
8471
+ }
8472
+ return {
8473
+ add,
8474
+ updateById,
8475
+ getAll,
8476
+ getById,
8477
+ deleteById
8478
+ };
8479
+ }
7924
8480
  export {
7925
8481
  ACCESS_TOKEN_EXPIRY,
7926
8482
  ACCESS_TOKEN_SECRET,