@goweekdays/core 2.1.2 → 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
@@ -6876,259 +6876,27 @@ function useAppRepo() {
6876
6876
  }
6877
6877
 
6878
6878
  // src/resources/app/app.service.ts
6879
- import { logger as logger20, useAtlas as useAtlas19 } from "@goweekdays/utils";
6880
- var init2 = false;
6881
- function useAppService() {
6882
- const {
6883
- updateById: _updateById,
6884
- getById: _getById,
6885
- deleteById: _deleteById,
6886
- getByCode: _getByCode,
6887
- add: _add
6888
- } = useAppRepo();
6889
- async function addDefaultApps() {
6890
- const apps = [
6891
- {
6892
- code: "admin",
6893
- name: "Admin",
6894
- description: "Administrative application.",
6895
- type: "default"
6896
- },
6897
- {
6898
- code: "marketplace",
6899
- name: "Marketplace",
6900
- description: "Marketplace for product listings."
6901
- },
6902
- {
6903
- code: "service",
6904
- name: "Services",
6905
- description: "Service offerings and management."
6906
- },
6907
- {
6908
- code: "stay",
6909
- name: "Stay",
6910
- description: "Accommodation and lodging services."
6911
- },
6912
- { code: "eat", name: "Eat", description: "Food and dining services." },
6913
- {
6914
- code: "experience",
6915
- name: "Experience",
6916
- description: "Experiential touring, travel, activities and events."
6917
- },
6918
- {
6919
- code: "ride",
6920
- name: "Ride",
6921
- description: "Transportation and ride services."
6922
- }
6923
- ];
6924
- const session = useAtlas19.getClient()?.startSession();
6925
- if (!session) {
6926
- throw new Error("Failed to start database session.");
6927
- }
6928
- try {
6929
- session?.startTransaction();
6930
- for (const app of apps) {
6931
- const existingApp = await _getByCode(app.code);
6932
- if (!existingApp) {
6933
- await _add(app, session);
6934
- }
6935
- }
6936
- await session.commitTransaction();
6937
- logger20.log({
6938
- level: "info",
6939
- message: "Default apps added successfully."
6940
- });
6941
- return;
6942
- } catch (error) {
6943
- await session.abortTransaction();
6944
- logger20.log({
6945
- level: "error",
6946
- message: `Failed to add default apps: ${error}`
6947
- });
6948
- throw error;
6949
- } finally {
6950
- await session.endSession();
6951
- }
6952
- }
6953
- if (!init2) {
6954
- addDefaultApps().catch((error) => {
6955
- logger20.log({
6956
- level: "error",
6957
- message: `Error in addDefaultApps: ${error}`
6958
- });
6959
- });
6960
- init2 = true;
6961
- }
6962
- async function deleteById(id) {
6963
- try {
6964
- await _deleteById(id);
6965
- return "App deleted successfully.";
6966
- } catch (error) {
6967
- throw error;
6968
- }
6969
- }
6970
- return {
6971
- deleteById
6972
- };
6973
- }
6879
+ import { logger as logger25, useAtlas as useAtlas22 } from "@goweekdays/utils";
6974
6880
 
6975
- // src/resources/app/app.controller.ts
6881
+ // src/resources/permission/permission.model.ts
6976
6882
  import { BadRequestError as BadRequestError36 } from "@goweekdays/utils";
6977
6883
  import Joi21 from "joi";
6978
- function useAppController() {
6979
- const {
6980
- getAll: _getAll,
6981
- getById: _getById,
6982
- add: _add,
6983
- updateById: _updateById
6984
- } = useAppRepo();
6985
- const { deleteById: _deleteById } = useAppService();
6986
- async function add(req, res, next) {
6987
- const value = req.body;
6988
- const { error } = schemaApp.validate(value);
6989
- if (error) {
6990
- next(new BadRequestError36(error.message));
6991
- return;
6992
- }
6993
- try {
6994
- const result = await _add(value);
6995
- res.json(result);
6996
- return;
6997
- } catch (error2) {
6998
- next(error2);
6999
- }
7000
- }
7001
- async function updateById(req, res, next) {
7002
- const id = req.params.id ?? "";
7003
- const { error: errorId } = Joi21.string().hex().required().validate(id);
7004
- if (errorId) {
7005
- next(new BadRequestError36(errorId.message));
7006
- return;
7007
- }
7008
- const value = req.body;
7009
- const { error } = schemaAppUpdate.validate(value);
7010
- if (error) {
7011
- next(new BadRequestError36(error.message));
7012
- return;
7013
- }
7014
- try {
7015
- const result = await _updateById(req.params.id, value);
7016
- res.json(result);
7017
- return;
7018
- } catch (error2) {
7019
- next(error2);
7020
- }
7021
- }
7022
- async function getAll(req, res, next) {
7023
- const query = req.query;
7024
- const validation = Joi21.object({
7025
- page: Joi21.number().min(1).optional().allow("", null),
7026
- limit: Joi21.number().min(1).optional().allow("", null),
7027
- search: Joi21.string().optional().allow("", null),
7028
- status: Joi21.string().optional().allow("", null),
7029
- type: Joi21.string().optional().allow("", null)
7030
- });
7031
- const { error } = validation.validate(query);
7032
- if (error) {
7033
- next(new BadRequestError36(error.message));
7034
- return;
7035
- }
7036
- const page = parseInt(req.query.page) ?? 1;
7037
- let limit = parseInt(req.query.limit) ?? 20;
7038
- limit = isNaN(limit) ? 20 : limit;
7039
- const sort = req.query.sort ? String(req.query.sort).split(",") : "";
7040
- const sortOrder = req.query.sortOrder ? String(req.query.sortOrder).split(",") : "";
7041
- const sortObj = {};
7042
- if (sort && Array.isArray(sort) && sort.length && sortOrder && Array.isArray(sortOrder) && sortOrder.length) {
7043
- sort.forEach((field, index) => {
7044
- sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
7045
- });
7046
- }
7047
- const status = req.query.status ?? "active";
7048
- const search = req.query.search ?? "";
7049
- let type = req.query.type ? req.query.type.split(",") : "standard";
7050
- try {
7051
- const buildings = await _getAll({
7052
- page,
7053
- limit,
7054
- sort: sortObj,
7055
- status,
7056
- search,
7057
- type
7058
- });
7059
- res.json(buildings);
7060
- return;
7061
- } catch (error2) {
7062
- next(error2);
7063
- }
7064
- }
7065
- async function getById(req, res, next) {
7066
- const id = req.params.id;
7067
- const validation = Joi21.object({
7068
- id: Joi21.string().hex().required()
7069
- });
7070
- const { error } = validation.validate({ id });
7071
- if (error) {
7072
- next(new BadRequestError36(error.message));
7073
- return;
7074
- }
7075
- try {
7076
- const data = await _getById(id);
7077
- res.json({
7078
- message: "Successfully retrieved app.",
7079
- data
7080
- });
7081
- return;
7082
- } catch (error2) {
7083
- next(error2);
7084
- }
7085
- }
7086
- async function deleteById(req, res, next) {
7087
- const id = req.params.id;
7088
- const validation = Joi21.object({
7089
- id: Joi21.string().hex().required()
7090
- });
7091
- const { error } = validation.validate({ id });
7092
- if (error) {
7093
- next(new BadRequestError36(error.message));
7094
- return;
7095
- }
7096
- try {
7097
- const message = await _deleteById(id);
7098
- res.json(message);
7099
- return;
7100
- } catch (error2) {
7101
- next(error2);
7102
- }
7103
- }
7104
- return {
7105
- add,
7106
- updateById,
7107
- getAll,
7108
- getById,
7109
- deleteById
7110
- };
7111
- }
7112
-
7113
- // src/resources/permission/permission.model.ts
7114
- import { BadRequestError as BadRequestError37 } from "@goweekdays/utils";
7115
- import Joi22 from "joi";
7116
- var schemaPermission = Joi22.object({
7117
- app: Joi22.string().required(),
7118
- key: Joi22.string().required(),
7119
- group: Joi22.string().required(),
7120
- description: Joi22.string().required(),
7121
- deprecated: Joi22.boolean().optional().allow(null)
6884
+ var schemaPermission = Joi21.object({
6885
+ app: Joi21.string().required(),
6886
+ key: Joi21.string().required(),
6887
+ group: Joi21.string().required(),
6888
+ description: Joi21.string().required(),
6889
+ deprecated: Joi21.boolean().optional().allow(null)
7122
6890
  });
7123
- var schemaPermissionUpdate = Joi22.object({
7124
- key: Joi22.string().optional().allow("", null),
7125
- group: Joi22.string().optional().allow("", null),
7126
- description: Joi22.string().max(1024).optional().allow("", null)
6891
+ var schemaPermissionUpdate = Joi21.object({
6892
+ key: Joi21.string().optional().allow("", null),
6893
+ group: Joi21.string().optional().allow("", null),
6894
+ description: Joi21.string().max(1024).optional().allow("", null)
7127
6895
  });
7128
6896
  function modelPermission(value) {
7129
6897
  const { error } = schemaPermission.validate(value);
7130
6898
  if (error) {
7131
- throw new BadRequestError37(error.message);
6899
+ throw new BadRequestError36(error.message);
7132
6900
  }
7133
6901
  return {
7134
6902
  _id: value._id,
@@ -7147,18 +6915,18 @@ function modelPermission(value) {
7147
6915
  // src/resources/permission/permission.repository.ts
7148
6916
  import {
7149
6917
  AppError as AppError14,
7150
- BadRequestError as BadRequestError38,
6918
+ BadRequestError as BadRequestError37,
7151
6919
  InternalServerError as InternalServerError20,
7152
- logger as logger22,
6920
+ logger as logger20,
7153
6921
  makeCacheKey as makeCacheKey14,
7154
6922
  paginate as paginate10,
7155
- useAtlas as useAtlas20,
6923
+ useAtlas as useAtlas19,
7156
6924
  useCache as useCache15
7157
6925
  } from "@goweekdays/utils";
7158
6926
  import { ObjectId as ObjectId20 } from "mongodb";
7159
- import Joi23 from "joi";
6927
+ import Joi22 from "joi";
7160
6928
  function usePermissionRepo() {
7161
- const db = useAtlas20.getDb();
6929
+ const db = useAtlas19.getDb();
7162
6930
  if (!db) {
7163
6931
  throw new Error("Unable to connect to server.");
7164
6932
  }
@@ -7186,7 +6954,7 @@ function usePermissionRepo() {
7186
6954
  }
7187
6955
  }
7188
6956
  createIndexes().catch((error) => {
7189
- logger22.log({ level: "error", message: `Index creation error: ${error}` });
6957
+ logger20.log({ level: "error", message: `Index creation error: ${error}` });
7190
6958
  });
7191
6959
  async function add(value, session) {
7192
6960
  try {
@@ -7195,7 +6963,7 @@ function usePermissionRepo() {
7195
6963
  delCachedData();
7196
6964
  return res.insertedId;
7197
6965
  } catch (error) {
7198
- logger22.log({
6966
+ logger20.log({
7199
6967
  level: "error",
7200
6968
  message: error.message
7201
6969
  });
@@ -7204,7 +6972,7 @@ function usePermissionRepo() {
7204
6972
  } else {
7205
6973
  const isDuplicated = error.message.includes("duplicate");
7206
6974
  if (isDuplicated) {
7207
- throw new BadRequestError38("Permission already exists.");
6975
+ throw new BadRequestError37("Permission already exists.");
7208
6976
  }
7209
6977
  throw new Error("Failed to create permission.");
7210
6978
  }
@@ -7214,11 +6982,11 @@ function usePermissionRepo() {
7214
6982
  try {
7215
6983
  _id = new ObjectId20(_id);
7216
6984
  } catch (error2) {
7217
- throw new BadRequestError38("Invalid ID.");
6985
+ throw new BadRequestError37("Invalid ID.");
7218
6986
  }
7219
6987
  const { error } = schemaPermissionUpdate.validate(value);
7220
6988
  if (error) {
7221
- throw new BadRequestError38(`Invalid data: ${error.message}`);
6989
+ throw new BadRequestError37(`Invalid data: ${error.message}`);
7222
6990
  }
7223
6991
  try {
7224
6992
  const res = await collection.updateOne(
@@ -7229,7 +6997,7 @@ function usePermissionRepo() {
7229
6997
  delCachedData();
7230
6998
  return res;
7231
6999
  } catch (error2) {
7232
- logger22.log({
7000
+ logger20.log({
7233
7001
  level: "error",
7234
7002
  message: error2.message
7235
7003
  });
@@ -7267,14 +7035,14 @@ function usePermissionRepo() {
7267
7035
  cacheParams.app = app;
7268
7036
  }
7269
7037
  const cacheKey = makeCacheKey14(namespace_collection, cacheParams);
7270
- logger22.log({
7038
+ logger20.log({
7271
7039
  level: "info",
7272
7040
  message: `Cache key for getAll permissions: ${cacheKey}`
7273
7041
  });
7274
7042
  try {
7275
7043
  const cached = await getCache(cacheKey);
7276
7044
  if (cached) {
7277
- logger22.log({
7045
+ logger20.log({
7278
7046
  level: "info",
7279
7047
  message: `Cache hit for getAll permissions: ${cacheKey}`
7280
7048
  });
@@ -7289,19 +7057,19 @@ function usePermissionRepo() {
7289
7057
  const length = await collection.countDocuments(query);
7290
7058
  const data = paginate10(items, page, limit, length);
7291
7059
  setCache(cacheKey, data, 600).then(() => {
7292
- logger22.log({
7060
+ logger20.log({
7293
7061
  level: "info",
7294
7062
  message: `Cache set for getAll permissions: ${cacheKey}`
7295
7063
  });
7296
7064
  }).catch((err) => {
7297
- logger22.log({
7065
+ logger20.log({
7298
7066
  level: "error",
7299
7067
  message: `Failed to set cache for getAll permissions: ${err.message}`
7300
7068
  });
7301
7069
  });
7302
7070
  return data;
7303
7071
  } catch (error) {
7304
- logger22.log({ level: "error", message: `${error}` });
7072
+ logger20.log({ level: "error", message: `${error}` });
7305
7073
  throw error;
7306
7074
  }
7307
7075
  }
@@ -7309,13 +7077,13 @@ function usePermissionRepo() {
7309
7077
  try {
7310
7078
  _id = new ObjectId20(_id);
7311
7079
  } catch (error) {
7312
- throw new BadRequestError38("Invalid ID.");
7080
+ throw new BadRequestError37("Invalid ID.");
7313
7081
  }
7314
7082
  const cacheKey = makeCacheKey14(namespace_collection, { _id: String(_id) });
7315
7083
  try {
7316
7084
  const cached = await getCache(cacheKey);
7317
7085
  if (cached) {
7318
- logger22.log({
7086
+ logger20.log({
7319
7087
  level: "info",
7320
7088
  message: `Cache hit for getById permission: ${cacheKey}`
7321
7089
  });
@@ -7325,12 +7093,12 @@ function usePermissionRepo() {
7325
7093
  _id
7326
7094
  });
7327
7095
  setCache(cacheKey, result, 300).then(() => {
7328
- logger22.log({
7096
+ logger20.log({
7329
7097
  level: "info",
7330
7098
  message: `Cache set for permission by id: ${cacheKey}`
7331
7099
  });
7332
7100
  }).catch((err) => {
7333
- logger22.log({
7101
+ logger20.log({
7334
7102
  level: "error",
7335
7103
  message: `Failed to set cache for permission by id: ${err.message}`
7336
7104
  });
@@ -7344,14 +7112,15 @@ function usePermissionRepo() {
7344
7112
  }
7345
7113
  }
7346
7114
  }
7347
- async function getByKey(key, group) {
7348
- const validation = Joi23.object({
7349
- key: Joi23.string().required(),
7350
- group: Joi23.string().optional().allow("", null)
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)
7351
7120
  });
7352
7121
  const { error } = validation.validate({ key, group });
7353
7122
  if (error) {
7354
- throw new BadRequestError38(`Invalid data: ${error.message}`);
7123
+ throw new BadRequestError37(`Invalid data: ${error.message}`);
7355
7124
  }
7356
7125
  const query = {};
7357
7126
  const cacheKeyOptions = {};
@@ -7361,11 +7130,15 @@ function usePermissionRepo() {
7361
7130
  query.group = group;
7362
7131
  cacheKeyOptions.group = group;
7363
7132
  }
7133
+ if (app) {
7134
+ query.app = app;
7135
+ cacheKeyOptions.app = app;
7136
+ }
7364
7137
  const cacheKey = makeCacheKey14(namespace_collection, cacheKeyOptions);
7365
7138
  try {
7366
7139
  const cached = await getCache(cacheKey);
7367
7140
  if (cached) {
7368
- logger22.log({
7141
+ logger20.log({
7369
7142
  level: "info",
7370
7143
  message: `Cache hit for getById permission: ${cacheKey}`
7371
7144
  });
@@ -7373,12 +7146,12 @@ function usePermissionRepo() {
7373
7146
  }
7374
7147
  const result = await collection.findOne(query);
7375
7148
  setCache(cacheKey, result, 300).then(() => {
7376
- logger22.log({
7149
+ logger20.log({
7377
7150
  level: "info",
7378
7151
  message: `Cache set for permission by key: ${cacheKey}`
7379
7152
  });
7380
7153
  }).catch((err) => {
7381
- logger22.log({
7154
+ logger20.log({
7382
7155
  level: "error",
7383
7156
  message: `Failed to set cache for permission by key: ${err.message}`
7384
7157
  });
@@ -7400,7 +7173,7 @@ function usePermissionRepo() {
7400
7173
  try {
7401
7174
  const cached = await getCache(cacheKey);
7402
7175
  if (cached) {
7403
- logger22.log({
7176
+ logger20.log({
7404
7177
  level: "info",
7405
7178
  message: `Cache hit for getById permission: ${cacheKey}`
7406
7179
  });
@@ -7410,12 +7183,12 @@ function usePermissionRepo() {
7410
7183
  group
7411
7184
  });
7412
7185
  setCache(cacheKey, result, 300).then(() => {
7413
- logger22.log({
7186
+ logger20.log({
7414
7187
  level: "info",
7415
7188
  message: `Cache set for permission count by group: ${cacheKey}`
7416
7189
  });
7417
7190
  }).catch((err) => {
7418
- logger22.log({
7191
+ logger20.log({
7419
7192
  level: "error",
7420
7193
  message: `Failed to set cache for permission by group: ${err.message}`
7421
7194
  });
@@ -7433,7 +7206,7 @@ function usePermissionRepo() {
7433
7206
  try {
7434
7207
  _id = new ObjectId20(_id);
7435
7208
  } catch (error) {
7436
- throw new BadRequestError38("Invalid ID.");
7209
+ throw new BadRequestError37("Invalid ID.");
7437
7210
  }
7438
7211
  try {
7439
7212
  const res = await collection.updateOne(
@@ -7443,7 +7216,7 @@ function usePermissionRepo() {
7443
7216
  delCachedData();
7444
7217
  return res;
7445
7218
  } catch (error) {
7446
- logger22.log({
7219
+ logger20.log({
7447
7220
  level: "error",
7448
7221
  message: error.message
7449
7222
  });
@@ -7456,12 +7229,12 @@ function usePermissionRepo() {
7456
7229
  }
7457
7230
  function delCachedData() {
7458
7231
  delNamespace().then(() => {
7459
- logger22.log({
7232
+ logger20.log({
7460
7233
  level: "info",
7461
7234
  message: `Cache namespace cleared for ${namespace_collection}`
7462
7235
  });
7463
7236
  }).catch((err) => {
7464
- logger22.log({
7237
+ logger20.log({
7465
7238
  level: "error",
7466
7239
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
7467
7240
  });
@@ -7500,8 +7273,8 @@ function usePermissionService() {
7500
7273
  }
7501
7274
 
7502
7275
  // src/resources/permission/permission.controller.ts
7503
- import { BadRequestError as BadRequestError39, logger as logger23 } from "@goweekdays/utils";
7504
- import Joi24 from "joi";
7276
+ import { BadRequestError as BadRequestError38, logger as logger21 } from "@goweekdays/utils";
7277
+ import Joi23 from "joi";
7505
7278
  function usePermissionController() {
7506
7279
  const {
7507
7280
  getAll: _getAll,
@@ -7514,8 +7287,8 @@ function usePermissionController() {
7514
7287
  const value = req.body;
7515
7288
  const { error } = schemaPermission.validate(value);
7516
7289
  if (error) {
7517
- next(new BadRequestError39(error.message));
7518
- logger23.info(`Controller: ${error.message}`);
7290
+ next(new BadRequestError38(error.message));
7291
+ logger21.info(`Controller: ${error.message}`);
7519
7292
  return;
7520
7293
  }
7521
7294
  try {
@@ -7528,16 +7301,16 @@ function usePermissionController() {
7528
7301
  }
7529
7302
  async function getAll(req, res, next) {
7530
7303
  const query = req.query;
7531
- const validation = Joi24.object({
7532
- page: Joi24.number().min(1).optional().allow("", null),
7533
- limit: Joi24.number().min(1).optional().allow("", null),
7534
- search: Joi24.string().optional().allow("", null),
7535
- app: Joi24.string().optional().allow("", null),
7536
- status: Joi24.string().optional().allow("", null)
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)
7537
7310
  });
7538
7311
  const { error } = validation.validate(query);
7539
7312
  if (error) {
7540
- next(new BadRequestError39(error.message));
7313
+ next(new BadRequestError38(error.message));
7541
7314
  return;
7542
7315
  }
7543
7316
  const page = parseInt(req.query.page) ?? 1;
@@ -7571,12 +7344,12 @@ function usePermissionController() {
7571
7344
  }
7572
7345
  async function getById(req, res, next) {
7573
7346
  const id = req.params.id;
7574
- const validation = Joi24.object({
7575
- id: Joi24.string().hex().required()
7347
+ const validation = Joi23.object({
7348
+ id: Joi23.string().hex().required()
7576
7349
  });
7577
7350
  const { error } = validation.validate({ id });
7578
7351
  if (error) {
7579
- next(new BadRequestError39(error.message));
7352
+ next(new BadRequestError38(error.message));
7580
7353
  return;
7581
7354
  }
7582
7355
  try {
@@ -7592,12 +7365,12 @@ function usePermissionController() {
7592
7365
  }
7593
7366
  async function deleteById(req, res, next) {
7594
7367
  const id = req.params.id;
7595
- const validation = Joi24.object({
7596
- id: Joi24.string().hex().required()
7368
+ const validation = Joi23.object({
7369
+ id: Joi23.string().hex().required()
7597
7370
  });
7598
7371
  const { error } = validation.validate({ id });
7599
7372
  if (error) {
7600
- next(new BadRequestError39(error.message));
7373
+ next(new BadRequestError38(error.message));
7601
7374
  return;
7602
7375
  }
7603
7376
  try {
@@ -7610,15 +7383,15 @@ function usePermissionController() {
7610
7383
  }
7611
7384
  async function updateById(req, res, next) {
7612
7385
  const id = req.params.id;
7613
- const { error: errorId } = Joi24.string().hex().required().validate(id);
7386
+ const { error: errorId } = Joi23.string().hex().required().validate(id);
7614
7387
  if (errorId) {
7615
- next(new BadRequestError39(errorId.message));
7388
+ next(new BadRequestError38(errorId.message));
7616
7389
  return;
7617
7390
  }
7618
7391
  const payload = req.body;
7619
7392
  const { error } = schemaPermissionUpdate.validate(payload);
7620
7393
  if (error) {
7621
- next(new BadRequestError39(error.message));
7394
+ next(new BadRequestError38(error.message));
7622
7395
  return;
7623
7396
  }
7624
7397
  try {
@@ -7639,23 +7412,23 @@ function usePermissionController() {
7639
7412
  }
7640
7413
 
7641
7414
  // src/resources/permission/permission.group.model.ts
7642
- import { BadRequestError as BadRequestError40 } from "@goweekdays/utils";
7643
- import Joi25 from "joi";
7644
- var schemaPermissionGroup = Joi25.object({
7645
- app: Joi25.string().required(),
7646
- key: Joi25.string().required(),
7647
- label: Joi25.string().required(),
7648
- order: Joi25.number().integer().optional().allow("", null)
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)
7649
7422
  });
7650
- var schemaPermissionGroupUpdate = Joi25.object({
7651
- key: Joi25.string().optional().allow("", null),
7652
- label: Joi25.string().optional().allow("", null),
7653
- order: Joi25.number().integer().optional().allow("", null)
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)
7654
7427
  });
7655
7428
  function modelPermissionGroup(value) {
7656
7429
  const { error } = schemaPermissionGroup.validate(value);
7657
7430
  if (error) {
7658
- throw new BadRequestError40(error.message);
7431
+ throw new BadRequestError39(error.message);
7659
7432
  }
7660
7433
  return {
7661
7434
  _id: value._id,
@@ -7673,19 +7446,19 @@ function modelPermissionGroup(value) {
7673
7446
  // src/resources/permission/permission.group.repository.ts
7674
7447
  import {
7675
7448
  AppError as AppError15,
7676
- BadRequestError as BadRequestError41,
7449
+ BadRequestError as BadRequestError40,
7677
7450
  InternalServerError as InternalServerError21,
7678
- logger as logger24,
7451
+ logger as logger22,
7679
7452
  makeCacheKey as makeCacheKey15,
7680
7453
  paginate as paginate11,
7681
- useAtlas as useAtlas21,
7454
+ useAtlas as useAtlas20,
7682
7455
  useCache as useCache16
7683
7456
  } from "@goweekdays/utils";
7684
7457
  import { ObjectId as ObjectId21 } from "mongodb";
7685
- import Joi26 from "joi";
7686
- var init3 = false;
7458
+ import Joi25 from "joi";
7459
+ var init2 = false;
7687
7460
  function usePermissionGroupRepo() {
7688
- const db = useAtlas21.getDb();
7461
+ const db = useAtlas20.getDb();
7689
7462
  if (!db) {
7690
7463
  throw new Error("Unable to connect to server.");
7691
7464
  }
@@ -7712,24 +7485,23 @@ function usePermissionGroupRepo() {
7712
7485
  throw new Error("Failed to create index on permission groups.");
7713
7486
  }
7714
7487
  }
7715
- if (!init3) {
7488
+ if (!init2) {
7716
7489
  createIndexes().catch((error) => {
7717
- logger24.log({
7490
+ logger22.log({
7718
7491
  level: "error",
7719
7492
  message: `Index creation error: ${error}`
7720
7493
  });
7721
7494
  });
7722
- init3 = true;
7495
+ init2 = true;
7723
7496
  }
7724
7497
  async function add(value, session) {
7725
7498
  try {
7726
7499
  value = modelPermissionGroup(value);
7727
- console.log(value);
7728
7500
  const res = await collection.insertOne(value, { session });
7729
7501
  delCachedData();
7730
7502
  return res.insertedId;
7731
7503
  } catch (error) {
7732
- logger24.log({
7504
+ logger22.log({
7733
7505
  level: "error",
7734
7506
  message: error.message
7735
7507
  });
@@ -7738,7 +7510,7 @@ function usePermissionGroupRepo() {
7738
7510
  } else {
7739
7511
  const isDuplicated = error.message.includes("duplicate");
7740
7512
  if (isDuplicated) {
7741
- throw new BadRequestError41("Permission group already exists.");
7513
+ throw new BadRequestError40("Permission group already exists.");
7742
7514
  }
7743
7515
  throw new Error("Failed to create permission group.");
7744
7516
  }
@@ -7748,11 +7520,11 @@ function usePermissionGroupRepo() {
7748
7520
  try {
7749
7521
  _id = new ObjectId21(_id);
7750
7522
  } catch (error2) {
7751
- throw new BadRequestError41("Invalid ID.");
7523
+ throw new BadRequestError40("Invalid ID.");
7752
7524
  }
7753
7525
  const { error } = schemaPermissionGroupUpdate.validate(value);
7754
7526
  if (error) {
7755
- throw new BadRequestError41(`Invalid data: ${error.message}`);
7527
+ throw new BadRequestError40(`Invalid data: ${error.message}`);
7756
7528
  }
7757
7529
  try {
7758
7530
  const res = await collection.updateOne(
@@ -7763,7 +7535,7 @@ function usePermissionGroupRepo() {
7763
7535
  delCachedData();
7764
7536
  return res;
7765
7537
  } catch (error2) {
7766
- logger24.log({
7538
+ logger22.log({
7767
7539
  level: "error",
7768
7540
  message: error2.message
7769
7541
  });
@@ -7801,14 +7573,14 @@ function usePermissionGroupRepo() {
7801
7573
  cacheParams.app = app;
7802
7574
  }
7803
7575
  const cacheKey = makeCacheKey15(namespace_collection, cacheParams);
7804
- logger24.log({
7576
+ logger22.log({
7805
7577
  level: "info",
7806
7578
  message: `Cache key for getAll permission groups: ${cacheKey}`
7807
7579
  });
7808
7580
  try {
7809
7581
  const cached = await getCache(cacheKey);
7810
7582
  if (cached) {
7811
- logger24.log({
7583
+ logger22.log({
7812
7584
  level: "info",
7813
7585
  message: `Cache hit for getAll permission groups: ${cacheKey}`
7814
7586
  });
@@ -7823,19 +7595,19 @@ function usePermissionGroupRepo() {
7823
7595
  const length = await collection.countDocuments(query);
7824
7596
  const data = paginate11(items, page, limit, length);
7825
7597
  setCache(cacheKey, data, 600).then(() => {
7826
- logger24.log({
7598
+ logger22.log({
7827
7599
  level: "info",
7828
7600
  message: `Cache set for getAll permission groups: ${cacheKey}`
7829
7601
  });
7830
7602
  }).catch((err) => {
7831
- logger24.log({
7603
+ logger22.log({
7832
7604
  level: "error",
7833
7605
  message: `Failed to set cache for getAll permission groups: ${err.message}`
7834
7606
  });
7835
7607
  });
7836
7608
  return data;
7837
7609
  } catch (error) {
7838
- logger24.log({ level: "error", message: `${error}` });
7610
+ logger22.log({ level: "error", message: `${error}` });
7839
7611
  throw error;
7840
7612
  }
7841
7613
  }
@@ -7843,13 +7615,13 @@ function usePermissionGroupRepo() {
7843
7615
  try {
7844
7616
  _id = new ObjectId21(_id);
7845
7617
  } catch (error) {
7846
- throw new BadRequestError41("Invalid ID.");
7618
+ throw new BadRequestError40("Invalid ID.");
7847
7619
  }
7848
7620
  const cacheKey = makeCacheKey15(namespace_collection, { _id: String(_id) });
7849
7621
  try {
7850
7622
  const cached = await getCache(cacheKey);
7851
7623
  if (cached) {
7852
- logger24.log({
7624
+ logger22.log({
7853
7625
  level: "info",
7854
7626
  message: `Cache hit for getById permission group: ${cacheKey}`
7855
7627
  });
@@ -7859,12 +7631,12 @@ function usePermissionGroupRepo() {
7859
7631
  _id
7860
7632
  });
7861
7633
  setCache(cacheKey, result, 300).then(() => {
7862
- logger24.log({
7634
+ logger22.log({
7863
7635
  level: "info",
7864
7636
  message: `Cache set for permission group by id: ${cacheKey}`
7865
7637
  });
7866
7638
  }).catch((err) => {
7867
- logger24.log({
7639
+ logger22.log({
7868
7640
  level: "error",
7869
7641
  message: `Failed to set cache for permission group by id: ${err.message}`
7870
7642
  });
@@ -7879,13 +7651,13 @@ function usePermissionGroupRepo() {
7879
7651
  }
7880
7652
  }
7881
7653
  async function getByKey(key, app) {
7882
- const validation = Joi26.object({
7883
- key: Joi26.string().required(),
7884
- app: Joi26.string().optional().allow(null, "")
7654
+ const validation = Joi25.object({
7655
+ key: Joi25.string().required(),
7656
+ app: Joi25.string().optional().allow(null, "")
7885
7657
  });
7886
7658
  const { error } = validation.validate({ key, app });
7887
7659
  if (error) {
7888
- throw new BadRequestError41("Invalid key.");
7660
+ throw new BadRequestError40("Invalid key.");
7889
7661
  }
7890
7662
  const query = { key };
7891
7663
  const cacheKeyOptions = { key, tag: "byKey" };
@@ -7897,7 +7669,7 @@ function usePermissionGroupRepo() {
7897
7669
  try {
7898
7670
  const cached = await getCache(cacheKey);
7899
7671
  if (cached) {
7900
- logger24.log({
7672
+ logger22.log({
7901
7673
  level: "info",
7902
7674
  message: `Cache hit for getById permission group: ${cacheKey}`
7903
7675
  });
@@ -7905,12 +7677,12 @@ function usePermissionGroupRepo() {
7905
7677
  }
7906
7678
  const result = await collection.findOne(query);
7907
7679
  setCache(cacheKey, result, 300).then(() => {
7908
- logger24.log({
7680
+ logger22.log({
7909
7681
  level: "info",
7910
7682
  message: `Cache set for permission group by key: ${cacheKey}`
7911
7683
  });
7912
7684
  }).catch((err) => {
7913
- logger24.log({
7685
+ logger22.log({
7914
7686
  level: "error",
7915
7687
  message: `Failed to set cache for permission group by key: ${err.message}`
7916
7688
  });
@@ -7928,7 +7700,7 @@ function usePermissionGroupRepo() {
7928
7700
  try {
7929
7701
  _id = new ObjectId21(_id);
7930
7702
  } catch (error) {
7931
- throw new BadRequestError41("Invalid ID.");
7703
+ throw new BadRequestError40("Invalid ID.");
7932
7704
  }
7933
7705
  try {
7934
7706
  const res = await collection.updateOne(
@@ -7938,7 +7710,7 @@ function usePermissionGroupRepo() {
7938
7710
  delCachedData();
7939
7711
  return res;
7940
7712
  } catch (error) {
7941
- logger24.log({
7713
+ logger22.log({
7942
7714
  level: "error",
7943
7715
  message: error.message
7944
7716
  });
@@ -7951,12 +7723,12 @@ function usePermissionGroupRepo() {
7951
7723
  }
7952
7724
  function delCachedData() {
7953
7725
  delNamespace().then(() => {
7954
- logger24.log({
7726
+ logger22.log({
7955
7727
  level: "info",
7956
7728
  message: `Cache namespace cleared for ${namespace_collection}`
7957
7729
  });
7958
7730
  }).catch((err) => {
7959
- logger24.log({
7731
+ logger22.log({
7960
7732
  level: "error",
7961
7733
  message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
7962
7734
  });
@@ -7976,12 +7748,11 @@ function usePermissionGroupRepo() {
7976
7748
  // src/resources/permission/permission.group.service.ts
7977
7749
  import {
7978
7750
  AppError as AppError16,
7979
- BadRequestError as BadRequestError42,
7751
+ BadRequestError as BadRequestError41,
7980
7752
  InternalServerError as InternalServerError22,
7981
- logger as logger25,
7982
- useAtlas as useAtlas22
7753
+ logger as logger23,
7754
+ useAtlas as useAtlas21
7983
7755
  } from "@goweekdays/utils";
7984
- var init4 = false;
7985
7756
  function usePermissionGroupService() {
7986
7757
  const {
7987
7758
  updateById: _updateById,
@@ -7997,7 +7768,7 @@ function usePermissionGroupService() {
7997
7768
  add: addPermission
7998
7769
  } = usePermissionRepo();
7999
7770
  async function addDefaultModule() {
8000
- const session = useAtlas22.getClient()?.startSession();
7771
+ const session = useAtlas21.getClient()?.startSession();
8001
7772
  if (!session) {
8002
7773
  throw new Error("Failed to start database session.");
8003
7774
  }
@@ -8074,14 +7845,14 @@ function usePermissionGroupService() {
8074
7845
  ];
8075
7846
  for (const app of apps.items) {
8076
7847
  for (const module of modules) {
8077
- const existingGroup = await _getByKey(module.key);
7848
+ const existingGroup = await _getByKey(module.key, app.code);
8078
7849
  if (!existingGroup) {
8079
7850
  await _add({
8080
7851
  app: app.code,
8081
7852
  key: module.key,
8082
7853
  label: module.label
8083
7854
  });
8084
- logger25.log({
7855
+ logger23.log({
8085
7856
  level: "info",
8086
7857
  message: `Default permission group added: ${app.code} - ${module.key}`
8087
7858
  });
@@ -8089,7 +7860,8 @@ function usePermissionGroupService() {
8089
7860
  for (const permission of module.permissions) {
8090
7861
  const existingPermission = await getPermissionByKey(
8091
7862
  permission.key,
8092
- module.key
7863
+ module.key,
7864
+ app.code
8093
7865
  );
8094
7866
  if (!existingPermission) {
8095
7867
  await addPermission({
@@ -8260,7 +8032,7 @@ function usePermissionGroupService() {
8260
8032
  key: module.key,
8261
8033
  label: module.label
8262
8034
  });
8263
- logger25.log({
8035
+ logger23.log({
8264
8036
  level: "info",
8265
8037
  message: `Default permission group added: ${app.code} - ${module.key}`
8266
8038
  });
@@ -8284,7 +8056,7 @@ function usePermissionGroupService() {
8284
8056
  }
8285
8057
  await session.commitTransaction();
8286
8058
  }
8287
- logger25.log({
8059
+ logger23.log({
8288
8060
  level: "info",
8289
8061
  message: "Default permission groups added successfully."
8290
8062
  });
@@ -8297,15 +8069,6 @@ function usePermissionGroupService() {
8297
8069
  await session.endSession();
8298
8070
  }
8299
8071
  }
8300
- if (!init4) {
8301
- addDefaultModule().catch((error) => {
8302
- logger25.log({
8303
- level: "error",
8304
- message: `Error in addDefaultModule: ${error}`
8305
- });
8306
- });
8307
- init4 = true;
8308
- }
8309
8072
  async function deleteById(id) {
8310
8073
  const permission = await _getById(id);
8311
8074
  if (!permission) {
@@ -8313,7 +8076,7 @@ function usePermissionGroupService() {
8313
8076
  }
8314
8077
  const associatedPermissionsCount = await countByGroup(permission.key);
8315
8078
  if (associatedPermissionsCount > 0) {
8316
- throw new BadRequestError42(
8079
+ throw new BadRequestError41(
8317
8080
  "Cannot delete Permission Group with associated Permissions."
8318
8081
  );
8319
8082
  }
@@ -8329,13 +8092,14 @@ function usePermissionGroupService() {
8329
8092
  }
8330
8093
  }
8331
8094
  return {
8095
+ addDefaultModule,
8332
8096
  deleteById
8333
8097
  };
8334
8098
  }
8335
8099
 
8336
8100
  // src/resources/permission/permission.group.controller.ts
8337
- import { BadRequestError as BadRequestError43, logger as logger26 } from "@goweekdays/utils";
8338
- import Joi27 from "joi";
8101
+ import { BadRequestError as BadRequestError42, logger as logger24 } from "@goweekdays/utils";
8102
+ import Joi26 from "joi";
8339
8103
  function usePermissionGroupController() {
8340
8104
  const {
8341
8105
  getAll: _getAll,
@@ -8348,8 +8112,8 @@ function usePermissionGroupController() {
8348
8112
  const value = req.body;
8349
8113
  const { error } = schemaPermissionGroup.validate(value);
8350
8114
  if (error) {
8351
- next(new BadRequestError43(error.message));
8352
- logger26.info(`Controller: ${error.message}`);
8115
+ next(new BadRequestError42(error.message));
8116
+ logger24.info(`Controller: ${error.message}`);
8353
8117
  return;
8354
8118
  }
8355
8119
  try {
@@ -8362,16 +8126,16 @@ function usePermissionGroupController() {
8362
8126
  }
8363
8127
  async function getAll(req, res, next) {
8364
8128
  const query = req.query;
8365
- const validation = Joi27.object({
8366
- page: Joi27.number().min(1).optional().allow("", null),
8367
- limit: Joi27.number().min(1).optional().allow("", null),
8368
- search: Joi27.string().optional().allow("", null),
8369
- app: Joi27.string().optional().allow("", null),
8370
- status: Joi27.string().optional().allow("", null)
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)
8371
8135
  });
8372
8136
  const { error } = validation.validate(query);
8373
8137
  if (error) {
8374
- next(new BadRequestError43(error.message));
8138
+ next(new BadRequestError42(error.message));
8375
8139
  return;
8376
8140
  }
8377
8141
  const page = parseInt(req.query.page) ?? 1;
@@ -8405,12 +8169,12 @@ function usePermissionGroupController() {
8405
8169
  }
8406
8170
  async function getById(req, res, next) {
8407
8171
  const id = req.params.id;
8408
- const validation = Joi27.object({
8409
- id: Joi27.string().hex().required()
8172
+ const validation = Joi26.object({
8173
+ id: Joi26.string().hex().required()
8410
8174
  });
8411
8175
  const { error } = validation.validate({ id });
8412
8176
  if (error) {
8413
- next(new BadRequestError43(error.message));
8177
+ next(new BadRequestError42(error.message));
8414
8178
  return;
8415
8179
  }
8416
8180
  try {
@@ -8426,12 +8190,12 @@ function usePermissionGroupController() {
8426
8190
  }
8427
8191
  async function deleteById(req, res, next) {
8428
8192
  const id = req.params.id;
8429
- const validation = Joi27.object({
8430
- id: Joi27.string().hex().required()
8193
+ const validation = Joi26.object({
8194
+ id: Joi26.string().hex().required()
8431
8195
  });
8432
8196
  const { error } = validation.validate({ id });
8433
8197
  if (error) {
8434
- next(new BadRequestError43(error.message));
8198
+ next(new BadRequestError42(error.message));
8435
8199
  return;
8436
8200
  }
8437
8201
  try {
@@ -8444,15 +8208,15 @@ function usePermissionGroupController() {
8444
8208
  }
8445
8209
  async function updateById(req, res, next) {
8446
8210
  const id = req.params.id;
8447
- const { error: errorId } = Joi27.string().hex().required().validate(id);
8211
+ const { error: errorId } = Joi26.string().hex().required().validate(id);
8448
8212
  if (errorId) {
8449
- next(new BadRequestError43(errorId.message));
8213
+ next(new BadRequestError42(errorId.message));
8450
8214
  return;
8451
8215
  }
8452
8216
  const payload = req.body;
8453
8217
  const { error } = schemaPermissionGroupUpdate.validate(payload);
8454
8218
  if (error) {
8455
- next(new BadRequestError43(error.message));
8219
+ next(new BadRequestError42(error.message));
8456
8220
  return;
8457
8221
  }
8458
8222
  try {
@@ -8471,6 +8235,248 @@ function usePermissionGroupController() {
8471
8235
  updateById
8472
8236
  };
8473
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
+ }
8474
8480
  export {
8475
8481
  ACCESS_TOKEN_EXPIRY,
8476
8482
  ACCESS_TOKEN_SECRET,