@goweekdays/core 2.0.0 → 2.1.1

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
@@ -6549,6 +6549,11 @@ var schemaApp = Joi19.object({
6549
6549
  name: Joi19.string().max(255).required(),
6550
6550
  description: Joi19.string().max(1024).optional().allow("", null)
6551
6551
  });
6552
+ var schemaAppUpdate = Joi19.object({
6553
+ code: Joi19.string().alphanum().max(20).optional().allow("", null),
6554
+ name: Joi19.string().max(255).optional().allow("", null),
6555
+ description: Joi19.string().max(1024).optional().allow("", null)
6556
+ });
6552
6557
  function modelApp(value) {
6553
6558
  const { error } = schemaApp.validate(value);
6554
6559
  if (error) {
@@ -6589,15 +6594,24 @@ function useAppRepo() {
6589
6594
  async function createIndexes() {
6590
6595
  try {
6591
6596
  await collection.createIndexes([
6592
- { key: { name: 1 }, unique: true, name: "unique_name_index" },
6593
6597
  { key: { code: 1 } },
6594
6598
  { key: { status: 1 } },
6595
6599
  {
6596
6600
  key: { code: "text", name: "text", description: "text" },
6597
6601
  name: "text_index"
6598
6602
  },
6599
- { key: { code: 1, status: 1 }, unique: true, name: "unique_code" },
6600
- { key: { name: 1, status: 1 }, unique: true, name: "unique_name" }
6603
+ {
6604
+ key: { code: 1, status: 1 },
6605
+ unique: true,
6606
+ name: "unique_code",
6607
+ partialFilterExpression: { status: { $in: ["active", "draft"] } }
6608
+ },
6609
+ {
6610
+ key: { name: 1, status: 1 },
6611
+ unique: true,
6612
+ name: "unique_name",
6613
+ partialFilterExpression: { status: { $in: ["active", "draft"] } }
6614
+ }
6601
6615
  ]);
6602
6616
  } catch (error) {
6603
6617
  throw new Error("Failed to create index on apps.");
@@ -6835,24 +6849,21 @@ function useAppService() {
6835
6849
  }
6836
6850
 
6837
6851
  // src/resources/app/app.controller.ts
6838
- import { BadRequestError as BadRequestError36, logger as logger20 } from "@goweekdays/utils";
6852
+ import { BadRequestError as BadRequestError36 } from "@goweekdays/utils";
6839
6853
  import Joi20 from "joi";
6840
6854
  function useAppController() {
6841
- const { getAll: _getAll, getById: _getById, add: _add } = useAppRepo();
6855
+ const {
6856
+ getAll: _getAll,
6857
+ getById: _getById,
6858
+ add: _add,
6859
+ updateById: _updateById
6860
+ } = useAppRepo();
6842
6861
  const { deleteById: _deleteById } = useAppService();
6843
6862
  async function add(req, res, next) {
6844
6863
  const value = req.body;
6845
- const validation = Joi20.object({
6846
- name: Joi20.string().required(),
6847
- school: Joi20.string().hex().required(),
6848
- levels: Joi20.number().integer().min(1).required(),
6849
- serial: Joi20.string().optional().allow("", null),
6850
- status: Joi20.string().optional().allow("", null)
6851
- });
6852
- const { error } = validation.validate(value);
6864
+ const { error } = schemaApp.validate(value);
6853
6865
  if (error) {
6854
6866
  next(new BadRequestError36(error.message));
6855
- logger20.info(`Controller: ${error.message}`);
6856
6867
  return;
6857
6868
  }
6858
6869
  try {
@@ -6863,6 +6874,27 @@ function useAppController() {
6863
6874
  next(error2);
6864
6875
  }
6865
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
+ }
6866
6898
  async function getAll(req, res, next) {
6867
6899
  const query = req.query;
6868
6900
  const validation = Joi20.object({
@@ -6947,6 +6979,7 @@ function useAppController() {
6947
6979
  }
6948
6980
  return {
6949
6981
  add,
6982
+ updateById,
6950
6983
  getAll,
6951
6984
  getById,
6952
6985
  deleteById
@@ -6963,6 +6996,11 @@ var schemaPermission = Joi21.object({
6963
6996
  description: Joi21.string().max(1024).required(),
6964
6997
  deprecated: Joi21.boolean().optional().allow(null)
6965
6998
  });
6999
+ var schemaPermissionUpdate = Joi21.object({
7000
+ key: Joi21.string().optional().allow("", null),
7001
+ group: Joi21.string().optional().allow("", null),
7002
+ description: Joi21.string().max(1024).optional().allow("", null)
7003
+ });
6966
7004
  function modelPermission(value) {
6967
7005
  const { error } = schemaPermission.validate(value);
6968
7006
  if (error) {
@@ -6975,6 +7013,7 @@ function modelPermission(value) {
6975
7013
  group: value.group,
6976
7014
  description: value.description,
6977
7015
  deprecated: value.deprecated ?? false,
7016
+ status: value.status ?? "active",
6978
7017
  createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
6979
7018
  updatedAt: "",
6980
7019
  deletedAt: ""
@@ -7049,9 +7088,13 @@ function usePermissionRepo() {
7049
7088
  async function updateById(_id, value, session) {
7050
7089
  try {
7051
7090
  _id = new ObjectId20(_id);
7052
- } catch (error) {
7091
+ } catch (error2) {
7053
7092
  throw new BadRequestError38("Invalid ID.");
7054
7093
  }
7094
+ const { error } = schemaPermissionUpdate.validate(value);
7095
+ if (error) {
7096
+ throw new BadRequestError38(`Invalid data: ${error.message}`);
7097
+ }
7055
7098
  try {
7056
7099
  const res = await collection.updateOne(
7057
7100
  { _id },
@@ -7060,13 +7103,13 @@ function usePermissionRepo() {
7060
7103
  );
7061
7104
  delCachedData();
7062
7105
  return res;
7063
- } catch (error) {
7106
+ } catch (error2) {
7064
7107
  logger21.log({
7065
7108
  level: "error",
7066
- message: error.message
7109
+ message: error2.message
7067
7110
  });
7068
- if (error instanceof AppError14) {
7069
- throw error;
7111
+ if (error2 instanceof AppError14) {
7112
+ throw error2;
7070
7113
  } else {
7071
7114
  throw new Error("Failed to update permission.");
7072
7115
  }
@@ -7077,35 +7120,27 @@ function usePermissionRepo() {
7077
7120
  page = 1,
7078
7121
  limit = 10,
7079
7122
  sort = {},
7080
- school = "",
7123
+ app = "",
7081
7124
  status = "active"
7082
7125
  } = {}) {
7083
7126
  page = page > 0 ? page - 1 : 0;
7084
- const query = {
7085
- status
7086
- };
7087
- sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
7088
- if (search) {
7089
- query.$text = { $search: search };
7090
- }
7091
- if (school) {
7092
- try {
7093
- query.school = new ObjectId20(school);
7094
- } catch (error) {
7095
- throw new BadRequestError38("Invalid school ID.");
7096
- }
7097
- }
7127
+ const query = {};
7098
7128
  const cacheParams = {
7099
7129
  page,
7100
7130
  limit,
7101
7131
  sort: JSON.stringify(sort)
7102
7132
  };
7103
- if (search)
7133
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
7134
+ query.status = status;
7135
+ cacheParams.status = status;
7136
+ if (search) {
7137
+ query.$text = { $search: search };
7104
7138
  cacheParams.search = search;
7105
- if (school)
7106
- cacheParams.school = school;
7107
- if (status !== "active")
7108
- cacheParams.status = status;
7139
+ }
7140
+ if (app) {
7141
+ query.app = app;
7142
+ cacheParams.app = app;
7143
+ }
7109
7144
  const cacheKey = makeCacheKey14(namespace_collection, cacheParams);
7110
7145
  logger21.log({
7111
7146
  level: "info",
@@ -7199,7 +7234,7 @@ function usePermissionRepo() {
7199
7234
  return cached;
7200
7235
  }
7201
7236
  const result = await collection.countDocuments({
7202
- key: group
7237
+ group
7203
7238
  });
7204
7239
  setCache(cacheKey, result, 300).then(() => {
7205
7240
  logger21.log({
@@ -7294,7 +7329,12 @@ function usePermissionService() {
7294
7329
  import { BadRequestError as BadRequestError39, logger as logger22 } from "@goweekdays/utils";
7295
7330
  import Joi22 from "joi";
7296
7331
  function usePermissionController() {
7297
- const { getAll: _getAll, getById: _getById, add: _add } = usePermissionRepo();
7332
+ const {
7333
+ getAll: _getAll,
7334
+ getById: _getById,
7335
+ add: _add,
7336
+ updateById: _updateById
7337
+ } = usePermissionRepo();
7298
7338
  const { deleteById: _deleteById } = usePermissionService();
7299
7339
  async function add(req, res, next) {
7300
7340
  const value = req.body;
@@ -7318,7 +7358,7 @@ function usePermissionController() {
7318
7358
  page: Joi22.number().min(1).optional().allow("", null),
7319
7359
  limit: Joi22.number().min(1).optional().allow("", null),
7320
7360
  search: Joi22.string().optional().allow("", null),
7321
- school: Joi22.string().hex().optional().allow("", null),
7361
+ app: Joi22.string().optional().allow("", null),
7322
7362
  status: Joi22.string().optional().allow("", null)
7323
7363
  });
7324
7364
  const { error } = validation.validate(query);
@@ -7337,17 +7377,17 @@ function usePermissionController() {
7337
7377
  sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
7338
7378
  });
7339
7379
  }
7340
- const status = req.query.status ?? "active";
7341
- const school = req.query.school ?? "";
7380
+ const app = req.query.app ?? "";
7342
7381
  const search = req.query.search ?? "";
7382
+ const status = req.query.status ?? "active";
7343
7383
  try {
7344
7384
  const buildings = await _getAll({
7345
7385
  page,
7346
7386
  limit,
7347
7387
  sort: sortObj,
7348
- status,
7349
- school,
7350
- search
7388
+ app,
7389
+ search,
7390
+ status
7351
7391
  });
7352
7392
  res.json(buildings);
7353
7393
  return;
@@ -7394,11 +7434,33 @@ function usePermissionController() {
7394
7434
  next(error2);
7395
7435
  }
7396
7436
  }
7437
+ async function updateById(req, res, next) {
7438
+ const id = req.params.id;
7439
+ const { error: errorId } = Joi22.string().hex().required().validate(id);
7440
+ if (errorId) {
7441
+ next(new BadRequestError39(errorId.message));
7442
+ return;
7443
+ }
7444
+ const payload = req.body;
7445
+ const { error } = schemaPermissionUpdate.validate(payload);
7446
+ if (error) {
7447
+ next(new BadRequestError39(error.message));
7448
+ return;
7449
+ }
7450
+ try {
7451
+ const message = await _updateById(id, payload);
7452
+ res.json(message);
7453
+ return;
7454
+ } catch (error2) {
7455
+ next(error2);
7456
+ }
7457
+ }
7397
7458
  return {
7398
7459
  add,
7399
7460
  getAll,
7400
7461
  getById,
7401
- deleteById
7462
+ deleteById,
7463
+ updateById
7402
7464
  };
7403
7465
  }
7404
7466
 
@@ -7409,7 +7471,12 @@ var schemaPermissionGroup = Joi23.object({
7409
7471
  app: Joi23.string().required(),
7410
7472
  key: Joi23.string().required(),
7411
7473
  label: Joi23.string().required(),
7412
- order: Joi23.number().integer().required()
7474
+ order: Joi23.number().integer().optional().allow("", null)
7475
+ });
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)
7413
7480
  });
7414
7481
  function modelPermissionGroup(value) {
7415
7482
  const { error } = schemaPermissionGroup.validate(value);
@@ -7421,7 +7488,8 @@ function modelPermissionGroup(value) {
7421
7488
  app: value.app,
7422
7489
  key: value.key,
7423
7490
  label: value.label,
7424
- order: value.order,
7491
+ order: value.order ?? Date.now(),
7492
+ status: value.status ?? "active",
7425
7493
  createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
7426
7494
  updatedAt: "",
7427
7495
  deletedAt: ""
@@ -7459,7 +7527,7 @@ function usePermissionGroupRepo() {
7459
7527
  name: "text_index"
7460
7528
  },
7461
7529
  {
7462
- key: { app: 1, key: 1 },
7530
+ key: { app: 1, key: 1, label: 1 },
7463
7531
  unique: true,
7464
7532
  name: "unique_permission_group"
7465
7533
  }
@@ -7496,9 +7564,13 @@ function usePermissionGroupRepo() {
7496
7564
  async function updateById(_id, value, session) {
7497
7565
  try {
7498
7566
  _id = new ObjectId21(_id);
7499
- } catch (error) {
7567
+ } catch (error2) {
7500
7568
  throw new BadRequestError41("Invalid ID.");
7501
7569
  }
7570
+ const { error } = schemaPermissionGroupUpdate.validate(value);
7571
+ if (error) {
7572
+ throw new BadRequestError41(`Invalid data: ${error.message}`);
7573
+ }
7502
7574
  try {
7503
7575
  const res = await collection.updateOne(
7504
7576
  { _id },
@@ -7507,13 +7579,13 @@ function usePermissionGroupRepo() {
7507
7579
  );
7508
7580
  delCachedData();
7509
7581
  return res;
7510
- } catch (error) {
7582
+ } catch (error2) {
7511
7583
  logger23.log({
7512
7584
  level: "error",
7513
- message: error.message
7585
+ message: error2.message
7514
7586
  });
7515
- if (error instanceof AppError15) {
7516
- throw error;
7587
+ if (error2 instanceof AppError15) {
7588
+ throw error2;
7517
7589
  } else {
7518
7590
  throw new Error("Failed to update permission group.");
7519
7591
  }
@@ -7524,35 +7596,27 @@ function usePermissionGroupRepo() {
7524
7596
  page = 1,
7525
7597
  limit = 10,
7526
7598
  sort = {},
7527
- school = "",
7599
+ app = "",
7528
7600
  status = "active"
7529
7601
  } = {}) {
7530
7602
  page = page > 0 ? page - 1 : 0;
7531
- const query = {
7532
- status
7533
- };
7534
- sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
7535
- if (search) {
7536
- query.$text = { $search: search };
7537
- }
7538
- if (school) {
7539
- try {
7540
- query.school = new ObjectId21(school);
7541
- } catch (error) {
7542
- throw new BadRequestError41("Invalid school ID.");
7543
- }
7544
- }
7603
+ const query = {};
7545
7604
  const cacheParams = {
7546
7605
  page,
7547
7606
  limit,
7548
7607
  sort: JSON.stringify(sort)
7549
7608
  };
7550
- if (search)
7609
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
7610
+ query.status = status;
7611
+ cacheParams.status = status;
7612
+ if (search) {
7613
+ query.$text = { $search: search };
7551
7614
  cacheParams.search = search;
7552
- if (school)
7553
- cacheParams.school = school;
7554
- if (status !== "active")
7555
- cacheParams.status = status;
7615
+ }
7616
+ if (app) {
7617
+ query.app = app;
7618
+ cacheParams.app = app;
7619
+ }
7556
7620
  const cacheKey = makeCacheKey15(namespace_collection, cacheParams);
7557
7621
  logger23.log({
7558
7622
  level: "info",
@@ -7680,6 +7744,11 @@ function usePermissionGroupRepo() {
7680
7744
  }
7681
7745
 
7682
7746
  // src/resources/permission/permission.group.service.ts
7747
+ import {
7748
+ AppError as AppError16,
7749
+ BadRequestError as BadRequestError42,
7750
+ InternalServerError as InternalServerError22
7751
+ } from "@goweekdays/utils";
7683
7752
  function usePermissionGroupService() {
7684
7753
  const {
7685
7754
  updateById: _updateById,
@@ -7694,7 +7763,7 @@ function usePermissionGroupService() {
7694
7763
  }
7695
7764
  const associatedPermissionsCount = await countByGroup(permission.key);
7696
7765
  if (associatedPermissionsCount > 0) {
7697
- throw new Error(
7766
+ throw new BadRequestError42(
7698
7767
  "Cannot delete Permission Group with associated Permissions."
7699
7768
  );
7700
7769
  }
@@ -7702,7 +7771,11 @@ function usePermissionGroupService() {
7702
7771
  await _deleteById(id);
7703
7772
  return "Permission deleted successfully.";
7704
7773
  } catch (error) {
7705
- throw error;
7774
+ if (error instanceof AppError16) {
7775
+ throw error;
7776
+ } else {
7777
+ throw new InternalServerError22("Failed to delete Permission Group.");
7778
+ }
7706
7779
  }
7707
7780
  }
7708
7781
  return {
@@ -7711,20 +7784,21 @@ function usePermissionGroupService() {
7711
7784
  }
7712
7785
 
7713
7786
  // src/resources/permission/permission.group.controller.ts
7714
- import { BadRequestError as BadRequestError42, logger as logger24 } from "@goweekdays/utils";
7787
+ import { BadRequestError as BadRequestError43, logger as logger24 } from "@goweekdays/utils";
7715
7788
  import Joi24 from "joi";
7716
7789
  function usePermissionGroupController() {
7717
7790
  const {
7718
7791
  getAll: _getAll,
7719
7792
  getById: _getById,
7720
- add: _add
7793
+ add: _add,
7794
+ updateById: _updateById
7721
7795
  } = usePermissionGroupRepo();
7722
7796
  const { deleteById: _deleteById } = usePermissionGroupService();
7723
7797
  async function add(req, res, next) {
7724
7798
  const value = req.body;
7725
7799
  const { error } = schemaPermissionGroup.validate(value);
7726
7800
  if (error) {
7727
- next(new BadRequestError42(error.message));
7801
+ next(new BadRequestError43(error.message));
7728
7802
  logger24.info(`Controller: ${error.message}`);
7729
7803
  return;
7730
7804
  }
@@ -7742,12 +7816,12 @@ function usePermissionGroupController() {
7742
7816
  page: Joi24.number().min(1).optional().allow("", null),
7743
7817
  limit: Joi24.number().min(1).optional().allow("", null),
7744
7818
  search: Joi24.string().optional().allow("", null),
7745
- school: Joi24.string().hex().optional().allow("", null),
7819
+ app: Joi24.string().optional().allow("", null),
7746
7820
  status: Joi24.string().optional().allow("", null)
7747
7821
  });
7748
7822
  const { error } = validation.validate(query);
7749
7823
  if (error) {
7750
- next(new BadRequestError42(error.message));
7824
+ next(new BadRequestError43(error.message));
7751
7825
  return;
7752
7826
  }
7753
7827
  const page = parseInt(req.query.page) ?? 1;
@@ -7761,17 +7835,17 @@ function usePermissionGroupController() {
7761
7835
  sortObj[field] = sortOrder[index] === "desc" ? -1 : 1;
7762
7836
  });
7763
7837
  }
7764
- const status = req.query.status ?? "active";
7765
- const school = req.query.school ?? "";
7838
+ const app = req.query.app ?? "";
7766
7839
  const search = req.query.search ?? "";
7840
+ const status = req.query.status ?? "active";
7767
7841
  try {
7768
7842
  const buildings = await _getAll({
7769
7843
  page,
7770
7844
  limit,
7771
7845
  sort: sortObj,
7772
- status,
7773
- school,
7774
- search
7846
+ app,
7847
+ search,
7848
+ status
7775
7849
  });
7776
7850
  res.json(buildings);
7777
7851
  return;
@@ -7786,7 +7860,7 @@ function usePermissionGroupController() {
7786
7860
  });
7787
7861
  const { error } = validation.validate({ id });
7788
7862
  if (error) {
7789
- next(new BadRequestError42(error.message));
7863
+ next(new BadRequestError43(error.message));
7790
7864
  return;
7791
7865
  }
7792
7866
  try {
@@ -7807,7 +7881,7 @@ function usePermissionGroupController() {
7807
7881
  });
7808
7882
  const { error } = validation.validate({ id });
7809
7883
  if (error) {
7810
- next(new BadRequestError42(error.message));
7884
+ next(new BadRequestError43(error.message));
7811
7885
  return;
7812
7886
  }
7813
7887
  try {
@@ -7818,11 +7892,33 @@ function usePermissionGroupController() {
7818
7892
  next(error2);
7819
7893
  }
7820
7894
  }
7895
+ async function updateById(req, res, next) {
7896
+ const id = req.params.id;
7897
+ const { error: errorId } = Joi24.string().hex().required().validate(id);
7898
+ if (errorId) {
7899
+ next(new BadRequestError43(errorId.message));
7900
+ return;
7901
+ }
7902
+ const payload = req.body;
7903
+ const { error } = schemaPermissionGroupUpdate.validate(payload);
7904
+ if (error) {
7905
+ next(new BadRequestError43(error.message));
7906
+ return;
7907
+ }
7908
+ try {
7909
+ const message = await _updateById(id, payload);
7910
+ res.json(message);
7911
+ return;
7912
+ } catch (error2) {
7913
+ next(error2);
7914
+ }
7915
+ }
7821
7916
  return {
7822
7917
  add,
7823
7918
  getAll,
7824
7919
  getById,
7825
- deleteById
7920
+ deleteById,
7921
+ updateById
7826
7922
  };
7827
7923
  }
7828
7924
  export {
@@ -7879,12 +7975,15 @@ export {
7879
7975
  modelPermission,
7880
7976
  modelPermissionGroup,
7881
7977
  schemaApp,
7978
+ schemaAppUpdate,
7882
7979
  schemaBuilding,
7883
7980
  schemaBuildingUnit,
7884
7981
  schemaOrg,
7885
7982
  schemaPSGC,
7886
7983
  schemaPermission,
7887
7984
  schemaPermissionGroup,
7985
+ schemaPermissionGroupUpdate,
7986
+ schemaPermissionUpdate,
7888
7987
  schemaUpdateOptions,
7889
7988
  transactionSchema,
7890
7989
  useAddressController,