@goweekdays/core 2.1.4 → 2.2.0

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
@@ -1507,6 +1507,43 @@ function useMemberRepo() {
1507
1507
  );
1508
1508
  }
1509
1509
  }
1510
+ async function getByRole(role) {
1511
+ try {
1512
+ role = new ObjectId9(role);
1513
+ } catch (error) {
1514
+ throw new BadRequestError10("Invalid role ID.");
1515
+ }
1516
+ try {
1517
+ const cacheKey = makeCacheKey5(namespace_collection, {
1518
+ role: role.toString()
1519
+ });
1520
+ const cached = await getCache(cacheKey);
1521
+ if (cached) {
1522
+ logger5.log({
1523
+ level: "info",
1524
+ message: `Cache hit for getById member: ${cacheKey}`
1525
+ });
1526
+ return cached;
1527
+ }
1528
+ const data = await collection.findOne({ role });
1529
+ setCache(cacheKey, data, 300).then(() => {
1530
+ logger5.log({
1531
+ level: "info",
1532
+ message: `Cache set for member by role: ${cacheKey}`
1533
+ });
1534
+ }).catch((err) => {
1535
+ logger5.log({
1536
+ level: "error",
1537
+ message: `Failed to set cache for member by role: ${err.message}`
1538
+ });
1539
+ });
1540
+ return data;
1541
+ } catch (error) {
1542
+ throw new InternalServerError5(
1543
+ "Internal server error, failed to retrieve member."
1544
+ );
1545
+ }
1546
+ }
1510
1547
  async function getByUserId(user) {
1511
1548
  try {
1512
1549
  user = new ObjectId9(user);
@@ -1957,6 +1994,7 @@ function useMemberRepo() {
1957
1994
  createTextIndex,
1958
1995
  add,
1959
1996
  getById,
1997
+ getByRole,
1960
1998
  getAll,
1961
1999
  getOrgsByUserId,
1962
2000
  updateStatusByUserId,
@@ -2467,7 +2505,7 @@ function useRoleRepo() {
2467
2505
  throw new InternalServerError7("Failed to retrieve role by user ID.");
2468
2506
  }
2469
2507
  }
2470
- async function getRoleById(_id) {
2508
+ async function getById(_id) {
2471
2509
  try {
2472
2510
  _id = new ObjectId11(_id);
2473
2511
  } catch (error) {
@@ -2481,7 +2519,7 @@ function useRoleRepo() {
2481
2519
  if (cached) {
2482
2520
  logger7.log({
2483
2521
  level: "info",
2484
- message: `Cache hit for getRoleById role: ${cacheKey}`
2522
+ message: `Cache hit for getById role: ${cacheKey}`
2485
2523
  });
2486
2524
  return cached;
2487
2525
  }
@@ -2664,7 +2702,7 @@ function useRoleRepo() {
2664
2702
  throw new InternalServerError7("Failed to update role permissions.");
2665
2703
  }
2666
2704
  }
2667
- async function deleteRole(_id, session) {
2705
+ async function deleteById(_id, session) {
2668
2706
  try {
2669
2707
  _id = new ObjectId11(_id);
2670
2708
  } catch (error) {
@@ -2690,10 +2728,10 @@ function useRoleRepo() {
2690
2728
  addRole,
2691
2729
  getRoles,
2692
2730
  getRoleByUserId,
2693
- getRoleById,
2731
+ getById,
2694
2732
  getRoleByName,
2695
2733
  updateRole,
2696
- deleteRole,
2734
+ deleteById,
2697
2735
  updatePermissionsById,
2698
2736
  delCachedData
2699
2737
  };
@@ -3937,6 +3975,12 @@ function useAppService() {
3937
3975
  description: "Administrative application.",
3938
3976
  type: "default"
3939
3977
  },
3978
+ {
3979
+ code: "org",
3980
+ name: "Organization",
3981
+ description: "Organization application.",
3982
+ type: "standard"
3983
+ },
3940
3984
  {
3941
3985
  code: "marketplace",
3942
3986
  name: "Marketplace",
@@ -7311,7 +7355,7 @@ function useOrgService() {
7311
7355
  try {
7312
7356
  const org = await addOrg(value, session);
7313
7357
  const allPermissions = await getAllPermission({
7314
- app: value.type,
7358
+ app: "org",
7315
7359
  limit: 100
7316
7360
  });
7317
7361
  let permissions = [];
@@ -7969,24 +8013,53 @@ function usePSGCController() {
7969
8013
  };
7970
8014
  }
7971
8015
 
8016
+ // src/resources/role/role.service.ts
8017
+ import {
8018
+ AppError as AppError14,
8019
+ BadRequestError as BadRequestError41,
8020
+ InternalServerError as InternalServerError20
8021
+ } from "@goweekdays/utils";
8022
+ function useRoleService() {
8023
+ const { getByRole } = useMemberRepo();
8024
+ const { deleteById: _deleteById } = useRoleRepo();
8025
+ async function deleteById(id) {
8026
+ try {
8027
+ const role = await getByRole(id);
8028
+ if (role) {
8029
+ throw new BadRequestError41("Cannot delete role assigned to members.");
8030
+ }
8031
+ await _deleteById(id);
8032
+ } catch (error) {
8033
+ if (error instanceof AppError14) {
8034
+ throw error;
8035
+ } else {
8036
+ throw new InternalServerError20("Failed to delete role.");
8037
+ }
8038
+ }
8039
+ }
8040
+ return {
8041
+ deleteById
8042
+ };
8043
+ }
8044
+
7972
8045
  // src/resources/role/role.controller.ts
7973
8046
  import Joi25 from "joi";
7974
- import { BadRequestError as BadRequestError41 } from "@goweekdays/utils";
8047
+ import { BadRequestError as BadRequestError42 } from "@goweekdays/utils";
7975
8048
  function useRoleController() {
7976
8049
  const {
7977
8050
  addRole: _createRole,
7978
- getRoleById: _getRoleById,
8051
+ getById,
7979
8052
  getRoleByUserId: _getRoleByUserId,
7980
8053
  getRoles: _getRoles,
7981
8054
  updateRole: _updateRole,
7982
- deleteRole: _deleteRole,
7983
8055
  updatePermissionsById: _updatePermissionsById
7984
8056
  } = useRoleRepo();
8057
+ const { deleteById: _deleteById } = useRoleService();
7985
8058
  async function createRole(req, res, next) {
7986
8059
  const payload = req.body;
7987
8060
  const { error } = schemaRole.validate(payload);
7988
8061
  if (error) {
7989
- next(new BadRequestError41(error.message));
8062
+ next(new BadRequestError42(error.message));
7990
8063
  return;
7991
8064
  }
7992
8065
  try {
@@ -8012,7 +8085,7 @@ function useRoleController() {
8012
8085
  });
8013
8086
  const { error } = validation.validate({ search, page, limit, type, id });
8014
8087
  if (error) {
8015
- next(new BadRequestError41(error.message));
8088
+ next(new BadRequestError42(error.message));
8016
8089
  return;
8017
8090
  }
8018
8091
  try {
@@ -8030,7 +8103,7 @@ function useRoleController() {
8030
8103
  });
8031
8104
  const { error } = validation.validate({ userId });
8032
8105
  if (error) {
8033
- next(new BadRequestError41(error.message));
8106
+ next(new BadRequestError42(error.message));
8034
8107
  return;
8035
8108
  }
8036
8109
  try {
@@ -8048,11 +8121,11 @@ function useRoleController() {
8048
8121
  });
8049
8122
  const { error } = validation.validate({ _id });
8050
8123
  if (error) {
8051
- next(new BadRequestError41(error.message));
8124
+ next(new BadRequestError42(error.message));
8052
8125
  return;
8053
8126
  }
8054
8127
  try {
8055
- const data = await _getRoleById(_id);
8128
+ const data = await getById(_id);
8056
8129
  res.json(data);
8057
8130
  return;
8058
8131
  } catch (error2) {
@@ -8070,7 +8143,7 @@ function useRoleController() {
8070
8143
  });
8071
8144
  const { error } = validation.validate({ _id, name, permissions });
8072
8145
  if (error) {
8073
- next(new BadRequestError41(error.message));
8146
+ next(new BadRequestError42(error.message));
8074
8147
  return;
8075
8148
  }
8076
8149
  try {
@@ -8090,7 +8163,7 @@ function useRoleController() {
8090
8163
  });
8091
8164
  const { error } = validation.validate({ _id, permissions });
8092
8165
  if (error) {
8093
- next(new BadRequestError41(error.message));
8166
+ next(new BadRequestError42(error.message));
8094
8167
  return;
8095
8168
  }
8096
8169
  try {
@@ -8108,11 +8181,11 @@ function useRoleController() {
8108
8181
  });
8109
8182
  const { error } = validation.validate({ _id });
8110
8183
  if (error) {
8111
- next(new BadRequestError41(error.message));
8184
+ next(new BadRequestError42(error.message));
8112
8185
  return;
8113
8186
  }
8114
8187
  try {
8115
- const message = await _deleteRole(_id);
8188
+ const message = await _deleteById(_id);
8116
8189
  res.json({ message });
8117
8190
  return;
8118
8191
  } catch (error2) {
@@ -8131,7 +8204,7 @@ function useRoleController() {
8131
8204
  }
8132
8205
 
8133
8206
  // src/resources/utils/github.service.ts
8134
- import { AppError as AppError14, BadRequestError as BadRequestError42 } from "@goweekdays/utils";
8207
+ import { AppError as AppError15, BadRequestError as BadRequestError43 } from "@goweekdays/utils";
8135
8208
  import { Octokit } from "@octokit/rest";
8136
8209
  import _sodium from "libsodium-wrappers";
8137
8210
  function useGitHubService() {
@@ -8145,23 +8218,23 @@ function useGitHubService() {
8145
8218
  try {
8146
8219
  const { data: repoData } = await octokit.repos.get({ owner, repo });
8147
8220
  if (!repoData.permissions?.admin) {
8148
- throw new BadRequestError42(
8221
+ throw new BadRequestError43(
8149
8222
  "You do not have admin access to this repository."
8150
8223
  );
8151
8224
  }
8152
8225
  } catch (error) {
8153
8226
  if (error.status === 404) {
8154
- throw new BadRequestError42(
8227
+ throw new BadRequestError43(
8155
8228
  "Repository not found or you don't have access to it."
8156
8229
  );
8157
8230
  } else if (error.status === 401) {
8158
- throw new BadRequestError42(
8231
+ throw new BadRequestError43(
8159
8232
  "Invalid GitHub token or insufficient permissions."
8160
8233
  );
8161
8234
  } else if (error.message.includes("admin access")) {
8162
8235
  throw error;
8163
8236
  } else {
8164
- throw new BadRequestError42(
8237
+ throw new BadRequestError43(
8165
8238
  `Failed to check repository permissions: ${error.message}`
8166
8239
  );
8167
8240
  }
@@ -8210,7 +8283,7 @@ function useGitHubService() {
8210
8283
  key_id: publicKeyRes.key_id
8211
8284
  });
8212
8285
  } catch (encryptionError) {
8213
- throw new BadRequestError42(
8286
+ throw new BadRequestError43(
8214
8287
  `Failed to encrypt secret '${key}': ${encryptionError.message}`
8215
8288
  );
8216
8289
  }
@@ -8240,22 +8313,22 @@ function useGitHubService() {
8240
8313
  }
8241
8314
  return `Successfully set ${lines.length} ${type} variables/secrets in environment '${environment}'`;
8242
8315
  } catch (error) {
8243
- if (error instanceof AppError14)
8316
+ if (error instanceof AppError15)
8244
8317
  throw error;
8245
8318
  if (error.status === 422) {
8246
- throw new BadRequestError42(
8319
+ throw new BadRequestError43(
8247
8320
  `GitHub API validation error: ${error.message}`
8248
8321
  );
8249
8322
  } else if (error.status === 404) {
8250
- throw new BadRequestError42("Environment or repository not found.");
8323
+ throw new BadRequestError43("Environment or repository not found.");
8251
8324
  } else if (error.status === 403) {
8252
- throw new BadRequestError42(
8325
+ throw new BadRequestError43(
8253
8326
  "Forbidden: Insufficient permissions or rate limit exceeded."
8254
8327
  );
8255
8328
  } else if (error.message.includes("admin access") || error.message.includes("permissions")) {
8256
8329
  throw error;
8257
8330
  } else {
8258
- throw new BadRequestError42(
8331
+ throw new BadRequestError43(
8259
8332
  `Failed to set GitHub variables: ${error.message}`
8260
8333
  );
8261
8334
  }
@@ -8269,9 +8342,9 @@ function useGitHubService() {
8269
8342
  // src/resources/utils/util.controller.ts
8270
8343
  import Joi26 from "joi";
8271
8344
  import {
8272
- AppError as AppError15,
8273
- BadRequestError as BadRequestError43,
8274
- InternalServerError as InternalServerError21,
8345
+ AppError as AppError16,
8346
+ BadRequestError as BadRequestError44,
8347
+ InternalServerError as InternalServerError22,
8275
8348
  logger as logger26
8276
8349
  } from "@goweekdays/utils";
8277
8350
  function useUtilController() {
@@ -8290,7 +8363,7 @@ function useUtilController() {
8290
8363
  });
8291
8364
  } catch (error) {
8292
8365
  logger26.error("Health check failed", { error: error.message });
8293
- next(new InternalServerError21("Health check failed"));
8366
+ next(new InternalServerError22("Health check failed"));
8294
8367
  }
8295
8368
  }
8296
8369
  async function setGitHubVariables(req, res, next) {
@@ -8327,13 +8400,13 @@ function useUtilController() {
8327
8400
  keyValues
8328
8401
  });
8329
8402
  if (error) {
8330
- next(new BadRequestError43(error.message));
8403
+ next(new BadRequestError44(error.message));
8331
8404
  return;
8332
8405
  }
8333
8406
  const repoUrlPattern = /github\.com[:\/]([^\/]+)\/(.+)\.git$/;
8334
8407
  if (!repoUrlPattern.test(repoUrl)) {
8335
8408
  next(
8336
- new BadRequestError43(
8409
+ new BadRequestError44(
8337
8410
  "Invalid GitHub repository URL format. Expected format: https://github.com/owner/repo.git"
8338
8411
  )
8339
8412
  );
@@ -8345,7 +8418,7 @@ function useUtilController() {
8345
8418
  );
8346
8419
  if (invalidLines.length > 0) {
8347
8420
  next(
8348
- new BadRequestError43(
8421
+ new BadRequestError44(
8349
8422
  "Invalid key-value format. Each pair should be in format: KEY=value. Pairs should be separated by semicolons."
8350
8423
  )
8351
8424
  );
@@ -8380,11 +8453,11 @@ function useUtilController() {
8380
8453
  error: error.message,
8381
8454
  stack: error.stack
8382
8455
  });
8383
- if (error instanceof AppError15) {
8456
+ if (error instanceof AppError16) {
8384
8457
  next(error);
8385
8458
  } else {
8386
8459
  next(
8387
- new InternalServerError21(
8460
+ new InternalServerError22(
8388
8461
  `Failed to set GitHub variables: ${error.message}`
8389
8462
  )
8390
8463
  );
@@ -8422,9 +8495,9 @@ var transactionSchema = Joi27.object({
8422
8495
 
8423
8496
  // src/resources/verification/verification.controller.ts
8424
8497
  import {
8425
- AppError as AppError16,
8426
- BadRequestError as BadRequestError44,
8427
- InternalServerError as InternalServerError22
8498
+ AppError as AppError17,
8499
+ BadRequestError as BadRequestError45,
8500
+ InternalServerError as InternalServerError23
8428
8501
  } from "@goweekdays/utils";
8429
8502
  import Joi28 from "joi";
8430
8503
  function useVerificationController() {
@@ -8446,7 +8519,7 @@ function useVerificationController() {
8446
8519
  });
8447
8520
  const { error } = validation.validate(req.body);
8448
8521
  if (error) {
8449
- next(new BadRequestError44(error.message));
8522
+ next(new BadRequestError45(error.message));
8450
8523
  return;
8451
8524
  }
8452
8525
  const email = req.body.email ?? "";
@@ -8477,7 +8550,7 @@ function useVerificationController() {
8477
8550
  const validation = Joi28.string().email().required();
8478
8551
  const { error } = validation.validate(email);
8479
8552
  if (error) {
8480
- next(new BadRequestError44(error.message));
8553
+ next(new BadRequestError45(error.message));
8481
8554
  return;
8482
8555
  }
8483
8556
  try {
@@ -8487,10 +8560,10 @@ function useVerificationController() {
8487
8560
  });
8488
8561
  return;
8489
8562
  } catch (error2) {
8490
- if (error2 instanceof AppError16) {
8563
+ if (error2 instanceof AppError17) {
8491
8564
  next(error2);
8492
8565
  } else {
8493
- next(new InternalServerError22("An unexpected error occurred"));
8566
+ next(new InternalServerError23("An unexpected error occurred"));
8494
8567
  }
8495
8568
  }
8496
8569
  }
@@ -8505,7 +8578,7 @@ function useVerificationController() {
8505
8578
  });
8506
8579
  const { error } = validation.validate(req.query);
8507
8580
  if (error) {
8508
- next(new BadRequestError44(error.message));
8581
+ next(new BadRequestError45(error.message));
8509
8582
  return;
8510
8583
  }
8511
8584
  const status = req.query.status ?? "";
@@ -8539,7 +8612,7 @@ function useVerificationController() {
8539
8612
  const validation = Joi28.string().hex().required();
8540
8613
  const { error } = validation.validate(id);
8541
8614
  if (error) {
8542
- next(new BadRequestError44(error.message));
8615
+ next(new BadRequestError45(error.message));
8543
8616
  return;
8544
8617
  }
8545
8618
  try {
@@ -8555,7 +8628,7 @@ function useVerificationController() {
8555
8628
  const validation = Joi28.string().hex().required();
8556
8629
  const { error } = validation.validate(otpId);
8557
8630
  if (error) {
8558
- next(new BadRequestError44(error.message));
8631
+ next(new BadRequestError45(error.message));
8559
8632
  return;
8560
8633
  }
8561
8634
  try {
@@ -8675,6 +8748,7 @@ export {
8675
8748
  usePermissionService,
8676
8749
  useRoleController,
8677
8750
  useRoleRepo,
8751
+ useRoleService,
8678
8752
  useUserController,
8679
8753
  useUserRepo,
8680
8754
  useUserService,