@goweekdays/core 2.3.1 → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.ts +130 -7
- package/dist/index.js +2054 -731
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2049 -718
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/tsconfig.json +1 -1
package/dist/index.js
CHANGED
|
@@ -70,6 +70,7 @@ __export(src_exports, {
|
|
|
70
70
|
XENDIT_BASE_URL: () => XENDIT_BASE_URL,
|
|
71
71
|
XENDIT_SECRET_KEY: () => XENDIT_SECRET_KEY,
|
|
72
72
|
addressSchema: () => addressSchema,
|
|
73
|
+
currencies: () => currencies,
|
|
73
74
|
isDev: () => isDev,
|
|
74
75
|
modelApp: () => modelApp,
|
|
75
76
|
modelMember: () => modelMember,
|
|
@@ -77,7 +78,10 @@ __export(src_exports, {
|
|
|
77
78
|
modelPSGC: () => modelPSGC,
|
|
78
79
|
modelPermission: () => modelPermission,
|
|
79
80
|
modelPermissionGroup: () => modelPermissionGroup,
|
|
81
|
+
modelPlan: () => modelPlan,
|
|
80
82
|
modelRole: () => modelRole,
|
|
83
|
+
modelSubscription: () => modelSubscription,
|
|
84
|
+
modelSubscriptionTransaction: () => modelSubscriptionTransaction,
|
|
81
85
|
modelUser: () => modelUser,
|
|
82
86
|
modelVerification: () => modelVerification,
|
|
83
87
|
schemaApp: () => schemaApp,
|
|
@@ -89,13 +93,20 @@ __export(src_exports, {
|
|
|
89
93
|
schemaMemberRole: () => schemaMemberRole,
|
|
90
94
|
schemaMemberStatus: () => schemaMemberStatus,
|
|
91
95
|
schemaOrg: () => schemaOrg,
|
|
96
|
+
schemaOrgAdd: () => schemaOrgAdd,
|
|
97
|
+
schemaOrgUpdate: () => schemaOrgUpdate,
|
|
92
98
|
schemaPSGC: () => schemaPSGC,
|
|
93
99
|
schemaPermission: () => schemaPermission,
|
|
94
100
|
schemaPermissionGroup: () => schemaPermissionGroup,
|
|
95
101
|
schemaPermissionGroupUpdate: () => schemaPermissionGroupUpdate,
|
|
96
102
|
schemaPermissionUpdate: () => schemaPermissionUpdate,
|
|
103
|
+
schemaPlan: () => schemaPlan,
|
|
97
104
|
schemaRole: () => schemaRole,
|
|
98
105
|
schemaRoleUpdate: () => schemaRoleUpdate,
|
|
106
|
+
schemaSubscription: () => schemaSubscription,
|
|
107
|
+
schemaSubscriptionSeats: () => schemaSubscriptionSeats,
|
|
108
|
+
schemaSubscriptionTransaction: () => schemaSubscriptionTransaction,
|
|
109
|
+
schemaSubscriptionUpdate: () => schemaSubscriptionUpdate,
|
|
99
110
|
schemaUpdateOptions: () => schemaUpdateOptions,
|
|
100
111
|
schemaUser: () => schemaUser,
|
|
101
112
|
schemaVerification: () => schemaVerification,
|
|
@@ -132,9 +143,16 @@ __export(src_exports, {
|
|
|
132
143
|
usePermissionGroupService: () => usePermissionGroupService,
|
|
133
144
|
usePermissionRepo: () => usePermissionRepo,
|
|
134
145
|
usePermissionService: () => usePermissionService,
|
|
146
|
+
usePlanController: () => usePlanController,
|
|
147
|
+
usePlanRepo: () => usePlanRepo,
|
|
148
|
+
usePlanService: () => usePlanService,
|
|
135
149
|
useRoleController: () => useRoleController,
|
|
136
150
|
useRoleRepo: () => useRoleRepo,
|
|
137
151
|
useRoleService: () => useRoleService,
|
|
152
|
+
useSubscriptionController: () => useSubscriptionController,
|
|
153
|
+
useSubscriptionRepo: () => useSubscriptionRepo,
|
|
154
|
+
useSubscriptionTransactionController: () => useSubscriptionTransactionController,
|
|
155
|
+
useSubscriptionTransactionRepo: () => useSubscriptionTransactionRepo,
|
|
138
156
|
useUserController: () => useUserController,
|
|
139
157
|
useUserRepo: () => useUserRepo,
|
|
140
158
|
useUserService: () => useUserService,
|
|
@@ -933,11 +951,11 @@ function useAuthService() {
|
|
|
933
951
|
}
|
|
934
952
|
|
|
935
953
|
// src/resources/auth/auth.controller.ts
|
|
936
|
-
var
|
|
937
|
-
var
|
|
954
|
+
var import_joi32 = __toESM(require("joi"));
|
|
955
|
+
var import_utils44 = require("@goweekdays/utils");
|
|
938
956
|
|
|
939
957
|
// src/resources/verification/verification.service.ts
|
|
940
|
-
var
|
|
958
|
+
var import_utils43 = require("@goweekdays/utils");
|
|
941
959
|
|
|
942
960
|
// src/resources/verification/verification.model.ts
|
|
943
961
|
var import_utils6 = require("@goweekdays/utils");
|
|
@@ -1330,7 +1348,7 @@ var XENDIT_BASE_URL = process.env.XENDIT_BASE_URL ?? "https://api.xendit.co";
|
|
|
1330
1348
|
var DOMAIN = process.env.DOMAIN || "localhost";
|
|
1331
1349
|
|
|
1332
1350
|
// src/resources/user/user.service.ts
|
|
1333
|
-
var
|
|
1351
|
+
var import_utils41 = require("@goweekdays/utils");
|
|
1334
1352
|
|
|
1335
1353
|
// src/resources/file/file.repository.ts
|
|
1336
1354
|
var import_utils8 = require("@goweekdays/utils");
|
|
@@ -1877,9 +1895,10 @@ function useMemberRepo() {
|
|
|
1877
1895
|
}
|
|
1878
1896
|
}
|
|
1879
1897
|
async function getAll({ search, limit, page, user, org, app, status } = {}) {
|
|
1880
|
-
limit = limit > 0 ? limit : 10;
|
|
1898
|
+
limit = limit && limit > 0 ? limit : 10;
|
|
1881
1899
|
search = search || "";
|
|
1882
|
-
page = page > 0 ? page - 1 : 0;
|
|
1900
|
+
page = page && page > 0 ? page - 1 : 0;
|
|
1901
|
+
status = status ?? "active";
|
|
1883
1902
|
const query = { app, status };
|
|
1884
1903
|
const cacheKeyOptions = { app, status, limit, page };
|
|
1885
1904
|
if (user) {
|
|
@@ -4188,6 +4207,33 @@ function usePermissionGroupService() {
|
|
|
4188
4207
|
description: "Allow user to revoke invitations"
|
|
4189
4208
|
}
|
|
4190
4209
|
]
|
|
4210
|
+
},
|
|
4211
|
+
{
|
|
4212
|
+
key: "billing",
|
|
4213
|
+
label: "Billing",
|
|
4214
|
+
permissions: [
|
|
4215
|
+
{
|
|
4216
|
+
key: "billing.view",
|
|
4217
|
+
name: "View Billing",
|
|
4218
|
+
description: "Allow user to view billing"
|
|
4219
|
+
}
|
|
4220
|
+
]
|
|
4221
|
+
},
|
|
4222
|
+
{
|
|
4223
|
+
key: "setting",
|
|
4224
|
+
label: "Setting",
|
|
4225
|
+
permissions: [
|
|
4226
|
+
{
|
|
4227
|
+
key: "setting.view",
|
|
4228
|
+
name: "View Setting",
|
|
4229
|
+
description: "Allow user to view setting"
|
|
4230
|
+
},
|
|
4231
|
+
{
|
|
4232
|
+
key: "setting.edit.details",
|
|
4233
|
+
name: "Edit Setting Details",
|
|
4234
|
+
description: "Allow user to edit setting details"
|
|
4235
|
+
}
|
|
4236
|
+
]
|
|
4191
4237
|
}
|
|
4192
4238
|
];
|
|
4193
4239
|
for (const app of apps.items) {
|
|
@@ -4622,13 +4668,25 @@ function usePermissionGroupController() {
|
|
|
4622
4668
|
var import_utils24 = require("@goweekdays/utils");
|
|
4623
4669
|
var import_joi17 = __toESM(require("joi"));
|
|
4624
4670
|
var import_mongodb15 = require("mongodb");
|
|
4625
|
-
var
|
|
4671
|
+
var schema = {
|
|
4626
4672
|
name: import_joi17.default.string().max(255).required(),
|
|
4627
4673
|
description: import_joi17.default.string().max(1024).optional().allow("", null),
|
|
4628
|
-
email: import_joi17.default.string().email().max(255).
|
|
4629
|
-
contact: import_joi17.default.string().max(50).optional().allow("", null)
|
|
4674
|
+
email: import_joi17.default.string().email().max(255).required(),
|
|
4675
|
+
contact: import_joi17.default.string().max(50).optional().allow("", null)
|
|
4676
|
+
};
|
|
4677
|
+
var schemaOrg = import_joi17.default.object({
|
|
4678
|
+
...schema,
|
|
4630
4679
|
createdBy: import_joi17.default.string().hex().required()
|
|
4631
4680
|
});
|
|
4681
|
+
var schemaOrgAdd = import_joi17.default.object({
|
|
4682
|
+
...schema,
|
|
4683
|
+
createdBy: import_joi17.default.string().hex().required(),
|
|
4684
|
+
seats: import_joi17.default.number().required()
|
|
4685
|
+
});
|
|
4686
|
+
var schemaOrgUpdate = import_joi17.default.object({
|
|
4687
|
+
...schema,
|
|
4688
|
+
_id: import_joi17.default.string().hex().required()
|
|
4689
|
+
});
|
|
4632
4690
|
function modelOrg(value) {
|
|
4633
4691
|
const { error } = schemaOrg.validate(value);
|
|
4634
4692
|
if (error) {
|
|
@@ -4902,6 +4960,30 @@ function useOrgRepo() {
|
|
|
4902
4960
|
throw new import_utils25.InternalServerError("Failed to delete organization.");
|
|
4903
4961
|
}
|
|
4904
4962
|
}
|
|
4963
|
+
async function updateById(_id, options) {
|
|
4964
|
+
const { error } = schemaOrgUpdate.validate({ ...options, _id });
|
|
4965
|
+
if (error) {
|
|
4966
|
+
throw new import_utils25.BadRequestError(error.message);
|
|
4967
|
+
}
|
|
4968
|
+
try {
|
|
4969
|
+
_id = new import_mongodb16.ObjectId(_id);
|
|
4970
|
+
} catch (error2) {
|
|
4971
|
+
throw new import_utils25.BadRequestError("Invalid org ID.");
|
|
4972
|
+
}
|
|
4973
|
+
try {
|
|
4974
|
+
await collection.updateOne(
|
|
4975
|
+
{ _id },
|
|
4976
|
+
{ $set: { ...options, updatedAt: /* @__PURE__ */ new Date() } }
|
|
4977
|
+
);
|
|
4978
|
+
delCachedData();
|
|
4979
|
+
} catch (error2) {
|
|
4980
|
+
const isDuplicated = error2.message.includes("duplicate");
|
|
4981
|
+
if (isDuplicated) {
|
|
4982
|
+
throw new import_utils25.BadRequestError("Organization name already exist.");
|
|
4983
|
+
}
|
|
4984
|
+
throw new import_utils25.InternalServerError("Failed to update org info.");
|
|
4985
|
+
}
|
|
4986
|
+
}
|
|
4905
4987
|
return {
|
|
4906
4988
|
createIndexes,
|
|
4907
4989
|
add,
|
|
@@ -4909,12 +4991,13 @@ function useOrgRepo() {
|
|
|
4909
4991
|
getById,
|
|
4910
4992
|
updateFieldById,
|
|
4911
4993
|
deleteById,
|
|
4912
|
-
getByName
|
|
4994
|
+
getByName,
|
|
4995
|
+
updateById
|
|
4913
4996
|
};
|
|
4914
4997
|
}
|
|
4915
4998
|
|
|
4916
4999
|
// src/resources/organization/organization.service.ts
|
|
4917
|
-
var
|
|
5000
|
+
var import_utils39 = require("@goweekdays/utils");
|
|
4918
5001
|
|
|
4919
5002
|
// src/resources/member/member.controller.ts
|
|
4920
5003
|
var import_joi20 = __toESM(require("joi"));
|
|
@@ -5321,6 +5404,44 @@ function useMemberController() {
|
|
|
5321
5404
|
next(error2);
|
|
5322
5405
|
}
|
|
5323
5406
|
}
|
|
5407
|
+
async function getAllByAppUser(req, res, next) {
|
|
5408
|
+
const limit = Number(req.query.limit) ?? 10;
|
|
5409
|
+
const search = req.query.search ?? "";
|
|
5410
|
+
const page = Number(req.query.page) ?? 1;
|
|
5411
|
+
const user = req.params.user ?? "";
|
|
5412
|
+
const app = req.params.app ?? "";
|
|
5413
|
+
const validation = import_joi20.default.object({
|
|
5414
|
+
limit: import_joi20.default.number().min(10).max(50).allow(null, ""),
|
|
5415
|
+
search: import_joi20.default.string().optional().allow("", null),
|
|
5416
|
+
page: import_joi20.default.number().optional().allow(null, ""),
|
|
5417
|
+
app: import_joi20.default.string().required(),
|
|
5418
|
+
user: import_joi20.default.string().hex().required()
|
|
5419
|
+
});
|
|
5420
|
+
const { error } = validation.validate({
|
|
5421
|
+
search,
|
|
5422
|
+
page,
|
|
5423
|
+
limit,
|
|
5424
|
+
user,
|
|
5425
|
+
app
|
|
5426
|
+
});
|
|
5427
|
+
if (error) {
|
|
5428
|
+
next(new import_utils29.BadRequestError(error.message));
|
|
5429
|
+
return;
|
|
5430
|
+
}
|
|
5431
|
+
try {
|
|
5432
|
+
const items = await _getAll({
|
|
5433
|
+
search,
|
|
5434
|
+
page,
|
|
5435
|
+
limit,
|
|
5436
|
+
user,
|
|
5437
|
+
app
|
|
5438
|
+
});
|
|
5439
|
+
res.json(items);
|
|
5440
|
+
return;
|
|
5441
|
+
} catch (error2) {
|
|
5442
|
+
next(error2);
|
|
5443
|
+
}
|
|
5444
|
+
}
|
|
5324
5445
|
async function getOrgsByMembership(req, res, next) {
|
|
5325
5446
|
const limit = Number(req.query.limit) ?? 10;
|
|
5326
5447
|
const search = req.query.search ?? "";
|
|
@@ -5427,184 +5548,1351 @@ function useMemberController() {
|
|
|
5427
5548
|
getByUserType,
|
|
5428
5549
|
updateRoleById,
|
|
5429
5550
|
updateStatusById,
|
|
5430
|
-
deleteById
|
|
5551
|
+
deleteById,
|
|
5552
|
+
getAllByAppUser
|
|
5431
5553
|
};
|
|
5432
5554
|
}
|
|
5433
5555
|
|
|
5434
|
-
// src/resources/
|
|
5435
|
-
|
|
5436
|
-
|
|
5437
|
-
|
|
5438
|
-
|
|
5439
|
-
|
|
5440
|
-
|
|
5441
|
-
|
|
5442
|
-
|
|
5443
|
-
|
|
5444
|
-
|
|
5445
|
-
|
|
5556
|
+
// src/resources/subscription/subscription.model.ts
|
|
5557
|
+
var import_utils30 = require("@goweekdays/utils");
|
|
5558
|
+
var import_joi21 = __toESM(require("joi"));
|
|
5559
|
+
var import_mongodb17 = require("mongodb");
|
|
5560
|
+
var schema2 = {
|
|
5561
|
+
seats: import_joi21.default.number().integer().min(1).required(),
|
|
5562
|
+
paidSeats: import_joi21.default.number().integer().min(0).required(),
|
|
5563
|
+
amount: import_joi21.default.number().positive().required(),
|
|
5564
|
+
promoCode: import_joi21.default.string().optional().allow("", null),
|
|
5565
|
+
nextBillingDate: import_joi21.default.date().optional().allow("", null)
|
|
5566
|
+
};
|
|
5567
|
+
var schemaSubscription = import_joi21.default.object({
|
|
5568
|
+
...schema2,
|
|
5569
|
+
org: import_joi21.default.string().hex().length(24).required(),
|
|
5570
|
+
currency: import_joi21.default.string().length(3).required(),
|
|
5571
|
+
billingCycle: import_joi21.default.string().valid("monthly", "yearly").required()
|
|
5572
|
+
});
|
|
5573
|
+
var schemaSubscriptionUpdate = import_joi21.default.object({
|
|
5574
|
+
...schema2,
|
|
5575
|
+
status: import_joi21.default.string().optional().allow("", null)
|
|
5576
|
+
});
|
|
5577
|
+
var schemaSubscriptionSeats = import_joi21.default.object({
|
|
5578
|
+
id: import_joi21.default.string().hex().length(24).required(),
|
|
5579
|
+
seats: import_joi21.default.number().integer().min(1).required(),
|
|
5580
|
+
amount: import_joi21.default.number().positive().optional().allow(null, 0),
|
|
5581
|
+
user: import_joi21.default.string().hex().length(24).required()
|
|
5582
|
+
});
|
|
5583
|
+
function modelSubscription(data) {
|
|
5584
|
+
const { error } = schemaSubscription.validate(data);
|
|
5585
|
+
if (error) {
|
|
5586
|
+
throw new import_utils30.BadRequestError(`Invalid subscription data: ${error.message}`);
|
|
5587
|
+
}
|
|
5588
|
+
if (data._id && typeof data._id === "string") {
|
|
5446
5589
|
try {
|
|
5447
|
-
|
|
5448
|
-
|
|
5449
|
-
|
|
5450
|
-
app: "org",
|
|
5451
|
-
limit: 100
|
|
5452
|
-
});
|
|
5453
|
-
let permissions = [];
|
|
5454
|
-
if (allPermissions && allPermissions.items && allPermissions.items.length) {
|
|
5455
|
-
permissions = allPermissions.items.map((perm) => perm.key);
|
|
5456
|
-
}
|
|
5457
|
-
if (permissions.length === 0) {
|
|
5458
|
-
throw new Error("No permissions found for the organization type.");
|
|
5459
|
-
}
|
|
5460
|
-
const createdBy = String(value.createdBy);
|
|
5461
|
-
const roleData = {
|
|
5462
|
-
org: String(org),
|
|
5463
|
-
name: "Owner",
|
|
5464
|
-
description: "Owner of the organization",
|
|
5465
|
-
permissions,
|
|
5466
|
-
createdBy,
|
|
5467
|
-
app: "org"
|
|
5468
|
-
};
|
|
5469
|
-
const role = await addRole(roleData, session);
|
|
5470
|
-
if (!role) {
|
|
5471
|
-
throw new import_utils30.BadRequestError("Role is required to create org member.");
|
|
5472
|
-
}
|
|
5473
|
-
const user = await getUserById(createdBy);
|
|
5474
|
-
if (!user) {
|
|
5475
|
-
throw new import_utils30.BadRequestError("User is required to create org member.");
|
|
5476
|
-
}
|
|
5477
|
-
await addMember(
|
|
5478
|
-
{
|
|
5479
|
-
role: String(role),
|
|
5480
|
-
roleName: roleData.name,
|
|
5481
|
-
org: String(org),
|
|
5482
|
-
name: `${user.firstName} ${user.lastName}`,
|
|
5483
|
-
user: createdBy,
|
|
5484
|
-
app: "org"
|
|
5485
|
-
},
|
|
5486
|
-
session
|
|
5487
|
-
);
|
|
5488
|
-
await session?.commitTransaction();
|
|
5489
|
-
return String(org);
|
|
5490
|
-
} catch (error) {
|
|
5491
|
-
await session?.abortTransaction();
|
|
5492
|
-
throw error;
|
|
5493
|
-
} finally {
|
|
5494
|
-
await session?.endSession();
|
|
5590
|
+
data._id = new import_mongodb17.ObjectId(data._id);
|
|
5591
|
+
} catch (error2) {
|
|
5592
|
+
throw new import_utils30.BadRequestError("Invalid subscription ID.");
|
|
5495
5593
|
}
|
|
5496
5594
|
}
|
|
5595
|
+
try {
|
|
5596
|
+
data.org = new import_mongodb17.ObjectId(data.org);
|
|
5597
|
+
} catch (error2) {
|
|
5598
|
+
throw new import_utils30.BadRequestError("Invalid organization ID.");
|
|
5599
|
+
}
|
|
5497
5600
|
return {
|
|
5498
|
-
|
|
5601
|
+
_id: data._id,
|
|
5602
|
+
org: data.org,
|
|
5603
|
+
seats: data.seats,
|
|
5604
|
+
paidSeats: data.paidSeats,
|
|
5605
|
+
amount: data.amount,
|
|
5606
|
+
currency: data.currency,
|
|
5607
|
+
billingCycle: data.billingCycle,
|
|
5608
|
+
promoCode: data.promoCode,
|
|
5609
|
+
status: data.status ?? "active",
|
|
5610
|
+
nextBillingDate: data.nextBillingDate,
|
|
5611
|
+
createdAt: data.createdAt ?? /* @__PURE__ */ new Date(),
|
|
5612
|
+
updatedAt: data.updatedAt ?? ""
|
|
5499
5613
|
};
|
|
5500
5614
|
}
|
|
5501
5615
|
|
|
5502
|
-
// src/resources/
|
|
5616
|
+
// src/resources/subscription/subscription.repository.ts
|
|
5503
5617
|
var import_utils31 = require("@goweekdays/utils");
|
|
5504
|
-
var
|
|
5505
|
-
|
|
5506
|
-
|
|
5507
|
-
const
|
|
5508
|
-
|
|
5509
|
-
|
|
5510
|
-
getAll: getAllOrg,
|
|
5511
|
-
getById: _getById
|
|
5512
|
-
} = useOrgRepo();
|
|
5513
|
-
async function add(req, res, next) {
|
|
5514
|
-
const value = req.body;
|
|
5515
|
-
const { error } = schemaOrg.validate(value);
|
|
5516
|
-
if (error) {
|
|
5517
|
-
next(new import_utils31.BadRequestError(error.message));
|
|
5518
|
-
return;
|
|
5519
|
-
}
|
|
5520
|
-
try {
|
|
5521
|
-
const org = await _add(value);
|
|
5522
|
-
res.json({
|
|
5523
|
-
message: "Organization created successfully.",
|
|
5524
|
-
data: { org }
|
|
5525
|
-
});
|
|
5526
|
-
return;
|
|
5527
|
-
} catch (error2) {
|
|
5528
|
-
next(error2);
|
|
5529
|
-
}
|
|
5618
|
+
var import_joi22 = __toESM(require("joi"));
|
|
5619
|
+
var import_mongodb18 = require("mongodb");
|
|
5620
|
+
function useSubscriptionRepo() {
|
|
5621
|
+
const db = import_utils31.useAtlas.getDb();
|
|
5622
|
+
if (!db) {
|
|
5623
|
+
throw new import_utils31.InternalServerError("Unable to connect to server.");
|
|
5530
5624
|
}
|
|
5531
|
-
|
|
5532
|
-
|
|
5533
|
-
|
|
5534
|
-
|
|
5535
|
-
|
|
5536
|
-
|
|
5537
|
-
|
|
5538
|
-
|
|
5539
|
-
|
|
5540
|
-
}
|
|
5541
|
-
|
|
5542
|
-
|
|
5543
|
-
|
|
5544
|
-
|
|
5545
|
-
}
|
|
5546
|
-
const validation = import_joi21.default.object({
|
|
5547
|
-
user: import_joi21.default.string().hex().required(),
|
|
5548
|
-
page: import_joi21.default.number().min(1).optional().allow("", null),
|
|
5549
|
-
limit: import_joi21.default.number().min(1).optional().allow("", null),
|
|
5550
|
-
search: import_joi21.default.string().optional().allow("", null)
|
|
5625
|
+
const namespace_collection = "subscriptions";
|
|
5626
|
+
const collection = db.collection(namespace_collection);
|
|
5627
|
+
const { getCache, setCache, delNamespace } = (0, import_utils31.useCache)(namespace_collection);
|
|
5628
|
+
function delCachedData() {
|
|
5629
|
+
delNamespace().then(() => {
|
|
5630
|
+
import_utils31.logger.log({
|
|
5631
|
+
level: "info",
|
|
5632
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
5633
|
+
});
|
|
5634
|
+
}).catch((err) => {
|
|
5635
|
+
import_utils31.logger.log({
|
|
5636
|
+
level: "error",
|
|
5637
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
5638
|
+
});
|
|
5551
5639
|
});
|
|
5552
|
-
|
|
5553
|
-
|
|
5554
|
-
next(new import_utils31.BadRequestError(error.message));
|
|
5555
|
-
return;
|
|
5556
|
-
}
|
|
5640
|
+
}
|
|
5641
|
+
async function createIndexes() {
|
|
5557
5642
|
try {
|
|
5558
|
-
|
|
5559
|
-
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
|
|
5643
|
+
await collection.createIndexes([
|
|
5644
|
+
{
|
|
5645
|
+
key: {
|
|
5646
|
+
org: 1
|
|
5647
|
+
},
|
|
5648
|
+
name: "org_index"
|
|
5649
|
+
},
|
|
5650
|
+
{
|
|
5651
|
+
key: { org: 1, status: 1 },
|
|
5652
|
+
name: "org_status_index"
|
|
5653
|
+
}
|
|
5654
|
+
]);
|
|
5655
|
+
} catch (error) {
|
|
5563
5656
|
}
|
|
5564
5657
|
}
|
|
5565
|
-
|
|
5566
|
-
|
|
5567
|
-
|
|
5568
|
-
|
|
5569
|
-
limit: import_joi21.default.number().min(1).optional().allow("", null),
|
|
5570
|
-
search: import_joi21.default.string().optional().allow("", null),
|
|
5571
|
-
status: import_joi21.default.string().valid("active", "suspended", "inactive", "deleted").optional()
|
|
5658
|
+
createIndexes().catch((error) => {
|
|
5659
|
+
import_utils31.logger.log({
|
|
5660
|
+
level: "error",
|
|
5661
|
+
message: `Failed to create indexes for subscriptions collection: ${error.message}`
|
|
5572
5662
|
});
|
|
5573
|
-
|
|
5574
|
-
|
|
5575
|
-
const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
|
|
5576
|
-
const search = req.query.search ?? "";
|
|
5577
|
-
const status = req.query.status ?? "active";
|
|
5578
|
-
const isPageNumber = isFinite(page);
|
|
5579
|
-
if (!isPageNumber) {
|
|
5580
|
-
next(new import_utils31.BadRequestError("Invalid page number."));
|
|
5581
|
-
return;
|
|
5582
|
-
}
|
|
5583
|
-
const isLimitNumber = isFinite(limit);
|
|
5584
|
-
if (!isLimitNumber) {
|
|
5585
|
-
next(new import_utils31.BadRequestError("Invalid limit number."));
|
|
5586
|
-
return;
|
|
5587
|
-
}
|
|
5588
|
-
if (error) {
|
|
5589
|
-
next(new import_utils31.BadRequestError(error.message));
|
|
5590
|
-
return;
|
|
5591
|
-
}
|
|
5663
|
+
});
|
|
5664
|
+
async function add(value, session) {
|
|
5592
5665
|
try {
|
|
5593
|
-
|
|
5594
|
-
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5666
|
+
value = modelSubscription(value);
|
|
5667
|
+
const result = await collection.insertOne(value, { session });
|
|
5668
|
+
delCachedData();
|
|
5669
|
+
return result.insertedId.toString();
|
|
5670
|
+
} catch (error) {
|
|
5671
|
+
import_utils31.logger.log({
|
|
5672
|
+
level: "error",
|
|
5673
|
+
message: `${error}`
|
|
5674
|
+
});
|
|
5675
|
+
if (error instanceof import_utils31.AppError) {
|
|
5676
|
+
throw error;
|
|
5677
|
+
}
|
|
5678
|
+
throw new import_utils31.InternalServerError("Failed to add subscription.");
|
|
5679
|
+
}
|
|
5680
|
+
}
|
|
5681
|
+
async function getAll({
|
|
5682
|
+
page = 1,
|
|
5683
|
+
limit = 10,
|
|
5684
|
+
search = "",
|
|
5685
|
+
status = "active"
|
|
5686
|
+
} = {}) {
|
|
5687
|
+
page = page < 1 ? page - 1 : page;
|
|
5688
|
+
const query = { status };
|
|
5689
|
+
const cacheKeyOptions = {
|
|
5690
|
+
page,
|
|
5691
|
+
limit,
|
|
5692
|
+
search,
|
|
5693
|
+
status,
|
|
5694
|
+
tag: "getAll"
|
|
5695
|
+
};
|
|
5696
|
+
if (search) {
|
|
5697
|
+
query.$text = { $search: search };
|
|
5698
|
+
}
|
|
5699
|
+
const cacheKey = (0, import_utils31.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
5700
|
+
try {
|
|
5701
|
+
const cachedData = await getCache(cacheKey);
|
|
5702
|
+
if (cachedData) {
|
|
5703
|
+
return cachedData;
|
|
5704
|
+
}
|
|
5705
|
+
const items = await collection.aggregate([
|
|
5706
|
+
{ $match: query },
|
|
5707
|
+
{ $skip: page * limit },
|
|
5708
|
+
{ $limit: limit }
|
|
5709
|
+
]).toArray();
|
|
5710
|
+
const length = await collection.countDocuments(query);
|
|
5711
|
+
const data = (0, import_utils31.paginate)(
|
|
5712
|
+
items,
|
|
5713
|
+
page,
|
|
5714
|
+
limit,
|
|
5715
|
+
length
|
|
5716
|
+
);
|
|
5717
|
+
setCache(cacheKey, data).then(() => {
|
|
5718
|
+
import_utils31.logger.log({
|
|
5719
|
+
level: "info",
|
|
5720
|
+
message: `Cache set for getAll subscription: ${cacheKey}`
|
|
5721
|
+
});
|
|
5722
|
+
}).catch((err) => {
|
|
5723
|
+
import_utils31.logger.log({
|
|
5724
|
+
level: "error",
|
|
5725
|
+
message: `Failed to set cache for getAll subscription: ${err.message}`
|
|
5726
|
+
});
|
|
5727
|
+
});
|
|
5728
|
+
return data;
|
|
5729
|
+
} catch (error) {
|
|
5730
|
+
throw new import_utils31.InternalServerError("Failed to get subscriptions.");
|
|
5731
|
+
}
|
|
5732
|
+
}
|
|
5733
|
+
async function getByOrg(org) {
|
|
5734
|
+
const { error } = import_joi22.default.string().hex().length(24).required().validate(org);
|
|
5735
|
+
if (error) {
|
|
5736
|
+
throw new Error(`Invalid org ID: ${error.message}`);
|
|
5737
|
+
}
|
|
5738
|
+
try {
|
|
5739
|
+
org = new import_mongodb18.ObjectId(org);
|
|
5740
|
+
} catch (error2) {
|
|
5741
|
+
throw new import_utils31.BadRequestError("Invalid org ID.");
|
|
5742
|
+
}
|
|
5743
|
+
try {
|
|
5744
|
+
const cacheKey = (0, import_utils31.makeCacheKey)(namespace_collection, {
|
|
5745
|
+
org: String(org),
|
|
5746
|
+
tag: "getByOrg"
|
|
5747
|
+
});
|
|
5748
|
+
const cachedData = await getCache(cacheKey);
|
|
5749
|
+
if (cachedData) {
|
|
5750
|
+
return cachedData;
|
|
5751
|
+
}
|
|
5752
|
+
const data = await collection.findOne({
|
|
5753
|
+
org,
|
|
5754
|
+
status: { $ne: "deleted" }
|
|
5755
|
+
});
|
|
5756
|
+
setCache(cacheKey, data).then(() => {
|
|
5757
|
+
import_utils31.logger.log({
|
|
5758
|
+
level: "info",
|
|
5759
|
+
message: `Cache set for getByOrg subscription: ${cacheKey}`
|
|
5760
|
+
});
|
|
5761
|
+
}).catch((err) => {
|
|
5762
|
+
import_utils31.logger.log({
|
|
5763
|
+
level: "error",
|
|
5764
|
+
message: `Failed to set cache for getByOrg subscription: ${err.message}`
|
|
5765
|
+
});
|
|
5766
|
+
});
|
|
5767
|
+
return data;
|
|
5768
|
+
} catch (error2) {
|
|
5769
|
+
throw new import_utils31.InternalServerError("Failed to get subscription.");
|
|
5770
|
+
}
|
|
5771
|
+
}
|
|
5772
|
+
async function getById(_id) {
|
|
5773
|
+
const { error } = import_joi22.default.string().hex().length(24).required().validate(_id);
|
|
5774
|
+
if (error) {
|
|
5775
|
+
throw new Error(`Invalid subscription ID: ${error.message}`);
|
|
5776
|
+
}
|
|
5777
|
+
try {
|
|
5778
|
+
_id = new import_mongodb18.ObjectId(_id);
|
|
5779
|
+
} catch (error2) {
|
|
5780
|
+
throw new import_utils31.BadRequestError("Invalid subscription ID.");
|
|
5781
|
+
}
|
|
5782
|
+
try {
|
|
5783
|
+
const cacheKey = (0, import_utils31.makeCacheKey)(namespace_collection, {
|
|
5784
|
+
_id: String(_id),
|
|
5785
|
+
tag: "getById"
|
|
5786
|
+
});
|
|
5787
|
+
const cachedData = await getCache(cacheKey);
|
|
5788
|
+
if (cachedData) {
|
|
5789
|
+
return cachedData;
|
|
5790
|
+
}
|
|
5791
|
+
const data = await collection.findOne({
|
|
5792
|
+
_id,
|
|
5793
|
+
status: { $ne: "deleted" }
|
|
5794
|
+
});
|
|
5795
|
+
setCache(cacheKey, data).then(() => {
|
|
5796
|
+
import_utils31.logger.log({
|
|
5797
|
+
level: "info",
|
|
5798
|
+
message: `Cache set for getById subscription: ${cacheKey}`
|
|
5799
|
+
});
|
|
5800
|
+
}).catch((err) => {
|
|
5801
|
+
import_utils31.logger.log({
|
|
5802
|
+
level: "error",
|
|
5803
|
+
message: `Failed to set cache for getById subscription: ${err.message}`
|
|
5804
|
+
});
|
|
5805
|
+
});
|
|
5806
|
+
return data;
|
|
5807
|
+
} catch (error2) {
|
|
5808
|
+
throw new import_utils31.InternalServerError("Failed to get subscription.");
|
|
5809
|
+
}
|
|
5810
|
+
}
|
|
5811
|
+
async function deleteById(_id) {
|
|
5812
|
+
const { error } = import_joi22.default.string().hex().length(24).required().validate(_id);
|
|
5813
|
+
if (error) {
|
|
5814
|
+
throw new Error(`Invalid subscription ID: ${error.message}`);
|
|
5815
|
+
}
|
|
5816
|
+
try {
|
|
5817
|
+
_id = new import_mongodb18.ObjectId(_id);
|
|
5818
|
+
} catch (error2) {
|
|
5819
|
+
throw new import_utils31.BadRequestError("Invalid subscription ID.");
|
|
5820
|
+
}
|
|
5821
|
+
try {
|
|
5822
|
+
const result = await collection.updateOne(
|
|
5823
|
+
{ _id },
|
|
5824
|
+
{ $set: { status: "deleted" } }
|
|
5825
|
+
);
|
|
5826
|
+
if (result.modifiedCount === 0) {
|
|
5827
|
+
throw new import_utils31.InternalServerError("Failed to delete subscription.");
|
|
5828
|
+
}
|
|
5829
|
+
delCachedData();
|
|
5830
|
+
return "Successfully deleted subscription.";
|
|
5831
|
+
} catch (error2) {
|
|
5832
|
+
if (error2 instanceof import_utils31.AppError) {
|
|
5833
|
+
throw error2;
|
|
5834
|
+
}
|
|
5835
|
+
throw new import_utils31.InternalServerError("Failed to delete subscription.");
|
|
5836
|
+
}
|
|
5837
|
+
}
|
|
5838
|
+
async function updateById(_id, options, session) {
|
|
5839
|
+
const { error: errorId } = import_joi22.default.string().hex().length(24).required().validate(_id);
|
|
5840
|
+
if (errorId) {
|
|
5841
|
+
throw new Error(`Invalid subscription ID: ${errorId.message}`);
|
|
5842
|
+
}
|
|
5843
|
+
const { error } = schemaSubscriptionUpdate.validate(options);
|
|
5844
|
+
if (error) {
|
|
5845
|
+
throw new import_utils31.BadRequestError(
|
|
5846
|
+
`Invalid subscription update data: ${error.message}`
|
|
5847
|
+
);
|
|
5848
|
+
}
|
|
5849
|
+
try {
|
|
5850
|
+
_id = new import_mongodb18.ObjectId(_id);
|
|
5851
|
+
} catch (error2) {
|
|
5852
|
+
throw new import_utils31.BadRequestError("Invalid subscription ID.");
|
|
5853
|
+
}
|
|
5854
|
+
try {
|
|
5855
|
+
const result = await collection.updateOne(
|
|
5856
|
+
{ _id },
|
|
5857
|
+
{ $set: { ...options, updatedAt: /* @__PURE__ */ new Date() } },
|
|
5858
|
+
{ session }
|
|
5859
|
+
);
|
|
5860
|
+
if (result.modifiedCount === 0) {
|
|
5861
|
+
throw new import_utils31.InternalServerError("Failed to update subscription.");
|
|
5862
|
+
}
|
|
5863
|
+
delCachedData();
|
|
5864
|
+
return "Successfully updated subscription.";
|
|
5865
|
+
} catch (error2) {
|
|
5866
|
+
if (error2 instanceof import_utils31.AppError) {
|
|
5867
|
+
throw error2;
|
|
5868
|
+
}
|
|
5869
|
+
throw new import_utils31.InternalServerError("Failed to update subscription.");
|
|
5870
|
+
}
|
|
5871
|
+
}
|
|
5872
|
+
return {
|
|
5873
|
+
add,
|
|
5874
|
+
getAll,
|
|
5875
|
+
getByOrg,
|
|
5876
|
+
getById,
|
|
5877
|
+
deleteById,
|
|
5878
|
+
updateById
|
|
5879
|
+
};
|
|
5880
|
+
}
|
|
5881
|
+
|
|
5882
|
+
// src/resources/subscription/subscription.controller.ts
|
|
5883
|
+
var import_joi24 = __toESM(require("joi"));
|
|
5884
|
+
var import_utils35 = require("@goweekdays/utils");
|
|
5885
|
+
|
|
5886
|
+
// src/resources/subscription/subscription.service.ts
|
|
5887
|
+
var import_utils34 = require("@goweekdays/utils");
|
|
5888
|
+
|
|
5889
|
+
// src/resources/subscription/subscription.transaction.repository.ts
|
|
5890
|
+
var import_utils33 = require("@goweekdays/utils");
|
|
5891
|
+
|
|
5892
|
+
// src/resources/subscription/subscription.transaction.model.ts
|
|
5893
|
+
var import_utils32 = require("@goweekdays/utils");
|
|
5894
|
+
var import_joi23 = __toESM(require("joi"));
|
|
5895
|
+
var import_mongodb19 = require("mongodb");
|
|
5896
|
+
var schemaSubscriptionTransaction = import_joi23.default.object({
|
|
5897
|
+
subscription: import_joi23.default.string().hex().length(24).required(),
|
|
5898
|
+
amount: import_joi23.default.number().positive().required(),
|
|
5899
|
+
currency: import_joi23.default.string().length(3).required(),
|
|
5900
|
+
type: import_joi23.default.string().valid("initiate", "add-seat", "remove-seat", "renewal").required(),
|
|
5901
|
+
description: import_joi23.default.string().max(255).optional().allow("", null),
|
|
5902
|
+
createdBy: import_joi23.default.string().hex().length(24).required(),
|
|
5903
|
+
createdByName: import_joi23.default.string().optional().allow("", null)
|
|
5904
|
+
});
|
|
5905
|
+
function modelSubscriptionTransaction(data) {
|
|
5906
|
+
const { error } = schemaSubscriptionTransaction.validate(data);
|
|
5907
|
+
if (error) {
|
|
5908
|
+
throw new import_utils32.BadRequestError(
|
|
5909
|
+
`Invalid subscription transaction data: ${error.message}`
|
|
5910
|
+
);
|
|
5911
|
+
}
|
|
5912
|
+
if (data._id && typeof data._id === "string") {
|
|
5913
|
+
try {
|
|
5914
|
+
data._id = new import_mongodb19.ObjectId(data._id);
|
|
5915
|
+
} catch (error2) {
|
|
5916
|
+
throw new import_utils32.BadRequestError("Invalid subscription transaction ID.");
|
|
5917
|
+
}
|
|
5918
|
+
}
|
|
5919
|
+
if (data.subscription && typeof data.subscription === "string") {
|
|
5920
|
+
try {
|
|
5921
|
+
data.subscription = new import_mongodb19.ObjectId(data.subscription);
|
|
5922
|
+
} catch (error2) {
|
|
5923
|
+
throw new import_utils32.BadRequestError("Invalid subscription ID.");
|
|
5924
|
+
}
|
|
5925
|
+
}
|
|
5926
|
+
if (data.createdBy && typeof data.createdBy === "string") {
|
|
5927
|
+
try {
|
|
5928
|
+
data.createdBy = new import_mongodb19.ObjectId(data.createdBy);
|
|
5929
|
+
} catch (error2) {
|
|
5930
|
+
throw new import_utils32.BadRequestError("Invalid createdBy ID.");
|
|
5931
|
+
}
|
|
5932
|
+
}
|
|
5933
|
+
return {
|
|
5934
|
+
_id: data._id,
|
|
5935
|
+
subscription: data.subscription,
|
|
5936
|
+
amount: data.amount,
|
|
5937
|
+
currency: data.currency,
|
|
5938
|
+
type: data.type,
|
|
5939
|
+
description: data.description ?? "",
|
|
5940
|
+
createdBy: data.createdBy,
|
|
5941
|
+
createdByName: data.createdByName,
|
|
5942
|
+
createdAt: data.createdAt ?? /* @__PURE__ */ new Date(),
|
|
5943
|
+
updatedAt: data.updatedAt ?? ""
|
|
5944
|
+
};
|
|
5945
|
+
}
|
|
5946
|
+
|
|
5947
|
+
// src/resources/subscription/subscription.transaction.repository.ts
|
|
5948
|
+
var import_mongodb20 = require("mongodb");
|
|
5949
|
+
function useSubscriptionTransactionRepo() {
|
|
5950
|
+
const db = import_utils33.useAtlas.getDb();
|
|
5951
|
+
if (!db) {
|
|
5952
|
+
throw new import_utils33.InternalServerError("Unable to connect to server.");
|
|
5953
|
+
}
|
|
5954
|
+
const namespace_collection = "subscription.transactions";
|
|
5955
|
+
const collection = db.collection(namespace_collection);
|
|
5956
|
+
const { getCache, setCache, delNamespace } = (0, import_utils33.useCache)(namespace_collection);
|
|
5957
|
+
function delCachedData() {
|
|
5958
|
+
delNamespace().then(() => {
|
|
5959
|
+
import_utils33.logger.log({
|
|
5960
|
+
level: "info",
|
|
5961
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
5962
|
+
});
|
|
5963
|
+
}).catch((err) => {
|
|
5964
|
+
import_utils33.logger.log({
|
|
5965
|
+
level: "error",
|
|
5966
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
5967
|
+
});
|
|
5968
|
+
});
|
|
5969
|
+
}
|
|
5970
|
+
async function createIndexes() {
|
|
5971
|
+
try {
|
|
5972
|
+
await collection.createIndexes([
|
|
5973
|
+
{
|
|
5974
|
+
key: {
|
|
5975
|
+
subscription: 1
|
|
5976
|
+
},
|
|
5977
|
+
name: "subscription_index"
|
|
5978
|
+
},
|
|
5979
|
+
{
|
|
5980
|
+
key: { subscription: 1, type: 1 },
|
|
5981
|
+
name: "subscription_type_index"
|
|
5982
|
+
},
|
|
5983
|
+
{
|
|
5984
|
+
key: { createdAt: -1 },
|
|
5985
|
+
name: "createdAt_index"
|
|
5986
|
+
}
|
|
5987
|
+
]);
|
|
5988
|
+
} catch (error) {
|
|
5989
|
+
}
|
|
5990
|
+
}
|
|
5991
|
+
createIndexes().catch((error) => {
|
|
5992
|
+
import_utils33.logger.log({
|
|
5993
|
+
level: "error",
|
|
5994
|
+
message: `Failed to create indexes for subscription_transactions collection: ${error.message}`
|
|
5995
|
+
});
|
|
5996
|
+
});
|
|
5997
|
+
async function add(value, session) {
|
|
5998
|
+
try {
|
|
5999
|
+
value = modelSubscriptionTransaction(value);
|
|
6000
|
+
await collection.insertOne(value, { session });
|
|
6001
|
+
delCachedData();
|
|
6002
|
+
return "Successfully added subscription transaction.";
|
|
6003
|
+
} catch (error) {
|
|
6004
|
+
import_utils33.logger.log({
|
|
6005
|
+
level: "error",
|
|
6006
|
+
message: `${error}`
|
|
6007
|
+
});
|
|
6008
|
+
if (error instanceof import_utils33.AppError) {
|
|
6009
|
+
throw error;
|
|
6010
|
+
}
|
|
6011
|
+
throw new import_utils33.InternalServerError("Failed to add subscription transaction.");
|
|
6012
|
+
}
|
|
6013
|
+
}
|
|
6014
|
+
async function getAll({ page = 1, limit = 10, search = "", id = "" } = {}) {
|
|
6015
|
+
page = page > 0 ? page - 1 : page;
|
|
6016
|
+
const query = {};
|
|
6017
|
+
const cacheKeyOptions = {
|
|
6018
|
+
page,
|
|
6019
|
+
limit,
|
|
6020
|
+
search,
|
|
6021
|
+
tag: "getAll"
|
|
6022
|
+
};
|
|
6023
|
+
try {
|
|
6024
|
+
query.subscription = new import_mongodb20.ObjectId(id);
|
|
6025
|
+
cacheKeyOptions.subscription = id;
|
|
6026
|
+
} catch (error) {
|
|
6027
|
+
throw new import_utils33.InternalServerError("Invalid organization ID.");
|
|
6028
|
+
}
|
|
6029
|
+
if (search) {
|
|
6030
|
+
query.$text = { $search: search };
|
|
6031
|
+
}
|
|
6032
|
+
const cacheKey = (0, import_utils33.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
6033
|
+
try {
|
|
6034
|
+
const cachedData = await getCache(
|
|
6035
|
+
cacheKey
|
|
6036
|
+
);
|
|
6037
|
+
if (cachedData) {
|
|
6038
|
+
import_utils33.logger.log({
|
|
6039
|
+
level: "info",
|
|
6040
|
+
message: `Cache hit for getAll subscription transaction: ${cacheKey}`
|
|
6041
|
+
});
|
|
6042
|
+
return cachedData;
|
|
6043
|
+
}
|
|
6044
|
+
const items = await collection.aggregate([
|
|
6045
|
+
{ $match: query },
|
|
6046
|
+
{ $skip: page * limit },
|
|
6047
|
+
{ $limit: limit }
|
|
6048
|
+
]).toArray();
|
|
6049
|
+
const length = await collection.countDocuments(query);
|
|
6050
|
+
const data = (0, import_utils33.paginate)(
|
|
6051
|
+
items,
|
|
6052
|
+
page,
|
|
6053
|
+
limit,
|
|
6054
|
+
length
|
|
6055
|
+
);
|
|
6056
|
+
setCache(cacheKey, data).then(() => {
|
|
6057
|
+
import_utils33.logger.log({
|
|
6058
|
+
level: "info",
|
|
6059
|
+
message: `Cache set for getAll subscription transaction: ${cacheKey}`
|
|
6060
|
+
});
|
|
6061
|
+
}).catch((err) => {
|
|
6062
|
+
import_utils33.logger.log({
|
|
6063
|
+
level: "error",
|
|
6064
|
+
message: `Failed to set cache for getAll subscription transaction: ${err.message}`
|
|
6065
|
+
});
|
|
6066
|
+
});
|
|
6067
|
+
return data;
|
|
6068
|
+
} catch (error) {
|
|
6069
|
+
throw new import_utils33.InternalServerError("Failed to get subscription transactions.");
|
|
6070
|
+
}
|
|
6071
|
+
}
|
|
6072
|
+
return {
|
|
6073
|
+
add,
|
|
6074
|
+
getAll
|
|
6075
|
+
};
|
|
6076
|
+
}
|
|
6077
|
+
|
|
6078
|
+
// src/resources/subscription/subscription.service.ts
|
|
6079
|
+
function useSubscriptionService() {
|
|
6080
|
+
const { getById, updateById } = useSubscriptionRepo();
|
|
6081
|
+
const { add: addTransaction } = useSubscriptionTransactionRepo();
|
|
6082
|
+
const { getUserById } = useUserRepo();
|
|
6083
|
+
async function updateSeats({
|
|
6084
|
+
id = "",
|
|
6085
|
+
user = "",
|
|
6086
|
+
amount = 0,
|
|
6087
|
+
seats = 0
|
|
6088
|
+
} = {}) {
|
|
6089
|
+
const { error } = schemaSubscriptionSeats.validate({
|
|
6090
|
+
id,
|
|
6091
|
+
seats,
|
|
6092
|
+
amount,
|
|
6093
|
+
user
|
|
6094
|
+
});
|
|
6095
|
+
if (error) {
|
|
6096
|
+
throw new import_utils34.BadRequestError(error.message);
|
|
6097
|
+
}
|
|
6098
|
+
const subscription = await getById(id);
|
|
6099
|
+
if (!subscription) {
|
|
6100
|
+
throw new Error("Subscription not found");
|
|
6101
|
+
}
|
|
6102
|
+
if (subscription.seats === seats) {
|
|
6103
|
+
throw new Error("Failed to update subscription, no changes detected.");
|
|
6104
|
+
}
|
|
6105
|
+
const userData = await getUserById(user);
|
|
6106
|
+
if (!userData) {
|
|
6107
|
+
throw new Error("User not found.");
|
|
6108
|
+
}
|
|
6109
|
+
const session = import_utils34.useAtlas.getClient()?.startSession();
|
|
6110
|
+
if (!session) {
|
|
6111
|
+
throw new Error("Unable to start database session.");
|
|
6112
|
+
}
|
|
6113
|
+
let subscriptionAmount = subscription.amount;
|
|
6114
|
+
const seatIncreased = seats > subscription.paidSeats;
|
|
6115
|
+
let paidSeats = subscription.paidSeats;
|
|
6116
|
+
if (seatIncreased) {
|
|
6117
|
+
subscriptionAmount += amount ?? 0;
|
|
6118
|
+
paidSeats = seats;
|
|
6119
|
+
}
|
|
6120
|
+
try {
|
|
6121
|
+
session.startTransaction();
|
|
6122
|
+
await updateById(
|
|
6123
|
+
id,
|
|
6124
|
+
{ seats, amount: subscriptionAmount, paidSeats },
|
|
6125
|
+
session
|
|
6126
|
+
);
|
|
6127
|
+
if (seatIncreased) {
|
|
6128
|
+
await addTransaction(
|
|
6129
|
+
{
|
|
6130
|
+
type: "add-seat",
|
|
6131
|
+
description: `Added ${seats - subscription.paidSeats} seats.`,
|
|
6132
|
+
amount: amount ?? 0,
|
|
6133
|
+
currency: subscription.currency,
|
|
6134
|
+
subscription: id,
|
|
6135
|
+
createdBy: user,
|
|
6136
|
+
createdByName: `${userData.firstName} ${userData.lastName}`
|
|
6137
|
+
},
|
|
6138
|
+
session
|
|
6139
|
+
);
|
|
6140
|
+
}
|
|
6141
|
+
await session.commitTransaction();
|
|
6142
|
+
return "Successfully updated subscription seats.";
|
|
6143
|
+
} catch (error2) {
|
|
6144
|
+
await session.abortTransaction();
|
|
6145
|
+
throw error2;
|
|
6146
|
+
} finally {
|
|
6147
|
+
session.endSession();
|
|
6148
|
+
}
|
|
6149
|
+
}
|
|
6150
|
+
return {
|
|
6151
|
+
updateSeats
|
|
6152
|
+
};
|
|
6153
|
+
}
|
|
6154
|
+
|
|
6155
|
+
// src/resources/subscription/subscription.controller.ts
|
|
6156
|
+
function useSubscriptionController() {
|
|
6157
|
+
const {
|
|
6158
|
+
getAll: _getAll,
|
|
6159
|
+
getById: _getById,
|
|
6160
|
+
getByOrg: _getByOrg
|
|
6161
|
+
} = useSubscriptionRepo();
|
|
6162
|
+
const { updateSeats: _updateSeats } = useSubscriptionService();
|
|
6163
|
+
async function getAll(req, res, next) {
|
|
6164
|
+
const validation = import_joi24.default.object({
|
|
6165
|
+
page: import_joi24.default.number().min(1).max(100).optional().allow(null, "").default(1),
|
|
6166
|
+
limit: import_joi24.default.number().min(1).max(100).optional().allow(null, "").default(10),
|
|
6167
|
+
status: import_joi24.default.string().valid("active", "suspended").optional().default("active")
|
|
6168
|
+
});
|
|
6169
|
+
const query = req.query;
|
|
6170
|
+
const { error, value } = validation.validate(query);
|
|
6171
|
+
if (error) {
|
|
6172
|
+
next(new import_utils35.BadRequestError(error.message));
|
|
6173
|
+
return;
|
|
6174
|
+
}
|
|
6175
|
+
try {
|
|
6176
|
+
const data = await _getAll(value);
|
|
6177
|
+
res.json(data);
|
|
6178
|
+
return;
|
|
6179
|
+
} catch (error2) {
|
|
6180
|
+
next(error2);
|
|
6181
|
+
}
|
|
6182
|
+
}
|
|
6183
|
+
async function getById(req, res, next) {
|
|
6184
|
+
const id = req.params.id ?? "";
|
|
6185
|
+
const validation = import_joi24.default.object({
|
|
6186
|
+
id: import_joi24.default.string().hex().length(24).required()
|
|
6187
|
+
});
|
|
6188
|
+
const { error, value } = validation.validate({ id });
|
|
6189
|
+
if (error) {
|
|
6190
|
+
next(new import_utils35.BadRequestError(error.message));
|
|
6191
|
+
return;
|
|
6192
|
+
}
|
|
6193
|
+
try {
|
|
6194
|
+
const data = await _getById(value.id);
|
|
6195
|
+
res.json(data);
|
|
6196
|
+
return;
|
|
6197
|
+
} catch (error2) {
|
|
6198
|
+
next(error2);
|
|
6199
|
+
}
|
|
6200
|
+
}
|
|
6201
|
+
async function getByOrg(req, res, next) {
|
|
6202
|
+
const org = req.params.org ?? "";
|
|
6203
|
+
const validation = import_joi24.default.object({
|
|
6204
|
+
org: import_joi24.default.string().hex().length(24).required()
|
|
6205
|
+
});
|
|
6206
|
+
const { error, value } = validation.validate({ org });
|
|
6207
|
+
if (error) {
|
|
6208
|
+
next(new import_utils35.BadRequestError(error.message));
|
|
6209
|
+
return;
|
|
6210
|
+
}
|
|
6211
|
+
try {
|
|
6212
|
+
const data = await _getByOrg(value.org);
|
|
6213
|
+
res.json(data);
|
|
6214
|
+
return;
|
|
6215
|
+
} catch (error2) {
|
|
6216
|
+
next(error2);
|
|
6217
|
+
}
|
|
6218
|
+
}
|
|
6219
|
+
async function updateSeats(req, res, next) {
|
|
6220
|
+
const id = req.params.id ?? "";
|
|
6221
|
+
const payload = req.body;
|
|
6222
|
+
const { error } = schemaSubscriptionSeats.validate({ ...payload, id });
|
|
6223
|
+
if (error) {
|
|
6224
|
+
next(new import_utils35.BadRequestError(error.message));
|
|
6225
|
+
return;
|
|
6226
|
+
}
|
|
6227
|
+
const user = payload.user ?? "";
|
|
6228
|
+
const seats = payload.seats ?? 0;
|
|
6229
|
+
const amount = payload.amount ?? 0;
|
|
6230
|
+
try {
|
|
6231
|
+
const message = await _updateSeats({ id, seats, amount, user });
|
|
6232
|
+
res.json({ message });
|
|
6233
|
+
return;
|
|
6234
|
+
} catch (error2) {
|
|
6235
|
+
next(error2);
|
|
6236
|
+
}
|
|
6237
|
+
}
|
|
6238
|
+
return {
|
|
6239
|
+
getAll,
|
|
6240
|
+
getById,
|
|
6241
|
+
getByOrg,
|
|
6242
|
+
updateSeats
|
|
6243
|
+
};
|
|
6244
|
+
}
|
|
6245
|
+
|
|
6246
|
+
// src/resources/subscription/subscription.transaction.controller.ts
|
|
6247
|
+
var import_joi25 = __toESM(require("joi"));
|
|
6248
|
+
var import_utils36 = require("@goweekdays/utils");
|
|
6249
|
+
function useSubscriptionTransactionController() {
|
|
6250
|
+
const { getAll: _getAll } = useSubscriptionTransactionRepo();
|
|
6251
|
+
async function getAll(req, res, next) {
|
|
6252
|
+
const validation = import_joi25.default.object({
|
|
6253
|
+
id: import_joi25.default.string().hex().length(24).required(),
|
|
6254
|
+
page: import_joi25.default.number().min(1).max(100).optional().allow(null, "").default(1),
|
|
6255
|
+
limit: import_joi25.default.number().min(1).max(100).optional().allow(null, "").default(10)
|
|
6256
|
+
});
|
|
6257
|
+
const query = req.query;
|
|
6258
|
+
const id = req.params.id ?? "";
|
|
6259
|
+
const { error, value } = validation.validate({ ...query, id });
|
|
6260
|
+
if (error) {
|
|
6261
|
+
next(new import_utils36.BadRequestError(error.message));
|
|
6262
|
+
return;
|
|
6263
|
+
}
|
|
6264
|
+
try {
|
|
6265
|
+
const data = await _getAll(value);
|
|
6266
|
+
res.json(data);
|
|
6267
|
+
return;
|
|
6268
|
+
} catch (error2) {
|
|
6269
|
+
next(error2);
|
|
6270
|
+
}
|
|
6271
|
+
}
|
|
6272
|
+
return {
|
|
6273
|
+
getAll
|
|
6274
|
+
};
|
|
6275
|
+
}
|
|
6276
|
+
|
|
6277
|
+
// src/resources/plan/plan.model.ts
|
|
6278
|
+
var import_joi26 = __toESM(require("joi"));
|
|
6279
|
+
var currencies = ["USD", "PHP"];
|
|
6280
|
+
var schemaPlan = import_joi26.default.object({
|
|
6281
|
+
name: import_joi26.default.string().min(3).max(100).required(),
|
|
6282
|
+
description: import_joi26.default.string().max(255).optional().allow("", null),
|
|
6283
|
+
features: import_joi26.default.array().items(import_joi26.default.string().max(100)).optional(),
|
|
6284
|
+
price: import_joi26.default.number().positive().required(),
|
|
6285
|
+
currency: import_joi26.default.string().length(3).allow(...currencies).required(),
|
|
6286
|
+
default: import_joi26.default.boolean().optional().allow(null, ""),
|
|
6287
|
+
billingCycle: import_joi26.default.string().valid("monthly", "yearly").required()
|
|
6288
|
+
});
|
|
6289
|
+
function modelPlan(data) {
|
|
6290
|
+
const { error } = schemaPlan.validate(data);
|
|
6291
|
+
if (error) {
|
|
6292
|
+
throw new Error(`Invalid plan data: ${error.message}`);
|
|
6293
|
+
}
|
|
6294
|
+
return {
|
|
6295
|
+
_id: data._id,
|
|
6296
|
+
name: data.name,
|
|
6297
|
+
description: data.description,
|
|
6298
|
+
features: data.features,
|
|
6299
|
+
price: data.price,
|
|
6300
|
+
currency: data.currency,
|
|
6301
|
+
billingCycle: data.billingCycle,
|
|
6302
|
+
status: data.status ?? "active",
|
|
6303
|
+
default: data.default ?? false,
|
|
6304
|
+
createdAt: data.createdAt ?? /* @__PURE__ */ new Date(),
|
|
6305
|
+
updatedAt: data.updatedAt ?? ""
|
|
6306
|
+
};
|
|
6307
|
+
}
|
|
6308
|
+
|
|
6309
|
+
// src/resources/plan/plan.repository.ts
|
|
6310
|
+
var import_utils37 = require("@goweekdays/utils");
|
|
6311
|
+
var import_joi27 = __toESM(require("joi"));
|
|
6312
|
+
var import_mongodb21 = require("mongodb");
|
|
6313
|
+
function usePlanRepo() {
|
|
6314
|
+
const db = import_utils37.useAtlas.getDb();
|
|
6315
|
+
if (!db) {
|
|
6316
|
+
throw new import_utils37.InternalServerError("Unable to connect to server.");
|
|
6317
|
+
}
|
|
6318
|
+
const namespace_collection = "plans";
|
|
6319
|
+
const collection = db.collection(namespace_collection);
|
|
6320
|
+
const { getCache, setCache, delNamespace } = (0, import_utils37.useCache)(namespace_collection);
|
|
6321
|
+
function delCachedData() {
|
|
6322
|
+
delNamespace().then(() => {
|
|
6323
|
+
import_utils37.logger.log({
|
|
6324
|
+
level: "info",
|
|
6325
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
6326
|
+
});
|
|
6327
|
+
}).catch((err) => {
|
|
6328
|
+
import_utils37.logger.log({
|
|
6329
|
+
level: "error",
|
|
6330
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
6331
|
+
});
|
|
6332
|
+
});
|
|
6333
|
+
}
|
|
6334
|
+
async function createIndexes() {
|
|
6335
|
+
try {
|
|
6336
|
+
await collection.createIndexes([
|
|
6337
|
+
{
|
|
6338
|
+
key: {
|
|
6339
|
+
status: 1
|
|
6340
|
+
}
|
|
6341
|
+
},
|
|
6342
|
+
{
|
|
6343
|
+
key: {
|
|
6344
|
+
name: 1
|
|
6345
|
+
}
|
|
6346
|
+
},
|
|
6347
|
+
{
|
|
6348
|
+
key: { billingCycle: 1 }
|
|
6349
|
+
},
|
|
6350
|
+
{
|
|
6351
|
+
key: { name: "text", description: "text" },
|
|
6352
|
+
name: "name_description_text_index"
|
|
6353
|
+
},
|
|
6354
|
+
{
|
|
6355
|
+
key: { name: 1, status: 1 },
|
|
6356
|
+
partialFilterExpression: { status: "active" },
|
|
6357
|
+
unique: true,
|
|
6358
|
+
name: "unique_name_index"
|
|
6359
|
+
},
|
|
6360
|
+
{
|
|
6361
|
+
key: { default: 1 },
|
|
6362
|
+
unique: true,
|
|
6363
|
+
partialFilterExpression: { status: "active" }
|
|
6364
|
+
}
|
|
6365
|
+
]);
|
|
6366
|
+
} catch (error) {
|
|
6367
|
+
}
|
|
6368
|
+
}
|
|
6369
|
+
createIndexes().catch((error) => {
|
|
6370
|
+
import_utils37.logger.log({
|
|
6371
|
+
level: "error",
|
|
6372
|
+
message: `Failed to create indexes for plans collection: ${error.message}`
|
|
6373
|
+
});
|
|
6374
|
+
});
|
|
6375
|
+
async function add(value) {
|
|
6376
|
+
try {
|
|
6377
|
+
value = modelPlan(value);
|
|
6378
|
+
await collection.insertOne(value);
|
|
6379
|
+
delCachedData();
|
|
6380
|
+
return "Successfully added plan.";
|
|
6381
|
+
} catch (error) {
|
|
6382
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
6383
|
+
if (isDuplicated) {
|
|
6384
|
+
throw new import_utils37.BadRequestError("Plan name already exist.");
|
|
6385
|
+
}
|
|
6386
|
+
throw new import_utils37.InternalServerError("Failed to add plan.");
|
|
6387
|
+
}
|
|
6388
|
+
}
|
|
6389
|
+
async function getAll({
|
|
6390
|
+
page = 1,
|
|
6391
|
+
limit = 10,
|
|
6392
|
+
search = "",
|
|
6393
|
+
status = "active"
|
|
6394
|
+
} = {}) {
|
|
6395
|
+
page = page < 1 ? page - 1 : page;
|
|
6396
|
+
const query = { status };
|
|
6397
|
+
const cacheKeyOptions = {
|
|
6398
|
+
page,
|
|
6399
|
+
limit,
|
|
6400
|
+
search,
|
|
6401
|
+
status,
|
|
6402
|
+
tag: "getAll"
|
|
6403
|
+
};
|
|
6404
|
+
if (search) {
|
|
6405
|
+
query.$text = { $search: search };
|
|
6406
|
+
}
|
|
6407
|
+
const cacheKey = (0, import_utils37.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
6408
|
+
try {
|
|
6409
|
+
const cachedData = await getCache(cacheKey);
|
|
6410
|
+
if (cachedData) {
|
|
6411
|
+
return cachedData;
|
|
6412
|
+
}
|
|
6413
|
+
const items = await collection.aggregate([
|
|
6414
|
+
{ $match: query },
|
|
6415
|
+
{ $skip: page * limit },
|
|
6416
|
+
{ $limit: limit }
|
|
6417
|
+
]).toArray();
|
|
6418
|
+
const length = await collection.countDocuments(query);
|
|
6419
|
+
const data = (0, import_utils37.paginate)(items, page, limit, length);
|
|
6420
|
+
setCache(cacheKey, data).then(() => {
|
|
6421
|
+
import_utils37.logger.log({
|
|
6422
|
+
level: "info",
|
|
6423
|
+
message: `Cache set for getAll plan: ${cacheKey}`
|
|
6424
|
+
});
|
|
6425
|
+
}).catch((err) => {
|
|
6426
|
+
import_utils37.logger.log({
|
|
6427
|
+
level: "error",
|
|
6428
|
+
message: `Failed to set cache for getAll plan: ${err.message}`
|
|
6429
|
+
});
|
|
6430
|
+
});
|
|
6431
|
+
return data;
|
|
6432
|
+
} catch (error) {
|
|
6433
|
+
throw new import_utils37.InternalServerError("Failed to get plans.");
|
|
6434
|
+
}
|
|
6435
|
+
}
|
|
6436
|
+
async function getById(_id) {
|
|
6437
|
+
const { error } = import_joi27.default.string().hex().length(24).required().validate(_id);
|
|
6438
|
+
if (error) {
|
|
6439
|
+
throw new Error(`Invalid plan ID: ${error.message}`);
|
|
6440
|
+
}
|
|
6441
|
+
try {
|
|
6442
|
+
_id = new import_mongodb21.ObjectId(_id);
|
|
6443
|
+
} catch (error2) {
|
|
6444
|
+
throw new import_utils37.BadRequestError("Invalid plan ID.");
|
|
6445
|
+
}
|
|
6446
|
+
try {
|
|
6447
|
+
const cacheKey = (0, import_utils37.makeCacheKey)(namespace_collection, {
|
|
6448
|
+
_id: String(_id),
|
|
6449
|
+
tag: "getById"
|
|
6450
|
+
});
|
|
6451
|
+
const cachedData = await getCache(cacheKey);
|
|
6452
|
+
if (cachedData) {
|
|
6453
|
+
return cachedData;
|
|
6454
|
+
}
|
|
6455
|
+
const data = await collection.findOne({
|
|
6456
|
+
_id,
|
|
6457
|
+
status: { $ne: "deleted" }
|
|
6458
|
+
});
|
|
6459
|
+
setCache(cacheKey, data).then(() => {
|
|
6460
|
+
import_utils37.logger.log({
|
|
6461
|
+
level: "info",
|
|
6462
|
+
message: `Cache set for getById plan: ${cacheKey}`
|
|
6463
|
+
});
|
|
6464
|
+
}).catch((err) => {
|
|
6465
|
+
import_utils37.logger.log({
|
|
6466
|
+
level: "error",
|
|
6467
|
+
message: `Failed to set cache for getById plan: ${err.message}`
|
|
6468
|
+
});
|
|
6469
|
+
});
|
|
6470
|
+
return data;
|
|
6471
|
+
} catch (error2) {
|
|
6472
|
+
throw new import_utils37.InternalServerError("Failed to get plan.");
|
|
6473
|
+
}
|
|
6474
|
+
}
|
|
6475
|
+
async function getDefault() {
|
|
6476
|
+
try {
|
|
6477
|
+
const cacheKey = (0, import_utils37.makeCacheKey)(namespace_collection, {
|
|
6478
|
+
default: true,
|
|
6479
|
+
tag: "getDefault"
|
|
6480
|
+
});
|
|
6481
|
+
const cachedData = await getCache(cacheKey);
|
|
6482
|
+
if (cachedData) {
|
|
6483
|
+
return cachedData;
|
|
6484
|
+
}
|
|
6485
|
+
const data = await collection.findOne({
|
|
6486
|
+
default: true,
|
|
6487
|
+
status: "active"
|
|
6488
|
+
});
|
|
6489
|
+
setCache(cacheKey, data).then(() => {
|
|
6490
|
+
import_utils37.logger.log({
|
|
6491
|
+
level: "info",
|
|
6492
|
+
message: `Cache set for default plan: ${cacheKey}`
|
|
6493
|
+
});
|
|
6494
|
+
}).catch((err) => {
|
|
6495
|
+
import_utils37.logger.log({
|
|
6496
|
+
level: "error",
|
|
6497
|
+
message: `Failed to set cache for default plan: ${err.message}`
|
|
6498
|
+
});
|
|
6499
|
+
});
|
|
6500
|
+
return data;
|
|
6501
|
+
} catch (error) {
|
|
6502
|
+
throw new import_utils37.InternalServerError("Failed to get default plan.");
|
|
6503
|
+
}
|
|
6504
|
+
}
|
|
6505
|
+
async function deleteById(_id) {
|
|
6506
|
+
const { error } = import_joi27.default.string().hex().length(24).required().validate(_id);
|
|
6507
|
+
if (error) {
|
|
6508
|
+
throw new Error(`Invalid plan ID: ${error.message}`);
|
|
6509
|
+
}
|
|
6510
|
+
try {
|
|
6511
|
+
_id = new import_mongodb21.ObjectId(_id);
|
|
6512
|
+
} catch (error2) {
|
|
6513
|
+
throw new import_utils37.BadRequestError("Invalid plan ID.");
|
|
6514
|
+
}
|
|
6515
|
+
try {
|
|
6516
|
+
const result = await collection.updateOne(
|
|
6517
|
+
{ _id },
|
|
6518
|
+
{ $set: { status: "deleted" } }
|
|
6519
|
+
);
|
|
6520
|
+
if (result.modifiedCount === 0) {
|
|
6521
|
+
throw new import_utils37.InternalServerError("Failed to delete plan.");
|
|
6522
|
+
}
|
|
6523
|
+
delCachedData();
|
|
6524
|
+
return "Successfully deleted plan.";
|
|
6525
|
+
} catch (error2) {
|
|
6526
|
+
if (error2 instanceof import_utils37.AppError) {
|
|
6527
|
+
throw error2;
|
|
6528
|
+
}
|
|
6529
|
+
throw new import_utils37.InternalServerError("Failed to delete plan.");
|
|
6530
|
+
}
|
|
6531
|
+
}
|
|
6532
|
+
return {
|
|
6533
|
+
add,
|
|
6534
|
+
getAll,
|
|
6535
|
+
getById,
|
|
6536
|
+
getDefault,
|
|
6537
|
+
deleteById
|
|
6538
|
+
};
|
|
6539
|
+
}
|
|
6540
|
+
|
|
6541
|
+
// src/resources/plan/plan.service.ts
|
|
6542
|
+
function usePlanService() {
|
|
6543
|
+
const { add } = usePlanRepo();
|
|
6544
|
+
async function addDefaultPlan() {
|
|
6545
|
+
try {
|
|
6546
|
+
await add({
|
|
6547
|
+
name: "Standard",
|
|
6548
|
+
description: "Default standard plan",
|
|
6549
|
+
price: 1e3,
|
|
6550
|
+
currency: "PHP",
|
|
6551
|
+
billingCycle: "monthly",
|
|
6552
|
+
default: true
|
|
6553
|
+
});
|
|
6554
|
+
} catch (error) {
|
|
6555
|
+
throw error;
|
|
6556
|
+
}
|
|
6557
|
+
}
|
|
6558
|
+
return {
|
|
6559
|
+
addDefaultPlan
|
|
6560
|
+
};
|
|
6561
|
+
}
|
|
6562
|
+
|
|
6563
|
+
// src/resources/plan/plan.controller.ts
|
|
6564
|
+
var import_joi28 = __toESM(require("joi"));
|
|
6565
|
+
var import_utils38 = require("@goweekdays/utils");
|
|
6566
|
+
function usePlanController() {
|
|
6567
|
+
const {
|
|
6568
|
+
add: _add,
|
|
6569
|
+
getAll: _getAll,
|
|
6570
|
+
getById: _getById,
|
|
6571
|
+
deleteById: _deleteById,
|
|
6572
|
+
getDefault: _getDefault
|
|
6573
|
+
} = usePlanRepo();
|
|
6574
|
+
async function add(req, res, next) {
|
|
6575
|
+
const value = req.body;
|
|
6576
|
+
const validation = import_joi28.default.object({
|
|
6577
|
+
name: import_joi28.default.string().min(3).max(100).required(),
|
|
6578
|
+
description: import_joi28.default.string().max(255).optional().allow("", null),
|
|
6579
|
+
features: import_joi28.default.array().items(import_joi28.default.string().max(100)).optional(),
|
|
6580
|
+
price: import_joi28.default.number().positive().required(),
|
|
6581
|
+
currency: import_joi28.default.string().length(3).required(),
|
|
6582
|
+
billingCycle: import_joi28.default.string().valid("monthly", "yearly").required()
|
|
6583
|
+
});
|
|
6584
|
+
const { error } = validation.validate(value);
|
|
6585
|
+
if (error) {
|
|
6586
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6587
|
+
return;
|
|
6588
|
+
}
|
|
6589
|
+
try {
|
|
6590
|
+
const message = await _add(value);
|
|
6591
|
+
res.json({ message });
|
|
6592
|
+
return;
|
|
6593
|
+
} catch (error2) {
|
|
6594
|
+
next(error2);
|
|
6595
|
+
}
|
|
6596
|
+
}
|
|
6597
|
+
async function getAll(req, res, next) {
|
|
6598
|
+
const status = req.query.status ?? "active";
|
|
6599
|
+
const search = req.query.search ?? "";
|
|
6600
|
+
const page = Number(req.query.page) ?? 1;
|
|
6601
|
+
const limit = Number(req.query.limit) ?? 10;
|
|
6602
|
+
const validation = import_joi28.default.object({
|
|
6603
|
+
status: import_joi28.default.string().required(),
|
|
6604
|
+
search: import_joi28.default.string().optional().allow("", null),
|
|
6605
|
+
page: import_joi28.default.number().required(),
|
|
6606
|
+
limit: import_joi28.default.number().required()
|
|
6607
|
+
});
|
|
6608
|
+
const { error } = validation.validate({ status, search, page, limit });
|
|
6609
|
+
if (error) {
|
|
6610
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6611
|
+
return;
|
|
6612
|
+
}
|
|
6613
|
+
try {
|
|
6614
|
+
const plans = await _getAll({ status, search, page, limit });
|
|
6615
|
+
res.json(plans);
|
|
6616
|
+
return;
|
|
6617
|
+
} catch (error2) {
|
|
6618
|
+
next(error2);
|
|
6619
|
+
}
|
|
6620
|
+
}
|
|
6621
|
+
async function getById(req, res, next) {
|
|
6622
|
+
const id = req.params.id;
|
|
6623
|
+
const validation = import_joi28.default.string().hex().length(24).required();
|
|
6624
|
+
const { error } = validation.validate(id);
|
|
6625
|
+
if (error) {
|
|
6626
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6627
|
+
return;
|
|
6628
|
+
}
|
|
6629
|
+
try {
|
|
6630
|
+
const plan = await _getById(id);
|
|
6631
|
+
res.json(plan);
|
|
6632
|
+
return;
|
|
6633
|
+
} catch (error2) {
|
|
6634
|
+
next(error2);
|
|
6635
|
+
}
|
|
6636
|
+
}
|
|
6637
|
+
async function deleteById(req, res, next) {
|
|
6638
|
+
const id = req.params.id;
|
|
6639
|
+
const validation = import_joi28.default.string().hex().length(24).required();
|
|
6640
|
+
const { error } = validation.validate(id);
|
|
6641
|
+
if (error) {
|
|
6642
|
+
next(new import_utils38.BadRequestError(error.message));
|
|
6643
|
+
return;
|
|
6644
|
+
}
|
|
6645
|
+
try {
|
|
6646
|
+
const message = await _deleteById(id);
|
|
6647
|
+
res.json({ message });
|
|
6648
|
+
return;
|
|
6649
|
+
} catch (error2) {
|
|
6650
|
+
next(error2);
|
|
6651
|
+
}
|
|
6652
|
+
}
|
|
6653
|
+
async function getDefault(req, res, next) {
|
|
6654
|
+
try {
|
|
6655
|
+
const plan = await _getDefault();
|
|
6656
|
+
res.json(plan);
|
|
6657
|
+
return;
|
|
6658
|
+
} catch (error) {
|
|
6659
|
+
next(error);
|
|
6660
|
+
}
|
|
6661
|
+
}
|
|
6662
|
+
return {
|
|
6663
|
+
add,
|
|
6664
|
+
getAll,
|
|
6665
|
+
getById,
|
|
6666
|
+
deleteById,
|
|
6667
|
+
getDefault
|
|
6668
|
+
};
|
|
6669
|
+
}
|
|
6670
|
+
|
|
6671
|
+
// src/resources/organization/organization.service.ts
|
|
6672
|
+
function useOrgService() {
|
|
6673
|
+
const { add: addOrg } = useOrgRepo();
|
|
6674
|
+
const { addRole } = useRoleRepo();
|
|
6675
|
+
const { getAll: getAllPermission } = usePermissionRepo();
|
|
6676
|
+
const { add: addMember, updateRoleById: _updateRoleById } = useMemberRepo();
|
|
6677
|
+
const { getUserById } = useUserRepo();
|
|
6678
|
+
const { getDefault } = usePlanRepo();
|
|
6679
|
+
const { add: addSubscription } = useSubscriptionRepo();
|
|
6680
|
+
const { add: addSubscriptionTransaction } = useSubscriptionTransactionRepo();
|
|
6681
|
+
async function add(value) {
|
|
6682
|
+
const { error } = schemaOrgAdd.validate(value);
|
|
6683
|
+
if (error) {
|
|
6684
|
+
throw new import_utils39.BadRequestError(error.message);
|
|
6685
|
+
}
|
|
6686
|
+
const session = import_utils39.useAtlas.getClient()?.startSession();
|
|
6687
|
+
if (!session) {
|
|
6688
|
+
throw new import_utils39.BadRequestError("Unable to start database session.");
|
|
6689
|
+
}
|
|
6690
|
+
try {
|
|
6691
|
+
session?.startTransaction();
|
|
6692
|
+
const org = await addOrg(
|
|
6693
|
+
{
|
|
6694
|
+
email: value.email,
|
|
6695
|
+
name: value.name,
|
|
6696
|
+
contact: value.contact,
|
|
6697
|
+
createdBy: value.createdBy
|
|
6698
|
+
},
|
|
6699
|
+
session
|
|
6700
|
+
);
|
|
6701
|
+
const plan = await getDefault();
|
|
6702
|
+
if (!plan) {
|
|
6703
|
+
throw new import_utils39.BadRequestError(
|
|
6704
|
+
"Failed to create organization, plan not found."
|
|
6705
|
+
);
|
|
6706
|
+
}
|
|
6707
|
+
const currentDate = /* @__PURE__ */ new Date();
|
|
6708
|
+
const nextBillingDate = new Date(currentDate);
|
|
6709
|
+
nextBillingDate.setMonth(currentDate.getMonth() + 1);
|
|
6710
|
+
const amount = plan.price * value.seats;
|
|
6711
|
+
const subscriptionId = await addSubscription(
|
|
6712
|
+
{
|
|
6713
|
+
amount,
|
|
6714
|
+
org: String(org),
|
|
6715
|
+
seats: value.seats,
|
|
6716
|
+
paidSeats: value.seats,
|
|
6717
|
+
currency: plan.currency,
|
|
6718
|
+
billingCycle: plan.billingCycle,
|
|
6719
|
+
nextBillingDate
|
|
6720
|
+
},
|
|
6721
|
+
session
|
|
6722
|
+
);
|
|
6723
|
+
const createdBy = String(value.createdBy);
|
|
6724
|
+
const user = await getUserById(createdBy);
|
|
6725
|
+
if (!user) {
|
|
6726
|
+
throw new import_utils39.BadRequestError("User is required to create org member.");
|
|
6727
|
+
}
|
|
6728
|
+
await addSubscriptionTransaction(
|
|
6729
|
+
{
|
|
6730
|
+
subscription: subscriptionId,
|
|
6731
|
+
type: "initiate",
|
|
6732
|
+
amount,
|
|
6733
|
+
currency: plan.currency,
|
|
6734
|
+
description: "Initial subscription transaction",
|
|
6735
|
+
createdBy: value.createdBy,
|
|
6736
|
+
createdByName: `${user.firstName} ${user.lastName}`
|
|
6737
|
+
},
|
|
6738
|
+
session
|
|
6739
|
+
);
|
|
6740
|
+
const allPermissions = await getAllPermission({
|
|
6741
|
+
app: "org",
|
|
6742
|
+
limit: 100
|
|
6743
|
+
});
|
|
6744
|
+
let permissions = [];
|
|
6745
|
+
if (allPermissions && allPermissions.items && allPermissions.items.length) {
|
|
6746
|
+
permissions = allPermissions.items.map((perm) => perm.key);
|
|
6747
|
+
}
|
|
6748
|
+
if (permissions.length === 0) {
|
|
6749
|
+
throw new Error("No permissions found for the organization type.");
|
|
6750
|
+
}
|
|
6751
|
+
const roleData = {
|
|
6752
|
+
org: String(org),
|
|
6753
|
+
name: "Owner",
|
|
6754
|
+
description: "Owner of the organization",
|
|
6755
|
+
permissions,
|
|
6756
|
+
createdBy,
|
|
6757
|
+
app: "org"
|
|
6758
|
+
};
|
|
6759
|
+
const role = await addRole(roleData, session);
|
|
6760
|
+
if (!role) {
|
|
6761
|
+
throw new import_utils39.BadRequestError("Role is required to create org member.");
|
|
6762
|
+
}
|
|
6763
|
+
await addMember(
|
|
6764
|
+
{
|
|
6765
|
+
role: String(role),
|
|
6766
|
+
roleName: roleData.name,
|
|
6767
|
+
org: String(org),
|
|
6768
|
+
orgName: value.name,
|
|
6769
|
+
name: `${user.firstName} ${user.lastName}`,
|
|
6770
|
+
user: createdBy,
|
|
6771
|
+
app: "org"
|
|
6772
|
+
},
|
|
6773
|
+
session
|
|
6774
|
+
);
|
|
6775
|
+
await session?.commitTransaction();
|
|
6776
|
+
return String(org);
|
|
6777
|
+
} catch (error2) {
|
|
6778
|
+
await session?.abortTransaction();
|
|
6779
|
+
throw error2;
|
|
6780
|
+
} finally {
|
|
6781
|
+
await session?.endSession();
|
|
6782
|
+
}
|
|
6783
|
+
}
|
|
6784
|
+
return {
|
|
6785
|
+
add
|
|
6786
|
+
};
|
|
6787
|
+
}
|
|
6788
|
+
|
|
6789
|
+
// src/resources/organization/organization.controller.ts
|
|
6790
|
+
var import_utils40 = require("@goweekdays/utils");
|
|
6791
|
+
var import_joi29 = __toESM(require("joi"));
|
|
6792
|
+
function useOrgController() {
|
|
6793
|
+
const { add: _add } = useOrgService();
|
|
6794
|
+
const { getOrgsByMembership } = useMemberRepo();
|
|
6795
|
+
const {
|
|
6796
|
+
getByName: _getByName,
|
|
6797
|
+
getAll: getAllOrg,
|
|
6798
|
+
getById: _getById,
|
|
6799
|
+
updateById: _updateById
|
|
6800
|
+
} = useOrgRepo();
|
|
6801
|
+
async function add(req, res, next) {
|
|
6802
|
+
const value = req.body;
|
|
6803
|
+
const { error } = schemaOrgAdd.validate(value);
|
|
6804
|
+
if (error) {
|
|
6805
|
+
next(new import_utils40.BadRequestError(error.message));
|
|
6806
|
+
return;
|
|
6807
|
+
}
|
|
6808
|
+
try {
|
|
6809
|
+
const org = await _add(value);
|
|
6810
|
+
res.json({
|
|
6811
|
+
message: "Organization created successfully.",
|
|
6812
|
+
data: { org }
|
|
6813
|
+
});
|
|
6814
|
+
return;
|
|
6815
|
+
} catch (error2) {
|
|
6816
|
+
next(error2);
|
|
6817
|
+
}
|
|
6818
|
+
}
|
|
6819
|
+
async function getOrgsByUserId(req, res, next) {
|
|
6820
|
+
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
6821
|
+
const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
|
|
6822
|
+
const search = req.query.search ?? "";
|
|
6823
|
+
const user = req.params.user ?? "";
|
|
6824
|
+
const isPageNumber = isFinite(page);
|
|
6825
|
+
if (!isPageNumber) {
|
|
6826
|
+
next(new import_utils40.BadRequestError("Invalid page number."));
|
|
6827
|
+
return;
|
|
6828
|
+
}
|
|
6829
|
+
const isLimitNumber = isFinite(limit);
|
|
6830
|
+
if (!isLimitNumber) {
|
|
6831
|
+
next(new import_utils40.BadRequestError("Invalid limit number."));
|
|
6832
|
+
return;
|
|
6833
|
+
}
|
|
6834
|
+
const validation = import_joi29.default.object({
|
|
6835
|
+
user: import_joi29.default.string().hex().required(),
|
|
6836
|
+
page: import_joi29.default.number().min(1).optional().allow("", null),
|
|
6837
|
+
limit: import_joi29.default.number().min(1).optional().allow("", null),
|
|
6838
|
+
search: import_joi29.default.string().optional().allow("", null)
|
|
6839
|
+
});
|
|
6840
|
+
const { error } = validation.validate({ user, page, limit, search });
|
|
6841
|
+
if (error) {
|
|
6842
|
+
next(new import_utils40.BadRequestError(error.message));
|
|
6843
|
+
return;
|
|
6844
|
+
}
|
|
6845
|
+
try {
|
|
6846
|
+
const orgs = await getOrgsByMembership({ user, page, limit, search });
|
|
6847
|
+
res.json(orgs);
|
|
6848
|
+
return;
|
|
6849
|
+
} catch (error2) {
|
|
6850
|
+
next(error2);
|
|
6851
|
+
}
|
|
6852
|
+
}
|
|
6853
|
+
async function getAll(req, res, next) {
|
|
6854
|
+
const query = req.query;
|
|
6855
|
+
const validation = import_joi29.default.object({
|
|
6856
|
+
page: import_joi29.default.number().min(1).optional().allow("", null),
|
|
6857
|
+
limit: import_joi29.default.number().min(1).optional().allow("", null),
|
|
6858
|
+
search: import_joi29.default.string().optional().allow("", null),
|
|
6859
|
+
status: import_joi29.default.string().valid("active", "suspended", "inactive", "deleted").optional()
|
|
6860
|
+
});
|
|
6861
|
+
const { error } = validation.validate(query);
|
|
6862
|
+
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
6863
|
+
const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
|
|
6864
|
+
const search = req.query.search ?? "";
|
|
6865
|
+
const status = req.query.status ?? "active";
|
|
6866
|
+
const isPageNumber = isFinite(page);
|
|
6867
|
+
if (!isPageNumber) {
|
|
6868
|
+
next(new import_utils40.BadRequestError("Invalid page number."));
|
|
6869
|
+
return;
|
|
6870
|
+
}
|
|
6871
|
+
const isLimitNumber = isFinite(limit);
|
|
6872
|
+
if (!isLimitNumber) {
|
|
6873
|
+
next(new import_utils40.BadRequestError("Invalid limit number."));
|
|
6874
|
+
return;
|
|
6875
|
+
}
|
|
6876
|
+
if (error) {
|
|
6877
|
+
next(new import_utils40.BadRequestError(error.message));
|
|
6878
|
+
return;
|
|
6879
|
+
}
|
|
6880
|
+
try {
|
|
6881
|
+
const orgs = await getAllOrg({ page, limit, search, status });
|
|
6882
|
+
res.json(orgs);
|
|
6883
|
+
return;
|
|
6884
|
+
} catch (error2) {
|
|
6885
|
+
next(error2);
|
|
6886
|
+
}
|
|
5599
6887
|
}
|
|
5600
6888
|
async function getByName(req, res, next) {
|
|
5601
6889
|
const name = req.params.name;
|
|
5602
|
-
const validation =
|
|
5603
|
-
name:
|
|
6890
|
+
const validation = import_joi29.default.object({
|
|
6891
|
+
name: import_joi29.default.string().required()
|
|
5604
6892
|
});
|
|
5605
6893
|
const { error } = validation.validate({ name });
|
|
5606
6894
|
if (error) {
|
|
5607
|
-
next(new
|
|
6895
|
+
next(new import_utils40.BadRequestError(error.message));
|
|
5608
6896
|
return;
|
|
5609
6897
|
}
|
|
5610
6898
|
try {
|
|
@@ -5617,12 +6905,12 @@ function useOrgController() {
|
|
|
5617
6905
|
}
|
|
5618
6906
|
async function getById(req, res, next) {
|
|
5619
6907
|
const id = req.params.id;
|
|
5620
|
-
const validation =
|
|
5621
|
-
id:
|
|
6908
|
+
const validation = import_joi29.default.object({
|
|
6909
|
+
id: import_joi29.default.string().hex().required()
|
|
5622
6910
|
});
|
|
5623
6911
|
const { error } = validation.validate({ id });
|
|
5624
6912
|
if (error) {
|
|
5625
|
-
next(new
|
|
6913
|
+
next(new import_utils40.BadRequestError(error.message));
|
|
5626
6914
|
return;
|
|
5627
6915
|
}
|
|
5628
6916
|
try {
|
|
@@ -5633,12 +6921,29 @@ function useOrgController() {
|
|
|
5633
6921
|
next(error2);
|
|
5634
6922
|
}
|
|
5635
6923
|
}
|
|
6924
|
+
async function updateById(req, res, next) {
|
|
6925
|
+
const _id = req.params.id ?? "";
|
|
6926
|
+
const payload = req.body;
|
|
6927
|
+
const { error } = schemaOrgUpdate.validate({ _id, ...payload });
|
|
6928
|
+
if (error) {
|
|
6929
|
+
next(new import_utils40.BadRequestError(error.message));
|
|
6930
|
+
return;
|
|
6931
|
+
}
|
|
6932
|
+
try {
|
|
6933
|
+
const message = await _updateById(_id, payload);
|
|
6934
|
+
res.json({ message });
|
|
6935
|
+
return;
|
|
6936
|
+
} catch (error2) {
|
|
6937
|
+
next(error2);
|
|
6938
|
+
}
|
|
6939
|
+
}
|
|
5636
6940
|
return {
|
|
5637
6941
|
add,
|
|
5638
6942
|
getOrgsByUserId,
|
|
5639
6943
|
getByName,
|
|
5640
6944
|
getAll,
|
|
5641
|
-
getById
|
|
6945
|
+
getById,
|
|
6946
|
+
updateById
|
|
5642
6947
|
};
|
|
5643
6948
|
}
|
|
5644
6949
|
|
|
@@ -5657,16 +6962,16 @@ function useUserService() {
|
|
|
5657
6962
|
const { getAll: getAllPermission } = usePermissionRepo();
|
|
5658
6963
|
const { getById: getOrgById } = useOrgRepo();
|
|
5659
6964
|
async function createDefaultUser() {
|
|
5660
|
-
const session =
|
|
6965
|
+
const session = import_utils41.useAtlas.getClient()?.startSession();
|
|
5661
6966
|
try {
|
|
5662
6967
|
session?.startTransaction();
|
|
5663
6968
|
const _user = await getUserByEmail(DEFAULT_USER_EMAIL);
|
|
5664
6969
|
if (_user) {
|
|
5665
|
-
throw new
|
|
6970
|
+
throw new import_utils41.BadRequestError(
|
|
5666
6971
|
`User already exists: ${DEFAULT_USER_EMAIL}.`
|
|
5667
6972
|
);
|
|
5668
6973
|
}
|
|
5669
|
-
const hashedPassword = await (0,
|
|
6974
|
+
const hashedPassword = await (0, import_utils41.hashPassword)(DEFAULT_USER_PASSWORD);
|
|
5670
6975
|
const userId = await addUser(
|
|
5671
6976
|
{
|
|
5672
6977
|
email: DEFAULT_USER_EMAIL,
|
|
@@ -5712,9 +7017,9 @@ function useUserService() {
|
|
|
5712
7017
|
try {
|
|
5713
7018
|
const _user = await getUserByEmail(value.email);
|
|
5714
7019
|
if (_user) {
|
|
5715
|
-
throw new
|
|
7020
|
+
throw new import_utils41.BadRequestError(`User already exists: ${value.email}.`);
|
|
5716
7021
|
}
|
|
5717
|
-
const hashedPassword = await (0,
|
|
7022
|
+
const hashedPassword = await (0, import_utils41.hashPassword)(value.password);
|
|
5718
7023
|
const insertedId = await addUser({
|
|
5719
7024
|
email: value.email,
|
|
5720
7025
|
password: hashedPassword,
|
|
@@ -5724,10 +7029,10 @@ function useUserService() {
|
|
|
5724
7029
|
});
|
|
5725
7030
|
return insertedId;
|
|
5726
7031
|
} catch (error) {
|
|
5727
|
-
if (error instanceof
|
|
7032
|
+
if (error instanceof import_utils41.AppError) {
|
|
5728
7033
|
throw error;
|
|
5729
7034
|
} else {
|
|
5730
|
-
throw new
|
|
7035
|
+
throw new import_utils41.InternalServerError(`Error creating user: ${error}`);
|
|
5731
7036
|
}
|
|
5732
7037
|
}
|
|
5733
7038
|
}
|
|
@@ -5738,28 +7043,28 @@ function useUserService() {
|
|
|
5738
7043
|
lastName = "",
|
|
5739
7044
|
password = ""
|
|
5740
7045
|
} = {}) {
|
|
5741
|
-
const session =
|
|
7046
|
+
const session = import_utils41.useAtlas.getClient()?.startSession();
|
|
5742
7047
|
session?.startTransaction();
|
|
5743
7048
|
try {
|
|
5744
7049
|
const invitation = await _getVerificationById(id);
|
|
5745
7050
|
if (!invitation || !invitation.metadata?.app || !invitation.metadata?.role) {
|
|
5746
|
-
throw new
|
|
7051
|
+
throw new import_utils41.BadRequestError("Invalid invitation.");
|
|
5747
7052
|
}
|
|
5748
7053
|
if (invitation.status === "complete") {
|
|
5749
|
-
throw new
|
|
7054
|
+
throw new import_utils41.BadRequestError("Invitation already used.");
|
|
5750
7055
|
}
|
|
5751
7056
|
if (!invitation.expireAt) {
|
|
5752
|
-
throw new
|
|
7057
|
+
throw new import_utils41.BadRequestError("Expiration date is required.");
|
|
5753
7058
|
}
|
|
5754
7059
|
const expired = new Date(invitation.expireAt) < /* @__PURE__ */ new Date();
|
|
5755
7060
|
if (invitation.status === "expired" || expired) {
|
|
5756
|
-
throw new
|
|
7061
|
+
throw new import_utils41.BadRequestError("Invitation expired.");
|
|
5757
7062
|
}
|
|
5758
7063
|
const email = invitation.email;
|
|
5759
7064
|
const user = await getUserByEmail(invitation.email);
|
|
5760
7065
|
let userId = user?._id ?? "";
|
|
5761
7066
|
if (!user) {
|
|
5762
|
-
const hashedPassword = await (0,
|
|
7067
|
+
const hashedPassword = await (0, import_utils41.hashPassword)(password);
|
|
5763
7068
|
userId = await addUser(
|
|
5764
7069
|
{
|
|
5765
7070
|
email,
|
|
@@ -5776,7 +7081,7 @@ function useUserService() {
|
|
|
5776
7081
|
if (invitation.metadata?.org) {
|
|
5777
7082
|
const org = await getOrgById(String(invitation.metadata.org));
|
|
5778
7083
|
if (!org) {
|
|
5779
|
-
throw new
|
|
7084
|
+
throw new import_utils41.NotFoundError("Organization not found.");
|
|
5780
7085
|
}
|
|
5781
7086
|
orgName = org.name;
|
|
5782
7087
|
}
|
|
@@ -5784,7 +7089,7 @@ function useUserService() {
|
|
|
5784
7089
|
if (invitation.metadata.role) {
|
|
5785
7090
|
const role = await getRoleById(String(invitation.metadata.role));
|
|
5786
7091
|
if (!role) {
|
|
5787
|
-
throw new
|
|
7092
|
+
throw new import_utils41.NotFoundError("Role not found.");
|
|
5788
7093
|
}
|
|
5789
7094
|
roleName = role.name;
|
|
5790
7095
|
}
|
|
@@ -5804,10 +7109,10 @@ function useUserService() {
|
|
|
5804
7109
|
return userId;
|
|
5805
7110
|
} catch (error) {
|
|
5806
7111
|
await session?.abortTransaction();
|
|
5807
|
-
if (error instanceof
|
|
7112
|
+
if (error instanceof import_utils41.AppError) {
|
|
5808
7113
|
throw error;
|
|
5809
7114
|
} else {
|
|
5810
|
-
throw new
|
|
7115
|
+
throw new import_utils41.InternalServerError("Failed to create user by invite.");
|
|
5811
7116
|
}
|
|
5812
7117
|
} finally {
|
|
5813
7118
|
session?.endSession();
|
|
@@ -5819,31 +7124,31 @@ function useUserService() {
|
|
|
5819
7124
|
lastName = "",
|
|
5820
7125
|
password = ""
|
|
5821
7126
|
} = {}) {
|
|
5822
|
-
const session =
|
|
7127
|
+
const session = import_utils41.useAtlas.getClient()?.startSession();
|
|
5823
7128
|
session?.startTransaction();
|
|
5824
7129
|
try {
|
|
5825
7130
|
const signUp = await _getVerificationById(id);
|
|
5826
7131
|
if (!signUp) {
|
|
5827
|
-
throw new
|
|
7132
|
+
throw new import_utils41.BadRequestError("Invalid sign up link.");
|
|
5828
7133
|
}
|
|
5829
7134
|
if (signUp.status === "complete") {
|
|
5830
|
-
throw new
|
|
7135
|
+
throw new import_utils41.BadRequestError(
|
|
5831
7136
|
"You have already an account created using this link."
|
|
5832
7137
|
);
|
|
5833
7138
|
}
|
|
5834
7139
|
if (!signUp.expireAt) {
|
|
5835
|
-
throw new
|
|
7140
|
+
throw new import_utils41.BadRequestError("Expiration date is required.");
|
|
5836
7141
|
}
|
|
5837
7142
|
const expired = new Date(signUp.expireAt) < /* @__PURE__ */ new Date();
|
|
5838
7143
|
if (signUp.status === "expired" || expired) {
|
|
5839
|
-
throw new
|
|
7144
|
+
throw new import_utils41.BadRequestError("Sign up link expired.");
|
|
5840
7145
|
}
|
|
5841
7146
|
const email = signUp.email;
|
|
5842
7147
|
const _user = await getUserByEmail(signUp.email);
|
|
5843
7148
|
if (_user) {
|
|
5844
|
-
throw new
|
|
7149
|
+
throw new import_utils41.BadRequestError(`User already exists: ${email}.`);
|
|
5845
7150
|
}
|
|
5846
|
-
const hashedPassword = await (0,
|
|
7151
|
+
const hashedPassword = await (0, import_utils41.hashPassword)(password);
|
|
5847
7152
|
const userId = await addUser(
|
|
5848
7153
|
{
|
|
5849
7154
|
email,
|
|
@@ -5867,33 +7172,33 @@ function useUserService() {
|
|
|
5867
7172
|
const { updateStatusById: updateVerificationStatusById } = useVerificationRepo();
|
|
5868
7173
|
async function resetPassword(value) {
|
|
5869
7174
|
if (value.newPassword !== value.confirmPassword) {
|
|
5870
|
-
throw new
|
|
7175
|
+
throw new import_utils41.BadRequestError("Passwords do not match.");
|
|
5871
7176
|
}
|
|
5872
7177
|
let hashedPassword = "";
|
|
5873
7178
|
try {
|
|
5874
|
-
hashedPassword = await (0,
|
|
7179
|
+
hashedPassword = await (0, import_utils41.hashPassword)(value.newPassword);
|
|
5875
7180
|
} catch (error) {
|
|
5876
|
-
throw new
|
|
7181
|
+
throw new import_utils41.InternalServerError(`Error hashing password: ${error}`);
|
|
5877
7182
|
}
|
|
5878
|
-
const session =
|
|
7183
|
+
const session = import_utils41.useAtlas.getClient()?.startSession();
|
|
5879
7184
|
if (!session) {
|
|
5880
|
-
throw new
|
|
7185
|
+
throw new import_utils41.InternalServerError("Failed to start database session.");
|
|
5881
7186
|
}
|
|
5882
7187
|
try {
|
|
5883
7188
|
session.startTransaction();
|
|
5884
7189
|
const otpDoc = await getById(value.id);
|
|
5885
7190
|
if (!otpDoc) {
|
|
5886
|
-
throw new
|
|
7191
|
+
throw new import_utils41.NotFoundError("You are using an invalid reset link.");
|
|
5887
7192
|
}
|
|
5888
7193
|
const user = await getUserByEmail(otpDoc.email);
|
|
5889
7194
|
if (!user) {
|
|
5890
|
-
throw new
|
|
7195
|
+
throw new import_utils41.NotFoundError("User not found.");
|
|
5891
7196
|
}
|
|
5892
7197
|
if (!user._id) {
|
|
5893
|
-
throw new
|
|
7198
|
+
throw new import_utils41.NotFoundError("User ID is invalid.");
|
|
5894
7199
|
}
|
|
5895
7200
|
if (otpDoc.status === "used") {
|
|
5896
|
-
throw new
|
|
7201
|
+
throw new import_utils41.BadRequestError("This link has already been invalidated.");
|
|
5897
7202
|
}
|
|
5898
7203
|
await updateVerificationStatusById(value.id, "used", session);
|
|
5899
7204
|
await _updateUserFieldById(
|
|
@@ -5904,31 +7209,31 @@ function useUserService() {
|
|
|
5904
7209
|
return "Successfully reset password.";
|
|
5905
7210
|
} catch (error) {
|
|
5906
7211
|
await session.abortTransaction();
|
|
5907
|
-
if (error instanceof
|
|
7212
|
+
if (error instanceof import_utils41.AppError) {
|
|
5908
7213
|
throw error;
|
|
5909
7214
|
}
|
|
5910
|
-
throw new
|
|
7215
|
+
throw new import_utils41.InternalServerError("Failed to reset password.");
|
|
5911
7216
|
}
|
|
5912
7217
|
}
|
|
5913
7218
|
const { updateName: updateMemberName } = useMemberRepo();
|
|
5914
7219
|
async function updateName(_id, firstName, lastName) {
|
|
5915
7220
|
if (!_id) {
|
|
5916
|
-
throw new
|
|
7221
|
+
throw new import_utils41.BadRequestError("Invalid user ID");
|
|
5917
7222
|
}
|
|
5918
7223
|
if (!firstName) {
|
|
5919
|
-
throw new
|
|
7224
|
+
throw new import_utils41.BadRequestError("Invalid firstName");
|
|
5920
7225
|
}
|
|
5921
7226
|
if (!lastName) {
|
|
5922
|
-
throw new
|
|
7227
|
+
throw new import_utils41.BadRequestError("Invalid lastName");
|
|
5923
7228
|
}
|
|
5924
|
-
const session =
|
|
7229
|
+
const session = import_utils41.useAtlas.getClient()?.startSession();
|
|
5925
7230
|
session?.startTransaction();
|
|
5926
|
-
const cacheKey = (0,
|
|
7231
|
+
const cacheKey = (0, import_utils41.makeCacheKey)("users", { user: _id });
|
|
5927
7232
|
try {
|
|
5928
|
-
(0,
|
|
5929
|
-
|
|
7233
|
+
(0, import_utils41.useCache)().delCache(cacheKey).then(() => {
|
|
7234
|
+
import_utils41.logger.info(`Cache cleared for user: ${_id}`);
|
|
5930
7235
|
}).catch((error) => {
|
|
5931
|
-
|
|
7236
|
+
import_utils41.logger.error(`Failed to clear cache for user: ${_id}`, error);
|
|
5932
7237
|
});
|
|
5933
7238
|
await _updateName({ _id, firstName, lastName }, session);
|
|
5934
7239
|
await updateMemberName(
|
|
@@ -5946,16 +7251,16 @@ function useUserService() {
|
|
|
5946
7251
|
}
|
|
5947
7252
|
async function updateBirthday(_id, month, day, year) {
|
|
5948
7253
|
if (!_id) {
|
|
5949
|
-
throw new
|
|
7254
|
+
throw new import_utils41.BadRequestError("Invalid user ID");
|
|
5950
7255
|
}
|
|
5951
7256
|
if (!month) {
|
|
5952
|
-
throw new
|
|
7257
|
+
throw new import_utils41.BadRequestError("Invalid birth month.");
|
|
5953
7258
|
}
|
|
5954
7259
|
if (!day) {
|
|
5955
|
-
throw new
|
|
7260
|
+
throw new import_utils41.BadRequestError("Invalid birthday.");
|
|
5956
7261
|
}
|
|
5957
7262
|
if (!year) {
|
|
5958
|
-
throw new
|
|
7263
|
+
throw new import_utils41.BadRequestError("Invalid birth year.");
|
|
5959
7264
|
}
|
|
5960
7265
|
try {
|
|
5961
7266
|
await _updateBirthday({ _id, month, day, year });
|
|
@@ -5972,7 +7277,7 @@ function useUserService() {
|
|
|
5972
7277
|
}
|
|
5973
7278
|
}
|
|
5974
7279
|
const { createFile: _createFile, deleteFileById } = useFileRepo();
|
|
5975
|
-
const s3 = new
|
|
7280
|
+
const s3 = new import_utils41.useS3({
|
|
5976
7281
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
5977
7282
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
5978
7283
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -5980,7 +7285,7 @@ function useUserService() {
|
|
|
5980
7285
|
bucket: SPACES_BUCKET
|
|
5981
7286
|
});
|
|
5982
7287
|
async function updateUserProfile({ file, user, previousProfile } = {}) {
|
|
5983
|
-
const session =
|
|
7288
|
+
const session = import_utils41.useAtlas.getClient()?.startSession();
|
|
5984
7289
|
session?.startTransaction();
|
|
5985
7290
|
const _file = {
|
|
5986
7291
|
name: file.originalname,
|
|
@@ -6024,8 +7329,8 @@ function useUserService() {
|
|
|
6024
7329
|
}
|
|
6025
7330
|
|
|
6026
7331
|
// src/resources/user/user.controller.ts
|
|
6027
|
-
var
|
|
6028
|
-
var
|
|
7332
|
+
var import_utils42 = require("@goweekdays/utils");
|
|
7333
|
+
var import_joi30 = __toESM(require("joi"));
|
|
6029
7334
|
function useUserController() {
|
|
6030
7335
|
const {
|
|
6031
7336
|
updateName: _updateName,
|
|
@@ -6041,14 +7346,14 @@ function useUserController() {
|
|
|
6041
7346
|
const status = req.query.status ?? "";
|
|
6042
7347
|
const search = req.query.search ?? "";
|
|
6043
7348
|
const page = Number(req.query.page) ?? 1;
|
|
6044
|
-
const validation =
|
|
6045
|
-
status:
|
|
6046
|
-
search:
|
|
6047
|
-
page:
|
|
7349
|
+
const validation = import_joi30.default.object({
|
|
7350
|
+
status: import_joi30.default.string().required(),
|
|
7351
|
+
search: import_joi30.default.string().optional().allow("", null),
|
|
7352
|
+
page: import_joi30.default.number().required()
|
|
6048
7353
|
});
|
|
6049
7354
|
const { error } = validation.validate({ status, search, page });
|
|
6050
7355
|
if (error) {
|
|
6051
|
-
next(new
|
|
7356
|
+
next(new import_utils42.BadRequestError(error.message));
|
|
6052
7357
|
return;
|
|
6053
7358
|
}
|
|
6054
7359
|
try {
|
|
@@ -6061,14 +7366,14 @@ function useUserController() {
|
|
|
6061
7366
|
}
|
|
6062
7367
|
async function getUserById(req, res, next) {
|
|
6063
7368
|
const id = req.params.id || "";
|
|
6064
|
-
const validation =
|
|
7369
|
+
const validation = import_joi30.default.string().hex().validate(id);
|
|
6065
7370
|
if (validation.error) {
|
|
6066
|
-
throw new
|
|
7371
|
+
throw new import_utils42.BadRequestError("Invalid id.");
|
|
6067
7372
|
}
|
|
6068
7373
|
try {
|
|
6069
7374
|
const user = await _getUserById(id);
|
|
6070
7375
|
if (!user) {
|
|
6071
|
-
throw new
|
|
7376
|
+
throw new import_utils42.BadRequestError("User not found.");
|
|
6072
7377
|
}
|
|
6073
7378
|
res.json(user);
|
|
6074
7379
|
} catch (error) {
|
|
@@ -6079,13 +7384,13 @@ function useUserController() {
|
|
|
6079
7384
|
const id = req.headers.user ?? "";
|
|
6080
7385
|
const firstName = req.body.firstName ?? "";
|
|
6081
7386
|
const lastName = req.body.lastName ?? "";
|
|
6082
|
-
const validation =
|
|
6083
|
-
firstName:
|
|
6084
|
-
lastName:
|
|
7387
|
+
const validation = import_joi30.default.object({
|
|
7388
|
+
firstName: import_joi30.default.string().required(),
|
|
7389
|
+
lastName: import_joi30.default.string().required()
|
|
6085
7390
|
});
|
|
6086
7391
|
const { error } = validation.validate({ firstName, lastName });
|
|
6087
7392
|
if (error) {
|
|
6088
|
-
next(new
|
|
7393
|
+
next(new import_utils42.BadRequestError(error.message));
|
|
6089
7394
|
return;
|
|
6090
7395
|
}
|
|
6091
7396
|
try {
|
|
@@ -6101,14 +7406,14 @@ function useUserController() {
|
|
|
6101
7406
|
const month = req.body.month ?? "";
|
|
6102
7407
|
const day = req.body.day ?? 0;
|
|
6103
7408
|
const year = req.body.year ?? 0;
|
|
6104
|
-
const validation =
|
|
6105
|
-
month:
|
|
6106
|
-
day:
|
|
6107
|
-
year:
|
|
7409
|
+
const validation = import_joi30.default.object({
|
|
7410
|
+
month: import_joi30.default.string().required(),
|
|
7411
|
+
day: import_joi30.default.number().integer().min(1).max(31).required(),
|
|
7412
|
+
year: import_joi30.default.number().integer().min(1900).max((/* @__PURE__ */ new Date()).getFullYear()).required()
|
|
6108
7413
|
});
|
|
6109
7414
|
const { error } = validation.validate({ month, day, year });
|
|
6110
7415
|
if (error) {
|
|
6111
|
-
next(new
|
|
7416
|
+
next(new import_utils42.BadRequestError(error.message));
|
|
6112
7417
|
return;
|
|
6113
7418
|
}
|
|
6114
7419
|
try {
|
|
@@ -6122,18 +7427,18 @@ function useUserController() {
|
|
|
6122
7427
|
async function updateUserFieldById(req, res, next) {
|
|
6123
7428
|
const _id = req.params.id;
|
|
6124
7429
|
const { field, value } = req.body;
|
|
6125
|
-
const validation =
|
|
6126
|
-
_id:
|
|
6127
|
-
field:
|
|
6128
|
-
value:
|
|
7430
|
+
const validation = import_joi30.default.object({
|
|
7431
|
+
_id: import_joi30.default.string().hex().required(),
|
|
7432
|
+
field: import_joi30.default.string().valid("gender", "email", "contact", "profile").required(),
|
|
7433
|
+
value: import_joi30.default.alternatives().conditional("field", {
|
|
6129
7434
|
is: "email",
|
|
6130
|
-
then:
|
|
6131
|
-
otherwise:
|
|
7435
|
+
then: import_joi30.default.string().email().required(),
|
|
7436
|
+
otherwise: import_joi30.default.string().required()
|
|
6132
7437
|
})
|
|
6133
7438
|
});
|
|
6134
7439
|
const { error } = validation.validate({ _id, field, value });
|
|
6135
7440
|
if (error) {
|
|
6136
|
-
next(new
|
|
7441
|
+
next(new import_utils42.BadRequestError(error.message));
|
|
6137
7442
|
return;
|
|
6138
7443
|
}
|
|
6139
7444
|
try {
|
|
@@ -6149,12 +7454,12 @@ function useUserController() {
|
|
|
6149
7454
|
return;
|
|
6150
7455
|
}
|
|
6151
7456
|
const previousProfile = req.body.previousProfile ?? "";
|
|
6152
|
-
const validation =
|
|
6153
|
-
previousProfile:
|
|
7457
|
+
const validation = import_joi30.default.object({
|
|
7458
|
+
previousProfile: import_joi30.default.string().hex().optional().allow("", null)
|
|
6154
7459
|
});
|
|
6155
7460
|
const { error } = validation.validate({ previousProfile });
|
|
6156
7461
|
if (error) {
|
|
6157
|
-
next(new
|
|
7462
|
+
next(new import_utils42.BadRequestError(error.message));
|
|
6158
7463
|
return;
|
|
6159
7464
|
}
|
|
6160
7465
|
const user = req.headers["user"] ?? "";
|
|
@@ -6167,10 +7472,10 @@ function useUserController() {
|
|
|
6167
7472
|
res.json({ message: "Successfully updated profile picture." });
|
|
6168
7473
|
return;
|
|
6169
7474
|
} catch (error2) {
|
|
6170
|
-
if (error2 instanceof
|
|
7475
|
+
if (error2 instanceof import_utils42.AppError) {
|
|
6171
7476
|
next(error2);
|
|
6172
7477
|
} else {
|
|
6173
|
-
next(new
|
|
7478
|
+
next(new import_utils42.InternalServerError(error2));
|
|
6174
7479
|
}
|
|
6175
7480
|
}
|
|
6176
7481
|
}
|
|
@@ -6180,12 +7485,12 @@ function useUserController() {
|
|
|
6180
7485
|
const password = req.body.password ?? "";
|
|
6181
7486
|
const id = req.params.id ?? "";
|
|
6182
7487
|
const type = req.body.type ?? "";
|
|
6183
|
-
const validation =
|
|
6184
|
-
firstName:
|
|
6185
|
-
lastName:
|
|
6186
|
-
password:
|
|
6187
|
-
id:
|
|
6188
|
-
type:
|
|
7488
|
+
const validation = import_joi30.default.object({
|
|
7489
|
+
firstName: import_joi30.default.string().required(),
|
|
7490
|
+
lastName: import_joi30.default.string().required(),
|
|
7491
|
+
password: import_joi30.default.string().required(),
|
|
7492
|
+
id: import_joi30.default.string().hex().required(),
|
|
7493
|
+
type: import_joi30.default.string().required()
|
|
6189
7494
|
});
|
|
6190
7495
|
const { error } = validation.validate({
|
|
6191
7496
|
firstName,
|
|
@@ -6195,7 +7500,7 @@ function useUserController() {
|
|
|
6195
7500
|
type
|
|
6196
7501
|
});
|
|
6197
7502
|
if (error) {
|
|
6198
|
-
next(new
|
|
7503
|
+
next(new import_utils42.BadRequestError(error.message));
|
|
6199
7504
|
return;
|
|
6200
7505
|
}
|
|
6201
7506
|
try {
|
|
@@ -6218,14 +7523,14 @@ function useUserController() {
|
|
|
6218
7523
|
}
|
|
6219
7524
|
async function resetPassword(req, res, next) {
|
|
6220
7525
|
const payload = req.body;
|
|
6221
|
-
const validation =
|
|
6222
|
-
id:
|
|
6223
|
-
newPassword:
|
|
6224
|
-
confirmPassword:
|
|
7526
|
+
const validation = import_joi30.default.object({
|
|
7527
|
+
id: import_joi30.default.string().hex().required(),
|
|
7528
|
+
newPassword: import_joi30.default.string().min(8).required(),
|
|
7529
|
+
confirmPassword: import_joi30.default.string().min(8).required()
|
|
6225
7530
|
});
|
|
6226
7531
|
const { error } = validation.validate(payload);
|
|
6227
7532
|
if (error) {
|
|
6228
|
-
next(new
|
|
7533
|
+
next(new import_utils42.BadRequestError(error.message));
|
|
6229
7534
|
return;
|
|
6230
7535
|
}
|
|
6231
7536
|
try {
|
|
@@ -6250,7 +7555,7 @@ function useUserController() {
|
|
|
6250
7555
|
}
|
|
6251
7556
|
|
|
6252
7557
|
// src/resources/verification/verification.service.ts
|
|
6253
|
-
var
|
|
7558
|
+
var import_joi31 = __toESM(require("joi"));
|
|
6254
7559
|
function useVerificationService() {
|
|
6255
7560
|
const MailerConfig = {
|
|
6256
7561
|
host: MAILER_TRANSPORT_HOST,
|
|
@@ -6259,7 +7564,7 @@ function useVerificationService() {
|
|
|
6259
7564
|
email: MAILER_EMAIL,
|
|
6260
7565
|
password: MAILER_PASSWORD
|
|
6261
7566
|
};
|
|
6262
|
-
const mailer = new
|
|
7567
|
+
const mailer = new import_utils43.useMailer(MailerConfig);
|
|
6263
7568
|
const {
|
|
6264
7569
|
add,
|
|
6265
7570
|
getById: _getById,
|
|
@@ -6285,7 +7590,7 @@ function useVerificationService() {
|
|
|
6285
7590
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
6286
7591
|
};
|
|
6287
7592
|
if (!metadata.app) {
|
|
6288
|
-
throw new
|
|
7593
|
+
throw new import_utils43.BadRequestError("App metadata is required.");
|
|
6289
7594
|
}
|
|
6290
7595
|
try {
|
|
6291
7596
|
const user = await getUserByEmail(email);
|
|
@@ -6293,8 +7598,8 @@ function useVerificationService() {
|
|
|
6293
7598
|
if (user) {
|
|
6294
7599
|
value.type = "member-invite";
|
|
6295
7600
|
const res2 = await add(value);
|
|
6296
|
-
const filePath2 = (0,
|
|
6297
|
-
const emailContent2 = (0,
|
|
7601
|
+
const filePath2 = (0, import_utils43.getDirectory)(dir, "./public/handlebars/member-invite");
|
|
7602
|
+
const emailContent2 = (0, import_utils43.compileHandlebar)({
|
|
6298
7603
|
context: {
|
|
6299
7604
|
validity: VERIFICATION_USER_INVITE_DURATION,
|
|
6300
7605
|
link: `${APP_MAIN}/verify/member-invite/${res2}`,
|
|
@@ -6308,7 +7613,7 @@ function useVerificationService() {
|
|
|
6308
7613
|
html: emailContent2,
|
|
6309
7614
|
from: "GoWeekdays"
|
|
6310
7615
|
}).catch((error) => {
|
|
6311
|
-
|
|
7616
|
+
import_utils43.logger.log({
|
|
6312
7617
|
level: "error",
|
|
6313
7618
|
message: `Error sending user invite email: ${error}`
|
|
6314
7619
|
});
|
|
@@ -6316,8 +7621,8 @@ function useVerificationService() {
|
|
|
6316
7621
|
return res2;
|
|
6317
7622
|
}
|
|
6318
7623
|
const res = await add(value);
|
|
6319
|
-
const filePath = (0,
|
|
6320
|
-
const emailContent = (0,
|
|
7624
|
+
const filePath = (0, import_utils43.getDirectory)(dir, "./public/handlebars/user-invite");
|
|
7625
|
+
const emailContent = (0, import_utils43.compileHandlebar)({
|
|
6321
7626
|
context: {
|
|
6322
7627
|
validity: VERIFICATION_USER_INVITE_DURATION,
|
|
6323
7628
|
link: `${APP_MAIN}/verify/invitation/${res}`
|
|
@@ -6330,7 +7635,7 @@ function useVerificationService() {
|
|
|
6330
7635
|
html: emailContent,
|
|
6331
7636
|
from: "GoWeekdays"
|
|
6332
7637
|
}).catch((error) => {
|
|
6333
|
-
|
|
7638
|
+
import_utils43.logger.log({
|
|
6334
7639
|
level: "error",
|
|
6335
7640
|
message: `Error sending user invite email: ${error}`
|
|
6336
7641
|
});
|
|
@@ -6351,8 +7656,8 @@ function useVerificationService() {
|
|
|
6351
7656
|
try {
|
|
6352
7657
|
const res = await add(value);
|
|
6353
7658
|
const dir = __dirname;
|
|
6354
|
-
const filePath = (0,
|
|
6355
|
-
const emailContent = (0,
|
|
7659
|
+
const filePath = (0, import_utils43.getDirectory)(dir, "./public/handlebars/forget-password");
|
|
7660
|
+
const emailContent = (0, import_utils43.compileHandlebar)({
|
|
6356
7661
|
context: {
|
|
6357
7662
|
validity: VERIFICATION_FORGET_PASSWORD_DURATION,
|
|
6358
7663
|
link: `${APP_MAIN}/reset-password/${res}`
|
|
@@ -6365,21 +7670,21 @@ function useVerificationService() {
|
|
|
6365
7670
|
from: "GoWeekdays",
|
|
6366
7671
|
html: emailContent
|
|
6367
7672
|
}).catch((error) => {
|
|
6368
|
-
|
|
7673
|
+
import_utils43.logger.log({
|
|
6369
7674
|
level: "error",
|
|
6370
7675
|
message: `Error sending forget password email: ${error}`
|
|
6371
7676
|
});
|
|
6372
7677
|
});
|
|
6373
7678
|
return "Successfully created a link to reset password. Please check your email.";
|
|
6374
7679
|
} catch (error) {
|
|
6375
|
-
throw new
|
|
7680
|
+
throw new import_utils43.InternalServerError("Failed to create forget password link.");
|
|
6376
7681
|
}
|
|
6377
7682
|
}
|
|
6378
7683
|
async function getById(id) {
|
|
6379
7684
|
try {
|
|
6380
7685
|
const _id = await _getById(id);
|
|
6381
7686
|
if (!_id) {
|
|
6382
|
-
throw new
|
|
7687
|
+
throw new import_utils43.NotFoundError("Verification not found.");
|
|
6383
7688
|
}
|
|
6384
7689
|
return _id;
|
|
6385
7690
|
} catch (error) {
|
|
@@ -6411,48 +7716,48 @@ function useVerificationService() {
|
|
|
6411
7716
|
}
|
|
6412
7717
|
function errorByType(type, status) {
|
|
6413
7718
|
if (type === "user-invite" && status === "expired") {
|
|
6414
|
-
throw new
|
|
7719
|
+
throw new import_utils43.BadRequestError(
|
|
6415
7720
|
"Invitation has already expired, please contact admin to resend the invitation."
|
|
6416
7721
|
);
|
|
6417
7722
|
}
|
|
6418
7723
|
if (type === "user-sign-up" && status === "expired") {
|
|
6419
|
-
throw new
|
|
7724
|
+
throw new import_utils43.BadRequestError(
|
|
6420
7725
|
"Sign up verification has expired, please sign up again to get a new verification link."
|
|
6421
7726
|
);
|
|
6422
7727
|
}
|
|
6423
7728
|
if (type === "user-invite" && status === "complete") {
|
|
6424
|
-
throw new
|
|
7729
|
+
throw new import_utils43.BadRequestError(
|
|
6425
7730
|
"User already registered, please login to continue."
|
|
6426
7731
|
);
|
|
6427
7732
|
}
|
|
6428
7733
|
if (type === "forget-password" && status === "complete") {
|
|
6429
|
-
throw new
|
|
7734
|
+
throw new import_utils43.BadRequestError(
|
|
6430
7735
|
"Forget password verification has already been used, please request a new one."
|
|
6431
7736
|
);
|
|
6432
7737
|
}
|
|
6433
7738
|
if (type === "forget-password" && status === "expired") {
|
|
6434
|
-
throw new
|
|
7739
|
+
throw new import_utils43.BadRequestError(
|
|
6435
7740
|
"Forget password verification has expired, please request a new one."
|
|
6436
7741
|
);
|
|
6437
7742
|
}
|
|
6438
|
-
throw new
|
|
7743
|
+
throw new import_utils43.BadRequestError("Invalid verification.");
|
|
6439
7744
|
}
|
|
6440
7745
|
async function verify(id) {
|
|
6441
|
-
const session =
|
|
7746
|
+
const session = import_utils43.useAtlas.getClient()?.startSession();
|
|
6442
7747
|
session?.startTransaction();
|
|
6443
7748
|
try {
|
|
6444
7749
|
const _id = await _getById(id);
|
|
6445
7750
|
if (!_id) {
|
|
6446
|
-
throw new
|
|
7751
|
+
throw new import_utils43.NotFoundError("Verification not found.");
|
|
6447
7752
|
}
|
|
6448
7753
|
if (_id.status === "expired") {
|
|
6449
7754
|
errorByType(_id.type, "expired");
|
|
6450
7755
|
}
|
|
6451
7756
|
if (_id.status === "complete") {
|
|
6452
|
-
throw new
|
|
7757
|
+
throw new import_utils43.BadRequestError("Verification already completed.");
|
|
6453
7758
|
}
|
|
6454
7759
|
if (!_id.expireAt) {
|
|
6455
|
-
throw new
|
|
7760
|
+
throw new import_utils43.BadRequestError("Expiration date is required.");
|
|
6456
7761
|
}
|
|
6457
7762
|
const expiration = new Date(_id.expireAt).getTime();
|
|
6458
7763
|
const now = (/* @__PURE__ */ new Date()).getTime();
|
|
@@ -6465,15 +7770,15 @@ function useVerificationService() {
|
|
|
6465
7770
|
await _updateStatusById(id, "complete", session);
|
|
6466
7771
|
const user = await getUserByEmail(_id.email);
|
|
6467
7772
|
if (!user) {
|
|
6468
|
-
throw new
|
|
7773
|
+
throw new import_utils43.NotFoundError("User not found for member invite.");
|
|
6469
7774
|
}
|
|
6470
7775
|
if (!_id.metadata?.app) {
|
|
6471
|
-
throw new
|
|
7776
|
+
throw new import_utils43.BadRequestError(
|
|
6472
7777
|
"App metadata is required for member invite."
|
|
6473
7778
|
);
|
|
6474
7779
|
}
|
|
6475
7780
|
if (!_id.metadata?.role || !_id.metadata?.roleName) {
|
|
6476
|
-
throw new
|
|
7781
|
+
throw new import_utils43.BadRequestError(
|
|
6477
7782
|
"Role metadata is required for member invite."
|
|
6478
7783
|
);
|
|
6479
7784
|
}
|
|
@@ -6495,7 +7800,7 @@ function useVerificationService() {
|
|
|
6495
7800
|
return _id;
|
|
6496
7801
|
} catch (error) {
|
|
6497
7802
|
await session?.abortTransaction();
|
|
6498
|
-
|
|
7803
|
+
import_utils43.logger.log({
|
|
6499
7804
|
level: "info",
|
|
6500
7805
|
message: `Error verifying user invitation: ${error}`
|
|
6501
7806
|
});
|
|
@@ -6508,7 +7813,7 @@ function useVerificationService() {
|
|
|
6508
7813
|
try {
|
|
6509
7814
|
await updateStatusById(id, "cancelled");
|
|
6510
7815
|
} catch (error) {
|
|
6511
|
-
throw new
|
|
7816
|
+
throw new import_utils43.InternalServerError(
|
|
6512
7817
|
`Error cancelling user invitation: ${error}`
|
|
6513
7818
|
);
|
|
6514
7819
|
}
|
|
@@ -6528,7 +7833,7 @@ function useVerificationService() {
|
|
|
6528
7833
|
try {
|
|
6529
7834
|
const user = await getUserByEmail(email);
|
|
6530
7835
|
if (user) {
|
|
6531
|
-
throw new
|
|
7836
|
+
throw new import_utils43.BadRequestError(
|
|
6532
7837
|
`Email ${email} is already registered, please login to continue.`
|
|
6533
7838
|
);
|
|
6534
7839
|
}
|
|
@@ -6541,8 +7846,8 @@ function useVerificationService() {
|
|
|
6541
7846
|
};
|
|
6542
7847
|
const res = await add(value);
|
|
6543
7848
|
const dir = __dirname;
|
|
6544
|
-
const filePath = (0,
|
|
6545
|
-
const emailContent = (0,
|
|
7849
|
+
const filePath = (0, import_utils43.getDirectory)(dir, "./public/handlebars/sign-up");
|
|
7850
|
+
const emailContent = (0, import_utils43.compileHandlebar)({
|
|
6546
7851
|
context: {
|
|
6547
7852
|
validity: "5 minutes",
|
|
6548
7853
|
link: `${APP_MAIN}/sign-up/${res}`
|
|
@@ -6555,7 +7860,7 @@ function useVerificationService() {
|
|
|
6555
7860
|
html: emailContent,
|
|
6556
7861
|
from: "GoWeekdays"
|
|
6557
7862
|
}).catch((error) => {
|
|
6558
|
-
|
|
7863
|
+
import_utils43.logger.log({
|
|
6559
7864
|
level: "error",
|
|
6560
7865
|
message: `Error sending user invite email: ${error}`
|
|
6561
7866
|
});
|
|
@@ -6568,11 +7873,11 @@ function useVerificationService() {
|
|
|
6568
7873
|
async function inviteMember(value) {
|
|
6569
7874
|
const { error } = schemaInviteMember.validate(value);
|
|
6570
7875
|
if (error) {
|
|
6571
|
-
throw new
|
|
7876
|
+
throw new import_utils43.BadRequestError(error.message);
|
|
6572
7877
|
}
|
|
6573
7878
|
const role = await getRoleById(value.role);
|
|
6574
7879
|
if (!role) {
|
|
6575
|
-
throw new
|
|
7880
|
+
throw new import_utils43.NotFoundError("Role not found.");
|
|
6576
7881
|
}
|
|
6577
7882
|
let verificationData = {
|
|
6578
7883
|
type: "user-invite",
|
|
@@ -6590,15 +7895,15 @@ function useVerificationService() {
|
|
|
6590
7895
|
if (user) {
|
|
6591
7896
|
verificationData.type = "member-invite";
|
|
6592
7897
|
const verificationId2 = await add(verificationData);
|
|
6593
|
-
const filePath2 = (0,
|
|
7898
|
+
const filePath2 = (0, import_utils43.getDirectory)(dir, "./public/handlebars/member-invite");
|
|
6594
7899
|
let org = null;
|
|
6595
7900
|
if (verificationData.metadata.org) {
|
|
6596
7901
|
org = await getOrgById(verificationData.metadata.org);
|
|
6597
7902
|
if (!org) {
|
|
6598
|
-
throw new
|
|
7903
|
+
throw new import_utils43.NotFoundError("Organization not found.");
|
|
6599
7904
|
}
|
|
6600
7905
|
}
|
|
6601
|
-
const emailContent2 = (0,
|
|
7906
|
+
const emailContent2 = (0, import_utils43.compileHandlebar)({
|
|
6602
7907
|
context: {
|
|
6603
7908
|
validity: VERIFICATION_USER_INVITE_DURATION,
|
|
6604
7909
|
link: `${APP_MAIN}/verify/member-invite/${verificationId2}`,
|
|
@@ -6612,7 +7917,7 @@ function useVerificationService() {
|
|
|
6612
7917
|
html: emailContent2,
|
|
6613
7918
|
from: "GoWeekdays"
|
|
6614
7919
|
}).catch((error2) => {
|
|
6615
|
-
|
|
7920
|
+
import_utils43.logger.log({
|
|
6616
7921
|
level: "error",
|
|
6617
7922
|
message: `Error sending user invite email: ${error2}`
|
|
6618
7923
|
});
|
|
@@ -6620,8 +7925,8 @@ function useVerificationService() {
|
|
|
6620
7925
|
return verificationId2;
|
|
6621
7926
|
}
|
|
6622
7927
|
const verificationId = await add(verificationData);
|
|
6623
|
-
const filePath = (0,
|
|
6624
|
-
const emailContent = (0,
|
|
7928
|
+
const filePath = (0, import_utils43.getDirectory)(dir, "./public/handlebars/user-invite");
|
|
7929
|
+
const emailContent = (0, import_utils43.compileHandlebar)({
|
|
6625
7930
|
context: {
|
|
6626
7931
|
validity: VERIFICATION_USER_INVITE_DURATION,
|
|
6627
7932
|
link: `${APP_MAIN}/verify/invitation/${verificationId}`
|
|
@@ -6634,55 +7939,55 @@ function useVerificationService() {
|
|
|
6634
7939
|
html: emailContent,
|
|
6635
7940
|
from: "GoWeekdays"
|
|
6636
7941
|
}).catch((error2) => {
|
|
6637
|
-
|
|
7942
|
+
import_utils43.logger.log({
|
|
6638
7943
|
level: "error",
|
|
6639
7944
|
message: `Error sending user invite email: ${error2}`
|
|
6640
7945
|
});
|
|
6641
7946
|
});
|
|
6642
7947
|
return verificationId;
|
|
6643
7948
|
} catch (error2) {
|
|
6644
|
-
if (error2 instanceof
|
|
7949
|
+
if (error2 instanceof import_utils43.AppError) {
|
|
6645
7950
|
throw error2;
|
|
6646
7951
|
} else {
|
|
6647
|
-
throw new
|
|
7952
|
+
throw new import_utils43.InternalServerError("Failed to invite member.");
|
|
6648
7953
|
}
|
|
6649
7954
|
}
|
|
6650
7955
|
}
|
|
6651
7956
|
async function cancelInviteMember(id) {
|
|
6652
|
-
const { error } =
|
|
7957
|
+
const { error } = import_joi31.default.string().hex().required().validate(id);
|
|
6653
7958
|
if (error) {
|
|
6654
|
-
throw new
|
|
7959
|
+
throw new import_utils43.BadRequestError("Invalid verification ID.");
|
|
6655
7960
|
}
|
|
6656
7961
|
try {
|
|
6657
7962
|
const invite = await _getById(id);
|
|
6658
7963
|
if (!invite) {
|
|
6659
|
-
throw new
|
|
7964
|
+
throw new import_utils43.NotFoundError("Invitation not found.");
|
|
6660
7965
|
}
|
|
6661
7966
|
if (invite.status === "cancelled") {
|
|
6662
|
-
throw new
|
|
7967
|
+
throw new import_utils43.BadRequestError("Invitation already cancelled.");
|
|
6663
7968
|
}
|
|
6664
7969
|
if (invite.status === "complete") {
|
|
6665
|
-
throw new
|
|
7970
|
+
throw new import_utils43.BadRequestError("Cannot cancel a completed invitation.");
|
|
6666
7971
|
}
|
|
6667
7972
|
await _updateStatusById(id, "cancelled");
|
|
6668
7973
|
return "Successfully cancelled the invitation.";
|
|
6669
7974
|
} catch (error2) {
|
|
6670
|
-
if (error2 instanceof
|
|
7975
|
+
if (error2 instanceof import_utils43.AppError) {
|
|
6671
7976
|
throw error2;
|
|
6672
7977
|
} else {
|
|
6673
|
-
throw new
|
|
7978
|
+
throw new import_utils43.InternalServerError("Failed to cancel the invitation.");
|
|
6674
7979
|
}
|
|
6675
7980
|
}
|
|
6676
7981
|
}
|
|
6677
7982
|
async function forgetPassword(email) {
|
|
6678
|
-
const { error } =
|
|
7983
|
+
const { error } = import_joi31.default.string().email().required().validate(email);
|
|
6679
7984
|
if (error) {
|
|
6680
|
-
throw new
|
|
7985
|
+
throw new import_utils43.BadRequestError("Invalid email address.");
|
|
6681
7986
|
}
|
|
6682
7987
|
try {
|
|
6683
7988
|
const member = await getUserByEmail(email);
|
|
6684
7989
|
if (!member) {
|
|
6685
|
-
throw new
|
|
7990
|
+
throw new import_utils43.NotFoundError("User not found.");
|
|
6686
7991
|
}
|
|
6687
7992
|
const value = {
|
|
6688
7993
|
type: "forget-password",
|
|
@@ -6692,8 +7997,8 @@ function useVerificationService() {
|
|
|
6692
7997
|
};
|
|
6693
7998
|
const res = await add(value);
|
|
6694
7999
|
const dir = __dirname;
|
|
6695
|
-
const filePath = (0,
|
|
6696
|
-
const emailContent = (0,
|
|
8000
|
+
const filePath = (0, import_utils43.getDirectory)(dir, "./public/handlebars/forget-password");
|
|
8001
|
+
const emailContent = (0, import_utils43.compileHandlebar)({
|
|
6697
8002
|
context: {
|
|
6698
8003
|
validity: VERIFICATION_FORGET_PASSWORD_DURATION,
|
|
6699
8004
|
link: `${APP_MAIN}/reset-password/${res}`
|
|
@@ -6706,17 +8011,17 @@ function useVerificationService() {
|
|
|
6706
8011
|
from: "GoWeekdays",
|
|
6707
8012
|
html: emailContent
|
|
6708
8013
|
}).catch((error2) => {
|
|
6709
|
-
|
|
8014
|
+
import_utils43.logger.log({
|
|
6710
8015
|
level: "error",
|
|
6711
8016
|
message: `Error sending forget password email: ${error2}`
|
|
6712
8017
|
});
|
|
6713
8018
|
});
|
|
6714
8019
|
return "Successfully created a link to reset password. Please check your email.";
|
|
6715
8020
|
} catch (error2) {
|
|
6716
|
-
if (error2 instanceof
|
|
8021
|
+
if (error2 instanceof import_utils43.AppError) {
|
|
6717
8022
|
throw error2;
|
|
6718
8023
|
} else {
|
|
6719
|
-
throw new
|
|
8024
|
+
throw new import_utils43.InternalServerError(
|
|
6720
8025
|
"Failed to process forget password request."
|
|
6721
8026
|
);
|
|
6722
8027
|
}
|
|
@@ -6743,13 +8048,13 @@ function useAuthController() {
|
|
|
6743
8048
|
async function login(req, res, next) {
|
|
6744
8049
|
const email = req.body.email;
|
|
6745
8050
|
const password = req.body.password;
|
|
6746
|
-
const validation =
|
|
6747
|
-
email:
|
|
6748
|
-
password:
|
|
8051
|
+
const validation = import_joi32.default.object({
|
|
8052
|
+
email: import_joi32.default.string().email().required(),
|
|
8053
|
+
password: import_joi32.default.string().required()
|
|
6749
8054
|
});
|
|
6750
8055
|
const { error } = validation.validate({ email, password });
|
|
6751
8056
|
if (error) {
|
|
6752
|
-
next(new
|
|
8057
|
+
next(new import_utils44.BadRequestError(error.message));
|
|
6753
8058
|
return;
|
|
6754
8059
|
}
|
|
6755
8060
|
try {
|
|
@@ -6765,14 +8070,14 @@ function useAuthController() {
|
|
|
6765
8070
|
res.cookie("sid", session.sid, cookieOptions).cookie("user", session.user, cookieOptions).json({ message: "Login successful" });
|
|
6766
8071
|
return;
|
|
6767
8072
|
} catch (error2) {
|
|
6768
|
-
|
|
8073
|
+
import_utils44.logger.log({
|
|
6769
8074
|
level: "error",
|
|
6770
8075
|
message: `Error during login: ${error2.message}`
|
|
6771
8076
|
});
|
|
6772
|
-
if (error2 instanceof
|
|
8077
|
+
if (error2 instanceof import_utils44.AppError) {
|
|
6773
8078
|
next(error2);
|
|
6774
8079
|
} else {
|
|
6775
|
-
next(new
|
|
8080
|
+
next(new import_utils44.InternalServerError("An unexpected error occurred"));
|
|
6776
8081
|
}
|
|
6777
8082
|
return;
|
|
6778
8083
|
}
|
|
@@ -6780,17 +8085,17 @@ function useAuthController() {
|
|
|
6780
8085
|
async function logout(req, res, next) {
|
|
6781
8086
|
const sid = req.headers["authorization"] ?? "";
|
|
6782
8087
|
if (!sid) {
|
|
6783
|
-
next(new
|
|
8088
|
+
next(new import_utils44.BadRequestError("Session ID is required"));
|
|
6784
8089
|
return;
|
|
6785
8090
|
}
|
|
6786
8091
|
try {
|
|
6787
8092
|
await useAuthService().logout(sid);
|
|
6788
8093
|
res.json({ message: "Logged out successfully" });
|
|
6789
8094
|
} catch (error) {
|
|
6790
|
-
if (error instanceof
|
|
8095
|
+
if (error instanceof import_utils44.AppError) {
|
|
6791
8096
|
next(error);
|
|
6792
8097
|
} else {
|
|
6793
|
-
next(new
|
|
8098
|
+
next(new import_utils44.InternalServerError("An unexpected error occurred"));
|
|
6794
8099
|
}
|
|
6795
8100
|
}
|
|
6796
8101
|
}
|
|
@@ -6801,64 +8106,64 @@ function useAuthController() {
|
|
|
6801
8106
|
}
|
|
6802
8107
|
|
|
6803
8108
|
// src/resources/building/building.model.ts
|
|
6804
|
-
var
|
|
6805
|
-
var
|
|
6806
|
-
var
|
|
6807
|
-
var schemaBuilding =
|
|
6808
|
-
_id:
|
|
6809
|
-
school:
|
|
6810
|
-
serial:
|
|
6811
|
-
name:
|
|
6812
|
-
levels:
|
|
6813
|
-
createdAt:
|
|
6814
|
-
updatedAt:
|
|
6815
|
-
deletedAt:
|
|
6816
|
-
status:
|
|
8109
|
+
var import_utils45 = require("@goweekdays/utils");
|
|
8110
|
+
var import_joi33 = __toESM(require("joi"));
|
|
8111
|
+
var import_mongodb22 = require("mongodb");
|
|
8112
|
+
var schemaBuilding = import_joi33.default.object({
|
|
8113
|
+
_id: import_joi33.default.string().hex().optional(),
|
|
8114
|
+
school: import_joi33.default.string().hex().required(),
|
|
8115
|
+
serial: import_joi33.default.string().optional().allow("", null),
|
|
8116
|
+
name: import_joi33.default.string().required(),
|
|
8117
|
+
levels: import_joi33.default.number().integer().min(1).required(),
|
|
8118
|
+
createdAt: import_joi33.default.date().optional().allow("", null),
|
|
8119
|
+
updatedAt: import_joi33.default.date().optional().allow("", null),
|
|
8120
|
+
deletedAt: import_joi33.default.date().optional().allow("", null),
|
|
8121
|
+
status: import_joi33.default.string().optional().allow("", null)
|
|
6817
8122
|
});
|
|
6818
|
-
var schemaBuildingUnit =
|
|
6819
|
-
_id:
|
|
6820
|
-
school:
|
|
6821
|
-
name:
|
|
6822
|
-
building:
|
|
6823
|
-
buildingName:
|
|
6824
|
-
level:
|
|
6825
|
-
category:
|
|
6826
|
-
type:
|
|
6827
|
-
seating_capacity:
|
|
6828
|
-
standing_capacity:
|
|
6829
|
-
description:
|
|
6830
|
-
unit_of_measurement:
|
|
6831
|
-
area:
|
|
6832
|
-
status:
|
|
8123
|
+
var schemaBuildingUnit = import_joi33.default.object({
|
|
8124
|
+
_id: import_joi33.default.string().hex().optional(),
|
|
8125
|
+
school: import_joi33.default.string().hex().required(),
|
|
8126
|
+
name: import_joi33.default.string().optional().allow("", null),
|
|
8127
|
+
building: import_joi33.default.string().hex().required(),
|
|
8128
|
+
buildingName: import_joi33.default.string().optional().allow("", null),
|
|
8129
|
+
level: import_joi33.default.number().integer().min(1).required(),
|
|
8130
|
+
category: import_joi33.default.string().required(),
|
|
8131
|
+
type: import_joi33.default.string().required(),
|
|
8132
|
+
seating_capacity: import_joi33.default.number().integer().min(0).required(),
|
|
8133
|
+
standing_capacity: import_joi33.default.number().integer().min(0).required(),
|
|
8134
|
+
description: import_joi33.default.string().optional().allow("", null),
|
|
8135
|
+
unit_of_measurement: import_joi33.default.string().valid("sqm").required(),
|
|
8136
|
+
area: import_joi33.default.number().positive().required(),
|
|
8137
|
+
status: import_joi33.default.string().optional().allow("", null)
|
|
6833
8138
|
});
|
|
6834
|
-
var schemaUpdateOptions =
|
|
6835
|
-
name:
|
|
6836
|
-
building:
|
|
6837
|
-
buildingName:
|
|
6838
|
-
level:
|
|
6839
|
-
category:
|
|
6840
|
-
type:
|
|
6841
|
-
seating_capacity:
|
|
6842
|
-
standing_capacity:
|
|
6843
|
-
area:
|
|
8139
|
+
var schemaUpdateOptions = import_joi33.default.object({
|
|
8140
|
+
name: import_joi33.default.string().optional().allow("", null),
|
|
8141
|
+
building: import_joi33.default.string().hex().optional().allow("", null),
|
|
8142
|
+
buildingName: import_joi33.default.string().optional().allow("", null),
|
|
8143
|
+
level: import_joi33.default.number().integer().min(1).optional().allow("", null),
|
|
8144
|
+
category: import_joi33.default.string().optional().allow("", null),
|
|
8145
|
+
type: import_joi33.default.string().optional().allow("", null),
|
|
8146
|
+
seating_capacity: import_joi33.default.number().integer().min(0).optional().allow("", null),
|
|
8147
|
+
standing_capacity: import_joi33.default.number().integer().min(0).optional().allow("", null),
|
|
8148
|
+
area: import_joi33.default.number().positive().optional().allow("", null)
|
|
6844
8149
|
});
|
|
6845
8150
|
function MBuilding(value) {
|
|
6846
8151
|
const { error } = schemaBuilding.validate(value);
|
|
6847
8152
|
if (error) {
|
|
6848
|
-
|
|
6849
|
-
throw new
|
|
8153
|
+
import_utils45.logger.info(`Building Model: ${error.message}`);
|
|
8154
|
+
throw new import_utils45.BadRequestError(error.message);
|
|
6850
8155
|
}
|
|
6851
8156
|
if (value._id && typeof value._id === "string") {
|
|
6852
8157
|
try {
|
|
6853
|
-
value._id = new
|
|
8158
|
+
value._id = new import_mongodb22.ObjectId(value._id);
|
|
6854
8159
|
} catch (error2) {
|
|
6855
|
-
throw new
|
|
8160
|
+
throw new import_utils45.BadRequestError("Invalid _id format");
|
|
6856
8161
|
}
|
|
6857
8162
|
}
|
|
6858
8163
|
try {
|
|
6859
|
-
value.school = new
|
|
8164
|
+
value.school = new import_mongodb22.ObjectId(value.school);
|
|
6860
8165
|
} catch (error2) {
|
|
6861
|
-
throw new
|
|
8166
|
+
throw new import_utils45.BadRequestError("Invalid school format");
|
|
6862
8167
|
}
|
|
6863
8168
|
return {
|
|
6864
8169
|
_id: value._id ?? void 0,
|
|
@@ -6875,25 +8180,25 @@ function MBuilding(value) {
|
|
|
6875
8180
|
function MBuildingUnit(value) {
|
|
6876
8181
|
const { error } = schemaBuildingUnit.validate(value);
|
|
6877
8182
|
if (error) {
|
|
6878
|
-
|
|
6879
|
-
throw new
|
|
8183
|
+
import_utils45.logger.info(`Building Unit Model: ${error.message}`);
|
|
8184
|
+
throw new import_utils45.BadRequestError(error.message);
|
|
6880
8185
|
}
|
|
6881
8186
|
if (value._id && typeof value._id === "string") {
|
|
6882
8187
|
try {
|
|
6883
|
-
value._id = new
|
|
8188
|
+
value._id = new import_mongodb22.ObjectId(value._id);
|
|
6884
8189
|
} catch (error2) {
|
|
6885
|
-
throw new
|
|
8190
|
+
throw new import_utils45.BadRequestError("Invalid ID");
|
|
6886
8191
|
}
|
|
6887
8192
|
}
|
|
6888
8193
|
try {
|
|
6889
|
-
value.school = new
|
|
8194
|
+
value.school = new import_mongodb22.ObjectId(value.school);
|
|
6890
8195
|
} catch (error2) {
|
|
6891
|
-
throw new
|
|
8196
|
+
throw new import_utils45.BadRequestError("Invalid school ID");
|
|
6892
8197
|
}
|
|
6893
8198
|
try {
|
|
6894
|
-
value.building = new
|
|
8199
|
+
value.building = new import_mongodb22.ObjectId(value.building);
|
|
6895
8200
|
} catch (error2) {
|
|
6896
|
-
throw new
|
|
8201
|
+
throw new import_utils45.BadRequestError("Invalid building ID");
|
|
6897
8202
|
}
|
|
6898
8203
|
return {
|
|
6899
8204
|
_id: value._id ?? void 0,
|
|
@@ -6917,16 +8222,16 @@ function MBuildingUnit(value) {
|
|
|
6917
8222
|
}
|
|
6918
8223
|
|
|
6919
8224
|
// src/resources/building/building.repository.ts
|
|
6920
|
-
var
|
|
6921
|
-
var
|
|
8225
|
+
var import_utils46 = require("@goweekdays/utils");
|
|
8226
|
+
var import_mongodb23 = require("mongodb");
|
|
6922
8227
|
function useBuildingRepo() {
|
|
6923
|
-
const db =
|
|
8228
|
+
const db = import_utils46.useAtlas.getDb();
|
|
6924
8229
|
if (!db) {
|
|
6925
8230
|
throw new Error("Unable to connect to server.");
|
|
6926
8231
|
}
|
|
6927
8232
|
const namespace_collection = "school.buildings";
|
|
6928
8233
|
const collection = db.collection(namespace_collection);
|
|
6929
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
8234
|
+
const { getCache, setCache, delNamespace } = (0, import_utils46.useCache)(namespace_collection);
|
|
6930
8235
|
async function createIndexes() {
|
|
6931
8236
|
try {
|
|
6932
8237
|
await collection.createIndexes([
|
|
@@ -6939,7 +8244,7 @@ function useBuildingRepo() {
|
|
|
6939
8244
|
}
|
|
6940
8245
|
}
|
|
6941
8246
|
createIndexes().catch((error) => {
|
|
6942
|
-
|
|
8247
|
+
import_utils46.logger.log({ level: "error", message: `Index creation error: ${error}` });
|
|
6943
8248
|
});
|
|
6944
8249
|
async function add(value, session) {
|
|
6945
8250
|
try {
|
|
@@ -6948,16 +8253,16 @@ function useBuildingRepo() {
|
|
|
6948
8253
|
delCachedData();
|
|
6949
8254
|
return res.insertedId;
|
|
6950
8255
|
} catch (error) {
|
|
6951
|
-
|
|
8256
|
+
import_utils46.logger.log({
|
|
6952
8257
|
level: "error",
|
|
6953
8258
|
message: error.message
|
|
6954
8259
|
});
|
|
6955
|
-
if (error instanceof
|
|
8260
|
+
if (error instanceof import_utils46.AppError) {
|
|
6956
8261
|
throw error;
|
|
6957
8262
|
} else {
|
|
6958
8263
|
const isDuplicated = error.message.includes("duplicate");
|
|
6959
8264
|
if (isDuplicated) {
|
|
6960
|
-
throw new
|
|
8265
|
+
throw new import_utils46.BadRequestError("Building already exists.");
|
|
6961
8266
|
}
|
|
6962
8267
|
throw new Error("Failed to create building.");
|
|
6963
8268
|
}
|
|
@@ -6965,9 +8270,9 @@ function useBuildingRepo() {
|
|
|
6965
8270
|
}
|
|
6966
8271
|
async function updateById(_id, value, session) {
|
|
6967
8272
|
try {
|
|
6968
|
-
_id = new
|
|
8273
|
+
_id = new import_mongodb23.ObjectId(_id);
|
|
6969
8274
|
} catch (error) {
|
|
6970
|
-
throw new
|
|
8275
|
+
throw new import_utils46.BadRequestError("Invalid ID.");
|
|
6971
8276
|
}
|
|
6972
8277
|
try {
|
|
6973
8278
|
const res = await collection.updateOne(
|
|
@@ -6978,11 +8283,11 @@ function useBuildingRepo() {
|
|
|
6978
8283
|
delCachedData();
|
|
6979
8284
|
return res;
|
|
6980
8285
|
} catch (error) {
|
|
6981
|
-
|
|
8286
|
+
import_utils46.logger.log({
|
|
6982
8287
|
level: "error",
|
|
6983
8288
|
message: error.message
|
|
6984
8289
|
});
|
|
6985
|
-
if (error instanceof
|
|
8290
|
+
if (error instanceof import_utils46.AppError) {
|
|
6986
8291
|
throw error;
|
|
6987
8292
|
} else {
|
|
6988
8293
|
throw new Error("Failed to update building.");
|
|
@@ -7007,9 +8312,9 @@ function useBuildingRepo() {
|
|
|
7007
8312
|
}
|
|
7008
8313
|
if (school) {
|
|
7009
8314
|
try {
|
|
7010
|
-
query.school = new
|
|
8315
|
+
query.school = new import_mongodb23.ObjectId(school);
|
|
7011
8316
|
} catch (error) {
|
|
7012
|
-
throw new
|
|
8317
|
+
throw new import_utils46.BadRequestError("Invalid school ID.");
|
|
7013
8318
|
}
|
|
7014
8319
|
}
|
|
7015
8320
|
const cacheParams = {
|
|
@@ -7023,15 +8328,15 @@ function useBuildingRepo() {
|
|
|
7023
8328
|
cacheParams.school = school;
|
|
7024
8329
|
if (status !== "active")
|
|
7025
8330
|
cacheParams.status = status;
|
|
7026
|
-
const cacheKey = (0,
|
|
7027
|
-
|
|
8331
|
+
const cacheKey = (0, import_utils46.makeCacheKey)(namespace_collection, cacheParams);
|
|
8332
|
+
import_utils46.logger.log({
|
|
7028
8333
|
level: "info",
|
|
7029
8334
|
message: `Cache key for getAll buildings: ${cacheKey}`
|
|
7030
8335
|
});
|
|
7031
8336
|
try {
|
|
7032
8337
|
const cached = await getCache(cacheKey);
|
|
7033
8338
|
if (cached) {
|
|
7034
|
-
|
|
8339
|
+
import_utils46.logger.log({
|
|
7035
8340
|
level: "info",
|
|
7036
8341
|
message: `Cache hit for getAll buildings: ${cacheKey}`
|
|
7037
8342
|
});
|
|
@@ -7044,35 +8349,35 @@ function useBuildingRepo() {
|
|
|
7044
8349
|
{ $limit: limit }
|
|
7045
8350
|
]).toArray();
|
|
7046
8351
|
const length = await collection.countDocuments(query);
|
|
7047
|
-
const data = (0,
|
|
8352
|
+
const data = (0, import_utils46.paginate)(items, page, limit, length);
|
|
7048
8353
|
setCache(cacheKey, data, 600).then(() => {
|
|
7049
|
-
|
|
8354
|
+
import_utils46.logger.log({
|
|
7050
8355
|
level: "info",
|
|
7051
8356
|
message: `Cache set for getAll buildings: ${cacheKey}`
|
|
7052
8357
|
});
|
|
7053
8358
|
}).catch((err) => {
|
|
7054
|
-
|
|
8359
|
+
import_utils46.logger.log({
|
|
7055
8360
|
level: "error",
|
|
7056
8361
|
message: `Failed to set cache for getAll buildings: ${err.message}`
|
|
7057
8362
|
});
|
|
7058
8363
|
});
|
|
7059
8364
|
return data;
|
|
7060
8365
|
} catch (error) {
|
|
7061
|
-
|
|
8366
|
+
import_utils46.logger.log({ level: "error", message: `${error}` });
|
|
7062
8367
|
throw error;
|
|
7063
8368
|
}
|
|
7064
8369
|
}
|
|
7065
8370
|
async function getById(_id) {
|
|
7066
8371
|
try {
|
|
7067
|
-
_id = new
|
|
8372
|
+
_id = new import_mongodb23.ObjectId(_id);
|
|
7068
8373
|
} catch (error) {
|
|
7069
|
-
throw new
|
|
8374
|
+
throw new import_utils46.BadRequestError("Invalid ID.");
|
|
7070
8375
|
}
|
|
7071
|
-
const cacheKey = (0,
|
|
8376
|
+
const cacheKey = (0, import_utils46.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
7072
8377
|
try {
|
|
7073
8378
|
const cached = await getCache(cacheKey);
|
|
7074
8379
|
if (cached) {
|
|
7075
|
-
|
|
8380
|
+
import_utils46.logger.log({
|
|
7076
8381
|
level: "info",
|
|
7077
8382
|
message: `Cache hit for getById building: ${cacheKey}`
|
|
7078
8383
|
});
|
|
@@ -7082,30 +8387,30 @@ function useBuildingRepo() {
|
|
|
7082
8387
|
_id
|
|
7083
8388
|
});
|
|
7084
8389
|
setCache(cacheKey, result, 300).then(() => {
|
|
7085
|
-
|
|
8390
|
+
import_utils46.logger.log({
|
|
7086
8391
|
level: "info",
|
|
7087
8392
|
message: `Cache set for building by id: ${cacheKey}`
|
|
7088
8393
|
});
|
|
7089
8394
|
}).catch((err) => {
|
|
7090
|
-
|
|
8395
|
+
import_utils46.logger.log({
|
|
7091
8396
|
level: "error",
|
|
7092
8397
|
message: `Failed to set cache for building by id: ${err.message}`
|
|
7093
8398
|
});
|
|
7094
8399
|
});
|
|
7095
8400
|
return result;
|
|
7096
8401
|
} catch (error) {
|
|
7097
|
-
if (error instanceof
|
|
8402
|
+
if (error instanceof import_utils46.AppError) {
|
|
7098
8403
|
throw error;
|
|
7099
8404
|
} else {
|
|
7100
|
-
throw new
|
|
8405
|
+
throw new import_utils46.InternalServerError("Failed to get building.");
|
|
7101
8406
|
}
|
|
7102
8407
|
}
|
|
7103
8408
|
}
|
|
7104
8409
|
async function deleteById(_id, session) {
|
|
7105
8410
|
try {
|
|
7106
|
-
_id = new
|
|
8411
|
+
_id = new import_mongodb23.ObjectId(_id);
|
|
7107
8412
|
} catch (error) {
|
|
7108
|
-
throw new
|
|
8413
|
+
throw new import_utils46.BadRequestError("Invalid ID.");
|
|
7109
8414
|
}
|
|
7110
8415
|
try {
|
|
7111
8416
|
const res = await collection.updateOne(
|
|
@@ -7115,25 +8420,25 @@ function useBuildingRepo() {
|
|
|
7115
8420
|
delCachedData();
|
|
7116
8421
|
return res;
|
|
7117
8422
|
} catch (error) {
|
|
7118
|
-
|
|
8423
|
+
import_utils46.logger.log({
|
|
7119
8424
|
level: "error",
|
|
7120
8425
|
message: error.message
|
|
7121
8426
|
});
|
|
7122
|
-
if (error instanceof
|
|
8427
|
+
if (error instanceof import_utils46.AppError) {
|
|
7123
8428
|
throw error;
|
|
7124
8429
|
} else {
|
|
7125
|
-
throw new
|
|
8430
|
+
throw new import_utils46.InternalServerError("Failed to delete building.");
|
|
7126
8431
|
}
|
|
7127
8432
|
}
|
|
7128
8433
|
}
|
|
7129
8434
|
function delCachedData() {
|
|
7130
8435
|
delNamespace().then(() => {
|
|
7131
|
-
|
|
8436
|
+
import_utils46.logger.log({
|
|
7132
8437
|
level: "info",
|
|
7133
8438
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
7134
8439
|
});
|
|
7135
8440
|
}).catch((err) => {
|
|
7136
|
-
|
|
8441
|
+
import_utils46.logger.log({
|
|
7137
8442
|
level: "error",
|
|
7138
8443
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
7139
8444
|
});
|
|
@@ -7150,19 +8455,19 @@ function useBuildingRepo() {
|
|
|
7150
8455
|
}
|
|
7151
8456
|
|
|
7152
8457
|
// src/resources/building/building.service.ts
|
|
7153
|
-
var
|
|
8458
|
+
var import_utils48 = require("@goweekdays/utils");
|
|
7154
8459
|
|
|
7155
8460
|
// src/resources/building/building-unit.repository.ts
|
|
7156
|
-
var
|
|
7157
|
-
var
|
|
8461
|
+
var import_utils47 = require("@goweekdays/utils");
|
|
8462
|
+
var import_mongodb24 = require("mongodb");
|
|
7158
8463
|
function useBuildingUnitRepo() {
|
|
7159
|
-
const db =
|
|
8464
|
+
const db = import_utils47.useAtlas.getDb();
|
|
7160
8465
|
if (!db) {
|
|
7161
8466
|
throw new Error("Unable to connect to server.");
|
|
7162
8467
|
}
|
|
7163
8468
|
const namespace_collection = "school.building-units";
|
|
7164
8469
|
const collection = db.collection(namespace_collection);
|
|
7165
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
8470
|
+
const { getCache, setCache, delNamespace } = (0, import_utils47.useCache)(namespace_collection);
|
|
7166
8471
|
async function createIndexes() {
|
|
7167
8472
|
try {
|
|
7168
8473
|
await collection.createIndexes([
|
|
@@ -7190,12 +8495,12 @@ function useBuildingUnitRepo() {
|
|
|
7190
8495
|
}
|
|
7191
8496
|
function delCachedData() {
|
|
7192
8497
|
delNamespace().then(() => {
|
|
7193
|
-
|
|
8498
|
+
import_utils47.logger.log({
|
|
7194
8499
|
level: "info",
|
|
7195
8500
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
7196
8501
|
});
|
|
7197
8502
|
}).catch((err) => {
|
|
7198
|
-
|
|
8503
|
+
import_utils47.logger.log({
|
|
7199
8504
|
level: "error",
|
|
7200
8505
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
7201
8506
|
});
|
|
@@ -7208,11 +8513,11 @@ function useBuildingUnitRepo() {
|
|
|
7208
8513
|
delCachedData();
|
|
7209
8514
|
return res.insertedId;
|
|
7210
8515
|
} catch (error) {
|
|
7211
|
-
|
|
8516
|
+
import_utils47.logger.log({
|
|
7212
8517
|
level: "error",
|
|
7213
8518
|
message: error.message
|
|
7214
8519
|
});
|
|
7215
|
-
if (error instanceof
|
|
8520
|
+
if (error instanceof import_utils47.AppError) {
|
|
7216
8521
|
throw error;
|
|
7217
8522
|
} else {
|
|
7218
8523
|
throw new Error("Failed to create building unit.");
|
|
@@ -7222,12 +8527,12 @@ function useBuildingUnitRepo() {
|
|
|
7222
8527
|
async function updateById(_id, value, session) {
|
|
7223
8528
|
const { error } = schemaUpdateOptions.validate(value);
|
|
7224
8529
|
if (error) {
|
|
7225
|
-
throw new
|
|
8530
|
+
throw new import_utils47.BadRequestError(error.message);
|
|
7226
8531
|
}
|
|
7227
8532
|
try {
|
|
7228
|
-
_id = new
|
|
8533
|
+
_id = new import_mongodb24.ObjectId(_id);
|
|
7229
8534
|
} catch (error2) {
|
|
7230
|
-
throw new
|
|
8535
|
+
throw new import_utils47.BadRequestError("Invalid ID.");
|
|
7231
8536
|
}
|
|
7232
8537
|
try {
|
|
7233
8538
|
const res = await collection.updateOne(
|
|
@@ -7238,11 +8543,11 @@ function useBuildingUnitRepo() {
|
|
|
7238
8543
|
delCachedData();
|
|
7239
8544
|
return res;
|
|
7240
8545
|
} catch (error2) {
|
|
7241
|
-
|
|
8546
|
+
import_utils47.logger.log({
|
|
7242
8547
|
level: "error",
|
|
7243
8548
|
message: error2.message
|
|
7244
8549
|
});
|
|
7245
|
-
if (error2 instanceof
|
|
8550
|
+
if (error2 instanceof import_utils47.AppError) {
|
|
7246
8551
|
throw error2;
|
|
7247
8552
|
} else {
|
|
7248
8553
|
throw new Error("Failed to create building unit.");
|
|
@@ -7252,12 +8557,12 @@ function useBuildingUnitRepo() {
|
|
|
7252
8557
|
async function updateByBuildingId(building, value, session) {
|
|
7253
8558
|
const { error } = schemaUpdateOptions.validate(value);
|
|
7254
8559
|
if (error) {
|
|
7255
|
-
throw new
|
|
8560
|
+
throw new import_utils47.BadRequestError(error.message);
|
|
7256
8561
|
}
|
|
7257
8562
|
try {
|
|
7258
|
-
building = new
|
|
8563
|
+
building = new import_mongodb24.ObjectId(building);
|
|
7259
8564
|
} catch (error2) {
|
|
7260
|
-
throw new
|
|
8565
|
+
throw new import_utils47.BadRequestError("Invalid building ID.");
|
|
7261
8566
|
}
|
|
7262
8567
|
try {
|
|
7263
8568
|
const res = await collection.updateMany(
|
|
@@ -7268,11 +8573,11 @@ function useBuildingUnitRepo() {
|
|
|
7268
8573
|
delCachedData();
|
|
7269
8574
|
return res;
|
|
7270
8575
|
} catch (error2) {
|
|
7271
|
-
|
|
8576
|
+
import_utils47.logger.log({
|
|
7272
8577
|
level: "error",
|
|
7273
8578
|
message: error2.message
|
|
7274
8579
|
});
|
|
7275
|
-
if (error2 instanceof
|
|
8580
|
+
if (error2 instanceof import_utils47.AppError) {
|
|
7276
8581
|
throw error2;
|
|
7277
8582
|
} else {
|
|
7278
8583
|
throw new Error("Failed to update building unit.");
|
|
@@ -7299,16 +8604,16 @@ function useBuildingUnitRepo() {
|
|
|
7299
8604
|
}
|
|
7300
8605
|
if (school) {
|
|
7301
8606
|
try {
|
|
7302
|
-
query.school = new
|
|
8607
|
+
query.school = new import_mongodb24.ObjectId(school);
|
|
7303
8608
|
} catch (error) {
|
|
7304
|
-
throw new
|
|
8609
|
+
throw new import_utils47.BadRequestError("Invalid school ID.");
|
|
7305
8610
|
}
|
|
7306
8611
|
}
|
|
7307
8612
|
if (building) {
|
|
7308
8613
|
try {
|
|
7309
|
-
query.building = new
|
|
8614
|
+
query.building = new import_mongodb24.ObjectId(building);
|
|
7310
8615
|
} catch (error) {
|
|
7311
|
-
throw new
|
|
8616
|
+
throw new import_utils47.BadRequestError("Invalid building ID.");
|
|
7312
8617
|
}
|
|
7313
8618
|
}
|
|
7314
8619
|
const cacheParams = {
|
|
@@ -7324,15 +8629,15 @@ function useBuildingUnitRepo() {
|
|
|
7324
8629
|
cacheParams.building = building;
|
|
7325
8630
|
if (status !== "active")
|
|
7326
8631
|
cacheParams.status = status;
|
|
7327
|
-
const cacheKey = (0,
|
|
7328
|
-
|
|
8632
|
+
const cacheKey = (0, import_utils47.makeCacheKey)(namespace_collection, cacheParams);
|
|
8633
|
+
import_utils47.logger.log({
|
|
7329
8634
|
level: "info",
|
|
7330
8635
|
message: `Cache key for getAll building units: ${cacheKey}`
|
|
7331
8636
|
});
|
|
7332
8637
|
try {
|
|
7333
8638
|
const cached = await getCache(cacheKey);
|
|
7334
8639
|
if (cached) {
|
|
7335
|
-
|
|
8640
|
+
import_utils47.logger.log({
|
|
7336
8641
|
level: "info",
|
|
7337
8642
|
message: `Cache hit for getAll building units: ${cacheKey}`
|
|
7338
8643
|
});
|
|
@@ -7345,35 +8650,35 @@ function useBuildingUnitRepo() {
|
|
|
7345
8650
|
{ $limit: limit }
|
|
7346
8651
|
]).toArray();
|
|
7347
8652
|
const length = await collection.countDocuments(query);
|
|
7348
|
-
const data = (0,
|
|
8653
|
+
const data = (0, import_utils47.paginate)(items, page, limit, length);
|
|
7349
8654
|
setCache(cacheKey, data, 600).then(() => {
|
|
7350
|
-
|
|
8655
|
+
import_utils47.logger.log({
|
|
7351
8656
|
level: "info",
|
|
7352
8657
|
message: `Cache set for getAll building units: ${cacheKey}`
|
|
7353
8658
|
});
|
|
7354
8659
|
}).catch((err) => {
|
|
7355
|
-
|
|
8660
|
+
import_utils47.logger.log({
|
|
7356
8661
|
level: "error",
|
|
7357
8662
|
message: `Failed to set cache for getAll building units: ${err.message}`
|
|
7358
8663
|
});
|
|
7359
8664
|
});
|
|
7360
8665
|
return data;
|
|
7361
8666
|
} catch (error) {
|
|
7362
|
-
|
|
8667
|
+
import_utils47.logger.log({ level: "error", message: `${error}` });
|
|
7363
8668
|
throw error;
|
|
7364
8669
|
}
|
|
7365
8670
|
}
|
|
7366
8671
|
async function getById(_id) {
|
|
7367
8672
|
try {
|
|
7368
|
-
_id = new
|
|
8673
|
+
_id = new import_mongodb24.ObjectId(_id);
|
|
7369
8674
|
} catch (error) {
|
|
7370
|
-
throw new
|
|
8675
|
+
throw new import_utils47.BadRequestError("Invalid ID.");
|
|
7371
8676
|
}
|
|
7372
|
-
const cacheKey = (0,
|
|
8677
|
+
const cacheKey = (0, import_utils47.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
7373
8678
|
try {
|
|
7374
8679
|
const cached = await getCache(cacheKey);
|
|
7375
8680
|
if (cached) {
|
|
7376
|
-
|
|
8681
|
+
import_utils47.logger.log({
|
|
7377
8682
|
level: "info",
|
|
7378
8683
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
7379
8684
|
});
|
|
@@ -7384,42 +8689,42 @@ function useBuildingUnitRepo() {
|
|
|
7384
8689
|
deletedAt: { $in: ["", null] }
|
|
7385
8690
|
});
|
|
7386
8691
|
if (!result) {
|
|
7387
|
-
throw new
|
|
8692
|
+
throw new import_utils47.BadRequestError("Building unit not found.");
|
|
7388
8693
|
}
|
|
7389
8694
|
setCache(cacheKey, result, 300).then(() => {
|
|
7390
|
-
|
|
8695
|
+
import_utils47.logger.log({
|
|
7391
8696
|
level: "info",
|
|
7392
8697
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
7393
8698
|
});
|
|
7394
8699
|
}).catch((err) => {
|
|
7395
|
-
|
|
8700
|
+
import_utils47.logger.log({
|
|
7396
8701
|
level: "error",
|
|
7397
8702
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
7398
8703
|
});
|
|
7399
8704
|
});
|
|
7400
8705
|
return result;
|
|
7401
8706
|
} catch (error) {
|
|
7402
|
-
if (error instanceof
|
|
8707
|
+
if (error instanceof import_utils47.AppError) {
|
|
7403
8708
|
throw error;
|
|
7404
8709
|
} else {
|
|
7405
|
-
throw new
|
|
8710
|
+
throw new import_utils47.InternalServerError("Failed to get building unit.");
|
|
7406
8711
|
}
|
|
7407
8712
|
}
|
|
7408
8713
|
}
|
|
7409
8714
|
async function getByBuildingLevel(building, level) {
|
|
7410
8715
|
try {
|
|
7411
|
-
building = new
|
|
8716
|
+
building = new import_mongodb24.ObjectId(building);
|
|
7412
8717
|
} catch (error) {
|
|
7413
|
-
throw new
|
|
8718
|
+
throw new import_utils47.BadRequestError("Invalid building ID.");
|
|
7414
8719
|
}
|
|
7415
|
-
const cacheKey = (0,
|
|
8720
|
+
const cacheKey = (0, import_utils47.makeCacheKey)(namespace_collection, {
|
|
7416
8721
|
building: String(building),
|
|
7417
8722
|
level
|
|
7418
8723
|
});
|
|
7419
8724
|
try {
|
|
7420
8725
|
const cached = await getCache(cacheKey);
|
|
7421
8726
|
if (cached) {
|
|
7422
|
-
|
|
8727
|
+
import_utils47.logger.log({
|
|
7423
8728
|
level: "info",
|
|
7424
8729
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
7425
8730
|
});
|
|
@@ -7431,38 +8736,38 @@ function useBuildingUnitRepo() {
|
|
|
7431
8736
|
status: "active"
|
|
7432
8737
|
});
|
|
7433
8738
|
setCache(cacheKey, result, 300).then(() => {
|
|
7434
|
-
|
|
8739
|
+
import_utils47.logger.log({
|
|
7435
8740
|
level: "info",
|
|
7436
8741
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
7437
8742
|
});
|
|
7438
8743
|
}).catch((err) => {
|
|
7439
|
-
|
|
8744
|
+
import_utils47.logger.log({
|
|
7440
8745
|
level: "error",
|
|
7441
8746
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
7442
8747
|
});
|
|
7443
8748
|
});
|
|
7444
8749
|
return result;
|
|
7445
8750
|
} catch (error) {
|
|
7446
|
-
if (error instanceof
|
|
8751
|
+
if (error instanceof import_utils47.AppError) {
|
|
7447
8752
|
throw error;
|
|
7448
8753
|
} else {
|
|
7449
|
-
throw new
|
|
8754
|
+
throw new import_utils47.InternalServerError("Failed to get building unit.");
|
|
7450
8755
|
}
|
|
7451
8756
|
}
|
|
7452
8757
|
}
|
|
7453
8758
|
async function getByBuilding(building) {
|
|
7454
8759
|
try {
|
|
7455
|
-
building = new
|
|
8760
|
+
building = new import_mongodb24.ObjectId(building);
|
|
7456
8761
|
} catch (error) {
|
|
7457
|
-
throw new
|
|
8762
|
+
throw new import_utils47.BadRequestError("Invalid building ID.");
|
|
7458
8763
|
}
|
|
7459
|
-
const cacheKey = (0,
|
|
8764
|
+
const cacheKey = (0, import_utils47.makeCacheKey)(namespace_collection, {
|
|
7460
8765
|
building: String(building)
|
|
7461
8766
|
});
|
|
7462
8767
|
try {
|
|
7463
8768
|
const cached = await getCache(cacheKey);
|
|
7464
8769
|
if (cached) {
|
|
7465
|
-
|
|
8770
|
+
import_utils47.logger.log({
|
|
7466
8771
|
level: "info",
|
|
7467
8772
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
7468
8773
|
});
|
|
@@ -7473,30 +8778,30 @@ function useBuildingUnitRepo() {
|
|
|
7473
8778
|
status: "active"
|
|
7474
8779
|
});
|
|
7475
8780
|
setCache(cacheKey, result, 300).then(() => {
|
|
7476
|
-
|
|
8781
|
+
import_utils47.logger.log({
|
|
7477
8782
|
level: "info",
|
|
7478
8783
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
7479
8784
|
});
|
|
7480
8785
|
}).catch((err) => {
|
|
7481
|
-
|
|
8786
|
+
import_utils47.logger.log({
|
|
7482
8787
|
level: "error",
|
|
7483
8788
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
7484
8789
|
});
|
|
7485
8790
|
});
|
|
7486
8791
|
return result;
|
|
7487
8792
|
} catch (error) {
|
|
7488
|
-
if (error instanceof
|
|
8793
|
+
if (error instanceof import_utils47.AppError) {
|
|
7489
8794
|
throw error;
|
|
7490
8795
|
} else {
|
|
7491
|
-
throw new
|
|
8796
|
+
throw new import_utils47.InternalServerError("Failed to get building unit.");
|
|
7492
8797
|
}
|
|
7493
8798
|
}
|
|
7494
8799
|
}
|
|
7495
8800
|
async function deleteById(_id, session) {
|
|
7496
8801
|
try {
|
|
7497
|
-
_id = new
|
|
8802
|
+
_id = new import_mongodb24.ObjectId(_id);
|
|
7498
8803
|
} catch (error) {
|
|
7499
|
-
throw new
|
|
8804
|
+
throw new import_utils47.BadRequestError("Invalid ID.");
|
|
7500
8805
|
}
|
|
7501
8806
|
try {
|
|
7502
8807
|
const res = await collection.updateOne(
|
|
@@ -7507,11 +8812,11 @@ function useBuildingUnitRepo() {
|
|
|
7507
8812
|
delCachedData();
|
|
7508
8813
|
return "Room/Facility deleted successfully.";
|
|
7509
8814
|
} catch (error) {
|
|
7510
|
-
|
|
8815
|
+
import_utils47.logger.log({
|
|
7511
8816
|
level: "error",
|
|
7512
8817
|
message: error.message
|
|
7513
8818
|
});
|
|
7514
|
-
if (error instanceof
|
|
8819
|
+
if (error instanceof import_utils47.AppError) {
|
|
7515
8820
|
throw error;
|
|
7516
8821
|
} else {
|
|
7517
8822
|
throw new Error("Failed to deleted room/facility.");
|
|
@@ -7541,16 +8846,16 @@ function useBuildingService() {
|
|
|
7541
8846
|
const { getByBuildingLevel, getByBuilding, updateByBuildingId } = useBuildingUnitRepo();
|
|
7542
8847
|
async function updateById(id, data) {
|
|
7543
8848
|
data.levels = Number(data.levels);
|
|
7544
|
-
const session =
|
|
8849
|
+
const session = import_utils48.useAtlas.getClient()?.startSession();
|
|
7545
8850
|
try {
|
|
7546
8851
|
const building = await _getById(id);
|
|
7547
8852
|
if (!building) {
|
|
7548
|
-
throw new
|
|
8853
|
+
throw new import_utils48.NotFoundError("Building not found.");
|
|
7549
8854
|
}
|
|
7550
8855
|
if (data.levels < building.levels) {
|
|
7551
8856
|
const unit = await getByBuildingLevel(id, building.levels);
|
|
7552
8857
|
if (unit) {
|
|
7553
|
-
throw new
|
|
8858
|
+
throw new import_utils48.BadRequestError(
|
|
7554
8859
|
"Cannot reduce floors, there are existing building units at higher floors."
|
|
7555
8860
|
);
|
|
7556
8861
|
}
|
|
@@ -7572,7 +8877,7 @@ function useBuildingService() {
|
|
|
7572
8877
|
async function deleteById(id) {
|
|
7573
8878
|
const building = await getByBuilding(id);
|
|
7574
8879
|
if (building) {
|
|
7575
|
-
throw new
|
|
8880
|
+
throw new import_utils48.BadRequestError(
|
|
7576
8881
|
"Cannot delete building with existing room/facility. Please delete room/facility first."
|
|
7577
8882
|
);
|
|
7578
8883
|
}
|
|
@@ -7590,24 +8895,24 @@ function useBuildingService() {
|
|
|
7590
8895
|
}
|
|
7591
8896
|
|
|
7592
8897
|
// src/resources/building/building.controller.ts
|
|
7593
|
-
var
|
|
7594
|
-
var
|
|
8898
|
+
var import_utils49 = require("@goweekdays/utils");
|
|
8899
|
+
var import_joi34 = __toESM(require("joi"));
|
|
7595
8900
|
function useBuildingController() {
|
|
7596
8901
|
const { getAll: _getAll, getById: _getById, add: _add } = useBuildingRepo();
|
|
7597
8902
|
const { updateById: _updateById, deleteById: _deleteById } = useBuildingService();
|
|
7598
8903
|
async function createBuilding(req, res, next) {
|
|
7599
8904
|
const value = req.body;
|
|
7600
|
-
const validation =
|
|
7601
|
-
name:
|
|
7602
|
-
school:
|
|
7603
|
-
levels:
|
|
7604
|
-
serial:
|
|
7605
|
-
status:
|
|
8905
|
+
const validation = import_joi34.default.object({
|
|
8906
|
+
name: import_joi34.default.string().required(),
|
|
8907
|
+
school: import_joi34.default.string().hex().required(),
|
|
8908
|
+
levels: import_joi34.default.number().integer().min(1).required(),
|
|
8909
|
+
serial: import_joi34.default.string().optional().allow("", null),
|
|
8910
|
+
status: import_joi34.default.string().optional().allow("", null)
|
|
7606
8911
|
});
|
|
7607
8912
|
const { error } = validation.validate(value);
|
|
7608
8913
|
if (error) {
|
|
7609
|
-
next(new
|
|
7610
|
-
|
|
8914
|
+
next(new import_utils49.BadRequestError(error.message));
|
|
8915
|
+
import_utils49.logger.info(`Controller: ${error.message}`);
|
|
7611
8916
|
return;
|
|
7612
8917
|
}
|
|
7613
8918
|
try {
|
|
@@ -7621,18 +8926,18 @@ function useBuildingController() {
|
|
|
7621
8926
|
async function updateById(req, res, next) {
|
|
7622
8927
|
const value = req.body;
|
|
7623
8928
|
const id = req.params.id ?? "";
|
|
7624
|
-
const validation =
|
|
7625
|
-
id:
|
|
7626
|
-
value:
|
|
7627
|
-
name:
|
|
7628
|
-
serial:
|
|
7629
|
-
levels:
|
|
8929
|
+
const validation = import_joi34.default.object({
|
|
8930
|
+
id: import_joi34.default.string().hex().required(),
|
|
8931
|
+
value: import_joi34.default.object({
|
|
8932
|
+
name: import_joi34.default.string().required(),
|
|
8933
|
+
serial: import_joi34.default.string().optional().allow("", null),
|
|
8934
|
+
levels: import_joi34.default.number().integer().min(1).required()
|
|
7630
8935
|
})
|
|
7631
8936
|
});
|
|
7632
8937
|
const { error } = validation.validate({ id, value });
|
|
7633
8938
|
if (error) {
|
|
7634
|
-
next(new
|
|
7635
|
-
|
|
8939
|
+
next(new import_utils49.BadRequestError(error.message));
|
|
8940
|
+
import_utils49.logger.info(`Controller: ${error.message}`);
|
|
7636
8941
|
return;
|
|
7637
8942
|
}
|
|
7638
8943
|
try {
|
|
@@ -7645,16 +8950,16 @@ function useBuildingController() {
|
|
|
7645
8950
|
}
|
|
7646
8951
|
async function getAll(req, res, next) {
|
|
7647
8952
|
const query = req.query;
|
|
7648
|
-
const validation =
|
|
7649
|
-
page:
|
|
7650
|
-
limit:
|
|
7651
|
-
search:
|
|
7652
|
-
school:
|
|
7653
|
-
status:
|
|
8953
|
+
const validation = import_joi34.default.object({
|
|
8954
|
+
page: import_joi34.default.number().min(1).optional().allow("", null),
|
|
8955
|
+
limit: import_joi34.default.number().min(1).optional().allow("", null),
|
|
8956
|
+
search: import_joi34.default.string().optional().allow("", null),
|
|
8957
|
+
school: import_joi34.default.string().hex().optional().allow("", null),
|
|
8958
|
+
status: import_joi34.default.string().optional().allow("", null)
|
|
7654
8959
|
});
|
|
7655
8960
|
const { error } = validation.validate(query);
|
|
7656
8961
|
if (error) {
|
|
7657
|
-
next(new
|
|
8962
|
+
next(new import_utils49.BadRequestError(error.message));
|
|
7658
8963
|
return;
|
|
7659
8964
|
}
|
|
7660
8965
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -7688,12 +8993,12 @@ function useBuildingController() {
|
|
|
7688
8993
|
}
|
|
7689
8994
|
async function getById(req, res, next) {
|
|
7690
8995
|
const id = req.params.id;
|
|
7691
|
-
const validation =
|
|
7692
|
-
id:
|
|
8996
|
+
const validation = import_joi34.default.object({
|
|
8997
|
+
id: import_joi34.default.string().hex().required()
|
|
7693
8998
|
});
|
|
7694
8999
|
const { error } = validation.validate({ id });
|
|
7695
9000
|
if (error) {
|
|
7696
|
-
next(new
|
|
9001
|
+
next(new import_utils49.BadRequestError(error.message));
|
|
7697
9002
|
return;
|
|
7698
9003
|
}
|
|
7699
9004
|
try {
|
|
@@ -7709,12 +9014,12 @@ function useBuildingController() {
|
|
|
7709
9014
|
}
|
|
7710
9015
|
async function deleteById(req, res, next) {
|
|
7711
9016
|
const id = req.params.id;
|
|
7712
|
-
const validation =
|
|
7713
|
-
id:
|
|
9017
|
+
const validation = import_joi34.default.object({
|
|
9018
|
+
id: import_joi34.default.string().hex().required()
|
|
7714
9019
|
});
|
|
7715
9020
|
const { error } = validation.validate({ id });
|
|
7716
9021
|
if (error) {
|
|
7717
|
-
next(new
|
|
9022
|
+
next(new import_utils49.BadRequestError(error.message));
|
|
7718
9023
|
return;
|
|
7719
9024
|
}
|
|
7720
9025
|
try {
|
|
@@ -7735,11 +9040,11 @@ function useBuildingController() {
|
|
|
7735
9040
|
}
|
|
7736
9041
|
|
|
7737
9042
|
// src/resources/building/building-unit.service.ts
|
|
7738
|
-
var
|
|
9043
|
+
var import_utils50 = require("@goweekdays/utils");
|
|
7739
9044
|
function useBuildingUnitService() {
|
|
7740
9045
|
const { add: _add } = useBuildingUnitRepo();
|
|
7741
9046
|
async function add(value) {
|
|
7742
|
-
const session =
|
|
9047
|
+
const session = import_utils50.useAtlas.getClient()?.startSession();
|
|
7743
9048
|
if (!session) {
|
|
7744
9049
|
throw new Error("Unable to start session for building unit service.");
|
|
7745
9050
|
}
|
|
@@ -7766,8 +9071,8 @@ function useBuildingUnitService() {
|
|
|
7766
9071
|
}
|
|
7767
9072
|
|
|
7768
9073
|
// src/resources/building/building-unit.controller.ts
|
|
7769
|
-
var
|
|
7770
|
-
var
|
|
9074
|
+
var import_utils51 = require("@goweekdays/utils");
|
|
9075
|
+
var import_joi35 = __toESM(require("joi"));
|
|
7771
9076
|
function useBuildingUnitController() {
|
|
7772
9077
|
const {
|
|
7773
9078
|
getAll: _getAll,
|
|
@@ -7778,27 +9083,27 @@ function useBuildingUnitController() {
|
|
|
7778
9083
|
const { add: _add } = useBuildingUnitService();
|
|
7779
9084
|
async function add(req, res, next) {
|
|
7780
9085
|
const data = req.body;
|
|
7781
|
-
const validation =
|
|
7782
|
-
building:
|
|
7783
|
-
school:
|
|
7784
|
-
name:
|
|
7785
|
-
building:
|
|
7786
|
-
buildingName:
|
|
7787
|
-
level:
|
|
7788
|
-
category:
|
|
7789
|
-
type:
|
|
7790
|
-
seating_capacity:
|
|
7791
|
-
standing_capacity:
|
|
7792
|
-
description:
|
|
7793
|
-
unit_of_measurement:
|
|
7794
|
-
area:
|
|
7795
|
-
status:
|
|
9086
|
+
const validation = import_joi35.default.object({
|
|
9087
|
+
building: import_joi35.default.object({
|
|
9088
|
+
school: import_joi35.default.string().hex().required(),
|
|
9089
|
+
name: import_joi35.default.string().optional().allow("", null),
|
|
9090
|
+
building: import_joi35.default.string().hex().required(),
|
|
9091
|
+
buildingName: import_joi35.default.string().optional().allow("", null),
|
|
9092
|
+
level: import_joi35.default.number().integer().min(1).required(),
|
|
9093
|
+
category: import_joi35.default.string().required(),
|
|
9094
|
+
type: import_joi35.default.string().required(),
|
|
9095
|
+
seating_capacity: import_joi35.default.number().integer().min(0).required(),
|
|
9096
|
+
standing_capacity: import_joi35.default.number().integer().min(0).required(),
|
|
9097
|
+
description: import_joi35.default.string().optional().allow("", null),
|
|
9098
|
+
unit_of_measurement: import_joi35.default.string().valid("sqm").required(),
|
|
9099
|
+
area: import_joi35.default.number().positive().required(),
|
|
9100
|
+
status: import_joi35.default.string().optional().allow("", null)
|
|
7796
9101
|
}),
|
|
7797
|
-
qty:
|
|
9102
|
+
qty: import_joi35.default.number().integer().min(1).max(20).optional().default(1)
|
|
7798
9103
|
});
|
|
7799
9104
|
const { error } = validation.validate(data);
|
|
7800
9105
|
if (error) {
|
|
7801
|
-
next(new
|
|
9106
|
+
next(new import_utils51.BadRequestError(error.message));
|
|
7802
9107
|
return;
|
|
7803
9108
|
}
|
|
7804
9109
|
try {
|
|
@@ -7814,13 +9119,13 @@ function useBuildingUnitController() {
|
|
|
7814
9119
|
async function updateById(req, res, next) {
|
|
7815
9120
|
const data = req.body;
|
|
7816
9121
|
const id = req.params.id ?? "";
|
|
7817
|
-
const validation =
|
|
7818
|
-
id:
|
|
9122
|
+
const validation = import_joi35.default.object({
|
|
9123
|
+
id: import_joi35.default.string().hex().required(),
|
|
7819
9124
|
value: schemaUpdateOptions
|
|
7820
9125
|
});
|
|
7821
9126
|
const { error } = validation.validate({ id, value: data });
|
|
7822
9127
|
if (error) {
|
|
7823
|
-
next(new
|
|
9128
|
+
next(new import_utils51.BadRequestError(error.message));
|
|
7824
9129
|
return;
|
|
7825
9130
|
}
|
|
7826
9131
|
try {
|
|
@@ -7835,17 +9140,17 @@ function useBuildingUnitController() {
|
|
|
7835
9140
|
}
|
|
7836
9141
|
async function getAll(req, res, next) {
|
|
7837
9142
|
const query = req.query;
|
|
7838
|
-
const validation =
|
|
7839
|
-
page:
|
|
7840
|
-
limit:
|
|
7841
|
-
search:
|
|
7842
|
-
school:
|
|
7843
|
-
building:
|
|
7844
|
-
status:
|
|
9143
|
+
const validation = import_joi35.default.object({
|
|
9144
|
+
page: import_joi35.default.number().min(1).optional().allow("", null),
|
|
9145
|
+
limit: import_joi35.default.number().min(1).optional().allow("", null),
|
|
9146
|
+
search: import_joi35.default.string().optional().allow("", null),
|
|
9147
|
+
school: import_joi35.default.string().hex().optional().allow("", null),
|
|
9148
|
+
building: import_joi35.default.string().hex().optional().allow("", null),
|
|
9149
|
+
status: import_joi35.default.string().optional().allow("", null)
|
|
7845
9150
|
});
|
|
7846
9151
|
const { error } = validation.validate(query);
|
|
7847
9152
|
if (error) {
|
|
7848
|
-
next(new
|
|
9153
|
+
next(new import_utils51.BadRequestError(error.message));
|
|
7849
9154
|
return;
|
|
7850
9155
|
}
|
|
7851
9156
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -7881,12 +9186,12 @@ function useBuildingUnitController() {
|
|
|
7881
9186
|
}
|
|
7882
9187
|
async function getById(req, res, next) {
|
|
7883
9188
|
const id = req.params.id;
|
|
7884
|
-
const validation =
|
|
7885
|
-
id:
|
|
9189
|
+
const validation = import_joi35.default.object({
|
|
9190
|
+
id: import_joi35.default.string().hex().required()
|
|
7886
9191
|
});
|
|
7887
9192
|
const { error } = validation.validate({ id });
|
|
7888
9193
|
if (error) {
|
|
7889
|
-
next(new
|
|
9194
|
+
next(new import_utils51.BadRequestError(error.message));
|
|
7890
9195
|
return;
|
|
7891
9196
|
}
|
|
7892
9197
|
try {
|
|
@@ -7902,12 +9207,12 @@ function useBuildingUnitController() {
|
|
|
7902
9207
|
}
|
|
7903
9208
|
async function deleteById(req, res, next) {
|
|
7904
9209
|
const id = req.params.id;
|
|
7905
|
-
const validation =
|
|
7906
|
-
id:
|
|
9210
|
+
const validation = import_joi35.default.object({
|
|
9211
|
+
id: import_joi35.default.string().hex().required()
|
|
7907
9212
|
});
|
|
7908
9213
|
const { error } = validation.validate({ id });
|
|
7909
9214
|
if (error) {
|
|
7910
|
-
next(new
|
|
9215
|
+
next(new import_utils51.BadRequestError(error.message));
|
|
7911
9216
|
return;
|
|
7912
9217
|
}
|
|
7913
9218
|
try {
|
|
@@ -7928,14 +9233,14 @@ function useBuildingUnitController() {
|
|
|
7928
9233
|
}
|
|
7929
9234
|
|
|
7930
9235
|
// src/resources/counter/counter.model.ts
|
|
7931
|
-
var
|
|
7932
|
-
var
|
|
9236
|
+
var import_utils52 = require("@goweekdays/utils");
|
|
9237
|
+
var import_mongodb25 = require("mongodb");
|
|
7933
9238
|
var import_zod = require("zod");
|
|
7934
9239
|
var TCounter = import_zod.z.object({
|
|
7935
9240
|
_id: import_zod.z.union([
|
|
7936
9241
|
import_zod.z.string().length(24, "Invalid ObjectId hex string"),
|
|
7937
|
-
import_zod.z.instanceof(
|
|
7938
|
-
]).transform((val) => typeof val === "string" ? new
|
|
9242
|
+
import_zod.z.instanceof(import_mongodb25.ObjectId)
|
|
9243
|
+
]).transform((val) => typeof val === "string" ? new import_mongodb25.ObjectId(val) : val).optional(),
|
|
7939
9244
|
count: import_zod.z.number().int().min(0).default(0),
|
|
7940
9245
|
type: import_zod.z.string(),
|
|
7941
9246
|
createdAt: import_zod.z.date().optional().default(() => /* @__PURE__ */ new Date()),
|
|
@@ -7948,7 +9253,7 @@ function useCounterModel(db) {
|
|
|
7948
9253
|
try {
|
|
7949
9254
|
return TCounter.parse(value);
|
|
7950
9255
|
} catch (error) {
|
|
7951
|
-
throw new
|
|
9256
|
+
throw new import_utils52.BadRequestError(error.issues[0].message);
|
|
7952
9257
|
}
|
|
7953
9258
|
}
|
|
7954
9259
|
function validateCounter(data) {
|
|
@@ -7962,23 +9267,23 @@ function useCounterModel(db) {
|
|
|
7962
9267
|
}
|
|
7963
9268
|
|
|
7964
9269
|
// src/resources/counter/counter.repository.ts
|
|
7965
|
-
var
|
|
9270
|
+
var import_utils53 = require("@goweekdays/utils");
|
|
7966
9271
|
function useCounterRepo() {
|
|
7967
|
-
const db =
|
|
9272
|
+
const db = import_utils53.useAtlas.getDb();
|
|
7968
9273
|
if (!db) {
|
|
7969
9274
|
throw new Error("Unable to connect to server.");
|
|
7970
9275
|
}
|
|
7971
9276
|
const namespace_collection = "counters";
|
|
7972
9277
|
const { collection, createCounter } = useCounterModel(db);
|
|
7973
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
9278
|
+
const { getCache, setCache, delNamespace } = (0, import_utils53.useCache)(namespace_collection);
|
|
7974
9279
|
function delCachedData() {
|
|
7975
9280
|
delNamespace().then(() => {
|
|
7976
|
-
|
|
9281
|
+
import_utils53.logger.log({
|
|
7977
9282
|
level: "info",
|
|
7978
9283
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
7979
9284
|
});
|
|
7980
9285
|
}).catch((err) => {
|
|
7981
|
-
|
|
9286
|
+
import_utils53.logger.log({
|
|
7982
9287
|
level: "error",
|
|
7983
9288
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
7984
9289
|
});
|
|
@@ -8030,11 +9335,11 @@ function useCounterRepo() {
|
|
|
8030
9335
|
}
|
|
8031
9336
|
}
|
|
8032
9337
|
async function getByType(type) {
|
|
8033
|
-
const cacheKey = (0,
|
|
9338
|
+
const cacheKey = (0, import_utils53.makeCacheKey)(namespace_collection, { type });
|
|
8034
9339
|
try {
|
|
8035
9340
|
const cached = await getCache(cacheKey);
|
|
8036
9341
|
if (cached) {
|
|
8037
|
-
|
|
9342
|
+
import_utils53.logger.log({
|
|
8038
9343
|
level: "info",
|
|
8039
9344
|
message: `Cache hit for getByType counter: ${cacheKey}`
|
|
8040
9345
|
});
|
|
@@ -8043,12 +9348,12 @@ function useCounterRepo() {
|
|
|
8043
9348
|
const data = await collection.findOne({ type });
|
|
8044
9349
|
if (data) {
|
|
8045
9350
|
setCache(cacheKey, data, 300).then(() => {
|
|
8046
|
-
|
|
9351
|
+
import_utils53.logger.log({
|
|
8047
9352
|
level: "info",
|
|
8048
9353
|
message: `Cache set for counter by type: ${cacheKey}`
|
|
8049
9354
|
});
|
|
8050
9355
|
}).catch((err) => {
|
|
8051
|
-
|
|
9356
|
+
import_utils53.logger.log({
|
|
8052
9357
|
level: "error",
|
|
8053
9358
|
message: `Failed to set cache for counter by type: ${err.message}`
|
|
8054
9359
|
});
|
|
@@ -8069,7 +9374,7 @@ function useCounterRepo() {
|
|
|
8069
9374
|
}
|
|
8070
9375
|
|
|
8071
9376
|
// src/resources/file/file.service.ts
|
|
8072
|
-
var
|
|
9377
|
+
var import_utils54 = require("@goweekdays/utils");
|
|
8073
9378
|
var import_node_cron = __toESM(require("node-cron"));
|
|
8074
9379
|
var fs = __toESM(require("fs"));
|
|
8075
9380
|
function useFileService() {
|
|
@@ -8078,7 +9383,7 @@ function useFileService() {
|
|
|
8078
9383
|
deleteFileById,
|
|
8079
9384
|
getAllDraftedFiles
|
|
8080
9385
|
} = useFileRepo();
|
|
8081
|
-
const s3 = new
|
|
9386
|
+
const s3 = new import_utils54.useS3({
|
|
8082
9387
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
8083
9388
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
8084
9389
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -8087,7 +9392,7 @@ function useFileService() {
|
|
|
8087
9392
|
forcePathStyle: true
|
|
8088
9393
|
});
|
|
8089
9394
|
async function createFile(value) {
|
|
8090
|
-
const session =
|
|
9395
|
+
const session = import_utils54.useAtlas.getClient()?.startSession();
|
|
8091
9396
|
session?.startTransaction();
|
|
8092
9397
|
const file = {
|
|
8093
9398
|
name: value.originalname,
|
|
@@ -8121,7 +9426,7 @@ function useFileService() {
|
|
|
8121
9426
|
}
|
|
8122
9427
|
}
|
|
8123
9428
|
async function deleteFile(id) {
|
|
8124
|
-
const session =
|
|
9429
|
+
const session = import_utils54.useAtlas.getClient()?.startSession();
|
|
8125
9430
|
session?.startTransaction();
|
|
8126
9431
|
try {
|
|
8127
9432
|
await deleteFileById(id, session);
|
|
@@ -8142,12 +9447,12 @@ function useFileService() {
|
|
|
8142
9447
|
const file = files[index];
|
|
8143
9448
|
try {
|
|
8144
9449
|
await deleteFile(file._id.toString());
|
|
8145
|
-
await
|
|
9450
|
+
await import_utils54.logger.log({
|
|
8146
9451
|
level: "info",
|
|
8147
9452
|
message: "Successfully deleted draft files."
|
|
8148
9453
|
});
|
|
8149
9454
|
} catch (error) {
|
|
8150
|
-
|
|
9455
|
+
import_utils54.logger.log({
|
|
8151
9456
|
level: "info",
|
|
8152
9457
|
message: "Successfully deleted draft files."
|
|
8153
9458
|
});
|
|
@@ -8164,8 +9469,8 @@ function useFileService() {
|
|
|
8164
9469
|
}
|
|
8165
9470
|
|
|
8166
9471
|
// src/resources/file/file.controller.ts
|
|
8167
|
-
var
|
|
8168
|
-
var
|
|
9472
|
+
var import_utils55 = require("@goweekdays/utils");
|
|
9473
|
+
var import_joi36 = __toESM(require("joi"));
|
|
8169
9474
|
function useFileController() {
|
|
8170
9475
|
const { createFile, deleteFile: _deleteFile } = useFileService();
|
|
8171
9476
|
async function upload(req, res, next) {
|
|
@@ -8178,29 +9483,29 @@ function useFileController() {
|
|
|
8178
9483
|
res.json({ message: "Successfully uploaded file", id });
|
|
8179
9484
|
return;
|
|
8180
9485
|
} catch (error) {
|
|
8181
|
-
if (error instanceof
|
|
9486
|
+
if (error instanceof import_utils55.AppError) {
|
|
8182
9487
|
next(error);
|
|
8183
9488
|
} else {
|
|
8184
|
-
next(new
|
|
9489
|
+
next(new import_utils55.InternalServerError(error));
|
|
8185
9490
|
}
|
|
8186
9491
|
}
|
|
8187
9492
|
}
|
|
8188
9493
|
async function deleteFile(req, res, next) {
|
|
8189
9494
|
const id = req.params.id;
|
|
8190
|
-
const validation =
|
|
9495
|
+
const validation = import_joi36.default.string().required();
|
|
8191
9496
|
const { error } = validation.validate(id);
|
|
8192
9497
|
if (error) {
|
|
8193
|
-
next(new
|
|
9498
|
+
next(new import_utils55.BadRequestError(error.message));
|
|
8194
9499
|
}
|
|
8195
9500
|
try {
|
|
8196
9501
|
const message = await _deleteFile(id);
|
|
8197
9502
|
res.json({ message });
|
|
8198
9503
|
return;
|
|
8199
9504
|
} catch (error2) {
|
|
8200
|
-
if (error2 instanceof
|
|
9505
|
+
if (error2 instanceof import_utils55.AppError) {
|
|
8201
9506
|
next(error2);
|
|
8202
9507
|
} else {
|
|
8203
|
-
next(new
|
|
9508
|
+
next(new import_utils55.InternalServerError(error2));
|
|
8204
9509
|
}
|
|
8205
9510
|
}
|
|
8206
9511
|
}
|
|
@@ -8211,11 +9516,11 @@ function useFileController() {
|
|
|
8211
9516
|
}
|
|
8212
9517
|
|
|
8213
9518
|
// src/resources/psgc/psgc.model.ts
|
|
8214
|
-
var
|
|
8215
|
-
var schemaPSGC =
|
|
8216
|
-
code:
|
|
8217
|
-
name:
|
|
8218
|
-
type:
|
|
9519
|
+
var import_joi37 = __toESM(require("joi"));
|
|
9520
|
+
var schemaPSGC = import_joi37.default.object({
|
|
9521
|
+
code: import_joi37.default.string().length(10).required(),
|
|
9522
|
+
name: import_joi37.default.string().required(),
|
|
9523
|
+
type: import_joi37.default.string().valid("Reg", "Prov", "City", "Mun", "Bgy").required()
|
|
8219
9524
|
});
|
|
8220
9525
|
function modelPSGC(data) {
|
|
8221
9526
|
const { error } = schemaPSGC.validate(data);
|
|
@@ -8230,16 +9535,16 @@ function modelPSGC(data) {
|
|
|
8230
9535
|
}
|
|
8231
9536
|
|
|
8232
9537
|
// src/resources/psgc/psgc.repository.ts
|
|
8233
|
-
var
|
|
8234
|
-
var
|
|
9538
|
+
var import_utils56 = require("@goweekdays/utils");
|
|
9539
|
+
var import_mongodb26 = require("mongodb");
|
|
8235
9540
|
function usePSGCRepo() {
|
|
8236
|
-
const db =
|
|
9541
|
+
const db = import_utils56.useAtlas.getDb();
|
|
8237
9542
|
if (!db) {
|
|
8238
9543
|
throw new Error("Unable to connect to server.");
|
|
8239
9544
|
}
|
|
8240
9545
|
const namespace_collection = "psgc";
|
|
8241
9546
|
const collection = db.collection(namespace_collection);
|
|
8242
|
-
const { getCache, setCache, delNamespace } = (0,
|
|
9547
|
+
const { getCache, setCache, delNamespace } = (0, import_utils56.useCache)(namespace_collection);
|
|
8243
9548
|
async function createIndexes() {
|
|
8244
9549
|
try {
|
|
8245
9550
|
await collection.createIndexes([
|
|
@@ -8253,12 +9558,12 @@ function usePSGCRepo() {
|
|
|
8253
9558
|
}
|
|
8254
9559
|
function delCachedData() {
|
|
8255
9560
|
delNamespace().then(() => {
|
|
8256
|
-
|
|
9561
|
+
import_utils56.logger.log({
|
|
8257
9562
|
level: "info",
|
|
8258
9563
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
8259
9564
|
});
|
|
8260
9565
|
}).catch((err) => {
|
|
8261
|
-
|
|
9566
|
+
import_utils56.logger.log({
|
|
8262
9567
|
level: "error",
|
|
8263
9568
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
8264
9569
|
});
|
|
@@ -8271,16 +9576,16 @@ function usePSGCRepo() {
|
|
|
8271
9576
|
delCachedData();
|
|
8272
9577
|
return res.insertedId;
|
|
8273
9578
|
} catch (error) {
|
|
8274
|
-
|
|
9579
|
+
import_utils56.logger.log({
|
|
8275
9580
|
level: "error",
|
|
8276
9581
|
message: error.message
|
|
8277
9582
|
});
|
|
8278
|
-
if (error instanceof
|
|
9583
|
+
if (error instanceof import_utils56.AppError) {
|
|
8279
9584
|
throw error;
|
|
8280
9585
|
} else {
|
|
8281
9586
|
const isDuplicated = error.message.includes("duplicate");
|
|
8282
9587
|
if (isDuplicated) {
|
|
8283
|
-
throw new
|
|
9588
|
+
throw new import_utils56.BadRequestError("Region already exists.");
|
|
8284
9589
|
}
|
|
8285
9590
|
throw new Error("Failed to create PSGC.");
|
|
8286
9591
|
}
|
|
@@ -8316,15 +9621,15 @@ function usePSGCRepo() {
|
|
|
8316
9621
|
query.$text = { $search: search };
|
|
8317
9622
|
cacheKeyOptions.search = search;
|
|
8318
9623
|
}
|
|
8319
|
-
const cacheKey = (0,
|
|
8320
|
-
|
|
9624
|
+
const cacheKey = (0, import_utils56.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
9625
|
+
import_utils56.logger.log({
|
|
8321
9626
|
level: "info",
|
|
8322
9627
|
message: `Cache key for getAll PSGC: ${cacheKey}`
|
|
8323
9628
|
});
|
|
8324
9629
|
try {
|
|
8325
9630
|
const cached = await getCache(cacheKey);
|
|
8326
9631
|
if (cached) {
|
|
8327
|
-
|
|
9632
|
+
import_utils56.logger.log({
|
|
8328
9633
|
level: "info",
|
|
8329
9634
|
message: `Cache hit for getAll PSGC: ${cacheKey}`
|
|
8330
9635
|
});
|
|
@@ -8337,35 +9642,35 @@ function usePSGCRepo() {
|
|
|
8337
9642
|
{ $limit: limit }
|
|
8338
9643
|
]).toArray();
|
|
8339
9644
|
const length = await collection.countDocuments(query);
|
|
8340
|
-
const data = (0,
|
|
9645
|
+
const data = (0, import_utils56.paginate)(items, page, limit, length);
|
|
8341
9646
|
setCache(cacheKey, data, 600).then(() => {
|
|
8342
|
-
|
|
9647
|
+
import_utils56.logger.log({
|
|
8343
9648
|
level: "info",
|
|
8344
9649
|
message: `Cache set for getAll PSGC: ${cacheKey}`
|
|
8345
9650
|
});
|
|
8346
9651
|
}).catch((err) => {
|
|
8347
|
-
|
|
9652
|
+
import_utils56.logger.log({
|
|
8348
9653
|
level: "error",
|
|
8349
9654
|
message: `Failed to set cache for getAll PSGC: ${err.message}`
|
|
8350
9655
|
});
|
|
8351
9656
|
});
|
|
8352
9657
|
return data;
|
|
8353
9658
|
} catch (error) {
|
|
8354
|
-
|
|
9659
|
+
import_utils56.logger.log({ level: "error", message: `${error}` });
|
|
8355
9660
|
throw error;
|
|
8356
9661
|
}
|
|
8357
9662
|
}
|
|
8358
9663
|
async function getById(_id) {
|
|
8359
9664
|
try {
|
|
8360
|
-
_id = new
|
|
9665
|
+
_id = new import_mongodb26.ObjectId(_id);
|
|
8361
9666
|
} catch (error) {
|
|
8362
|
-
throw new
|
|
9667
|
+
throw new import_utils56.BadRequestError("Invalid ID.");
|
|
8363
9668
|
}
|
|
8364
|
-
const cacheKey = (0,
|
|
9669
|
+
const cacheKey = (0, import_utils56.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
8365
9670
|
try {
|
|
8366
9671
|
const cached = await getCache(cacheKey);
|
|
8367
9672
|
if (cached) {
|
|
8368
|
-
|
|
9673
|
+
import_utils56.logger.log({
|
|
8369
9674
|
level: "info",
|
|
8370
9675
|
message: `Cache hit for getById PSGC: ${cacheKey}`
|
|
8371
9676
|
});
|
|
@@ -8376,25 +9681,25 @@ function usePSGCRepo() {
|
|
|
8376
9681
|
deletedAt: { $in: ["", null] }
|
|
8377
9682
|
});
|
|
8378
9683
|
if (!result) {
|
|
8379
|
-
throw new
|
|
9684
|
+
throw new import_utils56.BadRequestError("Region not found.");
|
|
8380
9685
|
}
|
|
8381
9686
|
setCache(cacheKey, result, 300).then(() => {
|
|
8382
|
-
|
|
9687
|
+
import_utils56.logger.log({
|
|
8383
9688
|
level: "info",
|
|
8384
9689
|
message: `Cache set for PSGC by id: ${cacheKey}`
|
|
8385
9690
|
});
|
|
8386
9691
|
}).catch((err) => {
|
|
8387
|
-
|
|
9692
|
+
import_utils56.logger.log({
|
|
8388
9693
|
level: "error",
|
|
8389
9694
|
message: `Failed to set cache for PSGC by id: ${err.message}`
|
|
8390
9695
|
});
|
|
8391
9696
|
});
|
|
8392
9697
|
return result;
|
|
8393
9698
|
} catch (error) {
|
|
8394
|
-
if (error instanceof
|
|
9699
|
+
if (error instanceof import_utils56.AppError) {
|
|
8395
9700
|
throw error;
|
|
8396
9701
|
} else {
|
|
8397
|
-
throw new
|
|
9702
|
+
throw new import_utils56.InternalServerError("Failed to get PSGC.");
|
|
8398
9703
|
}
|
|
8399
9704
|
}
|
|
8400
9705
|
}
|
|
@@ -8418,15 +9723,15 @@ function usePSGCRepo() {
|
|
|
8418
9723
|
query.code = { $regex: `^${prefix}` };
|
|
8419
9724
|
cacheKeyOptions.prefix = prefix;
|
|
8420
9725
|
}
|
|
8421
|
-
const cacheKey = (0,
|
|
8422
|
-
|
|
9726
|
+
const cacheKey = (0, import_utils56.makeCacheKey)(namespace_collection, { name });
|
|
9727
|
+
import_utils56.logger.log({
|
|
8423
9728
|
level: "info",
|
|
8424
9729
|
message: `Query for getByName PSGC: ${JSON.stringify(query)}`
|
|
8425
9730
|
});
|
|
8426
9731
|
try {
|
|
8427
9732
|
const cached = await getCache(cacheKey);
|
|
8428
9733
|
if (cached) {
|
|
8429
|
-
|
|
9734
|
+
import_utils56.logger.log({
|
|
8430
9735
|
level: "info",
|
|
8431
9736
|
message: `Cache hit for getByName PSGC: ${cacheKey}`
|
|
8432
9737
|
});
|
|
@@ -8434,36 +9739,36 @@ function usePSGCRepo() {
|
|
|
8434
9739
|
}
|
|
8435
9740
|
const result = await collection.findOne(query);
|
|
8436
9741
|
setCache(cacheKey, result, 300).then(() => {
|
|
8437
|
-
|
|
9742
|
+
import_utils56.logger.log({
|
|
8438
9743
|
level: "info",
|
|
8439
9744
|
message: `Cache set for PSGC by name: ${cacheKey}`
|
|
8440
9745
|
});
|
|
8441
9746
|
}).catch((err) => {
|
|
8442
|
-
|
|
9747
|
+
import_utils56.logger.log({
|
|
8443
9748
|
level: "error",
|
|
8444
9749
|
message: `Failed to set cache for PSGC by name: ${err.message}`
|
|
8445
9750
|
});
|
|
8446
9751
|
});
|
|
8447
9752
|
return result;
|
|
8448
9753
|
} catch (error) {
|
|
8449
|
-
if (error instanceof
|
|
9754
|
+
if (error instanceof import_utils56.AppError) {
|
|
8450
9755
|
throw error;
|
|
8451
9756
|
} else {
|
|
8452
|
-
throw new
|
|
9757
|
+
throw new import_utils56.InternalServerError("Failed to get PSGC.");
|
|
8453
9758
|
}
|
|
8454
9759
|
}
|
|
8455
9760
|
}
|
|
8456
9761
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
8457
9762
|
const allowedFields = ["name"];
|
|
8458
9763
|
if (!allowedFields.includes(field)) {
|
|
8459
|
-
throw new
|
|
9764
|
+
throw new import_utils56.BadRequestError(
|
|
8460
9765
|
`Field "${field}" is not allowed to be updated.`
|
|
8461
9766
|
);
|
|
8462
9767
|
}
|
|
8463
9768
|
try {
|
|
8464
|
-
_id = new
|
|
9769
|
+
_id = new import_mongodb26.ObjectId(_id);
|
|
8465
9770
|
} catch (error) {
|
|
8466
|
-
throw new
|
|
9771
|
+
throw new import_utils56.BadRequestError("Invalid ID.");
|
|
8467
9772
|
}
|
|
8468
9773
|
try {
|
|
8469
9774
|
await collection.updateOne(
|
|
@@ -8474,14 +9779,14 @@ function usePSGCRepo() {
|
|
|
8474
9779
|
delCachedData();
|
|
8475
9780
|
return `Successfully updated PSGC ${field}.`;
|
|
8476
9781
|
} catch (error) {
|
|
8477
|
-
throw new
|
|
9782
|
+
throw new import_utils56.InternalServerError(`Failed to update PSGC ${field}.`);
|
|
8478
9783
|
}
|
|
8479
9784
|
}
|
|
8480
9785
|
async function deleteById(_id) {
|
|
8481
9786
|
try {
|
|
8482
|
-
_id = new
|
|
9787
|
+
_id = new import_mongodb26.ObjectId(_id);
|
|
8483
9788
|
} catch (error) {
|
|
8484
|
-
throw new
|
|
9789
|
+
throw new import_utils56.BadRequestError("Invalid ID.");
|
|
8485
9790
|
}
|
|
8486
9791
|
try {
|
|
8487
9792
|
await collection.updateOne(
|
|
@@ -8491,7 +9796,7 @@ function usePSGCRepo() {
|
|
|
8491
9796
|
delCachedData();
|
|
8492
9797
|
return "Successfully deleted PSGC.";
|
|
8493
9798
|
} catch (error) {
|
|
8494
|
-
throw new
|
|
9799
|
+
throw new import_utils56.InternalServerError("Failed to delete PSGC.");
|
|
8495
9800
|
}
|
|
8496
9801
|
}
|
|
8497
9802
|
return {
|
|
@@ -8506,8 +9811,8 @@ function usePSGCRepo() {
|
|
|
8506
9811
|
}
|
|
8507
9812
|
|
|
8508
9813
|
// src/resources/psgc/psgc.controller.ts
|
|
8509
|
-
var
|
|
8510
|
-
var
|
|
9814
|
+
var import_utils57 = require("@goweekdays/utils");
|
|
9815
|
+
var import_joi38 = __toESM(require("joi"));
|
|
8511
9816
|
function usePSGCController() {
|
|
8512
9817
|
const {
|
|
8513
9818
|
add: _add,
|
|
@@ -8521,7 +9826,7 @@ function usePSGCController() {
|
|
|
8521
9826
|
const value = req.body;
|
|
8522
9827
|
const { error } = schemaPSGC.validate(value);
|
|
8523
9828
|
if (error) {
|
|
8524
|
-
next(new
|
|
9829
|
+
next(new import_utils57.BadRequestError(error.message));
|
|
8525
9830
|
return;
|
|
8526
9831
|
}
|
|
8527
9832
|
try {
|
|
@@ -8537,12 +9842,12 @@ function usePSGCController() {
|
|
|
8537
9842
|
}
|
|
8538
9843
|
async function getAll(req, res, next) {
|
|
8539
9844
|
const query = req.query;
|
|
8540
|
-
const validation =
|
|
8541
|
-
page:
|
|
8542
|
-
limit:
|
|
8543
|
-
search:
|
|
8544
|
-
type:
|
|
8545
|
-
prefix:
|
|
9845
|
+
const validation = import_joi38.default.object({
|
|
9846
|
+
page: import_joi38.default.number().min(1).optional().allow("", null),
|
|
9847
|
+
limit: import_joi38.default.number().min(1).optional().allow("", null),
|
|
9848
|
+
search: import_joi38.default.string().optional().allow("", null),
|
|
9849
|
+
type: import_joi38.default.string().valid("Reg", "Prov", "City", "Mun", "Bgy").required(),
|
|
9850
|
+
prefix: import_joi38.default.string().optional().allow("", null)
|
|
8546
9851
|
});
|
|
8547
9852
|
const { error } = validation.validate(query);
|
|
8548
9853
|
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
@@ -8552,16 +9857,16 @@ function usePSGCController() {
|
|
|
8552
9857
|
const prefix = req.query.prefix ? String(req.query.prefix) : "";
|
|
8553
9858
|
const isPageNumber = isFinite(page);
|
|
8554
9859
|
if (!isPageNumber) {
|
|
8555
|
-
next(new
|
|
9860
|
+
next(new import_utils57.BadRequestError("Invalid page number."));
|
|
8556
9861
|
return;
|
|
8557
9862
|
}
|
|
8558
9863
|
const isLimitNumber = isFinite(limit);
|
|
8559
9864
|
if (!isLimitNumber) {
|
|
8560
|
-
next(new
|
|
9865
|
+
next(new import_utils57.BadRequestError("Invalid limit number."));
|
|
8561
9866
|
return;
|
|
8562
9867
|
}
|
|
8563
9868
|
if (error) {
|
|
8564
|
-
next(new
|
|
9869
|
+
next(new import_utils57.BadRequestError(error.message));
|
|
8565
9870
|
return;
|
|
8566
9871
|
}
|
|
8567
9872
|
try {
|
|
@@ -8580,12 +9885,12 @@ function usePSGCController() {
|
|
|
8580
9885
|
}
|
|
8581
9886
|
async function getById(req, res, next) {
|
|
8582
9887
|
const id = req.params.id;
|
|
8583
|
-
const validation =
|
|
8584
|
-
id:
|
|
9888
|
+
const validation = import_joi38.default.object({
|
|
9889
|
+
id: import_joi38.default.string().hex().required()
|
|
8585
9890
|
});
|
|
8586
9891
|
const { error } = validation.validate({ id });
|
|
8587
9892
|
if (error) {
|
|
8588
|
-
next(new
|
|
9893
|
+
next(new import_utils57.BadRequestError(error.message));
|
|
8589
9894
|
return;
|
|
8590
9895
|
}
|
|
8591
9896
|
try {
|
|
@@ -8601,12 +9906,12 @@ function usePSGCController() {
|
|
|
8601
9906
|
}
|
|
8602
9907
|
async function getByName(req, res, next) {
|
|
8603
9908
|
const name = req.params.name;
|
|
8604
|
-
const validation =
|
|
8605
|
-
name:
|
|
9909
|
+
const validation = import_joi38.default.object({
|
|
9910
|
+
name: import_joi38.default.string().required()
|
|
8606
9911
|
});
|
|
8607
9912
|
const { error } = validation.validate({ name });
|
|
8608
9913
|
if (error) {
|
|
8609
|
-
next(new
|
|
9914
|
+
next(new import_utils57.BadRequestError(error.message));
|
|
8610
9915
|
return;
|
|
8611
9916
|
}
|
|
8612
9917
|
try {
|
|
@@ -8623,14 +9928,14 @@ function usePSGCController() {
|
|
|
8623
9928
|
async function updateField(req, res, next) {
|
|
8624
9929
|
const _id = req.params.id;
|
|
8625
9930
|
const { field, value } = req.body;
|
|
8626
|
-
const validation =
|
|
8627
|
-
_id:
|
|
8628
|
-
field:
|
|
8629
|
-
value:
|
|
9931
|
+
const validation = import_joi38.default.object({
|
|
9932
|
+
_id: import_joi38.default.string().hex().required(),
|
|
9933
|
+
field: import_joi38.default.string().valid("name", "director", "directorName").required(),
|
|
9934
|
+
value: import_joi38.default.string().required()
|
|
8630
9935
|
});
|
|
8631
9936
|
const { error } = validation.validate({ _id, field, value });
|
|
8632
9937
|
if (error) {
|
|
8633
|
-
next(new
|
|
9938
|
+
next(new import_utils57.BadRequestError(error.message));
|
|
8634
9939
|
return;
|
|
8635
9940
|
}
|
|
8636
9941
|
try {
|
|
@@ -8643,12 +9948,12 @@ function usePSGCController() {
|
|
|
8643
9948
|
}
|
|
8644
9949
|
async function deleteById(req, res, next) {
|
|
8645
9950
|
const _id = req.params.id;
|
|
8646
|
-
const validation =
|
|
8647
|
-
_id:
|
|
9951
|
+
const validation = import_joi38.default.object({
|
|
9952
|
+
_id: import_joi38.default.string().hex().required()
|
|
8648
9953
|
});
|
|
8649
9954
|
const { error } = validation.validate({ _id });
|
|
8650
9955
|
if (error) {
|
|
8651
|
-
next(new
|
|
9956
|
+
next(new import_utils57.BadRequestError(error.message));
|
|
8652
9957
|
return;
|
|
8653
9958
|
}
|
|
8654
9959
|
try {
|
|
@@ -8670,7 +9975,7 @@ function usePSGCController() {
|
|
|
8670
9975
|
}
|
|
8671
9976
|
|
|
8672
9977
|
// src/resources/utils/github.service.ts
|
|
8673
|
-
var
|
|
9978
|
+
var import_utils58 = require("@goweekdays/utils");
|
|
8674
9979
|
var import_rest = require("@octokit/rest");
|
|
8675
9980
|
var import_libsodium_wrappers = __toESM(require("libsodium-wrappers"));
|
|
8676
9981
|
function useGitHubService() {
|
|
@@ -8684,23 +9989,23 @@ function useGitHubService() {
|
|
|
8684
9989
|
try {
|
|
8685
9990
|
const { data: repoData } = await octokit.repos.get({ owner, repo });
|
|
8686
9991
|
if (!repoData.permissions?.admin) {
|
|
8687
|
-
throw new
|
|
9992
|
+
throw new import_utils58.BadRequestError(
|
|
8688
9993
|
"You do not have admin access to this repository."
|
|
8689
9994
|
);
|
|
8690
9995
|
}
|
|
8691
9996
|
} catch (error) {
|
|
8692
9997
|
if (error.status === 404) {
|
|
8693
|
-
throw new
|
|
9998
|
+
throw new import_utils58.BadRequestError(
|
|
8694
9999
|
"Repository not found or you don't have access to it."
|
|
8695
10000
|
);
|
|
8696
10001
|
} else if (error.status === 401) {
|
|
8697
|
-
throw new
|
|
10002
|
+
throw new import_utils58.BadRequestError(
|
|
8698
10003
|
"Invalid GitHub token or insufficient permissions."
|
|
8699
10004
|
);
|
|
8700
10005
|
} else if (error.message.includes("admin access")) {
|
|
8701
10006
|
throw error;
|
|
8702
10007
|
} else {
|
|
8703
|
-
throw new
|
|
10008
|
+
throw new import_utils58.BadRequestError(
|
|
8704
10009
|
`Failed to check repository permissions: ${error.message}`
|
|
8705
10010
|
);
|
|
8706
10011
|
}
|
|
@@ -8749,7 +10054,7 @@ function useGitHubService() {
|
|
|
8749
10054
|
key_id: publicKeyRes.key_id
|
|
8750
10055
|
});
|
|
8751
10056
|
} catch (encryptionError) {
|
|
8752
|
-
throw new
|
|
10057
|
+
throw new import_utils58.BadRequestError(
|
|
8753
10058
|
`Failed to encrypt secret '${key}': ${encryptionError.message}`
|
|
8754
10059
|
);
|
|
8755
10060
|
}
|
|
@@ -8779,22 +10084,22 @@ function useGitHubService() {
|
|
|
8779
10084
|
}
|
|
8780
10085
|
return `Successfully set ${lines.length} ${type} variables/secrets in environment '${environment}'`;
|
|
8781
10086
|
} catch (error) {
|
|
8782
|
-
if (error instanceof
|
|
10087
|
+
if (error instanceof import_utils58.AppError)
|
|
8783
10088
|
throw error;
|
|
8784
10089
|
if (error.status === 422) {
|
|
8785
|
-
throw new
|
|
10090
|
+
throw new import_utils58.BadRequestError(
|
|
8786
10091
|
`GitHub API validation error: ${error.message}`
|
|
8787
10092
|
);
|
|
8788
10093
|
} else if (error.status === 404) {
|
|
8789
|
-
throw new
|
|
10094
|
+
throw new import_utils58.BadRequestError("Environment or repository not found.");
|
|
8790
10095
|
} else if (error.status === 403) {
|
|
8791
|
-
throw new
|
|
10096
|
+
throw new import_utils58.BadRequestError(
|
|
8792
10097
|
"Forbidden: Insufficient permissions or rate limit exceeded."
|
|
8793
10098
|
);
|
|
8794
10099
|
} else if (error.message.includes("admin access") || error.message.includes("permissions")) {
|
|
8795
10100
|
throw error;
|
|
8796
10101
|
} else {
|
|
8797
|
-
throw new
|
|
10102
|
+
throw new import_utils58.BadRequestError(
|
|
8798
10103
|
`Failed to set GitHub variables: ${error.message}`
|
|
8799
10104
|
);
|
|
8800
10105
|
}
|
|
@@ -8806,8 +10111,8 @@ function useGitHubService() {
|
|
|
8806
10111
|
}
|
|
8807
10112
|
|
|
8808
10113
|
// src/resources/utils/util.controller.ts
|
|
8809
|
-
var
|
|
8810
|
-
var
|
|
10114
|
+
var import_joi39 = __toESM(require("joi"));
|
|
10115
|
+
var import_utils59 = require("@goweekdays/utils");
|
|
8811
10116
|
function useUtilController() {
|
|
8812
10117
|
async function healthCheck(req, res, next) {
|
|
8813
10118
|
try {
|
|
@@ -8823,32 +10128,32 @@ function useUtilController() {
|
|
|
8823
10128
|
}
|
|
8824
10129
|
});
|
|
8825
10130
|
} catch (error) {
|
|
8826
|
-
|
|
8827
|
-
next(new
|
|
10131
|
+
import_utils59.logger.error("Health check failed", { error: error.message });
|
|
10132
|
+
next(new import_utils59.InternalServerError("Health check failed"));
|
|
8828
10133
|
}
|
|
8829
10134
|
}
|
|
8830
10135
|
async function setGitHubVariables(req, res, next) {
|
|
8831
10136
|
try {
|
|
8832
10137
|
const { githubToken, repoUrl, environment, type, keyValues } = req.body;
|
|
8833
|
-
const validation =
|
|
8834
|
-
githubToken:
|
|
10138
|
+
const validation = import_joi39.default.object({
|
|
10139
|
+
githubToken: import_joi39.default.string().required().messages({
|
|
8835
10140
|
"string.empty": "GitHub token is required",
|
|
8836
10141
|
"any.required": "GitHub token is required"
|
|
8837
10142
|
}),
|
|
8838
|
-
repoUrl:
|
|
10143
|
+
repoUrl: import_joi39.default.string().uri().required().messages({
|
|
8839
10144
|
"string.empty": "Repository URL is required",
|
|
8840
10145
|
"string.uri": "Repository URL must be a valid URL",
|
|
8841
10146
|
"any.required": "Repository URL is required"
|
|
8842
10147
|
}),
|
|
8843
|
-
environment:
|
|
10148
|
+
environment: import_joi39.default.string().required().messages({
|
|
8844
10149
|
"string.empty": "Environment name is required",
|
|
8845
10150
|
"any.required": "Environment name is required"
|
|
8846
10151
|
}),
|
|
8847
|
-
type:
|
|
10152
|
+
type: import_joi39.default.string().valid("env", "secret").required().messages({
|
|
8848
10153
|
"any.only": 'Type must be either "env" or "secret"',
|
|
8849
10154
|
"any.required": "Type is required"
|
|
8850
10155
|
}),
|
|
8851
|
-
keyValues:
|
|
10156
|
+
keyValues: import_joi39.default.string().required().messages({
|
|
8852
10157
|
"string.empty": "Key-value pairs are required",
|
|
8853
10158
|
"any.required": "Key-value pairs are required"
|
|
8854
10159
|
})
|
|
@@ -8861,13 +10166,13 @@ function useUtilController() {
|
|
|
8861
10166
|
keyValues
|
|
8862
10167
|
});
|
|
8863
10168
|
if (error) {
|
|
8864
|
-
next(new
|
|
10169
|
+
next(new import_utils59.BadRequestError(error.message));
|
|
8865
10170
|
return;
|
|
8866
10171
|
}
|
|
8867
10172
|
const repoUrlPattern = /github\.com[:\/]([^\/]+)\/(.+)\.git$/;
|
|
8868
10173
|
if (!repoUrlPattern.test(repoUrl)) {
|
|
8869
10174
|
next(
|
|
8870
|
-
new
|
|
10175
|
+
new import_utils59.BadRequestError(
|
|
8871
10176
|
"Invalid GitHub repository URL format. Expected format: https://github.com/owner/repo.git"
|
|
8872
10177
|
)
|
|
8873
10178
|
);
|
|
@@ -8879,7 +10184,7 @@ function useUtilController() {
|
|
|
8879
10184
|
);
|
|
8880
10185
|
if (invalidLines.length > 0) {
|
|
8881
10186
|
next(
|
|
8882
|
-
new
|
|
10187
|
+
new import_utils59.BadRequestError(
|
|
8883
10188
|
"Invalid key-value format. Each pair should be in format: KEY=value. Pairs should be separated by semicolons."
|
|
8884
10189
|
)
|
|
8885
10190
|
);
|
|
@@ -8893,7 +10198,7 @@ function useUtilController() {
|
|
|
8893
10198
|
type,
|
|
8894
10199
|
keyValues
|
|
8895
10200
|
});
|
|
8896
|
-
|
|
10201
|
+
import_utils59.logger.info(`GitHub variables set successfully`, {
|
|
8897
10202
|
repoUrl,
|
|
8898
10203
|
environment,
|
|
8899
10204
|
type,
|
|
@@ -8910,15 +10215,15 @@ function useUtilController() {
|
|
|
8910
10215
|
}
|
|
8911
10216
|
});
|
|
8912
10217
|
} catch (error) {
|
|
8913
|
-
|
|
10218
|
+
import_utils59.logger.error("Failed to set GitHub variables", {
|
|
8914
10219
|
error: error.message,
|
|
8915
10220
|
stack: error.stack
|
|
8916
10221
|
});
|
|
8917
|
-
if (error instanceof
|
|
10222
|
+
if (error instanceof import_utils59.AppError) {
|
|
8918
10223
|
next(error);
|
|
8919
10224
|
} else {
|
|
8920
10225
|
next(
|
|
8921
|
-
new
|
|
10226
|
+
new import_utils59.InternalServerError(
|
|
8922
10227
|
`Failed to set GitHub variables: ${error.message}`
|
|
8923
10228
|
)
|
|
8924
10229
|
);
|
|
@@ -8932,31 +10237,31 @@ function useUtilController() {
|
|
|
8932
10237
|
}
|
|
8933
10238
|
|
|
8934
10239
|
// src/resources/utils/transaction.schema.ts
|
|
8935
|
-
var
|
|
8936
|
-
var transactionSchema =
|
|
8937
|
-
_id:
|
|
8938
|
-
payment:
|
|
8939
|
-
user:
|
|
8940
|
-
org:
|
|
8941
|
-
type:
|
|
8942
|
-
amount:
|
|
8943
|
-
currency:
|
|
8944
|
-
description:
|
|
8945
|
-
metadata:
|
|
8946
|
-
subscriptionId:
|
|
8947
|
-
cycle:
|
|
8948
|
-
seats:
|
|
8949
|
-
promoCode:
|
|
10240
|
+
var import_joi40 = __toESM(require("joi"));
|
|
10241
|
+
var transactionSchema = import_joi40.default.object({
|
|
10242
|
+
_id: import_joi40.default.string().hex().optional().allow("", null),
|
|
10243
|
+
payment: import_joi40.default.string().required(),
|
|
10244
|
+
user: import_joi40.default.string().hex().optional().allow("", null),
|
|
10245
|
+
org: import_joi40.default.string().hex().optional().allow("", null),
|
|
10246
|
+
type: import_joi40.default.string().required(),
|
|
10247
|
+
amount: import_joi40.default.number().positive().min(0).required(),
|
|
10248
|
+
currency: import_joi40.default.string().required(),
|
|
10249
|
+
description: import_joi40.default.string().optional().allow("", null),
|
|
10250
|
+
metadata: import_joi40.default.object({
|
|
10251
|
+
subscriptionId: import_joi40.default.string().hex().optional().allow("", null),
|
|
10252
|
+
cycle: import_joi40.default.number().optional().allow("", null),
|
|
10253
|
+
seats: import_joi40.default.number().optional().allow("", null),
|
|
10254
|
+
promoCode: import_joi40.default.string().optional().allow("", null)
|
|
8950
10255
|
}).optional().allow("", null),
|
|
8951
|
-
status:
|
|
8952
|
-
createdAt:
|
|
8953
|
-
updatedAt:
|
|
8954
|
-
deletedAt:
|
|
10256
|
+
status: import_joi40.default.string().optional().allow("", null),
|
|
10257
|
+
createdAt: import_joi40.default.string().optional().allow("", null),
|
|
10258
|
+
updatedAt: import_joi40.default.string().optional().allow("", null),
|
|
10259
|
+
deletedAt: import_joi40.default.string().optional().allow("", null)
|
|
8955
10260
|
});
|
|
8956
10261
|
|
|
8957
10262
|
// src/resources/verification/verification.controller.ts
|
|
8958
|
-
var
|
|
8959
|
-
var
|
|
10263
|
+
var import_utils60 = require("@goweekdays/utils");
|
|
10264
|
+
var import_joi41 = __toESM(require("joi"));
|
|
8960
10265
|
function useVerificationController() {
|
|
8961
10266
|
const {
|
|
8962
10267
|
createUserInvite: _createUserInvite,
|
|
@@ -8970,17 +10275,17 @@ function useVerificationController() {
|
|
|
8970
10275
|
} = useVerificationService();
|
|
8971
10276
|
const { getVerifications: _getVerifications } = useVerificationRepo();
|
|
8972
10277
|
async function createUserInvite(req, res, next) {
|
|
8973
|
-
const validation =
|
|
8974
|
-
email:
|
|
8975
|
-
app:
|
|
8976
|
-
role:
|
|
8977
|
-
roleName:
|
|
8978
|
-
org:
|
|
8979
|
-
orgName:
|
|
10278
|
+
const validation = import_joi41.default.object({
|
|
10279
|
+
email: import_joi41.default.string().email().required(),
|
|
10280
|
+
app: import_joi41.default.string().required(),
|
|
10281
|
+
role: import_joi41.default.string().hex().required(),
|
|
10282
|
+
roleName: import_joi41.default.string().required(),
|
|
10283
|
+
org: import_joi41.default.string().hex().optional().optional().allow("", null),
|
|
10284
|
+
orgName: import_joi41.default.string().optional().optional().allow("", null)
|
|
8980
10285
|
});
|
|
8981
10286
|
const { error } = validation.validate(req.body);
|
|
8982
10287
|
if (error) {
|
|
8983
|
-
next(new
|
|
10288
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
8984
10289
|
return;
|
|
8985
10290
|
}
|
|
8986
10291
|
const email = req.body.email ?? "";
|
|
@@ -9008,10 +10313,10 @@ function useVerificationController() {
|
|
|
9008
10313
|
}
|
|
9009
10314
|
async function createForgetPassword(req, res, next) {
|
|
9010
10315
|
const email = req.body.email || "";
|
|
9011
|
-
const validation =
|
|
10316
|
+
const validation = import_joi41.default.string().email().required();
|
|
9012
10317
|
const { error } = validation.validate(email);
|
|
9013
10318
|
if (error) {
|
|
9014
|
-
next(new
|
|
10319
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9015
10320
|
return;
|
|
9016
10321
|
}
|
|
9017
10322
|
try {
|
|
@@ -9021,26 +10326,26 @@ function useVerificationController() {
|
|
|
9021
10326
|
});
|
|
9022
10327
|
return;
|
|
9023
10328
|
} catch (error2) {
|
|
9024
|
-
if (error2 instanceof
|
|
10329
|
+
if (error2 instanceof import_utils60.AppError) {
|
|
9025
10330
|
next(error2);
|
|
9026
10331
|
} else {
|
|
9027
|
-
next(new
|
|
10332
|
+
next(new import_utils60.InternalServerError("An unexpected error occurred"));
|
|
9028
10333
|
}
|
|
9029
10334
|
}
|
|
9030
10335
|
}
|
|
9031
10336
|
async function getVerifications(req, res, next) {
|
|
9032
|
-
const validation =
|
|
9033
|
-
status:
|
|
9034
|
-
search:
|
|
9035
|
-
page:
|
|
9036
|
-
type:
|
|
9037
|
-
email:
|
|
9038
|
-
app:
|
|
9039
|
-
org:
|
|
10337
|
+
const validation = import_joi41.default.object({
|
|
10338
|
+
status: import_joi41.default.string().required(),
|
|
10339
|
+
search: import_joi41.default.string().optional().allow("", null),
|
|
10340
|
+
page: import_joi41.default.number().required(),
|
|
10341
|
+
type: import_joi41.default.string().optional().allow("", null),
|
|
10342
|
+
email: import_joi41.default.string().optional().allow("", null),
|
|
10343
|
+
app: import_joi41.default.string().optional().allow("", null),
|
|
10344
|
+
org: import_joi41.default.string().optional().allow("", null)
|
|
9040
10345
|
});
|
|
9041
10346
|
const { error } = validation.validate(req.query);
|
|
9042
10347
|
if (error) {
|
|
9043
|
-
next(new
|
|
10348
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9044
10349
|
return;
|
|
9045
10350
|
}
|
|
9046
10351
|
const status = req.query.status ?? "";
|
|
@@ -9073,10 +10378,10 @@ function useVerificationController() {
|
|
|
9073
10378
|
}
|
|
9074
10379
|
async function verify(req, res, next) {
|
|
9075
10380
|
const id = req.params.id || "";
|
|
9076
|
-
const validation =
|
|
10381
|
+
const validation = import_joi41.default.string().hex().required();
|
|
9077
10382
|
const { error } = validation.validate(id);
|
|
9078
10383
|
if (error) {
|
|
9079
|
-
next(new
|
|
10384
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9080
10385
|
return;
|
|
9081
10386
|
}
|
|
9082
10387
|
try {
|
|
@@ -9089,10 +10394,10 @@ function useVerificationController() {
|
|
|
9089
10394
|
}
|
|
9090
10395
|
async function cancelUserInvitation(req, res, next) {
|
|
9091
10396
|
const otpId = req.params.id || "";
|
|
9092
|
-
const validation =
|
|
10397
|
+
const validation = import_joi41.default.string().hex().required();
|
|
9093
10398
|
const { error } = validation.validate(otpId);
|
|
9094
10399
|
if (error) {
|
|
9095
|
-
next(new
|
|
10400
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9096
10401
|
return;
|
|
9097
10402
|
}
|
|
9098
10403
|
try {
|
|
@@ -9105,10 +10410,10 @@ function useVerificationController() {
|
|
|
9105
10410
|
}
|
|
9106
10411
|
}
|
|
9107
10412
|
async function signUp(req, res, next) {
|
|
9108
|
-
const validation =
|
|
10413
|
+
const validation = import_joi41.default.string().email().required();
|
|
9109
10414
|
const { error } = validation.validate(req.body.email);
|
|
9110
10415
|
if (error) {
|
|
9111
|
-
next(new
|
|
10416
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9112
10417
|
return;
|
|
9113
10418
|
}
|
|
9114
10419
|
const email = req.body.email ?? "";
|
|
@@ -9123,7 +10428,7 @@ function useVerificationController() {
|
|
|
9123
10428
|
async function inviteMember(req, res, next) {
|
|
9124
10429
|
const { error } = schemaInviteMember.validate(req.body);
|
|
9125
10430
|
if (error) {
|
|
9126
|
-
next(new
|
|
10431
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9127
10432
|
return;
|
|
9128
10433
|
}
|
|
9129
10434
|
try {
|
|
@@ -9136,10 +10441,10 @@ function useVerificationController() {
|
|
|
9136
10441
|
}
|
|
9137
10442
|
async function cancelInviteMember(req, res, next) {
|
|
9138
10443
|
const id = req.params.id || "";
|
|
9139
|
-
const validation =
|
|
10444
|
+
const validation = import_joi41.default.string().hex().required();
|
|
9140
10445
|
const { error } = validation.validate(id);
|
|
9141
10446
|
if (error) {
|
|
9142
|
-
next(new
|
|
10447
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9143
10448
|
return;
|
|
9144
10449
|
}
|
|
9145
10450
|
try {
|
|
@@ -9152,10 +10457,10 @@ function useVerificationController() {
|
|
|
9152
10457
|
}
|
|
9153
10458
|
async function forgetPassword(req, res, next) {
|
|
9154
10459
|
const email = req.body.email ?? "";
|
|
9155
|
-
const validation =
|
|
10460
|
+
const validation = import_joi41.default.string().email().required();
|
|
9156
10461
|
const { error } = validation.validate(email);
|
|
9157
10462
|
if (error) {
|
|
9158
|
-
next(new
|
|
10463
|
+
next(new import_utils60.BadRequestError(error.message));
|
|
9159
10464
|
return;
|
|
9160
10465
|
}
|
|
9161
10466
|
try {
|
|
@@ -9165,10 +10470,10 @@ function useVerificationController() {
|
|
|
9165
10470
|
});
|
|
9166
10471
|
return;
|
|
9167
10472
|
} catch (error2) {
|
|
9168
|
-
if (error2 instanceof
|
|
10473
|
+
if (error2 instanceof import_utils60.AppError) {
|
|
9169
10474
|
next(error2);
|
|
9170
10475
|
} else {
|
|
9171
|
-
next(new
|
|
10476
|
+
next(new import_utils60.InternalServerError("An unexpected error occurred"));
|
|
9172
10477
|
}
|
|
9173
10478
|
}
|
|
9174
10479
|
}
|
|
@@ -9226,6 +10531,7 @@ function useVerificationController() {
|
|
|
9226
10531
|
XENDIT_BASE_URL,
|
|
9227
10532
|
XENDIT_SECRET_KEY,
|
|
9228
10533
|
addressSchema,
|
|
10534
|
+
currencies,
|
|
9229
10535
|
isDev,
|
|
9230
10536
|
modelApp,
|
|
9231
10537
|
modelMember,
|
|
@@ -9233,7 +10539,10 @@ function useVerificationController() {
|
|
|
9233
10539
|
modelPSGC,
|
|
9234
10540
|
modelPermission,
|
|
9235
10541
|
modelPermissionGroup,
|
|
10542
|
+
modelPlan,
|
|
9236
10543
|
modelRole,
|
|
10544
|
+
modelSubscription,
|
|
10545
|
+
modelSubscriptionTransaction,
|
|
9237
10546
|
modelUser,
|
|
9238
10547
|
modelVerification,
|
|
9239
10548
|
schemaApp,
|
|
@@ -9245,13 +10554,20 @@ function useVerificationController() {
|
|
|
9245
10554
|
schemaMemberRole,
|
|
9246
10555
|
schemaMemberStatus,
|
|
9247
10556
|
schemaOrg,
|
|
10557
|
+
schemaOrgAdd,
|
|
10558
|
+
schemaOrgUpdate,
|
|
9248
10559
|
schemaPSGC,
|
|
9249
10560
|
schemaPermission,
|
|
9250
10561
|
schemaPermissionGroup,
|
|
9251
10562
|
schemaPermissionGroupUpdate,
|
|
9252
10563
|
schemaPermissionUpdate,
|
|
10564
|
+
schemaPlan,
|
|
9253
10565
|
schemaRole,
|
|
9254
10566
|
schemaRoleUpdate,
|
|
10567
|
+
schemaSubscription,
|
|
10568
|
+
schemaSubscriptionSeats,
|
|
10569
|
+
schemaSubscriptionTransaction,
|
|
10570
|
+
schemaSubscriptionUpdate,
|
|
9255
10571
|
schemaUpdateOptions,
|
|
9256
10572
|
schemaUser,
|
|
9257
10573
|
schemaVerification,
|
|
@@ -9288,9 +10604,16 @@ function useVerificationController() {
|
|
|
9288
10604
|
usePermissionGroupService,
|
|
9289
10605
|
usePermissionRepo,
|
|
9290
10606
|
usePermissionService,
|
|
10607
|
+
usePlanController,
|
|
10608
|
+
usePlanRepo,
|
|
10609
|
+
usePlanService,
|
|
9291
10610
|
useRoleController,
|
|
9292
10611
|
useRoleRepo,
|
|
9293
10612
|
useRoleService,
|
|
10613
|
+
useSubscriptionController,
|
|
10614
|
+
useSubscriptionRepo,
|
|
10615
|
+
useSubscriptionTransactionController,
|
|
10616
|
+
useSubscriptionTransactionRepo,
|
|
9294
10617
|
useUserController,
|
|
9295
10618
|
useUserRepo,
|
|
9296
10619
|
useUserService,
|