@goweekdays/core 2.1.4 → 2.2.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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +16 -12
- package/dist/index.js +714 -589
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +452 -324
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51,11 +51,9 @@ __export(src_exports, {
|
|
|
51
51
|
MMember: () => MMember,
|
|
52
52
|
MONGO_DB: () => MONGO_DB,
|
|
53
53
|
MONGO_URI: () => MONGO_URI,
|
|
54
|
-
MOrg: () => MOrg,
|
|
55
54
|
MUser: () => MUser,
|
|
56
55
|
MUserRole: () => MUserRole,
|
|
57
56
|
MVerification: () => MVerification,
|
|
58
|
-
OrgTypes: () => OrgTypes,
|
|
59
57
|
PAYPAL_API_URL: () => PAYPAL_API_URL,
|
|
60
58
|
PAYPAL_CLIENT_ID: () => PAYPAL_CLIENT_ID,
|
|
61
59
|
PAYPAL_CLIENT_SECRET: () => PAYPAL_CLIENT_SECRET,
|
|
@@ -78,6 +76,8 @@ __export(src_exports, {
|
|
|
78
76
|
addressSchema: () => addressSchema,
|
|
79
77
|
isDev: () => isDev,
|
|
80
78
|
modelApp: () => modelApp,
|
|
79
|
+
modelMember: () => modelMember,
|
|
80
|
+
modelOrg: () => modelOrg,
|
|
81
81
|
modelPSGC: () => modelPSGC,
|
|
82
82
|
modelPermission: () => modelPermission,
|
|
83
83
|
modelPermissionGroup: () => modelPermissionGroup,
|
|
@@ -86,6 +86,7 @@ __export(src_exports, {
|
|
|
86
86
|
schemaAppUpdate: () => schemaAppUpdate,
|
|
87
87
|
schemaBuilding: () => schemaBuilding,
|
|
88
88
|
schemaBuildingUnit: () => schemaBuildingUnit,
|
|
89
|
+
schemaMember: () => schemaMember,
|
|
89
90
|
schemaOrg: () => schemaOrg,
|
|
90
91
|
schemaPSGC: () => schemaPSGC,
|
|
91
92
|
schemaPermission: () => schemaPermission,
|
|
@@ -129,6 +130,7 @@ __export(src_exports, {
|
|
|
129
130
|
usePermissionService: () => usePermissionService,
|
|
130
131
|
useRoleController: () => useRoleController,
|
|
131
132
|
useRoleRepo: () => useRoleRepo,
|
|
133
|
+
useRoleService: () => useRoleService,
|
|
132
134
|
useUserController: () => useUserController,
|
|
133
135
|
useUserRepo: () => useUserRepo,
|
|
134
136
|
useUserService: () => useUserService,
|
|
@@ -952,10 +954,10 @@ function useAuthService() {
|
|
|
952
954
|
|
|
953
955
|
// src/resources/auth/auth.controller.ts
|
|
954
956
|
var import_joi14 = __toESM(require("joi"));
|
|
955
|
-
var
|
|
957
|
+
var import_utils26 = require("@goweekdays/utils");
|
|
956
958
|
|
|
957
959
|
// src/resources/user/user.service.ts
|
|
958
|
-
var
|
|
960
|
+
var import_utils25 = require("@goweekdays/utils");
|
|
959
961
|
|
|
960
962
|
// src/resources/file/file.repository.ts
|
|
961
963
|
var import_utils6 = require("@goweekdays/utils");
|
|
@@ -1458,6 +1460,56 @@ function MMember(value) {
|
|
|
1458
1460
|
deletedAt: ""
|
|
1459
1461
|
};
|
|
1460
1462
|
}
|
|
1463
|
+
var schemaMember = import_joi3.default.object({
|
|
1464
|
+
org: import_joi3.default.string().hex().optional().allow("", null),
|
|
1465
|
+
orgName: import_joi3.default.string().optional().allow("", null),
|
|
1466
|
+
name: import_joi3.default.string().required(),
|
|
1467
|
+
user: import_joi3.default.string().hex().required(),
|
|
1468
|
+
role: import_joi3.default.string().hex().required(),
|
|
1469
|
+
roleName: import_joi3.default.string().optional().allow("", null),
|
|
1470
|
+
type: import_joi3.default.string().required()
|
|
1471
|
+
});
|
|
1472
|
+
function modelMember(value) {
|
|
1473
|
+
const { error } = schemaMember.validate(value);
|
|
1474
|
+
if (error) {
|
|
1475
|
+
throw new import_utils9.BadRequestError(error.message);
|
|
1476
|
+
}
|
|
1477
|
+
if (value.org) {
|
|
1478
|
+
try {
|
|
1479
|
+
value.org = new import_mongodb8.ObjectId(value.org);
|
|
1480
|
+
} catch (error2) {
|
|
1481
|
+
throw new import_utils9.BadRequestError("Invalid org ID.");
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
if (value.user) {
|
|
1485
|
+
try {
|
|
1486
|
+
value.user = new import_mongodb8.ObjectId(value.user);
|
|
1487
|
+
} catch (error2) {
|
|
1488
|
+
throw new import_utils9.BadRequestError("Invalid user ID.");
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
if (value.role) {
|
|
1492
|
+
try {
|
|
1493
|
+
value.role = new import_mongodb8.ObjectId(value.role);
|
|
1494
|
+
} catch (error2) {
|
|
1495
|
+
throw new import_utils9.BadRequestError("Invalid role ID.");
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
return {
|
|
1499
|
+
_id: value._id,
|
|
1500
|
+
org: value.org ?? "",
|
|
1501
|
+
orgName: value.orgName ?? "",
|
|
1502
|
+
name: value.name,
|
|
1503
|
+
user: value.user,
|
|
1504
|
+
type: value.type,
|
|
1505
|
+
role: value.role,
|
|
1506
|
+
roleName: value.roleName ?? "",
|
|
1507
|
+
status: value.status ?? "active",
|
|
1508
|
+
createdAt: value.createdAt || /* @__PURE__ */ new Date(),
|
|
1509
|
+
updatedAt: "",
|
|
1510
|
+
deletedAt: ""
|
|
1511
|
+
};
|
|
1512
|
+
}
|
|
1461
1513
|
|
|
1462
1514
|
// src/resources/member/member.repository.ts
|
|
1463
1515
|
var import_mongodb9 = require("mongodb");
|
|
@@ -1526,7 +1578,7 @@ function useMemberRepo() {
|
|
|
1526
1578
|
}
|
|
1527
1579
|
async function add(value, session) {
|
|
1528
1580
|
try {
|
|
1529
|
-
value =
|
|
1581
|
+
value = modelMember(value);
|
|
1530
1582
|
await collection.insertOne(value, { session });
|
|
1531
1583
|
delCachedData();
|
|
1532
1584
|
return "Successfully added member.";
|
|
@@ -1579,6 +1631,43 @@ function useMemberRepo() {
|
|
|
1579
1631
|
);
|
|
1580
1632
|
}
|
|
1581
1633
|
}
|
|
1634
|
+
async function getByRole(role) {
|
|
1635
|
+
try {
|
|
1636
|
+
role = new import_mongodb9.ObjectId(role);
|
|
1637
|
+
} catch (error) {
|
|
1638
|
+
throw new import_utils10.BadRequestError("Invalid role ID.");
|
|
1639
|
+
}
|
|
1640
|
+
try {
|
|
1641
|
+
const cacheKey = (0, import_utils10.makeCacheKey)(namespace_collection, {
|
|
1642
|
+
role: role.toString()
|
|
1643
|
+
});
|
|
1644
|
+
const cached = await getCache(cacheKey);
|
|
1645
|
+
if (cached) {
|
|
1646
|
+
import_utils10.logger.log({
|
|
1647
|
+
level: "info",
|
|
1648
|
+
message: `Cache hit for getById member: ${cacheKey}`
|
|
1649
|
+
});
|
|
1650
|
+
return cached;
|
|
1651
|
+
}
|
|
1652
|
+
const data = await collection.findOne({ role });
|
|
1653
|
+
setCache(cacheKey, data, 300).then(() => {
|
|
1654
|
+
import_utils10.logger.log({
|
|
1655
|
+
level: "info",
|
|
1656
|
+
message: `Cache set for member by role: ${cacheKey}`
|
|
1657
|
+
});
|
|
1658
|
+
}).catch((err) => {
|
|
1659
|
+
import_utils10.logger.log({
|
|
1660
|
+
level: "error",
|
|
1661
|
+
message: `Failed to set cache for member by role: ${err.message}`
|
|
1662
|
+
});
|
|
1663
|
+
});
|
|
1664
|
+
return data;
|
|
1665
|
+
} catch (error) {
|
|
1666
|
+
throw new import_utils10.InternalServerError(
|
|
1667
|
+
"Internal server error, failed to retrieve member."
|
|
1668
|
+
);
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1582
1671
|
async function getByUserId(user) {
|
|
1583
1672
|
try {
|
|
1584
1673
|
user = new import_mongodb9.ObjectId(user);
|
|
@@ -2029,6 +2118,7 @@ function useMemberRepo() {
|
|
|
2029
2118
|
createTextIndex,
|
|
2030
2119
|
add,
|
|
2031
2120
|
getById,
|
|
2121
|
+
getByRole,
|
|
2032
2122
|
getAll,
|
|
2033
2123
|
getOrgsByUserId,
|
|
2034
2124
|
updateStatusByUserId,
|
|
@@ -2375,42 +2465,43 @@ var import_multer = require("multer");
|
|
|
2375
2465
|
var import_mongodb15 = require("mongodb");
|
|
2376
2466
|
|
|
2377
2467
|
// src/resources/role/role.repository.ts
|
|
2378
|
-
var
|
|
2468
|
+
var import_utils13 = require("@goweekdays/utils");
|
|
2379
2469
|
|
|
2380
2470
|
// src/resources/role/role.model.ts
|
|
2471
|
+
var import_utils12 = require("@goweekdays/utils");
|
|
2381
2472
|
var import_joi4 = __toESM(require("joi"));
|
|
2382
2473
|
var import_mongodb10 = require("mongodb");
|
|
2383
2474
|
var schemaRole = import_joi4.default.object({
|
|
2384
2475
|
name: import_joi4.default.string().required(),
|
|
2385
2476
|
description: import_joi4.default.string().max(1024).optional().allow("", null),
|
|
2386
2477
|
permissions: import_joi4.default.array().items(import_joi4.default.string()).required(),
|
|
2387
|
-
|
|
2388
|
-
|
|
2478
|
+
org: import_joi4.default.string().hex().optional().allow("", null),
|
|
2479
|
+
createdBy: import_joi4.default.string().hex().required()
|
|
2389
2480
|
});
|
|
2390
2481
|
function modelRole(value) {
|
|
2391
2482
|
const { error } = schemaRole.validate(value);
|
|
2392
2483
|
if (error) {
|
|
2393
|
-
throw new
|
|
2484
|
+
throw new import_utils12.BadRequestError(error.message);
|
|
2394
2485
|
}
|
|
2395
2486
|
if (value._id && typeof value._id === "string") {
|
|
2396
2487
|
try {
|
|
2397
2488
|
value._id = new import_mongodb10.ObjectId(value._id);
|
|
2398
2489
|
} catch (error2) {
|
|
2399
|
-
throw new
|
|
2490
|
+
throw new import_utils12.BadRequestError("Invalid _id.");
|
|
2400
2491
|
}
|
|
2401
2492
|
}
|
|
2402
2493
|
if (value.org && typeof value.org === "string" && value.org.length === 24) {
|
|
2403
2494
|
try {
|
|
2404
2495
|
value.org = new import_mongodb10.ObjectId(value.org);
|
|
2405
2496
|
} catch (error2) {
|
|
2406
|
-
throw new
|
|
2497
|
+
throw new import_utils12.BadRequestError("Invalid org.");
|
|
2407
2498
|
}
|
|
2408
2499
|
}
|
|
2409
2500
|
if (value.createdBy && typeof value.createdBy === "string" && value.createdBy.length === 24) {
|
|
2410
2501
|
try {
|
|
2411
2502
|
value.createdBy = new import_mongodb10.ObjectId(value.createdBy);
|
|
2412
2503
|
} catch (error2) {
|
|
2413
|
-
throw new
|
|
2504
|
+
throw new import_utils12.BadRequestError("Invalid createdBy.");
|
|
2414
2505
|
}
|
|
2415
2506
|
}
|
|
2416
2507
|
return {
|
|
@@ -2418,7 +2509,6 @@ function modelRole(value) {
|
|
|
2418
2509
|
name: value.name,
|
|
2419
2510
|
description: value.description ?? "",
|
|
2420
2511
|
permissions: value.permissions,
|
|
2421
|
-
type: value.type,
|
|
2422
2512
|
org: value.org,
|
|
2423
2513
|
status: value.status ?? "active",
|
|
2424
2514
|
default: value.default ?? false,
|
|
@@ -2432,21 +2522,21 @@ function modelRole(value) {
|
|
|
2432
2522
|
// src/resources/role/role.repository.ts
|
|
2433
2523
|
var import_mongodb11 = require("mongodb");
|
|
2434
2524
|
function useRoleRepo() {
|
|
2435
|
-
const db =
|
|
2525
|
+
const db = import_utils13.useAtlas.getDb();
|
|
2436
2526
|
if (!db) {
|
|
2437
|
-
throw new
|
|
2527
|
+
throw new import_utils13.InternalServerError("Unable to connect to server.");
|
|
2438
2528
|
}
|
|
2439
2529
|
const namespace_collection = "roles";
|
|
2440
2530
|
const collection = db.collection(namespace_collection);
|
|
2441
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
2531
|
+
const { getCache, setCache, delNamespace } = (0, import_utils13.useCache)(namespace_collection);
|
|
2442
2532
|
function delCachedData() {
|
|
2443
2533
|
delNamespace().then(() => {
|
|
2444
|
-
|
|
2534
|
+
import_utils13.logger.log({
|
|
2445
2535
|
level: "info",
|
|
2446
2536
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
2447
2537
|
});
|
|
2448
2538
|
}).catch((err) => {
|
|
2449
|
-
|
|
2539
|
+
import_utils13.logger.log({
|
|
2450
2540
|
level: "error",
|
|
2451
2541
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
2452
2542
|
});
|
|
@@ -2459,14 +2549,14 @@ function useRoleRepo() {
|
|
|
2459
2549
|
await collection.createIndex({ status: 1 });
|
|
2460
2550
|
await collection.createIndex({ id: 1 });
|
|
2461
2551
|
} catch (error) {
|
|
2462
|
-
throw new
|
|
2552
|
+
throw new import_utils13.InternalServerError("Failed to create index on role.");
|
|
2463
2553
|
}
|
|
2464
2554
|
}
|
|
2465
2555
|
async function createTextIndex() {
|
|
2466
2556
|
try {
|
|
2467
2557
|
await collection.createIndex({ name: "text" });
|
|
2468
2558
|
} catch (error) {
|
|
2469
|
-
throw new
|
|
2559
|
+
throw new import_utils13.InternalServerError("Failed to create text index on role.");
|
|
2470
2560
|
}
|
|
2471
2561
|
}
|
|
2472
2562
|
async function createUniqueIndex() {
|
|
@@ -2476,7 +2566,7 @@ function useRoleRepo() {
|
|
|
2476
2566
|
{ unique: true }
|
|
2477
2567
|
);
|
|
2478
2568
|
} catch (error) {
|
|
2479
|
-
throw new
|
|
2569
|
+
throw new import_utils13.InternalServerError("Failed to create unique index on role.");
|
|
2480
2570
|
}
|
|
2481
2571
|
}
|
|
2482
2572
|
async function addRole(value, session, clearCache = true) {
|
|
@@ -2488,27 +2578,31 @@ function useRoleRepo() {
|
|
|
2488
2578
|
}
|
|
2489
2579
|
return res.insertedId;
|
|
2490
2580
|
} catch (error) {
|
|
2491
|
-
|
|
2581
|
+
import_utils13.logger.log({ level: "error", message: `${error}` });
|
|
2492
2582
|
const isDuplicated = error.message.includes("duplicate");
|
|
2493
2583
|
if (isDuplicated) {
|
|
2494
|
-
throw new
|
|
2584
|
+
throw new import_utils13.BadRequestError("Role already exists");
|
|
2585
|
+
}
|
|
2586
|
+
if (error instanceof import_utils13.AppError) {
|
|
2587
|
+
throw error;
|
|
2588
|
+
} else {
|
|
2589
|
+
throw new import_utils13.InternalServerError("Failed to create role.");
|
|
2495
2590
|
}
|
|
2496
|
-
throw new import_utils12.InternalServerError("Failed to create role.");
|
|
2497
2591
|
}
|
|
2498
2592
|
}
|
|
2499
2593
|
async function getRoleByUserId(value) {
|
|
2500
2594
|
try {
|
|
2501
2595
|
value = new import_mongodb11.ObjectId(value);
|
|
2502
2596
|
} catch (error) {
|
|
2503
|
-
throw new
|
|
2597
|
+
throw new import_utils13.BadRequestError("Invalid user ID.");
|
|
2504
2598
|
}
|
|
2505
2599
|
try {
|
|
2506
|
-
const cacheKey = (0,
|
|
2600
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, {
|
|
2507
2601
|
user: String(value)
|
|
2508
2602
|
});
|
|
2509
2603
|
const cached = await getCache(cacheKey);
|
|
2510
2604
|
if (cached) {
|
|
2511
|
-
|
|
2605
|
+
import_utils13.logger.log({
|
|
2512
2606
|
level: "info",
|
|
2513
2607
|
message: `Cache hit for getRoleByUserId role: ${cacheKey}`
|
|
2514
2608
|
});
|
|
@@ -2516,67 +2610,67 @@ function useRoleRepo() {
|
|
|
2516
2610
|
}
|
|
2517
2611
|
const data = await collection.findOne({ user: value });
|
|
2518
2612
|
setCache(cacheKey, data, 300).then(() => {
|
|
2519
|
-
|
|
2613
|
+
import_utils13.logger.log({
|
|
2520
2614
|
level: "info",
|
|
2521
2615
|
message: `Cache set for role by user ID: ${cacheKey}`
|
|
2522
2616
|
});
|
|
2523
2617
|
}).catch((err) => {
|
|
2524
|
-
|
|
2618
|
+
import_utils13.logger.log({
|
|
2525
2619
|
level: "error",
|
|
2526
2620
|
message: `Failed to set cache for role by user ID: ${err.message}`
|
|
2527
2621
|
});
|
|
2528
2622
|
});
|
|
2529
2623
|
return data;
|
|
2530
2624
|
} catch (error) {
|
|
2531
|
-
throw new
|
|
2625
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by user ID.");
|
|
2532
2626
|
}
|
|
2533
2627
|
}
|
|
2534
|
-
async function
|
|
2628
|
+
async function getById(_id) {
|
|
2535
2629
|
try {
|
|
2536
2630
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2537
2631
|
} catch (error) {
|
|
2538
|
-
throw new
|
|
2632
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
2539
2633
|
}
|
|
2540
2634
|
try {
|
|
2541
|
-
const cacheKey = (0,
|
|
2635
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, {
|
|
2542
2636
|
id: _id.toString()
|
|
2543
2637
|
});
|
|
2544
2638
|
const cached = await getCache(cacheKey);
|
|
2545
2639
|
if (cached) {
|
|
2546
|
-
|
|
2640
|
+
import_utils13.logger.log({
|
|
2547
2641
|
level: "info",
|
|
2548
|
-
message: `Cache hit for
|
|
2642
|
+
message: `Cache hit for getById role: ${cacheKey}`
|
|
2549
2643
|
});
|
|
2550
2644
|
return cached;
|
|
2551
2645
|
}
|
|
2552
2646
|
const data = await collection.findOne({ _id });
|
|
2553
2647
|
setCache(cacheKey, data, 300).then(() => {
|
|
2554
|
-
|
|
2648
|
+
import_utils13.logger.log({
|
|
2555
2649
|
level: "info",
|
|
2556
2650
|
message: `Cache set for role by id: ${cacheKey}`
|
|
2557
2651
|
});
|
|
2558
2652
|
}).catch((err) => {
|
|
2559
|
-
|
|
2653
|
+
import_utils13.logger.log({
|
|
2560
2654
|
level: "error",
|
|
2561
2655
|
message: `Failed to set cache for role by id: ${err.message}`
|
|
2562
2656
|
});
|
|
2563
2657
|
});
|
|
2564
2658
|
return data;
|
|
2565
2659
|
} catch (error) {
|
|
2566
|
-
throw new
|
|
2660
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by ID.");
|
|
2567
2661
|
}
|
|
2568
2662
|
}
|
|
2569
2663
|
async function getRoleByName(name) {
|
|
2570
2664
|
if (!name) {
|
|
2571
|
-
throw new
|
|
2665
|
+
throw new import_utils13.BadRequestError("Role name is required.");
|
|
2572
2666
|
}
|
|
2573
2667
|
try {
|
|
2574
|
-
const cacheKey = (0,
|
|
2668
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, {
|
|
2575
2669
|
name
|
|
2576
2670
|
});
|
|
2577
2671
|
const cached = await getCache(cacheKey);
|
|
2578
2672
|
if (cached) {
|
|
2579
|
-
|
|
2673
|
+
import_utils13.logger.log({
|
|
2580
2674
|
level: "info",
|
|
2581
2675
|
message: `Cache hit for getRoleByName role: ${cacheKey}`
|
|
2582
2676
|
});
|
|
@@ -2584,19 +2678,19 @@ function useRoleRepo() {
|
|
|
2584
2678
|
}
|
|
2585
2679
|
const data = await collection.findOne({ name });
|
|
2586
2680
|
setCache(cacheKey, data, 300).then(() => {
|
|
2587
|
-
|
|
2681
|
+
import_utils13.logger.log({
|
|
2588
2682
|
level: "info",
|
|
2589
2683
|
message: `Cache set for role by name: ${cacheKey}`
|
|
2590
2684
|
});
|
|
2591
2685
|
}).catch((err) => {
|
|
2592
|
-
|
|
2686
|
+
import_utils13.logger.log({
|
|
2593
2687
|
level: "error",
|
|
2594
2688
|
message: `Failed to set cache for role by name: ${err.message}`
|
|
2595
2689
|
});
|
|
2596
2690
|
});
|
|
2597
2691
|
return data;
|
|
2598
2692
|
} catch (error) {
|
|
2599
|
-
throw new
|
|
2693
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by name.");
|
|
2600
2694
|
}
|
|
2601
2695
|
}
|
|
2602
2696
|
async function getRoles({
|
|
@@ -2614,7 +2708,7 @@ function useRoleRepo() {
|
|
|
2614
2708
|
try {
|
|
2615
2709
|
id = new import_mongodb11.ObjectId(id);
|
|
2616
2710
|
} catch (error) {
|
|
2617
|
-
throw new
|
|
2711
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
2618
2712
|
}
|
|
2619
2713
|
}
|
|
2620
2714
|
const query = { status: "active" };
|
|
@@ -2639,10 +2733,10 @@ function useRoleRepo() {
|
|
|
2639
2733
|
}
|
|
2640
2734
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
2641
2735
|
cacheKeyOptions.sort = JSON.stringify(sort);
|
|
2642
|
-
const cacheKey = (0,
|
|
2736
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
2643
2737
|
const cached = await getCache(cacheKey);
|
|
2644
2738
|
if (cached) {
|
|
2645
|
-
|
|
2739
|
+
import_utils13.logger.log({
|
|
2646
2740
|
level: "info",
|
|
2647
2741
|
message: `Cache hit for getRoles: ${cacheKey}`
|
|
2648
2742
|
});
|
|
@@ -2656,32 +2750,32 @@ function useRoleRepo() {
|
|
|
2656
2750
|
{ $limit: limit }
|
|
2657
2751
|
]).toArray();
|
|
2658
2752
|
const length = await collection.countDocuments(query);
|
|
2659
|
-
const data = (0,
|
|
2753
|
+
const data = (0, import_utils13.paginate)(items, page, limit, length);
|
|
2660
2754
|
setCache(cacheKey, data, 600).then(() => {
|
|
2661
|
-
|
|
2755
|
+
import_utils13.logger.log({
|
|
2662
2756
|
level: "info",
|
|
2663
2757
|
message: `Cache set for getRoles: ${cacheKey}`
|
|
2664
2758
|
});
|
|
2665
2759
|
}).catch((err) => {
|
|
2666
|
-
|
|
2760
|
+
import_utils13.logger.log({
|
|
2667
2761
|
level: "error",
|
|
2668
2762
|
message: `Failed to set cache for getRoles: ${err.message}`
|
|
2669
2763
|
});
|
|
2670
2764
|
});
|
|
2671
2765
|
return data;
|
|
2672
2766
|
} catch (error) {
|
|
2673
|
-
|
|
2767
|
+
import_utils13.logger.log({ level: "error", message: `${error}` });
|
|
2674
2768
|
throw error;
|
|
2675
2769
|
}
|
|
2676
2770
|
}
|
|
2677
2771
|
async function updateRole(_id, value, session) {
|
|
2678
2772
|
if (!_id) {
|
|
2679
|
-
throw new
|
|
2773
|
+
throw new import_utils13.BadRequestError("Role ID is required.");
|
|
2680
2774
|
}
|
|
2681
2775
|
try {
|
|
2682
2776
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2683
2777
|
} catch (error) {
|
|
2684
|
-
throw new
|
|
2778
|
+
throw new import_utils13.BadRequestError("Invalid role ID.");
|
|
2685
2779
|
}
|
|
2686
2780
|
if (!value.name) {
|
|
2687
2781
|
delete value.name;
|
|
@@ -2695,26 +2789,26 @@ function useRoleRepo() {
|
|
|
2695
2789
|
delCachedData();
|
|
2696
2790
|
return "Successfully updated role.";
|
|
2697
2791
|
} catch (error) {
|
|
2698
|
-
throw new
|
|
2792
|
+
throw new import_utils13.InternalServerError("Failed to update role.");
|
|
2699
2793
|
}
|
|
2700
2794
|
} else {
|
|
2701
|
-
throw new
|
|
2795
|
+
throw new import_utils13.BadRequestError("No fields to update.");
|
|
2702
2796
|
}
|
|
2703
2797
|
}
|
|
2704
2798
|
async function updatePermissionsById(_id, permissions, session) {
|
|
2705
2799
|
if (!_id) {
|
|
2706
|
-
throw new
|
|
2800
|
+
throw new import_utils13.BadRequestError("Role ID is required.");
|
|
2707
2801
|
}
|
|
2708
2802
|
try {
|
|
2709
2803
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2710
2804
|
} catch (error) {
|
|
2711
|
-
throw new
|
|
2805
|
+
throw new import_utils13.BadRequestError("Invalid role ID.");
|
|
2712
2806
|
}
|
|
2713
2807
|
if (!permissions) {
|
|
2714
|
-
throw new
|
|
2808
|
+
throw new import_utils13.BadRequestError("Permissions are required.");
|
|
2715
2809
|
}
|
|
2716
2810
|
if (permissions.length === 0) {
|
|
2717
|
-
throw new
|
|
2811
|
+
throw new import_utils13.BadRequestError("Permissions cannot be empty.");
|
|
2718
2812
|
}
|
|
2719
2813
|
try {
|
|
2720
2814
|
await collection.updateOne(
|
|
@@ -2725,14 +2819,14 @@ function useRoleRepo() {
|
|
|
2725
2819
|
delCachedData();
|
|
2726
2820
|
return "Successfully updated role permissions.";
|
|
2727
2821
|
} catch (error) {
|
|
2728
|
-
throw new
|
|
2822
|
+
throw new import_utils13.InternalServerError("Failed to update role permissions.");
|
|
2729
2823
|
}
|
|
2730
2824
|
}
|
|
2731
|
-
async function
|
|
2825
|
+
async function deleteById(_id, session) {
|
|
2732
2826
|
try {
|
|
2733
2827
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2734
2828
|
} catch (error) {
|
|
2735
|
-
throw new
|
|
2829
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
2736
2830
|
}
|
|
2737
2831
|
try {
|
|
2738
2832
|
await collection.deleteOne(
|
|
@@ -2744,7 +2838,7 @@ function useRoleRepo() {
|
|
|
2744
2838
|
delCachedData();
|
|
2745
2839
|
return "Successfully deleted role.";
|
|
2746
2840
|
} catch (error) {
|
|
2747
|
-
throw new
|
|
2841
|
+
throw new import_utils13.InternalServerError("Failed to delete role.");
|
|
2748
2842
|
}
|
|
2749
2843
|
}
|
|
2750
2844
|
return {
|
|
@@ -2754,17 +2848,17 @@ function useRoleRepo() {
|
|
|
2754
2848
|
addRole,
|
|
2755
2849
|
getRoles,
|
|
2756
2850
|
getRoleByUserId,
|
|
2757
|
-
|
|
2851
|
+
getById,
|
|
2758
2852
|
getRoleByName,
|
|
2759
2853
|
updateRole,
|
|
2760
|
-
|
|
2854
|
+
deleteById,
|
|
2761
2855
|
updatePermissionsById,
|
|
2762
2856
|
delCachedData
|
|
2763
2857
|
};
|
|
2764
2858
|
}
|
|
2765
2859
|
|
|
2766
2860
|
// src/resources/permission/permission.model.ts
|
|
2767
|
-
var
|
|
2861
|
+
var import_utils14 = require("@goweekdays/utils");
|
|
2768
2862
|
var import_joi5 = __toESM(require("joi"));
|
|
2769
2863
|
var schemaPermission = import_joi5.default.object({
|
|
2770
2864
|
app: import_joi5.default.string().required(),
|
|
@@ -2783,7 +2877,7 @@ var schemaPermissionUpdate = import_joi5.default.object({
|
|
|
2783
2877
|
function modelPermission(value) {
|
|
2784
2878
|
const { error } = schemaPermission.validate(value);
|
|
2785
2879
|
if (error) {
|
|
2786
|
-
throw new
|
|
2880
|
+
throw new import_utils14.BadRequestError(error.message);
|
|
2787
2881
|
}
|
|
2788
2882
|
return {
|
|
2789
2883
|
_id: value._id,
|
|
@@ -2801,17 +2895,17 @@ function modelPermission(value) {
|
|
|
2801
2895
|
}
|
|
2802
2896
|
|
|
2803
2897
|
// src/resources/permission/permission.repository.ts
|
|
2804
|
-
var
|
|
2898
|
+
var import_utils15 = require("@goweekdays/utils");
|
|
2805
2899
|
var import_mongodb12 = require("mongodb");
|
|
2806
2900
|
var import_joi6 = __toESM(require("joi"));
|
|
2807
2901
|
function usePermissionRepo() {
|
|
2808
|
-
const db =
|
|
2902
|
+
const db = import_utils15.useAtlas.getDb();
|
|
2809
2903
|
if (!db) {
|
|
2810
2904
|
throw new Error("Unable to connect to server.");
|
|
2811
2905
|
}
|
|
2812
2906
|
const namespace_collection = "permissions";
|
|
2813
2907
|
const collection = db.collection(namespace_collection);
|
|
2814
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
2908
|
+
const { getCache, setCache, delNamespace } = (0, import_utils15.useCache)(namespace_collection);
|
|
2815
2909
|
async function createIndexes() {
|
|
2816
2910
|
try {
|
|
2817
2911
|
await collection.createIndexes([
|
|
@@ -2840,7 +2934,7 @@ function usePermissionRepo() {
|
|
|
2840
2934
|
}
|
|
2841
2935
|
}
|
|
2842
2936
|
createIndexes().catch((error) => {
|
|
2843
|
-
|
|
2937
|
+
import_utils15.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
2844
2938
|
});
|
|
2845
2939
|
async function add(value, session) {
|
|
2846
2940
|
try {
|
|
@@ -2849,16 +2943,16 @@ function usePermissionRepo() {
|
|
|
2849
2943
|
delCachedData();
|
|
2850
2944
|
return res.insertedId;
|
|
2851
2945
|
} catch (error) {
|
|
2852
|
-
|
|
2946
|
+
import_utils15.logger.log({
|
|
2853
2947
|
level: "error",
|
|
2854
2948
|
message: error.message
|
|
2855
2949
|
});
|
|
2856
|
-
if (error instanceof
|
|
2950
|
+
if (error instanceof import_utils15.AppError) {
|
|
2857
2951
|
throw error;
|
|
2858
2952
|
} else {
|
|
2859
2953
|
const isDuplicated = error.message.includes("duplicate");
|
|
2860
2954
|
if (isDuplicated) {
|
|
2861
|
-
throw new
|
|
2955
|
+
throw new import_utils15.BadRequestError("Permission already exists.");
|
|
2862
2956
|
}
|
|
2863
2957
|
throw new Error("Failed to create permission.");
|
|
2864
2958
|
}
|
|
@@ -2868,11 +2962,11 @@ function usePermissionRepo() {
|
|
|
2868
2962
|
try {
|
|
2869
2963
|
_id = new import_mongodb12.ObjectId(_id);
|
|
2870
2964
|
} catch (error2) {
|
|
2871
|
-
throw new
|
|
2965
|
+
throw new import_utils15.BadRequestError("Invalid ID.");
|
|
2872
2966
|
}
|
|
2873
2967
|
const { error } = schemaPermissionUpdate.validate(value);
|
|
2874
2968
|
if (error) {
|
|
2875
|
-
throw new
|
|
2969
|
+
throw new import_utils15.BadRequestError(`Invalid data: ${error.message}`);
|
|
2876
2970
|
}
|
|
2877
2971
|
try {
|
|
2878
2972
|
const res = await collection.updateOne(
|
|
@@ -2883,11 +2977,11 @@ function usePermissionRepo() {
|
|
|
2883
2977
|
delCachedData();
|
|
2884
2978
|
return res;
|
|
2885
2979
|
} catch (error2) {
|
|
2886
|
-
|
|
2980
|
+
import_utils15.logger.log({
|
|
2887
2981
|
level: "error",
|
|
2888
2982
|
message: error2.message
|
|
2889
2983
|
});
|
|
2890
|
-
if (error2 instanceof
|
|
2984
|
+
if (error2 instanceof import_utils15.AppError) {
|
|
2891
2985
|
throw error2;
|
|
2892
2986
|
} else {
|
|
2893
2987
|
throw new Error("Failed to update permission.");
|
|
@@ -2920,15 +3014,15 @@ function usePermissionRepo() {
|
|
|
2920
3014
|
query.app = app;
|
|
2921
3015
|
cacheParams.app = app;
|
|
2922
3016
|
}
|
|
2923
|
-
const cacheKey = (0,
|
|
2924
|
-
|
|
3017
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, cacheParams);
|
|
3018
|
+
import_utils15.logger.log({
|
|
2925
3019
|
level: "info",
|
|
2926
3020
|
message: `Cache key for getAll permissions: ${cacheKey}`
|
|
2927
3021
|
});
|
|
2928
3022
|
try {
|
|
2929
3023
|
const cached = await getCache(cacheKey);
|
|
2930
3024
|
if (cached) {
|
|
2931
|
-
|
|
3025
|
+
import_utils15.logger.log({
|
|
2932
3026
|
level: "info",
|
|
2933
3027
|
message: `Cache hit for getAll permissions: ${cacheKey}`
|
|
2934
3028
|
});
|
|
@@ -2941,21 +3035,21 @@ function usePermissionRepo() {
|
|
|
2941
3035
|
{ $limit: limit }
|
|
2942
3036
|
]).toArray();
|
|
2943
3037
|
const length = await collection.countDocuments(query);
|
|
2944
|
-
const data = (0,
|
|
3038
|
+
const data = (0, import_utils15.paginate)(items, page, limit, length);
|
|
2945
3039
|
setCache(cacheKey, data, 600).then(() => {
|
|
2946
|
-
|
|
3040
|
+
import_utils15.logger.log({
|
|
2947
3041
|
level: "info",
|
|
2948
3042
|
message: `Cache set for getAll permissions: ${cacheKey}`
|
|
2949
3043
|
});
|
|
2950
3044
|
}).catch((err) => {
|
|
2951
|
-
|
|
3045
|
+
import_utils15.logger.log({
|
|
2952
3046
|
level: "error",
|
|
2953
3047
|
message: `Failed to set cache for getAll permissions: ${err.message}`
|
|
2954
3048
|
});
|
|
2955
3049
|
});
|
|
2956
3050
|
return data;
|
|
2957
3051
|
} catch (error) {
|
|
2958
|
-
|
|
3052
|
+
import_utils15.logger.log({ level: "error", message: `${error}` });
|
|
2959
3053
|
throw error;
|
|
2960
3054
|
}
|
|
2961
3055
|
}
|
|
@@ -2963,13 +3057,13 @@ function usePermissionRepo() {
|
|
|
2963
3057
|
try {
|
|
2964
3058
|
_id = new import_mongodb12.ObjectId(_id);
|
|
2965
3059
|
} catch (error) {
|
|
2966
|
-
throw new
|
|
3060
|
+
throw new import_utils15.BadRequestError("Invalid ID.");
|
|
2967
3061
|
}
|
|
2968
|
-
const cacheKey = (0,
|
|
3062
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
2969
3063
|
try {
|
|
2970
3064
|
const cached = await getCache(cacheKey);
|
|
2971
3065
|
if (cached) {
|
|
2972
|
-
|
|
3066
|
+
import_utils15.logger.log({
|
|
2973
3067
|
level: "info",
|
|
2974
3068
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
2975
3069
|
});
|
|
@@ -2979,22 +3073,22 @@ function usePermissionRepo() {
|
|
|
2979
3073
|
_id
|
|
2980
3074
|
});
|
|
2981
3075
|
setCache(cacheKey, result, 300).then(() => {
|
|
2982
|
-
|
|
3076
|
+
import_utils15.logger.log({
|
|
2983
3077
|
level: "info",
|
|
2984
3078
|
message: `Cache set for permission by id: ${cacheKey}`
|
|
2985
3079
|
});
|
|
2986
3080
|
}).catch((err) => {
|
|
2987
|
-
|
|
3081
|
+
import_utils15.logger.log({
|
|
2988
3082
|
level: "error",
|
|
2989
3083
|
message: `Failed to set cache for permission by id: ${err.message}`
|
|
2990
3084
|
});
|
|
2991
3085
|
});
|
|
2992
3086
|
return result;
|
|
2993
3087
|
} catch (error) {
|
|
2994
|
-
if (error instanceof
|
|
3088
|
+
if (error instanceof import_utils15.AppError) {
|
|
2995
3089
|
throw error;
|
|
2996
3090
|
} else {
|
|
2997
|
-
throw new
|
|
3091
|
+
throw new import_utils15.InternalServerError("Failed to get permission.");
|
|
2998
3092
|
}
|
|
2999
3093
|
}
|
|
3000
3094
|
}
|
|
@@ -3006,7 +3100,7 @@ function usePermissionRepo() {
|
|
|
3006
3100
|
});
|
|
3007
3101
|
const { error } = validation.validate({ key, group });
|
|
3008
3102
|
if (error) {
|
|
3009
|
-
throw new
|
|
3103
|
+
throw new import_utils15.BadRequestError(`Invalid data: ${error.message}`);
|
|
3010
3104
|
}
|
|
3011
3105
|
const query = {};
|
|
3012
3106
|
const cacheKeyOptions = {};
|
|
@@ -3020,11 +3114,11 @@ function usePermissionRepo() {
|
|
|
3020
3114
|
query.app = app;
|
|
3021
3115
|
cacheKeyOptions.app = app;
|
|
3022
3116
|
}
|
|
3023
|
-
const cacheKey = (0,
|
|
3117
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
3024
3118
|
try {
|
|
3025
3119
|
const cached = await getCache(cacheKey);
|
|
3026
3120
|
if (cached) {
|
|
3027
|
-
|
|
3121
|
+
import_utils15.logger.log({
|
|
3028
3122
|
level: "info",
|
|
3029
3123
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
3030
3124
|
});
|
|
@@ -3032,34 +3126,34 @@ function usePermissionRepo() {
|
|
|
3032
3126
|
}
|
|
3033
3127
|
const result = await collection.findOne(query);
|
|
3034
3128
|
setCache(cacheKey, result, 300).then(() => {
|
|
3035
|
-
|
|
3129
|
+
import_utils15.logger.log({
|
|
3036
3130
|
level: "info",
|
|
3037
3131
|
message: `Cache set for permission by key: ${cacheKey}`
|
|
3038
3132
|
});
|
|
3039
3133
|
}).catch((err) => {
|
|
3040
|
-
|
|
3134
|
+
import_utils15.logger.log({
|
|
3041
3135
|
level: "error",
|
|
3042
3136
|
message: `Failed to set cache for permission by key: ${err.message}`
|
|
3043
3137
|
});
|
|
3044
3138
|
});
|
|
3045
3139
|
return result;
|
|
3046
3140
|
} catch (error2) {
|
|
3047
|
-
if (error2 instanceof
|
|
3141
|
+
if (error2 instanceof import_utils15.AppError) {
|
|
3048
3142
|
throw error2;
|
|
3049
3143
|
} else {
|
|
3050
|
-
throw new
|
|
3144
|
+
throw new import_utils15.InternalServerError("Failed to get permission.");
|
|
3051
3145
|
}
|
|
3052
3146
|
}
|
|
3053
3147
|
}
|
|
3054
3148
|
async function countByGroup(group) {
|
|
3055
|
-
const cacheKey = (0,
|
|
3149
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, {
|
|
3056
3150
|
group,
|
|
3057
3151
|
tag: "countByGroup"
|
|
3058
3152
|
});
|
|
3059
3153
|
try {
|
|
3060
3154
|
const cached = await getCache(cacheKey);
|
|
3061
3155
|
if (cached) {
|
|
3062
|
-
|
|
3156
|
+
import_utils15.logger.log({
|
|
3063
3157
|
level: "info",
|
|
3064
3158
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
3065
3159
|
});
|
|
@@ -3069,22 +3163,22 @@ function usePermissionRepo() {
|
|
|
3069
3163
|
group
|
|
3070
3164
|
});
|
|
3071
3165
|
setCache(cacheKey, result, 300).then(() => {
|
|
3072
|
-
|
|
3166
|
+
import_utils15.logger.log({
|
|
3073
3167
|
level: "info",
|
|
3074
3168
|
message: `Cache set for permission count by group: ${cacheKey}`
|
|
3075
3169
|
});
|
|
3076
3170
|
}).catch((err) => {
|
|
3077
|
-
|
|
3171
|
+
import_utils15.logger.log({
|
|
3078
3172
|
level: "error",
|
|
3079
3173
|
message: `Failed to set cache for permission by group: ${err.message}`
|
|
3080
3174
|
});
|
|
3081
3175
|
});
|
|
3082
3176
|
return result;
|
|
3083
3177
|
} catch (error) {
|
|
3084
|
-
if (error instanceof
|
|
3178
|
+
if (error instanceof import_utils15.AppError) {
|
|
3085
3179
|
throw error;
|
|
3086
3180
|
} else {
|
|
3087
|
-
throw new
|
|
3181
|
+
throw new import_utils15.InternalServerError("Failed to count permission by group.");
|
|
3088
3182
|
}
|
|
3089
3183
|
}
|
|
3090
3184
|
}
|
|
@@ -3092,7 +3186,7 @@ function usePermissionRepo() {
|
|
|
3092
3186
|
try {
|
|
3093
3187
|
_id = new import_mongodb12.ObjectId(_id);
|
|
3094
3188
|
} catch (error) {
|
|
3095
|
-
throw new
|
|
3189
|
+
throw new import_utils15.BadRequestError("Invalid ID.");
|
|
3096
3190
|
}
|
|
3097
3191
|
try {
|
|
3098
3192
|
const res = await collection.updateOne(
|
|
@@ -3102,25 +3196,25 @@ function usePermissionRepo() {
|
|
|
3102
3196
|
delCachedData();
|
|
3103
3197
|
return res;
|
|
3104
3198
|
} catch (error) {
|
|
3105
|
-
|
|
3199
|
+
import_utils15.logger.log({
|
|
3106
3200
|
level: "error",
|
|
3107
3201
|
message: error.message
|
|
3108
3202
|
});
|
|
3109
|
-
if (error instanceof
|
|
3203
|
+
if (error instanceof import_utils15.AppError) {
|
|
3110
3204
|
throw error;
|
|
3111
3205
|
} else {
|
|
3112
|
-
throw new
|
|
3206
|
+
throw new import_utils15.InternalServerError("Failed to delete permission.");
|
|
3113
3207
|
}
|
|
3114
3208
|
}
|
|
3115
3209
|
}
|
|
3116
3210
|
function delCachedData() {
|
|
3117
3211
|
delNamespace().then(() => {
|
|
3118
|
-
|
|
3212
|
+
import_utils15.logger.log({
|
|
3119
3213
|
level: "info",
|
|
3120
3214
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
3121
3215
|
});
|
|
3122
3216
|
}).catch((err) => {
|
|
3123
|
-
|
|
3217
|
+
import_utils15.logger.log({
|
|
3124
3218
|
level: "error",
|
|
3125
3219
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
3126
3220
|
});
|
|
@@ -3159,7 +3253,7 @@ function usePermissionService() {
|
|
|
3159
3253
|
}
|
|
3160
3254
|
|
|
3161
3255
|
// src/resources/permission/permission.controller.ts
|
|
3162
|
-
var
|
|
3256
|
+
var import_utils16 = require("@goweekdays/utils");
|
|
3163
3257
|
var import_joi7 = __toESM(require("joi"));
|
|
3164
3258
|
function usePermissionController() {
|
|
3165
3259
|
const {
|
|
@@ -3173,8 +3267,8 @@ function usePermissionController() {
|
|
|
3173
3267
|
const value = req.body;
|
|
3174
3268
|
const { error } = schemaPermission.validate(value);
|
|
3175
3269
|
if (error) {
|
|
3176
|
-
next(new
|
|
3177
|
-
|
|
3270
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3271
|
+
import_utils16.logger.info(`Controller: ${error.message}`);
|
|
3178
3272
|
return;
|
|
3179
3273
|
}
|
|
3180
3274
|
try {
|
|
@@ -3196,7 +3290,7 @@ function usePermissionController() {
|
|
|
3196
3290
|
});
|
|
3197
3291
|
const { error } = validation.validate(query);
|
|
3198
3292
|
if (error) {
|
|
3199
|
-
next(new
|
|
3293
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3200
3294
|
return;
|
|
3201
3295
|
}
|
|
3202
3296
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -3235,7 +3329,7 @@ function usePermissionController() {
|
|
|
3235
3329
|
});
|
|
3236
3330
|
const { error } = validation.validate({ id });
|
|
3237
3331
|
if (error) {
|
|
3238
|
-
next(new
|
|
3332
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3239
3333
|
return;
|
|
3240
3334
|
}
|
|
3241
3335
|
try {
|
|
@@ -3256,7 +3350,7 @@ function usePermissionController() {
|
|
|
3256
3350
|
});
|
|
3257
3351
|
const { error } = validation.validate({ id });
|
|
3258
3352
|
if (error) {
|
|
3259
|
-
next(new
|
|
3353
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3260
3354
|
return;
|
|
3261
3355
|
}
|
|
3262
3356
|
try {
|
|
@@ -3271,13 +3365,13 @@ function usePermissionController() {
|
|
|
3271
3365
|
const id = req.params.id;
|
|
3272
3366
|
const { error: errorId } = import_joi7.default.string().hex().required().validate(id);
|
|
3273
3367
|
if (errorId) {
|
|
3274
|
-
next(new
|
|
3368
|
+
next(new import_utils16.BadRequestError(errorId.message));
|
|
3275
3369
|
return;
|
|
3276
3370
|
}
|
|
3277
3371
|
const payload = req.body;
|
|
3278
3372
|
const { error } = schemaPermissionUpdate.validate(payload);
|
|
3279
3373
|
if (error) {
|
|
3280
|
-
next(new
|
|
3374
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3281
3375
|
return;
|
|
3282
3376
|
}
|
|
3283
3377
|
try {
|
|
@@ -3298,7 +3392,7 @@ function usePermissionController() {
|
|
|
3298
3392
|
}
|
|
3299
3393
|
|
|
3300
3394
|
// src/resources/permission/permission.group.model.ts
|
|
3301
|
-
var
|
|
3395
|
+
var import_utils17 = require("@goweekdays/utils");
|
|
3302
3396
|
var import_joi8 = __toESM(require("joi"));
|
|
3303
3397
|
var schemaPermissionGroup = import_joi8.default.object({
|
|
3304
3398
|
app: import_joi8.default.string().required(),
|
|
@@ -3314,7 +3408,7 @@ var schemaPermissionGroupUpdate = import_joi8.default.object({
|
|
|
3314
3408
|
function modelPermissionGroup(value) {
|
|
3315
3409
|
const { error } = schemaPermissionGroup.validate(value);
|
|
3316
3410
|
if (error) {
|
|
3317
|
-
throw new
|
|
3411
|
+
throw new import_utils17.BadRequestError(error.message);
|
|
3318
3412
|
}
|
|
3319
3413
|
return {
|
|
3320
3414
|
_id: value._id,
|
|
@@ -3330,18 +3424,18 @@ function modelPermissionGroup(value) {
|
|
|
3330
3424
|
}
|
|
3331
3425
|
|
|
3332
3426
|
// src/resources/permission/permission.group.repository.ts
|
|
3333
|
-
var
|
|
3427
|
+
var import_utils18 = require("@goweekdays/utils");
|
|
3334
3428
|
var import_mongodb13 = require("mongodb");
|
|
3335
3429
|
var import_joi9 = __toESM(require("joi"));
|
|
3336
3430
|
var init = false;
|
|
3337
3431
|
function usePermissionGroupRepo() {
|
|
3338
|
-
const db =
|
|
3432
|
+
const db = import_utils18.useAtlas.getDb();
|
|
3339
3433
|
if (!db) {
|
|
3340
3434
|
throw new Error("Unable to connect to server.");
|
|
3341
3435
|
}
|
|
3342
3436
|
const namespace_collection = "permission.groups";
|
|
3343
3437
|
const collection = db.collection(namespace_collection);
|
|
3344
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
3438
|
+
const { getCache, setCache, delNamespace } = (0, import_utils18.useCache)(namespace_collection);
|
|
3345
3439
|
async function createIndexes() {
|
|
3346
3440
|
try {
|
|
3347
3441
|
await collection.createIndexes([
|
|
@@ -3364,7 +3458,7 @@ function usePermissionGroupRepo() {
|
|
|
3364
3458
|
}
|
|
3365
3459
|
if (!init) {
|
|
3366
3460
|
createIndexes().catch((error) => {
|
|
3367
|
-
|
|
3461
|
+
import_utils18.logger.log({
|
|
3368
3462
|
level: "error",
|
|
3369
3463
|
message: `Index creation error: ${error}`
|
|
3370
3464
|
});
|
|
@@ -3378,16 +3472,16 @@ function usePermissionGroupRepo() {
|
|
|
3378
3472
|
delCachedData();
|
|
3379
3473
|
return res.insertedId;
|
|
3380
3474
|
} catch (error) {
|
|
3381
|
-
|
|
3475
|
+
import_utils18.logger.log({
|
|
3382
3476
|
level: "error",
|
|
3383
3477
|
message: error.message
|
|
3384
3478
|
});
|
|
3385
|
-
if (error instanceof
|
|
3479
|
+
if (error instanceof import_utils18.AppError) {
|
|
3386
3480
|
throw error;
|
|
3387
3481
|
} else {
|
|
3388
3482
|
const isDuplicated = error.message.includes("duplicate");
|
|
3389
3483
|
if (isDuplicated) {
|
|
3390
|
-
throw new
|
|
3484
|
+
throw new import_utils18.BadRequestError("Permission group already exists.");
|
|
3391
3485
|
}
|
|
3392
3486
|
throw new Error("Failed to create permission group.");
|
|
3393
3487
|
}
|
|
@@ -3397,11 +3491,11 @@ function usePermissionGroupRepo() {
|
|
|
3397
3491
|
try {
|
|
3398
3492
|
_id = new import_mongodb13.ObjectId(_id);
|
|
3399
3493
|
} catch (error2) {
|
|
3400
|
-
throw new
|
|
3494
|
+
throw new import_utils18.BadRequestError("Invalid ID.");
|
|
3401
3495
|
}
|
|
3402
3496
|
const { error } = schemaPermissionGroupUpdate.validate(value);
|
|
3403
3497
|
if (error) {
|
|
3404
|
-
throw new
|
|
3498
|
+
throw new import_utils18.BadRequestError(`Invalid data: ${error.message}`);
|
|
3405
3499
|
}
|
|
3406
3500
|
try {
|
|
3407
3501
|
const res = await collection.updateOne(
|
|
@@ -3412,11 +3506,11 @@ function usePermissionGroupRepo() {
|
|
|
3412
3506
|
delCachedData();
|
|
3413
3507
|
return res;
|
|
3414
3508
|
} catch (error2) {
|
|
3415
|
-
|
|
3509
|
+
import_utils18.logger.log({
|
|
3416
3510
|
level: "error",
|
|
3417
3511
|
message: error2.message
|
|
3418
3512
|
});
|
|
3419
|
-
if (error2 instanceof
|
|
3513
|
+
if (error2 instanceof import_utils18.AppError) {
|
|
3420
3514
|
throw error2;
|
|
3421
3515
|
} else {
|
|
3422
3516
|
throw new Error("Failed to update permission group.");
|
|
@@ -3449,15 +3543,15 @@ function usePermissionGroupRepo() {
|
|
|
3449
3543
|
query.app = app;
|
|
3450
3544
|
cacheParams.app = app;
|
|
3451
3545
|
}
|
|
3452
|
-
const cacheKey = (0,
|
|
3453
|
-
|
|
3546
|
+
const cacheKey = (0, import_utils18.makeCacheKey)(namespace_collection, cacheParams);
|
|
3547
|
+
import_utils18.logger.log({
|
|
3454
3548
|
level: "info",
|
|
3455
3549
|
message: `Cache key for getAll permission groups: ${cacheKey}`
|
|
3456
3550
|
});
|
|
3457
3551
|
try {
|
|
3458
3552
|
const cached = await getCache(cacheKey);
|
|
3459
3553
|
if (cached) {
|
|
3460
|
-
|
|
3554
|
+
import_utils18.logger.log({
|
|
3461
3555
|
level: "info",
|
|
3462
3556
|
message: `Cache hit for getAll permission groups: ${cacheKey}`
|
|
3463
3557
|
});
|
|
@@ -3470,21 +3564,21 @@ function usePermissionGroupRepo() {
|
|
|
3470
3564
|
{ $limit: limit }
|
|
3471
3565
|
]).toArray();
|
|
3472
3566
|
const length = await collection.countDocuments(query);
|
|
3473
|
-
const data = (0,
|
|
3567
|
+
const data = (0, import_utils18.paginate)(items, page, limit, length);
|
|
3474
3568
|
setCache(cacheKey, data, 600).then(() => {
|
|
3475
|
-
|
|
3569
|
+
import_utils18.logger.log({
|
|
3476
3570
|
level: "info",
|
|
3477
3571
|
message: `Cache set for getAll permission groups: ${cacheKey}`
|
|
3478
3572
|
});
|
|
3479
3573
|
}).catch((err) => {
|
|
3480
|
-
|
|
3574
|
+
import_utils18.logger.log({
|
|
3481
3575
|
level: "error",
|
|
3482
3576
|
message: `Failed to set cache for getAll permission groups: ${err.message}`
|
|
3483
3577
|
});
|
|
3484
3578
|
});
|
|
3485
3579
|
return data;
|
|
3486
3580
|
} catch (error) {
|
|
3487
|
-
|
|
3581
|
+
import_utils18.logger.log({ level: "error", message: `${error}` });
|
|
3488
3582
|
throw error;
|
|
3489
3583
|
}
|
|
3490
3584
|
}
|
|
@@ -3492,13 +3586,13 @@ function usePermissionGroupRepo() {
|
|
|
3492
3586
|
try {
|
|
3493
3587
|
_id = new import_mongodb13.ObjectId(_id);
|
|
3494
3588
|
} catch (error) {
|
|
3495
|
-
throw new
|
|
3589
|
+
throw new import_utils18.BadRequestError("Invalid ID.");
|
|
3496
3590
|
}
|
|
3497
|
-
const cacheKey = (0,
|
|
3591
|
+
const cacheKey = (0, import_utils18.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
3498
3592
|
try {
|
|
3499
3593
|
const cached = await getCache(cacheKey);
|
|
3500
3594
|
if (cached) {
|
|
3501
|
-
|
|
3595
|
+
import_utils18.logger.log({
|
|
3502
3596
|
level: "info",
|
|
3503
3597
|
message: `Cache hit for getById permission group: ${cacheKey}`
|
|
3504
3598
|
});
|
|
@@ -3508,22 +3602,22 @@ function usePermissionGroupRepo() {
|
|
|
3508
3602
|
_id
|
|
3509
3603
|
});
|
|
3510
3604
|
setCache(cacheKey, result, 300).then(() => {
|
|
3511
|
-
|
|
3605
|
+
import_utils18.logger.log({
|
|
3512
3606
|
level: "info",
|
|
3513
3607
|
message: `Cache set for permission group by id: ${cacheKey}`
|
|
3514
3608
|
});
|
|
3515
3609
|
}).catch((err) => {
|
|
3516
|
-
|
|
3610
|
+
import_utils18.logger.log({
|
|
3517
3611
|
level: "error",
|
|
3518
3612
|
message: `Failed to set cache for permission group by id: ${err.message}`
|
|
3519
3613
|
});
|
|
3520
3614
|
});
|
|
3521
3615
|
return result;
|
|
3522
3616
|
} catch (error) {
|
|
3523
|
-
if (error instanceof
|
|
3617
|
+
if (error instanceof import_utils18.AppError) {
|
|
3524
3618
|
throw error;
|
|
3525
3619
|
} else {
|
|
3526
|
-
throw new
|
|
3620
|
+
throw new import_utils18.InternalServerError("Failed to get permission group.");
|
|
3527
3621
|
}
|
|
3528
3622
|
}
|
|
3529
3623
|
}
|
|
@@ -3534,7 +3628,7 @@ function usePermissionGroupRepo() {
|
|
|
3534
3628
|
});
|
|
3535
3629
|
const { error } = validation.validate({ key, app });
|
|
3536
3630
|
if (error) {
|
|
3537
|
-
throw new
|
|
3631
|
+
throw new import_utils18.BadRequestError("Invalid key.");
|
|
3538
3632
|
}
|
|
3539
3633
|
const query = { key };
|
|
3540
3634
|
const cacheKeyOptions = { key, tag: "byKey" };
|
|
@@ -3542,11 +3636,11 @@ function usePermissionGroupRepo() {
|
|
|
3542
3636
|
query.app = app;
|
|
3543
3637
|
cacheKeyOptions.app = app;
|
|
3544
3638
|
}
|
|
3545
|
-
const cacheKey = (0,
|
|
3639
|
+
const cacheKey = (0, import_utils18.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
3546
3640
|
try {
|
|
3547
3641
|
const cached = await getCache(cacheKey);
|
|
3548
3642
|
if (cached) {
|
|
3549
|
-
|
|
3643
|
+
import_utils18.logger.log({
|
|
3550
3644
|
level: "info",
|
|
3551
3645
|
message: `Cache hit for getById permission group: ${cacheKey}`
|
|
3552
3646
|
});
|
|
@@ -3554,22 +3648,22 @@ function usePermissionGroupRepo() {
|
|
|
3554
3648
|
}
|
|
3555
3649
|
const result = await collection.findOne(query);
|
|
3556
3650
|
setCache(cacheKey, result, 300).then(() => {
|
|
3557
|
-
|
|
3651
|
+
import_utils18.logger.log({
|
|
3558
3652
|
level: "info",
|
|
3559
3653
|
message: `Cache set for permission group by key: ${cacheKey}`
|
|
3560
3654
|
});
|
|
3561
3655
|
}).catch((err) => {
|
|
3562
|
-
|
|
3656
|
+
import_utils18.logger.log({
|
|
3563
3657
|
level: "error",
|
|
3564
3658
|
message: `Failed to set cache for permission group by key: ${err.message}`
|
|
3565
3659
|
});
|
|
3566
3660
|
});
|
|
3567
3661
|
return result;
|
|
3568
3662
|
} catch (error2) {
|
|
3569
|
-
if (error2 instanceof
|
|
3663
|
+
if (error2 instanceof import_utils18.AppError) {
|
|
3570
3664
|
throw error2;
|
|
3571
3665
|
} else {
|
|
3572
|
-
throw new
|
|
3666
|
+
throw new import_utils18.InternalServerError("Failed to get permission group.");
|
|
3573
3667
|
}
|
|
3574
3668
|
}
|
|
3575
3669
|
}
|
|
@@ -3577,7 +3671,7 @@ function usePermissionGroupRepo() {
|
|
|
3577
3671
|
try {
|
|
3578
3672
|
_id = new import_mongodb13.ObjectId(_id);
|
|
3579
3673
|
} catch (error) {
|
|
3580
|
-
throw new
|
|
3674
|
+
throw new import_utils18.BadRequestError("Invalid ID.");
|
|
3581
3675
|
}
|
|
3582
3676
|
try {
|
|
3583
3677
|
const res = await collection.updateOne(
|
|
@@ -3587,25 +3681,25 @@ function usePermissionGroupRepo() {
|
|
|
3587
3681
|
delCachedData();
|
|
3588
3682
|
return res;
|
|
3589
3683
|
} catch (error) {
|
|
3590
|
-
|
|
3684
|
+
import_utils18.logger.log({
|
|
3591
3685
|
level: "error",
|
|
3592
3686
|
message: error.message
|
|
3593
3687
|
});
|
|
3594
|
-
if (error instanceof
|
|
3688
|
+
if (error instanceof import_utils18.AppError) {
|
|
3595
3689
|
throw error;
|
|
3596
3690
|
} else {
|
|
3597
|
-
throw new
|
|
3691
|
+
throw new import_utils18.InternalServerError("Failed to delete permission group.");
|
|
3598
3692
|
}
|
|
3599
3693
|
}
|
|
3600
3694
|
}
|
|
3601
3695
|
function delCachedData() {
|
|
3602
3696
|
delNamespace().then(() => {
|
|
3603
|
-
|
|
3697
|
+
import_utils18.logger.log({
|
|
3604
3698
|
level: "info",
|
|
3605
3699
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
3606
3700
|
});
|
|
3607
3701
|
}).catch((err) => {
|
|
3608
|
-
|
|
3702
|
+
import_utils18.logger.log({
|
|
3609
3703
|
level: "error",
|
|
3610
3704
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
3611
3705
|
});
|
|
@@ -3623,10 +3717,10 @@ function usePermissionGroupRepo() {
|
|
|
3623
3717
|
}
|
|
3624
3718
|
|
|
3625
3719
|
// src/resources/permission/permission.group.service.ts
|
|
3626
|
-
var
|
|
3720
|
+
var import_utils23 = require("@goweekdays/utils");
|
|
3627
3721
|
|
|
3628
3722
|
// src/resources/app/app.model.ts
|
|
3629
|
-
var
|
|
3723
|
+
var import_utils19 = require("@goweekdays/utils");
|
|
3630
3724
|
var import_joi10 = __toESM(require("joi"));
|
|
3631
3725
|
var schemaApp = import_joi10.default.object({
|
|
3632
3726
|
code: import_joi10.default.string().alphanum().max(20).required(),
|
|
@@ -3642,7 +3736,7 @@ var schemaAppUpdate = import_joi10.default.object({
|
|
|
3642
3736
|
function modelApp(value) {
|
|
3643
3737
|
const { error } = schemaApp.validate(value);
|
|
3644
3738
|
if (error) {
|
|
3645
|
-
throw new
|
|
3739
|
+
throw new import_utils19.BadRequestError(error.message);
|
|
3646
3740
|
}
|
|
3647
3741
|
return {
|
|
3648
3742
|
_id: value._id,
|
|
@@ -3658,18 +3752,18 @@ function modelApp(value) {
|
|
|
3658
3752
|
}
|
|
3659
3753
|
|
|
3660
3754
|
// src/resources/app/app.repository.ts
|
|
3661
|
-
var
|
|
3755
|
+
var import_utils20 = require("@goweekdays/utils");
|
|
3662
3756
|
var import_mongodb14 = require("mongodb");
|
|
3663
3757
|
var import_joi11 = __toESM(require("joi"));
|
|
3664
3758
|
var init2 = false;
|
|
3665
3759
|
function useAppRepo() {
|
|
3666
|
-
const db =
|
|
3760
|
+
const db = import_utils20.useAtlas.getDb();
|
|
3667
3761
|
if (!db) {
|
|
3668
3762
|
throw new Error("Unable to connect to server.");
|
|
3669
3763
|
}
|
|
3670
3764
|
const namespace_collection = "apps";
|
|
3671
3765
|
const collection = db.collection(namespace_collection);
|
|
3672
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
3766
|
+
const { getCache, setCache, delNamespace } = (0, import_utils20.useCache)(namespace_collection);
|
|
3673
3767
|
async function createIndexes() {
|
|
3674
3768
|
try {
|
|
3675
3769
|
await collection.createIndexes([
|
|
@@ -3698,7 +3792,7 @@ function useAppRepo() {
|
|
|
3698
3792
|
}
|
|
3699
3793
|
if (!init2) {
|
|
3700
3794
|
createIndexes().catch((error) => {
|
|
3701
|
-
|
|
3795
|
+
import_utils20.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
3702
3796
|
});
|
|
3703
3797
|
init2 = true;
|
|
3704
3798
|
}
|
|
@@ -3709,16 +3803,16 @@ function useAppRepo() {
|
|
|
3709
3803
|
delCachedData();
|
|
3710
3804
|
return res.insertedId;
|
|
3711
3805
|
} catch (error) {
|
|
3712
|
-
|
|
3806
|
+
import_utils20.logger.log({
|
|
3713
3807
|
level: "error",
|
|
3714
3808
|
message: error.message
|
|
3715
3809
|
});
|
|
3716
|
-
if (error instanceof
|
|
3810
|
+
if (error instanceof import_utils20.AppError) {
|
|
3717
3811
|
throw error;
|
|
3718
3812
|
} else {
|
|
3719
3813
|
const isDuplicated = error.message.includes("duplicate");
|
|
3720
3814
|
if (isDuplicated) {
|
|
3721
|
-
throw new
|
|
3815
|
+
throw new import_utils20.BadRequestError("App already exists.");
|
|
3722
3816
|
}
|
|
3723
3817
|
throw new Error("Failed to create app.");
|
|
3724
3818
|
}
|
|
@@ -3728,7 +3822,7 @@ function useAppRepo() {
|
|
|
3728
3822
|
try {
|
|
3729
3823
|
_id = new import_mongodb14.ObjectId(_id);
|
|
3730
3824
|
} catch (error) {
|
|
3731
|
-
throw new
|
|
3825
|
+
throw new import_utils20.BadRequestError("Invalid ID.");
|
|
3732
3826
|
}
|
|
3733
3827
|
try {
|
|
3734
3828
|
const res = await collection.updateOne(
|
|
@@ -3739,11 +3833,11 @@ function useAppRepo() {
|
|
|
3739
3833
|
delCachedData();
|
|
3740
3834
|
return res;
|
|
3741
3835
|
} catch (error) {
|
|
3742
|
-
|
|
3836
|
+
import_utils20.logger.log({
|
|
3743
3837
|
level: "error",
|
|
3744
3838
|
message: error.message
|
|
3745
3839
|
});
|
|
3746
|
-
if (error instanceof
|
|
3840
|
+
if (error instanceof import_utils20.AppError) {
|
|
3747
3841
|
throw error;
|
|
3748
3842
|
} else {
|
|
3749
3843
|
throw new Error("Failed to update app.");
|
|
@@ -3781,15 +3875,15 @@ function useAppRepo() {
|
|
|
3781
3875
|
}
|
|
3782
3876
|
cacheParams.type = type;
|
|
3783
3877
|
}
|
|
3784
|
-
const cacheKey = (0,
|
|
3785
|
-
|
|
3878
|
+
const cacheKey = (0, import_utils20.makeCacheKey)(namespace_collection, cacheParams);
|
|
3879
|
+
import_utils20.logger.log({
|
|
3786
3880
|
level: "info",
|
|
3787
3881
|
message: `Cache key for getAll apps: ${cacheKey}`
|
|
3788
3882
|
});
|
|
3789
3883
|
try {
|
|
3790
3884
|
const cached = await getCache(cacheKey);
|
|
3791
3885
|
if (cached) {
|
|
3792
|
-
|
|
3886
|
+
import_utils20.logger.log({
|
|
3793
3887
|
level: "info",
|
|
3794
3888
|
message: `Cache hit for getAll apps: ${cacheKey}`
|
|
3795
3889
|
});
|
|
@@ -3802,21 +3896,21 @@ function useAppRepo() {
|
|
|
3802
3896
|
{ $limit: limit }
|
|
3803
3897
|
]).toArray();
|
|
3804
3898
|
const length = await collection.countDocuments(query);
|
|
3805
|
-
const data = (0,
|
|
3899
|
+
const data = (0, import_utils20.paginate)(items, page, limit, length);
|
|
3806
3900
|
setCache(cacheKey, data, 600).then(() => {
|
|
3807
|
-
|
|
3901
|
+
import_utils20.logger.log({
|
|
3808
3902
|
level: "info",
|
|
3809
3903
|
message: `Cache set for getAll apps: ${cacheKey}`
|
|
3810
3904
|
});
|
|
3811
3905
|
}).catch((err) => {
|
|
3812
|
-
|
|
3906
|
+
import_utils20.logger.log({
|
|
3813
3907
|
level: "error",
|
|
3814
3908
|
message: `Failed to set cache for getAll apps: ${err.message}`
|
|
3815
3909
|
});
|
|
3816
3910
|
});
|
|
3817
3911
|
return data;
|
|
3818
3912
|
} catch (error) {
|
|
3819
|
-
|
|
3913
|
+
import_utils20.logger.log({ level: "error", message: `${error}` });
|
|
3820
3914
|
throw error;
|
|
3821
3915
|
}
|
|
3822
3916
|
}
|
|
@@ -3824,13 +3918,13 @@ function useAppRepo() {
|
|
|
3824
3918
|
try {
|
|
3825
3919
|
_id = new import_mongodb14.ObjectId(_id);
|
|
3826
3920
|
} catch (error) {
|
|
3827
|
-
throw new
|
|
3921
|
+
throw new import_utils20.BadRequestError("Invalid ID.");
|
|
3828
3922
|
}
|
|
3829
|
-
const cacheKey = (0,
|
|
3923
|
+
const cacheKey = (0, import_utils20.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
3830
3924
|
try {
|
|
3831
3925
|
const cached = await getCache(cacheKey);
|
|
3832
3926
|
if (cached) {
|
|
3833
|
-
|
|
3927
|
+
import_utils20.logger.log({
|
|
3834
3928
|
level: "info",
|
|
3835
3929
|
message: `Cache hit for getById app: ${cacheKey}`
|
|
3836
3930
|
});
|
|
@@ -3840,22 +3934,22 @@ function useAppRepo() {
|
|
|
3840
3934
|
_id
|
|
3841
3935
|
});
|
|
3842
3936
|
setCache(cacheKey, result, 300).then(() => {
|
|
3843
|
-
|
|
3937
|
+
import_utils20.logger.log({
|
|
3844
3938
|
level: "info",
|
|
3845
3939
|
message: `Cache set for app by id: ${cacheKey}`
|
|
3846
3940
|
});
|
|
3847
3941
|
}).catch((err) => {
|
|
3848
|
-
|
|
3942
|
+
import_utils20.logger.log({
|
|
3849
3943
|
level: "error",
|
|
3850
3944
|
message: `Failed to set cache for app by id: ${err.message}`
|
|
3851
3945
|
});
|
|
3852
3946
|
});
|
|
3853
3947
|
return result;
|
|
3854
3948
|
} catch (error) {
|
|
3855
|
-
if (error instanceof
|
|
3949
|
+
if (error instanceof import_utils20.AppError) {
|
|
3856
3950
|
throw error;
|
|
3857
3951
|
} else {
|
|
3858
|
-
throw new
|
|
3952
|
+
throw new import_utils20.InternalServerError("Failed to get app.");
|
|
3859
3953
|
}
|
|
3860
3954
|
}
|
|
3861
3955
|
}
|
|
@@ -3863,16 +3957,16 @@ function useAppRepo() {
|
|
|
3863
3957
|
const validate = import_joi11.default.string().required();
|
|
3864
3958
|
const { error } = validate.validate(code);
|
|
3865
3959
|
if (error) {
|
|
3866
|
-
throw new
|
|
3960
|
+
throw new import_utils20.BadRequestError("Invalid code.");
|
|
3867
3961
|
}
|
|
3868
|
-
const cacheKey = (0,
|
|
3962
|
+
const cacheKey = (0, import_utils20.makeCacheKey)(namespace_collection, {
|
|
3869
3963
|
code,
|
|
3870
3964
|
tag: "byCode"
|
|
3871
3965
|
});
|
|
3872
3966
|
try {
|
|
3873
3967
|
const cached = await getCache(cacheKey);
|
|
3874
3968
|
if (cached) {
|
|
3875
|
-
|
|
3969
|
+
import_utils20.logger.log({
|
|
3876
3970
|
level: "info",
|
|
3877
3971
|
message: `Cache hit for getByCode app: ${cacheKey}`
|
|
3878
3972
|
});
|
|
@@ -3882,22 +3976,22 @@ function useAppRepo() {
|
|
|
3882
3976
|
code
|
|
3883
3977
|
});
|
|
3884
3978
|
setCache(cacheKey, result, 300).then(() => {
|
|
3885
|
-
|
|
3979
|
+
import_utils20.logger.log({
|
|
3886
3980
|
level: "info",
|
|
3887
3981
|
message: `Cache set for app by code: ${cacheKey}`
|
|
3888
3982
|
});
|
|
3889
3983
|
}).catch((err) => {
|
|
3890
|
-
|
|
3984
|
+
import_utils20.logger.log({
|
|
3891
3985
|
level: "error",
|
|
3892
3986
|
message: `Failed to set cache for app by code: ${err.message}`
|
|
3893
3987
|
});
|
|
3894
3988
|
});
|
|
3895
3989
|
return result;
|
|
3896
3990
|
} catch (error2) {
|
|
3897
|
-
if (error2 instanceof
|
|
3991
|
+
if (error2 instanceof import_utils20.AppError) {
|
|
3898
3992
|
throw error2;
|
|
3899
3993
|
} else {
|
|
3900
|
-
throw new
|
|
3994
|
+
throw new import_utils20.InternalServerError("Failed to get app.");
|
|
3901
3995
|
}
|
|
3902
3996
|
}
|
|
3903
3997
|
}
|
|
@@ -3905,7 +3999,7 @@ function useAppRepo() {
|
|
|
3905
3999
|
try {
|
|
3906
4000
|
_id = new import_mongodb14.ObjectId(_id);
|
|
3907
4001
|
} catch (error) {
|
|
3908
|
-
throw new
|
|
4002
|
+
throw new import_utils20.BadRequestError("Invalid ID.");
|
|
3909
4003
|
}
|
|
3910
4004
|
try {
|
|
3911
4005
|
const res = await collection.updateOne(
|
|
@@ -3915,25 +4009,25 @@ function useAppRepo() {
|
|
|
3915
4009
|
delCachedData();
|
|
3916
4010
|
return res;
|
|
3917
4011
|
} catch (error) {
|
|
3918
|
-
|
|
4012
|
+
import_utils20.logger.log({
|
|
3919
4013
|
level: "error",
|
|
3920
4014
|
message: error.message
|
|
3921
4015
|
});
|
|
3922
|
-
if (error instanceof
|
|
4016
|
+
if (error instanceof import_utils20.AppError) {
|
|
3923
4017
|
throw error;
|
|
3924
4018
|
} else {
|
|
3925
|
-
throw new
|
|
4019
|
+
throw new import_utils20.InternalServerError("Failed to delete app.");
|
|
3926
4020
|
}
|
|
3927
4021
|
}
|
|
3928
4022
|
}
|
|
3929
4023
|
function delCachedData() {
|
|
3930
4024
|
delNamespace().then(() => {
|
|
3931
|
-
|
|
4025
|
+
import_utils20.logger.log({
|
|
3932
4026
|
level: "info",
|
|
3933
4027
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
3934
4028
|
});
|
|
3935
4029
|
}).catch((err) => {
|
|
3936
|
-
|
|
4030
|
+
import_utils20.logger.log({
|
|
3937
4031
|
level: "error",
|
|
3938
4032
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
3939
4033
|
});
|
|
@@ -3951,7 +4045,7 @@ function useAppRepo() {
|
|
|
3951
4045
|
}
|
|
3952
4046
|
|
|
3953
4047
|
// src/resources/app/app.service.ts
|
|
3954
|
-
var
|
|
4048
|
+
var import_utils21 = require("@goweekdays/utils");
|
|
3955
4049
|
function useAppService() {
|
|
3956
4050
|
const {
|
|
3957
4051
|
updateById: _updateById,
|
|
@@ -3968,6 +4062,12 @@ function useAppService() {
|
|
|
3968
4062
|
description: "Administrative application.",
|
|
3969
4063
|
type: "default"
|
|
3970
4064
|
},
|
|
4065
|
+
{
|
|
4066
|
+
code: "org",
|
|
4067
|
+
name: "Organization",
|
|
4068
|
+
description: "Organization application.",
|
|
4069
|
+
type: "standard"
|
|
4070
|
+
},
|
|
3971
4071
|
{
|
|
3972
4072
|
code: "marketplace",
|
|
3973
4073
|
name: "Marketplace",
|
|
@@ -3995,7 +4095,7 @@ function useAppService() {
|
|
|
3995
4095
|
description: "Transportation and ride services."
|
|
3996
4096
|
}
|
|
3997
4097
|
];
|
|
3998
|
-
const session =
|
|
4098
|
+
const session = import_utils21.useAtlas.getClient()?.startSession();
|
|
3999
4099
|
if (!session) {
|
|
4000
4100
|
throw new Error("Failed to start database session.");
|
|
4001
4101
|
}
|
|
@@ -4008,14 +4108,14 @@ function useAppService() {
|
|
|
4008
4108
|
}
|
|
4009
4109
|
}
|
|
4010
4110
|
await session.commitTransaction();
|
|
4011
|
-
|
|
4111
|
+
import_utils21.logger.log({
|
|
4012
4112
|
level: "info",
|
|
4013
4113
|
message: "Default apps added successfully."
|
|
4014
4114
|
});
|
|
4015
4115
|
return;
|
|
4016
4116
|
} catch (error) {
|
|
4017
4117
|
await session.abortTransaction();
|
|
4018
|
-
|
|
4118
|
+
import_utils21.logger.log({
|
|
4019
4119
|
level: "error",
|
|
4020
4120
|
message: `Failed to add default apps: ${error}`
|
|
4021
4121
|
});
|
|
@@ -4039,7 +4139,7 @@ function useAppService() {
|
|
|
4039
4139
|
}
|
|
4040
4140
|
|
|
4041
4141
|
// src/resources/app/app.controller.ts
|
|
4042
|
-
var
|
|
4142
|
+
var import_utils22 = require("@goweekdays/utils");
|
|
4043
4143
|
var import_joi12 = __toESM(require("joi"));
|
|
4044
4144
|
function useAppController() {
|
|
4045
4145
|
const {
|
|
@@ -4053,7 +4153,7 @@ function useAppController() {
|
|
|
4053
4153
|
const value = req.body;
|
|
4054
4154
|
const { error } = schemaApp.validate(value);
|
|
4055
4155
|
if (error) {
|
|
4056
|
-
next(new
|
|
4156
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4057
4157
|
return;
|
|
4058
4158
|
}
|
|
4059
4159
|
try {
|
|
@@ -4068,13 +4168,13 @@ function useAppController() {
|
|
|
4068
4168
|
const id = req.params.id ?? "";
|
|
4069
4169
|
const { error: errorId } = import_joi12.default.string().hex().required().validate(id);
|
|
4070
4170
|
if (errorId) {
|
|
4071
|
-
next(new
|
|
4171
|
+
next(new import_utils22.BadRequestError(errorId.message));
|
|
4072
4172
|
return;
|
|
4073
4173
|
}
|
|
4074
4174
|
const value = req.body;
|
|
4075
4175
|
const { error } = schemaAppUpdate.validate(value);
|
|
4076
4176
|
if (error) {
|
|
4077
|
-
next(new
|
|
4177
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4078
4178
|
return;
|
|
4079
4179
|
}
|
|
4080
4180
|
try {
|
|
@@ -4096,7 +4196,7 @@ function useAppController() {
|
|
|
4096
4196
|
});
|
|
4097
4197
|
const { error } = validation.validate(query);
|
|
4098
4198
|
if (error) {
|
|
4099
|
-
next(new
|
|
4199
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4100
4200
|
return;
|
|
4101
4201
|
}
|
|
4102
4202
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -4135,7 +4235,7 @@ function useAppController() {
|
|
|
4135
4235
|
});
|
|
4136
4236
|
const { error } = validation.validate({ id });
|
|
4137
4237
|
if (error) {
|
|
4138
|
-
next(new
|
|
4238
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4139
4239
|
return;
|
|
4140
4240
|
}
|
|
4141
4241
|
try {
|
|
@@ -4156,7 +4256,7 @@ function useAppController() {
|
|
|
4156
4256
|
});
|
|
4157
4257
|
const { error } = validation.validate({ id });
|
|
4158
4258
|
if (error) {
|
|
4159
|
-
next(new
|
|
4259
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4160
4260
|
return;
|
|
4161
4261
|
}
|
|
4162
4262
|
try {
|
|
@@ -4192,7 +4292,7 @@ function usePermissionGroupService() {
|
|
|
4192
4292
|
add: addPermission
|
|
4193
4293
|
} = usePermissionRepo();
|
|
4194
4294
|
async function addDefaultModule() {
|
|
4195
|
-
const session =
|
|
4295
|
+
const session = import_utils23.useAtlas.getClient()?.startSession();
|
|
4196
4296
|
if (!session) {
|
|
4197
4297
|
throw new Error("Failed to start database session.");
|
|
4198
4298
|
}
|
|
@@ -4302,7 +4402,7 @@ function usePermissionGroupService() {
|
|
|
4302
4402
|
key: module2.key,
|
|
4303
4403
|
label: module2.label
|
|
4304
4404
|
});
|
|
4305
|
-
|
|
4405
|
+
import_utils23.logger.log({
|
|
4306
4406
|
level: "info",
|
|
4307
4407
|
message: `Default permission group added: ${app.code} - ${module2.key}`
|
|
4308
4408
|
});
|
|
@@ -4511,7 +4611,7 @@ function usePermissionGroupService() {
|
|
|
4511
4611
|
key: module2.key,
|
|
4512
4612
|
label: module2.label
|
|
4513
4613
|
});
|
|
4514
|
-
|
|
4614
|
+
import_utils23.logger.log({
|
|
4515
4615
|
level: "info",
|
|
4516
4616
|
message: `Default permission group added: ${app.code} - ${module2.key}`
|
|
4517
4617
|
});
|
|
@@ -4536,7 +4636,7 @@ function usePermissionGroupService() {
|
|
|
4536
4636
|
}
|
|
4537
4637
|
await session.commitTransaction();
|
|
4538
4638
|
}
|
|
4539
|
-
|
|
4639
|
+
import_utils23.logger.log({
|
|
4540
4640
|
level: "info",
|
|
4541
4641
|
message: "Default permission groups added successfully."
|
|
4542
4642
|
});
|
|
@@ -4556,7 +4656,7 @@ function usePermissionGroupService() {
|
|
|
4556
4656
|
}
|
|
4557
4657
|
const associatedPermissionsCount = await countByGroup(permission.key);
|
|
4558
4658
|
if (associatedPermissionsCount > 0) {
|
|
4559
|
-
throw new
|
|
4659
|
+
throw new import_utils23.BadRequestError(
|
|
4560
4660
|
"Cannot delete Permission Group with associated Permissions."
|
|
4561
4661
|
);
|
|
4562
4662
|
}
|
|
@@ -4564,10 +4664,10 @@ function usePermissionGroupService() {
|
|
|
4564
4664
|
await _deleteById(id);
|
|
4565
4665
|
return "Permission deleted successfully.";
|
|
4566
4666
|
} catch (error) {
|
|
4567
|
-
if (error instanceof
|
|
4667
|
+
if (error instanceof import_utils23.AppError) {
|
|
4568
4668
|
throw error;
|
|
4569
4669
|
} else {
|
|
4570
|
-
throw new
|
|
4670
|
+
throw new import_utils23.InternalServerError("Failed to delete Permission Group.");
|
|
4571
4671
|
}
|
|
4572
4672
|
}
|
|
4573
4673
|
}
|
|
@@ -4578,7 +4678,7 @@ function usePermissionGroupService() {
|
|
|
4578
4678
|
}
|
|
4579
4679
|
|
|
4580
4680
|
// src/resources/permission/permission.group.controller.ts
|
|
4581
|
-
var
|
|
4681
|
+
var import_utils24 = require("@goweekdays/utils");
|
|
4582
4682
|
var import_joi13 = __toESM(require("joi"));
|
|
4583
4683
|
function usePermissionGroupController() {
|
|
4584
4684
|
const {
|
|
@@ -4592,8 +4692,8 @@ function usePermissionGroupController() {
|
|
|
4592
4692
|
const value = req.body;
|
|
4593
4693
|
const { error } = schemaPermissionGroup.validate(value);
|
|
4594
4694
|
if (error) {
|
|
4595
|
-
next(new
|
|
4596
|
-
|
|
4695
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4696
|
+
import_utils24.logger.info(`Controller: ${error.message}`);
|
|
4597
4697
|
return;
|
|
4598
4698
|
}
|
|
4599
4699
|
try {
|
|
@@ -4615,7 +4715,7 @@ function usePermissionGroupController() {
|
|
|
4615
4715
|
});
|
|
4616
4716
|
const { error } = validation.validate(query);
|
|
4617
4717
|
if (error) {
|
|
4618
|
-
next(new
|
|
4718
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4619
4719
|
return;
|
|
4620
4720
|
}
|
|
4621
4721
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -4654,7 +4754,7 @@ function usePermissionGroupController() {
|
|
|
4654
4754
|
});
|
|
4655
4755
|
const { error } = validation.validate({ id });
|
|
4656
4756
|
if (error) {
|
|
4657
|
-
next(new
|
|
4757
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4658
4758
|
return;
|
|
4659
4759
|
}
|
|
4660
4760
|
try {
|
|
@@ -4675,7 +4775,7 @@ function usePermissionGroupController() {
|
|
|
4675
4775
|
});
|
|
4676
4776
|
const { error } = validation.validate({ id });
|
|
4677
4777
|
if (error) {
|
|
4678
|
-
next(new
|
|
4778
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4679
4779
|
return;
|
|
4680
4780
|
}
|
|
4681
4781
|
try {
|
|
@@ -4690,13 +4790,13 @@ function usePermissionGroupController() {
|
|
|
4690
4790
|
const id = req.params.id;
|
|
4691
4791
|
const { error: errorId } = import_joi13.default.string().hex().required().validate(id);
|
|
4692
4792
|
if (errorId) {
|
|
4693
|
-
next(new
|
|
4793
|
+
next(new import_utils24.BadRequestError(errorId.message));
|
|
4694
4794
|
return;
|
|
4695
4795
|
}
|
|
4696
4796
|
const payload = req.body;
|
|
4697
4797
|
const { error } = schemaPermissionGroupUpdate.validate(payload);
|
|
4698
4798
|
if (error) {
|
|
4699
|
-
next(new
|
|
4799
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4700
4800
|
return;
|
|
4701
4801
|
}
|
|
4702
4802
|
try {
|
|
@@ -4731,16 +4831,16 @@ function useUserService() {
|
|
|
4731
4831
|
const { add: addMember } = useMemberRepo();
|
|
4732
4832
|
const { getAll: getAllPermission } = usePermissionRepo();
|
|
4733
4833
|
async function createDefaultUser() {
|
|
4734
|
-
const session =
|
|
4834
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4735
4835
|
try {
|
|
4736
4836
|
session?.startTransaction();
|
|
4737
4837
|
const _user = await getUserByEmail(DEFAULT_USER_EMAIL);
|
|
4738
4838
|
if (_user) {
|
|
4739
|
-
throw new
|
|
4839
|
+
throw new import_utils25.BadRequestError(
|
|
4740
4840
|
`User already exists: ${DEFAULT_USER_EMAIL}.`
|
|
4741
4841
|
);
|
|
4742
4842
|
}
|
|
4743
|
-
const hashedPassword = await (0,
|
|
4843
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(DEFAULT_USER_PASSWORD);
|
|
4744
4844
|
const userId = new import_mongodb15.ObjectId();
|
|
4745
4845
|
const user = {
|
|
4746
4846
|
_id: userId,
|
|
@@ -4801,14 +4901,14 @@ function useUserService() {
|
|
|
4801
4901
|
}
|
|
4802
4902
|
}
|
|
4803
4903
|
async function createUser(value) {
|
|
4804
|
-
const session =
|
|
4904
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4805
4905
|
session?.startTransaction();
|
|
4806
4906
|
try {
|
|
4807
4907
|
const _user = await getUserByEmail(value.email);
|
|
4808
4908
|
if (_user) {
|
|
4809
|
-
throw new
|
|
4909
|
+
throw new import_utils25.BadRequestError(`User already exists: ${value.email}.`);
|
|
4810
4910
|
}
|
|
4811
|
-
const hashedPassword = await (0,
|
|
4911
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(value.password);
|
|
4812
4912
|
const userId = new import_mongodb15.ObjectId();
|
|
4813
4913
|
const user = {
|
|
4814
4914
|
_id: userId,
|
|
@@ -4840,25 +4940,25 @@ function useUserService() {
|
|
|
4840
4940
|
lastName = "",
|
|
4841
4941
|
password = ""
|
|
4842
4942
|
} = {}) {
|
|
4843
|
-
const session =
|
|
4943
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4844
4944
|
session?.startTransaction();
|
|
4845
4945
|
try {
|
|
4846
4946
|
const invitation = await _getVerificationById(id);
|
|
4847
4947
|
if (!invitation || !invitation.metadata?.app || !invitation.metadata?.role) {
|
|
4848
|
-
throw new
|
|
4948
|
+
throw new import_utils25.BadRequestError("Invalid invitation.");
|
|
4849
4949
|
}
|
|
4850
4950
|
if (invitation.status === "complete") {
|
|
4851
|
-
throw new
|
|
4951
|
+
throw new import_utils25.BadRequestError("Invitation already used.");
|
|
4852
4952
|
}
|
|
4853
4953
|
const expired = new Date(invitation.expireAt) < /* @__PURE__ */ new Date();
|
|
4854
4954
|
if (invitation.status === "expired" || expired) {
|
|
4855
|
-
throw new
|
|
4955
|
+
throw new import_utils25.BadRequestError("Invitation expired.");
|
|
4856
4956
|
}
|
|
4857
4957
|
const email = invitation.email;
|
|
4858
4958
|
const _user = await getUserByEmail(invitation.email);
|
|
4859
4959
|
let userId = _user?._id ?? "";
|
|
4860
4960
|
if (!_user) {
|
|
4861
|
-
const hashedPassword = await (0,
|
|
4961
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(password);
|
|
4862
4962
|
const user = new MUser({
|
|
4863
4963
|
email,
|
|
4864
4964
|
password: hashedPassword,
|
|
@@ -4899,28 +4999,28 @@ function useUserService() {
|
|
|
4899
4999
|
lastName = "",
|
|
4900
5000
|
password = ""
|
|
4901
5001
|
} = {}) {
|
|
4902
|
-
const session =
|
|
5002
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4903
5003
|
session?.startTransaction();
|
|
4904
5004
|
try {
|
|
4905
5005
|
const signUp = await _getVerificationById(id);
|
|
4906
5006
|
if (!signUp) {
|
|
4907
|
-
throw new
|
|
5007
|
+
throw new import_utils25.BadRequestError("Invalid sign up link.");
|
|
4908
5008
|
}
|
|
4909
5009
|
if (signUp.status === "complete") {
|
|
4910
|
-
throw new
|
|
5010
|
+
throw new import_utils25.BadRequestError(
|
|
4911
5011
|
"You have already an account created using this link."
|
|
4912
5012
|
);
|
|
4913
5013
|
}
|
|
4914
5014
|
const expired = new Date(signUp.expireAt) < /* @__PURE__ */ new Date();
|
|
4915
5015
|
if (signUp.status === "expired" || expired) {
|
|
4916
|
-
throw new
|
|
5016
|
+
throw new import_utils25.BadRequestError("Sign up link expired.");
|
|
4917
5017
|
}
|
|
4918
5018
|
const email = signUp.email;
|
|
4919
5019
|
const _user = await getUserByEmail(signUp.email);
|
|
4920
5020
|
if (_user) {
|
|
4921
|
-
throw new
|
|
5021
|
+
throw new import_utils25.BadRequestError(`User already exists: ${email}.`);
|
|
4922
5022
|
}
|
|
4923
|
-
const hashedPassword = await (0,
|
|
5023
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(password);
|
|
4924
5024
|
const userId = new import_mongodb15.ObjectId();
|
|
4925
5025
|
const user = {
|
|
4926
5026
|
_id: userId,
|
|
@@ -4952,21 +5052,21 @@ function useUserService() {
|
|
|
4952
5052
|
throw error;
|
|
4953
5053
|
}
|
|
4954
5054
|
if (newPassword !== passwordConfirmation) {
|
|
4955
|
-
throw new
|
|
5055
|
+
throw new import_utils25.BadRequestError("Passwords do not match.");
|
|
4956
5056
|
}
|
|
4957
5057
|
let hashedPassword = "";
|
|
4958
5058
|
try {
|
|
4959
|
-
hashedPassword = await (0,
|
|
5059
|
+
hashedPassword = await (0, import_utils25.hashPassword)(newPassword);
|
|
4960
5060
|
} catch (error) {
|
|
4961
|
-
throw new
|
|
5061
|
+
throw new import_utils25.InternalServerError(`Error hashing password: ${error}`);
|
|
4962
5062
|
}
|
|
4963
5063
|
try {
|
|
4964
5064
|
const otpDoc = await getById(id);
|
|
4965
5065
|
if (!otpDoc) {
|
|
4966
|
-
throw new
|
|
5066
|
+
throw new import_utils25.NotFoundError("You are using an invalid reset link.");
|
|
4967
5067
|
}
|
|
4968
5068
|
if (otpDoc.status === "used") {
|
|
4969
|
-
throw new
|
|
5069
|
+
throw new import_utils25.BadRequestError("This link has already been invalidated.");
|
|
4970
5070
|
}
|
|
4971
5071
|
await updateStatusById(id, "used");
|
|
4972
5072
|
return "Successfully reset password.";
|
|
@@ -4977,22 +5077,22 @@ function useUserService() {
|
|
|
4977
5077
|
const { updateName: updateMemberName } = useMemberRepo();
|
|
4978
5078
|
async function updateName(_id, firstName, lastName) {
|
|
4979
5079
|
if (!_id) {
|
|
4980
|
-
throw new
|
|
5080
|
+
throw new import_utils25.BadRequestError("Invalid user ID");
|
|
4981
5081
|
}
|
|
4982
5082
|
if (!firstName) {
|
|
4983
|
-
throw new
|
|
5083
|
+
throw new import_utils25.BadRequestError("Invalid firstName");
|
|
4984
5084
|
}
|
|
4985
5085
|
if (!lastName) {
|
|
4986
|
-
throw new
|
|
5086
|
+
throw new import_utils25.BadRequestError("Invalid lastName");
|
|
4987
5087
|
}
|
|
4988
|
-
const session =
|
|
5088
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4989
5089
|
session?.startTransaction();
|
|
4990
|
-
const cacheKey = (0,
|
|
5090
|
+
const cacheKey = (0, import_utils25.makeCacheKey)("users", { user: _id });
|
|
4991
5091
|
try {
|
|
4992
|
-
(0,
|
|
4993
|
-
|
|
5092
|
+
(0, import_utils25.useCache)().delCache(cacheKey).then(() => {
|
|
5093
|
+
import_utils25.logger.info(`Cache cleared for user: ${_id}`);
|
|
4994
5094
|
}).catch((error) => {
|
|
4995
|
-
|
|
5095
|
+
import_utils25.logger.error(`Failed to clear cache for user: ${_id}`, error);
|
|
4996
5096
|
});
|
|
4997
5097
|
await _updateName({ _id, firstName, lastName }, session);
|
|
4998
5098
|
await updateMemberName(
|
|
@@ -5010,16 +5110,16 @@ function useUserService() {
|
|
|
5010
5110
|
}
|
|
5011
5111
|
async function updateBirthday(_id, month, day, year) {
|
|
5012
5112
|
if (!_id) {
|
|
5013
|
-
throw new
|
|
5113
|
+
throw new import_utils25.BadRequestError("Invalid user ID");
|
|
5014
5114
|
}
|
|
5015
5115
|
if (!month) {
|
|
5016
|
-
throw new
|
|
5116
|
+
throw new import_utils25.BadRequestError("Invalid birth month.");
|
|
5017
5117
|
}
|
|
5018
5118
|
if (!day) {
|
|
5019
|
-
throw new
|
|
5119
|
+
throw new import_utils25.BadRequestError("Invalid birthday.");
|
|
5020
5120
|
}
|
|
5021
5121
|
if (!year) {
|
|
5022
|
-
throw new
|
|
5122
|
+
throw new import_utils25.BadRequestError("Invalid birth year.");
|
|
5023
5123
|
}
|
|
5024
5124
|
try {
|
|
5025
5125
|
await _updateBirthday({ _id, month, day, year });
|
|
@@ -5036,7 +5136,7 @@ function useUserService() {
|
|
|
5036
5136
|
}
|
|
5037
5137
|
}
|
|
5038
5138
|
const { createFile: _createFile, deleteFileById } = useFileRepo();
|
|
5039
|
-
const s3 = new
|
|
5139
|
+
const s3 = new import_utils25.useS3({
|
|
5040
5140
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
5041
5141
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
5042
5142
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -5044,7 +5144,7 @@ function useUserService() {
|
|
|
5044
5144
|
bucket: SPACES_BUCKET
|
|
5045
5145
|
});
|
|
5046
5146
|
async function updateUserProfile({ file, user, previousProfile } = {}) {
|
|
5047
|
-
const session =
|
|
5147
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
5048
5148
|
session?.startTransaction();
|
|
5049
5149
|
const _file = {
|
|
5050
5150
|
name: file.originalname,
|
|
@@ -5100,7 +5200,7 @@ function useAuthController() {
|
|
|
5100
5200
|
});
|
|
5101
5201
|
const { error } = validation.validate({ email, password });
|
|
5102
5202
|
if (error) {
|
|
5103
|
-
next(new
|
|
5203
|
+
next(new import_utils26.BadRequestError(error.message));
|
|
5104
5204
|
return;
|
|
5105
5205
|
}
|
|
5106
5206
|
try {
|
|
@@ -5116,14 +5216,14 @@ function useAuthController() {
|
|
|
5116
5216
|
res.cookie("sid", session.sid, cookieOptions).cookie("user", session.user, cookieOptions).json({ message: "Login successful" });
|
|
5117
5217
|
return;
|
|
5118
5218
|
} catch (error2) {
|
|
5119
|
-
|
|
5219
|
+
import_utils26.logger.log({
|
|
5120
5220
|
level: "error",
|
|
5121
5221
|
message: `Error during login: ${error2.message}`
|
|
5122
5222
|
});
|
|
5123
|
-
if (error2 instanceof
|
|
5223
|
+
if (error2 instanceof import_utils26.AppError) {
|
|
5124
5224
|
next(error2);
|
|
5125
5225
|
} else {
|
|
5126
|
-
next(new
|
|
5226
|
+
next(new import_utils26.InternalServerError("An unexpected error occurred"));
|
|
5127
5227
|
}
|
|
5128
5228
|
return;
|
|
5129
5229
|
}
|
|
@@ -5131,17 +5231,17 @@ function useAuthController() {
|
|
|
5131
5231
|
async function logout(req, res, next) {
|
|
5132
5232
|
const sid = req.headers["authorization"] ?? "";
|
|
5133
5233
|
if (!sid) {
|
|
5134
|
-
next(new
|
|
5234
|
+
next(new import_utils26.BadRequestError("Session ID is required"));
|
|
5135
5235
|
return;
|
|
5136
5236
|
}
|
|
5137
5237
|
try {
|
|
5138
5238
|
await useAuthService().logout(sid);
|
|
5139
5239
|
res.json({ message: "Logged out successfully" });
|
|
5140
5240
|
} catch (error) {
|
|
5141
|
-
if (error instanceof
|
|
5241
|
+
if (error instanceof import_utils26.AppError) {
|
|
5142
5242
|
next(error);
|
|
5143
5243
|
} else {
|
|
5144
|
-
next(new
|
|
5244
|
+
next(new import_utils26.InternalServerError("An unexpected error occurred"));
|
|
5145
5245
|
}
|
|
5146
5246
|
}
|
|
5147
5247
|
}
|
|
@@ -5160,7 +5260,7 @@ function useAuthController() {
|
|
|
5160
5260
|
passwordConfirmation
|
|
5161
5261
|
});
|
|
5162
5262
|
if (error) {
|
|
5163
|
-
next(new
|
|
5263
|
+
next(new import_utils26.BadRequestError(error.message));
|
|
5164
5264
|
return;
|
|
5165
5265
|
}
|
|
5166
5266
|
try {
|
|
@@ -5184,7 +5284,7 @@ function useAuthController() {
|
|
|
5184
5284
|
});
|
|
5185
5285
|
const { error } = validation.validate({ email, referralCode });
|
|
5186
5286
|
if (error) {
|
|
5187
|
-
next(new
|
|
5287
|
+
next(new import_utils26.BadRequestError(error.message));
|
|
5188
5288
|
return;
|
|
5189
5289
|
}
|
|
5190
5290
|
try {
|
|
@@ -5209,7 +5309,7 @@ function useAuthController() {
|
|
|
5209
5309
|
}
|
|
5210
5310
|
|
|
5211
5311
|
// src/resources/building/building.model.ts
|
|
5212
|
-
var
|
|
5312
|
+
var import_utils27 = require("@goweekdays/utils");
|
|
5213
5313
|
var import_joi15 = __toESM(require("joi"));
|
|
5214
5314
|
var import_mongodb16 = require("mongodb");
|
|
5215
5315
|
var schemaBuilding = import_joi15.default.object({
|
|
@@ -5253,20 +5353,20 @@ var schemaUpdateOptions = import_joi15.default.object({
|
|
|
5253
5353
|
function MBuilding(value) {
|
|
5254
5354
|
const { error } = schemaBuilding.validate(value);
|
|
5255
5355
|
if (error) {
|
|
5256
|
-
|
|
5257
|
-
throw new
|
|
5356
|
+
import_utils27.logger.info(`Building Model: ${error.message}`);
|
|
5357
|
+
throw new import_utils27.BadRequestError(error.message);
|
|
5258
5358
|
}
|
|
5259
5359
|
if (value._id && typeof value._id === "string") {
|
|
5260
5360
|
try {
|
|
5261
5361
|
value._id = new import_mongodb16.ObjectId(value._id);
|
|
5262
5362
|
} catch (error2) {
|
|
5263
|
-
throw new
|
|
5363
|
+
throw new import_utils27.BadRequestError("Invalid _id format");
|
|
5264
5364
|
}
|
|
5265
5365
|
}
|
|
5266
5366
|
try {
|
|
5267
5367
|
value.school = new import_mongodb16.ObjectId(value.school);
|
|
5268
5368
|
} catch (error2) {
|
|
5269
|
-
throw new
|
|
5369
|
+
throw new import_utils27.BadRequestError("Invalid school format");
|
|
5270
5370
|
}
|
|
5271
5371
|
return {
|
|
5272
5372
|
_id: value._id ?? void 0,
|
|
@@ -5283,25 +5383,25 @@ function MBuilding(value) {
|
|
|
5283
5383
|
function MBuildingUnit(value) {
|
|
5284
5384
|
const { error } = schemaBuildingUnit.validate(value);
|
|
5285
5385
|
if (error) {
|
|
5286
|
-
|
|
5287
|
-
throw new
|
|
5386
|
+
import_utils27.logger.info(`Building Unit Model: ${error.message}`);
|
|
5387
|
+
throw new import_utils27.BadRequestError(error.message);
|
|
5288
5388
|
}
|
|
5289
5389
|
if (value._id && typeof value._id === "string") {
|
|
5290
5390
|
try {
|
|
5291
5391
|
value._id = new import_mongodb16.ObjectId(value._id);
|
|
5292
5392
|
} catch (error2) {
|
|
5293
|
-
throw new
|
|
5393
|
+
throw new import_utils27.BadRequestError("Invalid ID");
|
|
5294
5394
|
}
|
|
5295
5395
|
}
|
|
5296
5396
|
try {
|
|
5297
5397
|
value.school = new import_mongodb16.ObjectId(value.school);
|
|
5298
5398
|
} catch (error2) {
|
|
5299
|
-
throw new
|
|
5399
|
+
throw new import_utils27.BadRequestError("Invalid school ID");
|
|
5300
5400
|
}
|
|
5301
5401
|
try {
|
|
5302
5402
|
value.building = new import_mongodb16.ObjectId(value.building);
|
|
5303
5403
|
} catch (error2) {
|
|
5304
|
-
throw new
|
|
5404
|
+
throw new import_utils27.BadRequestError("Invalid building ID");
|
|
5305
5405
|
}
|
|
5306
5406
|
return {
|
|
5307
5407
|
_id: value._id ?? void 0,
|
|
@@ -5325,16 +5425,16 @@ function MBuildingUnit(value) {
|
|
|
5325
5425
|
}
|
|
5326
5426
|
|
|
5327
5427
|
// src/resources/building/building.repository.ts
|
|
5328
|
-
var
|
|
5428
|
+
var import_utils28 = require("@goweekdays/utils");
|
|
5329
5429
|
var import_mongodb17 = require("mongodb");
|
|
5330
5430
|
function useBuildingRepo() {
|
|
5331
|
-
const db =
|
|
5431
|
+
const db = import_utils28.useAtlas.getDb();
|
|
5332
5432
|
if (!db) {
|
|
5333
5433
|
throw new Error("Unable to connect to server.");
|
|
5334
5434
|
}
|
|
5335
5435
|
const namespace_collection = "school.buildings";
|
|
5336
5436
|
const collection = db.collection(namespace_collection);
|
|
5337
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
5437
|
+
const { getCache, setCache, delNamespace } = (0, import_utils28.useCache)(namespace_collection);
|
|
5338
5438
|
async function createIndexes() {
|
|
5339
5439
|
try {
|
|
5340
5440
|
await collection.createIndexes([
|
|
@@ -5347,7 +5447,7 @@ function useBuildingRepo() {
|
|
|
5347
5447
|
}
|
|
5348
5448
|
}
|
|
5349
5449
|
createIndexes().catch((error) => {
|
|
5350
|
-
|
|
5450
|
+
import_utils28.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
5351
5451
|
});
|
|
5352
5452
|
async function add(value, session) {
|
|
5353
5453
|
try {
|
|
@@ -5356,16 +5456,16 @@ function useBuildingRepo() {
|
|
|
5356
5456
|
delCachedData();
|
|
5357
5457
|
return res.insertedId;
|
|
5358
5458
|
} catch (error) {
|
|
5359
|
-
|
|
5459
|
+
import_utils28.logger.log({
|
|
5360
5460
|
level: "error",
|
|
5361
5461
|
message: error.message
|
|
5362
5462
|
});
|
|
5363
|
-
if (error instanceof
|
|
5463
|
+
if (error instanceof import_utils28.AppError) {
|
|
5364
5464
|
throw error;
|
|
5365
5465
|
} else {
|
|
5366
5466
|
const isDuplicated = error.message.includes("duplicate");
|
|
5367
5467
|
if (isDuplicated) {
|
|
5368
|
-
throw new
|
|
5468
|
+
throw new import_utils28.BadRequestError("Building already exists.");
|
|
5369
5469
|
}
|
|
5370
5470
|
throw new Error("Failed to create building.");
|
|
5371
5471
|
}
|
|
@@ -5375,7 +5475,7 @@ function useBuildingRepo() {
|
|
|
5375
5475
|
try {
|
|
5376
5476
|
_id = new import_mongodb17.ObjectId(_id);
|
|
5377
5477
|
} catch (error) {
|
|
5378
|
-
throw new
|
|
5478
|
+
throw new import_utils28.BadRequestError("Invalid ID.");
|
|
5379
5479
|
}
|
|
5380
5480
|
try {
|
|
5381
5481
|
const res = await collection.updateOne(
|
|
@@ -5386,11 +5486,11 @@ function useBuildingRepo() {
|
|
|
5386
5486
|
delCachedData();
|
|
5387
5487
|
return res;
|
|
5388
5488
|
} catch (error) {
|
|
5389
|
-
|
|
5489
|
+
import_utils28.logger.log({
|
|
5390
5490
|
level: "error",
|
|
5391
5491
|
message: error.message
|
|
5392
5492
|
});
|
|
5393
|
-
if (error instanceof
|
|
5493
|
+
if (error instanceof import_utils28.AppError) {
|
|
5394
5494
|
throw error;
|
|
5395
5495
|
} else {
|
|
5396
5496
|
throw new Error("Failed to update building.");
|
|
@@ -5417,7 +5517,7 @@ function useBuildingRepo() {
|
|
|
5417
5517
|
try {
|
|
5418
5518
|
query.school = new import_mongodb17.ObjectId(school);
|
|
5419
5519
|
} catch (error) {
|
|
5420
|
-
throw new
|
|
5520
|
+
throw new import_utils28.BadRequestError("Invalid school ID.");
|
|
5421
5521
|
}
|
|
5422
5522
|
}
|
|
5423
5523
|
const cacheParams = {
|
|
@@ -5431,15 +5531,15 @@ function useBuildingRepo() {
|
|
|
5431
5531
|
cacheParams.school = school;
|
|
5432
5532
|
if (status !== "active")
|
|
5433
5533
|
cacheParams.status = status;
|
|
5434
|
-
const cacheKey = (0,
|
|
5435
|
-
|
|
5534
|
+
const cacheKey = (0, import_utils28.makeCacheKey)(namespace_collection, cacheParams);
|
|
5535
|
+
import_utils28.logger.log({
|
|
5436
5536
|
level: "info",
|
|
5437
5537
|
message: `Cache key for getAll buildings: ${cacheKey}`
|
|
5438
5538
|
});
|
|
5439
5539
|
try {
|
|
5440
5540
|
const cached = await getCache(cacheKey);
|
|
5441
5541
|
if (cached) {
|
|
5442
|
-
|
|
5542
|
+
import_utils28.logger.log({
|
|
5443
5543
|
level: "info",
|
|
5444
5544
|
message: `Cache hit for getAll buildings: ${cacheKey}`
|
|
5445
5545
|
});
|
|
@@ -5452,21 +5552,21 @@ function useBuildingRepo() {
|
|
|
5452
5552
|
{ $limit: limit }
|
|
5453
5553
|
]).toArray();
|
|
5454
5554
|
const length = await collection.countDocuments(query);
|
|
5455
|
-
const data = (0,
|
|
5555
|
+
const data = (0, import_utils28.paginate)(items, page, limit, length);
|
|
5456
5556
|
setCache(cacheKey, data, 600).then(() => {
|
|
5457
|
-
|
|
5557
|
+
import_utils28.logger.log({
|
|
5458
5558
|
level: "info",
|
|
5459
5559
|
message: `Cache set for getAll buildings: ${cacheKey}`
|
|
5460
5560
|
});
|
|
5461
5561
|
}).catch((err) => {
|
|
5462
|
-
|
|
5562
|
+
import_utils28.logger.log({
|
|
5463
5563
|
level: "error",
|
|
5464
5564
|
message: `Failed to set cache for getAll buildings: ${err.message}`
|
|
5465
5565
|
});
|
|
5466
5566
|
});
|
|
5467
5567
|
return data;
|
|
5468
5568
|
} catch (error) {
|
|
5469
|
-
|
|
5569
|
+
import_utils28.logger.log({ level: "error", message: `${error}` });
|
|
5470
5570
|
throw error;
|
|
5471
5571
|
}
|
|
5472
5572
|
}
|
|
@@ -5474,13 +5574,13 @@ function useBuildingRepo() {
|
|
|
5474
5574
|
try {
|
|
5475
5575
|
_id = new import_mongodb17.ObjectId(_id);
|
|
5476
5576
|
} catch (error) {
|
|
5477
|
-
throw new
|
|
5577
|
+
throw new import_utils28.BadRequestError("Invalid ID.");
|
|
5478
5578
|
}
|
|
5479
|
-
const cacheKey = (0,
|
|
5579
|
+
const cacheKey = (0, import_utils28.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
5480
5580
|
try {
|
|
5481
5581
|
const cached = await getCache(cacheKey);
|
|
5482
5582
|
if (cached) {
|
|
5483
|
-
|
|
5583
|
+
import_utils28.logger.log({
|
|
5484
5584
|
level: "info",
|
|
5485
5585
|
message: `Cache hit for getById building: ${cacheKey}`
|
|
5486
5586
|
});
|
|
@@ -5490,22 +5590,22 @@ function useBuildingRepo() {
|
|
|
5490
5590
|
_id
|
|
5491
5591
|
});
|
|
5492
5592
|
setCache(cacheKey, result, 300).then(() => {
|
|
5493
|
-
|
|
5593
|
+
import_utils28.logger.log({
|
|
5494
5594
|
level: "info",
|
|
5495
5595
|
message: `Cache set for building by id: ${cacheKey}`
|
|
5496
5596
|
});
|
|
5497
5597
|
}).catch((err) => {
|
|
5498
|
-
|
|
5598
|
+
import_utils28.logger.log({
|
|
5499
5599
|
level: "error",
|
|
5500
5600
|
message: `Failed to set cache for building by id: ${err.message}`
|
|
5501
5601
|
});
|
|
5502
5602
|
});
|
|
5503
5603
|
return result;
|
|
5504
5604
|
} catch (error) {
|
|
5505
|
-
if (error instanceof
|
|
5605
|
+
if (error instanceof import_utils28.AppError) {
|
|
5506
5606
|
throw error;
|
|
5507
5607
|
} else {
|
|
5508
|
-
throw new
|
|
5608
|
+
throw new import_utils28.InternalServerError("Failed to get building.");
|
|
5509
5609
|
}
|
|
5510
5610
|
}
|
|
5511
5611
|
}
|
|
@@ -5513,7 +5613,7 @@ function useBuildingRepo() {
|
|
|
5513
5613
|
try {
|
|
5514
5614
|
_id = new import_mongodb17.ObjectId(_id);
|
|
5515
5615
|
} catch (error) {
|
|
5516
|
-
throw new
|
|
5616
|
+
throw new import_utils28.BadRequestError("Invalid ID.");
|
|
5517
5617
|
}
|
|
5518
5618
|
try {
|
|
5519
5619
|
const res = await collection.updateOne(
|
|
@@ -5523,25 +5623,25 @@ function useBuildingRepo() {
|
|
|
5523
5623
|
delCachedData();
|
|
5524
5624
|
return res;
|
|
5525
5625
|
} catch (error) {
|
|
5526
|
-
|
|
5626
|
+
import_utils28.logger.log({
|
|
5527
5627
|
level: "error",
|
|
5528
5628
|
message: error.message
|
|
5529
5629
|
});
|
|
5530
|
-
if (error instanceof
|
|
5630
|
+
if (error instanceof import_utils28.AppError) {
|
|
5531
5631
|
throw error;
|
|
5532
5632
|
} else {
|
|
5533
|
-
throw new
|
|
5633
|
+
throw new import_utils28.InternalServerError("Failed to delete building.");
|
|
5534
5634
|
}
|
|
5535
5635
|
}
|
|
5536
5636
|
}
|
|
5537
5637
|
function delCachedData() {
|
|
5538
5638
|
delNamespace().then(() => {
|
|
5539
|
-
|
|
5639
|
+
import_utils28.logger.log({
|
|
5540
5640
|
level: "info",
|
|
5541
5641
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
5542
5642
|
});
|
|
5543
5643
|
}).catch((err) => {
|
|
5544
|
-
|
|
5644
|
+
import_utils28.logger.log({
|
|
5545
5645
|
level: "error",
|
|
5546
5646
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
5547
5647
|
});
|
|
@@ -5558,19 +5658,19 @@ function useBuildingRepo() {
|
|
|
5558
5658
|
}
|
|
5559
5659
|
|
|
5560
5660
|
// src/resources/building/building.service.ts
|
|
5561
|
-
var
|
|
5661
|
+
var import_utils30 = require("@goweekdays/utils");
|
|
5562
5662
|
|
|
5563
5663
|
// src/resources/building/building-unit.repository.ts
|
|
5564
|
-
var
|
|
5664
|
+
var import_utils29 = require("@goweekdays/utils");
|
|
5565
5665
|
var import_mongodb18 = require("mongodb");
|
|
5566
5666
|
function useBuildingUnitRepo() {
|
|
5567
|
-
const db =
|
|
5667
|
+
const db = import_utils29.useAtlas.getDb();
|
|
5568
5668
|
if (!db) {
|
|
5569
5669
|
throw new Error("Unable to connect to server.");
|
|
5570
5670
|
}
|
|
5571
5671
|
const namespace_collection = "school.building-units";
|
|
5572
5672
|
const collection = db.collection(namespace_collection);
|
|
5573
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
5673
|
+
const { getCache, setCache, delNamespace } = (0, import_utils29.useCache)(namespace_collection);
|
|
5574
5674
|
async function createIndexes() {
|
|
5575
5675
|
try {
|
|
5576
5676
|
await collection.createIndexes([
|
|
@@ -5598,12 +5698,12 @@ function useBuildingUnitRepo() {
|
|
|
5598
5698
|
}
|
|
5599
5699
|
function delCachedData() {
|
|
5600
5700
|
delNamespace().then(() => {
|
|
5601
|
-
|
|
5701
|
+
import_utils29.logger.log({
|
|
5602
5702
|
level: "info",
|
|
5603
5703
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
5604
5704
|
});
|
|
5605
5705
|
}).catch((err) => {
|
|
5606
|
-
|
|
5706
|
+
import_utils29.logger.log({
|
|
5607
5707
|
level: "error",
|
|
5608
5708
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
5609
5709
|
});
|
|
@@ -5616,11 +5716,11 @@ function useBuildingUnitRepo() {
|
|
|
5616
5716
|
delCachedData();
|
|
5617
5717
|
return res.insertedId;
|
|
5618
5718
|
} catch (error) {
|
|
5619
|
-
|
|
5719
|
+
import_utils29.logger.log({
|
|
5620
5720
|
level: "error",
|
|
5621
5721
|
message: error.message
|
|
5622
5722
|
});
|
|
5623
|
-
if (error instanceof
|
|
5723
|
+
if (error instanceof import_utils29.AppError) {
|
|
5624
5724
|
throw error;
|
|
5625
5725
|
} else {
|
|
5626
5726
|
throw new Error("Failed to create building unit.");
|
|
@@ -5630,12 +5730,12 @@ function useBuildingUnitRepo() {
|
|
|
5630
5730
|
async function updateById(_id, value, session) {
|
|
5631
5731
|
const { error } = schemaUpdateOptions.validate(value);
|
|
5632
5732
|
if (error) {
|
|
5633
|
-
throw new
|
|
5733
|
+
throw new import_utils29.BadRequestError(error.message);
|
|
5634
5734
|
}
|
|
5635
5735
|
try {
|
|
5636
5736
|
_id = new import_mongodb18.ObjectId(_id);
|
|
5637
5737
|
} catch (error2) {
|
|
5638
|
-
throw new
|
|
5738
|
+
throw new import_utils29.BadRequestError("Invalid ID.");
|
|
5639
5739
|
}
|
|
5640
5740
|
try {
|
|
5641
5741
|
const res = await collection.updateOne(
|
|
@@ -5646,11 +5746,11 @@ function useBuildingUnitRepo() {
|
|
|
5646
5746
|
delCachedData();
|
|
5647
5747
|
return res;
|
|
5648
5748
|
} catch (error2) {
|
|
5649
|
-
|
|
5749
|
+
import_utils29.logger.log({
|
|
5650
5750
|
level: "error",
|
|
5651
5751
|
message: error2.message
|
|
5652
5752
|
});
|
|
5653
|
-
if (error2 instanceof
|
|
5753
|
+
if (error2 instanceof import_utils29.AppError) {
|
|
5654
5754
|
throw error2;
|
|
5655
5755
|
} else {
|
|
5656
5756
|
throw new Error("Failed to create building unit.");
|
|
@@ -5660,12 +5760,12 @@ function useBuildingUnitRepo() {
|
|
|
5660
5760
|
async function updateByBuildingId(building, value, session) {
|
|
5661
5761
|
const { error } = schemaUpdateOptions.validate(value);
|
|
5662
5762
|
if (error) {
|
|
5663
|
-
throw new
|
|
5763
|
+
throw new import_utils29.BadRequestError(error.message);
|
|
5664
5764
|
}
|
|
5665
5765
|
try {
|
|
5666
5766
|
building = new import_mongodb18.ObjectId(building);
|
|
5667
5767
|
} catch (error2) {
|
|
5668
|
-
throw new
|
|
5768
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5669
5769
|
}
|
|
5670
5770
|
try {
|
|
5671
5771
|
const res = await collection.updateMany(
|
|
@@ -5676,11 +5776,11 @@ function useBuildingUnitRepo() {
|
|
|
5676
5776
|
delCachedData();
|
|
5677
5777
|
return res;
|
|
5678
5778
|
} catch (error2) {
|
|
5679
|
-
|
|
5779
|
+
import_utils29.logger.log({
|
|
5680
5780
|
level: "error",
|
|
5681
5781
|
message: error2.message
|
|
5682
5782
|
});
|
|
5683
|
-
if (error2 instanceof
|
|
5783
|
+
if (error2 instanceof import_utils29.AppError) {
|
|
5684
5784
|
throw error2;
|
|
5685
5785
|
} else {
|
|
5686
5786
|
throw new Error("Failed to update building unit.");
|
|
@@ -5709,14 +5809,14 @@ function useBuildingUnitRepo() {
|
|
|
5709
5809
|
try {
|
|
5710
5810
|
query.school = new import_mongodb18.ObjectId(school);
|
|
5711
5811
|
} catch (error) {
|
|
5712
|
-
throw new
|
|
5812
|
+
throw new import_utils29.BadRequestError("Invalid school ID.");
|
|
5713
5813
|
}
|
|
5714
5814
|
}
|
|
5715
5815
|
if (building) {
|
|
5716
5816
|
try {
|
|
5717
5817
|
query.building = new import_mongodb18.ObjectId(building);
|
|
5718
5818
|
} catch (error) {
|
|
5719
|
-
throw new
|
|
5819
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5720
5820
|
}
|
|
5721
5821
|
}
|
|
5722
5822
|
const cacheParams = {
|
|
@@ -5732,15 +5832,15 @@ function useBuildingUnitRepo() {
|
|
|
5732
5832
|
cacheParams.building = building;
|
|
5733
5833
|
if (status !== "active")
|
|
5734
5834
|
cacheParams.status = status;
|
|
5735
|
-
const cacheKey = (0,
|
|
5736
|
-
|
|
5835
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, cacheParams);
|
|
5836
|
+
import_utils29.logger.log({
|
|
5737
5837
|
level: "info",
|
|
5738
5838
|
message: `Cache key for getAll building units: ${cacheKey}`
|
|
5739
5839
|
});
|
|
5740
5840
|
try {
|
|
5741
5841
|
const cached = await getCache(cacheKey);
|
|
5742
5842
|
if (cached) {
|
|
5743
|
-
|
|
5843
|
+
import_utils29.logger.log({
|
|
5744
5844
|
level: "info",
|
|
5745
5845
|
message: `Cache hit for getAll building units: ${cacheKey}`
|
|
5746
5846
|
});
|
|
@@ -5753,21 +5853,21 @@ function useBuildingUnitRepo() {
|
|
|
5753
5853
|
{ $limit: limit }
|
|
5754
5854
|
]).toArray();
|
|
5755
5855
|
const length = await collection.countDocuments(query);
|
|
5756
|
-
const data = (0,
|
|
5856
|
+
const data = (0, import_utils29.paginate)(items, page, limit, length);
|
|
5757
5857
|
setCache(cacheKey, data, 600).then(() => {
|
|
5758
|
-
|
|
5858
|
+
import_utils29.logger.log({
|
|
5759
5859
|
level: "info",
|
|
5760
5860
|
message: `Cache set for getAll building units: ${cacheKey}`
|
|
5761
5861
|
});
|
|
5762
5862
|
}).catch((err) => {
|
|
5763
|
-
|
|
5863
|
+
import_utils29.logger.log({
|
|
5764
5864
|
level: "error",
|
|
5765
5865
|
message: `Failed to set cache for getAll building units: ${err.message}`
|
|
5766
5866
|
});
|
|
5767
5867
|
});
|
|
5768
5868
|
return data;
|
|
5769
5869
|
} catch (error) {
|
|
5770
|
-
|
|
5870
|
+
import_utils29.logger.log({ level: "error", message: `${error}` });
|
|
5771
5871
|
throw error;
|
|
5772
5872
|
}
|
|
5773
5873
|
}
|
|
@@ -5775,13 +5875,13 @@ function useBuildingUnitRepo() {
|
|
|
5775
5875
|
try {
|
|
5776
5876
|
_id = new import_mongodb18.ObjectId(_id);
|
|
5777
5877
|
} catch (error) {
|
|
5778
|
-
throw new
|
|
5878
|
+
throw new import_utils29.BadRequestError("Invalid ID.");
|
|
5779
5879
|
}
|
|
5780
|
-
const cacheKey = (0,
|
|
5880
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
5781
5881
|
try {
|
|
5782
5882
|
const cached = await getCache(cacheKey);
|
|
5783
5883
|
if (cached) {
|
|
5784
|
-
|
|
5884
|
+
import_utils29.logger.log({
|
|
5785
5885
|
level: "info",
|
|
5786
5886
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
5787
5887
|
});
|
|
@@ -5792,25 +5892,25 @@ function useBuildingUnitRepo() {
|
|
|
5792
5892
|
deletedAt: { $in: ["", null] }
|
|
5793
5893
|
});
|
|
5794
5894
|
if (!result) {
|
|
5795
|
-
throw new
|
|
5895
|
+
throw new import_utils29.BadRequestError("Building unit not found.");
|
|
5796
5896
|
}
|
|
5797
5897
|
setCache(cacheKey, result, 300).then(() => {
|
|
5798
|
-
|
|
5898
|
+
import_utils29.logger.log({
|
|
5799
5899
|
level: "info",
|
|
5800
5900
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
5801
5901
|
});
|
|
5802
5902
|
}).catch((err) => {
|
|
5803
|
-
|
|
5903
|
+
import_utils29.logger.log({
|
|
5804
5904
|
level: "error",
|
|
5805
5905
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
5806
5906
|
});
|
|
5807
5907
|
});
|
|
5808
5908
|
return result;
|
|
5809
5909
|
} catch (error) {
|
|
5810
|
-
if (error instanceof
|
|
5910
|
+
if (error instanceof import_utils29.AppError) {
|
|
5811
5911
|
throw error;
|
|
5812
5912
|
} else {
|
|
5813
|
-
throw new
|
|
5913
|
+
throw new import_utils29.InternalServerError("Failed to get building unit.");
|
|
5814
5914
|
}
|
|
5815
5915
|
}
|
|
5816
5916
|
}
|
|
@@ -5818,16 +5918,16 @@ function useBuildingUnitRepo() {
|
|
|
5818
5918
|
try {
|
|
5819
5919
|
building = new import_mongodb18.ObjectId(building);
|
|
5820
5920
|
} catch (error) {
|
|
5821
|
-
throw new
|
|
5921
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5822
5922
|
}
|
|
5823
|
-
const cacheKey = (0,
|
|
5923
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, {
|
|
5824
5924
|
building: String(building),
|
|
5825
5925
|
level
|
|
5826
5926
|
});
|
|
5827
5927
|
try {
|
|
5828
5928
|
const cached = await getCache(cacheKey);
|
|
5829
5929
|
if (cached) {
|
|
5830
|
-
|
|
5930
|
+
import_utils29.logger.log({
|
|
5831
5931
|
level: "info",
|
|
5832
5932
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
5833
5933
|
});
|
|
@@ -5839,22 +5939,22 @@ function useBuildingUnitRepo() {
|
|
|
5839
5939
|
status: "active"
|
|
5840
5940
|
});
|
|
5841
5941
|
setCache(cacheKey, result, 300).then(() => {
|
|
5842
|
-
|
|
5942
|
+
import_utils29.logger.log({
|
|
5843
5943
|
level: "info",
|
|
5844
5944
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
5845
5945
|
});
|
|
5846
5946
|
}).catch((err) => {
|
|
5847
|
-
|
|
5947
|
+
import_utils29.logger.log({
|
|
5848
5948
|
level: "error",
|
|
5849
5949
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
5850
5950
|
});
|
|
5851
5951
|
});
|
|
5852
5952
|
return result;
|
|
5853
5953
|
} catch (error) {
|
|
5854
|
-
if (error instanceof
|
|
5954
|
+
if (error instanceof import_utils29.AppError) {
|
|
5855
5955
|
throw error;
|
|
5856
5956
|
} else {
|
|
5857
|
-
throw new
|
|
5957
|
+
throw new import_utils29.InternalServerError("Failed to get building unit.");
|
|
5858
5958
|
}
|
|
5859
5959
|
}
|
|
5860
5960
|
}
|
|
@@ -5862,15 +5962,15 @@ function useBuildingUnitRepo() {
|
|
|
5862
5962
|
try {
|
|
5863
5963
|
building = new import_mongodb18.ObjectId(building);
|
|
5864
5964
|
} catch (error) {
|
|
5865
|
-
throw new
|
|
5965
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5866
5966
|
}
|
|
5867
|
-
const cacheKey = (0,
|
|
5967
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, {
|
|
5868
5968
|
building: String(building)
|
|
5869
5969
|
});
|
|
5870
5970
|
try {
|
|
5871
5971
|
const cached = await getCache(cacheKey);
|
|
5872
5972
|
if (cached) {
|
|
5873
|
-
|
|
5973
|
+
import_utils29.logger.log({
|
|
5874
5974
|
level: "info",
|
|
5875
5975
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
5876
5976
|
});
|
|
@@ -5881,22 +5981,22 @@ function useBuildingUnitRepo() {
|
|
|
5881
5981
|
status: "active"
|
|
5882
5982
|
});
|
|
5883
5983
|
setCache(cacheKey, result, 300).then(() => {
|
|
5884
|
-
|
|
5984
|
+
import_utils29.logger.log({
|
|
5885
5985
|
level: "info",
|
|
5886
5986
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
5887
5987
|
});
|
|
5888
5988
|
}).catch((err) => {
|
|
5889
|
-
|
|
5989
|
+
import_utils29.logger.log({
|
|
5890
5990
|
level: "error",
|
|
5891
5991
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
5892
5992
|
});
|
|
5893
5993
|
});
|
|
5894
5994
|
return result;
|
|
5895
5995
|
} catch (error) {
|
|
5896
|
-
if (error instanceof
|
|
5996
|
+
if (error instanceof import_utils29.AppError) {
|
|
5897
5997
|
throw error;
|
|
5898
5998
|
} else {
|
|
5899
|
-
throw new
|
|
5999
|
+
throw new import_utils29.InternalServerError("Failed to get building unit.");
|
|
5900
6000
|
}
|
|
5901
6001
|
}
|
|
5902
6002
|
}
|
|
@@ -5904,7 +6004,7 @@ function useBuildingUnitRepo() {
|
|
|
5904
6004
|
try {
|
|
5905
6005
|
_id = new import_mongodb18.ObjectId(_id);
|
|
5906
6006
|
} catch (error) {
|
|
5907
|
-
throw new
|
|
6007
|
+
throw new import_utils29.BadRequestError("Invalid ID.");
|
|
5908
6008
|
}
|
|
5909
6009
|
try {
|
|
5910
6010
|
const res = await collection.updateOne(
|
|
@@ -5915,11 +6015,11 @@ function useBuildingUnitRepo() {
|
|
|
5915
6015
|
delCachedData();
|
|
5916
6016
|
return "Room/Facility deleted successfully.";
|
|
5917
6017
|
} catch (error) {
|
|
5918
|
-
|
|
6018
|
+
import_utils29.logger.log({
|
|
5919
6019
|
level: "error",
|
|
5920
6020
|
message: error.message
|
|
5921
6021
|
});
|
|
5922
|
-
if (error instanceof
|
|
6022
|
+
if (error instanceof import_utils29.AppError) {
|
|
5923
6023
|
throw error;
|
|
5924
6024
|
} else {
|
|
5925
6025
|
throw new Error("Failed to deleted room/facility.");
|
|
@@ -5949,16 +6049,16 @@ function useBuildingService() {
|
|
|
5949
6049
|
const { getByBuildingLevel, getByBuilding, updateByBuildingId } = useBuildingUnitRepo();
|
|
5950
6050
|
async function updateById(id, data) {
|
|
5951
6051
|
data.levels = Number(data.levels);
|
|
5952
|
-
const session =
|
|
6052
|
+
const session = import_utils30.useAtlas.getClient()?.startSession();
|
|
5953
6053
|
try {
|
|
5954
6054
|
const building = await _getById(id);
|
|
5955
6055
|
if (!building) {
|
|
5956
|
-
throw new
|
|
6056
|
+
throw new import_utils30.NotFoundError("Building not found.");
|
|
5957
6057
|
}
|
|
5958
6058
|
if (data.levels < building.levels) {
|
|
5959
6059
|
const unit = await getByBuildingLevel(id, building.levels);
|
|
5960
6060
|
if (unit) {
|
|
5961
|
-
throw new
|
|
6061
|
+
throw new import_utils30.BadRequestError(
|
|
5962
6062
|
"Cannot reduce floors, there are existing building units at higher floors."
|
|
5963
6063
|
);
|
|
5964
6064
|
}
|
|
@@ -5980,7 +6080,7 @@ function useBuildingService() {
|
|
|
5980
6080
|
async function deleteById(id) {
|
|
5981
6081
|
const building = await getByBuilding(id);
|
|
5982
6082
|
if (building) {
|
|
5983
|
-
throw new
|
|
6083
|
+
throw new import_utils30.BadRequestError(
|
|
5984
6084
|
"Cannot delete building with existing room/facility. Please delete room/facility first."
|
|
5985
6085
|
);
|
|
5986
6086
|
}
|
|
@@ -5998,7 +6098,7 @@ function useBuildingService() {
|
|
|
5998
6098
|
}
|
|
5999
6099
|
|
|
6000
6100
|
// src/resources/building/building.controller.ts
|
|
6001
|
-
var
|
|
6101
|
+
var import_utils31 = require("@goweekdays/utils");
|
|
6002
6102
|
var import_joi16 = __toESM(require("joi"));
|
|
6003
6103
|
function useBuildingController() {
|
|
6004
6104
|
const { getAll: _getAll, getById: _getById, add: _add } = useBuildingRepo();
|
|
@@ -6014,8 +6114,8 @@ function useBuildingController() {
|
|
|
6014
6114
|
});
|
|
6015
6115
|
const { error } = validation.validate(value);
|
|
6016
6116
|
if (error) {
|
|
6017
|
-
next(new
|
|
6018
|
-
|
|
6117
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6118
|
+
import_utils31.logger.info(`Controller: ${error.message}`);
|
|
6019
6119
|
return;
|
|
6020
6120
|
}
|
|
6021
6121
|
try {
|
|
@@ -6039,8 +6139,8 @@ function useBuildingController() {
|
|
|
6039
6139
|
});
|
|
6040
6140
|
const { error } = validation.validate({ id, value });
|
|
6041
6141
|
if (error) {
|
|
6042
|
-
next(new
|
|
6043
|
-
|
|
6142
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6143
|
+
import_utils31.logger.info(`Controller: ${error.message}`);
|
|
6044
6144
|
return;
|
|
6045
6145
|
}
|
|
6046
6146
|
try {
|
|
@@ -6062,7 +6162,7 @@ function useBuildingController() {
|
|
|
6062
6162
|
});
|
|
6063
6163
|
const { error } = validation.validate(query);
|
|
6064
6164
|
if (error) {
|
|
6065
|
-
next(new
|
|
6165
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6066
6166
|
return;
|
|
6067
6167
|
}
|
|
6068
6168
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -6101,7 +6201,7 @@ function useBuildingController() {
|
|
|
6101
6201
|
});
|
|
6102
6202
|
const { error } = validation.validate({ id });
|
|
6103
6203
|
if (error) {
|
|
6104
|
-
next(new
|
|
6204
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6105
6205
|
return;
|
|
6106
6206
|
}
|
|
6107
6207
|
try {
|
|
@@ -6122,7 +6222,7 @@ function useBuildingController() {
|
|
|
6122
6222
|
});
|
|
6123
6223
|
const { error } = validation.validate({ id });
|
|
6124
6224
|
if (error) {
|
|
6125
|
-
next(new
|
|
6225
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6126
6226
|
return;
|
|
6127
6227
|
}
|
|
6128
6228
|
try {
|
|
@@ -6143,11 +6243,11 @@ function useBuildingController() {
|
|
|
6143
6243
|
}
|
|
6144
6244
|
|
|
6145
6245
|
// src/resources/building/building-unit.service.ts
|
|
6146
|
-
var
|
|
6246
|
+
var import_utils32 = require("@goweekdays/utils");
|
|
6147
6247
|
function useBuildingUnitService() {
|
|
6148
6248
|
const { add: _add } = useBuildingUnitRepo();
|
|
6149
6249
|
async function add(value) {
|
|
6150
|
-
const session =
|
|
6250
|
+
const session = import_utils32.useAtlas.getClient()?.startSession();
|
|
6151
6251
|
if (!session) {
|
|
6152
6252
|
throw new Error("Unable to start session for building unit service.");
|
|
6153
6253
|
}
|
|
@@ -6174,7 +6274,7 @@ function useBuildingUnitService() {
|
|
|
6174
6274
|
}
|
|
6175
6275
|
|
|
6176
6276
|
// src/resources/building/building-unit.controller.ts
|
|
6177
|
-
var
|
|
6277
|
+
var import_utils33 = require("@goweekdays/utils");
|
|
6178
6278
|
var import_joi17 = __toESM(require("joi"));
|
|
6179
6279
|
function useBuildingUnitController() {
|
|
6180
6280
|
const {
|
|
@@ -6206,7 +6306,7 @@ function useBuildingUnitController() {
|
|
|
6206
6306
|
});
|
|
6207
6307
|
const { error } = validation.validate(data);
|
|
6208
6308
|
if (error) {
|
|
6209
|
-
next(new
|
|
6309
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6210
6310
|
return;
|
|
6211
6311
|
}
|
|
6212
6312
|
try {
|
|
@@ -6228,7 +6328,7 @@ function useBuildingUnitController() {
|
|
|
6228
6328
|
});
|
|
6229
6329
|
const { error } = validation.validate({ id, value: data });
|
|
6230
6330
|
if (error) {
|
|
6231
|
-
next(new
|
|
6331
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6232
6332
|
return;
|
|
6233
6333
|
}
|
|
6234
6334
|
try {
|
|
@@ -6253,7 +6353,7 @@ function useBuildingUnitController() {
|
|
|
6253
6353
|
});
|
|
6254
6354
|
const { error } = validation.validate(query);
|
|
6255
6355
|
if (error) {
|
|
6256
|
-
next(new
|
|
6356
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6257
6357
|
return;
|
|
6258
6358
|
}
|
|
6259
6359
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -6294,7 +6394,7 @@ function useBuildingUnitController() {
|
|
|
6294
6394
|
});
|
|
6295
6395
|
const { error } = validation.validate({ id });
|
|
6296
6396
|
if (error) {
|
|
6297
|
-
next(new
|
|
6397
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6298
6398
|
return;
|
|
6299
6399
|
}
|
|
6300
6400
|
try {
|
|
@@ -6315,7 +6415,7 @@ function useBuildingUnitController() {
|
|
|
6315
6415
|
});
|
|
6316
6416
|
const { error } = validation.validate({ id });
|
|
6317
6417
|
if (error) {
|
|
6318
|
-
next(new
|
|
6418
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6319
6419
|
return;
|
|
6320
6420
|
}
|
|
6321
6421
|
try {
|
|
@@ -6336,7 +6436,7 @@ function useBuildingUnitController() {
|
|
|
6336
6436
|
}
|
|
6337
6437
|
|
|
6338
6438
|
// src/resources/counter/counter.model.ts
|
|
6339
|
-
var
|
|
6439
|
+
var import_utils34 = require("@goweekdays/utils");
|
|
6340
6440
|
var import_mongodb19 = require("mongodb");
|
|
6341
6441
|
var import_zod = require("zod");
|
|
6342
6442
|
var TCounter = import_zod.z.object({
|
|
@@ -6356,7 +6456,7 @@ function useCounterModel(db) {
|
|
|
6356
6456
|
try {
|
|
6357
6457
|
return TCounter.parse(value);
|
|
6358
6458
|
} catch (error) {
|
|
6359
|
-
throw new
|
|
6459
|
+
throw new import_utils34.BadRequestError(error.issues[0].message);
|
|
6360
6460
|
}
|
|
6361
6461
|
}
|
|
6362
6462
|
function validateCounter(data) {
|
|
@@ -6370,23 +6470,23 @@ function useCounterModel(db) {
|
|
|
6370
6470
|
}
|
|
6371
6471
|
|
|
6372
6472
|
// src/resources/counter/counter.repository.ts
|
|
6373
|
-
var
|
|
6473
|
+
var import_utils35 = require("@goweekdays/utils");
|
|
6374
6474
|
function useCounterRepo() {
|
|
6375
|
-
const db =
|
|
6475
|
+
const db = import_utils35.useAtlas.getDb();
|
|
6376
6476
|
if (!db) {
|
|
6377
6477
|
throw new Error("Unable to connect to server.");
|
|
6378
6478
|
}
|
|
6379
6479
|
const namespace_collection = "counters";
|
|
6380
6480
|
const { collection, createCounter } = useCounterModel(db);
|
|
6381
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
6481
|
+
const { getCache, setCache, delNamespace } = (0, import_utils35.useCache)(namespace_collection);
|
|
6382
6482
|
function delCachedData() {
|
|
6383
6483
|
delNamespace().then(() => {
|
|
6384
|
-
|
|
6484
|
+
import_utils35.logger.log({
|
|
6385
6485
|
level: "info",
|
|
6386
6486
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
6387
6487
|
});
|
|
6388
6488
|
}).catch((err) => {
|
|
6389
|
-
|
|
6489
|
+
import_utils35.logger.log({
|
|
6390
6490
|
level: "error",
|
|
6391
6491
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
6392
6492
|
});
|
|
@@ -6438,11 +6538,11 @@ function useCounterRepo() {
|
|
|
6438
6538
|
}
|
|
6439
6539
|
}
|
|
6440
6540
|
async function getByType(type) {
|
|
6441
|
-
const cacheKey = (0,
|
|
6541
|
+
const cacheKey = (0, import_utils35.makeCacheKey)(namespace_collection, { type });
|
|
6442
6542
|
try {
|
|
6443
6543
|
const cached = await getCache(cacheKey);
|
|
6444
6544
|
if (cached) {
|
|
6445
|
-
|
|
6545
|
+
import_utils35.logger.log({
|
|
6446
6546
|
level: "info",
|
|
6447
6547
|
message: `Cache hit for getByType counter: ${cacheKey}`
|
|
6448
6548
|
});
|
|
@@ -6451,12 +6551,12 @@ function useCounterRepo() {
|
|
|
6451
6551
|
const data = await collection.findOne({ type });
|
|
6452
6552
|
if (data) {
|
|
6453
6553
|
setCache(cacheKey, data, 300).then(() => {
|
|
6454
|
-
|
|
6554
|
+
import_utils35.logger.log({
|
|
6455
6555
|
level: "info",
|
|
6456
6556
|
message: `Cache set for counter by type: ${cacheKey}`
|
|
6457
6557
|
});
|
|
6458
6558
|
}).catch((err) => {
|
|
6459
|
-
|
|
6559
|
+
import_utils35.logger.log({
|
|
6460
6560
|
level: "error",
|
|
6461
6561
|
message: `Failed to set cache for counter by type: ${err.message}`
|
|
6462
6562
|
});
|
|
@@ -6477,7 +6577,7 @@ function useCounterRepo() {
|
|
|
6477
6577
|
}
|
|
6478
6578
|
|
|
6479
6579
|
// src/resources/file/file.service.ts
|
|
6480
|
-
var
|
|
6580
|
+
var import_utils36 = require("@goweekdays/utils");
|
|
6481
6581
|
var import_node_cron = __toESM(require("node-cron"));
|
|
6482
6582
|
var fs = __toESM(require("fs"));
|
|
6483
6583
|
function useFileService() {
|
|
@@ -6486,7 +6586,7 @@ function useFileService() {
|
|
|
6486
6586
|
deleteFileById,
|
|
6487
6587
|
getAllDraftedFiles
|
|
6488
6588
|
} = useFileRepo();
|
|
6489
|
-
const s3 = new
|
|
6589
|
+
const s3 = new import_utils36.useS3({
|
|
6490
6590
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
6491
6591
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
6492
6592
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -6495,7 +6595,7 @@ function useFileService() {
|
|
|
6495
6595
|
forcePathStyle: true
|
|
6496
6596
|
});
|
|
6497
6597
|
async function createFile(value) {
|
|
6498
|
-
const session =
|
|
6598
|
+
const session = import_utils36.useAtlas.getClient()?.startSession();
|
|
6499
6599
|
session?.startTransaction();
|
|
6500
6600
|
const file = {
|
|
6501
6601
|
name: value.originalname,
|
|
@@ -6529,7 +6629,7 @@ function useFileService() {
|
|
|
6529
6629
|
}
|
|
6530
6630
|
}
|
|
6531
6631
|
async function deleteFile(id) {
|
|
6532
|
-
const session =
|
|
6632
|
+
const session = import_utils36.useAtlas.getClient()?.startSession();
|
|
6533
6633
|
session?.startTransaction();
|
|
6534
6634
|
try {
|
|
6535
6635
|
await deleteFileById(id, session);
|
|
@@ -6550,12 +6650,12 @@ function useFileService() {
|
|
|
6550
6650
|
const file = files[index];
|
|
6551
6651
|
try {
|
|
6552
6652
|
await deleteFile(file._id.toString());
|
|
6553
|
-
await
|
|
6653
|
+
await import_utils36.logger.log({
|
|
6554
6654
|
level: "info",
|
|
6555
6655
|
message: "Successfully deleted draft files."
|
|
6556
6656
|
});
|
|
6557
6657
|
} catch (error) {
|
|
6558
|
-
|
|
6658
|
+
import_utils36.logger.log({
|
|
6559
6659
|
level: "info",
|
|
6560
6660
|
message: "Successfully deleted draft files."
|
|
6561
6661
|
});
|
|
@@ -6572,7 +6672,7 @@ function useFileService() {
|
|
|
6572
6672
|
}
|
|
6573
6673
|
|
|
6574
6674
|
// src/resources/file/file.controller.ts
|
|
6575
|
-
var
|
|
6675
|
+
var import_utils37 = require("@goweekdays/utils");
|
|
6576
6676
|
var import_joi18 = __toESM(require("joi"));
|
|
6577
6677
|
function useFileController() {
|
|
6578
6678
|
const { createFile, deleteFile: _deleteFile } = useFileService();
|
|
@@ -6586,10 +6686,10 @@ function useFileController() {
|
|
|
6586
6686
|
res.json({ message: "Successfully uploaded file", id });
|
|
6587
6687
|
return;
|
|
6588
6688
|
} catch (error) {
|
|
6589
|
-
if (error instanceof
|
|
6689
|
+
if (error instanceof import_utils37.AppError) {
|
|
6590
6690
|
next(error);
|
|
6591
6691
|
} else {
|
|
6592
|
-
next(new
|
|
6692
|
+
next(new import_utils37.InternalServerError(error));
|
|
6593
6693
|
}
|
|
6594
6694
|
}
|
|
6595
6695
|
}
|
|
@@ -6598,17 +6698,17 @@ function useFileController() {
|
|
|
6598
6698
|
const validation = import_joi18.default.string().required();
|
|
6599
6699
|
const { error } = validation.validate(id);
|
|
6600
6700
|
if (error) {
|
|
6601
|
-
next(new
|
|
6701
|
+
next(new import_utils37.BadRequestError(error.message));
|
|
6602
6702
|
}
|
|
6603
6703
|
try {
|
|
6604
6704
|
const message = await _deleteFile(id);
|
|
6605
6705
|
res.json({ message });
|
|
6606
6706
|
return;
|
|
6607
6707
|
} catch (error2) {
|
|
6608
|
-
if (error2 instanceof
|
|
6708
|
+
if (error2 instanceof import_utils37.AppError) {
|
|
6609
6709
|
next(error2);
|
|
6610
6710
|
} else {
|
|
6611
|
-
next(new
|
|
6711
|
+
next(new import_utils37.InternalServerError(error2));
|
|
6612
6712
|
}
|
|
6613
6713
|
}
|
|
6614
6714
|
}
|
|
@@ -6620,7 +6720,7 @@ function useFileController() {
|
|
|
6620
6720
|
|
|
6621
6721
|
// src/resources/member/member.controller.ts
|
|
6622
6722
|
var import_joi19 = __toESM(require("joi"));
|
|
6623
|
-
var
|
|
6723
|
+
var import_utils38 = require("@goweekdays/utils");
|
|
6624
6724
|
function useMemberController() {
|
|
6625
6725
|
const {
|
|
6626
6726
|
getByUserId: _getByUserId,
|
|
@@ -6636,7 +6736,7 @@ function useMemberController() {
|
|
|
6636
6736
|
});
|
|
6637
6737
|
const { error } = validation.validate({ id: userId });
|
|
6638
6738
|
if (error) {
|
|
6639
|
-
next(new
|
|
6739
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6640
6740
|
return;
|
|
6641
6741
|
}
|
|
6642
6742
|
try {
|
|
@@ -6658,7 +6758,7 @@ function useMemberController() {
|
|
|
6658
6758
|
});
|
|
6659
6759
|
const { error } = validation.validate({ ...req.params, ...req.query });
|
|
6660
6760
|
if (error) {
|
|
6661
|
-
next(new
|
|
6761
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6662
6762
|
return;
|
|
6663
6763
|
}
|
|
6664
6764
|
const orgId = req.query.org;
|
|
@@ -6702,7 +6802,7 @@ function useMemberController() {
|
|
|
6702
6802
|
status
|
|
6703
6803
|
});
|
|
6704
6804
|
if (error) {
|
|
6705
|
-
next(new
|
|
6805
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6706
6806
|
return;
|
|
6707
6807
|
}
|
|
6708
6808
|
try {
|
|
@@ -6739,7 +6839,7 @@ function useMemberController() {
|
|
|
6739
6839
|
limit
|
|
6740
6840
|
});
|
|
6741
6841
|
if (error) {
|
|
6742
|
-
next(new
|
|
6842
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6743
6843
|
}
|
|
6744
6844
|
try {
|
|
6745
6845
|
const items = await _getOrgsByMembership({
|
|
@@ -6761,7 +6861,7 @@ function useMemberController() {
|
|
|
6761
6861
|
});
|
|
6762
6862
|
const { error } = validation.validate(req.params);
|
|
6763
6863
|
if (error) {
|
|
6764
|
-
next(new
|
|
6864
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6765
6865
|
return;
|
|
6766
6866
|
}
|
|
6767
6867
|
const id = req.params.id;
|
|
@@ -6783,42 +6883,32 @@ function useMemberController() {
|
|
|
6783
6883
|
}
|
|
6784
6884
|
|
|
6785
6885
|
// src/resources/organization/organization.model.ts
|
|
6786
|
-
var
|
|
6886
|
+
var import_utils39 = require("@goweekdays/utils");
|
|
6787
6887
|
var import_joi20 = __toESM(require("joi"));
|
|
6788
6888
|
var import_mongodb20 = require("mongodb");
|
|
6789
|
-
var OrgTypes = [
|
|
6790
|
-
"marketplace",
|
|
6791
|
-
"stay",
|
|
6792
|
-
"eat",
|
|
6793
|
-
"service",
|
|
6794
|
-
"ride",
|
|
6795
|
-
"experience"
|
|
6796
|
-
];
|
|
6797
6889
|
var schemaOrg = import_joi20.default.object({
|
|
6798
6890
|
name: import_joi20.default.string().max(255).required(),
|
|
6799
6891
|
description: import_joi20.default.string().max(1024).optional().allow("", null),
|
|
6800
|
-
type: import_joi20.default.string().allow(...OrgTypes).required(),
|
|
6801
6892
|
email: import_joi20.default.string().email().max(255).optional().allow("", null),
|
|
6802
6893
|
contact: import_joi20.default.string().max(50).optional().allow("", null),
|
|
6803
6894
|
createdBy: import_joi20.default.string().hex().required()
|
|
6804
6895
|
});
|
|
6805
|
-
function
|
|
6896
|
+
function modelOrg(value) {
|
|
6806
6897
|
const { error } = schemaOrg.validate(value);
|
|
6807
6898
|
if (error) {
|
|
6808
|
-
throw new
|
|
6899
|
+
throw new import_utils39.BadRequestError(error.message);
|
|
6809
6900
|
}
|
|
6810
6901
|
if (value.createdBy && typeof value.createdBy === "string") {
|
|
6811
6902
|
try {
|
|
6812
6903
|
value.createdBy = new import_mongodb20.ObjectId(value.createdBy);
|
|
6813
6904
|
} catch (error2) {
|
|
6814
|
-
throw new
|
|
6905
|
+
throw new import_utils39.BadRequestError("Invalid createdBy ObjectId");
|
|
6815
6906
|
}
|
|
6816
6907
|
}
|
|
6817
6908
|
return {
|
|
6818
6909
|
_id: value._id,
|
|
6819
6910
|
name: value.name,
|
|
6820
|
-
description: value.description,
|
|
6821
|
-
type: value.type,
|
|
6911
|
+
description: value.description ?? "",
|
|
6822
6912
|
email: value.email,
|
|
6823
6913
|
contact: value.contact,
|
|
6824
6914
|
createdBy: value.createdBy,
|
|
@@ -6830,16 +6920,16 @@ function MOrg(value) {
|
|
|
6830
6920
|
}
|
|
6831
6921
|
|
|
6832
6922
|
// src/resources/organization/organization.repository.ts
|
|
6833
|
-
var
|
|
6923
|
+
var import_utils40 = require("@goweekdays/utils");
|
|
6834
6924
|
var import_mongodb21 = require("mongodb");
|
|
6835
6925
|
function useOrgRepo() {
|
|
6836
|
-
const db =
|
|
6926
|
+
const db = import_utils40.useAtlas.getDb();
|
|
6837
6927
|
if (!db) {
|
|
6838
6928
|
throw new Error("Unable to connect to server.");
|
|
6839
6929
|
}
|
|
6840
6930
|
const namespace_collection = "organizations";
|
|
6841
6931
|
const collection = db.collection(namespace_collection);
|
|
6842
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
6932
|
+
const { getCache, setCache, delNamespace } = (0, import_utils40.useCache)(namespace_collection);
|
|
6843
6933
|
async function createIndexes() {
|
|
6844
6934
|
try {
|
|
6845
6935
|
await collection.createIndexes([
|
|
@@ -6855,16 +6945,16 @@ function useOrgRepo() {
|
|
|
6855
6945
|
}
|
|
6856
6946
|
}
|
|
6857
6947
|
createIndexes().catch((error) => {
|
|
6858
|
-
|
|
6948
|
+
import_utils40.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
6859
6949
|
});
|
|
6860
6950
|
function delCachedData() {
|
|
6861
6951
|
delNamespace().then(() => {
|
|
6862
|
-
|
|
6952
|
+
import_utils40.logger.log({
|
|
6863
6953
|
level: "info",
|
|
6864
6954
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
6865
6955
|
});
|
|
6866
6956
|
}).catch((err) => {
|
|
6867
|
-
|
|
6957
|
+
import_utils40.logger.log({
|
|
6868
6958
|
level: "error",
|
|
6869
6959
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
6870
6960
|
});
|
|
@@ -6872,21 +6962,21 @@ function useOrgRepo() {
|
|
|
6872
6962
|
}
|
|
6873
6963
|
async function add(value, session) {
|
|
6874
6964
|
try {
|
|
6875
|
-
value =
|
|
6965
|
+
value = modelOrg(value);
|
|
6876
6966
|
const res = await collection.insertOne(value, { session });
|
|
6877
6967
|
delCachedData();
|
|
6878
6968
|
return res.insertedId;
|
|
6879
6969
|
} catch (error) {
|
|
6880
|
-
|
|
6970
|
+
import_utils40.logger.log({
|
|
6881
6971
|
level: "error",
|
|
6882
6972
|
message: error.message
|
|
6883
6973
|
});
|
|
6884
|
-
if (error instanceof
|
|
6974
|
+
if (error instanceof import_utils40.AppError) {
|
|
6885
6975
|
throw error;
|
|
6886
6976
|
} else {
|
|
6887
6977
|
const isDuplicated = error.message.includes("duplicate");
|
|
6888
6978
|
if (isDuplicated) {
|
|
6889
|
-
throw new
|
|
6979
|
+
throw new import_utils40.BadRequestError("Organization already exist.");
|
|
6890
6980
|
}
|
|
6891
6981
|
throw new Error("Failed to create organization.");
|
|
6892
6982
|
}
|
|
@@ -6905,21 +6995,21 @@ function useOrgRepo() {
|
|
|
6905
6995
|
if (search) {
|
|
6906
6996
|
query.$text = { $search: search };
|
|
6907
6997
|
}
|
|
6908
|
-
const cacheKey = (0,
|
|
6998
|
+
const cacheKey = (0, import_utils40.makeCacheKey)(namespace_collection, {
|
|
6909
6999
|
search,
|
|
6910
7000
|
page,
|
|
6911
7001
|
limit,
|
|
6912
7002
|
sort: JSON.stringify(sort),
|
|
6913
7003
|
status
|
|
6914
7004
|
});
|
|
6915
|
-
|
|
7005
|
+
import_utils40.logger.log({
|
|
6916
7006
|
level: "info",
|
|
6917
7007
|
message: `Cache key for getAll organizations: ${cacheKey}`
|
|
6918
7008
|
});
|
|
6919
7009
|
try {
|
|
6920
7010
|
const cached = await getCache(cacheKey);
|
|
6921
7011
|
if (cached) {
|
|
6922
|
-
|
|
7012
|
+
import_utils40.logger.log({
|
|
6923
7013
|
level: "info",
|
|
6924
7014
|
message: `Cache hit for getAll organizations: ${cacheKey}`
|
|
6925
7015
|
});
|
|
@@ -6941,21 +7031,21 @@ function useOrgRepo() {
|
|
|
6941
7031
|
}
|
|
6942
7032
|
]).toArray();
|
|
6943
7033
|
const length = await collection.countDocuments(query);
|
|
6944
|
-
const data = (0,
|
|
7034
|
+
const data = (0, import_utils40.paginate)(items, page, limit, length);
|
|
6945
7035
|
setCache(cacheKey, data, 600).then(() => {
|
|
6946
|
-
|
|
7036
|
+
import_utils40.logger.log({
|
|
6947
7037
|
level: "info",
|
|
6948
7038
|
message: `Cache set for getAll organizations: ${cacheKey}`
|
|
6949
7039
|
});
|
|
6950
7040
|
}).catch((err) => {
|
|
6951
|
-
|
|
7041
|
+
import_utils40.logger.log({
|
|
6952
7042
|
level: "error",
|
|
6953
7043
|
message: `Failed to set cache for getAll organizations: ${err.message}`
|
|
6954
7044
|
});
|
|
6955
7045
|
});
|
|
6956
7046
|
return data;
|
|
6957
7047
|
} catch (error) {
|
|
6958
|
-
|
|
7048
|
+
import_utils40.logger.log({ level: "error", message: `${error}` });
|
|
6959
7049
|
throw error;
|
|
6960
7050
|
}
|
|
6961
7051
|
}
|
|
@@ -6963,13 +7053,13 @@ function useOrgRepo() {
|
|
|
6963
7053
|
try {
|
|
6964
7054
|
_id = new import_mongodb21.ObjectId(_id);
|
|
6965
7055
|
} catch (error) {
|
|
6966
|
-
throw new
|
|
7056
|
+
throw new import_utils40.BadRequestError("Invalid ID.");
|
|
6967
7057
|
}
|
|
6968
|
-
const cacheKey = (0,
|
|
7058
|
+
const cacheKey = (0, import_utils40.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
6969
7059
|
try {
|
|
6970
7060
|
const cached = await getCache(cacheKey);
|
|
6971
7061
|
if (cached) {
|
|
6972
|
-
|
|
7062
|
+
import_utils40.logger.log({
|
|
6973
7063
|
level: "info",
|
|
6974
7064
|
message: `Cache hit for getById organization: ${cacheKey}`
|
|
6975
7065
|
});
|
|
@@ -6977,34 +7067,34 @@ function useOrgRepo() {
|
|
|
6977
7067
|
}
|
|
6978
7068
|
const result = await collection.findOne({ _id });
|
|
6979
7069
|
if (!result) {
|
|
6980
|
-
throw new
|
|
7070
|
+
throw new import_utils40.BadRequestError("Organization not found.");
|
|
6981
7071
|
}
|
|
6982
7072
|
setCache(cacheKey, result, 300).then(() => {
|
|
6983
|
-
|
|
7073
|
+
import_utils40.logger.log({
|
|
6984
7074
|
level: "info",
|
|
6985
7075
|
message: `Cache set for organization by id: ${cacheKey}`
|
|
6986
7076
|
});
|
|
6987
7077
|
}).catch((err) => {
|
|
6988
|
-
|
|
7078
|
+
import_utils40.logger.log({
|
|
6989
7079
|
level: "error",
|
|
6990
7080
|
message: `Failed to set cache for organization by id: ${err.message}`
|
|
6991
7081
|
});
|
|
6992
7082
|
});
|
|
6993
7083
|
return result;
|
|
6994
7084
|
} catch (error) {
|
|
6995
|
-
if (error instanceof
|
|
7085
|
+
if (error instanceof import_utils40.AppError) {
|
|
6996
7086
|
throw error;
|
|
6997
7087
|
} else {
|
|
6998
|
-
throw new
|
|
7088
|
+
throw new import_utils40.InternalServerError("Failed to get organization.");
|
|
6999
7089
|
}
|
|
7000
7090
|
}
|
|
7001
7091
|
}
|
|
7002
7092
|
async function getByName(name) {
|
|
7003
|
-
const cacheKey = (0,
|
|
7093
|
+
const cacheKey = (0, import_utils40.makeCacheKey)(namespace_collection, { name });
|
|
7004
7094
|
try {
|
|
7005
7095
|
const cached = await getCache(cacheKey);
|
|
7006
7096
|
if (cached) {
|
|
7007
|
-
|
|
7097
|
+
import_utils40.logger.log({
|
|
7008
7098
|
level: "info",
|
|
7009
7099
|
message: `Cache hit for getByName organization: ${cacheKey}`
|
|
7010
7100
|
});
|
|
@@ -7012,39 +7102,39 @@ function useOrgRepo() {
|
|
|
7012
7102
|
}
|
|
7013
7103
|
const result = await collection.findOne({ name });
|
|
7014
7104
|
if (!result) {
|
|
7015
|
-
throw new
|
|
7105
|
+
throw new import_utils40.BadRequestError("Organization not found.");
|
|
7016
7106
|
}
|
|
7017
7107
|
setCache(cacheKey, result, 300).then(() => {
|
|
7018
|
-
|
|
7108
|
+
import_utils40.logger.log({
|
|
7019
7109
|
level: "info",
|
|
7020
7110
|
message: `Cache set for organization by name: ${cacheKey}`
|
|
7021
7111
|
});
|
|
7022
7112
|
}).catch((err) => {
|
|
7023
|
-
|
|
7113
|
+
import_utils40.logger.log({
|
|
7024
7114
|
level: "error",
|
|
7025
7115
|
message: `Failed to set cache for organization by name: ${err.message}`
|
|
7026
7116
|
});
|
|
7027
7117
|
});
|
|
7028
7118
|
return result;
|
|
7029
7119
|
} catch (error) {
|
|
7030
|
-
if (error instanceof
|
|
7120
|
+
if (error instanceof import_utils40.AppError) {
|
|
7031
7121
|
throw error;
|
|
7032
7122
|
} else {
|
|
7033
|
-
throw new
|
|
7123
|
+
throw new import_utils40.InternalServerError("Failed to get organization.");
|
|
7034
7124
|
}
|
|
7035
7125
|
}
|
|
7036
7126
|
}
|
|
7037
7127
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
7038
7128
|
const allowedFields = ["name", "description"];
|
|
7039
7129
|
if (!allowedFields.includes(field)) {
|
|
7040
|
-
throw new
|
|
7130
|
+
throw new import_utils40.BadRequestError(
|
|
7041
7131
|
`Field "${field}" is not allowed to be updated.`
|
|
7042
7132
|
);
|
|
7043
7133
|
}
|
|
7044
7134
|
try {
|
|
7045
7135
|
_id = new import_mongodb21.ObjectId(_id);
|
|
7046
7136
|
} catch (error) {
|
|
7047
|
-
throw new
|
|
7137
|
+
throw new import_utils40.BadRequestError("Invalid ID.");
|
|
7048
7138
|
}
|
|
7049
7139
|
try {
|
|
7050
7140
|
await collection.updateOne(
|
|
@@ -7056,14 +7146,14 @@ function useOrgRepo() {
|
|
|
7056
7146
|
delCachedData();
|
|
7057
7147
|
return `Successfully updated user ${field}.`;
|
|
7058
7148
|
} catch (error) {
|
|
7059
|
-
throw new
|
|
7149
|
+
throw new import_utils40.InternalServerError(`Failed to update organization ${field}.`);
|
|
7060
7150
|
}
|
|
7061
7151
|
}
|
|
7062
7152
|
async function deleteById(_id) {
|
|
7063
7153
|
try {
|
|
7064
7154
|
_id = new import_mongodb21.ObjectId(_id);
|
|
7065
7155
|
} catch (error) {
|
|
7066
|
-
throw new
|
|
7156
|
+
throw new import_utils40.BadRequestError("Invalid ID.");
|
|
7067
7157
|
}
|
|
7068
7158
|
try {
|
|
7069
7159
|
await collection.updateOne(
|
|
@@ -7073,7 +7163,7 @@ function useOrgRepo() {
|
|
|
7073
7163
|
delCachedData();
|
|
7074
7164
|
return "Successfully deleted organization.";
|
|
7075
7165
|
} catch (error) {
|
|
7076
|
-
throw new
|
|
7166
|
+
throw new import_utils40.InternalServerError("Failed to delete organization.");
|
|
7077
7167
|
}
|
|
7078
7168
|
}
|
|
7079
7169
|
return {
|
|
@@ -7088,10 +7178,10 @@ function useOrgRepo() {
|
|
|
7088
7178
|
}
|
|
7089
7179
|
|
|
7090
7180
|
// src/resources/organization/organization.service.ts
|
|
7091
|
-
var
|
|
7181
|
+
var import_utils42 = require("@goweekdays/utils");
|
|
7092
7182
|
|
|
7093
7183
|
// src/resources/user/user.controller.ts
|
|
7094
|
-
var
|
|
7184
|
+
var import_utils41 = require("@goweekdays/utils");
|
|
7095
7185
|
var import_joi21 = __toESM(require("joi"));
|
|
7096
7186
|
function useUserController() {
|
|
7097
7187
|
const {
|
|
@@ -7115,7 +7205,7 @@ function useUserController() {
|
|
|
7115
7205
|
});
|
|
7116
7206
|
const { error } = validation.validate({ status, search, page });
|
|
7117
7207
|
if (error) {
|
|
7118
|
-
next(new
|
|
7208
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7119
7209
|
return;
|
|
7120
7210
|
}
|
|
7121
7211
|
try {
|
|
@@ -7130,12 +7220,12 @@ function useUserController() {
|
|
|
7130
7220
|
const id = req.params.id || "";
|
|
7131
7221
|
const validation = import_joi21.default.string().hex().validate(id);
|
|
7132
7222
|
if (validation.error) {
|
|
7133
|
-
throw new
|
|
7223
|
+
throw new import_utils41.BadRequestError("Invalid id.");
|
|
7134
7224
|
}
|
|
7135
7225
|
try {
|
|
7136
7226
|
const user = await _getUserById(id);
|
|
7137
7227
|
if (!user) {
|
|
7138
|
-
throw new
|
|
7228
|
+
throw new import_utils41.BadRequestError("User not found.");
|
|
7139
7229
|
}
|
|
7140
7230
|
res.json(user);
|
|
7141
7231
|
} catch (error) {
|
|
@@ -7152,7 +7242,7 @@ function useUserController() {
|
|
|
7152
7242
|
});
|
|
7153
7243
|
const { error } = validation.validate({ firstName, lastName });
|
|
7154
7244
|
if (error) {
|
|
7155
|
-
next(new
|
|
7245
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7156
7246
|
return;
|
|
7157
7247
|
}
|
|
7158
7248
|
try {
|
|
@@ -7175,7 +7265,7 @@ function useUserController() {
|
|
|
7175
7265
|
});
|
|
7176
7266
|
const { error } = validation.validate({ month, day, year });
|
|
7177
7267
|
if (error) {
|
|
7178
|
-
next(new
|
|
7268
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7179
7269
|
return;
|
|
7180
7270
|
}
|
|
7181
7271
|
try {
|
|
@@ -7200,7 +7290,7 @@ function useUserController() {
|
|
|
7200
7290
|
});
|
|
7201
7291
|
const { error } = validation.validate({ _id, field, value });
|
|
7202
7292
|
if (error) {
|
|
7203
|
-
next(new
|
|
7293
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7204
7294
|
return;
|
|
7205
7295
|
}
|
|
7206
7296
|
try {
|
|
@@ -7221,7 +7311,7 @@ function useUserController() {
|
|
|
7221
7311
|
});
|
|
7222
7312
|
const { error } = validation.validate({ previousProfile });
|
|
7223
7313
|
if (error) {
|
|
7224
|
-
next(new
|
|
7314
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7225
7315
|
return;
|
|
7226
7316
|
}
|
|
7227
7317
|
const user = req.headers["user"] ?? "";
|
|
@@ -7234,10 +7324,10 @@ function useUserController() {
|
|
|
7234
7324
|
res.json({ message: "Successfully updated profile picture." });
|
|
7235
7325
|
return;
|
|
7236
7326
|
} catch (error2) {
|
|
7237
|
-
if (error2 instanceof
|
|
7327
|
+
if (error2 instanceof import_utils41.AppError) {
|
|
7238
7328
|
next(error2);
|
|
7239
7329
|
} else {
|
|
7240
|
-
next(new
|
|
7330
|
+
next(new import_utils41.InternalServerError(error2));
|
|
7241
7331
|
}
|
|
7242
7332
|
}
|
|
7243
7333
|
}
|
|
@@ -7262,7 +7352,7 @@ function useUserController() {
|
|
|
7262
7352
|
type
|
|
7263
7353
|
});
|
|
7264
7354
|
if (error) {
|
|
7265
|
-
next(new
|
|
7355
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7266
7356
|
return;
|
|
7267
7357
|
}
|
|
7268
7358
|
try {
|
|
@@ -7302,12 +7392,15 @@ function useOrgService() {
|
|
|
7302
7392
|
const { add: addMember } = useMemberRepo();
|
|
7303
7393
|
const { getUserById } = useUserRepo();
|
|
7304
7394
|
async function add(value) {
|
|
7305
|
-
const session =
|
|
7306
|
-
session
|
|
7395
|
+
const session = import_utils42.useAtlas.getClient()?.startSession();
|
|
7396
|
+
if (!session) {
|
|
7397
|
+
throw new import_utils42.BadRequestError("Unable to start database session.");
|
|
7398
|
+
}
|
|
7307
7399
|
try {
|
|
7400
|
+
session?.startTransaction();
|
|
7308
7401
|
const org = await addOrg(value, session);
|
|
7309
7402
|
const allPermissions = await getAllPermission({
|
|
7310
|
-
app:
|
|
7403
|
+
app: "org",
|
|
7311
7404
|
limit: 100
|
|
7312
7405
|
});
|
|
7313
7406
|
let permissions = [];
|
|
@@ -7317,39 +7410,41 @@ function useOrgService() {
|
|
|
7317
7410
|
if (permissions.length === 0) {
|
|
7318
7411
|
throw new Error("No permissions found for the organization type.");
|
|
7319
7412
|
}
|
|
7413
|
+
const createdBy = String(value.createdBy);
|
|
7320
7414
|
const role = await addRole(
|
|
7321
7415
|
{
|
|
7322
|
-
|
|
7416
|
+
org: String(org),
|
|
7323
7417
|
name: "Owner",
|
|
7324
7418
|
description: "Owner of the organization",
|
|
7325
|
-
permissions
|
|
7419
|
+
permissions,
|
|
7420
|
+
createdBy
|
|
7326
7421
|
},
|
|
7327
7422
|
session
|
|
7328
7423
|
);
|
|
7329
7424
|
if (!role) {
|
|
7330
|
-
throw new
|
|
7425
|
+
throw new import_utils42.BadRequestError("Role is required to create org member.");
|
|
7331
7426
|
}
|
|
7332
|
-
const user = await getUserById(
|
|
7427
|
+
const user = await getUserById(createdBy);
|
|
7333
7428
|
if (!user) {
|
|
7334
|
-
throw new
|
|
7429
|
+
throw new import_utils42.BadRequestError("User is required to create org member.");
|
|
7335
7430
|
}
|
|
7336
7431
|
await addMember(
|
|
7337
7432
|
{
|
|
7338
7433
|
role: String(role),
|
|
7339
7434
|
org: String(org),
|
|
7340
7435
|
name: `${user.firstName} ${user.lastName}`,
|
|
7341
|
-
user:
|
|
7342
|
-
type: "
|
|
7436
|
+
user: createdBy,
|
|
7437
|
+
type: "org"
|
|
7343
7438
|
},
|
|
7344
7439
|
session
|
|
7345
7440
|
);
|
|
7346
|
-
session?.commitTransaction();
|
|
7347
|
-
return
|
|
7441
|
+
await session?.commitTransaction();
|
|
7442
|
+
return String(org);
|
|
7348
7443
|
} catch (error) {
|
|
7349
|
-
session?.abortTransaction();
|
|
7444
|
+
await session?.abortTransaction();
|
|
7350
7445
|
throw error;
|
|
7351
7446
|
} finally {
|
|
7352
|
-
session?.endSession();
|
|
7447
|
+
await session?.endSession();
|
|
7353
7448
|
}
|
|
7354
7449
|
}
|
|
7355
7450
|
return {
|
|
@@ -7358,7 +7453,7 @@ function useOrgService() {
|
|
|
7358
7453
|
}
|
|
7359
7454
|
|
|
7360
7455
|
// src/resources/organization/organization.controller.ts
|
|
7361
|
-
var
|
|
7456
|
+
var import_utils43 = require("@goweekdays/utils");
|
|
7362
7457
|
var import_joi22 = __toESM(require("joi"));
|
|
7363
7458
|
function useOrgController() {
|
|
7364
7459
|
const { add: _add } = useOrgService();
|
|
@@ -7368,16 +7463,19 @@ function useOrgController() {
|
|
|
7368
7463
|
getAll: getAllOrg,
|
|
7369
7464
|
getById: _getById
|
|
7370
7465
|
} = useOrgRepo();
|
|
7371
|
-
async function
|
|
7466
|
+
async function add(req, res, next) {
|
|
7372
7467
|
const value = req.body;
|
|
7373
7468
|
const { error } = schemaOrg.validate(value);
|
|
7374
7469
|
if (error) {
|
|
7375
|
-
next(new
|
|
7470
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7376
7471
|
return;
|
|
7377
7472
|
}
|
|
7378
7473
|
try {
|
|
7379
|
-
const
|
|
7380
|
-
res.json({
|
|
7474
|
+
const org = await _add(value);
|
|
7475
|
+
res.json({
|
|
7476
|
+
message: "Organization created successfully.",
|
|
7477
|
+
data: { org }
|
|
7478
|
+
});
|
|
7381
7479
|
return;
|
|
7382
7480
|
} catch (error2) {
|
|
7383
7481
|
next(error2);
|
|
@@ -7390,12 +7488,12 @@ function useOrgController() {
|
|
|
7390
7488
|
const user = req.params.user ?? "";
|
|
7391
7489
|
const isPageNumber = isFinite(page);
|
|
7392
7490
|
if (!isPageNumber) {
|
|
7393
|
-
next(new
|
|
7491
|
+
next(new import_utils43.BadRequestError("Invalid page number."));
|
|
7394
7492
|
return;
|
|
7395
7493
|
}
|
|
7396
7494
|
const isLimitNumber = isFinite(limit);
|
|
7397
7495
|
if (!isLimitNumber) {
|
|
7398
|
-
next(new
|
|
7496
|
+
next(new import_utils43.BadRequestError("Invalid limit number."));
|
|
7399
7497
|
return;
|
|
7400
7498
|
}
|
|
7401
7499
|
const validation = import_joi22.default.object({
|
|
@@ -7406,7 +7504,7 @@ function useOrgController() {
|
|
|
7406
7504
|
});
|
|
7407
7505
|
const { error } = validation.validate({ user, page, limit, search });
|
|
7408
7506
|
if (error) {
|
|
7409
|
-
next(new
|
|
7507
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7410
7508
|
return;
|
|
7411
7509
|
}
|
|
7412
7510
|
try {
|
|
@@ -7432,16 +7530,16 @@ function useOrgController() {
|
|
|
7432
7530
|
const status = req.query.status ?? "active";
|
|
7433
7531
|
const isPageNumber = isFinite(page);
|
|
7434
7532
|
if (!isPageNumber) {
|
|
7435
|
-
next(new
|
|
7533
|
+
next(new import_utils43.BadRequestError("Invalid page number."));
|
|
7436
7534
|
return;
|
|
7437
7535
|
}
|
|
7438
7536
|
const isLimitNumber = isFinite(limit);
|
|
7439
7537
|
if (!isLimitNumber) {
|
|
7440
|
-
next(new
|
|
7538
|
+
next(new import_utils43.BadRequestError("Invalid limit number."));
|
|
7441
7539
|
return;
|
|
7442
7540
|
}
|
|
7443
7541
|
if (error) {
|
|
7444
|
-
next(new
|
|
7542
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7445
7543
|
return;
|
|
7446
7544
|
}
|
|
7447
7545
|
try {
|
|
@@ -7459,7 +7557,7 @@ function useOrgController() {
|
|
|
7459
7557
|
});
|
|
7460
7558
|
const { error } = validation.validate({ name });
|
|
7461
7559
|
if (error) {
|
|
7462
|
-
next(new
|
|
7560
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7463
7561
|
return;
|
|
7464
7562
|
}
|
|
7465
7563
|
try {
|
|
@@ -7477,7 +7575,7 @@ function useOrgController() {
|
|
|
7477
7575
|
});
|
|
7478
7576
|
const { error } = validation.validate({ id });
|
|
7479
7577
|
if (error) {
|
|
7480
|
-
next(new
|
|
7578
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7481
7579
|
return;
|
|
7482
7580
|
}
|
|
7483
7581
|
try {
|
|
@@ -7489,7 +7587,7 @@ function useOrgController() {
|
|
|
7489
7587
|
}
|
|
7490
7588
|
}
|
|
7491
7589
|
return {
|
|
7492
|
-
|
|
7590
|
+
add,
|
|
7493
7591
|
getOrgsByUserId,
|
|
7494
7592
|
getByName,
|
|
7495
7593
|
getAll,
|
|
@@ -7517,16 +7615,16 @@ function modelPSGC(data) {
|
|
|
7517
7615
|
}
|
|
7518
7616
|
|
|
7519
7617
|
// src/resources/psgc/psgc.repository.ts
|
|
7520
|
-
var
|
|
7618
|
+
var import_utils44 = require("@goweekdays/utils");
|
|
7521
7619
|
var import_mongodb22 = require("mongodb");
|
|
7522
7620
|
function usePSGCRepo() {
|
|
7523
|
-
const db =
|
|
7621
|
+
const db = import_utils44.useAtlas.getDb();
|
|
7524
7622
|
if (!db) {
|
|
7525
7623
|
throw new Error("Unable to connect to server.");
|
|
7526
7624
|
}
|
|
7527
7625
|
const namespace_collection = "psgc";
|
|
7528
7626
|
const collection = db.collection(namespace_collection);
|
|
7529
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
7627
|
+
const { getCache, setCache, delNamespace } = (0, import_utils44.useCache)(namespace_collection);
|
|
7530
7628
|
async function createIndexes() {
|
|
7531
7629
|
try {
|
|
7532
7630
|
await collection.createIndexes([
|
|
@@ -7540,12 +7638,12 @@ function usePSGCRepo() {
|
|
|
7540
7638
|
}
|
|
7541
7639
|
function delCachedData() {
|
|
7542
7640
|
delNamespace().then(() => {
|
|
7543
|
-
|
|
7641
|
+
import_utils44.logger.log({
|
|
7544
7642
|
level: "info",
|
|
7545
7643
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
7546
7644
|
});
|
|
7547
7645
|
}).catch((err) => {
|
|
7548
|
-
|
|
7646
|
+
import_utils44.logger.log({
|
|
7549
7647
|
level: "error",
|
|
7550
7648
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
7551
7649
|
});
|
|
@@ -7558,16 +7656,16 @@ function usePSGCRepo() {
|
|
|
7558
7656
|
delCachedData();
|
|
7559
7657
|
return res.insertedId;
|
|
7560
7658
|
} catch (error) {
|
|
7561
|
-
|
|
7659
|
+
import_utils44.logger.log({
|
|
7562
7660
|
level: "error",
|
|
7563
7661
|
message: error.message
|
|
7564
7662
|
});
|
|
7565
|
-
if (error instanceof
|
|
7663
|
+
if (error instanceof import_utils44.AppError) {
|
|
7566
7664
|
throw error;
|
|
7567
7665
|
} else {
|
|
7568
7666
|
const isDuplicated = error.message.includes("duplicate");
|
|
7569
7667
|
if (isDuplicated) {
|
|
7570
|
-
throw new
|
|
7668
|
+
throw new import_utils44.BadRequestError("Region already exists.");
|
|
7571
7669
|
}
|
|
7572
7670
|
throw new Error("Failed to create PSGC.");
|
|
7573
7671
|
}
|
|
@@ -7603,15 +7701,15 @@ function usePSGCRepo() {
|
|
|
7603
7701
|
query.$text = { $search: search };
|
|
7604
7702
|
cacheKeyOptions.search = search;
|
|
7605
7703
|
}
|
|
7606
|
-
const cacheKey = (0,
|
|
7607
|
-
|
|
7704
|
+
const cacheKey = (0, import_utils44.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
7705
|
+
import_utils44.logger.log({
|
|
7608
7706
|
level: "info",
|
|
7609
7707
|
message: `Cache key for getAll PSGC: ${cacheKey}`
|
|
7610
7708
|
});
|
|
7611
7709
|
try {
|
|
7612
7710
|
const cached = await getCache(cacheKey);
|
|
7613
7711
|
if (cached) {
|
|
7614
|
-
|
|
7712
|
+
import_utils44.logger.log({
|
|
7615
7713
|
level: "info",
|
|
7616
7714
|
message: `Cache hit for getAll PSGC: ${cacheKey}`
|
|
7617
7715
|
});
|
|
@@ -7624,21 +7722,21 @@ function usePSGCRepo() {
|
|
|
7624
7722
|
{ $limit: limit }
|
|
7625
7723
|
]).toArray();
|
|
7626
7724
|
const length = await collection.countDocuments(query);
|
|
7627
|
-
const data = (0,
|
|
7725
|
+
const data = (0, import_utils44.paginate)(items, page, limit, length);
|
|
7628
7726
|
setCache(cacheKey, data, 600).then(() => {
|
|
7629
|
-
|
|
7727
|
+
import_utils44.logger.log({
|
|
7630
7728
|
level: "info",
|
|
7631
7729
|
message: `Cache set for getAll PSGC: ${cacheKey}`
|
|
7632
7730
|
});
|
|
7633
7731
|
}).catch((err) => {
|
|
7634
|
-
|
|
7732
|
+
import_utils44.logger.log({
|
|
7635
7733
|
level: "error",
|
|
7636
7734
|
message: `Failed to set cache for getAll PSGC: ${err.message}`
|
|
7637
7735
|
});
|
|
7638
7736
|
});
|
|
7639
7737
|
return data;
|
|
7640
7738
|
} catch (error) {
|
|
7641
|
-
|
|
7739
|
+
import_utils44.logger.log({ level: "error", message: `${error}` });
|
|
7642
7740
|
throw error;
|
|
7643
7741
|
}
|
|
7644
7742
|
}
|
|
@@ -7646,13 +7744,13 @@ function usePSGCRepo() {
|
|
|
7646
7744
|
try {
|
|
7647
7745
|
_id = new import_mongodb22.ObjectId(_id);
|
|
7648
7746
|
} catch (error) {
|
|
7649
|
-
throw new
|
|
7747
|
+
throw new import_utils44.BadRequestError("Invalid ID.");
|
|
7650
7748
|
}
|
|
7651
|
-
const cacheKey = (0,
|
|
7749
|
+
const cacheKey = (0, import_utils44.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
7652
7750
|
try {
|
|
7653
7751
|
const cached = await getCache(cacheKey);
|
|
7654
7752
|
if (cached) {
|
|
7655
|
-
|
|
7753
|
+
import_utils44.logger.log({
|
|
7656
7754
|
level: "info",
|
|
7657
7755
|
message: `Cache hit for getById PSGC: ${cacheKey}`
|
|
7658
7756
|
});
|
|
@@ -7663,25 +7761,25 @@ function usePSGCRepo() {
|
|
|
7663
7761
|
deletedAt: { $in: ["", null] }
|
|
7664
7762
|
});
|
|
7665
7763
|
if (!result) {
|
|
7666
|
-
throw new
|
|
7764
|
+
throw new import_utils44.BadRequestError("Region not found.");
|
|
7667
7765
|
}
|
|
7668
7766
|
setCache(cacheKey, result, 300).then(() => {
|
|
7669
|
-
|
|
7767
|
+
import_utils44.logger.log({
|
|
7670
7768
|
level: "info",
|
|
7671
7769
|
message: `Cache set for PSGC by id: ${cacheKey}`
|
|
7672
7770
|
});
|
|
7673
7771
|
}).catch((err) => {
|
|
7674
|
-
|
|
7772
|
+
import_utils44.logger.log({
|
|
7675
7773
|
level: "error",
|
|
7676
7774
|
message: `Failed to set cache for PSGC by id: ${err.message}`
|
|
7677
7775
|
});
|
|
7678
7776
|
});
|
|
7679
7777
|
return result;
|
|
7680
7778
|
} catch (error) {
|
|
7681
|
-
if (error instanceof
|
|
7779
|
+
if (error instanceof import_utils44.AppError) {
|
|
7682
7780
|
throw error;
|
|
7683
7781
|
} else {
|
|
7684
|
-
throw new
|
|
7782
|
+
throw new import_utils44.InternalServerError("Failed to get PSGC.");
|
|
7685
7783
|
}
|
|
7686
7784
|
}
|
|
7687
7785
|
}
|
|
@@ -7705,15 +7803,15 @@ function usePSGCRepo() {
|
|
|
7705
7803
|
query.code = { $regex: `^${prefix}` };
|
|
7706
7804
|
cacheKeyOptions.prefix = prefix;
|
|
7707
7805
|
}
|
|
7708
|
-
const cacheKey = (0,
|
|
7709
|
-
|
|
7806
|
+
const cacheKey = (0, import_utils44.makeCacheKey)(namespace_collection, { name });
|
|
7807
|
+
import_utils44.logger.log({
|
|
7710
7808
|
level: "info",
|
|
7711
7809
|
message: `Query for getByName PSGC: ${JSON.stringify(query)}`
|
|
7712
7810
|
});
|
|
7713
7811
|
try {
|
|
7714
7812
|
const cached = await getCache(cacheKey);
|
|
7715
7813
|
if (cached) {
|
|
7716
|
-
|
|
7814
|
+
import_utils44.logger.log({
|
|
7717
7815
|
level: "info",
|
|
7718
7816
|
message: `Cache hit for getByName PSGC: ${cacheKey}`
|
|
7719
7817
|
});
|
|
@@ -7721,36 +7819,36 @@ function usePSGCRepo() {
|
|
|
7721
7819
|
}
|
|
7722
7820
|
const result = await collection.findOne(query);
|
|
7723
7821
|
setCache(cacheKey, result, 300).then(() => {
|
|
7724
|
-
|
|
7822
|
+
import_utils44.logger.log({
|
|
7725
7823
|
level: "info",
|
|
7726
7824
|
message: `Cache set for PSGC by name: ${cacheKey}`
|
|
7727
7825
|
});
|
|
7728
7826
|
}).catch((err) => {
|
|
7729
|
-
|
|
7827
|
+
import_utils44.logger.log({
|
|
7730
7828
|
level: "error",
|
|
7731
7829
|
message: `Failed to set cache for PSGC by name: ${err.message}`
|
|
7732
7830
|
});
|
|
7733
7831
|
});
|
|
7734
7832
|
return result;
|
|
7735
7833
|
} catch (error) {
|
|
7736
|
-
if (error instanceof
|
|
7834
|
+
if (error instanceof import_utils44.AppError) {
|
|
7737
7835
|
throw error;
|
|
7738
7836
|
} else {
|
|
7739
|
-
throw new
|
|
7837
|
+
throw new import_utils44.InternalServerError("Failed to get PSGC.");
|
|
7740
7838
|
}
|
|
7741
7839
|
}
|
|
7742
7840
|
}
|
|
7743
7841
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
7744
7842
|
const allowedFields = ["name"];
|
|
7745
7843
|
if (!allowedFields.includes(field)) {
|
|
7746
|
-
throw new
|
|
7844
|
+
throw new import_utils44.BadRequestError(
|
|
7747
7845
|
`Field "${field}" is not allowed to be updated.`
|
|
7748
7846
|
);
|
|
7749
7847
|
}
|
|
7750
7848
|
try {
|
|
7751
7849
|
_id = new import_mongodb22.ObjectId(_id);
|
|
7752
7850
|
} catch (error) {
|
|
7753
|
-
throw new
|
|
7851
|
+
throw new import_utils44.BadRequestError("Invalid ID.");
|
|
7754
7852
|
}
|
|
7755
7853
|
try {
|
|
7756
7854
|
await collection.updateOne(
|
|
@@ -7761,14 +7859,14 @@ function usePSGCRepo() {
|
|
|
7761
7859
|
delCachedData();
|
|
7762
7860
|
return `Successfully updated PSGC ${field}.`;
|
|
7763
7861
|
} catch (error) {
|
|
7764
|
-
throw new
|
|
7862
|
+
throw new import_utils44.InternalServerError(`Failed to update PSGC ${field}.`);
|
|
7765
7863
|
}
|
|
7766
7864
|
}
|
|
7767
7865
|
async function deleteById(_id) {
|
|
7768
7866
|
try {
|
|
7769
7867
|
_id = new import_mongodb22.ObjectId(_id);
|
|
7770
7868
|
} catch (error) {
|
|
7771
|
-
throw new
|
|
7869
|
+
throw new import_utils44.BadRequestError("Invalid ID.");
|
|
7772
7870
|
}
|
|
7773
7871
|
try {
|
|
7774
7872
|
await collection.updateOne(
|
|
@@ -7778,7 +7876,7 @@ function usePSGCRepo() {
|
|
|
7778
7876
|
delCachedData();
|
|
7779
7877
|
return "Successfully deleted PSGC.";
|
|
7780
7878
|
} catch (error) {
|
|
7781
|
-
throw new
|
|
7879
|
+
throw new import_utils44.InternalServerError("Failed to delete PSGC.");
|
|
7782
7880
|
}
|
|
7783
7881
|
}
|
|
7784
7882
|
return {
|
|
@@ -7793,7 +7891,7 @@ function usePSGCRepo() {
|
|
|
7793
7891
|
}
|
|
7794
7892
|
|
|
7795
7893
|
// src/resources/psgc/psgc.controller.ts
|
|
7796
|
-
var
|
|
7894
|
+
var import_utils45 = require("@goweekdays/utils");
|
|
7797
7895
|
var import_joi24 = __toESM(require("joi"));
|
|
7798
7896
|
function usePSGCController() {
|
|
7799
7897
|
const {
|
|
@@ -7808,7 +7906,7 @@ function usePSGCController() {
|
|
|
7808
7906
|
const value = req.body;
|
|
7809
7907
|
const { error } = schemaPSGC.validate(value);
|
|
7810
7908
|
if (error) {
|
|
7811
|
-
next(new
|
|
7909
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7812
7910
|
return;
|
|
7813
7911
|
}
|
|
7814
7912
|
try {
|
|
@@ -7839,16 +7937,16 @@ function usePSGCController() {
|
|
|
7839
7937
|
const prefix = req.query.prefix ? String(req.query.prefix) : "";
|
|
7840
7938
|
const isPageNumber = isFinite(page);
|
|
7841
7939
|
if (!isPageNumber) {
|
|
7842
|
-
next(new
|
|
7940
|
+
next(new import_utils45.BadRequestError("Invalid page number."));
|
|
7843
7941
|
return;
|
|
7844
7942
|
}
|
|
7845
7943
|
const isLimitNumber = isFinite(limit);
|
|
7846
7944
|
if (!isLimitNumber) {
|
|
7847
|
-
next(new
|
|
7945
|
+
next(new import_utils45.BadRequestError("Invalid limit number."));
|
|
7848
7946
|
return;
|
|
7849
7947
|
}
|
|
7850
7948
|
if (error) {
|
|
7851
|
-
next(new
|
|
7949
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7852
7950
|
return;
|
|
7853
7951
|
}
|
|
7854
7952
|
try {
|
|
@@ -7872,7 +7970,7 @@ function usePSGCController() {
|
|
|
7872
7970
|
});
|
|
7873
7971
|
const { error } = validation.validate({ id });
|
|
7874
7972
|
if (error) {
|
|
7875
|
-
next(new
|
|
7973
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7876
7974
|
return;
|
|
7877
7975
|
}
|
|
7878
7976
|
try {
|
|
@@ -7893,7 +7991,7 @@ function usePSGCController() {
|
|
|
7893
7991
|
});
|
|
7894
7992
|
const { error } = validation.validate({ name });
|
|
7895
7993
|
if (error) {
|
|
7896
|
-
next(new
|
|
7994
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7897
7995
|
return;
|
|
7898
7996
|
}
|
|
7899
7997
|
try {
|
|
@@ -7917,7 +8015,7 @@ function usePSGCController() {
|
|
|
7917
8015
|
});
|
|
7918
8016
|
const { error } = validation.validate({ _id, field, value });
|
|
7919
8017
|
if (error) {
|
|
7920
|
-
next(new
|
|
8018
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7921
8019
|
return;
|
|
7922
8020
|
}
|
|
7923
8021
|
try {
|
|
@@ -7935,7 +8033,7 @@ function usePSGCController() {
|
|
|
7935
8033
|
});
|
|
7936
8034
|
const { error } = validation.validate({ _id });
|
|
7937
8035
|
if (error) {
|
|
7938
|
-
next(new
|
|
8036
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7939
8037
|
return;
|
|
7940
8038
|
}
|
|
7941
8039
|
try {
|
|
@@ -7956,24 +8054,49 @@ function usePSGCController() {
|
|
|
7956
8054
|
};
|
|
7957
8055
|
}
|
|
7958
8056
|
|
|
8057
|
+
// src/resources/role/role.service.ts
|
|
8058
|
+
var import_utils46 = require("@goweekdays/utils");
|
|
8059
|
+
function useRoleService() {
|
|
8060
|
+
const { getByRole } = useMemberRepo();
|
|
8061
|
+
const { deleteById: _deleteById } = useRoleRepo();
|
|
8062
|
+
async function deleteById(id) {
|
|
8063
|
+
try {
|
|
8064
|
+
const role = await getByRole(id);
|
|
8065
|
+
if (role) {
|
|
8066
|
+
throw new import_utils46.BadRequestError("Cannot delete role assigned to members.");
|
|
8067
|
+
}
|
|
8068
|
+
await _deleteById(id);
|
|
8069
|
+
} catch (error) {
|
|
8070
|
+
if (error instanceof import_utils46.AppError) {
|
|
8071
|
+
throw error;
|
|
8072
|
+
} else {
|
|
8073
|
+
throw new import_utils46.InternalServerError("Failed to delete role.");
|
|
8074
|
+
}
|
|
8075
|
+
}
|
|
8076
|
+
}
|
|
8077
|
+
return {
|
|
8078
|
+
deleteById
|
|
8079
|
+
};
|
|
8080
|
+
}
|
|
8081
|
+
|
|
7959
8082
|
// src/resources/role/role.controller.ts
|
|
7960
8083
|
var import_joi25 = __toESM(require("joi"));
|
|
7961
|
-
var
|
|
8084
|
+
var import_utils47 = require("@goweekdays/utils");
|
|
7962
8085
|
function useRoleController() {
|
|
7963
8086
|
const {
|
|
7964
8087
|
addRole: _createRole,
|
|
7965
|
-
|
|
8088
|
+
getById,
|
|
7966
8089
|
getRoleByUserId: _getRoleByUserId,
|
|
7967
8090
|
getRoles: _getRoles,
|
|
7968
8091
|
updateRole: _updateRole,
|
|
7969
|
-
deleteRole: _deleteRole,
|
|
7970
8092
|
updatePermissionsById: _updatePermissionsById
|
|
7971
8093
|
} = useRoleRepo();
|
|
8094
|
+
const { deleteById: _deleteById } = useRoleService();
|
|
7972
8095
|
async function createRole(req, res, next) {
|
|
7973
8096
|
const payload = req.body;
|
|
7974
8097
|
const { error } = schemaRole.validate(payload);
|
|
7975
8098
|
if (error) {
|
|
7976
|
-
next(new
|
|
8099
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
7977
8100
|
return;
|
|
7978
8101
|
}
|
|
7979
8102
|
try {
|
|
@@ -7999,7 +8122,7 @@ function useRoleController() {
|
|
|
7999
8122
|
});
|
|
8000
8123
|
const { error } = validation.validate({ search, page, limit, type, id });
|
|
8001
8124
|
if (error) {
|
|
8002
|
-
next(new
|
|
8125
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8003
8126
|
return;
|
|
8004
8127
|
}
|
|
8005
8128
|
try {
|
|
@@ -8017,7 +8140,7 @@ function useRoleController() {
|
|
|
8017
8140
|
});
|
|
8018
8141
|
const { error } = validation.validate({ userId });
|
|
8019
8142
|
if (error) {
|
|
8020
|
-
next(new
|
|
8143
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8021
8144
|
return;
|
|
8022
8145
|
}
|
|
8023
8146
|
try {
|
|
@@ -8035,11 +8158,11 @@ function useRoleController() {
|
|
|
8035
8158
|
});
|
|
8036
8159
|
const { error } = validation.validate({ _id });
|
|
8037
8160
|
if (error) {
|
|
8038
|
-
next(new
|
|
8161
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8039
8162
|
return;
|
|
8040
8163
|
}
|
|
8041
8164
|
try {
|
|
8042
|
-
const data = await
|
|
8165
|
+
const data = await getById(_id);
|
|
8043
8166
|
res.json(data);
|
|
8044
8167
|
return;
|
|
8045
8168
|
} catch (error2) {
|
|
@@ -8057,7 +8180,7 @@ function useRoleController() {
|
|
|
8057
8180
|
});
|
|
8058
8181
|
const { error } = validation.validate({ _id, name, permissions });
|
|
8059
8182
|
if (error) {
|
|
8060
|
-
next(new
|
|
8183
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8061
8184
|
return;
|
|
8062
8185
|
}
|
|
8063
8186
|
try {
|
|
@@ -8077,7 +8200,7 @@ function useRoleController() {
|
|
|
8077
8200
|
});
|
|
8078
8201
|
const { error } = validation.validate({ _id, permissions });
|
|
8079
8202
|
if (error) {
|
|
8080
|
-
next(new
|
|
8203
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8081
8204
|
return;
|
|
8082
8205
|
}
|
|
8083
8206
|
try {
|
|
@@ -8095,11 +8218,11 @@ function useRoleController() {
|
|
|
8095
8218
|
});
|
|
8096
8219
|
const { error } = validation.validate({ _id });
|
|
8097
8220
|
if (error) {
|
|
8098
|
-
next(new
|
|
8221
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8099
8222
|
return;
|
|
8100
8223
|
}
|
|
8101
8224
|
try {
|
|
8102
|
-
const message = await
|
|
8225
|
+
const message = await _deleteById(_id);
|
|
8103
8226
|
res.json({ message });
|
|
8104
8227
|
return;
|
|
8105
8228
|
} catch (error2) {
|
|
@@ -8118,7 +8241,7 @@ function useRoleController() {
|
|
|
8118
8241
|
}
|
|
8119
8242
|
|
|
8120
8243
|
// src/resources/utils/github.service.ts
|
|
8121
|
-
var
|
|
8244
|
+
var import_utils48 = require("@goweekdays/utils");
|
|
8122
8245
|
var import_rest = require("@octokit/rest");
|
|
8123
8246
|
var import_libsodium_wrappers = __toESM(require("libsodium-wrappers"));
|
|
8124
8247
|
function useGitHubService() {
|
|
@@ -8132,23 +8255,23 @@ function useGitHubService() {
|
|
|
8132
8255
|
try {
|
|
8133
8256
|
const { data: repoData } = await octokit.repos.get({ owner, repo });
|
|
8134
8257
|
if (!repoData.permissions?.admin) {
|
|
8135
|
-
throw new
|
|
8258
|
+
throw new import_utils48.BadRequestError(
|
|
8136
8259
|
"You do not have admin access to this repository."
|
|
8137
8260
|
);
|
|
8138
8261
|
}
|
|
8139
8262
|
} catch (error) {
|
|
8140
8263
|
if (error.status === 404) {
|
|
8141
|
-
throw new
|
|
8264
|
+
throw new import_utils48.BadRequestError(
|
|
8142
8265
|
"Repository not found or you don't have access to it."
|
|
8143
8266
|
);
|
|
8144
8267
|
} else if (error.status === 401) {
|
|
8145
|
-
throw new
|
|
8268
|
+
throw new import_utils48.BadRequestError(
|
|
8146
8269
|
"Invalid GitHub token or insufficient permissions."
|
|
8147
8270
|
);
|
|
8148
8271
|
} else if (error.message.includes("admin access")) {
|
|
8149
8272
|
throw error;
|
|
8150
8273
|
} else {
|
|
8151
|
-
throw new
|
|
8274
|
+
throw new import_utils48.BadRequestError(
|
|
8152
8275
|
`Failed to check repository permissions: ${error.message}`
|
|
8153
8276
|
);
|
|
8154
8277
|
}
|
|
@@ -8197,7 +8320,7 @@ function useGitHubService() {
|
|
|
8197
8320
|
key_id: publicKeyRes.key_id
|
|
8198
8321
|
});
|
|
8199
8322
|
} catch (encryptionError) {
|
|
8200
|
-
throw new
|
|
8323
|
+
throw new import_utils48.BadRequestError(
|
|
8201
8324
|
`Failed to encrypt secret '${key}': ${encryptionError.message}`
|
|
8202
8325
|
);
|
|
8203
8326
|
}
|
|
@@ -8227,22 +8350,22 @@ function useGitHubService() {
|
|
|
8227
8350
|
}
|
|
8228
8351
|
return `Successfully set ${lines.length} ${type} variables/secrets in environment '${environment}'`;
|
|
8229
8352
|
} catch (error) {
|
|
8230
|
-
if (error instanceof
|
|
8353
|
+
if (error instanceof import_utils48.AppError)
|
|
8231
8354
|
throw error;
|
|
8232
8355
|
if (error.status === 422) {
|
|
8233
|
-
throw new
|
|
8356
|
+
throw new import_utils48.BadRequestError(
|
|
8234
8357
|
`GitHub API validation error: ${error.message}`
|
|
8235
8358
|
);
|
|
8236
8359
|
} else if (error.status === 404) {
|
|
8237
|
-
throw new
|
|
8360
|
+
throw new import_utils48.BadRequestError("Environment or repository not found.");
|
|
8238
8361
|
} else if (error.status === 403) {
|
|
8239
|
-
throw new
|
|
8362
|
+
throw new import_utils48.BadRequestError(
|
|
8240
8363
|
"Forbidden: Insufficient permissions or rate limit exceeded."
|
|
8241
8364
|
);
|
|
8242
8365
|
} else if (error.message.includes("admin access") || error.message.includes("permissions")) {
|
|
8243
8366
|
throw error;
|
|
8244
8367
|
} else {
|
|
8245
|
-
throw new
|
|
8368
|
+
throw new import_utils48.BadRequestError(
|
|
8246
8369
|
`Failed to set GitHub variables: ${error.message}`
|
|
8247
8370
|
);
|
|
8248
8371
|
}
|
|
@@ -8255,7 +8378,7 @@ function useGitHubService() {
|
|
|
8255
8378
|
|
|
8256
8379
|
// src/resources/utils/util.controller.ts
|
|
8257
8380
|
var import_joi26 = __toESM(require("joi"));
|
|
8258
|
-
var
|
|
8381
|
+
var import_utils49 = require("@goweekdays/utils");
|
|
8259
8382
|
function useUtilController() {
|
|
8260
8383
|
async function healthCheck(req, res, next) {
|
|
8261
8384
|
try {
|
|
@@ -8271,8 +8394,8 @@ function useUtilController() {
|
|
|
8271
8394
|
}
|
|
8272
8395
|
});
|
|
8273
8396
|
} catch (error) {
|
|
8274
|
-
|
|
8275
|
-
next(new
|
|
8397
|
+
import_utils49.logger.error("Health check failed", { error: error.message });
|
|
8398
|
+
next(new import_utils49.InternalServerError("Health check failed"));
|
|
8276
8399
|
}
|
|
8277
8400
|
}
|
|
8278
8401
|
async function setGitHubVariables(req, res, next) {
|
|
@@ -8309,13 +8432,13 @@ function useUtilController() {
|
|
|
8309
8432
|
keyValues
|
|
8310
8433
|
});
|
|
8311
8434
|
if (error) {
|
|
8312
|
-
next(new
|
|
8435
|
+
next(new import_utils49.BadRequestError(error.message));
|
|
8313
8436
|
return;
|
|
8314
8437
|
}
|
|
8315
8438
|
const repoUrlPattern = /github\.com[:\/]([^\/]+)\/(.+)\.git$/;
|
|
8316
8439
|
if (!repoUrlPattern.test(repoUrl)) {
|
|
8317
8440
|
next(
|
|
8318
|
-
new
|
|
8441
|
+
new import_utils49.BadRequestError(
|
|
8319
8442
|
"Invalid GitHub repository URL format. Expected format: https://github.com/owner/repo.git"
|
|
8320
8443
|
)
|
|
8321
8444
|
);
|
|
@@ -8327,7 +8450,7 @@ function useUtilController() {
|
|
|
8327
8450
|
);
|
|
8328
8451
|
if (invalidLines.length > 0) {
|
|
8329
8452
|
next(
|
|
8330
|
-
new
|
|
8453
|
+
new import_utils49.BadRequestError(
|
|
8331
8454
|
"Invalid key-value format. Each pair should be in format: KEY=value. Pairs should be separated by semicolons."
|
|
8332
8455
|
)
|
|
8333
8456
|
);
|
|
@@ -8341,7 +8464,7 @@ function useUtilController() {
|
|
|
8341
8464
|
type,
|
|
8342
8465
|
keyValues
|
|
8343
8466
|
});
|
|
8344
|
-
|
|
8467
|
+
import_utils49.logger.info(`GitHub variables set successfully`, {
|
|
8345
8468
|
repoUrl,
|
|
8346
8469
|
environment,
|
|
8347
8470
|
type,
|
|
@@ -8358,15 +8481,15 @@ function useUtilController() {
|
|
|
8358
8481
|
}
|
|
8359
8482
|
});
|
|
8360
8483
|
} catch (error) {
|
|
8361
|
-
|
|
8484
|
+
import_utils49.logger.error("Failed to set GitHub variables", {
|
|
8362
8485
|
error: error.message,
|
|
8363
8486
|
stack: error.stack
|
|
8364
8487
|
});
|
|
8365
|
-
if (error instanceof
|
|
8488
|
+
if (error instanceof import_utils49.AppError) {
|
|
8366
8489
|
next(error);
|
|
8367
8490
|
} else {
|
|
8368
8491
|
next(
|
|
8369
|
-
new
|
|
8492
|
+
new import_utils49.InternalServerError(
|
|
8370
8493
|
`Failed to set GitHub variables: ${error.message}`
|
|
8371
8494
|
)
|
|
8372
8495
|
);
|
|
@@ -8403,7 +8526,7 @@ var transactionSchema = import_joi27.default.object({
|
|
|
8403
8526
|
});
|
|
8404
8527
|
|
|
8405
8528
|
// src/resources/verification/verification.controller.ts
|
|
8406
|
-
var
|
|
8529
|
+
var import_utils50 = require("@goweekdays/utils");
|
|
8407
8530
|
var import_joi28 = __toESM(require("joi"));
|
|
8408
8531
|
function useVerificationController() {
|
|
8409
8532
|
const {
|
|
@@ -8424,7 +8547,7 @@ function useVerificationController() {
|
|
|
8424
8547
|
});
|
|
8425
8548
|
const { error } = validation.validate(req.body);
|
|
8426
8549
|
if (error) {
|
|
8427
|
-
next(new
|
|
8550
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8428
8551
|
return;
|
|
8429
8552
|
}
|
|
8430
8553
|
const email = req.body.email ?? "";
|
|
@@ -8455,7 +8578,7 @@ function useVerificationController() {
|
|
|
8455
8578
|
const validation = import_joi28.default.string().email().required();
|
|
8456
8579
|
const { error } = validation.validate(email);
|
|
8457
8580
|
if (error) {
|
|
8458
|
-
next(new
|
|
8581
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8459
8582
|
return;
|
|
8460
8583
|
}
|
|
8461
8584
|
try {
|
|
@@ -8465,10 +8588,10 @@ function useVerificationController() {
|
|
|
8465
8588
|
});
|
|
8466
8589
|
return;
|
|
8467
8590
|
} catch (error2) {
|
|
8468
|
-
if (error2 instanceof
|
|
8591
|
+
if (error2 instanceof import_utils50.AppError) {
|
|
8469
8592
|
next(error2);
|
|
8470
8593
|
} else {
|
|
8471
|
-
next(new
|
|
8594
|
+
next(new import_utils50.InternalServerError("An unexpected error occurred"));
|
|
8472
8595
|
}
|
|
8473
8596
|
}
|
|
8474
8597
|
}
|
|
@@ -8483,7 +8606,7 @@ function useVerificationController() {
|
|
|
8483
8606
|
});
|
|
8484
8607
|
const { error } = validation.validate(req.query);
|
|
8485
8608
|
if (error) {
|
|
8486
|
-
next(new
|
|
8609
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8487
8610
|
return;
|
|
8488
8611
|
}
|
|
8489
8612
|
const status = req.query.status ?? "";
|
|
@@ -8517,7 +8640,7 @@ function useVerificationController() {
|
|
|
8517
8640
|
const validation = import_joi28.default.string().hex().required();
|
|
8518
8641
|
const { error } = validation.validate(id);
|
|
8519
8642
|
if (error) {
|
|
8520
|
-
next(new
|
|
8643
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8521
8644
|
return;
|
|
8522
8645
|
}
|
|
8523
8646
|
try {
|
|
@@ -8533,7 +8656,7 @@ function useVerificationController() {
|
|
|
8533
8656
|
const validation = import_joi28.default.string().hex().required();
|
|
8534
8657
|
const { error } = validation.validate(otpId);
|
|
8535
8658
|
if (error) {
|
|
8536
|
-
next(new
|
|
8659
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8537
8660
|
return;
|
|
8538
8661
|
}
|
|
8539
8662
|
try {
|
|
@@ -8576,11 +8699,9 @@ function useVerificationController() {
|
|
|
8576
8699
|
MMember,
|
|
8577
8700
|
MONGO_DB,
|
|
8578
8701
|
MONGO_URI,
|
|
8579
|
-
MOrg,
|
|
8580
8702
|
MUser,
|
|
8581
8703
|
MUserRole,
|
|
8582
8704
|
MVerification,
|
|
8583
|
-
OrgTypes,
|
|
8584
8705
|
PAYPAL_API_URL,
|
|
8585
8706
|
PAYPAL_CLIENT_ID,
|
|
8586
8707
|
PAYPAL_CLIENT_SECRET,
|
|
@@ -8603,6 +8724,8 @@ function useVerificationController() {
|
|
|
8603
8724
|
addressSchema,
|
|
8604
8725
|
isDev,
|
|
8605
8726
|
modelApp,
|
|
8727
|
+
modelMember,
|
|
8728
|
+
modelOrg,
|
|
8606
8729
|
modelPSGC,
|
|
8607
8730
|
modelPermission,
|
|
8608
8731
|
modelPermissionGroup,
|
|
@@ -8611,6 +8734,7 @@ function useVerificationController() {
|
|
|
8611
8734
|
schemaAppUpdate,
|
|
8612
8735
|
schemaBuilding,
|
|
8613
8736
|
schemaBuildingUnit,
|
|
8737
|
+
schemaMember,
|
|
8614
8738
|
schemaOrg,
|
|
8615
8739
|
schemaPSGC,
|
|
8616
8740
|
schemaPermission,
|
|
@@ -8654,6 +8778,7 @@ function useVerificationController() {
|
|
|
8654
8778
|
usePermissionService,
|
|
8655
8779
|
useRoleController,
|
|
8656
8780
|
useRoleRepo,
|
|
8781
|
+
useRoleService,
|
|
8657
8782
|
useUserController,
|
|
8658
8783
|
useUserRepo,
|
|
8659
8784
|
useUserService,
|