@goweekdays/core 2.2.0 → 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 +6 -0
- package/dist/index.d.ts +9 -10
- package/dist/index.js +637 -583
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +367 -313
- 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,
|
|
@@ -953,10 +954,10 @@ function useAuthService() {
|
|
|
953
954
|
|
|
954
955
|
// src/resources/auth/auth.controller.ts
|
|
955
956
|
var import_joi14 = __toESM(require("joi"));
|
|
956
|
-
var
|
|
957
|
+
var import_utils26 = require("@goweekdays/utils");
|
|
957
958
|
|
|
958
959
|
// src/resources/user/user.service.ts
|
|
959
|
-
var
|
|
960
|
+
var import_utils25 = require("@goweekdays/utils");
|
|
960
961
|
|
|
961
962
|
// src/resources/file/file.repository.ts
|
|
962
963
|
var import_utils6 = require("@goweekdays/utils");
|
|
@@ -1459,6 +1460,56 @@ function MMember(value) {
|
|
|
1459
1460
|
deletedAt: ""
|
|
1460
1461
|
};
|
|
1461
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
|
+
}
|
|
1462
1513
|
|
|
1463
1514
|
// src/resources/member/member.repository.ts
|
|
1464
1515
|
var import_mongodb9 = require("mongodb");
|
|
@@ -1527,7 +1578,7 @@ function useMemberRepo() {
|
|
|
1527
1578
|
}
|
|
1528
1579
|
async function add(value, session) {
|
|
1529
1580
|
try {
|
|
1530
|
-
value =
|
|
1581
|
+
value = modelMember(value);
|
|
1531
1582
|
await collection.insertOne(value, { session });
|
|
1532
1583
|
delCachedData();
|
|
1533
1584
|
return "Successfully added member.";
|
|
@@ -2414,42 +2465,43 @@ var import_multer = require("multer");
|
|
|
2414
2465
|
var import_mongodb15 = require("mongodb");
|
|
2415
2466
|
|
|
2416
2467
|
// src/resources/role/role.repository.ts
|
|
2417
|
-
var
|
|
2468
|
+
var import_utils13 = require("@goweekdays/utils");
|
|
2418
2469
|
|
|
2419
2470
|
// src/resources/role/role.model.ts
|
|
2471
|
+
var import_utils12 = require("@goweekdays/utils");
|
|
2420
2472
|
var import_joi4 = __toESM(require("joi"));
|
|
2421
2473
|
var import_mongodb10 = require("mongodb");
|
|
2422
2474
|
var schemaRole = import_joi4.default.object({
|
|
2423
2475
|
name: import_joi4.default.string().required(),
|
|
2424
2476
|
description: import_joi4.default.string().max(1024).optional().allow("", null),
|
|
2425
2477
|
permissions: import_joi4.default.array().items(import_joi4.default.string()).required(),
|
|
2426
|
-
|
|
2427
|
-
|
|
2478
|
+
org: import_joi4.default.string().hex().optional().allow("", null),
|
|
2479
|
+
createdBy: import_joi4.default.string().hex().required()
|
|
2428
2480
|
});
|
|
2429
2481
|
function modelRole(value) {
|
|
2430
2482
|
const { error } = schemaRole.validate(value);
|
|
2431
2483
|
if (error) {
|
|
2432
|
-
throw new
|
|
2484
|
+
throw new import_utils12.BadRequestError(error.message);
|
|
2433
2485
|
}
|
|
2434
2486
|
if (value._id && typeof value._id === "string") {
|
|
2435
2487
|
try {
|
|
2436
2488
|
value._id = new import_mongodb10.ObjectId(value._id);
|
|
2437
2489
|
} catch (error2) {
|
|
2438
|
-
throw new
|
|
2490
|
+
throw new import_utils12.BadRequestError("Invalid _id.");
|
|
2439
2491
|
}
|
|
2440
2492
|
}
|
|
2441
2493
|
if (value.org && typeof value.org === "string" && value.org.length === 24) {
|
|
2442
2494
|
try {
|
|
2443
2495
|
value.org = new import_mongodb10.ObjectId(value.org);
|
|
2444
2496
|
} catch (error2) {
|
|
2445
|
-
throw new
|
|
2497
|
+
throw new import_utils12.BadRequestError("Invalid org.");
|
|
2446
2498
|
}
|
|
2447
2499
|
}
|
|
2448
2500
|
if (value.createdBy && typeof value.createdBy === "string" && value.createdBy.length === 24) {
|
|
2449
2501
|
try {
|
|
2450
2502
|
value.createdBy = new import_mongodb10.ObjectId(value.createdBy);
|
|
2451
2503
|
} catch (error2) {
|
|
2452
|
-
throw new
|
|
2504
|
+
throw new import_utils12.BadRequestError("Invalid createdBy.");
|
|
2453
2505
|
}
|
|
2454
2506
|
}
|
|
2455
2507
|
return {
|
|
@@ -2457,7 +2509,6 @@ function modelRole(value) {
|
|
|
2457
2509
|
name: value.name,
|
|
2458
2510
|
description: value.description ?? "",
|
|
2459
2511
|
permissions: value.permissions,
|
|
2460
|
-
type: value.type,
|
|
2461
2512
|
org: value.org,
|
|
2462
2513
|
status: value.status ?? "active",
|
|
2463
2514
|
default: value.default ?? false,
|
|
@@ -2471,21 +2522,21 @@ function modelRole(value) {
|
|
|
2471
2522
|
// src/resources/role/role.repository.ts
|
|
2472
2523
|
var import_mongodb11 = require("mongodb");
|
|
2473
2524
|
function useRoleRepo() {
|
|
2474
|
-
const db =
|
|
2525
|
+
const db = import_utils13.useAtlas.getDb();
|
|
2475
2526
|
if (!db) {
|
|
2476
|
-
throw new
|
|
2527
|
+
throw new import_utils13.InternalServerError("Unable to connect to server.");
|
|
2477
2528
|
}
|
|
2478
2529
|
const namespace_collection = "roles";
|
|
2479
2530
|
const collection = db.collection(namespace_collection);
|
|
2480
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
2531
|
+
const { getCache, setCache, delNamespace } = (0, import_utils13.useCache)(namespace_collection);
|
|
2481
2532
|
function delCachedData() {
|
|
2482
2533
|
delNamespace().then(() => {
|
|
2483
|
-
|
|
2534
|
+
import_utils13.logger.log({
|
|
2484
2535
|
level: "info",
|
|
2485
2536
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
2486
2537
|
});
|
|
2487
2538
|
}).catch((err) => {
|
|
2488
|
-
|
|
2539
|
+
import_utils13.logger.log({
|
|
2489
2540
|
level: "error",
|
|
2490
2541
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
2491
2542
|
});
|
|
@@ -2498,14 +2549,14 @@ function useRoleRepo() {
|
|
|
2498
2549
|
await collection.createIndex({ status: 1 });
|
|
2499
2550
|
await collection.createIndex({ id: 1 });
|
|
2500
2551
|
} catch (error) {
|
|
2501
|
-
throw new
|
|
2552
|
+
throw new import_utils13.InternalServerError("Failed to create index on role.");
|
|
2502
2553
|
}
|
|
2503
2554
|
}
|
|
2504
2555
|
async function createTextIndex() {
|
|
2505
2556
|
try {
|
|
2506
2557
|
await collection.createIndex({ name: "text" });
|
|
2507
2558
|
} catch (error) {
|
|
2508
|
-
throw new
|
|
2559
|
+
throw new import_utils13.InternalServerError("Failed to create text index on role.");
|
|
2509
2560
|
}
|
|
2510
2561
|
}
|
|
2511
2562
|
async function createUniqueIndex() {
|
|
@@ -2515,7 +2566,7 @@ function useRoleRepo() {
|
|
|
2515
2566
|
{ unique: true }
|
|
2516
2567
|
);
|
|
2517
2568
|
} catch (error) {
|
|
2518
|
-
throw new
|
|
2569
|
+
throw new import_utils13.InternalServerError("Failed to create unique index on role.");
|
|
2519
2570
|
}
|
|
2520
2571
|
}
|
|
2521
2572
|
async function addRole(value, session, clearCache = true) {
|
|
@@ -2527,27 +2578,31 @@ function useRoleRepo() {
|
|
|
2527
2578
|
}
|
|
2528
2579
|
return res.insertedId;
|
|
2529
2580
|
} catch (error) {
|
|
2530
|
-
|
|
2581
|
+
import_utils13.logger.log({ level: "error", message: `${error}` });
|
|
2531
2582
|
const isDuplicated = error.message.includes("duplicate");
|
|
2532
2583
|
if (isDuplicated) {
|
|
2533
|
-
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.");
|
|
2534
2590
|
}
|
|
2535
|
-
throw new import_utils12.InternalServerError("Failed to create role.");
|
|
2536
2591
|
}
|
|
2537
2592
|
}
|
|
2538
2593
|
async function getRoleByUserId(value) {
|
|
2539
2594
|
try {
|
|
2540
2595
|
value = new import_mongodb11.ObjectId(value);
|
|
2541
2596
|
} catch (error) {
|
|
2542
|
-
throw new
|
|
2597
|
+
throw new import_utils13.BadRequestError("Invalid user ID.");
|
|
2543
2598
|
}
|
|
2544
2599
|
try {
|
|
2545
|
-
const cacheKey = (0,
|
|
2600
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, {
|
|
2546
2601
|
user: String(value)
|
|
2547
2602
|
});
|
|
2548
2603
|
const cached = await getCache(cacheKey);
|
|
2549
2604
|
if (cached) {
|
|
2550
|
-
|
|
2605
|
+
import_utils13.logger.log({
|
|
2551
2606
|
level: "info",
|
|
2552
2607
|
message: `Cache hit for getRoleByUserId role: ${cacheKey}`
|
|
2553
2608
|
});
|
|
@@ -2555,34 +2610,34 @@ function useRoleRepo() {
|
|
|
2555
2610
|
}
|
|
2556
2611
|
const data = await collection.findOne({ user: value });
|
|
2557
2612
|
setCache(cacheKey, data, 300).then(() => {
|
|
2558
|
-
|
|
2613
|
+
import_utils13.logger.log({
|
|
2559
2614
|
level: "info",
|
|
2560
2615
|
message: `Cache set for role by user ID: ${cacheKey}`
|
|
2561
2616
|
});
|
|
2562
2617
|
}).catch((err) => {
|
|
2563
|
-
|
|
2618
|
+
import_utils13.logger.log({
|
|
2564
2619
|
level: "error",
|
|
2565
2620
|
message: `Failed to set cache for role by user ID: ${err.message}`
|
|
2566
2621
|
});
|
|
2567
2622
|
});
|
|
2568
2623
|
return data;
|
|
2569
2624
|
} catch (error) {
|
|
2570
|
-
throw new
|
|
2625
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by user ID.");
|
|
2571
2626
|
}
|
|
2572
2627
|
}
|
|
2573
2628
|
async function getById(_id) {
|
|
2574
2629
|
try {
|
|
2575
2630
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2576
2631
|
} catch (error) {
|
|
2577
|
-
throw new
|
|
2632
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
2578
2633
|
}
|
|
2579
2634
|
try {
|
|
2580
|
-
const cacheKey = (0,
|
|
2635
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, {
|
|
2581
2636
|
id: _id.toString()
|
|
2582
2637
|
});
|
|
2583
2638
|
const cached = await getCache(cacheKey);
|
|
2584
2639
|
if (cached) {
|
|
2585
|
-
|
|
2640
|
+
import_utils13.logger.log({
|
|
2586
2641
|
level: "info",
|
|
2587
2642
|
message: `Cache hit for getById role: ${cacheKey}`
|
|
2588
2643
|
});
|
|
@@ -2590,32 +2645,32 @@ function useRoleRepo() {
|
|
|
2590
2645
|
}
|
|
2591
2646
|
const data = await collection.findOne({ _id });
|
|
2592
2647
|
setCache(cacheKey, data, 300).then(() => {
|
|
2593
|
-
|
|
2648
|
+
import_utils13.logger.log({
|
|
2594
2649
|
level: "info",
|
|
2595
2650
|
message: `Cache set for role by id: ${cacheKey}`
|
|
2596
2651
|
});
|
|
2597
2652
|
}).catch((err) => {
|
|
2598
|
-
|
|
2653
|
+
import_utils13.logger.log({
|
|
2599
2654
|
level: "error",
|
|
2600
2655
|
message: `Failed to set cache for role by id: ${err.message}`
|
|
2601
2656
|
});
|
|
2602
2657
|
});
|
|
2603
2658
|
return data;
|
|
2604
2659
|
} catch (error) {
|
|
2605
|
-
throw new
|
|
2660
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by ID.");
|
|
2606
2661
|
}
|
|
2607
2662
|
}
|
|
2608
2663
|
async function getRoleByName(name) {
|
|
2609
2664
|
if (!name) {
|
|
2610
|
-
throw new
|
|
2665
|
+
throw new import_utils13.BadRequestError("Role name is required.");
|
|
2611
2666
|
}
|
|
2612
2667
|
try {
|
|
2613
|
-
const cacheKey = (0,
|
|
2668
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, {
|
|
2614
2669
|
name
|
|
2615
2670
|
});
|
|
2616
2671
|
const cached = await getCache(cacheKey);
|
|
2617
2672
|
if (cached) {
|
|
2618
|
-
|
|
2673
|
+
import_utils13.logger.log({
|
|
2619
2674
|
level: "info",
|
|
2620
2675
|
message: `Cache hit for getRoleByName role: ${cacheKey}`
|
|
2621
2676
|
});
|
|
@@ -2623,19 +2678,19 @@ function useRoleRepo() {
|
|
|
2623
2678
|
}
|
|
2624
2679
|
const data = await collection.findOne({ name });
|
|
2625
2680
|
setCache(cacheKey, data, 300).then(() => {
|
|
2626
|
-
|
|
2681
|
+
import_utils13.logger.log({
|
|
2627
2682
|
level: "info",
|
|
2628
2683
|
message: `Cache set for role by name: ${cacheKey}`
|
|
2629
2684
|
});
|
|
2630
2685
|
}).catch((err) => {
|
|
2631
|
-
|
|
2686
|
+
import_utils13.logger.log({
|
|
2632
2687
|
level: "error",
|
|
2633
2688
|
message: `Failed to set cache for role by name: ${err.message}`
|
|
2634
2689
|
});
|
|
2635
2690
|
});
|
|
2636
2691
|
return data;
|
|
2637
2692
|
} catch (error) {
|
|
2638
|
-
throw new
|
|
2693
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by name.");
|
|
2639
2694
|
}
|
|
2640
2695
|
}
|
|
2641
2696
|
async function getRoles({
|
|
@@ -2653,7 +2708,7 @@ function useRoleRepo() {
|
|
|
2653
2708
|
try {
|
|
2654
2709
|
id = new import_mongodb11.ObjectId(id);
|
|
2655
2710
|
} catch (error) {
|
|
2656
|
-
throw new
|
|
2711
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
2657
2712
|
}
|
|
2658
2713
|
}
|
|
2659
2714
|
const query = { status: "active" };
|
|
@@ -2678,10 +2733,10 @@ function useRoleRepo() {
|
|
|
2678
2733
|
}
|
|
2679
2734
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
2680
2735
|
cacheKeyOptions.sort = JSON.stringify(sort);
|
|
2681
|
-
const cacheKey = (0,
|
|
2736
|
+
const cacheKey = (0, import_utils13.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
2682
2737
|
const cached = await getCache(cacheKey);
|
|
2683
2738
|
if (cached) {
|
|
2684
|
-
|
|
2739
|
+
import_utils13.logger.log({
|
|
2685
2740
|
level: "info",
|
|
2686
2741
|
message: `Cache hit for getRoles: ${cacheKey}`
|
|
2687
2742
|
});
|
|
@@ -2695,32 +2750,32 @@ function useRoleRepo() {
|
|
|
2695
2750
|
{ $limit: limit }
|
|
2696
2751
|
]).toArray();
|
|
2697
2752
|
const length = await collection.countDocuments(query);
|
|
2698
|
-
const data = (0,
|
|
2753
|
+
const data = (0, import_utils13.paginate)(items, page, limit, length);
|
|
2699
2754
|
setCache(cacheKey, data, 600).then(() => {
|
|
2700
|
-
|
|
2755
|
+
import_utils13.logger.log({
|
|
2701
2756
|
level: "info",
|
|
2702
2757
|
message: `Cache set for getRoles: ${cacheKey}`
|
|
2703
2758
|
});
|
|
2704
2759
|
}).catch((err) => {
|
|
2705
|
-
|
|
2760
|
+
import_utils13.logger.log({
|
|
2706
2761
|
level: "error",
|
|
2707
2762
|
message: `Failed to set cache for getRoles: ${err.message}`
|
|
2708
2763
|
});
|
|
2709
2764
|
});
|
|
2710
2765
|
return data;
|
|
2711
2766
|
} catch (error) {
|
|
2712
|
-
|
|
2767
|
+
import_utils13.logger.log({ level: "error", message: `${error}` });
|
|
2713
2768
|
throw error;
|
|
2714
2769
|
}
|
|
2715
2770
|
}
|
|
2716
2771
|
async function updateRole(_id, value, session) {
|
|
2717
2772
|
if (!_id) {
|
|
2718
|
-
throw new
|
|
2773
|
+
throw new import_utils13.BadRequestError("Role ID is required.");
|
|
2719
2774
|
}
|
|
2720
2775
|
try {
|
|
2721
2776
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2722
2777
|
} catch (error) {
|
|
2723
|
-
throw new
|
|
2778
|
+
throw new import_utils13.BadRequestError("Invalid role ID.");
|
|
2724
2779
|
}
|
|
2725
2780
|
if (!value.name) {
|
|
2726
2781
|
delete value.name;
|
|
@@ -2734,26 +2789,26 @@ function useRoleRepo() {
|
|
|
2734
2789
|
delCachedData();
|
|
2735
2790
|
return "Successfully updated role.";
|
|
2736
2791
|
} catch (error) {
|
|
2737
|
-
throw new
|
|
2792
|
+
throw new import_utils13.InternalServerError("Failed to update role.");
|
|
2738
2793
|
}
|
|
2739
2794
|
} else {
|
|
2740
|
-
throw new
|
|
2795
|
+
throw new import_utils13.BadRequestError("No fields to update.");
|
|
2741
2796
|
}
|
|
2742
2797
|
}
|
|
2743
2798
|
async function updatePermissionsById(_id, permissions, session) {
|
|
2744
2799
|
if (!_id) {
|
|
2745
|
-
throw new
|
|
2800
|
+
throw new import_utils13.BadRequestError("Role ID is required.");
|
|
2746
2801
|
}
|
|
2747
2802
|
try {
|
|
2748
2803
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2749
2804
|
} catch (error) {
|
|
2750
|
-
throw new
|
|
2805
|
+
throw new import_utils13.BadRequestError("Invalid role ID.");
|
|
2751
2806
|
}
|
|
2752
2807
|
if (!permissions) {
|
|
2753
|
-
throw new
|
|
2808
|
+
throw new import_utils13.BadRequestError("Permissions are required.");
|
|
2754
2809
|
}
|
|
2755
2810
|
if (permissions.length === 0) {
|
|
2756
|
-
throw new
|
|
2811
|
+
throw new import_utils13.BadRequestError("Permissions cannot be empty.");
|
|
2757
2812
|
}
|
|
2758
2813
|
try {
|
|
2759
2814
|
await collection.updateOne(
|
|
@@ -2764,14 +2819,14 @@ function useRoleRepo() {
|
|
|
2764
2819
|
delCachedData();
|
|
2765
2820
|
return "Successfully updated role permissions.";
|
|
2766
2821
|
} catch (error) {
|
|
2767
|
-
throw new
|
|
2822
|
+
throw new import_utils13.InternalServerError("Failed to update role permissions.");
|
|
2768
2823
|
}
|
|
2769
2824
|
}
|
|
2770
2825
|
async function deleteById(_id, session) {
|
|
2771
2826
|
try {
|
|
2772
2827
|
_id = new import_mongodb11.ObjectId(_id);
|
|
2773
2828
|
} catch (error) {
|
|
2774
|
-
throw new
|
|
2829
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
2775
2830
|
}
|
|
2776
2831
|
try {
|
|
2777
2832
|
await collection.deleteOne(
|
|
@@ -2783,7 +2838,7 @@ function useRoleRepo() {
|
|
|
2783
2838
|
delCachedData();
|
|
2784
2839
|
return "Successfully deleted role.";
|
|
2785
2840
|
} catch (error) {
|
|
2786
|
-
throw new
|
|
2841
|
+
throw new import_utils13.InternalServerError("Failed to delete role.");
|
|
2787
2842
|
}
|
|
2788
2843
|
}
|
|
2789
2844
|
return {
|
|
@@ -2803,7 +2858,7 @@ function useRoleRepo() {
|
|
|
2803
2858
|
}
|
|
2804
2859
|
|
|
2805
2860
|
// src/resources/permission/permission.model.ts
|
|
2806
|
-
var
|
|
2861
|
+
var import_utils14 = require("@goweekdays/utils");
|
|
2807
2862
|
var import_joi5 = __toESM(require("joi"));
|
|
2808
2863
|
var schemaPermission = import_joi5.default.object({
|
|
2809
2864
|
app: import_joi5.default.string().required(),
|
|
@@ -2822,7 +2877,7 @@ var schemaPermissionUpdate = import_joi5.default.object({
|
|
|
2822
2877
|
function modelPermission(value) {
|
|
2823
2878
|
const { error } = schemaPermission.validate(value);
|
|
2824
2879
|
if (error) {
|
|
2825
|
-
throw new
|
|
2880
|
+
throw new import_utils14.BadRequestError(error.message);
|
|
2826
2881
|
}
|
|
2827
2882
|
return {
|
|
2828
2883
|
_id: value._id,
|
|
@@ -2840,17 +2895,17 @@ function modelPermission(value) {
|
|
|
2840
2895
|
}
|
|
2841
2896
|
|
|
2842
2897
|
// src/resources/permission/permission.repository.ts
|
|
2843
|
-
var
|
|
2898
|
+
var import_utils15 = require("@goweekdays/utils");
|
|
2844
2899
|
var import_mongodb12 = require("mongodb");
|
|
2845
2900
|
var import_joi6 = __toESM(require("joi"));
|
|
2846
2901
|
function usePermissionRepo() {
|
|
2847
|
-
const db =
|
|
2902
|
+
const db = import_utils15.useAtlas.getDb();
|
|
2848
2903
|
if (!db) {
|
|
2849
2904
|
throw new Error("Unable to connect to server.");
|
|
2850
2905
|
}
|
|
2851
2906
|
const namespace_collection = "permissions";
|
|
2852
2907
|
const collection = db.collection(namespace_collection);
|
|
2853
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
2908
|
+
const { getCache, setCache, delNamespace } = (0, import_utils15.useCache)(namespace_collection);
|
|
2854
2909
|
async function createIndexes() {
|
|
2855
2910
|
try {
|
|
2856
2911
|
await collection.createIndexes([
|
|
@@ -2879,7 +2934,7 @@ function usePermissionRepo() {
|
|
|
2879
2934
|
}
|
|
2880
2935
|
}
|
|
2881
2936
|
createIndexes().catch((error) => {
|
|
2882
|
-
|
|
2937
|
+
import_utils15.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
2883
2938
|
});
|
|
2884
2939
|
async function add(value, session) {
|
|
2885
2940
|
try {
|
|
@@ -2888,16 +2943,16 @@ function usePermissionRepo() {
|
|
|
2888
2943
|
delCachedData();
|
|
2889
2944
|
return res.insertedId;
|
|
2890
2945
|
} catch (error) {
|
|
2891
|
-
|
|
2946
|
+
import_utils15.logger.log({
|
|
2892
2947
|
level: "error",
|
|
2893
2948
|
message: error.message
|
|
2894
2949
|
});
|
|
2895
|
-
if (error instanceof
|
|
2950
|
+
if (error instanceof import_utils15.AppError) {
|
|
2896
2951
|
throw error;
|
|
2897
2952
|
} else {
|
|
2898
2953
|
const isDuplicated = error.message.includes("duplicate");
|
|
2899
2954
|
if (isDuplicated) {
|
|
2900
|
-
throw new
|
|
2955
|
+
throw new import_utils15.BadRequestError("Permission already exists.");
|
|
2901
2956
|
}
|
|
2902
2957
|
throw new Error("Failed to create permission.");
|
|
2903
2958
|
}
|
|
@@ -2907,11 +2962,11 @@ function usePermissionRepo() {
|
|
|
2907
2962
|
try {
|
|
2908
2963
|
_id = new import_mongodb12.ObjectId(_id);
|
|
2909
2964
|
} catch (error2) {
|
|
2910
|
-
throw new
|
|
2965
|
+
throw new import_utils15.BadRequestError("Invalid ID.");
|
|
2911
2966
|
}
|
|
2912
2967
|
const { error } = schemaPermissionUpdate.validate(value);
|
|
2913
2968
|
if (error) {
|
|
2914
|
-
throw new
|
|
2969
|
+
throw new import_utils15.BadRequestError(`Invalid data: ${error.message}`);
|
|
2915
2970
|
}
|
|
2916
2971
|
try {
|
|
2917
2972
|
const res = await collection.updateOne(
|
|
@@ -2922,11 +2977,11 @@ function usePermissionRepo() {
|
|
|
2922
2977
|
delCachedData();
|
|
2923
2978
|
return res;
|
|
2924
2979
|
} catch (error2) {
|
|
2925
|
-
|
|
2980
|
+
import_utils15.logger.log({
|
|
2926
2981
|
level: "error",
|
|
2927
2982
|
message: error2.message
|
|
2928
2983
|
});
|
|
2929
|
-
if (error2 instanceof
|
|
2984
|
+
if (error2 instanceof import_utils15.AppError) {
|
|
2930
2985
|
throw error2;
|
|
2931
2986
|
} else {
|
|
2932
2987
|
throw new Error("Failed to update permission.");
|
|
@@ -2959,15 +3014,15 @@ function usePermissionRepo() {
|
|
|
2959
3014
|
query.app = app;
|
|
2960
3015
|
cacheParams.app = app;
|
|
2961
3016
|
}
|
|
2962
|
-
const cacheKey = (0,
|
|
2963
|
-
|
|
3017
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, cacheParams);
|
|
3018
|
+
import_utils15.logger.log({
|
|
2964
3019
|
level: "info",
|
|
2965
3020
|
message: `Cache key for getAll permissions: ${cacheKey}`
|
|
2966
3021
|
});
|
|
2967
3022
|
try {
|
|
2968
3023
|
const cached = await getCache(cacheKey);
|
|
2969
3024
|
if (cached) {
|
|
2970
|
-
|
|
3025
|
+
import_utils15.logger.log({
|
|
2971
3026
|
level: "info",
|
|
2972
3027
|
message: `Cache hit for getAll permissions: ${cacheKey}`
|
|
2973
3028
|
});
|
|
@@ -2980,21 +3035,21 @@ function usePermissionRepo() {
|
|
|
2980
3035
|
{ $limit: limit }
|
|
2981
3036
|
]).toArray();
|
|
2982
3037
|
const length = await collection.countDocuments(query);
|
|
2983
|
-
const data = (0,
|
|
3038
|
+
const data = (0, import_utils15.paginate)(items, page, limit, length);
|
|
2984
3039
|
setCache(cacheKey, data, 600).then(() => {
|
|
2985
|
-
|
|
3040
|
+
import_utils15.logger.log({
|
|
2986
3041
|
level: "info",
|
|
2987
3042
|
message: `Cache set for getAll permissions: ${cacheKey}`
|
|
2988
3043
|
});
|
|
2989
3044
|
}).catch((err) => {
|
|
2990
|
-
|
|
3045
|
+
import_utils15.logger.log({
|
|
2991
3046
|
level: "error",
|
|
2992
3047
|
message: `Failed to set cache for getAll permissions: ${err.message}`
|
|
2993
3048
|
});
|
|
2994
3049
|
});
|
|
2995
3050
|
return data;
|
|
2996
3051
|
} catch (error) {
|
|
2997
|
-
|
|
3052
|
+
import_utils15.logger.log({ level: "error", message: `${error}` });
|
|
2998
3053
|
throw error;
|
|
2999
3054
|
}
|
|
3000
3055
|
}
|
|
@@ -3002,13 +3057,13 @@ function usePermissionRepo() {
|
|
|
3002
3057
|
try {
|
|
3003
3058
|
_id = new import_mongodb12.ObjectId(_id);
|
|
3004
3059
|
} catch (error) {
|
|
3005
|
-
throw new
|
|
3060
|
+
throw new import_utils15.BadRequestError("Invalid ID.");
|
|
3006
3061
|
}
|
|
3007
|
-
const cacheKey = (0,
|
|
3062
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
3008
3063
|
try {
|
|
3009
3064
|
const cached = await getCache(cacheKey);
|
|
3010
3065
|
if (cached) {
|
|
3011
|
-
|
|
3066
|
+
import_utils15.logger.log({
|
|
3012
3067
|
level: "info",
|
|
3013
3068
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
3014
3069
|
});
|
|
@@ -3018,22 +3073,22 @@ function usePermissionRepo() {
|
|
|
3018
3073
|
_id
|
|
3019
3074
|
});
|
|
3020
3075
|
setCache(cacheKey, result, 300).then(() => {
|
|
3021
|
-
|
|
3076
|
+
import_utils15.logger.log({
|
|
3022
3077
|
level: "info",
|
|
3023
3078
|
message: `Cache set for permission by id: ${cacheKey}`
|
|
3024
3079
|
});
|
|
3025
3080
|
}).catch((err) => {
|
|
3026
|
-
|
|
3081
|
+
import_utils15.logger.log({
|
|
3027
3082
|
level: "error",
|
|
3028
3083
|
message: `Failed to set cache for permission by id: ${err.message}`
|
|
3029
3084
|
});
|
|
3030
3085
|
});
|
|
3031
3086
|
return result;
|
|
3032
3087
|
} catch (error) {
|
|
3033
|
-
if (error instanceof
|
|
3088
|
+
if (error instanceof import_utils15.AppError) {
|
|
3034
3089
|
throw error;
|
|
3035
3090
|
} else {
|
|
3036
|
-
throw new
|
|
3091
|
+
throw new import_utils15.InternalServerError("Failed to get permission.");
|
|
3037
3092
|
}
|
|
3038
3093
|
}
|
|
3039
3094
|
}
|
|
@@ -3045,7 +3100,7 @@ function usePermissionRepo() {
|
|
|
3045
3100
|
});
|
|
3046
3101
|
const { error } = validation.validate({ key, group });
|
|
3047
3102
|
if (error) {
|
|
3048
|
-
throw new
|
|
3103
|
+
throw new import_utils15.BadRequestError(`Invalid data: ${error.message}`);
|
|
3049
3104
|
}
|
|
3050
3105
|
const query = {};
|
|
3051
3106
|
const cacheKeyOptions = {};
|
|
@@ -3059,11 +3114,11 @@ function usePermissionRepo() {
|
|
|
3059
3114
|
query.app = app;
|
|
3060
3115
|
cacheKeyOptions.app = app;
|
|
3061
3116
|
}
|
|
3062
|
-
const cacheKey = (0,
|
|
3117
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
3063
3118
|
try {
|
|
3064
3119
|
const cached = await getCache(cacheKey);
|
|
3065
3120
|
if (cached) {
|
|
3066
|
-
|
|
3121
|
+
import_utils15.logger.log({
|
|
3067
3122
|
level: "info",
|
|
3068
3123
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
3069
3124
|
});
|
|
@@ -3071,34 +3126,34 @@ function usePermissionRepo() {
|
|
|
3071
3126
|
}
|
|
3072
3127
|
const result = await collection.findOne(query);
|
|
3073
3128
|
setCache(cacheKey, result, 300).then(() => {
|
|
3074
|
-
|
|
3129
|
+
import_utils15.logger.log({
|
|
3075
3130
|
level: "info",
|
|
3076
3131
|
message: `Cache set for permission by key: ${cacheKey}`
|
|
3077
3132
|
});
|
|
3078
3133
|
}).catch((err) => {
|
|
3079
|
-
|
|
3134
|
+
import_utils15.logger.log({
|
|
3080
3135
|
level: "error",
|
|
3081
3136
|
message: `Failed to set cache for permission by key: ${err.message}`
|
|
3082
3137
|
});
|
|
3083
3138
|
});
|
|
3084
3139
|
return result;
|
|
3085
3140
|
} catch (error2) {
|
|
3086
|
-
if (error2 instanceof
|
|
3141
|
+
if (error2 instanceof import_utils15.AppError) {
|
|
3087
3142
|
throw error2;
|
|
3088
3143
|
} else {
|
|
3089
|
-
throw new
|
|
3144
|
+
throw new import_utils15.InternalServerError("Failed to get permission.");
|
|
3090
3145
|
}
|
|
3091
3146
|
}
|
|
3092
3147
|
}
|
|
3093
3148
|
async function countByGroup(group) {
|
|
3094
|
-
const cacheKey = (0,
|
|
3149
|
+
const cacheKey = (0, import_utils15.makeCacheKey)(namespace_collection, {
|
|
3095
3150
|
group,
|
|
3096
3151
|
tag: "countByGroup"
|
|
3097
3152
|
});
|
|
3098
3153
|
try {
|
|
3099
3154
|
const cached = await getCache(cacheKey);
|
|
3100
3155
|
if (cached) {
|
|
3101
|
-
|
|
3156
|
+
import_utils15.logger.log({
|
|
3102
3157
|
level: "info",
|
|
3103
3158
|
message: `Cache hit for getById permission: ${cacheKey}`
|
|
3104
3159
|
});
|
|
@@ -3108,22 +3163,22 @@ function usePermissionRepo() {
|
|
|
3108
3163
|
group
|
|
3109
3164
|
});
|
|
3110
3165
|
setCache(cacheKey, result, 300).then(() => {
|
|
3111
|
-
|
|
3166
|
+
import_utils15.logger.log({
|
|
3112
3167
|
level: "info",
|
|
3113
3168
|
message: `Cache set for permission count by group: ${cacheKey}`
|
|
3114
3169
|
});
|
|
3115
3170
|
}).catch((err) => {
|
|
3116
|
-
|
|
3171
|
+
import_utils15.logger.log({
|
|
3117
3172
|
level: "error",
|
|
3118
3173
|
message: `Failed to set cache for permission by group: ${err.message}`
|
|
3119
3174
|
});
|
|
3120
3175
|
});
|
|
3121
3176
|
return result;
|
|
3122
3177
|
} catch (error) {
|
|
3123
|
-
if (error instanceof
|
|
3178
|
+
if (error instanceof import_utils15.AppError) {
|
|
3124
3179
|
throw error;
|
|
3125
3180
|
} else {
|
|
3126
|
-
throw new
|
|
3181
|
+
throw new import_utils15.InternalServerError("Failed to count permission by group.");
|
|
3127
3182
|
}
|
|
3128
3183
|
}
|
|
3129
3184
|
}
|
|
@@ -3131,7 +3186,7 @@ function usePermissionRepo() {
|
|
|
3131
3186
|
try {
|
|
3132
3187
|
_id = new import_mongodb12.ObjectId(_id);
|
|
3133
3188
|
} catch (error) {
|
|
3134
|
-
throw new
|
|
3189
|
+
throw new import_utils15.BadRequestError("Invalid ID.");
|
|
3135
3190
|
}
|
|
3136
3191
|
try {
|
|
3137
3192
|
const res = await collection.updateOne(
|
|
@@ -3141,25 +3196,25 @@ function usePermissionRepo() {
|
|
|
3141
3196
|
delCachedData();
|
|
3142
3197
|
return res;
|
|
3143
3198
|
} catch (error) {
|
|
3144
|
-
|
|
3199
|
+
import_utils15.logger.log({
|
|
3145
3200
|
level: "error",
|
|
3146
3201
|
message: error.message
|
|
3147
3202
|
});
|
|
3148
|
-
if (error instanceof
|
|
3203
|
+
if (error instanceof import_utils15.AppError) {
|
|
3149
3204
|
throw error;
|
|
3150
3205
|
} else {
|
|
3151
|
-
throw new
|
|
3206
|
+
throw new import_utils15.InternalServerError("Failed to delete permission.");
|
|
3152
3207
|
}
|
|
3153
3208
|
}
|
|
3154
3209
|
}
|
|
3155
3210
|
function delCachedData() {
|
|
3156
3211
|
delNamespace().then(() => {
|
|
3157
|
-
|
|
3212
|
+
import_utils15.logger.log({
|
|
3158
3213
|
level: "info",
|
|
3159
3214
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
3160
3215
|
});
|
|
3161
3216
|
}).catch((err) => {
|
|
3162
|
-
|
|
3217
|
+
import_utils15.logger.log({
|
|
3163
3218
|
level: "error",
|
|
3164
3219
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
3165
3220
|
});
|
|
@@ -3198,7 +3253,7 @@ function usePermissionService() {
|
|
|
3198
3253
|
}
|
|
3199
3254
|
|
|
3200
3255
|
// src/resources/permission/permission.controller.ts
|
|
3201
|
-
var
|
|
3256
|
+
var import_utils16 = require("@goweekdays/utils");
|
|
3202
3257
|
var import_joi7 = __toESM(require("joi"));
|
|
3203
3258
|
function usePermissionController() {
|
|
3204
3259
|
const {
|
|
@@ -3212,8 +3267,8 @@ function usePermissionController() {
|
|
|
3212
3267
|
const value = req.body;
|
|
3213
3268
|
const { error } = schemaPermission.validate(value);
|
|
3214
3269
|
if (error) {
|
|
3215
|
-
next(new
|
|
3216
|
-
|
|
3270
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3271
|
+
import_utils16.logger.info(`Controller: ${error.message}`);
|
|
3217
3272
|
return;
|
|
3218
3273
|
}
|
|
3219
3274
|
try {
|
|
@@ -3235,7 +3290,7 @@ function usePermissionController() {
|
|
|
3235
3290
|
});
|
|
3236
3291
|
const { error } = validation.validate(query);
|
|
3237
3292
|
if (error) {
|
|
3238
|
-
next(new
|
|
3293
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3239
3294
|
return;
|
|
3240
3295
|
}
|
|
3241
3296
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -3274,7 +3329,7 @@ function usePermissionController() {
|
|
|
3274
3329
|
});
|
|
3275
3330
|
const { error } = validation.validate({ id });
|
|
3276
3331
|
if (error) {
|
|
3277
|
-
next(new
|
|
3332
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3278
3333
|
return;
|
|
3279
3334
|
}
|
|
3280
3335
|
try {
|
|
@@ -3295,7 +3350,7 @@ function usePermissionController() {
|
|
|
3295
3350
|
});
|
|
3296
3351
|
const { error } = validation.validate({ id });
|
|
3297
3352
|
if (error) {
|
|
3298
|
-
next(new
|
|
3353
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3299
3354
|
return;
|
|
3300
3355
|
}
|
|
3301
3356
|
try {
|
|
@@ -3310,13 +3365,13 @@ function usePermissionController() {
|
|
|
3310
3365
|
const id = req.params.id;
|
|
3311
3366
|
const { error: errorId } = import_joi7.default.string().hex().required().validate(id);
|
|
3312
3367
|
if (errorId) {
|
|
3313
|
-
next(new
|
|
3368
|
+
next(new import_utils16.BadRequestError(errorId.message));
|
|
3314
3369
|
return;
|
|
3315
3370
|
}
|
|
3316
3371
|
const payload = req.body;
|
|
3317
3372
|
const { error } = schemaPermissionUpdate.validate(payload);
|
|
3318
3373
|
if (error) {
|
|
3319
|
-
next(new
|
|
3374
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
3320
3375
|
return;
|
|
3321
3376
|
}
|
|
3322
3377
|
try {
|
|
@@ -3337,7 +3392,7 @@ function usePermissionController() {
|
|
|
3337
3392
|
}
|
|
3338
3393
|
|
|
3339
3394
|
// src/resources/permission/permission.group.model.ts
|
|
3340
|
-
var
|
|
3395
|
+
var import_utils17 = require("@goweekdays/utils");
|
|
3341
3396
|
var import_joi8 = __toESM(require("joi"));
|
|
3342
3397
|
var schemaPermissionGroup = import_joi8.default.object({
|
|
3343
3398
|
app: import_joi8.default.string().required(),
|
|
@@ -3353,7 +3408,7 @@ var schemaPermissionGroupUpdate = import_joi8.default.object({
|
|
|
3353
3408
|
function modelPermissionGroup(value) {
|
|
3354
3409
|
const { error } = schemaPermissionGroup.validate(value);
|
|
3355
3410
|
if (error) {
|
|
3356
|
-
throw new
|
|
3411
|
+
throw new import_utils17.BadRequestError(error.message);
|
|
3357
3412
|
}
|
|
3358
3413
|
return {
|
|
3359
3414
|
_id: value._id,
|
|
@@ -3369,18 +3424,18 @@ function modelPermissionGroup(value) {
|
|
|
3369
3424
|
}
|
|
3370
3425
|
|
|
3371
3426
|
// src/resources/permission/permission.group.repository.ts
|
|
3372
|
-
var
|
|
3427
|
+
var import_utils18 = require("@goweekdays/utils");
|
|
3373
3428
|
var import_mongodb13 = require("mongodb");
|
|
3374
3429
|
var import_joi9 = __toESM(require("joi"));
|
|
3375
3430
|
var init = false;
|
|
3376
3431
|
function usePermissionGroupRepo() {
|
|
3377
|
-
const db =
|
|
3432
|
+
const db = import_utils18.useAtlas.getDb();
|
|
3378
3433
|
if (!db) {
|
|
3379
3434
|
throw new Error("Unable to connect to server.");
|
|
3380
3435
|
}
|
|
3381
3436
|
const namespace_collection = "permission.groups";
|
|
3382
3437
|
const collection = db.collection(namespace_collection);
|
|
3383
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
3438
|
+
const { getCache, setCache, delNamespace } = (0, import_utils18.useCache)(namespace_collection);
|
|
3384
3439
|
async function createIndexes() {
|
|
3385
3440
|
try {
|
|
3386
3441
|
await collection.createIndexes([
|
|
@@ -3403,7 +3458,7 @@ function usePermissionGroupRepo() {
|
|
|
3403
3458
|
}
|
|
3404
3459
|
if (!init) {
|
|
3405
3460
|
createIndexes().catch((error) => {
|
|
3406
|
-
|
|
3461
|
+
import_utils18.logger.log({
|
|
3407
3462
|
level: "error",
|
|
3408
3463
|
message: `Index creation error: ${error}`
|
|
3409
3464
|
});
|
|
@@ -3417,16 +3472,16 @@ function usePermissionGroupRepo() {
|
|
|
3417
3472
|
delCachedData();
|
|
3418
3473
|
return res.insertedId;
|
|
3419
3474
|
} catch (error) {
|
|
3420
|
-
|
|
3475
|
+
import_utils18.logger.log({
|
|
3421
3476
|
level: "error",
|
|
3422
3477
|
message: error.message
|
|
3423
3478
|
});
|
|
3424
|
-
if (error instanceof
|
|
3479
|
+
if (error instanceof import_utils18.AppError) {
|
|
3425
3480
|
throw error;
|
|
3426
3481
|
} else {
|
|
3427
3482
|
const isDuplicated = error.message.includes("duplicate");
|
|
3428
3483
|
if (isDuplicated) {
|
|
3429
|
-
throw new
|
|
3484
|
+
throw new import_utils18.BadRequestError("Permission group already exists.");
|
|
3430
3485
|
}
|
|
3431
3486
|
throw new Error("Failed to create permission group.");
|
|
3432
3487
|
}
|
|
@@ -3436,11 +3491,11 @@ function usePermissionGroupRepo() {
|
|
|
3436
3491
|
try {
|
|
3437
3492
|
_id = new import_mongodb13.ObjectId(_id);
|
|
3438
3493
|
} catch (error2) {
|
|
3439
|
-
throw new
|
|
3494
|
+
throw new import_utils18.BadRequestError("Invalid ID.");
|
|
3440
3495
|
}
|
|
3441
3496
|
const { error } = schemaPermissionGroupUpdate.validate(value);
|
|
3442
3497
|
if (error) {
|
|
3443
|
-
throw new
|
|
3498
|
+
throw new import_utils18.BadRequestError(`Invalid data: ${error.message}`);
|
|
3444
3499
|
}
|
|
3445
3500
|
try {
|
|
3446
3501
|
const res = await collection.updateOne(
|
|
@@ -3451,11 +3506,11 @@ function usePermissionGroupRepo() {
|
|
|
3451
3506
|
delCachedData();
|
|
3452
3507
|
return res;
|
|
3453
3508
|
} catch (error2) {
|
|
3454
|
-
|
|
3509
|
+
import_utils18.logger.log({
|
|
3455
3510
|
level: "error",
|
|
3456
3511
|
message: error2.message
|
|
3457
3512
|
});
|
|
3458
|
-
if (error2 instanceof
|
|
3513
|
+
if (error2 instanceof import_utils18.AppError) {
|
|
3459
3514
|
throw error2;
|
|
3460
3515
|
} else {
|
|
3461
3516
|
throw new Error("Failed to update permission group.");
|
|
@@ -3488,15 +3543,15 @@ function usePermissionGroupRepo() {
|
|
|
3488
3543
|
query.app = app;
|
|
3489
3544
|
cacheParams.app = app;
|
|
3490
3545
|
}
|
|
3491
|
-
const cacheKey = (0,
|
|
3492
|
-
|
|
3546
|
+
const cacheKey = (0, import_utils18.makeCacheKey)(namespace_collection, cacheParams);
|
|
3547
|
+
import_utils18.logger.log({
|
|
3493
3548
|
level: "info",
|
|
3494
3549
|
message: `Cache key for getAll permission groups: ${cacheKey}`
|
|
3495
3550
|
});
|
|
3496
3551
|
try {
|
|
3497
3552
|
const cached = await getCache(cacheKey);
|
|
3498
3553
|
if (cached) {
|
|
3499
|
-
|
|
3554
|
+
import_utils18.logger.log({
|
|
3500
3555
|
level: "info",
|
|
3501
3556
|
message: `Cache hit for getAll permission groups: ${cacheKey}`
|
|
3502
3557
|
});
|
|
@@ -3509,21 +3564,21 @@ function usePermissionGroupRepo() {
|
|
|
3509
3564
|
{ $limit: limit }
|
|
3510
3565
|
]).toArray();
|
|
3511
3566
|
const length = await collection.countDocuments(query);
|
|
3512
|
-
const data = (0,
|
|
3567
|
+
const data = (0, import_utils18.paginate)(items, page, limit, length);
|
|
3513
3568
|
setCache(cacheKey, data, 600).then(() => {
|
|
3514
|
-
|
|
3569
|
+
import_utils18.logger.log({
|
|
3515
3570
|
level: "info",
|
|
3516
3571
|
message: `Cache set for getAll permission groups: ${cacheKey}`
|
|
3517
3572
|
});
|
|
3518
3573
|
}).catch((err) => {
|
|
3519
|
-
|
|
3574
|
+
import_utils18.logger.log({
|
|
3520
3575
|
level: "error",
|
|
3521
3576
|
message: `Failed to set cache for getAll permission groups: ${err.message}`
|
|
3522
3577
|
});
|
|
3523
3578
|
});
|
|
3524
3579
|
return data;
|
|
3525
3580
|
} catch (error) {
|
|
3526
|
-
|
|
3581
|
+
import_utils18.logger.log({ level: "error", message: `${error}` });
|
|
3527
3582
|
throw error;
|
|
3528
3583
|
}
|
|
3529
3584
|
}
|
|
@@ -3531,13 +3586,13 @@ function usePermissionGroupRepo() {
|
|
|
3531
3586
|
try {
|
|
3532
3587
|
_id = new import_mongodb13.ObjectId(_id);
|
|
3533
3588
|
} catch (error) {
|
|
3534
|
-
throw new
|
|
3589
|
+
throw new import_utils18.BadRequestError("Invalid ID.");
|
|
3535
3590
|
}
|
|
3536
|
-
const cacheKey = (0,
|
|
3591
|
+
const cacheKey = (0, import_utils18.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
3537
3592
|
try {
|
|
3538
3593
|
const cached = await getCache(cacheKey);
|
|
3539
3594
|
if (cached) {
|
|
3540
|
-
|
|
3595
|
+
import_utils18.logger.log({
|
|
3541
3596
|
level: "info",
|
|
3542
3597
|
message: `Cache hit for getById permission group: ${cacheKey}`
|
|
3543
3598
|
});
|
|
@@ -3547,22 +3602,22 @@ function usePermissionGroupRepo() {
|
|
|
3547
3602
|
_id
|
|
3548
3603
|
});
|
|
3549
3604
|
setCache(cacheKey, result, 300).then(() => {
|
|
3550
|
-
|
|
3605
|
+
import_utils18.logger.log({
|
|
3551
3606
|
level: "info",
|
|
3552
3607
|
message: `Cache set for permission group by id: ${cacheKey}`
|
|
3553
3608
|
});
|
|
3554
3609
|
}).catch((err) => {
|
|
3555
|
-
|
|
3610
|
+
import_utils18.logger.log({
|
|
3556
3611
|
level: "error",
|
|
3557
3612
|
message: `Failed to set cache for permission group by id: ${err.message}`
|
|
3558
3613
|
});
|
|
3559
3614
|
});
|
|
3560
3615
|
return result;
|
|
3561
3616
|
} catch (error) {
|
|
3562
|
-
if (error instanceof
|
|
3617
|
+
if (error instanceof import_utils18.AppError) {
|
|
3563
3618
|
throw error;
|
|
3564
3619
|
} else {
|
|
3565
|
-
throw new
|
|
3620
|
+
throw new import_utils18.InternalServerError("Failed to get permission group.");
|
|
3566
3621
|
}
|
|
3567
3622
|
}
|
|
3568
3623
|
}
|
|
@@ -3573,7 +3628,7 @@ function usePermissionGroupRepo() {
|
|
|
3573
3628
|
});
|
|
3574
3629
|
const { error } = validation.validate({ key, app });
|
|
3575
3630
|
if (error) {
|
|
3576
|
-
throw new
|
|
3631
|
+
throw new import_utils18.BadRequestError("Invalid key.");
|
|
3577
3632
|
}
|
|
3578
3633
|
const query = { key };
|
|
3579
3634
|
const cacheKeyOptions = { key, tag: "byKey" };
|
|
@@ -3581,11 +3636,11 @@ function usePermissionGroupRepo() {
|
|
|
3581
3636
|
query.app = app;
|
|
3582
3637
|
cacheKeyOptions.app = app;
|
|
3583
3638
|
}
|
|
3584
|
-
const cacheKey = (0,
|
|
3639
|
+
const cacheKey = (0, import_utils18.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
3585
3640
|
try {
|
|
3586
3641
|
const cached = await getCache(cacheKey);
|
|
3587
3642
|
if (cached) {
|
|
3588
|
-
|
|
3643
|
+
import_utils18.logger.log({
|
|
3589
3644
|
level: "info",
|
|
3590
3645
|
message: `Cache hit for getById permission group: ${cacheKey}`
|
|
3591
3646
|
});
|
|
@@ -3593,22 +3648,22 @@ function usePermissionGroupRepo() {
|
|
|
3593
3648
|
}
|
|
3594
3649
|
const result = await collection.findOne(query);
|
|
3595
3650
|
setCache(cacheKey, result, 300).then(() => {
|
|
3596
|
-
|
|
3651
|
+
import_utils18.logger.log({
|
|
3597
3652
|
level: "info",
|
|
3598
3653
|
message: `Cache set for permission group by key: ${cacheKey}`
|
|
3599
3654
|
});
|
|
3600
3655
|
}).catch((err) => {
|
|
3601
|
-
|
|
3656
|
+
import_utils18.logger.log({
|
|
3602
3657
|
level: "error",
|
|
3603
3658
|
message: `Failed to set cache for permission group by key: ${err.message}`
|
|
3604
3659
|
});
|
|
3605
3660
|
});
|
|
3606
3661
|
return result;
|
|
3607
3662
|
} catch (error2) {
|
|
3608
|
-
if (error2 instanceof
|
|
3663
|
+
if (error2 instanceof import_utils18.AppError) {
|
|
3609
3664
|
throw error2;
|
|
3610
3665
|
} else {
|
|
3611
|
-
throw new
|
|
3666
|
+
throw new import_utils18.InternalServerError("Failed to get permission group.");
|
|
3612
3667
|
}
|
|
3613
3668
|
}
|
|
3614
3669
|
}
|
|
@@ -3616,7 +3671,7 @@ function usePermissionGroupRepo() {
|
|
|
3616
3671
|
try {
|
|
3617
3672
|
_id = new import_mongodb13.ObjectId(_id);
|
|
3618
3673
|
} catch (error) {
|
|
3619
|
-
throw new
|
|
3674
|
+
throw new import_utils18.BadRequestError("Invalid ID.");
|
|
3620
3675
|
}
|
|
3621
3676
|
try {
|
|
3622
3677
|
const res = await collection.updateOne(
|
|
@@ -3626,25 +3681,25 @@ function usePermissionGroupRepo() {
|
|
|
3626
3681
|
delCachedData();
|
|
3627
3682
|
return res;
|
|
3628
3683
|
} catch (error) {
|
|
3629
|
-
|
|
3684
|
+
import_utils18.logger.log({
|
|
3630
3685
|
level: "error",
|
|
3631
3686
|
message: error.message
|
|
3632
3687
|
});
|
|
3633
|
-
if (error instanceof
|
|
3688
|
+
if (error instanceof import_utils18.AppError) {
|
|
3634
3689
|
throw error;
|
|
3635
3690
|
} else {
|
|
3636
|
-
throw new
|
|
3691
|
+
throw new import_utils18.InternalServerError("Failed to delete permission group.");
|
|
3637
3692
|
}
|
|
3638
3693
|
}
|
|
3639
3694
|
}
|
|
3640
3695
|
function delCachedData() {
|
|
3641
3696
|
delNamespace().then(() => {
|
|
3642
|
-
|
|
3697
|
+
import_utils18.logger.log({
|
|
3643
3698
|
level: "info",
|
|
3644
3699
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
3645
3700
|
});
|
|
3646
3701
|
}).catch((err) => {
|
|
3647
|
-
|
|
3702
|
+
import_utils18.logger.log({
|
|
3648
3703
|
level: "error",
|
|
3649
3704
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
3650
3705
|
});
|
|
@@ -3662,10 +3717,10 @@ function usePermissionGroupRepo() {
|
|
|
3662
3717
|
}
|
|
3663
3718
|
|
|
3664
3719
|
// src/resources/permission/permission.group.service.ts
|
|
3665
|
-
var
|
|
3720
|
+
var import_utils23 = require("@goweekdays/utils");
|
|
3666
3721
|
|
|
3667
3722
|
// src/resources/app/app.model.ts
|
|
3668
|
-
var
|
|
3723
|
+
var import_utils19 = require("@goweekdays/utils");
|
|
3669
3724
|
var import_joi10 = __toESM(require("joi"));
|
|
3670
3725
|
var schemaApp = import_joi10.default.object({
|
|
3671
3726
|
code: import_joi10.default.string().alphanum().max(20).required(),
|
|
@@ -3681,7 +3736,7 @@ var schemaAppUpdate = import_joi10.default.object({
|
|
|
3681
3736
|
function modelApp(value) {
|
|
3682
3737
|
const { error } = schemaApp.validate(value);
|
|
3683
3738
|
if (error) {
|
|
3684
|
-
throw new
|
|
3739
|
+
throw new import_utils19.BadRequestError(error.message);
|
|
3685
3740
|
}
|
|
3686
3741
|
return {
|
|
3687
3742
|
_id: value._id,
|
|
@@ -3697,18 +3752,18 @@ function modelApp(value) {
|
|
|
3697
3752
|
}
|
|
3698
3753
|
|
|
3699
3754
|
// src/resources/app/app.repository.ts
|
|
3700
|
-
var
|
|
3755
|
+
var import_utils20 = require("@goweekdays/utils");
|
|
3701
3756
|
var import_mongodb14 = require("mongodb");
|
|
3702
3757
|
var import_joi11 = __toESM(require("joi"));
|
|
3703
3758
|
var init2 = false;
|
|
3704
3759
|
function useAppRepo() {
|
|
3705
|
-
const db =
|
|
3760
|
+
const db = import_utils20.useAtlas.getDb();
|
|
3706
3761
|
if (!db) {
|
|
3707
3762
|
throw new Error("Unable to connect to server.");
|
|
3708
3763
|
}
|
|
3709
3764
|
const namespace_collection = "apps";
|
|
3710
3765
|
const collection = db.collection(namespace_collection);
|
|
3711
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
3766
|
+
const { getCache, setCache, delNamespace } = (0, import_utils20.useCache)(namespace_collection);
|
|
3712
3767
|
async function createIndexes() {
|
|
3713
3768
|
try {
|
|
3714
3769
|
await collection.createIndexes([
|
|
@@ -3737,7 +3792,7 @@ function useAppRepo() {
|
|
|
3737
3792
|
}
|
|
3738
3793
|
if (!init2) {
|
|
3739
3794
|
createIndexes().catch((error) => {
|
|
3740
|
-
|
|
3795
|
+
import_utils20.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
3741
3796
|
});
|
|
3742
3797
|
init2 = true;
|
|
3743
3798
|
}
|
|
@@ -3748,16 +3803,16 @@ function useAppRepo() {
|
|
|
3748
3803
|
delCachedData();
|
|
3749
3804
|
return res.insertedId;
|
|
3750
3805
|
} catch (error) {
|
|
3751
|
-
|
|
3806
|
+
import_utils20.logger.log({
|
|
3752
3807
|
level: "error",
|
|
3753
3808
|
message: error.message
|
|
3754
3809
|
});
|
|
3755
|
-
if (error instanceof
|
|
3810
|
+
if (error instanceof import_utils20.AppError) {
|
|
3756
3811
|
throw error;
|
|
3757
3812
|
} else {
|
|
3758
3813
|
const isDuplicated = error.message.includes("duplicate");
|
|
3759
3814
|
if (isDuplicated) {
|
|
3760
|
-
throw new
|
|
3815
|
+
throw new import_utils20.BadRequestError("App already exists.");
|
|
3761
3816
|
}
|
|
3762
3817
|
throw new Error("Failed to create app.");
|
|
3763
3818
|
}
|
|
@@ -3767,7 +3822,7 @@ function useAppRepo() {
|
|
|
3767
3822
|
try {
|
|
3768
3823
|
_id = new import_mongodb14.ObjectId(_id);
|
|
3769
3824
|
} catch (error) {
|
|
3770
|
-
throw new
|
|
3825
|
+
throw new import_utils20.BadRequestError("Invalid ID.");
|
|
3771
3826
|
}
|
|
3772
3827
|
try {
|
|
3773
3828
|
const res = await collection.updateOne(
|
|
@@ -3778,11 +3833,11 @@ function useAppRepo() {
|
|
|
3778
3833
|
delCachedData();
|
|
3779
3834
|
return res;
|
|
3780
3835
|
} catch (error) {
|
|
3781
|
-
|
|
3836
|
+
import_utils20.logger.log({
|
|
3782
3837
|
level: "error",
|
|
3783
3838
|
message: error.message
|
|
3784
3839
|
});
|
|
3785
|
-
if (error instanceof
|
|
3840
|
+
if (error instanceof import_utils20.AppError) {
|
|
3786
3841
|
throw error;
|
|
3787
3842
|
} else {
|
|
3788
3843
|
throw new Error("Failed to update app.");
|
|
@@ -3820,15 +3875,15 @@ function useAppRepo() {
|
|
|
3820
3875
|
}
|
|
3821
3876
|
cacheParams.type = type;
|
|
3822
3877
|
}
|
|
3823
|
-
const cacheKey = (0,
|
|
3824
|
-
|
|
3878
|
+
const cacheKey = (0, import_utils20.makeCacheKey)(namespace_collection, cacheParams);
|
|
3879
|
+
import_utils20.logger.log({
|
|
3825
3880
|
level: "info",
|
|
3826
3881
|
message: `Cache key for getAll apps: ${cacheKey}`
|
|
3827
3882
|
});
|
|
3828
3883
|
try {
|
|
3829
3884
|
const cached = await getCache(cacheKey);
|
|
3830
3885
|
if (cached) {
|
|
3831
|
-
|
|
3886
|
+
import_utils20.logger.log({
|
|
3832
3887
|
level: "info",
|
|
3833
3888
|
message: `Cache hit for getAll apps: ${cacheKey}`
|
|
3834
3889
|
});
|
|
@@ -3841,21 +3896,21 @@ function useAppRepo() {
|
|
|
3841
3896
|
{ $limit: limit }
|
|
3842
3897
|
]).toArray();
|
|
3843
3898
|
const length = await collection.countDocuments(query);
|
|
3844
|
-
const data = (0,
|
|
3899
|
+
const data = (0, import_utils20.paginate)(items, page, limit, length);
|
|
3845
3900
|
setCache(cacheKey, data, 600).then(() => {
|
|
3846
|
-
|
|
3901
|
+
import_utils20.logger.log({
|
|
3847
3902
|
level: "info",
|
|
3848
3903
|
message: `Cache set for getAll apps: ${cacheKey}`
|
|
3849
3904
|
});
|
|
3850
3905
|
}).catch((err) => {
|
|
3851
|
-
|
|
3906
|
+
import_utils20.logger.log({
|
|
3852
3907
|
level: "error",
|
|
3853
3908
|
message: `Failed to set cache for getAll apps: ${err.message}`
|
|
3854
3909
|
});
|
|
3855
3910
|
});
|
|
3856
3911
|
return data;
|
|
3857
3912
|
} catch (error) {
|
|
3858
|
-
|
|
3913
|
+
import_utils20.logger.log({ level: "error", message: `${error}` });
|
|
3859
3914
|
throw error;
|
|
3860
3915
|
}
|
|
3861
3916
|
}
|
|
@@ -3863,13 +3918,13 @@ function useAppRepo() {
|
|
|
3863
3918
|
try {
|
|
3864
3919
|
_id = new import_mongodb14.ObjectId(_id);
|
|
3865
3920
|
} catch (error) {
|
|
3866
|
-
throw new
|
|
3921
|
+
throw new import_utils20.BadRequestError("Invalid ID.");
|
|
3867
3922
|
}
|
|
3868
|
-
const cacheKey = (0,
|
|
3923
|
+
const cacheKey = (0, import_utils20.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
3869
3924
|
try {
|
|
3870
3925
|
const cached = await getCache(cacheKey);
|
|
3871
3926
|
if (cached) {
|
|
3872
|
-
|
|
3927
|
+
import_utils20.logger.log({
|
|
3873
3928
|
level: "info",
|
|
3874
3929
|
message: `Cache hit for getById app: ${cacheKey}`
|
|
3875
3930
|
});
|
|
@@ -3879,22 +3934,22 @@ function useAppRepo() {
|
|
|
3879
3934
|
_id
|
|
3880
3935
|
});
|
|
3881
3936
|
setCache(cacheKey, result, 300).then(() => {
|
|
3882
|
-
|
|
3937
|
+
import_utils20.logger.log({
|
|
3883
3938
|
level: "info",
|
|
3884
3939
|
message: `Cache set for app by id: ${cacheKey}`
|
|
3885
3940
|
});
|
|
3886
3941
|
}).catch((err) => {
|
|
3887
|
-
|
|
3942
|
+
import_utils20.logger.log({
|
|
3888
3943
|
level: "error",
|
|
3889
3944
|
message: `Failed to set cache for app by id: ${err.message}`
|
|
3890
3945
|
});
|
|
3891
3946
|
});
|
|
3892
3947
|
return result;
|
|
3893
3948
|
} catch (error) {
|
|
3894
|
-
if (error instanceof
|
|
3949
|
+
if (error instanceof import_utils20.AppError) {
|
|
3895
3950
|
throw error;
|
|
3896
3951
|
} else {
|
|
3897
|
-
throw new
|
|
3952
|
+
throw new import_utils20.InternalServerError("Failed to get app.");
|
|
3898
3953
|
}
|
|
3899
3954
|
}
|
|
3900
3955
|
}
|
|
@@ -3902,16 +3957,16 @@ function useAppRepo() {
|
|
|
3902
3957
|
const validate = import_joi11.default.string().required();
|
|
3903
3958
|
const { error } = validate.validate(code);
|
|
3904
3959
|
if (error) {
|
|
3905
|
-
throw new
|
|
3960
|
+
throw new import_utils20.BadRequestError("Invalid code.");
|
|
3906
3961
|
}
|
|
3907
|
-
const cacheKey = (0,
|
|
3962
|
+
const cacheKey = (0, import_utils20.makeCacheKey)(namespace_collection, {
|
|
3908
3963
|
code,
|
|
3909
3964
|
tag: "byCode"
|
|
3910
3965
|
});
|
|
3911
3966
|
try {
|
|
3912
3967
|
const cached = await getCache(cacheKey);
|
|
3913
3968
|
if (cached) {
|
|
3914
|
-
|
|
3969
|
+
import_utils20.logger.log({
|
|
3915
3970
|
level: "info",
|
|
3916
3971
|
message: `Cache hit for getByCode app: ${cacheKey}`
|
|
3917
3972
|
});
|
|
@@ -3921,22 +3976,22 @@ function useAppRepo() {
|
|
|
3921
3976
|
code
|
|
3922
3977
|
});
|
|
3923
3978
|
setCache(cacheKey, result, 300).then(() => {
|
|
3924
|
-
|
|
3979
|
+
import_utils20.logger.log({
|
|
3925
3980
|
level: "info",
|
|
3926
3981
|
message: `Cache set for app by code: ${cacheKey}`
|
|
3927
3982
|
});
|
|
3928
3983
|
}).catch((err) => {
|
|
3929
|
-
|
|
3984
|
+
import_utils20.logger.log({
|
|
3930
3985
|
level: "error",
|
|
3931
3986
|
message: `Failed to set cache for app by code: ${err.message}`
|
|
3932
3987
|
});
|
|
3933
3988
|
});
|
|
3934
3989
|
return result;
|
|
3935
3990
|
} catch (error2) {
|
|
3936
|
-
if (error2 instanceof
|
|
3991
|
+
if (error2 instanceof import_utils20.AppError) {
|
|
3937
3992
|
throw error2;
|
|
3938
3993
|
} else {
|
|
3939
|
-
throw new
|
|
3994
|
+
throw new import_utils20.InternalServerError("Failed to get app.");
|
|
3940
3995
|
}
|
|
3941
3996
|
}
|
|
3942
3997
|
}
|
|
@@ -3944,7 +3999,7 @@ function useAppRepo() {
|
|
|
3944
3999
|
try {
|
|
3945
4000
|
_id = new import_mongodb14.ObjectId(_id);
|
|
3946
4001
|
} catch (error) {
|
|
3947
|
-
throw new
|
|
4002
|
+
throw new import_utils20.BadRequestError("Invalid ID.");
|
|
3948
4003
|
}
|
|
3949
4004
|
try {
|
|
3950
4005
|
const res = await collection.updateOne(
|
|
@@ -3954,25 +4009,25 @@ function useAppRepo() {
|
|
|
3954
4009
|
delCachedData();
|
|
3955
4010
|
return res;
|
|
3956
4011
|
} catch (error) {
|
|
3957
|
-
|
|
4012
|
+
import_utils20.logger.log({
|
|
3958
4013
|
level: "error",
|
|
3959
4014
|
message: error.message
|
|
3960
4015
|
});
|
|
3961
|
-
if (error instanceof
|
|
4016
|
+
if (error instanceof import_utils20.AppError) {
|
|
3962
4017
|
throw error;
|
|
3963
4018
|
} else {
|
|
3964
|
-
throw new
|
|
4019
|
+
throw new import_utils20.InternalServerError("Failed to delete app.");
|
|
3965
4020
|
}
|
|
3966
4021
|
}
|
|
3967
4022
|
}
|
|
3968
4023
|
function delCachedData() {
|
|
3969
4024
|
delNamespace().then(() => {
|
|
3970
|
-
|
|
4025
|
+
import_utils20.logger.log({
|
|
3971
4026
|
level: "info",
|
|
3972
4027
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
3973
4028
|
});
|
|
3974
4029
|
}).catch((err) => {
|
|
3975
|
-
|
|
4030
|
+
import_utils20.logger.log({
|
|
3976
4031
|
level: "error",
|
|
3977
4032
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
3978
4033
|
});
|
|
@@ -3990,7 +4045,7 @@ function useAppRepo() {
|
|
|
3990
4045
|
}
|
|
3991
4046
|
|
|
3992
4047
|
// src/resources/app/app.service.ts
|
|
3993
|
-
var
|
|
4048
|
+
var import_utils21 = require("@goweekdays/utils");
|
|
3994
4049
|
function useAppService() {
|
|
3995
4050
|
const {
|
|
3996
4051
|
updateById: _updateById,
|
|
@@ -4040,7 +4095,7 @@ function useAppService() {
|
|
|
4040
4095
|
description: "Transportation and ride services."
|
|
4041
4096
|
}
|
|
4042
4097
|
];
|
|
4043
|
-
const session =
|
|
4098
|
+
const session = import_utils21.useAtlas.getClient()?.startSession();
|
|
4044
4099
|
if (!session) {
|
|
4045
4100
|
throw new Error("Failed to start database session.");
|
|
4046
4101
|
}
|
|
@@ -4053,14 +4108,14 @@ function useAppService() {
|
|
|
4053
4108
|
}
|
|
4054
4109
|
}
|
|
4055
4110
|
await session.commitTransaction();
|
|
4056
|
-
|
|
4111
|
+
import_utils21.logger.log({
|
|
4057
4112
|
level: "info",
|
|
4058
4113
|
message: "Default apps added successfully."
|
|
4059
4114
|
});
|
|
4060
4115
|
return;
|
|
4061
4116
|
} catch (error) {
|
|
4062
4117
|
await session.abortTransaction();
|
|
4063
|
-
|
|
4118
|
+
import_utils21.logger.log({
|
|
4064
4119
|
level: "error",
|
|
4065
4120
|
message: `Failed to add default apps: ${error}`
|
|
4066
4121
|
});
|
|
@@ -4084,7 +4139,7 @@ function useAppService() {
|
|
|
4084
4139
|
}
|
|
4085
4140
|
|
|
4086
4141
|
// src/resources/app/app.controller.ts
|
|
4087
|
-
var
|
|
4142
|
+
var import_utils22 = require("@goweekdays/utils");
|
|
4088
4143
|
var import_joi12 = __toESM(require("joi"));
|
|
4089
4144
|
function useAppController() {
|
|
4090
4145
|
const {
|
|
@@ -4098,7 +4153,7 @@ function useAppController() {
|
|
|
4098
4153
|
const value = req.body;
|
|
4099
4154
|
const { error } = schemaApp.validate(value);
|
|
4100
4155
|
if (error) {
|
|
4101
|
-
next(new
|
|
4156
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4102
4157
|
return;
|
|
4103
4158
|
}
|
|
4104
4159
|
try {
|
|
@@ -4113,13 +4168,13 @@ function useAppController() {
|
|
|
4113
4168
|
const id = req.params.id ?? "";
|
|
4114
4169
|
const { error: errorId } = import_joi12.default.string().hex().required().validate(id);
|
|
4115
4170
|
if (errorId) {
|
|
4116
|
-
next(new
|
|
4171
|
+
next(new import_utils22.BadRequestError(errorId.message));
|
|
4117
4172
|
return;
|
|
4118
4173
|
}
|
|
4119
4174
|
const value = req.body;
|
|
4120
4175
|
const { error } = schemaAppUpdate.validate(value);
|
|
4121
4176
|
if (error) {
|
|
4122
|
-
next(new
|
|
4177
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4123
4178
|
return;
|
|
4124
4179
|
}
|
|
4125
4180
|
try {
|
|
@@ -4141,7 +4196,7 @@ function useAppController() {
|
|
|
4141
4196
|
});
|
|
4142
4197
|
const { error } = validation.validate(query);
|
|
4143
4198
|
if (error) {
|
|
4144
|
-
next(new
|
|
4199
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4145
4200
|
return;
|
|
4146
4201
|
}
|
|
4147
4202
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -4180,7 +4235,7 @@ function useAppController() {
|
|
|
4180
4235
|
});
|
|
4181
4236
|
const { error } = validation.validate({ id });
|
|
4182
4237
|
if (error) {
|
|
4183
|
-
next(new
|
|
4238
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4184
4239
|
return;
|
|
4185
4240
|
}
|
|
4186
4241
|
try {
|
|
@@ -4201,7 +4256,7 @@ function useAppController() {
|
|
|
4201
4256
|
});
|
|
4202
4257
|
const { error } = validation.validate({ id });
|
|
4203
4258
|
if (error) {
|
|
4204
|
-
next(new
|
|
4259
|
+
next(new import_utils22.BadRequestError(error.message));
|
|
4205
4260
|
return;
|
|
4206
4261
|
}
|
|
4207
4262
|
try {
|
|
@@ -4237,7 +4292,7 @@ function usePermissionGroupService() {
|
|
|
4237
4292
|
add: addPermission
|
|
4238
4293
|
} = usePermissionRepo();
|
|
4239
4294
|
async function addDefaultModule() {
|
|
4240
|
-
const session =
|
|
4295
|
+
const session = import_utils23.useAtlas.getClient()?.startSession();
|
|
4241
4296
|
if (!session) {
|
|
4242
4297
|
throw new Error("Failed to start database session.");
|
|
4243
4298
|
}
|
|
@@ -4347,7 +4402,7 @@ function usePermissionGroupService() {
|
|
|
4347
4402
|
key: module2.key,
|
|
4348
4403
|
label: module2.label
|
|
4349
4404
|
});
|
|
4350
|
-
|
|
4405
|
+
import_utils23.logger.log({
|
|
4351
4406
|
level: "info",
|
|
4352
4407
|
message: `Default permission group added: ${app.code} - ${module2.key}`
|
|
4353
4408
|
});
|
|
@@ -4556,7 +4611,7 @@ function usePermissionGroupService() {
|
|
|
4556
4611
|
key: module2.key,
|
|
4557
4612
|
label: module2.label
|
|
4558
4613
|
});
|
|
4559
|
-
|
|
4614
|
+
import_utils23.logger.log({
|
|
4560
4615
|
level: "info",
|
|
4561
4616
|
message: `Default permission group added: ${app.code} - ${module2.key}`
|
|
4562
4617
|
});
|
|
@@ -4581,7 +4636,7 @@ function usePermissionGroupService() {
|
|
|
4581
4636
|
}
|
|
4582
4637
|
await session.commitTransaction();
|
|
4583
4638
|
}
|
|
4584
|
-
|
|
4639
|
+
import_utils23.logger.log({
|
|
4585
4640
|
level: "info",
|
|
4586
4641
|
message: "Default permission groups added successfully."
|
|
4587
4642
|
});
|
|
@@ -4601,7 +4656,7 @@ function usePermissionGroupService() {
|
|
|
4601
4656
|
}
|
|
4602
4657
|
const associatedPermissionsCount = await countByGroup(permission.key);
|
|
4603
4658
|
if (associatedPermissionsCount > 0) {
|
|
4604
|
-
throw new
|
|
4659
|
+
throw new import_utils23.BadRequestError(
|
|
4605
4660
|
"Cannot delete Permission Group with associated Permissions."
|
|
4606
4661
|
);
|
|
4607
4662
|
}
|
|
@@ -4609,10 +4664,10 @@ function usePermissionGroupService() {
|
|
|
4609
4664
|
await _deleteById(id);
|
|
4610
4665
|
return "Permission deleted successfully.";
|
|
4611
4666
|
} catch (error) {
|
|
4612
|
-
if (error instanceof
|
|
4667
|
+
if (error instanceof import_utils23.AppError) {
|
|
4613
4668
|
throw error;
|
|
4614
4669
|
} else {
|
|
4615
|
-
throw new
|
|
4670
|
+
throw new import_utils23.InternalServerError("Failed to delete Permission Group.");
|
|
4616
4671
|
}
|
|
4617
4672
|
}
|
|
4618
4673
|
}
|
|
@@ -4623,7 +4678,7 @@ function usePermissionGroupService() {
|
|
|
4623
4678
|
}
|
|
4624
4679
|
|
|
4625
4680
|
// src/resources/permission/permission.group.controller.ts
|
|
4626
|
-
var
|
|
4681
|
+
var import_utils24 = require("@goweekdays/utils");
|
|
4627
4682
|
var import_joi13 = __toESM(require("joi"));
|
|
4628
4683
|
function usePermissionGroupController() {
|
|
4629
4684
|
const {
|
|
@@ -4637,8 +4692,8 @@ function usePermissionGroupController() {
|
|
|
4637
4692
|
const value = req.body;
|
|
4638
4693
|
const { error } = schemaPermissionGroup.validate(value);
|
|
4639
4694
|
if (error) {
|
|
4640
|
-
next(new
|
|
4641
|
-
|
|
4695
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4696
|
+
import_utils24.logger.info(`Controller: ${error.message}`);
|
|
4642
4697
|
return;
|
|
4643
4698
|
}
|
|
4644
4699
|
try {
|
|
@@ -4660,7 +4715,7 @@ function usePermissionGroupController() {
|
|
|
4660
4715
|
});
|
|
4661
4716
|
const { error } = validation.validate(query);
|
|
4662
4717
|
if (error) {
|
|
4663
|
-
next(new
|
|
4718
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4664
4719
|
return;
|
|
4665
4720
|
}
|
|
4666
4721
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -4699,7 +4754,7 @@ function usePermissionGroupController() {
|
|
|
4699
4754
|
});
|
|
4700
4755
|
const { error } = validation.validate({ id });
|
|
4701
4756
|
if (error) {
|
|
4702
|
-
next(new
|
|
4757
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4703
4758
|
return;
|
|
4704
4759
|
}
|
|
4705
4760
|
try {
|
|
@@ -4720,7 +4775,7 @@ function usePermissionGroupController() {
|
|
|
4720
4775
|
});
|
|
4721
4776
|
const { error } = validation.validate({ id });
|
|
4722
4777
|
if (error) {
|
|
4723
|
-
next(new
|
|
4778
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4724
4779
|
return;
|
|
4725
4780
|
}
|
|
4726
4781
|
try {
|
|
@@ -4735,13 +4790,13 @@ function usePermissionGroupController() {
|
|
|
4735
4790
|
const id = req.params.id;
|
|
4736
4791
|
const { error: errorId } = import_joi13.default.string().hex().required().validate(id);
|
|
4737
4792
|
if (errorId) {
|
|
4738
|
-
next(new
|
|
4793
|
+
next(new import_utils24.BadRequestError(errorId.message));
|
|
4739
4794
|
return;
|
|
4740
4795
|
}
|
|
4741
4796
|
const payload = req.body;
|
|
4742
4797
|
const { error } = schemaPermissionGroupUpdate.validate(payload);
|
|
4743
4798
|
if (error) {
|
|
4744
|
-
next(new
|
|
4799
|
+
next(new import_utils24.BadRequestError(error.message));
|
|
4745
4800
|
return;
|
|
4746
4801
|
}
|
|
4747
4802
|
try {
|
|
@@ -4776,16 +4831,16 @@ function useUserService() {
|
|
|
4776
4831
|
const { add: addMember } = useMemberRepo();
|
|
4777
4832
|
const { getAll: getAllPermission } = usePermissionRepo();
|
|
4778
4833
|
async function createDefaultUser() {
|
|
4779
|
-
const session =
|
|
4834
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4780
4835
|
try {
|
|
4781
4836
|
session?.startTransaction();
|
|
4782
4837
|
const _user = await getUserByEmail(DEFAULT_USER_EMAIL);
|
|
4783
4838
|
if (_user) {
|
|
4784
|
-
throw new
|
|
4839
|
+
throw new import_utils25.BadRequestError(
|
|
4785
4840
|
`User already exists: ${DEFAULT_USER_EMAIL}.`
|
|
4786
4841
|
);
|
|
4787
4842
|
}
|
|
4788
|
-
const hashedPassword = await (0,
|
|
4843
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(DEFAULT_USER_PASSWORD);
|
|
4789
4844
|
const userId = new import_mongodb15.ObjectId();
|
|
4790
4845
|
const user = {
|
|
4791
4846
|
_id: userId,
|
|
@@ -4846,14 +4901,14 @@ function useUserService() {
|
|
|
4846
4901
|
}
|
|
4847
4902
|
}
|
|
4848
4903
|
async function createUser(value) {
|
|
4849
|
-
const session =
|
|
4904
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4850
4905
|
session?.startTransaction();
|
|
4851
4906
|
try {
|
|
4852
4907
|
const _user = await getUserByEmail(value.email);
|
|
4853
4908
|
if (_user) {
|
|
4854
|
-
throw new
|
|
4909
|
+
throw new import_utils25.BadRequestError(`User already exists: ${value.email}.`);
|
|
4855
4910
|
}
|
|
4856
|
-
const hashedPassword = await (0,
|
|
4911
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(value.password);
|
|
4857
4912
|
const userId = new import_mongodb15.ObjectId();
|
|
4858
4913
|
const user = {
|
|
4859
4914
|
_id: userId,
|
|
@@ -4885,25 +4940,25 @@ function useUserService() {
|
|
|
4885
4940
|
lastName = "",
|
|
4886
4941
|
password = ""
|
|
4887
4942
|
} = {}) {
|
|
4888
|
-
const session =
|
|
4943
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4889
4944
|
session?.startTransaction();
|
|
4890
4945
|
try {
|
|
4891
4946
|
const invitation = await _getVerificationById(id);
|
|
4892
4947
|
if (!invitation || !invitation.metadata?.app || !invitation.metadata?.role) {
|
|
4893
|
-
throw new
|
|
4948
|
+
throw new import_utils25.BadRequestError("Invalid invitation.");
|
|
4894
4949
|
}
|
|
4895
4950
|
if (invitation.status === "complete") {
|
|
4896
|
-
throw new
|
|
4951
|
+
throw new import_utils25.BadRequestError("Invitation already used.");
|
|
4897
4952
|
}
|
|
4898
4953
|
const expired = new Date(invitation.expireAt) < /* @__PURE__ */ new Date();
|
|
4899
4954
|
if (invitation.status === "expired" || expired) {
|
|
4900
|
-
throw new
|
|
4955
|
+
throw new import_utils25.BadRequestError("Invitation expired.");
|
|
4901
4956
|
}
|
|
4902
4957
|
const email = invitation.email;
|
|
4903
4958
|
const _user = await getUserByEmail(invitation.email);
|
|
4904
4959
|
let userId = _user?._id ?? "";
|
|
4905
4960
|
if (!_user) {
|
|
4906
|
-
const hashedPassword = await (0,
|
|
4961
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(password);
|
|
4907
4962
|
const user = new MUser({
|
|
4908
4963
|
email,
|
|
4909
4964
|
password: hashedPassword,
|
|
@@ -4944,28 +4999,28 @@ function useUserService() {
|
|
|
4944
4999
|
lastName = "",
|
|
4945
5000
|
password = ""
|
|
4946
5001
|
} = {}) {
|
|
4947
|
-
const session =
|
|
5002
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
4948
5003
|
session?.startTransaction();
|
|
4949
5004
|
try {
|
|
4950
5005
|
const signUp = await _getVerificationById(id);
|
|
4951
5006
|
if (!signUp) {
|
|
4952
|
-
throw new
|
|
5007
|
+
throw new import_utils25.BadRequestError("Invalid sign up link.");
|
|
4953
5008
|
}
|
|
4954
5009
|
if (signUp.status === "complete") {
|
|
4955
|
-
throw new
|
|
5010
|
+
throw new import_utils25.BadRequestError(
|
|
4956
5011
|
"You have already an account created using this link."
|
|
4957
5012
|
);
|
|
4958
5013
|
}
|
|
4959
5014
|
const expired = new Date(signUp.expireAt) < /* @__PURE__ */ new Date();
|
|
4960
5015
|
if (signUp.status === "expired" || expired) {
|
|
4961
|
-
throw new
|
|
5016
|
+
throw new import_utils25.BadRequestError("Sign up link expired.");
|
|
4962
5017
|
}
|
|
4963
5018
|
const email = signUp.email;
|
|
4964
5019
|
const _user = await getUserByEmail(signUp.email);
|
|
4965
5020
|
if (_user) {
|
|
4966
|
-
throw new
|
|
5021
|
+
throw new import_utils25.BadRequestError(`User already exists: ${email}.`);
|
|
4967
5022
|
}
|
|
4968
|
-
const hashedPassword = await (0,
|
|
5023
|
+
const hashedPassword = await (0, import_utils25.hashPassword)(password);
|
|
4969
5024
|
const userId = new import_mongodb15.ObjectId();
|
|
4970
5025
|
const user = {
|
|
4971
5026
|
_id: userId,
|
|
@@ -4997,21 +5052,21 @@ function useUserService() {
|
|
|
4997
5052
|
throw error;
|
|
4998
5053
|
}
|
|
4999
5054
|
if (newPassword !== passwordConfirmation) {
|
|
5000
|
-
throw new
|
|
5055
|
+
throw new import_utils25.BadRequestError("Passwords do not match.");
|
|
5001
5056
|
}
|
|
5002
5057
|
let hashedPassword = "";
|
|
5003
5058
|
try {
|
|
5004
|
-
hashedPassword = await (0,
|
|
5059
|
+
hashedPassword = await (0, import_utils25.hashPassword)(newPassword);
|
|
5005
5060
|
} catch (error) {
|
|
5006
|
-
throw new
|
|
5061
|
+
throw new import_utils25.InternalServerError(`Error hashing password: ${error}`);
|
|
5007
5062
|
}
|
|
5008
5063
|
try {
|
|
5009
5064
|
const otpDoc = await getById(id);
|
|
5010
5065
|
if (!otpDoc) {
|
|
5011
|
-
throw new
|
|
5066
|
+
throw new import_utils25.NotFoundError("You are using an invalid reset link.");
|
|
5012
5067
|
}
|
|
5013
5068
|
if (otpDoc.status === "used") {
|
|
5014
|
-
throw new
|
|
5069
|
+
throw new import_utils25.BadRequestError("This link has already been invalidated.");
|
|
5015
5070
|
}
|
|
5016
5071
|
await updateStatusById(id, "used");
|
|
5017
5072
|
return "Successfully reset password.";
|
|
@@ -5022,22 +5077,22 @@ function useUserService() {
|
|
|
5022
5077
|
const { updateName: updateMemberName } = useMemberRepo();
|
|
5023
5078
|
async function updateName(_id, firstName, lastName) {
|
|
5024
5079
|
if (!_id) {
|
|
5025
|
-
throw new
|
|
5080
|
+
throw new import_utils25.BadRequestError("Invalid user ID");
|
|
5026
5081
|
}
|
|
5027
5082
|
if (!firstName) {
|
|
5028
|
-
throw new
|
|
5083
|
+
throw new import_utils25.BadRequestError("Invalid firstName");
|
|
5029
5084
|
}
|
|
5030
5085
|
if (!lastName) {
|
|
5031
|
-
throw new
|
|
5086
|
+
throw new import_utils25.BadRequestError("Invalid lastName");
|
|
5032
5087
|
}
|
|
5033
|
-
const session =
|
|
5088
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
5034
5089
|
session?.startTransaction();
|
|
5035
|
-
const cacheKey = (0,
|
|
5090
|
+
const cacheKey = (0, import_utils25.makeCacheKey)("users", { user: _id });
|
|
5036
5091
|
try {
|
|
5037
|
-
(0,
|
|
5038
|
-
|
|
5092
|
+
(0, import_utils25.useCache)().delCache(cacheKey).then(() => {
|
|
5093
|
+
import_utils25.logger.info(`Cache cleared for user: ${_id}`);
|
|
5039
5094
|
}).catch((error) => {
|
|
5040
|
-
|
|
5095
|
+
import_utils25.logger.error(`Failed to clear cache for user: ${_id}`, error);
|
|
5041
5096
|
});
|
|
5042
5097
|
await _updateName({ _id, firstName, lastName }, session);
|
|
5043
5098
|
await updateMemberName(
|
|
@@ -5055,16 +5110,16 @@ function useUserService() {
|
|
|
5055
5110
|
}
|
|
5056
5111
|
async function updateBirthday(_id, month, day, year) {
|
|
5057
5112
|
if (!_id) {
|
|
5058
|
-
throw new
|
|
5113
|
+
throw new import_utils25.BadRequestError("Invalid user ID");
|
|
5059
5114
|
}
|
|
5060
5115
|
if (!month) {
|
|
5061
|
-
throw new
|
|
5116
|
+
throw new import_utils25.BadRequestError("Invalid birth month.");
|
|
5062
5117
|
}
|
|
5063
5118
|
if (!day) {
|
|
5064
|
-
throw new
|
|
5119
|
+
throw new import_utils25.BadRequestError("Invalid birthday.");
|
|
5065
5120
|
}
|
|
5066
5121
|
if (!year) {
|
|
5067
|
-
throw new
|
|
5122
|
+
throw new import_utils25.BadRequestError("Invalid birth year.");
|
|
5068
5123
|
}
|
|
5069
5124
|
try {
|
|
5070
5125
|
await _updateBirthday({ _id, month, day, year });
|
|
@@ -5081,7 +5136,7 @@ function useUserService() {
|
|
|
5081
5136
|
}
|
|
5082
5137
|
}
|
|
5083
5138
|
const { createFile: _createFile, deleteFileById } = useFileRepo();
|
|
5084
|
-
const s3 = new
|
|
5139
|
+
const s3 = new import_utils25.useS3({
|
|
5085
5140
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
5086
5141
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
5087
5142
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -5089,7 +5144,7 @@ function useUserService() {
|
|
|
5089
5144
|
bucket: SPACES_BUCKET
|
|
5090
5145
|
});
|
|
5091
5146
|
async function updateUserProfile({ file, user, previousProfile } = {}) {
|
|
5092
|
-
const session =
|
|
5147
|
+
const session = import_utils25.useAtlas.getClient()?.startSession();
|
|
5093
5148
|
session?.startTransaction();
|
|
5094
5149
|
const _file = {
|
|
5095
5150
|
name: file.originalname,
|
|
@@ -5145,7 +5200,7 @@ function useAuthController() {
|
|
|
5145
5200
|
});
|
|
5146
5201
|
const { error } = validation.validate({ email, password });
|
|
5147
5202
|
if (error) {
|
|
5148
|
-
next(new
|
|
5203
|
+
next(new import_utils26.BadRequestError(error.message));
|
|
5149
5204
|
return;
|
|
5150
5205
|
}
|
|
5151
5206
|
try {
|
|
@@ -5161,14 +5216,14 @@ function useAuthController() {
|
|
|
5161
5216
|
res.cookie("sid", session.sid, cookieOptions).cookie("user", session.user, cookieOptions).json({ message: "Login successful" });
|
|
5162
5217
|
return;
|
|
5163
5218
|
} catch (error2) {
|
|
5164
|
-
|
|
5219
|
+
import_utils26.logger.log({
|
|
5165
5220
|
level: "error",
|
|
5166
5221
|
message: `Error during login: ${error2.message}`
|
|
5167
5222
|
});
|
|
5168
|
-
if (error2 instanceof
|
|
5223
|
+
if (error2 instanceof import_utils26.AppError) {
|
|
5169
5224
|
next(error2);
|
|
5170
5225
|
} else {
|
|
5171
|
-
next(new
|
|
5226
|
+
next(new import_utils26.InternalServerError("An unexpected error occurred"));
|
|
5172
5227
|
}
|
|
5173
5228
|
return;
|
|
5174
5229
|
}
|
|
@@ -5176,17 +5231,17 @@ function useAuthController() {
|
|
|
5176
5231
|
async function logout(req, res, next) {
|
|
5177
5232
|
const sid = req.headers["authorization"] ?? "";
|
|
5178
5233
|
if (!sid) {
|
|
5179
|
-
next(new
|
|
5234
|
+
next(new import_utils26.BadRequestError("Session ID is required"));
|
|
5180
5235
|
return;
|
|
5181
5236
|
}
|
|
5182
5237
|
try {
|
|
5183
5238
|
await useAuthService().logout(sid);
|
|
5184
5239
|
res.json({ message: "Logged out successfully" });
|
|
5185
5240
|
} catch (error) {
|
|
5186
|
-
if (error instanceof
|
|
5241
|
+
if (error instanceof import_utils26.AppError) {
|
|
5187
5242
|
next(error);
|
|
5188
5243
|
} else {
|
|
5189
|
-
next(new
|
|
5244
|
+
next(new import_utils26.InternalServerError("An unexpected error occurred"));
|
|
5190
5245
|
}
|
|
5191
5246
|
}
|
|
5192
5247
|
}
|
|
@@ -5205,7 +5260,7 @@ function useAuthController() {
|
|
|
5205
5260
|
passwordConfirmation
|
|
5206
5261
|
});
|
|
5207
5262
|
if (error) {
|
|
5208
|
-
next(new
|
|
5263
|
+
next(new import_utils26.BadRequestError(error.message));
|
|
5209
5264
|
return;
|
|
5210
5265
|
}
|
|
5211
5266
|
try {
|
|
@@ -5229,7 +5284,7 @@ function useAuthController() {
|
|
|
5229
5284
|
});
|
|
5230
5285
|
const { error } = validation.validate({ email, referralCode });
|
|
5231
5286
|
if (error) {
|
|
5232
|
-
next(new
|
|
5287
|
+
next(new import_utils26.BadRequestError(error.message));
|
|
5233
5288
|
return;
|
|
5234
5289
|
}
|
|
5235
5290
|
try {
|
|
@@ -5254,7 +5309,7 @@ function useAuthController() {
|
|
|
5254
5309
|
}
|
|
5255
5310
|
|
|
5256
5311
|
// src/resources/building/building.model.ts
|
|
5257
|
-
var
|
|
5312
|
+
var import_utils27 = require("@goweekdays/utils");
|
|
5258
5313
|
var import_joi15 = __toESM(require("joi"));
|
|
5259
5314
|
var import_mongodb16 = require("mongodb");
|
|
5260
5315
|
var schemaBuilding = import_joi15.default.object({
|
|
@@ -5298,20 +5353,20 @@ var schemaUpdateOptions = import_joi15.default.object({
|
|
|
5298
5353
|
function MBuilding(value) {
|
|
5299
5354
|
const { error } = schemaBuilding.validate(value);
|
|
5300
5355
|
if (error) {
|
|
5301
|
-
|
|
5302
|
-
throw new
|
|
5356
|
+
import_utils27.logger.info(`Building Model: ${error.message}`);
|
|
5357
|
+
throw new import_utils27.BadRequestError(error.message);
|
|
5303
5358
|
}
|
|
5304
5359
|
if (value._id && typeof value._id === "string") {
|
|
5305
5360
|
try {
|
|
5306
5361
|
value._id = new import_mongodb16.ObjectId(value._id);
|
|
5307
5362
|
} catch (error2) {
|
|
5308
|
-
throw new
|
|
5363
|
+
throw new import_utils27.BadRequestError("Invalid _id format");
|
|
5309
5364
|
}
|
|
5310
5365
|
}
|
|
5311
5366
|
try {
|
|
5312
5367
|
value.school = new import_mongodb16.ObjectId(value.school);
|
|
5313
5368
|
} catch (error2) {
|
|
5314
|
-
throw new
|
|
5369
|
+
throw new import_utils27.BadRequestError("Invalid school format");
|
|
5315
5370
|
}
|
|
5316
5371
|
return {
|
|
5317
5372
|
_id: value._id ?? void 0,
|
|
@@ -5328,25 +5383,25 @@ function MBuilding(value) {
|
|
|
5328
5383
|
function MBuildingUnit(value) {
|
|
5329
5384
|
const { error } = schemaBuildingUnit.validate(value);
|
|
5330
5385
|
if (error) {
|
|
5331
|
-
|
|
5332
|
-
throw new
|
|
5386
|
+
import_utils27.logger.info(`Building Unit Model: ${error.message}`);
|
|
5387
|
+
throw new import_utils27.BadRequestError(error.message);
|
|
5333
5388
|
}
|
|
5334
5389
|
if (value._id && typeof value._id === "string") {
|
|
5335
5390
|
try {
|
|
5336
5391
|
value._id = new import_mongodb16.ObjectId(value._id);
|
|
5337
5392
|
} catch (error2) {
|
|
5338
|
-
throw new
|
|
5393
|
+
throw new import_utils27.BadRequestError("Invalid ID");
|
|
5339
5394
|
}
|
|
5340
5395
|
}
|
|
5341
5396
|
try {
|
|
5342
5397
|
value.school = new import_mongodb16.ObjectId(value.school);
|
|
5343
5398
|
} catch (error2) {
|
|
5344
|
-
throw new
|
|
5399
|
+
throw new import_utils27.BadRequestError("Invalid school ID");
|
|
5345
5400
|
}
|
|
5346
5401
|
try {
|
|
5347
5402
|
value.building = new import_mongodb16.ObjectId(value.building);
|
|
5348
5403
|
} catch (error2) {
|
|
5349
|
-
throw new
|
|
5404
|
+
throw new import_utils27.BadRequestError("Invalid building ID");
|
|
5350
5405
|
}
|
|
5351
5406
|
return {
|
|
5352
5407
|
_id: value._id ?? void 0,
|
|
@@ -5370,16 +5425,16 @@ function MBuildingUnit(value) {
|
|
|
5370
5425
|
}
|
|
5371
5426
|
|
|
5372
5427
|
// src/resources/building/building.repository.ts
|
|
5373
|
-
var
|
|
5428
|
+
var import_utils28 = require("@goweekdays/utils");
|
|
5374
5429
|
var import_mongodb17 = require("mongodb");
|
|
5375
5430
|
function useBuildingRepo() {
|
|
5376
|
-
const db =
|
|
5431
|
+
const db = import_utils28.useAtlas.getDb();
|
|
5377
5432
|
if (!db) {
|
|
5378
5433
|
throw new Error("Unable to connect to server.");
|
|
5379
5434
|
}
|
|
5380
5435
|
const namespace_collection = "school.buildings";
|
|
5381
5436
|
const collection = db.collection(namespace_collection);
|
|
5382
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
5437
|
+
const { getCache, setCache, delNamespace } = (0, import_utils28.useCache)(namespace_collection);
|
|
5383
5438
|
async function createIndexes() {
|
|
5384
5439
|
try {
|
|
5385
5440
|
await collection.createIndexes([
|
|
@@ -5392,7 +5447,7 @@ function useBuildingRepo() {
|
|
|
5392
5447
|
}
|
|
5393
5448
|
}
|
|
5394
5449
|
createIndexes().catch((error) => {
|
|
5395
|
-
|
|
5450
|
+
import_utils28.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
5396
5451
|
});
|
|
5397
5452
|
async function add(value, session) {
|
|
5398
5453
|
try {
|
|
@@ -5401,16 +5456,16 @@ function useBuildingRepo() {
|
|
|
5401
5456
|
delCachedData();
|
|
5402
5457
|
return res.insertedId;
|
|
5403
5458
|
} catch (error) {
|
|
5404
|
-
|
|
5459
|
+
import_utils28.logger.log({
|
|
5405
5460
|
level: "error",
|
|
5406
5461
|
message: error.message
|
|
5407
5462
|
});
|
|
5408
|
-
if (error instanceof
|
|
5463
|
+
if (error instanceof import_utils28.AppError) {
|
|
5409
5464
|
throw error;
|
|
5410
5465
|
} else {
|
|
5411
5466
|
const isDuplicated = error.message.includes("duplicate");
|
|
5412
5467
|
if (isDuplicated) {
|
|
5413
|
-
throw new
|
|
5468
|
+
throw new import_utils28.BadRequestError("Building already exists.");
|
|
5414
5469
|
}
|
|
5415
5470
|
throw new Error("Failed to create building.");
|
|
5416
5471
|
}
|
|
@@ -5420,7 +5475,7 @@ function useBuildingRepo() {
|
|
|
5420
5475
|
try {
|
|
5421
5476
|
_id = new import_mongodb17.ObjectId(_id);
|
|
5422
5477
|
} catch (error) {
|
|
5423
|
-
throw new
|
|
5478
|
+
throw new import_utils28.BadRequestError("Invalid ID.");
|
|
5424
5479
|
}
|
|
5425
5480
|
try {
|
|
5426
5481
|
const res = await collection.updateOne(
|
|
@@ -5431,11 +5486,11 @@ function useBuildingRepo() {
|
|
|
5431
5486
|
delCachedData();
|
|
5432
5487
|
return res;
|
|
5433
5488
|
} catch (error) {
|
|
5434
|
-
|
|
5489
|
+
import_utils28.logger.log({
|
|
5435
5490
|
level: "error",
|
|
5436
5491
|
message: error.message
|
|
5437
5492
|
});
|
|
5438
|
-
if (error instanceof
|
|
5493
|
+
if (error instanceof import_utils28.AppError) {
|
|
5439
5494
|
throw error;
|
|
5440
5495
|
} else {
|
|
5441
5496
|
throw new Error("Failed to update building.");
|
|
@@ -5462,7 +5517,7 @@ function useBuildingRepo() {
|
|
|
5462
5517
|
try {
|
|
5463
5518
|
query.school = new import_mongodb17.ObjectId(school);
|
|
5464
5519
|
} catch (error) {
|
|
5465
|
-
throw new
|
|
5520
|
+
throw new import_utils28.BadRequestError("Invalid school ID.");
|
|
5466
5521
|
}
|
|
5467
5522
|
}
|
|
5468
5523
|
const cacheParams = {
|
|
@@ -5476,15 +5531,15 @@ function useBuildingRepo() {
|
|
|
5476
5531
|
cacheParams.school = school;
|
|
5477
5532
|
if (status !== "active")
|
|
5478
5533
|
cacheParams.status = status;
|
|
5479
|
-
const cacheKey = (0,
|
|
5480
|
-
|
|
5534
|
+
const cacheKey = (0, import_utils28.makeCacheKey)(namespace_collection, cacheParams);
|
|
5535
|
+
import_utils28.logger.log({
|
|
5481
5536
|
level: "info",
|
|
5482
5537
|
message: `Cache key for getAll buildings: ${cacheKey}`
|
|
5483
5538
|
});
|
|
5484
5539
|
try {
|
|
5485
5540
|
const cached = await getCache(cacheKey);
|
|
5486
5541
|
if (cached) {
|
|
5487
|
-
|
|
5542
|
+
import_utils28.logger.log({
|
|
5488
5543
|
level: "info",
|
|
5489
5544
|
message: `Cache hit for getAll buildings: ${cacheKey}`
|
|
5490
5545
|
});
|
|
@@ -5497,21 +5552,21 @@ function useBuildingRepo() {
|
|
|
5497
5552
|
{ $limit: limit }
|
|
5498
5553
|
]).toArray();
|
|
5499
5554
|
const length = await collection.countDocuments(query);
|
|
5500
|
-
const data = (0,
|
|
5555
|
+
const data = (0, import_utils28.paginate)(items, page, limit, length);
|
|
5501
5556
|
setCache(cacheKey, data, 600).then(() => {
|
|
5502
|
-
|
|
5557
|
+
import_utils28.logger.log({
|
|
5503
5558
|
level: "info",
|
|
5504
5559
|
message: `Cache set for getAll buildings: ${cacheKey}`
|
|
5505
5560
|
});
|
|
5506
5561
|
}).catch((err) => {
|
|
5507
|
-
|
|
5562
|
+
import_utils28.logger.log({
|
|
5508
5563
|
level: "error",
|
|
5509
5564
|
message: `Failed to set cache for getAll buildings: ${err.message}`
|
|
5510
5565
|
});
|
|
5511
5566
|
});
|
|
5512
5567
|
return data;
|
|
5513
5568
|
} catch (error) {
|
|
5514
|
-
|
|
5569
|
+
import_utils28.logger.log({ level: "error", message: `${error}` });
|
|
5515
5570
|
throw error;
|
|
5516
5571
|
}
|
|
5517
5572
|
}
|
|
@@ -5519,13 +5574,13 @@ function useBuildingRepo() {
|
|
|
5519
5574
|
try {
|
|
5520
5575
|
_id = new import_mongodb17.ObjectId(_id);
|
|
5521
5576
|
} catch (error) {
|
|
5522
|
-
throw new
|
|
5577
|
+
throw new import_utils28.BadRequestError("Invalid ID.");
|
|
5523
5578
|
}
|
|
5524
|
-
const cacheKey = (0,
|
|
5579
|
+
const cacheKey = (0, import_utils28.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
5525
5580
|
try {
|
|
5526
5581
|
const cached = await getCache(cacheKey);
|
|
5527
5582
|
if (cached) {
|
|
5528
|
-
|
|
5583
|
+
import_utils28.logger.log({
|
|
5529
5584
|
level: "info",
|
|
5530
5585
|
message: `Cache hit for getById building: ${cacheKey}`
|
|
5531
5586
|
});
|
|
@@ -5535,22 +5590,22 @@ function useBuildingRepo() {
|
|
|
5535
5590
|
_id
|
|
5536
5591
|
});
|
|
5537
5592
|
setCache(cacheKey, result, 300).then(() => {
|
|
5538
|
-
|
|
5593
|
+
import_utils28.logger.log({
|
|
5539
5594
|
level: "info",
|
|
5540
5595
|
message: `Cache set for building by id: ${cacheKey}`
|
|
5541
5596
|
});
|
|
5542
5597
|
}).catch((err) => {
|
|
5543
|
-
|
|
5598
|
+
import_utils28.logger.log({
|
|
5544
5599
|
level: "error",
|
|
5545
5600
|
message: `Failed to set cache for building by id: ${err.message}`
|
|
5546
5601
|
});
|
|
5547
5602
|
});
|
|
5548
5603
|
return result;
|
|
5549
5604
|
} catch (error) {
|
|
5550
|
-
if (error instanceof
|
|
5605
|
+
if (error instanceof import_utils28.AppError) {
|
|
5551
5606
|
throw error;
|
|
5552
5607
|
} else {
|
|
5553
|
-
throw new
|
|
5608
|
+
throw new import_utils28.InternalServerError("Failed to get building.");
|
|
5554
5609
|
}
|
|
5555
5610
|
}
|
|
5556
5611
|
}
|
|
@@ -5558,7 +5613,7 @@ function useBuildingRepo() {
|
|
|
5558
5613
|
try {
|
|
5559
5614
|
_id = new import_mongodb17.ObjectId(_id);
|
|
5560
5615
|
} catch (error) {
|
|
5561
|
-
throw new
|
|
5616
|
+
throw new import_utils28.BadRequestError("Invalid ID.");
|
|
5562
5617
|
}
|
|
5563
5618
|
try {
|
|
5564
5619
|
const res = await collection.updateOne(
|
|
@@ -5568,25 +5623,25 @@ function useBuildingRepo() {
|
|
|
5568
5623
|
delCachedData();
|
|
5569
5624
|
return res;
|
|
5570
5625
|
} catch (error) {
|
|
5571
|
-
|
|
5626
|
+
import_utils28.logger.log({
|
|
5572
5627
|
level: "error",
|
|
5573
5628
|
message: error.message
|
|
5574
5629
|
});
|
|
5575
|
-
if (error instanceof
|
|
5630
|
+
if (error instanceof import_utils28.AppError) {
|
|
5576
5631
|
throw error;
|
|
5577
5632
|
} else {
|
|
5578
|
-
throw new
|
|
5633
|
+
throw new import_utils28.InternalServerError("Failed to delete building.");
|
|
5579
5634
|
}
|
|
5580
5635
|
}
|
|
5581
5636
|
}
|
|
5582
5637
|
function delCachedData() {
|
|
5583
5638
|
delNamespace().then(() => {
|
|
5584
|
-
|
|
5639
|
+
import_utils28.logger.log({
|
|
5585
5640
|
level: "info",
|
|
5586
5641
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
5587
5642
|
});
|
|
5588
5643
|
}).catch((err) => {
|
|
5589
|
-
|
|
5644
|
+
import_utils28.logger.log({
|
|
5590
5645
|
level: "error",
|
|
5591
5646
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
5592
5647
|
});
|
|
@@ -5603,19 +5658,19 @@ function useBuildingRepo() {
|
|
|
5603
5658
|
}
|
|
5604
5659
|
|
|
5605
5660
|
// src/resources/building/building.service.ts
|
|
5606
|
-
var
|
|
5661
|
+
var import_utils30 = require("@goweekdays/utils");
|
|
5607
5662
|
|
|
5608
5663
|
// src/resources/building/building-unit.repository.ts
|
|
5609
|
-
var
|
|
5664
|
+
var import_utils29 = require("@goweekdays/utils");
|
|
5610
5665
|
var import_mongodb18 = require("mongodb");
|
|
5611
5666
|
function useBuildingUnitRepo() {
|
|
5612
|
-
const db =
|
|
5667
|
+
const db = import_utils29.useAtlas.getDb();
|
|
5613
5668
|
if (!db) {
|
|
5614
5669
|
throw new Error("Unable to connect to server.");
|
|
5615
5670
|
}
|
|
5616
5671
|
const namespace_collection = "school.building-units";
|
|
5617
5672
|
const collection = db.collection(namespace_collection);
|
|
5618
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
5673
|
+
const { getCache, setCache, delNamespace } = (0, import_utils29.useCache)(namespace_collection);
|
|
5619
5674
|
async function createIndexes() {
|
|
5620
5675
|
try {
|
|
5621
5676
|
await collection.createIndexes([
|
|
@@ -5643,12 +5698,12 @@ function useBuildingUnitRepo() {
|
|
|
5643
5698
|
}
|
|
5644
5699
|
function delCachedData() {
|
|
5645
5700
|
delNamespace().then(() => {
|
|
5646
|
-
|
|
5701
|
+
import_utils29.logger.log({
|
|
5647
5702
|
level: "info",
|
|
5648
5703
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
5649
5704
|
});
|
|
5650
5705
|
}).catch((err) => {
|
|
5651
|
-
|
|
5706
|
+
import_utils29.logger.log({
|
|
5652
5707
|
level: "error",
|
|
5653
5708
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
5654
5709
|
});
|
|
@@ -5661,11 +5716,11 @@ function useBuildingUnitRepo() {
|
|
|
5661
5716
|
delCachedData();
|
|
5662
5717
|
return res.insertedId;
|
|
5663
5718
|
} catch (error) {
|
|
5664
|
-
|
|
5719
|
+
import_utils29.logger.log({
|
|
5665
5720
|
level: "error",
|
|
5666
5721
|
message: error.message
|
|
5667
5722
|
});
|
|
5668
|
-
if (error instanceof
|
|
5723
|
+
if (error instanceof import_utils29.AppError) {
|
|
5669
5724
|
throw error;
|
|
5670
5725
|
} else {
|
|
5671
5726
|
throw new Error("Failed to create building unit.");
|
|
@@ -5675,12 +5730,12 @@ function useBuildingUnitRepo() {
|
|
|
5675
5730
|
async function updateById(_id, value, session) {
|
|
5676
5731
|
const { error } = schemaUpdateOptions.validate(value);
|
|
5677
5732
|
if (error) {
|
|
5678
|
-
throw new
|
|
5733
|
+
throw new import_utils29.BadRequestError(error.message);
|
|
5679
5734
|
}
|
|
5680
5735
|
try {
|
|
5681
5736
|
_id = new import_mongodb18.ObjectId(_id);
|
|
5682
5737
|
} catch (error2) {
|
|
5683
|
-
throw new
|
|
5738
|
+
throw new import_utils29.BadRequestError("Invalid ID.");
|
|
5684
5739
|
}
|
|
5685
5740
|
try {
|
|
5686
5741
|
const res = await collection.updateOne(
|
|
@@ -5691,11 +5746,11 @@ function useBuildingUnitRepo() {
|
|
|
5691
5746
|
delCachedData();
|
|
5692
5747
|
return res;
|
|
5693
5748
|
} catch (error2) {
|
|
5694
|
-
|
|
5749
|
+
import_utils29.logger.log({
|
|
5695
5750
|
level: "error",
|
|
5696
5751
|
message: error2.message
|
|
5697
5752
|
});
|
|
5698
|
-
if (error2 instanceof
|
|
5753
|
+
if (error2 instanceof import_utils29.AppError) {
|
|
5699
5754
|
throw error2;
|
|
5700
5755
|
} else {
|
|
5701
5756
|
throw new Error("Failed to create building unit.");
|
|
@@ -5705,12 +5760,12 @@ function useBuildingUnitRepo() {
|
|
|
5705
5760
|
async function updateByBuildingId(building, value, session) {
|
|
5706
5761
|
const { error } = schemaUpdateOptions.validate(value);
|
|
5707
5762
|
if (error) {
|
|
5708
|
-
throw new
|
|
5763
|
+
throw new import_utils29.BadRequestError(error.message);
|
|
5709
5764
|
}
|
|
5710
5765
|
try {
|
|
5711
5766
|
building = new import_mongodb18.ObjectId(building);
|
|
5712
5767
|
} catch (error2) {
|
|
5713
|
-
throw new
|
|
5768
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5714
5769
|
}
|
|
5715
5770
|
try {
|
|
5716
5771
|
const res = await collection.updateMany(
|
|
@@ -5721,11 +5776,11 @@ function useBuildingUnitRepo() {
|
|
|
5721
5776
|
delCachedData();
|
|
5722
5777
|
return res;
|
|
5723
5778
|
} catch (error2) {
|
|
5724
|
-
|
|
5779
|
+
import_utils29.logger.log({
|
|
5725
5780
|
level: "error",
|
|
5726
5781
|
message: error2.message
|
|
5727
5782
|
});
|
|
5728
|
-
if (error2 instanceof
|
|
5783
|
+
if (error2 instanceof import_utils29.AppError) {
|
|
5729
5784
|
throw error2;
|
|
5730
5785
|
} else {
|
|
5731
5786
|
throw new Error("Failed to update building unit.");
|
|
@@ -5754,14 +5809,14 @@ function useBuildingUnitRepo() {
|
|
|
5754
5809
|
try {
|
|
5755
5810
|
query.school = new import_mongodb18.ObjectId(school);
|
|
5756
5811
|
} catch (error) {
|
|
5757
|
-
throw new
|
|
5812
|
+
throw new import_utils29.BadRequestError("Invalid school ID.");
|
|
5758
5813
|
}
|
|
5759
5814
|
}
|
|
5760
5815
|
if (building) {
|
|
5761
5816
|
try {
|
|
5762
5817
|
query.building = new import_mongodb18.ObjectId(building);
|
|
5763
5818
|
} catch (error) {
|
|
5764
|
-
throw new
|
|
5819
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5765
5820
|
}
|
|
5766
5821
|
}
|
|
5767
5822
|
const cacheParams = {
|
|
@@ -5777,15 +5832,15 @@ function useBuildingUnitRepo() {
|
|
|
5777
5832
|
cacheParams.building = building;
|
|
5778
5833
|
if (status !== "active")
|
|
5779
5834
|
cacheParams.status = status;
|
|
5780
|
-
const cacheKey = (0,
|
|
5781
|
-
|
|
5835
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, cacheParams);
|
|
5836
|
+
import_utils29.logger.log({
|
|
5782
5837
|
level: "info",
|
|
5783
5838
|
message: `Cache key for getAll building units: ${cacheKey}`
|
|
5784
5839
|
});
|
|
5785
5840
|
try {
|
|
5786
5841
|
const cached = await getCache(cacheKey);
|
|
5787
5842
|
if (cached) {
|
|
5788
|
-
|
|
5843
|
+
import_utils29.logger.log({
|
|
5789
5844
|
level: "info",
|
|
5790
5845
|
message: `Cache hit for getAll building units: ${cacheKey}`
|
|
5791
5846
|
});
|
|
@@ -5798,21 +5853,21 @@ function useBuildingUnitRepo() {
|
|
|
5798
5853
|
{ $limit: limit }
|
|
5799
5854
|
]).toArray();
|
|
5800
5855
|
const length = await collection.countDocuments(query);
|
|
5801
|
-
const data = (0,
|
|
5856
|
+
const data = (0, import_utils29.paginate)(items, page, limit, length);
|
|
5802
5857
|
setCache(cacheKey, data, 600).then(() => {
|
|
5803
|
-
|
|
5858
|
+
import_utils29.logger.log({
|
|
5804
5859
|
level: "info",
|
|
5805
5860
|
message: `Cache set for getAll building units: ${cacheKey}`
|
|
5806
5861
|
});
|
|
5807
5862
|
}).catch((err) => {
|
|
5808
|
-
|
|
5863
|
+
import_utils29.logger.log({
|
|
5809
5864
|
level: "error",
|
|
5810
5865
|
message: `Failed to set cache for getAll building units: ${err.message}`
|
|
5811
5866
|
});
|
|
5812
5867
|
});
|
|
5813
5868
|
return data;
|
|
5814
5869
|
} catch (error) {
|
|
5815
|
-
|
|
5870
|
+
import_utils29.logger.log({ level: "error", message: `${error}` });
|
|
5816
5871
|
throw error;
|
|
5817
5872
|
}
|
|
5818
5873
|
}
|
|
@@ -5820,13 +5875,13 @@ function useBuildingUnitRepo() {
|
|
|
5820
5875
|
try {
|
|
5821
5876
|
_id = new import_mongodb18.ObjectId(_id);
|
|
5822
5877
|
} catch (error) {
|
|
5823
|
-
throw new
|
|
5878
|
+
throw new import_utils29.BadRequestError("Invalid ID.");
|
|
5824
5879
|
}
|
|
5825
|
-
const cacheKey = (0,
|
|
5880
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
5826
5881
|
try {
|
|
5827
5882
|
const cached = await getCache(cacheKey);
|
|
5828
5883
|
if (cached) {
|
|
5829
|
-
|
|
5884
|
+
import_utils29.logger.log({
|
|
5830
5885
|
level: "info",
|
|
5831
5886
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
5832
5887
|
});
|
|
@@ -5837,25 +5892,25 @@ function useBuildingUnitRepo() {
|
|
|
5837
5892
|
deletedAt: { $in: ["", null] }
|
|
5838
5893
|
});
|
|
5839
5894
|
if (!result) {
|
|
5840
|
-
throw new
|
|
5895
|
+
throw new import_utils29.BadRequestError("Building unit not found.");
|
|
5841
5896
|
}
|
|
5842
5897
|
setCache(cacheKey, result, 300).then(() => {
|
|
5843
|
-
|
|
5898
|
+
import_utils29.logger.log({
|
|
5844
5899
|
level: "info",
|
|
5845
5900
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
5846
5901
|
});
|
|
5847
5902
|
}).catch((err) => {
|
|
5848
|
-
|
|
5903
|
+
import_utils29.logger.log({
|
|
5849
5904
|
level: "error",
|
|
5850
5905
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
5851
5906
|
});
|
|
5852
5907
|
});
|
|
5853
5908
|
return result;
|
|
5854
5909
|
} catch (error) {
|
|
5855
|
-
if (error instanceof
|
|
5910
|
+
if (error instanceof import_utils29.AppError) {
|
|
5856
5911
|
throw error;
|
|
5857
5912
|
} else {
|
|
5858
|
-
throw new
|
|
5913
|
+
throw new import_utils29.InternalServerError("Failed to get building unit.");
|
|
5859
5914
|
}
|
|
5860
5915
|
}
|
|
5861
5916
|
}
|
|
@@ -5863,16 +5918,16 @@ function useBuildingUnitRepo() {
|
|
|
5863
5918
|
try {
|
|
5864
5919
|
building = new import_mongodb18.ObjectId(building);
|
|
5865
5920
|
} catch (error) {
|
|
5866
|
-
throw new
|
|
5921
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5867
5922
|
}
|
|
5868
|
-
const cacheKey = (0,
|
|
5923
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, {
|
|
5869
5924
|
building: String(building),
|
|
5870
5925
|
level
|
|
5871
5926
|
});
|
|
5872
5927
|
try {
|
|
5873
5928
|
const cached = await getCache(cacheKey);
|
|
5874
5929
|
if (cached) {
|
|
5875
|
-
|
|
5930
|
+
import_utils29.logger.log({
|
|
5876
5931
|
level: "info",
|
|
5877
5932
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
5878
5933
|
});
|
|
@@ -5884,22 +5939,22 @@ function useBuildingUnitRepo() {
|
|
|
5884
5939
|
status: "active"
|
|
5885
5940
|
});
|
|
5886
5941
|
setCache(cacheKey, result, 300).then(() => {
|
|
5887
|
-
|
|
5942
|
+
import_utils29.logger.log({
|
|
5888
5943
|
level: "info",
|
|
5889
5944
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
5890
5945
|
});
|
|
5891
5946
|
}).catch((err) => {
|
|
5892
|
-
|
|
5947
|
+
import_utils29.logger.log({
|
|
5893
5948
|
level: "error",
|
|
5894
5949
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
5895
5950
|
});
|
|
5896
5951
|
});
|
|
5897
5952
|
return result;
|
|
5898
5953
|
} catch (error) {
|
|
5899
|
-
if (error instanceof
|
|
5954
|
+
if (error instanceof import_utils29.AppError) {
|
|
5900
5955
|
throw error;
|
|
5901
5956
|
} else {
|
|
5902
|
-
throw new
|
|
5957
|
+
throw new import_utils29.InternalServerError("Failed to get building unit.");
|
|
5903
5958
|
}
|
|
5904
5959
|
}
|
|
5905
5960
|
}
|
|
@@ -5907,15 +5962,15 @@ function useBuildingUnitRepo() {
|
|
|
5907
5962
|
try {
|
|
5908
5963
|
building = new import_mongodb18.ObjectId(building);
|
|
5909
5964
|
} catch (error) {
|
|
5910
|
-
throw new
|
|
5965
|
+
throw new import_utils29.BadRequestError("Invalid building ID.");
|
|
5911
5966
|
}
|
|
5912
|
-
const cacheKey = (0,
|
|
5967
|
+
const cacheKey = (0, import_utils29.makeCacheKey)(namespace_collection, {
|
|
5913
5968
|
building: String(building)
|
|
5914
5969
|
});
|
|
5915
5970
|
try {
|
|
5916
5971
|
const cached = await getCache(cacheKey);
|
|
5917
5972
|
if (cached) {
|
|
5918
|
-
|
|
5973
|
+
import_utils29.logger.log({
|
|
5919
5974
|
level: "info",
|
|
5920
5975
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
5921
5976
|
});
|
|
@@ -5926,22 +5981,22 @@ function useBuildingUnitRepo() {
|
|
|
5926
5981
|
status: "active"
|
|
5927
5982
|
});
|
|
5928
5983
|
setCache(cacheKey, result, 300).then(() => {
|
|
5929
|
-
|
|
5984
|
+
import_utils29.logger.log({
|
|
5930
5985
|
level: "info",
|
|
5931
5986
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
5932
5987
|
});
|
|
5933
5988
|
}).catch((err) => {
|
|
5934
|
-
|
|
5989
|
+
import_utils29.logger.log({
|
|
5935
5990
|
level: "error",
|
|
5936
5991
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
5937
5992
|
});
|
|
5938
5993
|
});
|
|
5939
5994
|
return result;
|
|
5940
5995
|
} catch (error) {
|
|
5941
|
-
if (error instanceof
|
|
5996
|
+
if (error instanceof import_utils29.AppError) {
|
|
5942
5997
|
throw error;
|
|
5943
5998
|
} else {
|
|
5944
|
-
throw new
|
|
5999
|
+
throw new import_utils29.InternalServerError("Failed to get building unit.");
|
|
5945
6000
|
}
|
|
5946
6001
|
}
|
|
5947
6002
|
}
|
|
@@ -5949,7 +6004,7 @@ function useBuildingUnitRepo() {
|
|
|
5949
6004
|
try {
|
|
5950
6005
|
_id = new import_mongodb18.ObjectId(_id);
|
|
5951
6006
|
} catch (error) {
|
|
5952
|
-
throw new
|
|
6007
|
+
throw new import_utils29.BadRequestError("Invalid ID.");
|
|
5953
6008
|
}
|
|
5954
6009
|
try {
|
|
5955
6010
|
const res = await collection.updateOne(
|
|
@@ -5960,11 +6015,11 @@ function useBuildingUnitRepo() {
|
|
|
5960
6015
|
delCachedData();
|
|
5961
6016
|
return "Room/Facility deleted successfully.";
|
|
5962
6017
|
} catch (error) {
|
|
5963
|
-
|
|
6018
|
+
import_utils29.logger.log({
|
|
5964
6019
|
level: "error",
|
|
5965
6020
|
message: error.message
|
|
5966
6021
|
});
|
|
5967
|
-
if (error instanceof
|
|
6022
|
+
if (error instanceof import_utils29.AppError) {
|
|
5968
6023
|
throw error;
|
|
5969
6024
|
} else {
|
|
5970
6025
|
throw new Error("Failed to deleted room/facility.");
|
|
@@ -5994,16 +6049,16 @@ function useBuildingService() {
|
|
|
5994
6049
|
const { getByBuildingLevel, getByBuilding, updateByBuildingId } = useBuildingUnitRepo();
|
|
5995
6050
|
async function updateById(id, data) {
|
|
5996
6051
|
data.levels = Number(data.levels);
|
|
5997
|
-
const session =
|
|
6052
|
+
const session = import_utils30.useAtlas.getClient()?.startSession();
|
|
5998
6053
|
try {
|
|
5999
6054
|
const building = await _getById(id);
|
|
6000
6055
|
if (!building) {
|
|
6001
|
-
throw new
|
|
6056
|
+
throw new import_utils30.NotFoundError("Building not found.");
|
|
6002
6057
|
}
|
|
6003
6058
|
if (data.levels < building.levels) {
|
|
6004
6059
|
const unit = await getByBuildingLevel(id, building.levels);
|
|
6005
6060
|
if (unit) {
|
|
6006
|
-
throw new
|
|
6061
|
+
throw new import_utils30.BadRequestError(
|
|
6007
6062
|
"Cannot reduce floors, there are existing building units at higher floors."
|
|
6008
6063
|
);
|
|
6009
6064
|
}
|
|
@@ -6025,7 +6080,7 @@ function useBuildingService() {
|
|
|
6025
6080
|
async function deleteById(id) {
|
|
6026
6081
|
const building = await getByBuilding(id);
|
|
6027
6082
|
if (building) {
|
|
6028
|
-
throw new
|
|
6083
|
+
throw new import_utils30.BadRequestError(
|
|
6029
6084
|
"Cannot delete building with existing room/facility. Please delete room/facility first."
|
|
6030
6085
|
);
|
|
6031
6086
|
}
|
|
@@ -6043,7 +6098,7 @@ function useBuildingService() {
|
|
|
6043
6098
|
}
|
|
6044
6099
|
|
|
6045
6100
|
// src/resources/building/building.controller.ts
|
|
6046
|
-
var
|
|
6101
|
+
var import_utils31 = require("@goweekdays/utils");
|
|
6047
6102
|
var import_joi16 = __toESM(require("joi"));
|
|
6048
6103
|
function useBuildingController() {
|
|
6049
6104
|
const { getAll: _getAll, getById: _getById, add: _add } = useBuildingRepo();
|
|
@@ -6059,8 +6114,8 @@ function useBuildingController() {
|
|
|
6059
6114
|
});
|
|
6060
6115
|
const { error } = validation.validate(value);
|
|
6061
6116
|
if (error) {
|
|
6062
|
-
next(new
|
|
6063
|
-
|
|
6117
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6118
|
+
import_utils31.logger.info(`Controller: ${error.message}`);
|
|
6064
6119
|
return;
|
|
6065
6120
|
}
|
|
6066
6121
|
try {
|
|
@@ -6084,8 +6139,8 @@ function useBuildingController() {
|
|
|
6084
6139
|
});
|
|
6085
6140
|
const { error } = validation.validate({ id, value });
|
|
6086
6141
|
if (error) {
|
|
6087
|
-
next(new
|
|
6088
|
-
|
|
6142
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6143
|
+
import_utils31.logger.info(`Controller: ${error.message}`);
|
|
6089
6144
|
return;
|
|
6090
6145
|
}
|
|
6091
6146
|
try {
|
|
@@ -6107,7 +6162,7 @@ function useBuildingController() {
|
|
|
6107
6162
|
});
|
|
6108
6163
|
const { error } = validation.validate(query);
|
|
6109
6164
|
if (error) {
|
|
6110
|
-
next(new
|
|
6165
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6111
6166
|
return;
|
|
6112
6167
|
}
|
|
6113
6168
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -6146,7 +6201,7 @@ function useBuildingController() {
|
|
|
6146
6201
|
});
|
|
6147
6202
|
const { error } = validation.validate({ id });
|
|
6148
6203
|
if (error) {
|
|
6149
|
-
next(new
|
|
6204
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6150
6205
|
return;
|
|
6151
6206
|
}
|
|
6152
6207
|
try {
|
|
@@ -6167,7 +6222,7 @@ function useBuildingController() {
|
|
|
6167
6222
|
});
|
|
6168
6223
|
const { error } = validation.validate({ id });
|
|
6169
6224
|
if (error) {
|
|
6170
|
-
next(new
|
|
6225
|
+
next(new import_utils31.BadRequestError(error.message));
|
|
6171
6226
|
return;
|
|
6172
6227
|
}
|
|
6173
6228
|
try {
|
|
@@ -6188,11 +6243,11 @@ function useBuildingController() {
|
|
|
6188
6243
|
}
|
|
6189
6244
|
|
|
6190
6245
|
// src/resources/building/building-unit.service.ts
|
|
6191
|
-
var
|
|
6246
|
+
var import_utils32 = require("@goweekdays/utils");
|
|
6192
6247
|
function useBuildingUnitService() {
|
|
6193
6248
|
const { add: _add } = useBuildingUnitRepo();
|
|
6194
6249
|
async function add(value) {
|
|
6195
|
-
const session =
|
|
6250
|
+
const session = import_utils32.useAtlas.getClient()?.startSession();
|
|
6196
6251
|
if (!session) {
|
|
6197
6252
|
throw new Error("Unable to start session for building unit service.");
|
|
6198
6253
|
}
|
|
@@ -6219,7 +6274,7 @@ function useBuildingUnitService() {
|
|
|
6219
6274
|
}
|
|
6220
6275
|
|
|
6221
6276
|
// src/resources/building/building-unit.controller.ts
|
|
6222
|
-
var
|
|
6277
|
+
var import_utils33 = require("@goweekdays/utils");
|
|
6223
6278
|
var import_joi17 = __toESM(require("joi"));
|
|
6224
6279
|
function useBuildingUnitController() {
|
|
6225
6280
|
const {
|
|
@@ -6251,7 +6306,7 @@ function useBuildingUnitController() {
|
|
|
6251
6306
|
});
|
|
6252
6307
|
const { error } = validation.validate(data);
|
|
6253
6308
|
if (error) {
|
|
6254
|
-
next(new
|
|
6309
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6255
6310
|
return;
|
|
6256
6311
|
}
|
|
6257
6312
|
try {
|
|
@@ -6273,7 +6328,7 @@ function useBuildingUnitController() {
|
|
|
6273
6328
|
});
|
|
6274
6329
|
const { error } = validation.validate({ id, value: data });
|
|
6275
6330
|
if (error) {
|
|
6276
|
-
next(new
|
|
6331
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6277
6332
|
return;
|
|
6278
6333
|
}
|
|
6279
6334
|
try {
|
|
@@ -6298,7 +6353,7 @@ function useBuildingUnitController() {
|
|
|
6298
6353
|
});
|
|
6299
6354
|
const { error } = validation.validate(query);
|
|
6300
6355
|
if (error) {
|
|
6301
|
-
next(new
|
|
6356
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6302
6357
|
return;
|
|
6303
6358
|
}
|
|
6304
6359
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -6339,7 +6394,7 @@ function useBuildingUnitController() {
|
|
|
6339
6394
|
});
|
|
6340
6395
|
const { error } = validation.validate({ id });
|
|
6341
6396
|
if (error) {
|
|
6342
|
-
next(new
|
|
6397
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6343
6398
|
return;
|
|
6344
6399
|
}
|
|
6345
6400
|
try {
|
|
@@ -6360,7 +6415,7 @@ function useBuildingUnitController() {
|
|
|
6360
6415
|
});
|
|
6361
6416
|
const { error } = validation.validate({ id });
|
|
6362
6417
|
if (error) {
|
|
6363
|
-
next(new
|
|
6418
|
+
next(new import_utils33.BadRequestError(error.message));
|
|
6364
6419
|
return;
|
|
6365
6420
|
}
|
|
6366
6421
|
try {
|
|
@@ -6381,7 +6436,7 @@ function useBuildingUnitController() {
|
|
|
6381
6436
|
}
|
|
6382
6437
|
|
|
6383
6438
|
// src/resources/counter/counter.model.ts
|
|
6384
|
-
var
|
|
6439
|
+
var import_utils34 = require("@goweekdays/utils");
|
|
6385
6440
|
var import_mongodb19 = require("mongodb");
|
|
6386
6441
|
var import_zod = require("zod");
|
|
6387
6442
|
var TCounter = import_zod.z.object({
|
|
@@ -6401,7 +6456,7 @@ function useCounterModel(db) {
|
|
|
6401
6456
|
try {
|
|
6402
6457
|
return TCounter.parse(value);
|
|
6403
6458
|
} catch (error) {
|
|
6404
|
-
throw new
|
|
6459
|
+
throw new import_utils34.BadRequestError(error.issues[0].message);
|
|
6405
6460
|
}
|
|
6406
6461
|
}
|
|
6407
6462
|
function validateCounter(data) {
|
|
@@ -6415,23 +6470,23 @@ function useCounterModel(db) {
|
|
|
6415
6470
|
}
|
|
6416
6471
|
|
|
6417
6472
|
// src/resources/counter/counter.repository.ts
|
|
6418
|
-
var
|
|
6473
|
+
var import_utils35 = require("@goweekdays/utils");
|
|
6419
6474
|
function useCounterRepo() {
|
|
6420
|
-
const db =
|
|
6475
|
+
const db = import_utils35.useAtlas.getDb();
|
|
6421
6476
|
if (!db) {
|
|
6422
6477
|
throw new Error("Unable to connect to server.");
|
|
6423
6478
|
}
|
|
6424
6479
|
const namespace_collection = "counters";
|
|
6425
6480
|
const { collection, createCounter } = useCounterModel(db);
|
|
6426
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
6481
|
+
const { getCache, setCache, delNamespace } = (0, import_utils35.useCache)(namespace_collection);
|
|
6427
6482
|
function delCachedData() {
|
|
6428
6483
|
delNamespace().then(() => {
|
|
6429
|
-
|
|
6484
|
+
import_utils35.logger.log({
|
|
6430
6485
|
level: "info",
|
|
6431
6486
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
6432
6487
|
});
|
|
6433
6488
|
}).catch((err) => {
|
|
6434
|
-
|
|
6489
|
+
import_utils35.logger.log({
|
|
6435
6490
|
level: "error",
|
|
6436
6491
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
6437
6492
|
});
|
|
@@ -6483,11 +6538,11 @@ function useCounterRepo() {
|
|
|
6483
6538
|
}
|
|
6484
6539
|
}
|
|
6485
6540
|
async function getByType(type) {
|
|
6486
|
-
const cacheKey = (0,
|
|
6541
|
+
const cacheKey = (0, import_utils35.makeCacheKey)(namespace_collection, { type });
|
|
6487
6542
|
try {
|
|
6488
6543
|
const cached = await getCache(cacheKey);
|
|
6489
6544
|
if (cached) {
|
|
6490
|
-
|
|
6545
|
+
import_utils35.logger.log({
|
|
6491
6546
|
level: "info",
|
|
6492
6547
|
message: `Cache hit for getByType counter: ${cacheKey}`
|
|
6493
6548
|
});
|
|
@@ -6496,12 +6551,12 @@ function useCounterRepo() {
|
|
|
6496
6551
|
const data = await collection.findOne({ type });
|
|
6497
6552
|
if (data) {
|
|
6498
6553
|
setCache(cacheKey, data, 300).then(() => {
|
|
6499
|
-
|
|
6554
|
+
import_utils35.logger.log({
|
|
6500
6555
|
level: "info",
|
|
6501
6556
|
message: `Cache set for counter by type: ${cacheKey}`
|
|
6502
6557
|
});
|
|
6503
6558
|
}).catch((err) => {
|
|
6504
|
-
|
|
6559
|
+
import_utils35.logger.log({
|
|
6505
6560
|
level: "error",
|
|
6506
6561
|
message: `Failed to set cache for counter by type: ${err.message}`
|
|
6507
6562
|
});
|
|
@@ -6522,7 +6577,7 @@ function useCounterRepo() {
|
|
|
6522
6577
|
}
|
|
6523
6578
|
|
|
6524
6579
|
// src/resources/file/file.service.ts
|
|
6525
|
-
var
|
|
6580
|
+
var import_utils36 = require("@goweekdays/utils");
|
|
6526
6581
|
var import_node_cron = __toESM(require("node-cron"));
|
|
6527
6582
|
var fs = __toESM(require("fs"));
|
|
6528
6583
|
function useFileService() {
|
|
@@ -6531,7 +6586,7 @@ function useFileService() {
|
|
|
6531
6586
|
deleteFileById,
|
|
6532
6587
|
getAllDraftedFiles
|
|
6533
6588
|
} = useFileRepo();
|
|
6534
|
-
const s3 = new
|
|
6589
|
+
const s3 = new import_utils36.useS3({
|
|
6535
6590
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
6536
6591
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
6537
6592
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -6540,7 +6595,7 @@ function useFileService() {
|
|
|
6540
6595
|
forcePathStyle: true
|
|
6541
6596
|
});
|
|
6542
6597
|
async function createFile(value) {
|
|
6543
|
-
const session =
|
|
6598
|
+
const session = import_utils36.useAtlas.getClient()?.startSession();
|
|
6544
6599
|
session?.startTransaction();
|
|
6545
6600
|
const file = {
|
|
6546
6601
|
name: value.originalname,
|
|
@@ -6574,7 +6629,7 @@ function useFileService() {
|
|
|
6574
6629
|
}
|
|
6575
6630
|
}
|
|
6576
6631
|
async function deleteFile(id) {
|
|
6577
|
-
const session =
|
|
6632
|
+
const session = import_utils36.useAtlas.getClient()?.startSession();
|
|
6578
6633
|
session?.startTransaction();
|
|
6579
6634
|
try {
|
|
6580
6635
|
await deleteFileById(id, session);
|
|
@@ -6595,12 +6650,12 @@ function useFileService() {
|
|
|
6595
6650
|
const file = files[index];
|
|
6596
6651
|
try {
|
|
6597
6652
|
await deleteFile(file._id.toString());
|
|
6598
|
-
await
|
|
6653
|
+
await import_utils36.logger.log({
|
|
6599
6654
|
level: "info",
|
|
6600
6655
|
message: "Successfully deleted draft files."
|
|
6601
6656
|
});
|
|
6602
6657
|
} catch (error) {
|
|
6603
|
-
|
|
6658
|
+
import_utils36.logger.log({
|
|
6604
6659
|
level: "info",
|
|
6605
6660
|
message: "Successfully deleted draft files."
|
|
6606
6661
|
});
|
|
@@ -6617,7 +6672,7 @@ function useFileService() {
|
|
|
6617
6672
|
}
|
|
6618
6673
|
|
|
6619
6674
|
// src/resources/file/file.controller.ts
|
|
6620
|
-
var
|
|
6675
|
+
var import_utils37 = require("@goweekdays/utils");
|
|
6621
6676
|
var import_joi18 = __toESM(require("joi"));
|
|
6622
6677
|
function useFileController() {
|
|
6623
6678
|
const { createFile, deleteFile: _deleteFile } = useFileService();
|
|
@@ -6631,10 +6686,10 @@ function useFileController() {
|
|
|
6631
6686
|
res.json({ message: "Successfully uploaded file", id });
|
|
6632
6687
|
return;
|
|
6633
6688
|
} catch (error) {
|
|
6634
|
-
if (error instanceof
|
|
6689
|
+
if (error instanceof import_utils37.AppError) {
|
|
6635
6690
|
next(error);
|
|
6636
6691
|
} else {
|
|
6637
|
-
next(new
|
|
6692
|
+
next(new import_utils37.InternalServerError(error));
|
|
6638
6693
|
}
|
|
6639
6694
|
}
|
|
6640
6695
|
}
|
|
@@ -6643,17 +6698,17 @@ function useFileController() {
|
|
|
6643
6698
|
const validation = import_joi18.default.string().required();
|
|
6644
6699
|
const { error } = validation.validate(id);
|
|
6645
6700
|
if (error) {
|
|
6646
|
-
next(new
|
|
6701
|
+
next(new import_utils37.BadRequestError(error.message));
|
|
6647
6702
|
}
|
|
6648
6703
|
try {
|
|
6649
6704
|
const message = await _deleteFile(id);
|
|
6650
6705
|
res.json({ message });
|
|
6651
6706
|
return;
|
|
6652
6707
|
} catch (error2) {
|
|
6653
|
-
if (error2 instanceof
|
|
6708
|
+
if (error2 instanceof import_utils37.AppError) {
|
|
6654
6709
|
next(error2);
|
|
6655
6710
|
} else {
|
|
6656
|
-
next(new
|
|
6711
|
+
next(new import_utils37.InternalServerError(error2));
|
|
6657
6712
|
}
|
|
6658
6713
|
}
|
|
6659
6714
|
}
|
|
@@ -6665,7 +6720,7 @@ function useFileController() {
|
|
|
6665
6720
|
|
|
6666
6721
|
// src/resources/member/member.controller.ts
|
|
6667
6722
|
var import_joi19 = __toESM(require("joi"));
|
|
6668
|
-
var
|
|
6723
|
+
var import_utils38 = require("@goweekdays/utils");
|
|
6669
6724
|
function useMemberController() {
|
|
6670
6725
|
const {
|
|
6671
6726
|
getByUserId: _getByUserId,
|
|
@@ -6681,7 +6736,7 @@ function useMemberController() {
|
|
|
6681
6736
|
});
|
|
6682
6737
|
const { error } = validation.validate({ id: userId });
|
|
6683
6738
|
if (error) {
|
|
6684
|
-
next(new
|
|
6739
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6685
6740
|
return;
|
|
6686
6741
|
}
|
|
6687
6742
|
try {
|
|
@@ -6703,7 +6758,7 @@ function useMemberController() {
|
|
|
6703
6758
|
});
|
|
6704
6759
|
const { error } = validation.validate({ ...req.params, ...req.query });
|
|
6705
6760
|
if (error) {
|
|
6706
|
-
next(new
|
|
6761
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6707
6762
|
return;
|
|
6708
6763
|
}
|
|
6709
6764
|
const orgId = req.query.org;
|
|
@@ -6747,7 +6802,7 @@ function useMemberController() {
|
|
|
6747
6802
|
status
|
|
6748
6803
|
});
|
|
6749
6804
|
if (error) {
|
|
6750
|
-
next(new
|
|
6805
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6751
6806
|
return;
|
|
6752
6807
|
}
|
|
6753
6808
|
try {
|
|
@@ -6784,7 +6839,7 @@ function useMemberController() {
|
|
|
6784
6839
|
limit
|
|
6785
6840
|
});
|
|
6786
6841
|
if (error) {
|
|
6787
|
-
next(new
|
|
6842
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6788
6843
|
}
|
|
6789
6844
|
try {
|
|
6790
6845
|
const items = await _getOrgsByMembership({
|
|
@@ -6806,7 +6861,7 @@ function useMemberController() {
|
|
|
6806
6861
|
});
|
|
6807
6862
|
const { error } = validation.validate(req.params);
|
|
6808
6863
|
if (error) {
|
|
6809
|
-
next(new
|
|
6864
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6810
6865
|
return;
|
|
6811
6866
|
}
|
|
6812
6867
|
const id = req.params.id;
|
|
@@ -6828,42 +6883,32 @@ function useMemberController() {
|
|
|
6828
6883
|
}
|
|
6829
6884
|
|
|
6830
6885
|
// src/resources/organization/organization.model.ts
|
|
6831
|
-
var
|
|
6886
|
+
var import_utils39 = require("@goweekdays/utils");
|
|
6832
6887
|
var import_joi20 = __toESM(require("joi"));
|
|
6833
6888
|
var import_mongodb20 = require("mongodb");
|
|
6834
|
-
var OrgTypes = [
|
|
6835
|
-
"marketplace",
|
|
6836
|
-
"stay",
|
|
6837
|
-
"eat",
|
|
6838
|
-
"service",
|
|
6839
|
-
"ride",
|
|
6840
|
-
"experience"
|
|
6841
|
-
];
|
|
6842
6889
|
var schemaOrg = import_joi20.default.object({
|
|
6843
6890
|
name: import_joi20.default.string().max(255).required(),
|
|
6844
6891
|
description: import_joi20.default.string().max(1024).optional().allow("", null),
|
|
6845
|
-
type: import_joi20.default.string().allow(...OrgTypes).required(),
|
|
6846
6892
|
email: import_joi20.default.string().email().max(255).optional().allow("", null),
|
|
6847
6893
|
contact: import_joi20.default.string().max(50).optional().allow("", null),
|
|
6848
6894
|
createdBy: import_joi20.default.string().hex().required()
|
|
6849
6895
|
});
|
|
6850
|
-
function
|
|
6896
|
+
function modelOrg(value) {
|
|
6851
6897
|
const { error } = schemaOrg.validate(value);
|
|
6852
6898
|
if (error) {
|
|
6853
|
-
throw new
|
|
6899
|
+
throw new import_utils39.BadRequestError(error.message);
|
|
6854
6900
|
}
|
|
6855
6901
|
if (value.createdBy && typeof value.createdBy === "string") {
|
|
6856
6902
|
try {
|
|
6857
6903
|
value.createdBy = new import_mongodb20.ObjectId(value.createdBy);
|
|
6858
6904
|
} catch (error2) {
|
|
6859
|
-
throw new
|
|
6905
|
+
throw new import_utils39.BadRequestError("Invalid createdBy ObjectId");
|
|
6860
6906
|
}
|
|
6861
6907
|
}
|
|
6862
6908
|
return {
|
|
6863
6909
|
_id: value._id,
|
|
6864
6910
|
name: value.name,
|
|
6865
|
-
description: value.description,
|
|
6866
|
-
type: value.type,
|
|
6911
|
+
description: value.description ?? "",
|
|
6867
6912
|
email: value.email,
|
|
6868
6913
|
contact: value.contact,
|
|
6869
6914
|
createdBy: value.createdBy,
|
|
@@ -6875,16 +6920,16 @@ function MOrg(value) {
|
|
|
6875
6920
|
}
|
|
6876
6921
|
|
|
6877
6922
|
// src/resources/organization/organization.repository.ts
|
|
6878
|
-
var
|
|
6923
|
+
var import_utils40 = require("@goweekdays/utils");
|
|
6879
6924
|
var import_mongodb21 = require("mongodb");
|
|
6880
6925
|
function useOrgRepo() {
|
|
6881
|
-
const db =
|
|
6926
|
+
const db = import_utils40.useAtlas.getDb();
|
|
6882
6927
|
if (!db) {
|
|
6883
6928
|
throw new Error("Unable to connect to server.");
|
|
6884
6929
|
}
|
|
6885
6930
|
const namespace_collection = "organizations";
|
|
6886
6931
|
const collection = db.collection(namespace_collection);
|
|
6887
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
6932
|
+
const { getCache, setCache, delNamespace } = (0, import_utils40.useCache)(namespace_collection);
|
|
6888
6933
|
async function createIndexes() {
|
|
6889
6934
|
try {
|
|
6890
6935
|
await collection.createIndexes([
|
|
@@ -6900,16 +6945,16 @@ function useOrgRepo() {
|
|
|
6900
6945
|
}
|
|
6901
6946
|
}
|
|
6902
6947
|
createIndexes().catch((error) => {
|
|
6903
|
-
|
|
6948
|
+
import_utils40.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
6904
6949
|
});
|
|
6905
6950
|
function delCachedData() {
|
|
6906
6951
|
delNamespace().then(() => {
|
|
6907
|
-
|
|
6952
|
+
import_utils40.logger.log({
|
|
6908
6953
|
level: "info",
|
|
6909
6954
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
6910
6955
|
});
|
|
6911
6956
|
}).catch((err) => {
|
|
6912
|
-
|
|
6957
|
+
import_utils40.logger.log({
|
|
6913
6958
|
level: "error",
|
|
6914
6959
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
6915
6960
|
});
|
|
@@ -6917,21 +6962,21 @@ function useOrgRepo() {
|
|
|
6917
6962
|
}
|
|
6918
6963
|
async function add(value, session) {
|
|
6919
6964
|
try {
|
|
6920
|
-
value =
|
|
6965
|
+
value = modelOrg(value);
|
|
6921
6966
|
const res = await collection.insertOne(value, { session });
|
|
6922
6967
|
delCachedData();
|
|
6923
6968
|
return res.insertedId;
|
|
6924
6969
|
} catch (error) {
|
|
6925
|
-
|
|
6970
|
+
import_utils40.logger.log({
|
|
6926
6971
|
level: "error",
|
|
6927
6972
|
message: error.message
|
|
6928
6973
|
});
|
|
6929
|
-
if (error instanceof
|
|
6974
|
+
if (error instanceof import_utils40.AppError) {
|
|
6930
6975
|
throw error;
|
|
6931
6976
|
} else {
|
|
6932
6977
|
const isDuplicated = error.message.includes("duplicate");
|
|
6933
6978
|
if (isDuplicated) {
|
|
6934
|
-
throw new
|
|
6979
|
+
throw new import_utils40.BadRequestError("Organization already exist.");
|
|
6935
6980
|
}
|
|
6936
6981
|
throw new Error("Failed to create organization.");
|
|
6937
6982
|
}
|
|
@@ -6950,21 +6995,21 @@ function useOrgRepo() {
|
|
|
6950
6995
|
if (search) {
|
|
6951
6996
|
query.$text = { $search: search };
|
|
6952
6997
|
}
|
|
6953
|
-
const cacheKey = (0,
|
|
6998
|
+
const cacheKey = (0, import_utils40.makeCacheKey)(namespace_collection, {
|
|
6954
6999
|
search,
|
|
6955
7000
|
page,
|
|
6956
7001
|
limit,
|
|
6957
7002
|
sort: JSON.stringify(sort),
|
|
6958
7003
|
status
|
|
6959
7004
|
});
|
|
6960
|
-
|
|
7005
|
+
import_utils40.logger.log({
|
|
6961
7006
|
level: "info",
|
|
6962
7007
|
message: `Cache key for getAll organizations: ${cacheKey}`
|
|
6963
7008
|
});
|
|
6964
7009
|
try {
|
|
6965
7010
|
const cached = await getCache(cacheKey);
|
|
6966
7011
|
if (cached) {
|
|
6967
|
-
|
|
7012
|
+
import_utils40.logger.log({
|
|
6968
7013
|
level: "info",
|
|
6969
7014
|
message: `Cache hit for getAll organizations: ${cacheKey}`
|
|
6970
7015
|
});
|
|
@@ -6986,21 +7031,21 @@ function useOrgRepo() {
|
|
|
6986
7031
|
}
|
|
6987
7032
|
]).toArray();
|
|
6988
7033
|
const length = await collection.countDocuments(query);
|
|
6989
|
-
const data = (0,
|
|
7034
|
+
const data = (0, import_utils40.paginate)(items, page, limit, length);
|
|
6990
7035
|
setCache(cacheKey, data, 600).then(() => {
|
|
6991
|
-
|
|
7036
|
+
import_utils40.logger.log({
|
|
6992
7037
|
level: "info",
|
|
6993
7038
|
message: `Cache set for getAll organizations: ${cacheKey}`
|
|
6994
7039
|
});
|
|
6995
7040
|
}).catch((err) => {
|
|
6996
|
-
|
|
7041
|
+
import_utils40.logger.log({
|
|
6997
7042
|
level: "error",
|
|
6998
7043
|
message: `Failed to set cache for getAll organizations: ${err.message}`
|
|
6999
7044
|
});
|
|
7000
7045
|
});
|
|
7001
7046
|
return data;
|
|
7002
7047
|
} catch (error) {
|
|
7003
|
-
|
|
7048
|
+
import_utils40.logger.log({ level: "error", message: `${error}` });
|
|
7004
7049
|
throw error;
|
|
7005
7050
|
}
|
|
7006
7051
|
}
|
|
@@ -7008,13 +7053,13 @@ function useOrgRepo() {
|
|
|
7008
7053
|
try {
|
|
7009
7054
|
_id = new import_mongodb21.ObjectId(_id);
|
|
7010
7055
|
} catch (error) {
|
|
7011
|
-
throw new
|
|
7056
|
+
throw new import_utils40.BadRequestError("Invalid ID.");
|
|
7012
7057
|
}
|
|
7013
|
-
const cacheKey = (0,
|
|
7058
|
+
const cacheKey = (0, import_utils40.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
7014
7059
|
try {
|
|
7015
7060
|
const cached = await getCache(cacheKey);
|
|
7016
7061
|
if (cached) {
|
|
7017
|
-
|
|
7062
|
+
import_utils40.logger.log({
|
|
7018
7063
|
level: "info",
|
|
7019
7064
|
message: `Cache hit for getById organization: ${cacheKey}`
|
|
7020
7065
|
});
|
|
@@ -7022,34 +7067,34 @@ function useOrgRepo() {
|
|
|
7022
7067
|
}
|
|
7023
7068
|
const result = await collection.findOne({ _id });
|
|
7024
7069
|
if (!result) {
|
|
7025
|
-
throw new
|
|
7070
|
+
throw new import_utils40.BadRequestError("Organization not found.");
|
|
7026
7071
|
}
|
|
7027
7072
|
setCache(cacheKey, result, 300).then(() => {
|
|
7028
|
-
|
|
7073
|
+
import_utils40.logger.log({
|
|
7029
7074
|
level: "info",
|
|
7030
7075
|
message: `Cache set for organization by id: ${cacheKey}`
|
|
7031
7076
|
});
|
|
7032
7077
|
}).catch((err) => {
|
|
7033
|
-
|
|
7078
|
+
import_utils40.logger.log({
|
|
7034
7079
|
level: "error",
|
|
7035
7080
|
message: `Failed to set cache for organization by id: ${err.message}`
|
|
7036
7081
|
});
|
|
7037
7082
|
});
|
|
7038
7083
|
return result;
|
|
7039
7084
|
} catch (error) {
|
|
7040
|
-
if (error instanceof
|
|
7085
|
+
if (error instanceof import_utils40.AppError) {
|
|
7041
7086
|
throw error;
|
|
7042
7087
|
} else {
|
|
7043
|
-
throw new
|
|
7088
|
+
throw new import_utils40.InternalServerError("Failed to get organization.");
|
|
7044
7089
|
}
|
|
7045
7090
|
}
|
|
7046
7091
|
}
|
|
7047
7092
|
async function getByName(name) {
|
|
7048
|
-
const cacheKey = (0,
|
|
7093
|
+
const cacheKey = (0, import_utils40.makeCacheKey)(namespace_collection, { name });
|
|
7049
7094
|
try {
|
|
7050
7095
|
const cached = await getCache(cacheKey);
|
|
7051
7096
|
if (cached) {
|
|
7052
|
-
|
|
7097
|
+
import_utils40.logger.log({
|
|
7053
7098
|
level: "info",
|
|
7054
7099
|
message: `Cache hit for getByName organization: ${cacheKey}`
|
|
7055
7100
|
});
|
|
@@ -7057,39 +7102,39 @@ function useOrgRepo() {
|
|
|
7057
7102
|
}
|
|
7058
7103
|
const result = await collection.findOne({ name });
|
|
7059
7104
|
if (!result) {
|
|
7060
|
-
throw new
|
|
7105
|
+
throw new import_utils40.BadRequestError("Organization not found.");
|
|
7061
7106
|
}
|
|
7062
7107
|
setCache(cacheKey, result, 300).then(() => {
|
|
7063
|
-
|
|
7108
|
+
import_utils40.logger.log({
|
|
7064
7109
|
level: "info",
|
|
7065
7110
|
message: `Cache set for organization by name: ${cacheKey}`
|
|
7066
7111
|
});
|
|
7067
7112
|
}).catch((err) => {
|
|
7068
|
-
|
|
7113
|
+
import_utils40.logger.log({
|
|
7069
7114
|
level: "error",
|
|
7070
7115
|
message: `Failed to set cache for organization by name: ${err.message}`
|
|
7071
7116
|
});
|
|
7072
7117
|
});
|
|
7073
7118
|
return result;
|
|
7074
7119
|
} catch (error) {
|
|
7075
|
-
if (error instanceof
|
|
7120
|
+
if (error instanceof import_utils40.AppError) {
|
|
7076
7121
|
throw error;
|
|
7077
7122
|
} else {
|
|
7078
|
-
throw new
|
|
7123
|
+
throw new import_utils40.InternalServerError("Failed to get organization.");
|
|
7079
7124
|
}
|
|
7080
7125
|
}
|
|
7081
7126
|
}
|
|
7082
7127
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
7083
7128
|
const allowedFields = ["name", "description"];
|
|
7084
7129
|
if (!allowedFields.includes(field)) {
|
|
7085
|
-
throw new
|
|
7130
|
+
throw new import_utils40.BadRequestError(
|
|
7086
7131
|
`Field "${field}" is not allowed to be updated.`
|
|
7087
7132
|
);
|
|
7088
7133
|
}
|
|
7089
7134
|
try {
|
|
7090
7135
|
_id = new import_mongodb21.ObjectId(_id);
|
|
7091
7136
|
} catch (error) {
|
|
7092
|
-
throw new
|
|
7137
|
+
throw new import_utils40.BadRequestError("Invalid ID.");
|
|
7093
7138
|
}
|
|
7094
7139
|
try {
|
|
7095
7140
|
await collection.updateOne(
|
|
@@ -7101,14 +7146,14 @@ function useOrgRepo() {
|
|
|
7101
7146
|
delCachedData();
|
|
7102
7147
|
return `Successfully updated user ${field}.`;
|
|
7103
7148
|
} catch (error) {
|
|
7104
|
-
throw new
|
|
7149
|
+
throw new import_utils40.InternalServerError(`Failed to update organization ${field}.`);
|
|
7105
7150
|
}
|
|
7106
7151
|
}
|
|
7107
7152
|
async function deleteById(_id) {
|
|
7108
7153
|
try {
|
|
7109
7154
|
_id = new import_mongodb21.ObjectId(_id);
|
|
7110
7155
|
} catch (error) {
|
|
7111
|
-
throw new
|
|
7156
|
+
throw new import_utils40.BadRequestError("Invalid ID.");
|
|
7112
7157
|
}
|
|
7113
7158
|
try {
|
|
7114
7159
|
await collection.updateOne(
|
|
@@ -7118,7 +7163,7 @@ function useOrgRepo() {
|
|
|
7118
7163
|
delCachedData();
|
|
7119
7164
|
return "Successfully deleted organization.";
|
|
7120
7165
|
} catch (error) {
|
|
7121
|
-
throw new
|
|
7166
|
+
throw new import_utils40.InternalServerError("Failed to delete organization.");
|
|
7122
7167
|
}
|
|
7123
7168
|
}
|
|
7124
7169
|
return {
|
|
@@ -7133,10 +7178,10 @@ function useOrgRepo() {
|
|
|
7133
7178
|
}
|
|
7134
7179
|
|
|
7135
7180
|
// src/resources/organization/organization.service.ts
|
|
7136
|
-
var
|
|
7181
|
+
var import_utils42 = require("@goweekdays/utils");
|
|
7137
7182
|
|
|
7138
7183
|
// src/resources/user/user.controller.ts
|
|
7139
|
-
var
|
|
7184
|
+
var import_utils41 = require("@goweekdays/utils");
|
|
7140
7185
|
var import_joi21 = __toESM(require("joi"));
|
|
7141
7186
|
function useUserController() {
|
|
7142
7187
|
const {
|
|
@@ -7160,7 +7205,7 @@ function useUserController() {
|
|
|
7160
7205
|
});
|
|
7161
7206
|
const { error } = validation.validate({ status, search, page });
|
|
7162
7207
|
if (error) {
|
|
7163
|
-
next(new
|
|
7208
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7164
7209
|
return;
|
|
7165
7210
|
}
|
|
7166
7211
|
try {
|
|
@@ -7175,12 +7220,12 @@ function useUserController() {
|
|
|
7175
7220
|
const id = req.params.id || "";
|
|
7176
7221
|
const validation = import_joi21.default.string().hex().validate(id);
|
|
7177
7222
|
if (validation.error) {
|
|
7178
|
-
throw new
|
|
7223
|
+
throw new import_utils41.BadRequestError("Invalid id.");
|
|
7179
7224
|
}
|
|
7180
7225
|
try {
|
|
7181
7226
|
const user = await _getUserById(id);
|
|
7182
7227
|
if (!user) {
|
|
7183
|
-
throw new
|
|
7228
|
+
throw new import_utils41.BadRequestError("User not found.");
|
|
7184
7229
|
}
|
|
7185
7230
|
res.json(user);
|
|
7186
7231
|
} catch (error) {
|
|
@@ -7197,7 +7242,7 @@ function useUserController() {
|
|
|
7197
7242
|
});
|
|
7198
7243
|
const { error } = validation.validate({ firstName, lastName });
|
|
7199
7244
|
if (error) {
|
|
7200
|
-
next(new
|
|
7245
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7201
7246
|
return;
|
|
7202
7247
|
}
|
|
7203
7248
|
try {
|
|
@@ -7220,7 +7265,7 @@ function useUserController() {
|
|
|
7220
7265
|
});
|
|
7221
7266
|
const { error } = validation.validate({ month, day, year });
|
|
7222
7267
|
if (error) {
|
|
7223
|
-
next(new
|
|
7268
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7224
7269
|
return;
|
|
7225
7270
|
}
|
|
7226
7271
|
try {
|
|
@@ -7245,7 +7290,7 @@ function useUserController() {
|
|
|
7245
7290
|
});
|
|
7246
7291
|
const { error } = validation.validate({ _id, field, value });
|
|
7247
7292
|
if (error) {
|
|
7248
|
-
next(new
|
|
7293
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7249
7294
|
return;
|
|
7250
7295
|
}
|
|
7251
7296
|
try {
|
|
@@ -7266,7 +7311,7 @@ function useUserController() {
|
|
|
7266
7311
|
});
|
|
7267
7312
|
const { error } = validation.validate({ previousProfile });
|
|
7268
7313
|
if (error) {
|
|
7269
|
-
next(new
|
|
7314
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7270
7315
|
return;
|
|
7271
7316
|
}
|
|
7272
7317
|
const user = req.headers["user"] ?? "";
|
|
@@ -7279,10 +7324,10 @@ function useUserController() {
|
|
|
7279
7324
|
res.json({ message: "Successfully updated profile picture." });
|
|
7280
7325
|
return;
|
|
7281
7326
|
} catch (error2) {
|
|
7282
|
-
if (error2 instanceof
|
|
7327
|
+
if (error2 instanceof import_utils41.AppError) {
|
|
7283
7328
|
next(error2);
|
|
7284
7329
|
} else {
|
|
7285
|
-
next(new
|
|
7330
|
+
next(new import_utils41.InternalServerError(error2));
|
|
7286
7331
|
}
|
|
7287
7332
|
}
|
|
7288
7333
|
}
|
|
@@ -7307,7 +7352,7 @@ function useUserController() {
|
|
|
7307
7352
|
type
|
|
7308
7353
|
});
|
|
7309
7354
|
if (error) {
|
|
7310
|
-
next(new
|
|
7355
|
+
next(new import_utils41.BadRequestError(error.message));
|
|
7311
7356
|
return;
|
|
7312
7357
|
}
|
|
7313
7358
|
try {
|
|
@@ -7347,9 +7392,12 @@ function useOrgService() {
|
|
|
7347
7392
|
const { add: addMember } = useMemberRepo();
|
|
7348
7393
|
const { getUserById } = useUserRepo();
|
|
7349
7394
|
async function add(value) {
|
|
7350
|
-
const session =
|
|
7351
|
-
session
|
|
7395
|
+
const session = import_utils42.useAtlas.getClient()?.startSession();
|
|
7396
|
+
if (!session) {
|
|
7397
|
+
throw new import_utils42.BadRequestError("Unable to start database session.");
|
|
7398
|
+
}
|
|
7352
7399
|
try {
|
|
7400
|
+
session?.startTransaction();
|
|
7353
7401
|
const org = await addOrg(value, session);
|
|
7354
7402
|
const allPermissions = await getAllPermission({
|
|
7355
7403
|
app: "org",
|
|
@@ -7362,39 +7410,41 @@ function useOrgService() {
|
|
|
7362
7410
|
if (permissions.length === 0) {
|
|
7363
7411
|
throw new Error("No permissions found for the organization type.");
|
|
7364
7412
|
}
|
|
7413
|
+
const createdBy = String(value.createdBy);
|
|
7365
7414
|
const role = await addRole(
|
|
7366
7415
|
{
|
|
7367
|
-
|
|
7416
|
+
org: String(org),
|
|
7368
7417
|
name: "Owner",
|
|
7369
7418
|
description: "Owner of the organization",
|
|
7370
|
-
permissions
|
|
7419
|
+
permissions,
|
|
7420
|
+
createdBy
|
|
7371
7421
|
},
|
|
7372
7422
|
session
|
|
7373
7423
|
);
|
|
7374
7424
|
if (!role) {
|
|
7375
|
-
throw new
|
|
7425
|
+
throw new import_utils42.BadRequestError("Role is required to create org member.");
|
|
7376
7426
|
}
|
|
7377
|
-
const user = await getUserById(
|
|
7427
|
+
const user = await getUserById(createdBy);
|
|
7378
7428
|
if (!user) {
|
|
7379
|
-
throw new
|
|
7429
|
+
throw new import_utils42.BadRequestError("User is required to create org member.");
|
|
7380
7430
|
}
|
|
7381
7431
|
await addMember(
|
|
7382
7432
|
{
|
|
7383
7433
|
role: String(role),
|
|
7384
7434
|
org: String(org),
|
|
7385
7435
|
name: `${user.firstName} ${user.lastName}`,
|
|
7386
|
-
user:
|
|
7387
|
-
type: "
|
|
7436
|
+
user: createdBy,
|
|
7437
|
+
type: "org"
|
|
7388
7438
|
},
|
|
7389
7439
|
session
|
|
7390
7440
|
);
|
|
7391
|
-
session?.commitTransaction();
|
|
7392
|
-
return
|
|
7441
|
+
await session?.commitTransaction();
|
|
7442
|
+
return String(org);
|
|
7393
7443
|
} catch (error) {
|
|
7394
|
-
session?.abortTransaction();
|
|
7444
|
+
await session?.abortTransaction();
|
|
7395
7445
|
throw error;
|
|
7396
7446
|
} finally {
|
|
7397
|
-
session?.endSession();
|
|
7447
|
+
await session?.endSession();
|
|
7398
7448
|
}
|
|
7399
7449
|
}
|
|
7400
7450
|
return {
|
|
@@ -7403,7 +7453,7 @@ function useOrgService() {
|
|
|
7403
7453
|
}
|
|
7404
7454
|
|
|
7405
7455
|
// src/resources/organization/organization.controller.ts
|
|
7406
|
-
var
|
|
7456
|
+
var import_utils43 = require("@goweekdays/utils");
|
|
7407
7457
|
var import_joi22 = __toESM(require("joi"));
|
|
7408
7458
|
function useOrgController() {
|
|
7409
7459
|
const { add: _add } = useOrgService();
|
|
@@ -7413,16 +7463,19 @@ function useOrgController() {
|
|
|
7413
7463
|
getAll: getAllOrg,
|
|
7414
7464
|
getById: _getById
|
|
7415
7465
|
} = useOrgRepo();
|
|
7416
|
-
async function
|
|
7466
|
+
async function add(req, res, next) {
|
|
7417
7467
|
const value = req.body;
|
|
7418
7468
|
const { error } = schemaOrg.validate(value);
|
|
7419
7469
|
if (error) {
|
|
7420
|
-
next(new
|
|
7470
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7421
7471
|
return;
|
|
7422
7472
|
}
|
|
7423
7473
|
try {
|
|
7424
|
-
const
|
|
7425
|
-
res.json({
|
|
7474
|
+
const org = await _add(value);
|
|
7475
|
+
res.json({
|
|
7476
|
+
message: "Organization created successfully.",
|
|
7477
|
+
data: { org }
|
|
7478
|
+
});
|
|
7426
7479
|
return;
|
|
7427
7480
|
} catch (error2) {
|
|
7428
7481
|
next(error2);
|
|
@@ -7435,12 +7488,12 @@ function useOrgController() {
|
|
|
7435
7488
|
const user = req.params.user ?? "";
|
|
7436
7489
|
const isPageNumber = isFinite(page);
|
|
7437
7490
|
if (!isPageNumber) {
|
|
7438
|
-
next(new
|
|
7491
|
+
next(new import_utils43.BadRequestError("Invalid page number."));
|
|
7439
7492
|
return;
|
|
7440
7493
|
}
|
|
7441
7494
|
const isLimitNumber = isFinite(limit);
|
|
7442
7495
|
if (!isLimitNumber) {
|
|
7443
|
-
next(new
|
|
7496
|
+
next(new import_utils43.BadRequestError("Invalid limit number."));
|
|
7444
7497
|
return;
|
|
7445
7498
|
}
|
|
7446
7499
|
const validation = import_joi22.default.object({
|
|
@@ -7451,7 +7504,7 @@ function useOrgController() {
|
|
|
7451
7504
|
});
|
|
7452
7505
|
const { error } = validation.validate({ user, page, limit, search });
|
|
7453
7506
|
if (error) {
|
|
7454
|
-
next(new
|
|
7507
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7455
7508
|
return;
|
|
7456
7509
|
}
|
|
7457
7510
|
try {
|
|
@@ -7477,16 +7530,16 @@ function useOrgController() {
|
|
|
7477
7530
|
const status = req.query.status ?? "active";
|
|
7478
7531
|
const isPageNumber = isFinite(page);
|
|
7479
7532
|
if (!isPageNumber) {
|
|
7480
|
-
next(new
|
|
7533
|
+
next(new import_utils43.BadRequestError("Invalid page number."));
|
|
7481
7534
|
return;
|
|
7482
7535
|
}
|
|
7483
7536
|
const isLimitNumber = isFinite(limit);
|
|
7484
7537
|
if (!isLimitNumber) {
|
|
7485
|
-
next(new
|
|
7538
|
+
next(new import_utils43.BadRequestError("Invalid limit number."));
|
|
7486
7539
|
return;
|
|
7487
7540
|
}
|
|
7488
7541
|
if (error) {
|
|
7489
|
-
next(new
|
|
7542
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7490
7543
|
return;
|
|
7491
7544
|
}
|
|
7492
7545
|
try {
|
|
@@ -7504,7 +7557,7 @@ function useOrgController() {
|
|
|
7504
7557
|
});
|
|
7505
7558
|
const { error } = validation.validate({ name });
|
|
7506
7559
|
if (error) {
|
|
7507
|
-
next(new
|
|
7560
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7508
7561
|
return;
|
|
7509
7562
|
}
|
|
7510
7563
|
try {
|
|
@@ -7522,7 +7575,7 @@ function useOrgController() {
|
|
|
7522
7575
|
});
|
|
7523
7576
|
const { error } = validation.validate({ id });
|
|
7524
7577
|
if (error) {
|
|
7525
|
-
next(new
|
|
7578
|
+
next(new import_utils43.BadRequestError(error.message));
|
|
7526
7579
|
return;
|
|
7527
7580
|
}
|
|
7528
7581
|
try {
|
|
@@ -7534,7 +7587,7 @@ function useOrgController() {
|
|
|
7534
7587
|
}
|
|
7535
7588
|
}
|
|
7536
7589
|
return {
|
|
7537
|
-
|
|
7590
|
+
add,
|
|
7538
7591
|
getOrgsByUserId,
|
|
7539
7592
|
getByName,
|
|
7540
7593
|
getAll,
|
|
@@ -7562,16 +7615,16 @@ function modelPSGC(data) {
|
|
|
7562
7615
|
}
|
|
7563
7616
|
|
|
7564
7617
|
// src/resources/psgc/psgc.repository.ts
|
|
7565
|
-
var
|
|
7618
|
+
var import_utils44 = require("@goweekdays/utils");
|
|
7566
7619
|
var import_mongodb22 = require("mongodb");
|
|
7567
7620
|
function usePSGCRepo() {
|
|
7568
|
-
const db =
|
|
7621
|
+
const db = import_utils44.useAtlas.getDb();
|
|
7569
7622
|
if (!db) {
|
|
7570
7623
|
throw new Error("Unable to connect to server.");
|
|
7571
7624
|
}
|
|
7572
7625
|
const namespace_collection = "psgc";
|
|
7573
7626
|
const collection = db.collection(namespace_collection);
|
|
7574
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
7627
|
+
const { getCache, setCache, delNamespace } = (0, import_utils44.useCache)(namespace_collection);
|
|
7575
7628
|
async function createIndexes() {
|
|
7576
7629
|
try {
|
|
7577
7630
|
await collection.createIndexes([
|
|
@@ -7585,12 +7638,12 @@ function usePSGCRepo() {
|
|
|
7585
7638
|
}
|
|
7586
7639
|
function delCachedData() {
|
|
7587
7640
|
delNamespace().then(() => {
|
|
7588
|
-
|
|
7641
|
+
import_utils44.logger.log({
|
|
7589
7642
|
level: "info",
|
|
7590
7643
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
7591
7644
|
});
|
|
7592
7645
|
}).catch((err) => {
|
|
7593
|
-
|
|
7646
|
+
import_utils44.logger.log({
|
|
7594
7647
|
level: "error",
|
|
7595
7648
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
7596
7649
|
});
|
|
@@ -7603,16 +7656,16 @@ function usePSGCRepo() {
|
|
|
7603
7656
|
delCachedData();
|
|
7604
7657
|
return res.insertedId;
|
|
7605
7658
|
} catch (error) {
|
|
7606
|
-
|
|
7659
|
+
import_utils44.logger.log({
|
|
7607
7660
|
level: "error",
|
|
7608
7661
|
message: error.message
|
|
7609
7662
|
});
|
|
7610
|
-
if (error instanceof
|
|
7663
|
+
if (error instanceof import_utils44.AppError) {
|
|
7611
7664
|
throw error;
|
|
7612
7665
|
} else {
|
|
7613
7666
|
const isDuplicated = error.message.includes("duplicate");
|
|
7614
7667
|
if (isDuplicated) {
|
|
7615
|
-
throw new
|
|
7668
|
+
throw new import_utils44.BadRequestError("Region already exists.");
|
|
7616
7669
|
}
|
|
7617
7670
|
throw new Error("Failed to create PSGC.");
|
|
7618
7671
|
}
|
|
@@ -7648,15 +7701,15 @@ function usePSGCRepo() {
|
|
|
7648
7701
|
query.$text = { $search: search };
|
|
7649
7702
|
cacheKeyOptions.search = search;
|
|
7650
7703
|
}
|
|
7651
|
-
const cacheKey = (0,
|
|
7652
|
-
|
|
7704
|
+
const cacheKey = (0, import_utils44.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
7705
|
+
import_utils44.logger.log({
|
|
7653
7706
|
level: "info",
|
|
7654
7707
|
message: `Cache key for getAll PSGC: ${cacheKey}`
|
|
7655
7708
|
});
|
|
7656
7709
|
try {
|
|
7657
7710
|
const cached = await getCache(cacheKey);
|
|
7658
7711
|
if (cached) {
|
|
7659
|
-
|
|
7712
|
+
import_utils44.logger.log({
|
|
7660
7713
|
level: "info",
|
|
7661
7714
|
message: `Cache hit for getAll PSGC: ${cacheKey}`
|
|
7662
7715
|
});
|
|
@@ -7669,21 +7722,21 @@ function usePSGCRepo() {
|
|
|
7669
7722
|
{ $limit: limit }
|
|
7670
7723
|
]).toArray();
|
|
7671
7724
|
const length = await collection.countDocuments(query);
|
|
7672
|
-
const data = (0,
|
|
7725
|
+
const data = (0, import_utils44.paginate)(items, page, limit, length);
|
|
7673
7726
|
setCache(cacheKey, data, 600).then(() => {
|
|
7674
|
-
|
|
7727
|
+
import_utils44.logger.log({
|
|
7675
7728
|
level: "info",
|
|
7676
7729
|
message: `Cache set for getAll PSGC: ${cacheKey}`
|
|
7677
7730
|
});
|
|
7678
7731
|
}).catch((err) => {
|
|
7679
|
-
|
|
7732
|
+
import_utils44.logger.log({
|
|
7680
7733
|
level: "error",
|
|
7681
7734
|
message: `Failed to set cache for getAll PSGC: ${err.message}`
|
|
7682
7735
|
});
|
|
7683
7736
|
});
|
|
7684
7737
|
return data;
|
|
7685
7738
|
} catch (error) {
|
|
7686
|
-
|
|
7739
|
+
import_utils44.logger.log({ level: "error", message: `${error}` });
|
|
7687
7740
|
throw error;
|
|
7688
7741
|
}
|
|
7689
7742
|
}
|
|
@@ -7691,13 +7744,13 @@ function usePSGCRepo() {
|
|
|
7691
7744
|
try {
|
|
7692
7745
|
_id = new import_mongodb22.ObjectId(_id);
|
|
7693
7746
|
} catch (error) {
|
|
7694
|
-
throw new
|
|
7747
|
+
throw new import_utils44.BadRequestError("Invalid ID.");
|
|
7695
7748
|
}
|
|
7696
|
-
const cacheKey = (0,
|
|
7749
|
+
const cacheKey = (0, import_utils44.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
7697
7750
|
try {
|
|
7698
7751
|
const cached = await getCache(cacheKey);
|
|
7699
7752
|
if (cached) {
|
|
7700
|
-
|
|
7753
|
+
import_utils44.logger.log({
|
|
7701
7754
|
level: "info",
|
|
7702
7755
|
message: `Cache hit for getById PSGC: ${cacheKey}`
|
|
7703
7756
|
});
|
|
@@ -7708,25 +7761,25 @@ function usePSGCRepo() {
|
|
|
7708
7761
|
deletedAt: { $in: ["", null] }
|
|
7709
7762
|
});
|
|
7710
7763
|
if (!result) {
|
|
7711
|
-
throw new
|
|
7764
|
+
throw new import_utils44.BadRequestError("Region not found.");
|
|
7712
7765
|
}
|
|
7713
7766
|
setCache(cacheKey, result, 300).then(() => {
|
|
7714
|
-
|
|
7767
|
+
import_utils44.logger.log({
|
|
7715
7768
|
level: "info",
|
|
7716
7769
|
message: `Cache set for PSGC by id: ${cacheKey}`
|
|
7717
7770
|
});
|
|
7718
7771
|
}).catch((err) => {
|
|
7719
|
-
|
|
7772
|
+
import_utils44.logger.log({
|
|
7720
7773
|
level: "error",
|
|
7721
7774
|
message: `Failed to set cache for PSGC by id: ${err.message}`
|
|
7722
7775
|
});
|
|
7723
7776
|
});
|
|
7724
7777
|
return result;
|
|
7725
7778
|
} catch (error) {
|
|
7726
|
-
if (error instanceof
|
|
7779
|
+
if (error instanceof import_utils44.AppError) {
|
|
7727
7780
|
throw error;
|
|
7728
7781
|
} else {
|
|
7729
|
-
throw new
|
|
7782
|
+
throw new import_utils44.InternalServerError("Failed to get PSGC.");
|
|
7730
7783
|
}
|
|
7731
7784
|
}
|
|
7732
7785
|
}
|
|
@@ -7750,15 +7803,15 @@ function usePSGCRepo() {
|
|
|
7750
7803
|
query.code = { $regex: `^${prefix}` };
|
|
7751
7804
|
cacheKeyOptions.prefix = prefix;
|
|
7752
7805
|
}
|
|
7753
|
-
const cacheKey = (0,
|
|
7754
|
-
|
|
7806
|
+
const cacheKey = (0, import_utils44.makeCacheKey)(namespace_collection, { name });
|
|
7807
|
+
import_utils44.logger.log({
|
|
7755
7808
|
level: "info",
|
|
7756
7809
|
message: `Query for getByName PSGC: ${JSON.stringify(query)}`
|
|
7757
7810
|
});
|
|
7758
7811
|
try {
|
|
7759
7812
|
const cached = await getCache(cacheKey);
|
|
7760
7813
|
if (cached) {
|
|
7761
|
-
|
|
7814
|
+
import_utils44.logger.log({
|
|
7762
7815
|
level: "info",
|
|
7763
7816
|
message: `Cache hit for getByName PSGC: ${cacheKey}`
|
|
7764
7817
|
});
|
|
@@ -7766,36 +7819,36 @@ function usePSGCRepo() {
|
|
|
7766
7819
|
}
|
|
7767
7820
|
const result = await collection.findOne(query);
|
|
7768
7821
|
setCache(cacheKey, result, 300).then(() => {
|
|
7769
|
-
|
|
7822
|
+
import_utils44.logger.log({
|
|
7770
7823
|
level: "info",
|
|
7771
7824
|
message: `Cache set for PSGC by name: ${cacheKey}`
|
|
7772
7825
|
});
|
|
7773
7826
|
}).catch((err) => {
|
|
7774
|
-
|
|
7827
|
+
import_utils44.logger.log({
|
|
7775
7828
|
level: "error",
|
|
7776
7829
|
message: `Failed to set cache for PSGC by name: ${err.message}`
|
|
7777
7830
|
});
|
|
7778
7831
|
});
|
|
7779
7832
|
return result;
|
|
7780
7833
|
} catch (error) {
|
|
7781
|
-
if (error instanceof
|
|
7834
|
+
if (error instanceof import_utils44.AppError) {
|
|
7782
7835
|
throw error;
|
|
7783
7836
|
} else {
|
|
7784
|
-
throw new
|
|
7837
|
+
throw new import_utils44.InternalServerError("Failed to get PSGC.");
|
|
7785
7838
|
}
|
|
7786
7839
|
}
|
|
7787
7840
|
}
|
|
7788
7841
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
7789
7842
|
const allowedFields = ["name"];
|
|
7790
7843
|
if (!allowedFields.includes(field)) {
|
|
7791
|
-
throw new
|
|
7844
|
+
throw new import_utils44.BadRequestError(
|
|
7792
7845
|
`Field "${field}" is not allowed to be updated.`
|
|
7793
7846
|
);
|
|
7794
7847
|
}
|
|
7795
7848
|
try {
|
|
7796
7849
|
_id = new import_mongodb22.ObjectId(_id);
|
|
7797
7850
|
} catch (error) {
|
|
7798
|
-
throw new
|
|
7851
|
+
throw new import_utils44.BadRequestError("Invalid ID.");
|
|
7799
7852
|
}
|
|
7800
7853
|
try {
|
|
7801
7854
|
await collection.updateOne(
|
|
@@ -7806,14 +7859,14 @@ function usePSGCRepo() {
|
|
|
7806
7859
|
delCachedData();
|
|
7807
7860
|
return `Successfully updated PSGC ${field}.`;
|
|
7808
7861
|
} catch (error) {
|
|
7809
|
-
throw new
|
|
7862
|
+
throw new import_utils44.InternalServerError(`Failed to update PSGC ${field}.`);
|
|
7810
7863
|
}
|
|
7811
7864
|
}
|
|
7812
7865
|
async function deleteById(_id) {
|
|
7813
7866
|
try {
|
|
7814
7867
|
_id = new import_mongodb22.ObjectId(_id);
|
|
7815
7868
|
} catch (error) {
|
|
7816
|
-
throw new
|
|
7869
|
+
throw new import_utils44.BadRequestError("Invalid ID.");
|
|
7817
7870
|
}
|
|
7818
7871
|
try {
|
|
7819
7872
|
await collection.updateOne(
|
|
@@ -7823,7 +7876,7 @@ function usePSGCRepo() {
|
|
|
7823
7876
|
delCachedData();
|
|
7824
7877
|
return "Successfully deleted PSGC.";
|
|
7825
7878
|
} catch (error) {
|
|
7826
|
-
throw new
|
|
7879
|
+
throw new import_utils44.InternalServerError("Failed to delete PSGC.");
|
|
7827
7880
|
}
|
|
7828
7881
|
}
|
|
7829
7882
|
return {
|
|
@@ -7838,7 +7891,7 @@ function usePSGCRepo() {
|
|
|
7838
7891
|
}
|
|
7839
7892
|
|
|
7840
7893
|
// src/resources/psgc/psgc.controller.ts
|
|
7841
|
-
var
|
|
7894
|
+
var import_utils45 = require("@goweekdays/utils");
|
|
7842
7895
|
var import_joi24 = __toESM(require("joi"));
|
|
7843
7896
|
function usePSGCController() {
|
|
7844
7897
|
const {
|
|
@@ -7853,7 +7906,7 @@ function usePSGCController() {
|
|
|
7853
7906
|
const value = req.body;
|
|
7854
7907
|
const { error } = schemaPSGC.validate(value);
|
|
7855
7908
|
if (error) {
|
|
7856
|
-
next(new
|
|
7909
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7857
7910
|
return;
|
|
7858
7911
|
}
|
|
7859
7912
|
try {
|
|
@@ -7884,16 +7937,16 @@ function usePSGCController() {
|
|
|
7884
7937
|
const prefix = req.query.prefix ? String(req.query.prefix) : "";
|
|
7885
7938
|
const isPageNumber = isFinite(page);
|
|
7886
7939
|
if (!isPageNumber) {
|
|
7887
|
-
next(new
|
|
7940
|
+
next(new import_utils45.BadRequestError("Invalid page number."));
|
|
7888
7941
|
return;
|
|
7889
7942
|
}
|
|
7890
7943
|
const isLimitNumber = isFinite(limit);
|
|
7891
7944
|
if (!isLimitNumber) {
|
|
7892
|
-
next(new
|
|
7945
|
+
next(new import_utils45.BadRequestError("Invalid limit number."));
|
|
7893
7946
|
return;
|
|
7894
7947
|
}
|
|
7895
7948
|
if (error) {
|
|
7896
|
-
next(new
|
|
7949
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7897
7950
|
return;
|
|
7898
7951
|
}
|
|
7899
7952
|
try {
|
|
@@ -7917,7 +7970,7 @@ function usePSGCController() {
|
|
|
7917
7970
|
});
|
|
7918
7971
|
const { error } = validation.validate({ id });
|
|
7919
7972
|
if (error) {
|
|
7920
|
-
next(new
|
|
7973
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7921
7974
|
return;
|
|
7922
7975
|
}
|
|
7923
7976
|
try {
|
|
@@ -7938,7 +7991,7 @@ function usePSGCController() {
|
|
|
7938
7991
|
});
|
|
7939
7992
|
const { error } = validation.validate({ name });
|
|
7940
7993
|
if (error) {
|
|
7941
|
-
next(new
|
|
7994
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7942
7995
|
return;
|
|
7943
7996
|
}
|
|
7944
7997
|
try {
|
|
@@ -7962,7 +8015,7 @@ function usePSGCController() {
|
|
|
7962
8015
|
});
|
|
7963
8016
|
const { error } = validation.validate({ _id, field, value });
|
|
7964
8017
|
if (error) {
|
|
7965
|
-
next(new
|
|
8018
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7966
8019
|
return;
|
|
7967
8020
|
}
|
|
7968
8021
|
try {
|
|
@@ -7980,7 +8033,7 @@ function usePSGCController() {
|
|
|
7980
8033
|
});
|
|
7981
8034
|
const { error } = validation.validate({ _id });
|
|
7982
8035
|
if (error) {
|
|
7983
|
-
next(new
|
|
8036
|
+
next(new import_utils45.BadRequestError(error.message));
|
|
7984
8037
|
return;
|
|
7985
8038
|
}
|
|
7986
8039
|
try {
|
|
@@ -8002,7 +8055,7 @@ function usePSGCController() {
|
|
|
8002
8055
|
}
|
|
8003
8056
|
|
|
8004
8057
|
// src/resources/role/role.service.ts
|
|
8005
|
-
var
|
|
8058
|
+
var import_utils46 = require("@goweekdays/utils");
|
|
8006
8059
|
function useRoleService() {
|
|
8007
8060
|
const { getByRole } = useMemberRepo();
|
|
8008
8061
|
const { deleteById: _deleteById } = useRoleRepo();
|
|
@@ -8010,14 +8063,14 @@ function useRoleService() {
|
|
|
8010
8063
|
try {
|
|
8011
8064
|
const role = await getByRole(id);
|
|
8012
8065
|
if (role) {
|
|
8013
|
-
throw new
|
|
8066
|
+
throw new import_utils46.BadRequestError("Cannot delete role assigned to members.");
|
|
8014
8067
|
}
|
|
8015
8068
|
await _deleteById(id);
|
|
8016
8069
|
} catch (error) {
|
|
8017
|
-
if (error instanceof
|
|
8070
|
+
if (error instanceof import_utils46.AppError) {
|
|
8018
8071
|
throw error;
|
|
8019
8072
|
} else {
|
|
8020
|
-
throw new
|
|
8073
|
+
throw new import_utils46.InternalServerError("Failed to delete role.");
|
|
8021
8074
|
}
|
|
8022
8075
|
}
|
|
8023
8076
|
}
|
|
@@ -8028,7 +8081,7 @@ function useRoleService() {
|
|
|
8028
8081
|
|
|
8029
8082
|
// src/resources/role/role.controller.ts
|
|
8030
8083
|
var import_joi25 = __toESM(require("joi"));
|
|
8031
|
-
var
|
|
8084
|
+
var import_utils47 = require("@goweekdays/utils");
|
|
8032
8085
|
function useRoleController() {
|
|
8033
8086
|
const {
|
|
8034
8087
|
addRole: _createRole,
|
|
@@ -8043,7 +8096,7 @@ function useRoleController() {
|
|
|
8043
8096
|
const payload = req.body;
|
|
8044
8097
|
const { error } = schemaRole.validate(payload);
|
|
8045
8098
|
if (error) {
|
|
8046
|
-
next(new
|
|
8099
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8047
8100
|
return;
|
|
8048
8101
|
}
|
|
8049
8102
|
try {
|
|
@@ -8069,7 +8122,7 @@ function useRoleController() {
|
|
|
8069
8122
|
});
|
|
8070
8123
|
const { error } = validation.validate({ search, page, limit, type, id });
|
|
8071
8124
|
if (error) {
|
|
8072
|
-
next(new
|
|
8125
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8073
8126
|
return;
|
|
8074
8127
|
}
|
|
8075
8128
|
try {
|
|
@@ -8087,7 +8140,7 @@ function useRoleController() {
|
|
|
8087
8140
|
});
|
|
8088
8141
|
const { error } = validation.validate({ userId });
|
|
8089
8142
|
if (error) {
|
|
8090
|
-
next(new
|
|
8143
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8091
8144
|
return;
|
|
8092
8145
|
}
|
|
8093
8146
|
try {
|
|
@@ -8105,7 +8158,7 @@ function useRoleController() {
|
|
|
8105
8158
|
});
|
|
8106
8159
|
const { error } = validation.validate({ _id });
|
|
8107
8160
|
if (error) {
|
|
8108
|
-
next(new
|
|
8161
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8109
8162
|
return;
|
|
8110
8163
|
}
|
|
8111
8164
|
try {
|
|
@@ -8127,7 +8180,7 @@ function useRoleController() {
|
|
|
8127
8180
|
});
|
|
8128
8181
|
const { error } = validation.validate({ _id, name, permissions });
|
|
8129
8182
|
if (error) {
|
|
8130
|
-
next(new
|
|
8183
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8131
8184
|
return;
|
|
8132
8185
|
}
|
|
8133
8186
|
try {
|
|
@@ -8147,7 +8200,7 @@ function useRoleController() {
|
|
|
8147
8200
|
});
|
|
8148
8201
|
const { error } = validation.validate({ _id, permissions });
|
|
8149
8202
|
if (error) {
|
|
8150
|
-
next(new
|
|
8203
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8151
8204
|
return;
|
|
8152
8205
|
}
|
|
8153
8206
|
try {
|
|
@@ -8165,7 +8218,7 @@ function useRoleController() {
|
|
|
8165
8218
|
});
|
|
8166
8219
|
const { error } = validation.validate({ _id });
|
|
8167
8220
|
if (error) {
|
|
8168
|
-
next(new
|
|
8221
|
+
next(new import_utils47.BadRequestError(error.message));
|
|
8169
8222
|
return;
|
|
8170
8223
|
}
|
|
8171
8224
|
try {
|
|
@@ -8188,7 +8241,7 @@ function useRoleController() {
|
|
|
8188
8241
|
}
|
|
8189
8242
|
|
|
8190
8243
|
// src/resources/utils/github.service.ts
|
|
8191
|
-
var
|
|
8244
|
+
var import_utils48 = require("@goweekdays/utils");
|
|
8192
8245
|
var import_rest = require("@octokit/rest");
|
|
8193
8246
|
var import_libsodium_wrappers = __toESM(require("libsodium-wrappers"));
|
|
8194
8247
|
function useGitHubService() {
|
|
@@ -8202,23 +8255,23 @@ function useGitHubService() {
|
|
|
8202
8255
|
try {
|
|
8203
8256
|
const { data: repoData } = await octokit.repos.get({ owner, repo });
|
|
8204
8257
|
if (!repoData.permissions?.admin) {
|
|
8205
|
-
throw new
|
|
8258
|
+
throw new import_utils48.BadRequestError(
|
|
8206
8259
|
"You do not have admin access to this repository."
|
|
8207
8260
|
);
|
|
8208
8261
|
}
|
|
8209
8262
|
} catch (error) {
|
|
8210
8263
|
if (error.status === 404) {
|
|
8211
|
-
throw new
|
|
8264
|
+
throw new import_utils48.BadRequestError(
|
|
8212
8265
|
"Repository not found or you don't have access to it."
|
|
8213
8266
|
);
|
|
8214
8267
|
} else if (error.status === 401) {
|
|
8215
|
-
throw new
|
|
8268
|
+
throw new import_utils48.BadRequestError(
|
|
8216
8269
|
"Invalid GitHub token or insufficient permissions."
|
|
8217
8270
|
);
|
|
8218
8271
|
} else if (error.message.includes("admin access")) {
|
|
8219
8272
|
throw error;
|
|
8220
8273
|
} else {
|
|
8221
|
-
throw new
|
|
8274
|
+
throw new import_utils48.BadRequestError(
|
|
8222
8275
|
`Failed to check repository permissions: ${error.message}`
|
|
8223
8276
|
);
|
|
8224
8277
|
}
|
|
@@ -8267,7 +8320,7 @@ function useGitHubService() {
|
|
|
8267
8320
|
key_id: publicKeyRes.key_id
|
|
8268
8321
|
});
|
|
8269
8322
|
} catch (encryptionError) {
|
|
8270
|
-
throw new
|
|
8323
|
+
throw new import_utils48.BadRequestError(
|
|
8271
8324
|
`Failed to encrypt secret '${key}': ${encryptionError.message}`
|
|
8272
8325
|
);
|
|
8273
8326
|
}
|
|
@@ -8297,22 +8350,22 @@ function useGitHubService() {
|
|
|
8297
8350
|
}
|
|
8298
8351
|
return `Successfully set ${lines.length} ${type} variables/secrets in environment '${environment}'`;
|
|
8299
8352
|
} catch (error) {
|
|
8300
|
-
if (error instanceof
|
|
8353
|
+
if (error instanceof import_utils48.AppError)
|
|
8301
8354
|
throw error;
|
|
8302
8355
|
if (error.status === 422) {
|
|
8303
|
-
throw new
|
|
8356
|
+
throw new import_utils48.BadRequestError(
|
|
8304
8357
|
`GitHub API validation error: ${error.message}`
|
|
8305
8358
|
);
|
|
8306
8359
|
} else if (error.status === 404) {
|
|
8307
|
-
throw new
|
|
8360
|
+
throw new import_utils48.BadRequestError("Environment or repository not found.");
|
|
8308
8361
|
} else if (error.status === 403) {
|
|
8309
|
-
throw new
|
|
8362
|
+
throw new import_utils48.BadRequestError(
|
|
8310
8363
|
"Forbidden: Insufficient permissions or rate limit exceeded."
|
|
8311
8364
|
);
|
|
8312
8365
|
} else if (error.message.includes("admin access") || error.message.includes("permissions")) {
|
|
8313
8366
|
throw error;
|
|
8314
8367
|
} else {
|
|
8315
|
-
throw new
|
|
8368
|
+
throw new import_utils48.BadRequestError(
|
|
8316
8369
|
`Failed to set GitHub variables: ${error.message}`
|
|
8317
8370
|
);
|
|
8318
8371
|
}
|
|
@@ -8325,7 +8378,7 @@ function useGitHubService() {
|
|
|
8325
8378
|
|
|
8326
8379
|
// src/resources/utils/util.controller.ts
|
|
8327
8380
|
var import_joi26 = __toESM(require("joi"));
|
|
8328
|
-
var
|
|
8381
|
+
var import_utils49 = require("@goweekdays/utils");
|
|
8329
8382
|
function useUtilController() {
|
|
8330
8383
|
async function healthCheck(req, res, next) {
|
|
8331
8384
|
try {
|
|
@@ -8341,8 +8394,8 @@ function useUtilController() {
|
|
|
8341
8394
|
}
|
|
8342
8395
|
});
|
|
8343
8396
|
} catch (error) {
|
|
8344
|
-
|
|
8345
|
-
next(new
|
|
8397
|
+
import_utils49.logger.error("Health check failed", { error: error.message });
|
|
8398
|
+
next(new import_utils49.InternalServerError("Health check failed"));
|
|
8346
8399
|
}
|
|
8347
8400
|
}
|
|
8348
8401
|
async function setGitHubVariables(req, res, next) {
|
|
@@ -8379,13 +8432,13 @@ function useUtilController() {
|
|
|
8379
8432
|
keyValues
|
|
8380
8433
|
});
|
|
8381
8434
|
if (error) {
|
|
8382
|
-
next(new
|
|
8435
|
+
next(new import_utils49.BadRequestError(error.message));
|
|
8383
8436
|
return;
|
|
8384
8437
|
}
|
|
8385
8438
|
const repoUrlPattern = /github\.com[:\/]([^\/]+)\/(.+)\.git$/;
|
|
8386
8439
|
if (!repoUrlPattern.test(repoUrl)) {
|
|
8387
8440
|
next(
|
|
8388
|
-
new
|
|
8441
|
+
new import_utils49.BadRequestError(
|
|
8389
8442
|
"Invalid GitHub repository URL format. Expected format: https://github.com/owner/repo.git"
|
|
8390
8443
|
)
|
|
8391
8444
|
);
|
|
@@ -8397,7 +8450,7 @@ function useUtilController() {
|
|
|
8397
8450
|
);
|
|
8398
8451
|
if (invalidLines.length > 0) {
|
|
8399
8452
|
next(
|
|
8400
|
-
new
|
|
8453
|
+
new import_utils49.BadRequestError(
|
|
8401
8454
|
"Invalid key-value format. Each pair should be in format: KEY=value. Pairs should be separated by semicolons."
|
|
8402
8455
|
)
|
|
8403
8456
|
);
|
|
@@ -8411,7 +8464,7 @@ function useUtilController() {
|
|
|
8411
8464
|
type,
|
|
8412
8465
|
keyValues
|
|
8413
8466
|
});
|
|
8414
|
-
|
|
8467
|
+
import_utils49.logger.info(`GitHub variables set successfully`, {
|
|
8415
8468
|
repoUrl,
|
|
8416
8469
|
environment,
|
|
8417
8470
|
type,
|
|
@@ -8428,15 +8481,15 @@ function useUtilController() {
|
|
|
8428
8481
|
}
|
|
8429
8482
|
});
|
|
8430
8483
|
} catch (error) {
|
|
8431
|
-
|
|
8484
|
+
import_utils49.logger.error("Failed to set GitHub variables", {
|
|
8432
8485
|
error: error.message,
|
|
8433
8486
|
stack: error.stack
|
|
8434
8487
|
});
|
|
8435
|
-
if (error instanceof
|
|
8488
|
+
if (error instanceof import_utils49.AppError) {
|
|
8436
8489
|
next(error);
|
|
8437
8490
|
} else {
|
|
8438
8491
|
next(
|
|
8439
|
-
new
|
|
8492
|
+
new import_utils49.InternalServerError(
|
|
8440
8493
|
`Failed to set GitHub variables: ${error.message}`
|
|
8441
8494
|
)
|
|
8442
8495
|
);
|
|
@@ -8473,7 +8526,7 @@ var transactionSchema = import_joi27.default.object({
|
|
|
8473
8526
|
});
|
|
8474
8527
|
|
|
8475
8528
|
// src/resources/verification/verification.controller.ts
|
|
8476
|
-
var
|
|
8529
|
+
var import_utils50 = require("@goweekdays/utils");
|
|
8477
8530
|
var import_joi28 = __toESM(require("joi"));
|
|
8478
8531
|
function useVerificationController() {
|
|
8479
8532
|
const {
|
|
@@ -8494,7 +8547,7 @@ function useVerificationController() {
|
|
|
8494
8547
|
});
|
|
8495
8548
|
const { error } = validation.validate(req.body);
|
|
8496
8549
|
if (error) {
|
|
8497
|
-
next(new
|
|
8550
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8498
8551
|
return;
|
|
8499
8552
|
}
|
|
8500
8553
|
const email = req.body.email ?? "";
|
|
@@ -8525,7 +8578,7 @@ function useVerificationController() {
|
|
|
8525
8578
|
const validation = import_joi28.default.string().email().required();
|
|
8526
8579
|
const { error } = validation.validate(email);
|
|
8527
8580
|
if (error) {
|
|
8528
|
-
next(new
|
|
8581
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8529
8582
|
return;
|
|
8530
8583
|
}
|
|
8531
8584
|
try {
|
|
@@ -8535,10 +8588,10 @@ function useVerificationController() {
|
|
|
8535
8588
|
});
|
|
8536
8589
|
return;
|
|
8537
8590
|
} catch (error2) {
|
|
8538
|
-
if (error2 instanceof
|
|
8591
|
+
if (error2 instanceof import_utils50.AppError) {
|
|
8539
8592
|
next(error2);
|
|
8540
8593
|
} else {
|
|
8541
|
-
next(new
|
|
8594
|
+
next(new import_utils50.InternalServerError("An unexpected error occurred"));
|
|
8542
8595
|
}
|
|
8543
8596
|
}
|
|
8544
8597
|
}
|
|
@@ -8553,7 +8606,7 @@ function useVerificationController() {
|
|
|
8553
8606
|
});
|
|
8554
8607
|
const { error } = validation.validate(req.query);
|
|
8555
8608
|
if (error) {
|
|
8556
|
-
next(new
|
|
8609
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8557
8610
|
return;
|
|
8558
8611
|
}
|
|
8559
8612
|
const status = req.query.status ?? "";
|
|
@@ -8587,7 +8640,7 @@ function useVerificationController() {
|
|
|
8587
8640
|
const validation = import_joi28.default.string().hex().required();
|
|
8588
8641
|
const { error } = validation.validate(id);
|
|
8589
8642
|
if (error) {
|
|
8590
|
-
next(new
|
|
8643
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8591
8644
|
return;
|
|
8592
8645
|
}
|
|
8593
8646
|
try {
|
|
@@ -8603,7 +8656,7 @@ function useVerificationController() {
|
|
|
8603
8656
|
const validation = import_joi28.default.string().hex().required();
|
|
8604
8657
|
const { error } = validation.validate(otpId);
|
|
8605
8658
|
if (error) {
|
|
8606
|
-
next(new
|
|
8659
|
+
next(new import_utils50.BadRequestError(error.message));
|
|
8607
8660
|
return;
|
|
8608
8661
|
}
|
|
8609
8662
|
try {
|
|
@@ -8646,11 +8699,9 @@ function useVerificationController() {
|
|
|
8646
8699
|
MMember,
|
|
8647
8700
|
MONGO_DB,
|
|
8648
8701
|
MONGO_URI,
|
|
8649
|
-
MOrg,
|
|
8650
8702
|
MUser,
|
|
8651
8703
|
MUserRole,
|
|
8652
8704
|
MVerification,
|
|
8653
|
-
OrgTypes,
|
|
8654
8705
|
PAYPAL_API_URL,
|
|
8655
8706
|
PAYPAL_CLIENT_ID,
|
|
8656
8707
|
PAYPAL_CLIENT_SECRET,
|
|
@@ -8673,6 +8724,8 @@ function useVerificationController() {
|
|
|
8673
8724
|
addressSchema,
|
|
8674
8725
|
isDev,
|
|
8675
8726
|
modelApp,
|
|
8727
|
+
modelMember,
|
|
8728
|
+
modelOrg,
|
|
8676
8729
|
modelPSGC,
|
|
8677
8730
|
modelPermission,
|
|
8678
8731
|
modelPermissionGroup,
|
|
@@ -8681,6 +8734,7 @@ function useVerificationController() {
|
|
|
8681
8734
|
schemaAppUpdate,
|
|
8682
8735
|
schemaBuilding,
|
|
8683
8736
|
schemaBuildingUnit,
|
|
8737
|
+
schemaMember,
|
|
8684
8738
|
schemaOrg,
|
|
8685
8739
|
schemaPSGC,
|
|
8686
8740
|
schemaPermission,
|