@goweekdays/core 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +254 -10
- package/dist/index.js +1918 -432
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1780 -291
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -11334,12 +11334,16 @@ __export(src_exports, {
|
|
|
11334
11334
|
MAILER_TRANSPORT_HOST: () => MAILER_TRANSPORT_HOST,
|
|
11335
11335
|
MAILER_TRANSPORT_PORT: () => MAILER_TRANSPORT_PORT,
|
|
11336
11336
|
MAILER_TRANSPORT_SECURE: () => MAILER_TRANSPORT_SECURE,
|
|
11337
|
+
MAddress: () => MAddress,
|
|
11337
11338
|
MCapBldgAct: () => MCapBldgAct,
|
|
11338
11339
|
MComment: () => MComment,
|
|
11339
11340
|
MEntity: () => MEntity,
|
|
11340
11341
|
MFile: () => MFile,
|
|
11342
|
+
MMember: () => MMember,
|
|
11341
11343
|
MONGO_DB: () => MONGO_DB,
|
|
11342
11344
|
MONGO_URI: () => MONGO_URI,
|
|
11345
|
+
MOrg: () => MOrg,
|
|
11346
|
+
MPaymentMethod: () => MPaymentMethod,
|
|
11343
11347
|
MRole: () => MRole,
|
|
11344
11348
|
MSubscription: () => MSubscription,
|
|
11345
11349
|
MToken: () => MToken,
|
|
@@ -11364,7 +11368,11 @@ __export(src_exports, {
|
|
|
11364
11368
|
SPACES_SECRET_KEY: () => SPACES_SECRET_KEY,
|
|
11365
11369
|
VERIFICATION_FORGET_PASSWORD_DURATION: () => VERIFICATION_FORGET_PASSWORD_DURATION,
|
|
11366
11370
|
VERIFICATION_USER_INVITE_DURATION: () => VERIFICATION_USER_INVITE_DURATION,
|
|
11371
|
+
XENDIT_BASE_URL: () => XENDIT_BASE_URL,
|
|
11372
|
+
XENDIT_SECRET_KEY: () => XENDIT_SECRET_KEY,
|
|
11367
11373
|
isDev: () => isDev,
|
|
11374
|
+
useAddressController: () => useAddressController,
|
|
11375
|
+
useAddressRepo: () => useAddressRepo,
|
|
11368
11376
|
useAuthController: () => useAuthController,
|
|
11369
11377
|
useAuthService: () => useAuthService,
|
|
11370
11378
|
useCapBldgActController: () => useCapBldgActController,
|
|
@@ -11379,6 +11387,13 @@ __export(src_exports, {
|
|
|
11379
11387
|
useFileController: () => useFileController,
|
|
11380
11388
|
useFileRepo: () => useFileRepo,
|
|
11381
11389
|
useFileService: () => useFileService,
|
|
11390
|
+
useMemberRepo: () => useMemberRepo,
|
|
11391
|
+
useOrgController: () => useOrgController,
|
|
11392
|
+
useOrgRepo: () => useOrgRepo,
|
|
11393
|
+
useOrgService: () => useOrgService,
|
|
11394
|
+
usePaymentMethodController: () => usePaymentMethodController,
|
|
11395
|
+
usePaymentMethodRepo: () => usePaymentMethodRepo,
|
|
11396
|
+
usePaymentMethodService: () => usePaymentMethodService,
|
|
11382
11397
|
useRoleController: () => useRoleController,
|
|
11383
11398
|
useRoleRepo: () => useRoleRepo,
|
|
11384
11399
|
useRoleService: () => useRoleService,
|
|
@@ -11394,17 +11409,26 @@ __export(src_exports, {
|
|
|
11394
11409
|
useVerificationService: () => useVerificationService,
|
|
11395
11410
|
useWorkflowController: () => useWorkflowController,
|
|
11396
11411
|
useWorkflowRepo: () => useWorkflowRepo,
|
|
11397
|
-
useWorkflowService: () => useWorkflowService
|
|
11412
|
+
useWorkflowService: () => useWorkflowService,
|
|
11413
|
+
useXenditService: () => useXenditService
|
|
11398
11414
|
});
|
|
11399
11415
|
module.exports = __toCommonJS(src_exports);
|
|
11400
11416
|
|
|
11401
11417
|
// src/models/verification.model.ts
|
|
11418
|
+
var import_utils = require("@goweekdays/utils");
|
|
11402
11419
|
var import_mongodb = require("mongodb");
|
|
11403
11420
|
var MVerification = class {
|
|
11404
11421
|
constructor(value) {
|
|
11405
11422
|
this._id = value._id ?? new import_mongodb.ObjectId();
|
|
11406
11423
|
this.type = value.type ?? "";
|
|
11407
11424
|
this.email = value.email ?? "";
|
|
11425
|
+
if (this.metadata?.org) {
|
|
11426
|
+
try {
|
|
11427
|
+
this.metadata.org = new import_mongodb.ObjectId(this.metadata?.org);
|
|
11428
|
+
} catch (error) {
|
|
11429
|
+
throw new import_utils.BadRequestError("Invalid org ID.");
|
|
11430
|
+
}
|
|
11431
|
+
}
|
|
11408
11432
|
this.metadata = value.metadata ?? {};
|
|
11409
11433
|
this.status = value.status ?? "pending";
|
|
11410
11434
|
this.createdAt = value.createdAt ?? /* @__PURE__ */ new Date();
|
|
@@ -11414,14 +11438,26 @@ var MVerification = class {
|
|
|
11414
11438
|
};
|
|
11415
11439
|
|
|
11416
11440
|
// src/repositories/verification.repository.ts
|
|
11417
|
-
var
|
|
11441
|
+
var import_utils2 = require("@goweekdays/utils");
|
|
11418
11442
|
var import_mongodb2 = require("mongodb");
|
|
11419
11443
|
function useVerificationRepo() {
|
|
11420
|
-
const db =
|
|
11444
|
+
const db = import_utils2.useAtlas.getDb();
|
|
11421
11445
|
if (!db) {
|
|
11422
|
-
throw new
|
|
11446
|
+
throw new import_utils2.InternalServerError("Unable to connect to server.");
|
|
11423
11447
|
}
|
|
11424
11448
|
const collection = db.collection("verifications");
|
|
11449
|
+
async function createIndex() {
|
|
11450
|
+
try {
|
|
11451
|
+
await collection.createIndex([
|
|
11452
|
+
{ email: 1 },
|
|
11453
|
+
{ type: 1 },
|
|
11454
|
+
{ status: 1 },
|
|
11455
|
+
{ "metadata.org": 1 }
|
|
11456
|
+
]);
|
|
11457
|
+
} catch (error) {
|
|
11458
|
+
throw new import_utils2.InternalServerError("Failed to create index.");
|
|
11459
|
+
}
|
|
11460
|
+
}
|
|
11425
11461
|
async function createTextIndex() {
|
|
11426
11462
|
try {
|
|
11427
11463
|
await collection.createIndex({
|
|
@@ -11437,23 +11473,23 @@ function useVerificationRepo() {
|
|
|
11437
11473
|
const res = await collection.insertOne(value, { session });
|
|
11438
11474
|
return res.insertedId;
|
|
11439
11475
|
} catch (error) {
|
|
11440
|
-
|
|
11476
|
+
import_utils2.logger.log({
|
|
11441
11477
|
level: "info",
|
|
11442
11478
|
message: String(error)
|
|
11443
11479
|
});
|
|
11444
|
-
throw new
|
|
11480
|
+
throw new import_utils2.InternalServerError("Server internal error.");
|
|
11445
11481
|
}
|
|
11446
11482
|
}
|
|
11447
11483
|
async function getById(_id) {
|
|
11448
11484
|
try {
|
|
11449
11485
|
_id = new import_mongodb2.ObjectId(_id);
|
|
11450
11486
|
} catch (error) {
|
|
11451
|
-
throw new
|
|
11487
|
+
throw new import_utils2.BadRequestError("Invalid ID.");
|
|
11452
11488
|
}
|
|
11453
11489
|
try {
|
|
11454
11490
|
return await collection.findOne({ _id });
|
|
11455
11491
|
} catch (error) {
|
|
11456
|
-
throw new
|
|
11492
|
+
throw new import_utils2.InternalServerError(
|
|
11457
11493
|
"Internal server error, failed to retrieve verification."
|
|
11458
11494
|
);
|
|
11459
11495
|
}
|
|
@@ -11464,7 +11500,8 @@ function useVerificationRepo() {
|
|
|
11464
11500
|
limit = 10,
|
|
11465
11501
|
sort = {},
|
|
11466
11502
|
status = "active",
|
|
11467
|
-
type = ""
|
|
11503
|
+
type = "",
|
|
11504
|
+
email = ""
|
|
11468
11505
|
} = {}) {
|
|
11469
11506
|
page = page > 0 ? page - 1 : 0;
|
|
11470
11507
|
const query = { status };
|
|
@@ -11475,6 +11512,9 @@ function useVerificationRepo() {
|
|
|
11475
11512
|
if (type) {
|
|
11476
11513
|
query.type = type;
|
|
11477
11514
|
}
|
|
11515
|
+
if (email) {
|
|
11516
|
+
query.email = email;
|
|
11517
|
+
}
|
|
11478
11518
|
try {
|
|
11479
11519
|
const items = await collection.aggregate([
|
|
11480
11520
|
{ $match: query },
|
|
@@ -11493,9 +11533,9 @@ function useVerificationRepo() {
|
|
|
11493
11533
|
}
|
|
11494
11534
|
]).toArray();
|
|
11495
11535
|
const length = await collection.countDocuments(query);
|
|
11496
|
-
return (0,
|
|
11536
|
+
return (0, import_utils2.paginate)(items, page, limit, length);
|
|
11497
11537
|
} catch (error) {
|
|
11498
|
-
|
|
11538
|
+
import_utils2.logger.log({ level: "error", message: `${error}` });
|
|
11499
11539
|
throw error;
|
|
11500
11540
|
}
|
|
11501
11541
|
}
|
|
@@ -11510,7 +11550,7 @@ function useVerificationRepo() {
|
|
|
11510
11550
|
try {
|
|
11511
11551
|
_id = new import_mongodb2.ObjectId(_id);
|
|
11512
11552
|
} catch (error) {
|
|
11513
|
-
throw new
|
|
11553
|
+
throw new import_utils2.BadRequestError("Invalid ID.");
|
|
11514
11554
|
}
|
|
11515
11555
|
try {
|
|
11516
11556
|
return await collection.updateOne(
|
|
@@ -11519,10 +11559,11 @@ function useVerificationRepo() {
|
|
|
11519
11559
|
{ session }
|
|
11520
11560
|
);
|
|
11521
11561
|
} catch (error) {
|
|
11522
|
-
throw new
|
|
11562
|
+
throw new import_utils2.InternalServerError("Error updating verification status.");
|
|
11523
11563
|
}
|
|
11524
11564
|
}
|
|
11525
11565
|
return {
|
|
11566
|
+
createIndex,
|
|
11526
11567
|
createTextIndex,
|
|
11527
11568
|
add,
|
|
11528
11569
|
getVerifications,
|
|
@@ -11533,7 +11574,7 @@ function useVerificationRepo() {
|
|
|
11533
11574
|
}
|
|
11534
11575
|
|
|
11535
11576
|
// src/services/verification.service.ts
|
|
11536
|
-
var
|
|
11577
|
+
var import_utils5 = require("@goweekdays/utils");
|
|
11537
11578
|
|
|
11538
11579
|
// src/config.ts
|
|
11539
11580
|
var dotenv = __toESM(require("dotenv"));
|
|
@@ -11573,13 +11614,15 @@ var SPACES_BUCKET = process.env.SPACES_BUCKET;
|
|
|
11573
11614
|
var PAYPAL_CLIENT_ID = process.env.PAYPAL_CLIENT_ID ?? "";
|
|
11574
11615
|
var PAYPAL_CLIENT_SECRET = process.env.PAYPAL_CLIENT_SECRET ?? "";
|
|
11575
11616
|
var PAYPAL_API_URL = process.env.PAYPAL_API_URL ?? "https://api-m.sandbox.paypal.com";
|
|
11617
|
+
var XENDIT_SECRET_KEY = process.env.XENDIT_SECRET_KEY ?? "";
|
|
11618
|
+
var XENDIT_BASE_URL = process.env.XENDIT_BASE_URL ?? "https://api.xendit.co";
|
|
11576
11619
|
|
|
11577
11620
|
// src/repositories/user.repository.ts
|
|
11578
11621
|
var import_mongodb4 = require("mongodb");
|
|
11579
11622
|
|
|
11580
11623
|
// src/models/user.model.ts
|
|
11581
11624
|
var import_mongodb3 = require("mongodb");
|
|
11582
|
-
var
|
|
11625
|
+
var import_utils3 = require("@goweekdays/utils");
|
|
11583
11626
|
var MUserRole = class {
|
|
11584
11627
|
constructor(value) {
|
|
11585
11628
|
this.name = value.name ?? "";
|
|
@@ -11608,13 +11651,13 @@ var MUser = class {
|
|
|
11608
11651
|
try {
|
|
11609
11652
|
role.role = new import_mongodb3.ObjectId(role.role);
|
|
11610
11653
|
} catch (error) {
|
|
11611
|
-
throw new
|
|
11654
|
+
throw new import_utils3.BadRequestError("Invalid role ID.");
|
|
11612
11655
|
}
|
|
11613
11656
|
if (!role.app) {
|
|
11614
|
-
throw new
|
|
11657
|
+
throw new import_utils3.BadRequestError("App is required.");
|
|
11615
11658
|
}
|
|
11616
11659
|
if (!role.name) {
|
|
11617
|
-
throw new
|
|
11660
|
+
throw new import_utils3.BadRequestError("Name is required.");
|
|
11618
11661
|
}
|
|
11619
11662
|
role.status = role.status ?? "active";
|
|
11620
11663
|
});
|
|
@@ -11630,11 +11673,11 @@ var MUser = class {
|
|
|
11630
11673
|
};
|
|
11631
11674
|
|
|
11632
11675
|
// src/repositories/user.repository.ts
|
|
11633
|
-
var
|
|
11676
|
+
var import_utils4 = require("@goweekdays/utils");
|
|
11634
11677
|
function useUserRepo() {
|
|
11635
|
-
const db =
|
|
11678
|
+
const db = import_utils4.useAtlas.getDb();
|
|
11636
11679
|
if (!db) {
|
|
11637
|
-
throw new
|
|
11680
|
+
throw new import_utils4.InternalServerError("Unable to connect to server.");
|
|
11638
11681
|
}
|
|
11639
11682
|
const collection = db.collection("users");
|
|
11640
11683
|
async function createTextIndex() {
|
|
@@ -11665,12 +11708,12 @@ function useUserRepo() {
|
|
|
11665
11708
|
const res = await collection.insertOne(value, { session });
|
|
11666
11709
|
return res.insertedId;
|
|
11667
11710
|
} catch (error) {
|
|
11668
|
-
|
|
11711
|
+
import_utils4.logger.log({ level: "error", message: `${error}` });
|
|
11669
11712
|
const isDuplicated = error.message.includes("duplicate");
|
|
11670
11713
|
if (isDuplicated) {
|
|
11671
|
-
throw new
|
|
11714
|
+
throw new import_utils4.BadRequestError("Item name already exists");
|
|
11672
11715
|
}
|
|
11673
|
-
throw new
|
|
11716
|
+
throw new import_utils4.InternalServerError("Internal server error.");
|
|
11674
11717
|
}
|
|
11675
11718
|
}
|
|
11676
11719
|
async function getUserByEmail(email) {
|
|
@@ -11742,9 +11785,9 @@ function useUserRepo() {
|
|
|
11742
11785
|
}
|
|
11743
11786
|
]).toArray();
|
|
11744
11787
|
const length = await collection.countDocuments(query);
|
|
11745
|
-
return (0,
|
|
11788
|
+
return (0, import_utils4.paginate)(items, page, limit, length);
|
|
11746
11789
|
} catch (error) {
|
|
11747
|
-
|
|
11790
|
+
import_utils4.logger.log({ level: "error", message: `${error}` });
|
|
11748
11791
|
throw error;
|
|
11749
11792
|
}
|
|
11750
11793
|
}
|
|
@@ -11799,14 +11842,14 @@ function useUserRepo() {
|
|
|
11799
11842
|
async function updateUserFieldById({ _id, field, value } = {}, session) {
|
|
11800
11843
|
const allowedFields = ["gender", "email", "contact", "profile"];
|
|
11801
11844
|
if (!allowedFields.includes(field)) {
|
|
11802
|
-
throw new
|
|
11845
|
+
throw new import_utils4.BadRequestError(
|
|
11803
11846
|
`Field "${field}" is not allowed to be updated.`
|
|
11804
11847
|
);
|
|
11805
11848
|
}
|
|
11806
11849
|
try {
|
|
11807
11850
|
_id = new import_mongodb4.ObjectId(_id);
|
|
11808
11851
|
} catch (error) {
|
|
11809
|
-
throw new
|
|
11852
|
+
throw new import_utils4.BadRequestError("Invalid ID.");
|
|
11810
11853
|
}
|
|
11811
11854
|
try {
|
|
11812
11855
|
await collection.updateOne(
|
|
@@ -11817,14 +11860,14 @@ function useUserRepo() {
|
|
|
11817
11860
|
);
|
|
11818
11861
|
return `Successfully updated user ${field}.`;
|
|
11819
11862
|
} catch (error) {
|
|
11820
|
-
throw new
|
|
11863
|
+
throw new import_utils4.InternalServerError(`Failed to update user ${field}.`);
|
|
11821
11864
|
}
|
|
11822
11865
|
}
|
|
11823
11866
|
async function addUserRole({ _id, role } = {}, session) {
|
|
11824
11867
|
try {
|
|
11825
11868
|
_id = new import_mongodb4.ObjectId(_id);
|
|
11826
11869
|
} catch (error) {
|
|
11827
|
-
throw new
|
|
11870
|
+
throw new import_utils4.BadRequestError("Invalid user ID.");
|
|
11828
11871
|
}
|
|
11829
11872
|
role = new MUserRole(role);
|
|
11830
11873
|
try {
|
|
@@ -11835,7 +11878,7 @@ function useUserRepo() {
|
|
|
11835
11878
|
{ session }
|
|
11836
11879
|
);
|
|
11837
11880
|
} catch (error) {
|
|
11838
|
-
throw new
|
|
11881
|
+
throw new import_utils4.InternalServerError("Failed to add user role.");
|
|
11839
11882
|
}
|
|
11840
11883
|
}
|
|
11841
11884
|
return {
|
|
@@ -11864,7 +11907,7 @@ function useVerificationService() {
|
|
|
11864
11907
|
email: MAILER_EMAIL,
|
|
11865
11908
|
password: MAILER_PASSWORD
|
|
11866
11909
|
};
|
|
11867
|
-
const mailer = new
|
|
11910
|
+
const mailer = new import_utils5.useMailer(MailerConfig);
|
|
11868
11911
|
const {
|
|
11869
11912
|
add,
|
|
11870
11913
|
getById: _getById,
|
|
@@ -11889,14 +11932,14 @@ function useVerificationService() {
|
|
|
11889
11932
|
try {
|
|
11890
11933
|
const user = await getByEmailApp(email, metadata.app);
|
|
11891
11934
|
if (user) {
|
|
11892
|
-
throw new
|
|
11935
|
+
throw new import_utils5.BadRequestError(
|
|
11893
11936
|
`User already a user in ${metadata.app} app.`
|
|
11894
11937
|
);
|
|
11895
11938
|
}
|
|
11896
11939
|
const res = await add(value);
|
|
11897
11940
|
const dir = __dirname;
|
|
11898
|
-
const filePath = (0,
|
|
11899
|
-
const emailContent = (0,
|
|
11941
|
+
const filePath = (0, import_utils5.getDirectory)(dir, "./public/handlebars/user-invite");
|
|
11942
|
+
const emailContent = (0, import_utils5.compileHandlebar)({
|
|
11900
11943
|
context: {
|
|
11901
11944
|
validity: VERIFICATION_USER_INVITE_DURATION,
|
|
11902
11945
|
link: `${APP_MAIN}/verify/invitation/${res}`
|
|
@@ -11909,7 +11952,7 @@ function useVerificationService() {
|
|
|
11909
11952
|
html: emailContent,
|
|
11910
11953
|
from: "GoWeekDays"
|
|
11911
11954
|
}).catch((error) => {
|
|
11912
|
-
|
|
11955
|
+
import_utils5.logger.log({
|
|
11913
11956
|
level: "error",
|
|
11914
11957
|
message: `Error sending user invite email: ${error}`
|
|
11915
11958
|
});
|
|
@@ -11930,8 +11973,8 @@ function useVerificationService() {
|
|
|
11930
11973
|
try {
|
|
11931
11974
|
const res = await add(value);
|
|
11932
11975
|
const dir = __dirname;
|
|
11933
|
-
const filePath = (0,
|
|
11934
|
-
const emailContent = (0,
|
|
11976
|
+
const filePath = (0, import_utils5.getDirectory)(dir, "./public/handlebars/forget-password");
|
|
11977
|
+
const emailContent = (0, import_utils5.compileHandlebar)({
|
|
11935
11978
|
context: {
|
|
11936
11979
|
validity: VERIFICATION_FORGET_PASSWORD_DURATION,
|
|
11937
11980
|
link: `${APP_MAIN}/reset-password/${res}`
|
|
@@ -11944,21 +11987,21 @@ function useVerificationService() {
|
|
|
11944
11987
|
from: "GoWeekdays",
|
|
11945
11988
|
html: emailContent
|
|
11946
11989
|
}).catch((error) => {
|
|
11947
|
-
|
|
11990
|
+
import_utils5.logger.log({
|
|
11948
11991
|
level: "error",
|
|
11949
11992
|
message: `Error sending forget password email: ${error}`
|
|
11950
11993
|
});
|
|
11951
11994
|
});
|
|
11952
11995
|
return "Successfully created a link to reset password. Please check your email.";
|
|
11953
11996
|
} catch (error) {
|
|
11954
|
-
throw new
|
|
11997
|
+
throw new import_utils5.InternalServerError("Failed to create forget password link.");
|
|
11955
11998
|
}
|
|
11956
11999
|
}
|
|
11957
12000
|
async function getById(id) {
|
|
11958
12001
|
try {
|
|
11959
12002
|
const _id = await _getById(id);
|
|
11960
12003
|
if (!_id) {
|
|
11961
|
-
throw new
|
|
12004
|
+
throw new import_utils5.NotFoundError("Verification not found.");
|
|
11962
12005
|
}
|
|
11963
12006
|
return _id;
|
|
11964
12007
|
} catch (error) {
|
|
@@ -11970,55 +12013,63 @@ function useVerificationService() {
|
|
|
11970
12013
|
page = 1,
|
|
11971
12014
|
status = "",
|
|
11972
12015
|
type = "",
|
|
12016
|
+
email = "",
|
|
11973
12017
|
limit = 10
|
|
11974
12018
|
} = {}) {
|
|
11975
12019
|
try {
|
|
11976
|
-
return await _getVerifications({
|
|
12020
|
+
return await _getVerifications({
|
|
12021
|
+
search,
|
|
12022
|
+
page,
|
|
12023
|
+
status,
|
|
12024
|
+
type,
|
|
12025
|
+
limit,
|
|
12026
|
+
email
|
|
12027
|
+
});
|
|
11977
12028
|
} catch (error) {
|
|
11978
12029
|
throw error;
|
|
11979
12030
|
}
|
|
11980
12031
|
}
|
|
11981
12032
|
function errorByType(type, status) {
|
|
11982
12033
|
if (type === "user-invite" && status === "expired") {
|
|
11983
|
-
throw new
|
|
12034
|
+
throw new import_utils5.BadRequestError(
|
|
11984
12035
|
"Invitation has already expired, please contact admin to resend the invitation."
|
|
11985
12036
|
);
|
|
11986
12037
|
}
|
|
11987
12038
|
if (type === "user-sign-up" && status === "expired") {
|
|
11988
|
-
throw new
|
|
12039
|
+
throw new import_utils5.BadRequestError(
|
|
11989
12040
|
"Sign up verification has expired, please sign up again to get a new verification link."
|
|
11990
12041
|
);
|
|
11991
12042
|
}
|
|
11992
12043
|
if (type === "user-invite" && status === "complete") {
|
|
11993
|
-
throw new
|
|
12044
|
+
throw new import_utils5.BadRequestError(
|
|
11994
12045
|
"User already registered, please login to continue."
|
|
11995
12046
|
);
|
|
11996
12047
|
}
|
|
11997
12048
|
if (type === "forget-password" && status === "complete") {
|
|
11998
|
-
throw new
|
|
12049
|
+
throw new import_utils5.BadRequestError(
|
|
11999
12050
|
"Forget password verification has already been used, please request a new one."
|
|
12000
12051
|
);
|
|
12001
12052
|
}
|
|
12002
12053
|
if (type === "forget-password" && status === "expired") {
|
|
12003
|
-
throw new
|
|
12054
|
+
throw new import_utils5.BadRequestError(
|
|
12004
12055
|
"Forget password verification has expired, please request a new one."
|
|
12005
12056
|
);
|
|
12006
12057
|
}
|
|
12007
|
-
throw new
|
|
12058
|
+
throw new import_utils5.BadRequestError("Invalid verification.");
|
|
12008
12059
|
}
|
|
12009
12060
|
async function verify(id) {
|
|
12010
|
-
const session =
|
|
12061
|
+
const session = import_utils5.useAtlas.getClient()?.startSession();
|
|
12011
12062
|
session?.startTransaction();
|
|
12012
12063
|
try {
|
|
12013
12064
|
const _id = await _getById(id);
|
|
12014
12065
|
if (!_id) {
|
|
12015
|
-
throw new
|
|
12066
|
+
throw new import_utils5.NotFoundError("Verification not found.");
|
|
12016
12067
|
}
|
|
12017
12068
|
if (_id.status === "expired") {
|
|
12018
12069
|
errorByType(_id.type, "expired");
|
|
12019
12070
|
}
|
|
12020
12071
|
if (_id.status === "complete") {
|
|
12021
|
-
throw new
|
|
12072
|
+
throw new import_utils5.BadRequestError("Verification already completed.");
|
|
12022
12073
|
}
|
|
12023
12074
|
const expiration = new Date(_id.expireAt).getTime();
|
|
12024
12075
|
const now = (/* @__PURE__ */ new Date()).getTime();
|
|
@@ -12046,14 +12097,14 @@ function useVerificationService() {
|
|
|
12046
12097
|
}
|
|
12047
12098
|
if (appMember?.length && _id.type === "user-invite") {
|
|
12048
12099
|
await _updateStatusById(id, "expired");
|
|
12049
|
-
throw new
|
|
12100
|
+
throw new import_utils5.BadRequestError(
|
|
12050
12101
|
`Invalid user invitation. User is already a user in the ${_id.metadata?.app} app.`
|
|
12051
12102
|
);
|
|
12052
12103
|
}
|
|
12053
12104
|
return _id;
|
|
12054
12105
|
} catch (error) {
|
|
12055
12106
|
await session?.abortTransaction();
|
|
12056
|
-
|
|
12107
|
+
import_utils5.logger.log({
|
|
12057
12108
|
level: "info",
|
|
12058
12109
|
message: `Error verifying user invitation: ${error}`
|
|
12059
12110
|
});
|
|
@@ -12066,7 +12117,7 @@ function useVerificationService() {
|
|
|
12066
12117
|
try {
|
|
12067
12118
|
await updateStatusById(id, "cancelled");
|
|
12068
12119
|
} catch (error) {
|
|
12069
|
-
throw new
|
|
12120
|
+
throw new import_utils5.InternalServerError(
|
|
12070
12121
|
`Error cancelling user invitation: ${error}`
|
|
12071
12122
|
);
|
|
12072
12123
|
}
|
|
@@ -12087,12 +12138,12 @@ function useVerificationService() {
|
|
|
12087
12138
|
if (metadata && metadata.referralCode && typeof metadata.referralCode === "string") {
|
|
12088
12139
|
const code = await getUserByReferralCode(metadata.referralCode);
|
|
12089
12140
|
if (!code) {
|
|
12090
|
-
throw new
|
|
12141
|
+
throw new import_utils5.BadRequestError("Invalid referral code.");
|
|
12091
12142
|
}
|
|
12092
12143
|
}
|
|
12093
12144
|
const user = await getUserByEmail(email);
|
|
12094
12145
|
if (user) {
|
|
12095
|
-
throw new
|
|
12146
|
+
throw new import_utils5.BadRequestError(
|
|
12096
12147
|
`Email ${email} is already registered, please login to continue.`
|
|
12097
12148
|
);
|
|
12098
12149
|
}
|
|
@@ -12106,8 +12157,8 @@ function useVerificationService() {
|
|
|
12106
12157
|
};
|
|
12107
12158
|
const res = await add(value);
|
|
12108
12159
|
const dir = __dirname;
|
|
12109
|
-
const filePath = (0,
|
|
12110
|
-
const emailContent = (0,
|
|
12160
|
+
const filePath = (0, import_utils5.getDirectory)(dir, "./public/handlebars/sign-up");
|
|
12161
|
+
const emailContent = (0, import_utils5.compileHandlebar)({
|
|
12111
12162
|
context: {
|
|
12112
12163
|
validity: "5 minutes",
|
|
12113
12164
|
link: `${APP_MAIN}/sign-up/${res}`
|
|
@@ -12120,7 +12171,7 @@ function useVerificationService() {
|
|
|
12120
12171
|
html: emailContent,
|
|
12121
12172
|
from: "GoWeekdays"
|
|
12122
12173
|
}).catch((error) => {
|
|
12123
|
-
|
|
12174
|
+
import_utils5.logger.log({
|
|
12124
12175
|
level: "error",
|
|
12125
12176
|
message: `Error sending user invite email: ${error}`
|
|
12126
12177
|
});
|
|
@@ -12143,7 +12194,7 @@ function useVerificationService() {
|
|
|
12143
12194
|
}
|
|
12144
12195
|
|
|
12145
12196
|
// src/controllers/verification.controller.ts
|
|
12146
|
-
var
|
|
12197
|
+
var import_utils6 = require("@goweekdays/utils");
|
|
12147
12198
|
var import_joi = __toESM(require("joi"));
|
|
12148
12199
|
function useVerificationController() {
|
|
12149
12200
|
const {
|
|
@@ -12171,7 +12222,7 @@ function useVerificationController() {
|
|
|
12171
12222
|
name
|
|
12172
12223
|
});
|
|
12173
12224
|
if (error) {
|
|
12174
|
-
next(new
|
|
12225
|
+
next(new import_utils6.BadRequestError(error.message));
|
|
12175
12226
|
return;
|
|
12176
12227
|
}
|
|
12177
12228
|
try {
|
|
@@ -12195,7 +12246,7 @@ function useVerificationController() {
|
|
|
12195
12246
|
const validation = import_joi.default.string().email().required();
|
|
12196
12247
|
const { error } = validation.validate(email);
|
|
12197
12248
|
if (error) {
|
|
12198
|
-
next(new
|
|
12249
|
+
next(new import_utils6.BadRequestError(error.message));
|
|
12199
12250
|
return;
|
|
12200
12251
|
}
|
|
12201
12252
|
try {
|
|
@@ -12205,10 +12256,10 @@ function useVerificationController() {
|
|
|
12205
12256
|
});
|
|
12206
12257
|
return;
|
|
12207
12258
|
} catch (error2) {
|
|
12208
|
-
if (error2 instanceof
|
|
12259
|
+
if (error2 instanceof import_utils6.AppError) {
|
|
12209
12260
|
next(error2);
|
|
12210
12261
|
} else {
|
|
12211
|
-
next(new
|
|
12262
|
+
next(new import_utils6.InternalServerError("An unexpected error occurred"));
|
|
12212
12263
|
}
|
|
12213
12264
|
}
|
|
12214
12265
|
}
|
|
@@ -12216,18 +12267,34 @@ function useVerificationController() {
|
|
|
12216
12267
|
const status = req.query.status ?? "";
|
|
12217
12268
|
const search = req.query.search ?? "";
|
|
12218
12269
|
const page = Number(req.query.page) ?? 1;
|
|
12270
|
+
const type = req.query.type ?? "";
|
|
12271
|
+
const email = req.query.email ?? "";
|
|
12219
12272
|
const validation = import_joi.default.object({
|
|
12220
12273
|
status: import_joi.default.string().required(),
|
|
12221
12274
|
search: import_joi.default.string().optional().allow("", null),
|
|
12222
|
-
page: import_joi.default.number().required()
|
|
12275
|
+
page: import_joi.default.number().required(),
|
|
12276
|
+
type: import_joi.default.string().optional().allow("", null),
|
|
12277
|
+
email: import_joi.default.string().optional().allow("", null)
|
|
12278
|
+
});
|
|
12279
|
+
const { error } = validation.validate({
|
|
12280
|
+
status,
|
|
12281
|
+
search,
|
|
12282
|
+
page,
|
|
12283
|
+
type,
|
|
12284
|
+
email
|
|
12223
12285
|
});
|
|
12224
|
-
const { error } = validation.validate({ status, search, page });
|
|
12225
12286
|
if (error) {
|
|
12226
|
-
next(new
|
|
12287
|
+
next(new import_utils6.BadRequestError(error.message));
|
|
12227
12288
|
return;
|
|
12228
12289
|
}
|
|
12229
12290
|
try {
|
|
12230
|
-
const items = await _getVerifications({
|
|
12291
|
+
const items = await _getVerifications({
|
|
12292
|
+
status,
|
|
12293
|
+
search,
|
|
12294
|
+
page,
|
|
12295
|
+
type,
|
|
12296
|
+
email
|
|
12297
|
+
});
|
|
12231
12298
|
res.json(items);
|
|
12232
12299
|
return;
|
|
12233
12300
|
} catch (error2) {
|
|
@@ -12239,7 +12306,7 @@ function useVerificationController() {
|
|
|
12239
12306
|
const validation = import_joi.default.string().hex().required();
|
|
12240
12307
|
const { error } = validation.validate(id);
|
|
12241
12308
|
if (error) {
|
|
12242
|
-
next(new
|
|
12309
|
+
next(new import_utils6.BadRequestError(error.message));
|
|
12243
12310
|
return;
|
|
12244
12311
|
}
|
|
12245
12312
|
try {
|
|
@@ -12255,7 +12322,7 @@ function useVerificationController() {
|
|
|
12255
12322
|
const validation = import_joi.default.string().hex().required();
|
|
12256
12323
|
const { error } = validation.validate(otpId);
|
|
12257
12324
|
if (error) {
|
|
12258
|
-
next(new
|
|
12325
|
+
next(new import_utils6.BadRequestError(error.message));
|
|
12259
12326
|
return;
|
|
12260
12327
|
}
|
|
12261
12328
|
try {
|
|
@@ -12287,12 +12354,12 @@ var MToken = class {
|
|
|
12287
12354
|
};
|
|
12288
12355
|
|
|
12289
12356
|
// src/repositories/token.repository.ts
|
|
12290
|
-
var
|
|
12357
|
+
var import_utils7 = require("@goweekdays/utils");
|
|
12291
12358
|
var import_mongodb6 = require("mongodb");
|
|
12292
12359
|
function useTokenRepo() {
|
|
12293
|
-
const db =
|
|
12360
|
+
const db = import_utils7.useAtlas.getDb();
|
|
12294
12361
|
if (!db) {
|
|
12295
|
-
throw new
|
|
12362
|
+
throw new import_utils7.InternalServerError("Unable to connect to server.");
|
|
12296
12363
|
}
|
|
12297
12364
|
const collection = db.collection("tokens");
|
|
12298
12365
|
async function createToken({ token, user } = {}) {
|
|
@@ -12330,10 +12397,10 @@ function useTokenRepo() {
|
|
|
12330
12397
|
}
|
|
12331
12398
|
|
|
12332
12399
|
// src/services/user.service.ts
|
|
12333
|
-
var
|
|
12400
|
+
var import_utils9 = require("@goweekdays/utils");
|
|
12334
12401
|
|
|
12335
12402
|
// src/repositories/file.repository.ts
|
|
12336
|
-
var
|
|
12403
|
+
var import_utils8 = require("@goweekdays/utils");
|
|
12337
12404
|
|
|
12338
12405
|
// src/models/file.model.ts
|
|
12339
12406
|
var MFile = class {
|
|
@@ -12349,9 +12416,9 @@ var MFile = class {
|
|
|
12349
12416
|
// src/repositories/file.repository.ts
|
|
12350
12417
|
var import_mongodb7 = require("mongodb");
|
|
12351
12418
|
function useFileRepo() {
|
|
12352
|
-
const db =
|
|
12419
|
+
const db = import_utils8.useAtlas.getDb();
|
|
12353
12420
|
if (!db) {
|
|
12354
|
-
throw new
|
|
12421
|
+
throw new import_utils8.InternalServerError("Unable to connect to server.");
|
|
12355
12422
|
}
|
|
12356
12423
|
const collection = db.collection("files");
|
|
12357
12424
|
async function createFile(value, session) {
|
|
@@ -12360,27 +12427,27 @@ function useFileRepo() {
|
|
|
12360
12427
|
const res = await collection.insertOne(value, { session });
|
|
12361
12428
|
return res.insertedId.toString();
|
|
12362
12429
|
} catch (error) {
|
|
12363
|
-
throw new
|
|
12430
|
+
throw new import_utils8.InternalServerError("Failed to create file.");
|
|
12364
12431
|
}
|
|
12365
12432
|
}
|
|
12366
12433
|
async function deleteFileById(_id, session) {
|
|
12367
12434
|
try {
|
|
12368
12435
|
_id = new import_mongodb7.ObjectId(_id);
|
|
12369
12436
|
} catch (error) {
|
|
12370
|
-
throw new
|
|
12437
|
+
throw new import_utils8.BadRequestError("Invalid file id.");
|
|
12371
12438
|
}
|
|
12372
12439
|
try {
|
|
12373
12440
|
await collection.deleteOne({ _id }, { session });
|
|
12374
12441
|
return "File deleted successfully";
|
|
12375
12442
|
} catch (error) {
|
|
12376
|
-
throw new
|
|
12443
|
+
throw new import_utils8.InternalServerError("Failed to delete file.");
|
|
12377
12444
|
}
|
|
12378
12445
|
}
|
|
12379
12446
|
async function getAllDraftedFiles() {
|
|
12380
12447
|
try {
|
|
12381
12448
|
return await collection.find({ $and: [{ status: "draft" }, { status: null }] }).toArray();
|
|
12382
12449
|
} catch (error) {
|
|
12383
|
-
throw new
|
|
12450
|
+
throw new import_utils8.InternalServerError("Failed to get drafted files.");
|
|
12384
12451
|
}
|
|
12385
12452
|
}
|
|
12386
12453
|
return {
|
|
@@ -12406,7 +12473,7 @@ function useUserService() {
|
|
|
12406
12473
|
try {
|
|
12407
12474
|
return await _getById(id);
|
|
12408
12475
|
} catch (error) {
|
|
12409
|
-
throw new
|
|
12476
|
+
throw new import_utils9.InternalServerError();
|
|
12410
12477
|
}
|
|
12411
12478
|
}
|
|
12412
12479
|
async function getUsers({
|
|
@@ -12423,14 +12490,14 @@ function useUserService() {
|
|
|
12423
12490
|
}
|
|
12424
12491
|
}
|
|
12425
12492
|
async function createUser(value) {
|
|
12426
|
-
const session =
|
|
12493
|
+
const session = import_utils9.useAtlas.getClient()?.startSession();
|
|
12427
12494
|
session?.startTransaction();
|
|
12428
12495
|
try {
|
|
12429
12496
|
const _user = await getUserByEmail(value.email);
|
|
12430
12497
|
if (_user) {
|
|
12431
|
-
throw new
|
|
12498
|
+
throw new import_utils9.BadRequestError(`User already exists: ${value.email}.`);
|
|
12432
12499
|
}
|
|
12433
|
-
const hashedPassword = await (0,
|
|
12500
|
+
const hashedPassword = await (0, import_utils9.hashPassword)(value.password);
|
|
12434
12501
|
const user = {
|
|
12435
12502
|
email: value.email,
|
|
12436
12503
|
password: hashedPassword,
|
|
@@ -12461,26 +12528,26 @@ function useUserService() {
|
|
|
12461
12528
|
lastName = "",
|
|
12462
12529
|
password = ""
|
|
12463
12530
|
} = {}) {
|
|
12464
|
-
const session =
|
|
12531
|
+
const session = import_utils9.useAtlas.getClient()?.startSession();
|
|
12465
12532
|
session?.startTransaction();
|
|
12466
12533
|
try {
|
|
12467
12534
|
const invitation = await _getVerificationById(id);
|
|
12468
12535
|
if (!invitation || !invitation.metadata?.app || !invitation.metadata?.role) {
|
|
12469
|
-
throw new
|
|
12536
|
+
throw new import_utils9.BadRequestError("Invalid invitation.");
|
|
12470
12537
|
}
|
|
12471
12538
|
if (invitation.status === "complete") {
|
|
12472
|
-
throw new
|
|
12539
|
+
throw new import_utils9.BadRequestError("Invitation already used.");
|
|
12473
12540
|
}
|
|
12474
12541
|
const expired = new Date(invitation.expireAt) < /* @__PURE__ */ new Date();
|
|
12475
12542
|
if (invitation.status === "expired" || expired) {
|
|
12476
|
-
throw new
|
|
12543
|
+
throw new import_utils9.BadRequestError("Invitation expired.");
|
|
12477
12544
|
}
|
|
12478
12545
|
const email = invitation.email;
|
|
12479
12546
|
const _user = await getUserByEmail(invitation.email);
|
|
12480
12547
|
if (_user) {
|
|
12481
|
-
throw new
|
|
12548
|
+
throw new import_utils9.BadRequestError(`User already exists: ${email}.`);
|
|
12482
12549
|
}
|
|
12483
|
-
const hashedPassword = await (0,
|
|
12550
|
+
const hashedPassword = await (0, import_utils9.hashPassword)(password);
|
|
12484
12551
|
const user = {
|
|
12485
12552
|
email,
|
|
12486
12553
|
password: hashedPassword,
|
|
@@ -12514,28 +12581,28 @@ function useUserService() {
|
|
|
12514
12581
|
lastName = "",
|
|
12515
12582
|
password = ""
|
|
12516
12583
|
} = {}) {
|
|
12517
|
-
const session =
|
|
12584
|
+
const session = import_utils9.useAtlas.getClient()?.startSession();
|
|
12518
12585
|
session?.startTransaction();
|
|
12519
12586
|
try {
|
|
12520
12587
|
const signUp = await _getVerificationById(id);
|
|
12521
12588
|
if (!signUp) {
|
|
12522
|
-
throw new
|
|
12589
|
+
throw new import_utils9.BadRequestError("Invalid sign up link.");
|
|
12523
12590
|
}
|
|
12524
12591
|
if (signUp.status === "complete") {
|
|
12525
|
-
throw new
|
|
12592
|
+
throw new import_utils9.BadRequestError(
|
|
12526
12593
|
"You have already an account created using this link."
|
|
12527
12594
|
);
|
|
12528
12595
|
}
|
|
12529
12596
|
const expired = new Date(signUp.expireAt) < /* @__PURE__ */ new Date();
|
|
12530
12597
|
if (signUp.status === "expired" || expired) {
|
|
12531
|
-
throw new
|
|
12598
|
+
throw new import_utils9.BadRequestError("Sign up link expired.");
|
|
12532
12599
|
}
|
|
12533
12600
|
const email = signUp.email;
|
|
12534
12601
|
const _user = await getUserByEmail(signUp.email);
|
|
12535
12602
|
if (_user) {
|
|
12536
|
-
throw new
|
|
12603
|
+
throw new import_utils9.BadRequestError(`User already exists: ${email}.`);
|
|
12537
12604
|
}
|
|
12538
|
-
const hashedPassword = await (0,
|
|
12605
|
+
const hashedPassword = await (0, import_utils9.hashPassword)(password);
|
|
12539
12606
|
const user = {
|
|
12540
12607
|
email,
|
|
12541
12608
|
password: hashedPassword,
|
|
@@ -12565,21 +12632,21 @@ function useUserService() {
|
|
|
12565
12632
|
throw error;
|
|
12566
12633
|
}
|
|
12567
12634
|
if (newPassword !== passwordConfirmation) {
|
|
12568
|
-
throw new
|
|
12635
|
+
throw new import_utils9.BadRequestError("Passwords do not match.");
|
|
12569
12636
|
}
|
|
12570
12637
|
let hashedPassword;
|
|
12571
12638
|
try {
|
|
12572
|
-
hashedPassword = await (0,
|
|
12639
|
+
hashedPassword = await (0, import_utils9.hashPassword)(newPassword);
|
|
12573
12640
|
} catch (error) {
|
|
12574
|
-
throw new
|
|
12641
|
+
throw new import_utils9.InternalServerError(`Error hashing password: ${error}`);
|
|
12575
12642
|
}
|
|
12576
12643
|
try {
|
|
12577
12644
|
const otpDoc = await getById(id);
|
|
12578
12645
|
if (!otpDoc) {
|
|
12579
|
-
throw new
|
|
12646
|
+
throw new import_utils9.NotFoundError("You are using an invalid reset link.");
|
|
12580
12647
|
}
|
|
12581
12648
|
if (otpDoc.status === "used") {
|
|
12582
|
-
throw new
|
|
12649
|
+
throw new import_utils9.BadRequestError("This link has already been invalidated.");
|
|
12583
12650
|
}
|
|
12584
12651
|
await updateStatusById(id, "used");
|
|
12585
12652
|
return "Successfully reset password.";
|
|
@@ -12589,15 +12656,15 @@ function useUserService() {
|
|
|
12589
12656
|
}
|
|
12590
12657
|
async function updateName(_id, firstName, lastName) {
|
|
12591
12658
|
if (!_id) {
|
|
12592
|
-
throw new
|
|
12659
|
+
throw new import_utils9.BadRequestError("Invalid user ID");
|
|
12593
12660
|
}
|
|
12594
12661
|
if (!firstName) {
|
|
12595
|
-
throw new
|
|
12662
|
+
throw new import_utils9.BadRequestError("Invalid firstName");
|
|
12596
12663
|
}
|
|
12597
12664
|
if (!lastName) {
|
|
12598
|
-
throw new
|
|
12665
|
+
throw new import_utils9.BadRequestError("Invalid lastName");
|
|
12599
12666
|
}
|
|
12600
|
-
const session =
|
|
12667
|
+
const session = import_utils9.useAtlas.getClient()?.startSession();
|
|
12601
12668
|
session?.startTransaction();
|
|
12602
12669
|
try {
|
|
12603
12670
|
await _updateName({ _id, firstName, lastName }, session);
|
|
@@ -12612,16 +12679,16 @@ function useUserService() {
|
|
|
12612
12679
|
}
|
|
12613
12680
|
async function updateBirthday(_id, month, day, year) {
|
|
12614
12681
|
if (!_id) {
|
|
12615
|
-
throw new
|
|
12682
|
+
throw new import_utils9.BadRequestError("Invalid user ID");
|
|
12616
12683
|
}
|
|
12617
12684
|
if (!month) {
|
|
12618
|
-
throw new
|
|
12685
|
+
throw new import_utils9.BadRequestError("Invalid birth month.");
|
|
12619
12686
|
}
|
|
12620
12687
|
if (!day) {
|
|
12621
|
-
throw new
|
|
12688
|
+
throw new import_utils9.BadRequestError("Invalid birthday.");
|
|
12622
12689
|
}
|
|
12623
12690
|
if (!year) {
|
|
12624
|
-
throw new
|
|
12691
|
+
throw new import_utils9.BadRequestError("Invalid birth year.");
|
|
12625
12692
|
}
|
|
12626
12693
|
try {
|
|
12627
12694
|
await _updateBirthday({ _id, month, day, year });
|
|
@@ -12638,7 +12705,7 @@ function useUserService() {
|
|
|
12638
12705
|
}
|
|
12639
12706
|
}
|
|
12640
12707
|
const { createFile: _createFile, deleteFileById } = useFileRepo();
|
|
12641
|
-
const s3 = new
|
|
12708
|
+
const s3 = new import_utils9.useS3({
|
|
12642
12709
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
12643
12710
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
12644
12711
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -12646,7 +12713,7 @@ function useUserService() {
|
|
|
12646
12713
|
bucket: SPACES_BUCKET
|
|
12647
12714
|
});
|
|
12648
12715
|
async function updateUserProfile({ file, user, previousProfile } = {}) {
|
|
12649
|
-
const session =
|
|
12716
|
+
const session = import_utils9.useAtlas.getClient()?.startSession();
|
|
12650
12717
|
session?.startTransaction();
|
|
12651
12718
|
const _file = {
|
|
12652
12719
|
name: file.originalname,
|
|
@@ -12691,7 +12758,7 @@ function useUserService() {
|
|
|
12691
12758
|
}
|
|
12692
12759
|
|
|
12693
12760
|
// src/controllers/user.controller.ts
|
|
12694
|
-
var
|
|
12761
|
+
var import_utils10 = require("@goweekdays/utils");
|
|
12695
12762
|
var import_joi2 = __toESM(require("joi"));
|
|
12696
12763
|
function useUserController() {
|
|
12697
12764
|
const {
|
|
@@ -12715,7 +12782,7 @@ function useUserController() {
|
|
|
12715
12782
|
});
|
|
12716
12783
|
const { error } = validation.validate({ status, search, page });
|
|
12717
12784
|
if (error) {
|
|
12718
|
-
next(new
|
|
12785
|
+
next(new import_utils10.BadRequestError(error.message));
|
|
12719
12786
|
}
|
|
12720
12787
|
try {
|
|
12721
12788
|
const items = await _getUsers({ status, search, page });
|
|
@@ -12729,12 +12796,12 @@ function useUserController() {
|
|
|
12729
12796
|
const id = req.params.id || "";
|
|
12730
12797
|
const validation = import_joi2.default.string().hex().validate(id);
|
|
12731
12798
|
if (validation.error) {
|
|
12732
|
-
throw new
|
|
12799
|
+
throw new import_utils10.BadRequestError("Invalid id.");
|
|
12733
12800
|
}
|
|
12734
12801
|
try {
|
|
12735
12802
|
const user = await _getUserById(id);
|
|
12736
12803
|
if (!user) {
|
|
12737
|
-
throw new
|
|
12804
|
+
throw new import_utils10.BadRequestError("User not found.");
|
|
12738
12805
|
}
|
|
12739
12806
|
res.json(user);
|
|
12740
12807
|
} catch (error) {
|
|
@@ -12751,7 +12818,7 @@ function useUserController() {
|
|
|
12751
12818
|
});
|
|
12752
12819
|
const { error } = validation.validate({ firstName, lastName });
|
|
12753
12820
|
if (error) {
|
|
12754
|
-
next(new
|
|
12821
|
+
next(new import_utils10.BadRequestError(error.message));
|
|
12755
12822
|
return;
|
|
12756
12823
|
}
|
|
12757
12824
|
try {
|
|
@@ -12774,7 +12841,7 @@ function useUserController() {
|
|
|
12774
12841
|
});
|
|
12775
12842
|
const { error } = validation.validate({ month, day, year });
|
|
12776
12843
|
if (error) {
|
|
12777
|
-
next(new
|
|
12844
|
+
next(new import_utils10.BadRequestError(error.message));
|
|
12778
12845
|
return;
|
|
12779
12846
|
}
|
|
12780
12847
|
try {
|
|
@@ -12799,7 +12866,7 @@ function useUserController() {
|
|
|
12799
12866
|
});
|
|
12800
12867
|
const { error } = validation.validate({ _id, field, value });
|
|
12801
12868
|
if (error) {
|
|
12802
|
-
next(new
|
|
12869
|
+
next(new import_utils10.BadRequestError(error.message));
|
|
12803
12870
|
return;
|
|
12804
12871
|
}
|
|
12805
12872
|
try {
|
|
@@ -12820,7 +12887,7 @@ function useUserController() {
|
|
|
12820
12887
|
});
|
|
12821
12888
|
const { error } = validation.validate({ previousProfile });
|
|
12822
12889
|
if (error) {
|
|
12823
|
-
next(new
|
|
12890
|
+
next(new import_utils10.BadRequestError(error.message));
|
|
12824
12891
|
return;
|
|
12825
12892
|
}
|
|
12826
12893
|
const user = req.headers["user"] ?? "";
|
|
@@ -12833,10 +12900,10 @@ function useUserController() {
|
|
|
12833
12900
|
res.json({ message: "Successfully updated profile picture." });
|
|
12834
12901
|
return;
|
|
12835
12902
|
} catch (error2) {
|
|
12836
|
-
if (error2 instanceof
|
|
12903
|
+
if (error2 instanceof import_utils10.AppError) {
|
|
12837
12904
|
next(error2);
|
|
12838
12905
|
} else {
|
|
12839
|
-
next(new
|
|
12906
|
+
next(new import_utils10.InternalServerError(error2));
|
|
12840
12907
|
}
|
|
12841
12908
|
}
|
|
12842
12909
|
}
|
|
@@ -12861,7 +12928,7 @@ function useUserController() {
|
|
|
12861
12928
|
type
|
|
12862
12929
|
});
|
|
12863
12930
|
if (error) {
|
|
12864
|
-
next(new
|
|
12931
|
+
next(new import_utils10.BadRequestError(error.message));
|
|
12865
12932
|
return;
|
|
12866
12933
|
}
|
|
12867
12934
|
try {
|
|
@@ -12894,67 +12961,67 @@ function useUserController() {
|
|
|
12894
12961
|
}
|
|
12895
12962
|
|
|
12896
12963
|
// src/services/auth.service.ts
|
|
12897
|
-
var
|
|
12964
|
+
var import_utils11 = require("@goweekdays/utils");
|
|
12898
12965
|
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
|
12899
12966
|
function useAuthService() {
|
|
12900
12967
|
const expiresIn = "1m";
|
|
12901
12968
|
async function login({ email, password } = {}) {
|
|
12902
12969
|
if (!email) {
|
|
12903
|
-
throw new
|
|
12970
|
+
throw new import_utils11.BadRequestError("Email is required");
|
|
12904
12971
|
}
|
|
12905
12972
|
if (!password) {
|
|
12906
|
-
throw new
|
|
12973
|
+
throw new import_utils11.BadRequestError("Password is required");
|
|
12907
12974
|
}
|
|
12908
12975
|
let _user;
|
|
12909
12976
|
try {
|
|
12910
12977
|
_user = await useUserRepo().getUserByEmail(email);
|
|
12911
12978
|
} catch (error) {
|
|
12912
|
-
if (error instanceof
|
|
12979
|
+
if (error instanceof import_utils11.AppError) {
|
|
12913
12980
|
throw error;
|
|
12914
12981
|
} else {
|
|
12915
|
-
throw new
|
|
12982
|
+
throw new import_utils11.InternalServerError(`${error}`);
|
|
12916
12983
|
}
|
|
12917
12984
|
}
|
|
12918
12985
|
if (!_user) {
|
|
12919
|
-
throw new
|
|
12986
|
+
throw new import_utils11.NotFoundError(
|
|
12920
12987
|
"Invalid user email. Please check your email and try again."
|
|
12921
12988
|
);
|
|
12922
12989
|
}
|
|
12923
12990
|
if (_user.status === "suspended") {
|
|
12924
|
-
throw new
|
|
12991
|
+
throw new import_utils11.BadRequestError(
|
|
12925
12992
|
"Your account is currently suspended. Please contact support for assistance."
|
|
12926
12993
|
);
|
|
12927
12994
|
}
|
|
12928
|
-
const isPasswordValid = await (0,
|
|
12995
|
+
const isPasswordValid = await (0, import_utils11.comparePassword)(password, _user.password);
|
|
12929
12996
|
if (!isPasswordValid) {
|
|
12930
|
-
throw new
|
|
12997
|
+
throw new import_utils11.BadRequestError("Invalid password");
|
|
12931
12998
|
}
|
|
12932
12999
|
const metadata = { user: _user._id };
|
|
12933
13000
|
let refreshToken2;
|
|
12934
13001
|
try {
|
|
12935
|
-
refreshToken2 = (0,
|
|
13002
|
+
refreshToken2 = (0, import_utils11.generateToken)({
|
|
12936
13003
|
secret: REFRESH_TOKEN_SECRET,
|
|
12937
13004
|
metadata,
|
|
12938
13005
|
options: { expiresIn: "7d" }
|
|
12939
13006
|
});
|
|
12940
13007
|
} catch (error) {
|
|
12941
|
-
throw new
|
|
13008
|
+
throw new import_utils11.BadRequestError("Error generating refresh token");
|
|
12942
13009
|
}
|
|
12943
13010
|
let accessToken;
|
|
12944
13011
|
try {
|
|
12945
|
-
accessToken = (0,
|
|
13012
|
+
accessToken = (0, import_utils11.generateToken)({
|
|
12946
13013
|
secret: ACCESS_TOKEN_SECRET,
|
|
12947
13014
|
metadata,
|
|
12948
13015
|
options: { expiresIn }
|
|
12949
13016
|
});
|
|
12950
13017
|
} catch (error) {
|
|
12951
|
-
throw new
|
|
13018
|
+
throw new import_utils11.BadRequestError("Error generating access token");
|
|
12952
13019
|
}
|
|
12953
13020
|
const user = _user._id ?? "";
|
|
12954
13021
|
try {
|
|
12955
13022
|
await useTokenRepo().createToken({ token: refreshToken2, user });
|
|
12956
13023
|
} catch (error) {
|
|
12957
|
-
throw new
|
|
13024
|
+
throw new import_utils11.BadRequestError("Error creating refresh token");
|
|
12958
13025
|
}
|
|
12959
13026
|
return { accessToken, refreshToken: refreshToken2, id: _user._id };
|
|
12960
13027
|
}
|
|
@@ -12963,30 +13030,30 @@ function useAuthService() {
|
|
|
12963
13030
|
try {
|
|
12964
13031
|
decoded = await import_jsonwebtoken.default.verify(token, REFRESH_TOKEN_SECRET);
|
|
12965
13032
|
} catch (error) {
|
|
12966
|
-
throw new
|
|
13033
|
+
throw new import_utils11.BadRequestError("Invalid refresh token");
|
|
12967
13034
|
}
|
|
12968
13035
|
let _token;
|
|
12969
13036
|
try {
|
|
12970
13037
|
_token = await useTokenRepo().getToken(token);
|
|
12971
13038
|
if (!_token) {
|
|
12972
|
-
throw new
|
|
13039
|
+
throw new import_utils11.NotFoundError("Invalid token");
|
|
12973
13040
|
}
|
|
12974
13041
|
} catch (error) {
|
|
12975
|
-
if (error instanceof
|
|
13042
|
+
if (error instanceof import_utils11.AppError) {
|
|
12976
13043
|
throw error;
|
|
12977
13044
|
} else {
|
|
12978
|
-
throw new
|
|
13045
|
+
throw new import_utils11.InternalServerError(`${error}`);
|
|
12979
13046
|
}
|
|
12980
13047
|
}
|
|
12981
13048
|
let accessToken;
|
|
12982
13049
|
try {
|
|
12983
|
-
accessToken = (0,
|
|
13050
|
+
accessToken = (0, import_utils11.generateToken)({
|
|
12984
13051
|
secret: ACCESS_TOKEN_SECRET,
|
|
12985
13052
|
metadata: { user: decoded.user },
|
|
12986
13053
|
options: { expiresIn }
|
|
12987
13054
|
});
|
|
12988
13055
|
} catch (error) {
|
|
12989
|
-
throw new
|
|
13056
|
+
throw new import_utils11.BadRequestError("Error generating access token");
|
|
12990
13057
|
}
|
|
12991
13058
|
return accessToken;
|
|
12992
13059
|
}
|
|
@@ -12995,19 +13062,19 @@ function useAuthService() {
|
|
|
12995
13062
|
try {
|
|
12996
13063
|
_token = await useTokenRepo().getToken(token);
|
|
12997
13064
|
if (!_token) {
|
|
12998
|
-
throw new
|
|
13065
|
+
throw new import_utils11.NotFoundError("Invalid token");
|
|
12999
13066
|
}
|
|
13000
13067
|
} catch (error) {
|
|
13001
|
-
if (error instanceof
|
|
13068
|
+
if (error instanceof import_utils11.AppError) {
|
|
13002
13069
|
throw error;
|
|
13003
13070
|
} else {
|
|
13004
|
-
throw new
|
|
13071
|
+
throw new import_utils11.InternalServerError(`${error}`);
|
|
13005
13072
|
}
|
|
13006
13073
|
}
|
|
13007
13074
|
try {
|
|
13008
13075
|
await useTokenRepo().deleteToken(token);
|
|
13009
13076
|
} catch (error) {
|
|
13010
|
-
throw new
|
|
13077
|
+
throw new import_utils11.InternalServerError("Error deleting token");
|
|
13011
13078
|
}
|
|
13012
13079
|
return "Logged out successfully";
|
|
13013
13080
|
}
|
|
@@ -13020,7 +13087,7 @@ function useAuthService() {
|
|
|
13020
13087
|
|
|
13021
13088
|
// src/controllers/auth.controller.ts
|
|
13022
13089
|
var import_joi3 = __toESM(require("joi"));
|
|
13023
|
-
var
|
|
13090
|
+
var import_utils12 = require("@goweekdays/utils");
|
|
13024
13091
|
function useAuthController() {
|
|
13025
13092
|
const { signUp: _signUp } = useVerificationService();
|
|
13026
13093
|
async function login(req, res, next) {
|
|
@@ -13032,50 +13099,50 @@ function useAuthController() {
|
|
|
13032
13099
|
});
|
|
13033
13100
|
const { error } = validation.validate({ email, password });
|
|
13034
13101
|
if (error) {
|
|
13035
|
-
next(new
|
|
13102
|
+
next(new import_utils12.BadRequestError(error.message));
|
|
13036
13103
|
}
|
|
13037
13104
|
try {
|
|
13038
13105
|
const token = await useAuthService().login({ email, password });
|
|
13039
13106
|
res.json(token);
|
|
13040
13107
|
} catch (error2) {
|
|
13041
|
-
if (error2 instanceof
|
|
13108
|
+
if (error2 instanceof import_utils12.AppError) {
|
|
13042
13109
|
next(error2);
|
|
13043
13110
|
} else {
|
|
13044
|
-
next(new
|
|
13111
|
+
next(new import_utils12.InternalServerError("An unexpected error occurred"));
|
|
13045
13112
|
}
|
|
13046
13113
|
}
|
|
13047
13114
|
}
|
|
13048
13115
|
async function refreshToken(req, res, next) {
|
|
13049
13116
|
const refreshToken2 = req.body.token;
|
|
13050
13117
|
if (!refreshToken2) {
|
|
13051
|
-
next(new
|
|
13118
|
+
next(new import_utils12.BadRequestError("Refresh token is required"));
|
|
13052
13119
|
return;
|
|
13053
13120
|
}
|
|
13054
13121
|
try {
|
|
13055
13122
|
const newRefreshToken = await useAuthService().refreshToken(refreshToken2);
|
|
13056
13123
|
res.json({ token: newRefreshToken });
|
|
13057
13124
|
} catch (error) {
|
|
13058
|
-
if (error instanceof
|
|
13125
|
+
if (error instanceof import_utils12.AppError) {
|
|
13059
13126
|
next(error);
|
|
13060
13127
|
} else {
|
|
13061
|
-
next(new
|
|
13128
|
+
next(new import_utils12.InternalServerError("An unexpected error occurred"));
|
|
13062
13129
|
}
|
|
13063
13130
|
}
|
|
13064
13131
|
}
|
|
13065
13132
|
async function logout(req, res, next) {
|
|
13066
13133
|
const token = req.params.id || "";
|
|
13067
13134
|
if (!token) {
|
|
13068
|
-
next(new
|
|
13135
|
+
next(new import_utils12.BadRequestError("Token is required"));
|
|
13069
13136
|
return;
|
|
13070
13137
|
}
|
|
13071
13138
|
try {
|
|
13072
13139
|
await useAuthService().logout(token);
|
|
13073
13140
|
res.json({ message: "Logged out successfully" });
|
|
13074
13141
|
} catch (error) {
|
|
13075
|
-
if (error instanceof
|
|
13142
|
+
if (error instanceof import_utils12.AppError) {
|
|
13076
13143
|
next(error);
|
|
13077
13144
|
} else {
|
|
13078
|
-
next(new
|
|
13145
|
+
next(new import_utils12.InternalServerError("An unexpected error occurred"));
|
|
13079
13146
|
}
|
|
13080
13147
|
}
|
|
13081
13148
|
}
|
|
@@ -13094,7 +13161,7 @@ function useAuthController() {
|
|
|
13094
13161
|
passwordConfirmation
|
|
13095
13162
|
});
|
|
13096
13163
|
if (error) {
|
|
13097
|
-
next(new
|
|
13164
|
+
next(new import_utils12.BadRequestError(error.message));
|
|
13098
13165
|
return;
|
|
13099
13166
|
}
|
|
13100
13167
|
try {
|
|
@@ -13118,7 +13185,7 @@ function useAuthController() {
|
|
|
13118
13185
|
});
|
|
13119
13186
|
const { error } = validation.validate({ email, referralCode });
|
|
13120
13187
|
if (error) {
|
|
13121
|
-
next(new
|
|
13188
|
+
next(new import_utils12.BadRequestError(error.message));
|
|
13122
13189
|
return;
|
|
13123
13190
|
}
|
|
13124
13191
|
try {
|
|
@@ -13154,8 +13221,17 @@ var MRole = class {
|
|
|
13154
13221
|
throw new Error("Invalid _id.");
|
|
13155
13222
|
}
|
|
13156
13223
|
}
|
|
13224
|
+
if (typeof value.org === "string") {
|
|
13225
|
+
try {
|
|
13226
|
+
value.org = new import_mongodb8.ObjectId(value.org);
|
|
13227
|
+
} catch (error) {
|
|
13228
|
+
throw new Error("Invalid org.");
|
|
13229
|
+
}
|
|
13230
|
+
}
|
|
13231
|
+
this.org = value.org ?? "";
|
|
13157
13232
|
this._id = value._id ?? new import_mongodb8.ObjectId();
|
|
13158
13233
|
this.name = value.name ?? "";
|
|
13234
|
+
this.description = value.description ?? "";
|
|
13159
13235
|
this.permissions = value.permissions ?? [];
|
|
13160
13236
|
this.type = value.type ? value.type : "account";
|
|
13161
13237
|
this.status = value.status ?? "active";
|
|
@@ -13174,12 +13250,12 @@ var MRole = class {
|
|
|
13174
13250
|
};
|
|
13175
13251
|
|
|
13176
13252
|
// src/repositories/role.repository.ts
|
|
13177
|
-
var
|
|
13253
|
+
var import_utils13 = require("@goweekdays/utils");
|
|
13178
13254
|
var import_mongodb9 = require("mongodb");
|
|
13179
13255
|
function useRoleRepo() {
|
|
13180
|
-
const db =
|
|
13256
|
+
const db = import_utils13.useAtlas.getDb();
|
|
13181
13257
|
if (!db) {
|
|
13182
|
-
throw new
|
|
13258
|
+
throw new import_utils13.InternalServerError("Unable to connect to server.");
|
|
13183
13259
|
}
|
|
13184
13260
|
const collection = db.collection("roles");
|
|
13185
13261
|
async function createIndex() {
|
|
@@ -13188,21 +13264,21 @@ function useRoleRepo() {
|
|
|
13188
13264
|
await collection.createIndex({ type: 1 });
|
|
13189
13265
|
await collection.createIndex({ status: 1 });
|
|
13190
13266
|
} catch (error) {
|
|
13191
|
-
throw new
|
|
13267
|
+
throw new import_utils13.InternalServerError("Failed to create index on role.");
|
|
13192
13268
|
}
|
|
13193
13269
|
}
|
|
13194
13270
|
async function createTextIndex() {
|
|
13195
13271
|
try {
|
|
13196
13272
|
await collection.createIndex({ name: "text" });
|
|
13197
13273
|
} catch (error) {
|
|
13198
|
-
throw new
|
|
13274
|
+
throw new import_utils13.InternalServerError("Failed to create text index on role.");
|
|
13199
13275
|
}
|
|
13200
13276
|
}
|
|
13201
13277
|
async function createUniqueIndex() {
|
|
13202
13278
|
try {
|
|
13203
13279
|
await collection.createIndex({ name: 1, type: 1 }, { unique: true });
|
|
13204
13280
|
} catch (error) {
|
|
13205
|
-
throw new
|
|
13281
|
+
throw new import_utils13.InternalServerError("Failed to create unique index on role.");
|
|
13206
13282
|
}
|
|
13207
13283
|
}
|
|
13208
13284
|
async function addRole(value, session) {
|
|
@@ -13211,46 +13287,46 @@ function useRoleRepo() {
|
|
|
13211
13287
|
const res = await collection.insertOne(value, { session });
|
|
13212
13288
|
return res.insertedId;
|
|
13213
13289
|
} catch (error) {
|
|
13214
|
-
|
|
13290
|
+
import_utils13.logger.log({ level: "error", message: `${error}` });
|
|
13215
13291
|
const isDuplicated = error.message.includes("duplicate");
|
|
13216
13292
|
if (isDuplicated) {
|
|
13217
|
-
throw new
|
|
13293
|
+
throw new import_utils13.BadRequestError("Item role already exists");
|
|
13218
13294
|
}
|
|
13219
|
-
throw new
|
|
13295
|
+
throw new import_utils13.InternalServerError("Failed to create role.");
|
|
13220
13296
|
}
|
|
13221
13297
|
}
|
|
13222
13298
|
async function getRoleByUserId(value) {
|
|
13223
13299
|
try {
|
|
13224
13300
|
value = new import_mongodb9.ObjectId(value);
|
|
13225
13301
|
} catch (error) {
|
|
13226
|
-
throw new
|
|
13302
|
+
throw new import_utils13.BadRequestError("Invalid user ID.");
|
|
13227
13303
|
}
|
|
13228
13304
|
try {
|
|
13229
13305
|
return await collection.findOne({ user: value });
|
|
13230
13306
|
} catch (error) {
|
|
13231
|
-
throw new
|
|
13307
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by user ID.");
|
|
13232
13308
|
}
|
|
13233
13309
|
}
|
|
13234
13310
|
async function getRoleById(_id) {
|
|
13235
13311
|
try {
|
|
13236
13312
|
_id = new import_mongodb9.ObjectId(_id);
|
|
13237
13313
|
} catch (error) {
|
|
13238
|
-
throw new
|
|
13314
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
13239
13315
|
}
|
|
13240
13316
|
try {
|
|
13241
13317
|
return await collection.findOne({ _id });
|
|
13242
13318
|
} catch (error) {
|
|
13243
|
-
throw new
|
|
13319
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by ID.");
|
|
13244
13320
|
}
|
|
13245
13321
|
}
|
|
13246
13322
|
async function getRoleByName(name) {
|
|
13247
13323
|
if (!name) {
|
|
13248
|
-
throw new
|
|
13324
|
+
throw new import_utils13.BadRequestError("Role name is required.");
|
|
13249
13325
|
}
|
|
13250
13326
|
try {
|
|
13251
13327
|
return await collection.findOne({ name });
|
|
13252
13328
|
} catch (error) {
|
|
13253
|
-
throw new
|
|
13329
|
+
throw new import_utils13.InternalServerError("Failed to retrieve role by name.");
|
|
13254
13330
|
}
|
|
13255
13331
|
}
|
|
13256
13332
|
async function getRoles({
|
|
@@ -13277,20 +13353,20 @@ function useRoleRepo() {
|
|
|
13277
13353
|
{ $limit: limit }
|
|
13278
13354
|
]).toArray();
|
|
13279
13355
|
const length = await collection.countDocuments(query);
|
|
13280
|
-
return (0,
|
|
13356
|
+
return (0, import_utils13.paginate)(items, page, limit, length);
|
|
13281
13357
|
} catch (error) {
|
|
13282
|
-
|
|
13358
|
+
import_utils13.logger.log({ level: "error", message: `${error}` });
|
|
13283
13359
|
throw error;
|
|
13284
13360
|
}
|
|
13285
13361
|
}
|
|
13286
13362
|
async function updateRole(_id, value, session) {
|
|
13287
13363
|
if (!_id) {
|
|
13288
|
-
throw new
|
|
13364
|
+
throw new import_utils13.BadRequestError("Role ID is required.");
|
|
13289
13365
|
}
|
|
13290
13366
|
try {
|
|
13291
13367
|
_id = new import_mongodb9.ObjectId(_id);
|
|
13292
13368
|
} catch (error) {
|
|
13293
|
-
throw new
|
|
13369
|
+
throw new import_utils13.BadRequestError("Invalid role ID.");
|
|
13294
13370
|
}
|
|
13295
13371
|
if (!value.name) {
|
|
13296
13372
|
delete value.name;
|
|
@@ -13303,17 +13379,17 @@ function useRoleRepo() {
|
|
|
13303
13379
|
await collection.updateOne({ _id }, { $set: value }, { session });
|
|
13304
13380
|
return "Successfully updated role.";
|
|
13305
13381
|
} catch (error) {
|
|
13306
|
-
throw new
|
|
13382
|
+
throw new import_utils13.InternalServerError("Failed to update role.");
|
|
13307
13383
|
}
|
|
13308
13384
|
} else {
|
|
13309
|
-
throw new
|
|
13385
|
+
throw new import_utils13.BadRequestError("No fields to update.");
|
|
13310
13386
|
}
|
|
13311
13387
|
}
|
|
13312
13388
|
async function deleteRole(_id, session) {
|
|
13313
13389
|
try {
|
|
13314
13390
|
_id = new import_mongodb9.ObjectId(_id);
|
|
13315
13391
|
} catch (error) {
|
|
13316
|
-
throw new
|
|
13392
|
+
throw new import_utils13.BadRequestError("Invalid ID.");
|
|
13317
13393
|
}
|
|
13318
13394
|
try {
|
|
13319
13395
|
await collection.deleteOne(
|
|
@@ -13324,7 +13400,7 @@ function useRoleRepo() {
|
|
|
13324
13400
|
);
|
|
13325
13401
|
return "Successfully deleted role.";
|
|
13326
13402
|
} catch (error) {
|
|
13327
|
-
throw new
|
|
13403
|
+
throw new import_utils13.InternalServerError("Failed to delete role.");
|
|
13328
13404
|
}
|
|
13329
13405
|
}
|
|
13330
13406
|
return {
|
|
@@ -13342,7 +13418,7 @@ function useRoleRepo() {
|
|
|
13342
13418
|
}
|
|
13343
13419
|
|
|
13344
13420
|
// src/services/file.service.ts
|
|
13345
|
-
var
|
|
13421
|
+
var import_utils14 = require("@goweekdays/utils");
|
|
13346
13422
|
var import_node_cron = __toESM(require("node-cron"));
|
|
13347
13423
|
function useFileService() {
|
|
13348
13424
|
const {
|
|
@@ -13350,7 +13426,7 @@ function useFileService() {
|
|
|
13350
13426
|
deleteFileById,
|
|
13351
13427
|
getAllDraftedFiles
|
|
13352
13428
|
} = useFileRepo();
|
|
13353
|
-
const s3 = new
|
|
13429
|
+
const s3 = new import_utils14.useS3({
|
|
13354
13430
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
13355
13431
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
13356
13432
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -13358,7 +13434,7 @@ function useFileService() {
|
|
|
13358
13434
|
bucket: SPACES_BUCKET
|
|
13359
13435
|
});
|
|
13360
13436
|
async function createFile(value) {
|
|
13361
|
-
const session =
|
|
13437
|
+
const session = import_utils14.useAtlas.getClient()?.startSession();
|
|
13362
13438
|
session?.startTransaction();
|
|
13363
13439
|
const file = {
|
|
13364
13440
|
name: value.originalname,
|
|
@@ -13381,7 +13457,7 @@ function useFileService() {
|
|
|
13381
13457
|
}
|
|
13382
13458
|
}
|
|
13383
13459
|
async function deleteFile(id) {
|
|
13384
|
-
const session =
|
|
13460
|
+
const session = import_utils14.useAtlas.getClient()?.startSession();
|
|
13385
13461
|
session?.startTransaction();
|
|
13386
13462
|
try {
|
|
13387
13463
|
await deleteFileById(id, session);
|
|
@@ -13402,12 +13478,12 @@ function useFileService() {
|
|
|
13402
13478
|
const file = files[index];
|
|
13403
13479
|
try {
|
|
13404
13480
|
await deleteFile(file._id.toString());
|
|
13405
|
-
await
|
|
13481
|
+
await import_utils14.logger.log({
|
|
13406
13482
|
level: "info",
|
|
13407
13483
|
message: "Successfully deleted draft files."
|
|
13408
13484
|
});
|
|
13409
13485
|
} catch (error) {
|
|
13410
|
-
|
|
13486
|
+
import_utils14.logger.log({
|
|
13411
13487
|
level: "info",
|
|
13412
13488
|
message: "Successfully deleted draft files."
|
|
13413
13489
|
});
|
|
@@ -13424,7 +13500,7 @@ function useFileService() {
|
|
|
13424
13500
|
}
|
|
13425
13501
|
|
|
13426
13502
|
// src/controllers/file.controller.ts
|
|
13427
|
-
var
|
|
13503
|
+
var import_utils15 = require("@goweekdays/utils");
|
|
13428
13504
|
var import_joi4 = __toESM(require("joi"));
|
|
13429
13505
|
function useFileController() {
|
|
13430
13506
|
const { createFile, deleteFile: _deleteFile } = useFileService();
|
|
@@ -13438,10 +13514,10 @@ function useFileController() {
|
|
|
13438
13514
|
res.json({ message: "Successfully uploaded file", id });
|
|
13439
13515
|
return;
|
|
13440
13516
|
} catch (error) {
|
|
13441
|
-
if (error instanceof
|
|
13517
|
+
if (error instanceof import_utils15.AppError) {
|
|
13442
13518
|
next(error);
|
|
13443
13519
|
} else {
|
|
13444
|
-
next(new
|
|
13520
|
+
next(new import_utils15.InternalServerError(error));
|
|
13445
13521
|
}
|
|
13446
13522
|
}
|
|
13447
13523
|
}
|
|
@@ -13450,17 +13526,17 @@ function useFileController() {
|
|
|
13450
13526
|
const validation = import_joi4.default.string().required();
|
|
13451
13527
|
const { error } = validation.validate(id);
|
|
13452
13528
|
if (error) {
|
|
13453
|
-
next(new
|
|
13529
|
+
next(new import_utils15.BadRequestError(error.message));
|
|
13454
13530
|
}
|
|
13455
13531
|
try {
|
|
13456
13532
|
const message = await _deleteFile(id);
|
|
13457
13533
|
res.json({ message });
|
|
13458
13534
|
return;
|
|
13459
13535
|
} catch (error2) {
|
|
13460
|
-
if (error2 instanceof
|
|
13536
|
+
if (error2 instanceof import_utils15.AppError) {
|
|
13461
13537
|
next(error2);
|
|
13462
13538
|
} else {
|
|
13463
|
-
next(new
|
|
13539
|
+
next(new import_utils15.InternalServerError(error2));
|
|
13464
13540
|
}
|
|
13465
13541
|
}
|
|
13466
13542
|
}
|
|
@@ -13548,7 +13624,7 @@ function useRoleService() {
|
|
|
13548
13624
|
|
|
13549
13625
|
// src/controllers/role.controller.ts
|
|
13550
13626
|
var import_joi5 = __toESM(require("joi"));
|
|
13551
|
-
var
|
|
13627
|
+
var import_utils16 = require("@goweekdays/utils");
|
|
13552
13628
|
function useRoleController() {
|
|
13553
13629
|
const {
|
|
13554
13630
|
createRole: _createRole,
|
|
@@ -13569,7 +13645,7 @@ function useRoleController() {
|
|
|
13569
13645
|
});
|
|
13570
13646
|
const { error } = validation.validate({ name, permissions, type });
|
|
13571
13647
|
if (error) {
|
|
13572
|
-
next(new
|
|
13648
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
13573
13649
|
return;
|
|
13574
13650
|
}
|
|
13575
13651
|
try {
|
|
@@ -13593,7 +13669,7 @@ function useRoleController() {
|
|
|
13593
13669
|
});
|
|
13594
13670
|
const { error } = validation.validate({ search, page, limit, type });
|
|
13595
13671
|
if (error) {
|
|
13596
|
-
next(new
|
|
13672
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
13597
13673
|
return;
|
|
13598
13674
|
}
|
|
13599
13675
|
try {
|
|
@@ -13612,7 +13688,7 @@ function useRoleController() {
|
|
|
13612
13688
|
});
|
|
13613
13689
|
const { error } = validation.validate({ userId });
|
|
13614
13690
|
if (error) {
|
|
13615
|
-
next(new
|
|
13691
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
13616
13692
|
return;
|
|
13617
13693
|
}
|
|
13618
13694
|
try {
|
|
@@ -13630,7 +13706,7 @@ function useRoleController() {
|
|
|
13630
13706
|
});
|
|
13631
13707
|
const { error } = validation.validate({ _id });
|
|
13632
13708
|
if (error) {
|
|
13633
|
-
next(new
|
|
13709
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
13634
13710
|
return;
|
|
13635
13711
|
}
|
|
13636
13712
|
try {
|
|
@@ -13652,7 +13728,7 @@ function useRoleController() {
|
|
|
13652
13728
|
});
|
|
13653
13729
|
const { error } = validation.validate({ _id, name, permissions });
|
|
13654
13730
|
if (error) {
|
|
13655
|
-
next(new
|
|
13731
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
13656
13732
|
return;
|
|
13657
13733
|
}
|
|
13658
13734
|
try {
|
|
@@ -13670,7 +13746,7 @@ function useRoleController() {
|
|
|
13670
13746
|
});
|
|
13671
13747
|
const { error } = validation.validate({ _id });
|
|
13672
13748
|
if (error) {
|
|
13673
|
-
next(new
|
|
13749
|
+
next(new import_utils16.BadRequestError(error.message));
|
|
13674
13750
|
return;
|
|
13675
13751
|
}
|
|
13676
13752
|
try {
|
|
@@ -13708,11 +13784,11 @@ var MEntity = class {
|
|
|
13708
13784
|
|
|
13709
13785
|
// src/repositories/entity.repository.ts
|
|
13710
13786
|
var import_mongodb11 = require("mongodb");
|
|
13711
|
-
var
|
|
13787
|
+
var import_utils17 = require("@goweekdays/utils");
|
|
13712
13788
|
function useEntityRepo() {
|
|
13713
|
-
const db =
|
|
13789
|
+
const db = import_utils17.useAtlas.getDb();
|
|
13714
13790
|
if (!db) {
|
|
13715
|
-
throw new
|
|
13791
|
+
throw new import_utils17.InternalServerError("Unable to connect to server.");
|
|
13716
13792
|
}
|
|
13717
13793
|
const collection = db.collection("entities");
|
|
13718
13794
|
async function createIndex() {
|
|
@@ -13738,12 +13814,12 @@ function useEntityRepo() {
|
|
|
13738
13814
|
});
|
|
13739
13815
|
return res.insertedId.toString();
|
|
13740
13816
|
} catch (error) {
|
|
13741
|
-
|
|
13817
|
+
import_utils17.logger.log({ level: "error", message: `${error}` });
|
|
13742
13818
|
const isDuplicated = error.message.includes("duplicate");
|
|
13743
13819
|
if (isDuplicated) {
|
|
13744
|
-
throw new
|
|
13820
|
+
throw new import_utils17.BadRequestError("Entity name already exists.");
|
|
13745
13821
|
}
|
|
13746
|
-
throw new
|
|
13822
|
+
throw new import_utils17.InternalServerError("Failed to create entity.");
|
|
13747
13823
|
}
|
|
13748
13824
|
}
|
|
13749
13825
|
async function getEntities({
|
|
@@ -13766,23 +13842,23 @@ function useEntityRepo() {
|
|
|
13766
13842
|
{ $limit: limit }
|
|
13767
13843
|
]).toArray();
|
|
13768
13844
|
const length = await collection.countDocuments(query);
|
|
13769
|
-
return (0,
|
|
13845
|
+
return (0, import_utils17.paginate)(items, page, limit, length);
|
|
13770
13846
|
} catch (error) {
|
|
13771
|
-
|
|
13847
|
+
import_utils17.logger.log({ level: "error", message: `${error}` });
|
|
13772
13848
|
throw error;
|
|
13773
13849
|
}
|
|
13774
13850
|
}
|
|
13775
13851
|
async function updateEntityFieldById({ _id, field, value } = {}, session) {
|
|
13776
13852
|
const allowedFields = ["name"];
|
|
13777
13853
|
if (!allowedFields.includes(field)) {
|
|
13778
|
-
throw new
|
|
13854
|
+
throw new import_utils17.BadRequestError(
|
|
13779
13855
|
`Field "${field}" is not allowed to be updated.`
|
|
13780
13856
|
);
|
|
13781
13857
|
}
|
|
13782
13858
|
try {
|
|
13783
13859
|
_id = new import_mongodb11.ObjectId(_id);
|
|
13784
13860
|
} catch (error) {
|
|
13785
|
-
throw new
|
|
13861
|
+
throw new import_utils17.BadRequestError("Invalid ID.");
|
|
13786
13862
|
}
|
|
13787
13863
|
try {
|
|
13788
13864
|
await collection.updateOne(
|
|
@@ -13793,14 +13869,14 @@ function useEntityRepo() {
|
|
|
13793
13869
|
);
|
|
13794
13870
|
return `Successfully updated entity ${field}.`;
|
|
13795
13871
|
} catch (error) {
|
|
13796
|
-
throw new
|
|
13872
|
+
throw new import_utils17.InternalServerError(`Failed to update entity ${field}.`);
|
|
13797
13873
|
}
|
|
13798
13874
|
}
|
|
13799
13875
|
async function deleteEntity(_id) {
|
|
13800
13876
|
try {
|
|
13801
13877
|
_id = new import_mongodb11.ObjectId(_id);
|
|
13802
13878
|
} catch (error) {
|
|
13803
|
-
throw new
|
|
13879
|
+
throw new import_utils17.BadRequestError("Invalid entity ID.");
|
|
13804
13880
|
}
|
|
13805
13881
|
try {
|
|
13806
13882
|
return await collection.updateOne(
|
|
@@ -13822,7 +13898,7 @@ function useEntityRepo() {
|
|
|
13822
13898
|
}
|
|
13823
13899
|
|
|
13824
13900
|
// src/services/entity.service.ts
|
|
13825
|
-
var
|
|
13901
|
+
var import_utils18 = require("@goweekdays/utils");
|
|
13826
13902
|
function useEntityService() {
|
|
13827
13903
|
const {
|
|
13828
13904
|
createEntity: _createEntity,
|
|
@@ -13834,21 +13910,21 @@ function useEntityService() {
|
|
|
13834
13910
|
try {
|
|
13835
13911
|
return await _createEntity(value);
|
|
13836
13912
|
} catch (error) {
|
|
13837
|
-
throw new
|
|
13913
|
+
throw new import_utils18.InternalServerError();
|
|
13838
13914
|
}
|
|
13839
13915
|
}
|
|
13840
13916
|
async function getEntities(value) {
|
|
13841
13917
|
try {
|
|
13842
13918
|
return await _getEntities(value);
|
|
13843
13919
|
} catch (error) {
|
|
13844
|
-
throw new
|
|
13920
|
+
throw new import_utils18.InternalServerError();
|
|
13845
13921
|
}
|
|
13846
13922
|
}
|
|
13847
13923
|
async function updateEntityFieldById(_id, field, value) {
|
|
13848
13924
|
try {
|
|
13849
13925
|
return await _updateEntityFieldById({ _id, field, value });
|
|
13850
13926
|
} catch (error) {
|
|
13851
|
-
throw new
|
|
13927
|
+
throw new import_utils18.InternalServerError();
|
|
13852
13928
|
}
|
|
13853
13929
|
}
|
|
13854
13930
|
async function deleteEntity(_id) {
|
|
@@ -13868,7 +13944,7 @@ function useEntityService() {
|
|
|
13868
13944
|
|
|
13869
13945
|
// src/controllers/entity.controller.ts
|
|
13870
13946
|
var import_joi6 = __toESM(require("joi"));
|
|
13871
|
-
var
|
|
13947
|
+
var import_utils19 = require("@goweekdays/utils");
|
|
13872
13948
|
function useEntityController() {
|
|
13873
13949
|
const {
|
|
13874
13950
|
createEntity: _createEntity,
|
|
@@ -13890,7 +13966,7 @@ function useEntityController() {
|
|
|
13890
13966
|
});
|
|
13891
13967
|
const { error } = validation.validate(value);
|
|
13892
13968
|
if (error) {
|
|
13893
|
-
next(new
|
|
13969
|
+
next(new import_utils19.BadRequestError(error.message));
|
|
13894
13970
|
return;
|
|
13895
13971
|
}
|
|
13896
13972
|
try {
|
|
@@ -13914,7 +13990,7 @@ function useEntityController() {
|
|
|
13914
13990
|
});
|
|
13915
13991
|
const { error } = validation.validate(req.query);
|
|
13916
13992
|
if (error) {
|
|
13917
|
-
next(new
|
|
13993
|
+
next(new import_utils19.BadRequestError(error.message));
|
|
13918
13994
|
return;
|
|
13919
13995
|
}
|
|
13920
13996
|
try {
|
|
@@ -13939,7 +14015,7 @@ function useEntityController() {
|
|
|
13939
14015
|
});
|
|
13940
14016
|
const { error } = validation.validate({ id, field, value });
|
|
13941
14017
|
if (error) {
|
|
13942
|
-
next(new
|
|
14018
|
+
next(new import_utils19.BadRequestError(error.message));
|
|
13943
14019
|
return;
|
|
13944
14020
|
}
|
|
13945
14021
|
try {
|
|
@@ -13958,7 +14034,7 @@ function useEntityController() {
|
|
|
13958
14034
|
});
|
|
13959
14035
|
const { error } = validation.validate({ id });
|
|
13960
14036
|
if (error) {
|
|
13961
|
-
next(new
|
|
14037
|
+
next(new import_utils19.BadRequestError(error.message));
|
|
13962
14038
|
return;
|
|
13963
14039
|
}
|
|
13964
14040
|
try {
|
|
@@ -13994,11 +14070,11 @@ var MWorkflow = class {
|
|
|
13994
14070
|
|
|
13995
14071
|
// src/repositories/workflow.repository.ts
|
|
13996
14072
|
var import_mongodb13 = require("mongodb");
|
|
13997
|
-
var
|
|
14073
|
+
var import_utils20 = require("@goweekdays/utils");
|
|
13998
14074
|
function useWorkflowRepo() {
|
|
13999
|
-
const db =
|
|
14075
|
+
const db = import_utils20.useAtlas.getDb();
|
|
14000
14076
|
if (!db) {
|
|
14001
|
-
throw new
|
|
14077
|
+
throw new import_utils20.InternalServerError("Unable to connect to server.");
|
|
14002
14078
|
}
|
|
14003
14079
|
const collection = db.collection("workflows");
|
|
14004
14080
|
async function createIndex() {
|
|
@@ -14024,9 +14100,9 @@ function useWorkflowRepo() {
|
|
|
14024
14100
|
} catch (error) {
|
|
14025
14101
|
const duplicated = error.message.includes("duplicate");
|
|
14026
14102
|
if (duplicated) {
|
|
14027
|
-
throw new
|
|
14103
|
+
throw new import_utils20.BadRequestError("Workflow name already exists.");
|
|
14028
14104
|
}
|
|
14029
|
-
throw new
|
|
14105
|
+
throw new import_utils20.InternalServerError("Failed to create workflow.");
|
|
14030
14106
|
}
|
|
14031
14107
|
}
|
|
14032
14108
|
async function getWorkflows({
|
|
@@ -14049,9 +14125,9 @@ function useWorkflowRepo() {
|
|
|
14049
14125
|
{ $limit: limit }
|
|
14050
14126
|
]).toArray();
|
|
14051
14127
|
const length = await collection.countDocuments(query);
|
|
14052
|
-
return (0,
|
|
14128
|
+
return (0, import_utils20.paginate)(items, page, limit, length);
|
|
14053
14129
|
} catch (error) {
|
|
14054
|
-
|
|
14130
|
+
import_utils20.logger.log({ level: "error", message: `${error}` });
|
|
14055
14131
|
throw error;
|
|
14056
14132
|
}
|
|
14057
14133
|
}
|
|
@@ -14059,25 +14135,25 @@ function useWorkflowRepo() {
|
|
|
14059
14135
|
try {
|
|
14060
14136
|
_id = new import_mongodb13.ObjectId(_id);
|
|
14061
14137
|
} catch (error) {
|
|
14062
|
-
throw new
|
|
14138
|
+
throw new import_utils20.BadRequestError("Invalid workflow ID.");
|
|
14063
14139
|
}
|
|
14064
14140
|
try {
|
|
14065
14141
|
return await collection.findOne({ _id });
|
|
14066
14142
|
} catch (error) {
|
|
14067
|
-
throw new
|
|
14143
|
+
throw new import_utils20.InternalServerError("Failed to get workflow.");
|
|
14068
14144
|
}
|
|
14069
14145
|
}
|
|
14070
14146
|
async function updateWorkflowFieldById({ _id, field, value } = {}, session) {
|
|
14071
14147
|
const allowedFields = ["name"];
|
|
14072
14148
|
if (!allowedFields.includes(field)) {
|
|
14073
|
-
throw new
|
|
14149
|
+
throw new import_utils20.BadRequestError(
|
|
14074
14150
|
`Field "${field}" is not allowed to be updated.`
|
|
14075
14151
|
);
|
|
14076
14152
|
}
|
|
14077
14153
|
try {
|
|
14078
14154
|
_id = new import_mongodb13.ObjectId(_id);
|
|
14079
14155
|
} catch (error) {
|
|
14080
|
-
throw new
|
|
14156
|
+
throw new import_utils20.BadRequestError("Invalid ID.");
|
|
14081
14157
|
}
|
|
14082
14158
|
try {
|
|
14083
14159
|
await collection.updateOne(
|
|
@@ -14088,14 +14164,14 @@ function useWorkflowRepo() {
|
|
|
14088
14164
|
);
|
|
14089
14165
|
return `Successfully updated entity ${field}.`;
|
|
14090
14166
|
} catch (error) {
|
|
14091
|
-
throw new
|
|
14167
|
+
throw new import_utils20.InternalServerError(`Failed to update entity ${field}.`);
|
|
14092
14168
|
}
|
|
14093
14169
|
}
|
|
14094
14170
|
async function deleteWorkflow(_id) {
|
|
14095
14171
|
try {
|
|
14096
14172
|
_id = new import_mongodb13.ObjectId(_id);
|
|
14097
14173
|
} catch (error) {
|
|
14098
|
-
throw new
|
|
14174
|
+
throw new import_utils20.BadRequestError("Invalid entity ID.");
|
|
14099
14175
|
}
|
|
14100
14176
|
try {
|
|
14101
14177
|
return await collection.updateOne(
|
|
@@ -14381,11 +14457,11 @@ var MCapBldgAct = class {
|
|
|
14381
14457
|
|
|
14382
14458
|
// src/repositories/cap-bldg-act.repository.ts
|
|
14383
14459
|
var import_mongodb15 = require("mongodb");
|
|
14384
|
-
var
|
|
14460
|
+
var import_utils21 = require("@goweekdays/utils");
|
|
14385
14461
|
function useCapBldgActRepo() {
|
|
14386
|
-
const db =
|
|
14462
|
+
const db = import_utils21.useAtlas.getDb();
|
|
14387
14463
|
if (!db) {
|
|
14388
|
-
throw new
|
|
14464
|
+
throw new import_utils21.InternalServerError("Unable to connect to server.");
|
|
14389
14465
|
}
|
|
14390
14466
|
const collection = db.collection("cap-bldg-acts");
|
|
14391
14467
|
async function createIndex() {
|
|
@@ -14417,19 +14493,19 @@ function useCapBldgActRepo() {
|
|
|
14417
14493
|
});
|
|
14418
14494
|
return res.insertedId.toString();
|
|
14419
14495
|
} catch (error) {
|
|
14420
|
-
|
|
14496
|
+
import_utils21.logger.log({ level: "error", message: `${error}` });
|
|
14421
14497
|
const isDuplicated = error.message.includes("duplicate");
|
|
14422
14498
|
if (isDuplicated) {
|
|
14423
|
-
throw new
|
|
14499
|
+
throw new import_utils21.BadRequestError("CapBldgAct name already exists.");
|
|
14424
14500
|
}
|
|
14425
|
-
throw new
|
|
14501
|
+
throw new import_utils21.InternalServerError("Failed to create CapBldgAct.");
|
|
14426
14502
|
}
|
|
14427
14503
|
}
|
|
14428
14504
|
async function updateCapBldgActBasicInfo({ _id, value } = {}, session) {
|
|
14429
14505
|
try {
|
|
14430
14506
|
_id = new import_mongodb15.ObjectId(_id);
|
|
14431
14507
|
} catch (error) {
|
|
14432
|
-
throw new
|
|
14508
|
+
throw new import_utils21.BadRequestError("Invalid CapBldgAct ID.");
|
|
14433
14509
|
}
|
|
14434
14510
|
try {
|
|
14435
14511
|
await collection.updateOne(
|
|
@@ -14440,15 +14516,15 @@ function useCapBldgActRepo() {
|
|
|
14440
14516
|
}
|
|
14441
14517
|
);
|
|
14442
14518
|
} catch (error) {
|
|
14443
|
-
|
|
14444
|
-
throw new
|
|
14519
|
+
import_utils21.logger.log({ level: "error", message: `${error}` });
|
|
14520
|
+
throw new import_utils21.InternalServerError("Failed to update CapBldgAct basic info.");
|
|
14445
14521
|
}
|
|
14446
14522
|
}
|
|
14447
14523
|
async function updateCapBldgActInitRevEval({ _id, value } = {}, session) {
|
|
14448
14524
|
try {
|
|
14449
14525
|
_id = new import_mongodb15.ObjectId(_id);
|
|
14450
14526
|
} catch (error) {
|
|
14451
|
-
throw new
|
|
14527
|
+
throw new import_utils21.BadRequestError("Invalid CapBldgAct ID.");
|
|
14452
14528
|
}
|
|
14453
14529
|
try {
|
|
14454
14530
|
await collection.updateOne(
|
|
@@ -14459,8 +14535,8 @@ function useCapBldgActRepo() {
|
|
|
14459
14535
|
}
|
|
14460
14536
|
);
|
|
14461
14537
|
} catch (error) {
|
|
14462
|
-
|
|
14463
|
-
throw new
|
|
14538
|
+
import_utils21.logger.log({ level: "error", message: `${error}` });
|
|
14539
|
+
throw new import_utils21.InternalServerError(
|
|
14464
14540
|
"Failed to update CapBldgAct initial review and evaluation."
|
|
14465
14541
|
);
|
|
14466
14542
|
}
|
|
@@ -14469,7 +14545,7 @@ function useCapBldgActRepo() {
|
|
|
14469
14545
|
try {
|
|
14470
14546
|
_id = new import_mongodb15.ObjectId(_id);
|
|
14471
14547
|
} catch (error) {
|
|
14472
|
-
throw new
|
|
14548
|
+
throw new import_utils21.BadRequestError("Invalid CapBldgAct ID.");
|
|
14473
14549
|
}
|
|
14474
14550
|
try {
|
|
14475
14551
|
await collection.updateOne(
|
|
@@ -14480,8 +14556,8 @@ function useCapBldgActRepo() {
|
|
|
14480
14556
|
}
|
|
14481
14557
|
);
|
|
14482
14558
|
} catch (error) {
|
|
14483
|
-
|
|
14484
|
-
throw new
|
|
14559
|
+
import_utils21.logger.log({ level: "error", message: `${error}` });
|
|
14560
|
+
throw new import_utils21.InternalServerError(
|
|
14485
14561
|
"Failed to update CapBldgAct assessment criteria."
|
|
14486
14562
|
);
|
|
14487
14563
|
}
|
|
@@ -14490,13 +14566,13 @@ function useCapBldgActRepo() {
|
|
|
14490
14566
|
try {
|
|
14491
14567
|
_id = new import_mongodb15.ObjectId(_id);
|
|
14492
14568
|
} catch (error) {
|
|
14493
|
-
throw new
|
|
14569
|
+
throw new import_utils21.BadRequestError("Invalid CapBldgAct ID.");
|
|
14494
14570
|
}
|
|
14495
14571
|
try {
|
|
14496
14572
|
return await collection.findOne({ _id });
|
|
14497
14573
|
} catch (error) {
|
|
14498
|
-
|
|
14499
|
-
throw new
|
|
14574
|
+
import_utils21.logger.log({ level: "error", message: `${error}` });
|
|
14575
|
+
throw new import_utils21.InternalServerError("Failed to create CapBldgAct.");
|
|
14500
14576
|
}
|
|
14501
14577
|
}
|
|
14502
14578
|
async function getCapBldgActs({
|
|
@@ -14534,9 +14610,9 @@ function useCapBldgActRepo() {
|
|
|
14534
14610
|
{ $limit: limit }
|
|
14535
14611
|
]).toArray();
|
|
14536
14612
|
const length = await collection.countDocuments(query);
|
|
14537
|
-
return (0,
|
|
14613
|
+
return (0, import_utils21.paginate)(items, page, limit, length);
|
|
14538
14614
|
} catch (error) {
|
|
14539
|
-
|
|
14615
|
+
import_utils21.logger.log({ level: "error", message: `${error}` });
|
|
14540
14616
|
throw error;
|
|
14541
14617
|
}
|
|
14542
14618
|
}
|
|
@@ -14554,14 +14630,14 @@ function useCapBldgActRepo() {
|
|
|
14554
14630
|
"status"
|
|
14555
14631
|
];
|
|
14556
14632
|
if (!allowedFields.includes(field)) {
|
|
14557
|
-
throw new
|
|
14633
|
+
throw new import_utils21.BadRequestError(
|
|
14558
14634
|
`Field "${field}" is not allowed to be updated.`
|
|
14559
14635
|
);
|
|
14560
14636
|
}
|
|
14561
14637
|
try {
|
|
14562
14638
|
_id = new import_mongodb15.ObjectId(_id);
|
|
14563
14639
|
} catch (error) {
|
|
14564
|
-
throw new
|
|
14640
|
+
throw new import_utils21.BadRequestError("Invalid ID.");
|
|
14565
14641
|
}
|
|
14566
14642
|
try {
|
|
14567
14643
|
await collection.updateOne(
|
|
@@ -14572,14 +14648,14 @@ function useCapBldgActRepo() {
|
|
|
14572
14648
|
);
|
|
14573
14649
|
return `Successfully updated CapBldgAct ${field}.`;
|
|
14574
14650
|
} catch (error) {
|
|
14575
|
-
throw new
|
|
14651
|
+
throw new import_utils21.InternalServerError(`Failed to update CapBldgAct ${field}.`);
|
|
14576
14652
|
}
|
|
14577
14653
|
}
|
|
14578
14654
|
async function deleteCapBldgAct(_id) {
|
|
14579
14655
|
try {
|
|
14580
14656
|
_id = new import_mongodb15.ObjectId(_id);
|
|
14581
14657
|
} catch (error) {
|
|
14582
|
-
throw new
|
|
14658
|
+
throw new import_utils21.BadRequestError("Invalid CapBldgAct ID.");
|
|
14583
14659
|
}
|
|
14584
14660
|
try {
|
|
14585
14661
|
return await collection.updateOne(
|
|
@@ -14605,7 +14681,7 @@ function useCapBldgActRepo() {
|
|
|
14605
14681
|
}
|
|
14606
14682
|
|
|
14607
14683
|
// src/services/cap-bldg-act.service.ts
|
|
14608
|
-
var
|
|
14684
|
+
var import_utils22 = require("@goweekdays/utils");
|
|
14609
14685
|
function useCapBldgActService() {
|
|
14610
14686
|
const {
|
|
14611
14687
|
createCapBldgAct: _createCapBldgAct,
|
|
@@ -14683,7 +14759,7 @@ function useCapBldgActService() {
|
|
|
14683
14759
|
throw error;
|
|
14684
14760
|
}
|
|
14685
14761
|
}
|
|
14686
|
-
const s3 = new
|
|
14762
|
+
const s3 = new import_utils22.useS3({
|
|
14687
14763
|
accessKeyId: SPACES_ACCESS_KEY,
|
|
14688
14764
|
secretAccessKey: SPACES_SECRET_KEY,
|
|
14689
14765
|
endpoint: SPACES_ENDPOINT,
|
|
@@ -14692,7 +14768,7 @@ function useCapBldgActService() {
|
|
|
14692
14768
|
});
|
|
14693
14769
|
const { createFile: _createFile, deleteFileById } = useFileRepo();
|
|
14694
14770
|
async function uploadAttachment(value, id, field) {
|
|
14695
|
-
const session =
|
|
14771
|
+
const session = import_utils22.useAtlas.getClient()?.startSession();
|
|
14696
14772
|
session?.startTransaction();
|
|
14697
14773
|
const file = {
|
|
14698
14774
|
name: value.originalname,
|
|
@@ -14716,7 +14792,7 @@ function useCapBldgActService() {
|
|
|
14716
14792
|
}
|
|
14717
14793
|
}
|
|
14718
14794
|
async function deleteAttachment(attachment, id, field) {
|
|
14719
|
-
const session =
|
|
14795
|
+
const session = import_utils22.useAtlas.getClient()?.startSession();
|
|
14720
14796
|
session?.startTransaction();
|
|
14721
14797
|
try {
|
|
14722
14798
|
await deleteFileById(attachment, session);
|
|
@@ -14747,7 +14823,7 @@ function useCapBldgActService() {
|
|
|
14747
14823
|
|
|
14748
14824
|
// src/controllers/cap-bldg-act.controller.ts
|
|
14749
14825
|
var import_joi8 = __toESM(require("joi"));
|
|
14750
|
-
var
|
|
14826
|
+
var import_utils23 = require("@goweekdays/utils");
|
|
14751
14827
|
function useCapBldgActController() {
|
|
14752
14828
|
const {
|
|
14753
14829
|
createCapBldgAct: _createCapBldgAct,
|
|
@@ -14774,7 +14850,7 @@ function useCapBldgActController() {
|
|
|
14774
14850
|
});
|
|
14775
14851
|
const { error } = validation.validate(value);
|
|
14776
14852
|
if (error) {
|
|
14777
|
-
next(new
|
|
14853
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14778
14854
|
return;
|
|
14779
14855
|
}
|
|
14780
14856
|
try {
|
|
@@ -14800,7 +14876,7 @@ function useCapBldgActController() {
|
|
|
14800
14876
|
});
|
|
14801
14877
|
const { error } = validation.validate({ ...value, id });
|
|
14802
14878
|
if (error) {
|
|
14803
|
-
next(new
|
|
14879
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14804
14880
|
return;
|
|
14805
14881
|
}
|
|
14806
14882
|
try {
|
|
@@ -14853,7 +14929,7 @@ function useCapBldgActController() {
|
|
|
14853
14929
|
});
|
|
14854
14930
|
const { error } = validation.validate({ ...value, id });
|
|
14855
14931
|
if (error) {
|
|
14856
|
-
next(new
|
|
14932
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14857
14933
|
return;
|
|
14858
14934
|
}
|
|
14859
14935
|
try {
|
|
@@ -14883,7 +14959,7 @@ function useCapBldgActController() {
|
|
|
14883
14959
|
});
|
|
14884
14960
|
const { error } = validation.validate({ ...value, id });
|
|
14885
14961
|
if (error) {
|
|
14886
|
-
next(new
|
|
14962
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14887
14963
|
return;
|
|
14888
14964
|
}
|
|
14889
14965
|
try {
|
|
@@ -14909,7 +14985,7 @@ function useCapBldgActController() {
|
|
|
14909
14985
|
});
|
|
14910
14986
|
const { error } = validation.validate(req.query);
|
|
14911
14987
|
if (error) {
|
|
14912
|
-
next(new
|
|
14988
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14913
14989
|
return;
|
|
14914
14990
|
}
|
|
14915
14991
|
try {
|
|
@@ -14927,7 +15003,7 @@ function useCapBldgActController() {
|
|
|
14927
15003
|
});
|
|
14928
15004
|
const { error } = validation.validate({ id });
|
|
14929
15005
|
if (error) {
|
|
14930
|
-
next(new
|
|
15006
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14931
15007
|
return;
|
|
14932
15008
|
}
|
|
14933
15009
|
try {
|
|
@@ -14959,7 +15035,7 @@ function useCapBldgActController() {
|
|
|
14959
15035
|
});
|
|
14960
15036
|
const { error } = validation.validate({ _id, field, value });
|
|
14961
15037
|
if (error) {
|
|
14962
|
-
next(new
|
|
15038
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14963
15039
|
return;
|
|
14964
15040
|
}
|
|
14965
15041
|
try {
|
|
@@ -14983,7 +15059,7 @@ function useCapBldgActController() {
|
|
|
14983
15059
|
});
|
|
14984
15060
|
const { error } = validation.validate({ id, field });
|
|
14985
15061
|
if (error) {
|
|
14986
|
-
next(new
|
|
15062
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
14987
15063
|
return;
|
|
14988
15064
|
}
|
|
14989
15065
|
try {
|
|
@@ -15001,7 +15077,7 @@ function useCapBldgActController() {
|
|
|
15001
15077
|
const validation = import_joi8.default.string().required();
|
|
15002
15078
|
const { error } = validation.validate(id);
|
|
15003
15079
|
if (error) {
|
|
15004
|
-
next(new
|
|
15080
|
+
next(new import_utils23.BadRequestError(error.message));
|
|
15005
15081
|
}
|
|
15006
15082
|
try {
|
|
15007
15083
|
const message = await _deleteAttachment(attachment, id, field);
|
|
@@ -15056,9 +15132,9 @@ var MComment = class {
|
|
|
15056
15132
|
|
|
15057
15133
|
// src/repositories/comment.repository.ts
|
|
15058
15134
|
var import_mongodb17 = require("mongodb");
|
|
15059
|
-
var
|
|
15135
|
+
var import_utils24 = require("@goweekdays/utils");
|
|
15060
15136
|
function useCommentRepo() {
|
|
15061
|
-
const db =
|
|
15137
|
+
const db = import_utils24.useAtlas.getDb();
|
|
15062
15138
|
if (!db) {
|
|
15063
15139
|
throw new Error("Database not initialized");
|
|
15064
15140
|
}
|
|
@@ -15076,7 +15152,7 @@ function useCommentRepo() {
|
|
|
15076
15152
|
await collection.insertOne(value);
|
|
15077
15153
|
return "Comment added successfully.";
|
|
15078
15154
|
} catch (error) {
|
|
15079
|
-
throw new
|
|
15155
|
+
throw new import_utils24.InternalServerError("Failed to add comment.");
|
|
15080
15156
|
}
|
|
15081
15157
|
}
|
|
15082
15158
|
async function getComments({ search = "", page = 1, limit = 10, sort = {}, document: document2 = "" } = {}) {
|
|
@@ -15101,9 +15177,9 @@ function useCommentRepo() {
|
|
|
15101
15177
|
{ $limit: limit }
|
|
15102
15178
|
]).toArray();
|
|
15103
15179
|
const length = await collection.countDocuments(query);
|
|
15104
|
-
return (0,
|
|
15180
|
+
return (0, import_utils24.paginate)(items, page, limit, length);
|
|
15105
15181
|
} catch (error) {
|
|
15106
|
-
|
|
15182
|
+
import_utils24.logger.log({ level: "error", message: `${error}` });
|
|
15107
15183
|
throw error;
|
|
15108
15184
|
}
|
|
15109
15185
|
}
|
|
@@ -15145,7 +15221,7 @@ function useCommentService() {
|
|
|
15145
15221
|
|
|
15146
15222
|
// src/controllers/comment.controller.ts
|
|
15147
15223
|
var import_joi9 = __toESM(require("joi"));
|
|
15148
|
-
var
|
|
15224
|
+
var import_utils25 = require("@goweekdays/utils");
|
|
15149
15225
|
function useCommentController() {
|
|
15150
15226
|
const { addComment: _addComment, getComments: _getComments } = useCommentService();
|
|
15151
15227
|
async function addComment(req, res, next) {
|
|
@@ -15159,7 +15235,7 @@ function useCommentController() {
|
|
|
15159
15235
|
});
|
|
15160
15236
|
const { error } = validation.validate(value);
|
|
15161
15237
|
if (error) {
|
|
15162
|
-
next(new
|
|
15238
|
+
next(new import_utils25.BadRequestError(error.message));
|
|
15163
15239
|
return;
|
|
15164
15240
|
}
|
|
15165
15241
|
try {
|
|
@@ -15186,7 +15262,7 @@ function useCommentController() {
|
|
|
15186
15262
|
});
|
|
15187
15263
|
const { error } = validation.validate({ page, document: document2 });
|
|
15188
15264
|
if (error) {
|
|
15189
|
-
next(new
|
|
15265
|
+
next(new import_utils25.BadRequestError(error.message));
|
|
15190
15266
|
return;
|
|
15191
15267
|
}
|
|
15192
15268
|
try {
|
|
@@ -15205,24 +15281,34 @@ function useCommentController() {
|
|
|
15205
15281
|
}
|
|
15206
15282
|
|
|
15207
15283
|
// src/models/subscription.model.ts
|
|
15208
|
-
var
|
|
15284
|
+
var import_utils26 = require("@goweekdays/utils");
|
|
15209
15285
|
var import_mongodb18 = require("mongodb");
|
|
15210
15286
|
function MSubscription(value) {
|
|
15211
15287
|
if (value._id) {
|
|
15212
15288
|
try {
|
|
15213
15289
|
value._id = new import_mongodb18.ObjectId(value._id);
|
|
15214
15290
|
} catch (error) {
|
|
15215
|
-
throw new
|
|
15291
|
+
throw new import_utils26.BadRequestError("Invalid ID.");
|
|
15216
15292
|
}
|
|
15217
15293
|
}
|
|
15218
|
-
|
|
15219
|
-
|
|
15220
|
-
|
|
15221
|
-
|
|
15294
|
+
if (value.user) {
|
|
15295
|
+
try {
|
|
15296
|
+
value.user = new import_mongodb18.ObjectId(value.user);
|
|
15297
|
+
} catch (error) {
|
|
15298
|
+
throw new import_utils26.BadRequestError("Invalid user ID.");
|
|
15299
|
+
}
|
|
15300
|
+
}
|
|
15301
|
+
if (value.org) {
|
|
15302
|
+
try {
|
|
15303
|
+
value.org = new import_mongodb18.ObjectId(value.org);
|
|
15304
|
+
} catch (error) {
|
|
15305
|
+
throw new import_utils26.BadRequestError("Invalid org ID.");
|
|
15306
|
+
}
|
|
15222
15307
|
}
|
|
15223
15308
|
return {
|
|
15224
15309
|
_id: value._id,
|
|
15225
|
-
user: value.user,
|
|
15310
|
+
user: value.user ?? "",
|
|
15311
|
+
org: value.org ?? "",
|
|
15226
15312
|
subscriptionId: value.subscriptionId,
|
|
15227
15313
|
status: "active",
|
|
15228
15314
|
createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -15232,12 +15318,12 @@ function MSubscription(value) {
|
|
|
15232
15318
|
}
|
|
15233
15319
|
|
|
15234
15320
|
// src/repositories/subscription.repository.ts
|
|
15235
|
-
var
|
|
15321
|
+
var import_utils27 = require("@goweekdays/utils");
|
|
15236
15322
|
var import_mongodb19 = require("mongodb");
|
|
15237
15323
|
function useSubscriptionRepo() {
|
|
15238
|
-
const db =
|
|
15324
|
+
const db = import_utils27.useAtlas.getDb();
|
|
15239
15325
|
if (!db) {
|
|
15240
|
-
throw new
|
|
15326
|
+
throw new import_utils27.BadRequestError("Unable to connect to server.");
|
|
15241
15327
|
}
|
|
15242
15328
|
const collection = db.collection("subscriptions");
|
|
15243
15329
|
async function createIndex() {
|
|
@@ -15248,56 +15334,56 @@ function useSubscriptionRepo() {
|
|
|
15248
15334
|
{ status: 1 }
|
|
15249
15335
|
]);
|
|
15250
15336
|
} catch (error) {
|
|
15251
|
-
throw new
|
|
15337
|
+
throw new import_utils27.BadRequestError("Failed to create index on subscription.");
|
|
15252
15338
|
}
|
|
15253
15339
|
}
|
|
15254
15340
|
async function createUniqueIndex() {
|
|
15255
15341
|
try {
|
|
15256
15342
|
await collection.createIndex({ user: 1, status: 1 }, { unique: true });
|
|
15257
15343
|
} catch (error) {
|
|
15258
|
-
throw new
|
|
15344
|
+
throw new import_utils27.BadRequestError(
|
|
15259
15345
|
"Failed to create unique index on subscription."
|
|
15260
15346
|
);
|
|
15261
15347
|
}
|
|
15262
15348
|
}
|
|
15263
|
-
async function add(value) {
|
|
15349
|
+
async function add(value, session) {
|
|
15264
15350
|
try {
|
|
15265
15351
|
value = MSubscription(value);
|
|
15266
|
-
const res = await collection.insertOne(value);
|
|
15352
|
+
const res = await collection.insertOne(value, { session });
|
|
15267
15353
|
return res.insertedId;
|
|
15268
15354
|
} catch (error) {
|
|
15269
|
-
throw new
|
|
15355
|
+
throw new import_utils27.BadRequestError("Failed to create subscription.");
|
|
15270
15356
|
}
|
|
15271
15357
|
}
|
|
15272
15358
|
async function getById(_id) {
|
|
15273
15359
|
try {
|
|
15274
15360
|
_id = new import_mongodb19.ObjectId(_id);
|
|
15275
15361
|
} catch (error) {
|
|
15276
|
-
throw new
|
|
15362
|
+
throw new import_utils27.BadRequestError("Invalid ID.");
|
|
15277
15363
|
}
|
|
15278
15364
|
try {
|
|
15279
15365
|
return await collection.findOne({ _id });
|
|
15280
15366
|
} catch (error) {
|
|
15281
|
-
throw new
|
|
15367
|
+
throw new import_utils27.BadRequestError("Failed to get subscription by ID.");
|
|
15282
15368
|
}
|
|
15283
15369
|
}
|
|
15284
15370
|
async function getByUserId(user) {
|
|
15285
15371
|
try {
|
|
15286
15372
|
user = new import_mongodb19.ObjectId(user);
|
|
15287
15373
|
} catch (error) {
|
|
15288
|
-
throw new
|
|
15374
|
+
throw new import_utils27.BadRequestError("Invalid user ID.");
|
|
15289
15375
|
}
|
|
15290
15376
|
try {
|
|
15291
15377
|
return await collection.findOne({ user });
|
|
15292
15378
|
} catch (error) {
|
|
15293
|
-
throw new
|
|
15379
|
+
throw new import_utils27.BadRequestError("Failed to get subscription by ID.");
|
|
15294
15380
|
}
|
|
15295
15381
|
}
|
|
15296
15382
|
async function getBySubscriptionId(subscriptionId) {
|
|
15297
15383
|
try {
|
|
15298
15384
|
return await collection.findOne({ subscriptionId });
|
|
15299
15385
|
} catch (error) {
|
|
15300
|
-
throw new
|
|
15386
|
+
throw new import_utils27.BadRequestError(
|
|
15301
15387
|
"Failed to get subscription by subscription ID."
|
|
15302
15388
|
);
|
|
15303
15389
|
}
|
|
@@ -15323,9 +15409,9 @@ function useSubscriptionRepo() {
|
|
|
15323
15409
|
{ $limit: limit }
|
|
15324
15410
|
]).toArray();
|
|
15325
15411
|
const length = await collection.countDocuments(query);
|
|
15326
|
-
return (0,
|
|
15412
|
+
return (0, import_utils27.paginate)(items, page, limit, length);
|
|
15327
15413
|
} catch (error) {
|
|
15328
|
-
|
|
15414
|
+
import_utils27.logger.log({ level: "error", message: `${error}` });
|
|
15329
15415
|
throw error;
|
|
15330
15416
|
}
|
|
15331
15417
|
}
|
|
@@ -15333,13 +15419,13 @@ function useSubscriptionRepo() {
|
|
|
15333
15419
|
try {
|
|
15334
15420
|
_id = new import_mongodb19.ObjectId(_id);
|
|
15335
15421
|
} catch (error) {
|
|
15336
|
-
throw new
|
|
15422
|
+
throw new import_utils27.BadRequestError("Invalid ID.");
|
|
15337
15423
|
}
|
|
15338
15424
|
try {
|
|
15339
15425
|
await collection.updateOne({ _id }, { $set: { status } });
|
|
15340
15426
|
return "Successfully updated subscription status.";
|
|
15341
15427
|
} catch (error) {
|
|
15342
|
-
throw new
|
|
15428
|
+
throw new import_utils27.BadRequestError("Failed to update subscription status.");
|
|
15343
15429
|
}
|
|
15344
15430
|
}
|
|
15345
15431
|
return {
|
|
@@ -15355,7 +15441,7 @@ function useSubscriptionRepo() {
|
|
|
15355
15441
|
}
|
|
15356
15442
|
|
|
15357
15443
|
// src/services/subscription.service.ts
|
|
15358
|
-
var
|
|
15444
|
+
var import_utils61 = require("@goweekdays/utils");
|
|
15359
15445
|
|
|
15360
15446
|
// node_modules/axios/lib/helpers/bind.js
|
|
15361
15447
|
function bind(fn, thisArg) {
|
|
@@ -18658,177 +18744,1232 @@ var {
|
|
|
18658
18744
|
mergeConfig: mergeConfig2
|
|
18659
18745
|
} = axios_default;
|
|
18660
18746
|
|
|
18661
|
-
// src/services/
|
|
18662
|
-
|
|
18663
|
-
|
|
18664
|
-
|
|
18747
|
+
// src/services/xendit.service.ts
|
|
18748
|
+
var import_utils54 = require("@goweekdays/utils");
|
|
18749
|
+
var import_mongodb20 = require("mongodb");
|
|
18750
|
+
function useXenditService() {
|
|
18751
|
+
const basicAuth = Buffer.from(`${XENDIT_SECRET_KEY}:`).toString("base64");
|
|
18752
|
+
const axios2 = axios_default.create({
|
|
18753
|
+
baseURL: XENDIT_BASE_URL,
|
|
18754
|
+
headers: {
|
|
18755
|
+
"Content-Type": "application/json",
|
|
18756
|
+
Authorization: `Basic ${basicAuth}`
|
|
18757
|
+
}
|
|
18758
|
+
});
|
|
18759
|
+
async function createCustomer({
|
|
18760
|
+
mobile_number = "",
|
|
18761
|
+
email = "",
|
|
18762
|
+
type = "INDIVIDUAL",
|
|
18763
|
+
given_names = "",
|
|
18764
|
+
surname = ""
|
|
18765
|
+
} = {}) {
|
|
18665
18766
|
try {
|
|
18666
|
-
const
|
|
18667
|
-
|
|
18668
|
-
|
|
18669
|
-
|
|
18670
|
-
|
|
18767
|
+
const res = await axios2.post("/customers", {
|
|
18768
|
+
reference_id: new import_mongodb20.ObjectId().toString(),
|
|
18769
|
+
mobile_number,
|
|
18770
|
+
email,
|
|
18771
|
+
type,
|
|
18772
|
+
individual_detail: {
|
|
18773
|
+
given_names,
|
|
18774
|
+
surname
|
|
18775
|
+
}
|
|
18776
|
+
});
|
|
18777
|
+
return res.data;
|
|
18671
18778
|
} catch (error) {
|
|
18672
|
-
throw
|
|
18779
|
+
throw new import_utils54.BadRequestError("Failed to create customer.");
|
|
18673
18780
|
}
|
|
18674
18781
|
}
|
|
18675
|
-
async function
|
|
18782
|
+
async function linkPaymentMethodEWallet({
|
|
18783
|
+
customerId = "",
|
|
18784
|
+
type = "GCASH",
|
|
18785
|
+
success_return_url = `${APP_ACCOUNT}/payment-methods`,
|
|
18786
|
+
failure_return_url = `${APP_ACCOUNT}/payment-methods`,
|
|
18787
|
+
cancel_return_url = `${APP_ACCOUNT}/payment-methods`
|
|
18788
|
+
} = {}) {
|
|
18676
18789
|
try {
|
|
18677
|
-
const
|
|
18678
|
-
|
|
18679
|
-
|
|
18680
|
-
|
|
18681
|
-
|
|
18682
|
-
|
|
18683
|
-
|
|
18684
|
-
|
|
18685
|
-
|
|
18686
|
-
|
|
18790
|
+
const res = await axios2.post("/v2/payment_methods", {
|
|
18791
|
+
type: "EWALLET",
|
|
18792
|
+
reusability: "MULTIPLE_USE",
|
|
18793
|
+
customer_id: customerId,
|
|
18794
|
+
ewallet: {
|
|
18795
|
+
channel_code: type,
|
|
18796
|
+
channel_properties: {
|
|
18797
|
+
success_return_url,
|
|
18798
|
+
failure_return_url,
|
|
18799
|
+
cancel_return_url
|
|
18687
18800
|
}
|
|
18688
18801
|
}
|
|
18689
|
-
);
|
|
18690
|
-
return
|
|
18802
|
+
});
|
|
18803
|
+
return res.data;
|
|
18804
|
+
} catch (error) {
|
|
18805
|
+
throw new import_utils54.BadRequestError("Failed to initiate GCash linking.");
|
|
18806
|
+
}
|
|
18807
|
+
}
|
|
18808
|
+
async function linkPaymentMethodCard({
|
|
18809
|
+
success_return_url = `${APP_ACCOUNT}/payment-methods`,
|
|
18810
|
+
failure_return_url = `${APP_ACCOUNT}/payment-methods`,
|
|
18811
|
+
card_number = "",
|
|
18812
|
+
expiry_month = "",
|
|
18813
|
+
expiry_year = "",
|
|
18814
|
+
cvv = "",
|
|
18815
|
+
cardholder_name = "",
|
|
18816
|
+
currency = "PHP"
|
|
18817
|
+
} = {}) {
|
|
18818
|
+
try {
|
|
18819
|
+
const res = await axios2.post("/v2/payment_methods", {
|
|
18820
|
+
type: "CARD",
|
|
18821
|
+
card: {
|
|
18822
|
+
currency,
|
|
18823
|
+
channel_properties: {
|
|
18824
|
+
skip_three_d_secure: true,
|
|
18825
|
+
success_return_url,
|
|
18826
|
+
failure_return_url
|
|
18827
|
+
},
|
|
18828
|
+
card_information: {
|
|
18829
|
+
card_number,
|
|
18830
|
+
expiry_month,
|
|
18831
|
+
expiry_year,
|
|
18832
|
+
cvv,
|
|
18833
|
+
cardholder_name
|
|
18834
|
+
}
|
|
18835
|
+
},
|
|
18836
|
+
reusability: "MULTIPLE_USE",
|
|
18837
|
+
description: "Linking card to GoWeekdays account."
|
|
18838
|
+
});
|
|
18839
|
+
return res.data;
|
|
18691
18840
|
} catch (error) {
|
|
18692
|
-
throw new
|
|
18841
|
+
throw new import_utils54.BadRequestError("Failed to initiate card linking..");
|
|
18693
18842
|
}
|
|
18694
18843
|
}
|
|
18695
|
-
async function
|
|
18844
|
+
async function eWalletSubsequentPayment({
|
|
18845
|
+
amount = 0,
|
|
18846
|
+
currency = "PHP",
|
|
18847
|
+
payment_method_id = "",
|
|
18848
|
+
customer_id = "",
|
|
18849
|
+
description = ""
|
|
18850
|
+
} = {}) {
|
|
18696
18851
|
try {
|
|
18697
|
-
const
|
|
18698
|
-
|
|
18699
|
-
|
|
18700
|
-
|
|
18701
|
-
|
|
18702
|
-
|
|
18703
|
-
|
|
18704
|
-
|
|
18852
|
+
const res = await axios2.post("/payment_requests", {
|
|
18853
|
+
amount,
|
|
18854
|
+
currency,
|
|
18855
|
+
payment_method_id,
|
|
18856
|
+
customer_id,
|
|
18857
|
+
description
|
|
18858
|
+
});
|
|
18859
|
+
return res.data;
|
|
18860
|
+
} catch (error) {
|
|
18861
|
+
console.log(error);
|
|
18862
|
+
throw new import_utils54.BadRequestError(
|
|
18863
|
+
"Failed to initiate GCash linking and payment request."
|
|
18864
|
+
);
|
|
18865
|
+
}
|
|
18866
|
+
}
|
|
18867
|
+
async function initGCashLinkingAndPaymentRequest({
|
|
18868
|
+
amount = 0,
|
|
18869
|
+
currency = "PHP",
|
|
18870
|
+
countryCode = "PH",
|
|
18871
|
+
customerId = "",
|
|
18872
|
+
success_return_url = `${APP_MAIN}/checkout/success`,
|
|
18873
|
+
failure_return_url = `${APP_MAIN}/checkout/failed`
|
|
18874
|
+
} = {}) {
|
|
18875
|
+
try {
|
|
18876
|
+
const res = await axios2.post("/payment_requests", {
|
|
18877
|
+
amount,
|
|
18878
|
+
currency,
|
|
18879
|
+
country: countryCode,
|
|
18880
|
+
customer_id: customerId,
|
|
18881
|
+
payment_method: {
|
|
18882
|
+
type: "EWALLET",
|
|
18883
|
+
ewallet: {
|
|
18884
|
+
channel_code: "GCASH",
|
|
18885
|
+
channel_properties: {
|
|
18886
|
+
success_return_url,
|
|
18887
|
+
failure_return_url
|
|
18888
|
+
}
|
|
18889
|
+
},
|
|
18890
|
+
reusability: "MULTIPLE_USE"
|
|
18705
18891
|
}
|
|
18892
|
+
});
|
|
18893
|
+
return res.data;
|
|
18894
|
+
} catch (error) {
|
|
18895
|
+
console.log(error);
|
|
18896
|
+
throw new import_utils54.BadRequestError(
|
|
18897
|
+
"Failed to initiate GCash linking and payment request."
|
|
18706
18898
|
);
|
|
18707
|
-
|
|
18899
|
+
}
|
|
18900
|
+
}
|
|
18901
|
+
async function initSubscription({
|
|
18902
|
+
customer = "",
|
|
18903
|
+
currency = "PHP",
|
|
18904
|
+
amount = 0,
|
|
18905
|
+
paymentMethod = "",
|
|
18906
|
+
description = "",
|
|
18907
|
+
interval = "MONTH"
|
|
18908
|
+
} = {}) {
|
|
18909
|
+
try {
|
|
18910
|
+
const res = await axios2.post("/recurring/plans", {
|
|
18911
|
+
reference_id: new import_mongodb20.ObjectId().toString(),
|
|
18912
|
+
customer_id: customer,
|
|
18913
|
+
recurring_action: "PAYMENT",
|
|
18914
|
+
currency,
|
|
18915
|
+
amount,
|
|
18916
|
+
payment_methods: [
|
|
18917
|
+
{
|
|
18918
|
+
payment_method_id: paymentMethod,
|
|
18919
|
+
rank: 1
|
|
18920
|
+
}
|
|
18921
|
+
],
|
|
18922
|
+
schedule: {
|
|
18923
|
+
reference_id: new import_mongodb20.ObjectId().toString(),
|
|
18924
|
+
interval,
|
|
18925
|
+
interval_count: 1,
|
|
18926
|
+
anchor_date: "2022-02-15T09:44:19.546Z",
|
|
18927
|
+
retry_interval: "DAY",
|
|
18928
|
+
retry_interval_count: 1,
|
|
18929
|
+
total_retry: 3,
|
|
18930
|
+
failed_attempt_notifications: [1, 3]
|
|
18931
|
+
},
|
|
18932
|
+
notification_config: {
|
|
18933
|
+
locale: "en",
|
|
18934
|
+
recurring_created: ["WHATSAPP", "EMAIL"],
|
|
18935
|
+
recurring_succeeded: ["WHATSAPP", "EMAIL"],
|
|
18936
|
+
recurring_failed: ["WHATSAPP", "EMAIL"]
|
|
18937
|
+
},
|
|
18938
|
+
failed_cycle_action: "STOP",
|
|
18939
|
+
immediate_action_type: "FULL_AMOUNT",
|
|
18940
|
+
metadata: null,
|
|
18941
|
+
description
|
|
18942
|
+
});
|
|
18943
|
+
return res.data;
|
|
18944
|
+
} catch (error) {
|
|
18945
|
+
console.log(error);
|
|
18946
|
+
throw new import_utils54.BadRequestError("Failed to create subscription.");
|
|
18947
|
+
}
|
|
18948
|
+
}
|
|
18949
|
+
async function checkSubscriptionStatus(subscriptionId) {
|
|
18950
|
+
try {
|
|
18951
|
+
const res = await axios2.get(`/recurring/plans/${subscriptionId}`);
|
|
18952
|
+
return res.data.status;
|
|
18708
18953
|
} catch (error) {
|
|
18709
|
-
throw new
|
|
18954
|
+
throw new import_utils54.BadRequestError("Failed to get subscription status.");
|
|
18710
18955
|
}
|
|
18711
18956
|
}
|
|
18712
18957
|
async function cancelSubscription(subscriptionId) {
|
|
18713
18958
|
try {
|
|
18714
|
-
const
|
|
18715
|
-
|
|
18716
|
-
const response = await axios_default.post(
|
|
18717
|
-
`${PAYPAL_API_URL}/v1/billing/subscriptions/${subscriptionId}/cancel`,
|
|
18718
|
-
{},
|
|
18719
|
-
{
|
|
18720
|
-
headers: {
|
|
18721
|
-
Authorization: `Bearer ${accessToken}`,
|
|
18722
|
-
"Content-Type": "application/json"
|
|
18723
|
-
}
|
|
18724
|
-
}
|
|
18959
|
+
const res = await axios2.get(
|
|
18960
|
+
`/recurring/plans/${subscriptionId}/deactivate`
|
|
18725
18961
|
);
|
|
18726
|
-
|
|
18727
|
-
return { message: "Subscription canceled successfully." };
|
|
18728
|
-
} else {
|
|
18729
|
-
throw new import_utils53.BadRequestError("Failed to cancel subscription.");
|
|
18730
|
-
}
|
|
18962
|
+
return res.data.status;
|
|
18731
18963
|
} catch (error) {
|
|
18732
|
-
throw new
|
|
18964
|
+
throw new import_utils54.BadRequestError("Failed to get subscription status.");
|
|
18733
18965
|
}
|
|
18734
18966
|
}
|
|
18735
18967
|
return {
|
|
18736
|
-
|
|
18737
|
-
|
|
18968
|
+
createCustomer,
|
|
18969
|
+
linkPaymentMethodEWallet,
|
|
18970
|
+
initGCashLinkingAndPaymentRequest,
|
|
18971
|
+
initSubscription,
|
|
18972
|
+
linkPaymentMethodCard,
|
|
18973
|
+
eWalletSubsequentPayment,
|
|
18974
|
+
checkSubscriptionStatus,
|
|
18738
18975
|
cancelSubscription
|
|
18739
18976
|
};
|
|
18740
18977
|
}
|
|
18741
18978
|
|
|
18742
|
-
// src/
|
|
18743
|
-
var
|
|
18744
|
-
|
|
18745
|
-
|
|
18746
|
-
|
|
18747
|
-
|
|
18748
|
-
|
|
18749
|
-
|
|
18750
|
-
cancelSubscription: _cancelSubscription
|
|
18751
|
-
} = useSubscriptionService();
|
|
18752
|
-
async function add(req, res, next) {
|
|
18753
|
-
const value = req.body;
|
|
18754
|
-
const validation = import_joi10.default.object({
|
|
18755
|
-
user: import_joi10.default.string().required(),
|
|
18756
|
-
subscriptionId: import_joi10.default.string().required()
|
|
18757
|
-
});
|
|
18758
|
-
const { error } = validation.validate(value);
|
|
18759
|
-
if (error) {
|
|
18760
|
-
next(new import_utils54.BadRequestError(error.message));
|
|
18761
|
-
}
|
|
18979
|
+
// src/repositories/payment-method.repository.ts
|
|
18980
|
+
var import_utils56 = require("@goweekdays/utils");
|
|
18981
|
+
|
|
18982
|
+
// src/models/payment-method.model.ts
|
|
18983
|
+
var import_utils55 = require("@goweekdays/utils");
|
|
18984
|
+
var import_mongodb21 = require("mongodb");
|
|
18985
|
+
function MPaymentMethod(value) {
|
|
18986
|
+
if (value.user) {
|
|
18762
18987
|
try {
|
|
18763
|
-
|
|
18764
|
-
|
|
18765
|
-
|
|
18766
|
-
return;
|
|
18767
|
-
} catch (error2) {
|
|
18768
|
-
next(error2);
|
|
18988
|
+
value.user = new import_mongodb21.ObjectId(value.user);
|
|
18989
|
+
} catch (error) {
|
|
18990
|
+
throw new import_utils55.BadRequestError("Invalid user ID.");
|
|
18769
18991
|
}
|
|
18770
18992
|
}
|
|
18771
|
-
|
|
18772
|
-
const id = req.params.id;
|
|
18773
|
-
const validation = import_joi10.default.string().required();
|
|
18774
|
-
const { error } = validation.validate(id);
|
|
18775
|
-
if (error) {
|
|
18776
|
-
next(new import_utils54.BadRequestError(error.message));
|
|
18777
|
-
}
|
|
18993
|
+
if (value.org) {
|
|
18778
18994
|
try {
|
|
18779
|
-
|
|
18780
|
-
|
|
18781
|
-
|
|
18782
|
-
} catch (error2) {
|
|
18783
|
-
next(error2);
|
|
18995
|
+
value.org = new import_mongodb21.ObjectId(value.org);
|
|
18996
|
+
} catch (error) {
|
|
18997
|
+
throw new import_utils55.BadRequestError("Invalid org ID.");
|
|
18784
18998
|
}
|
|
18785
18999
|
}
|
|
18786
|
-
|
|
18787
|
-
|
|
18788
|
-
|
|
18789
|
-
|
|
18790
|
-
|
|
18791
|
-
|
|
18792
|
-
|
|
18793
|
-
|
|
18794
|
-
|
|
18795
|
-
|
|
18796
|
-
|
|
18797
|
-
|
|
18798
|
-
|
|
19000
|
+
return {
|
|
19001
|
+
_id: value._id ?? new import_mongodb21.ObjectId(),
|
|
19002
|
+
user: value.user ?? "",
|
|
19003
|
+
org: value.org ?? "",
|
|
19004
|
+
name: value.name,
|
|
19005
|
+
description: value.description,
|
|
19006
|
+
type: value.type,
|
|
19007
|
+
paymentId: value.paymentId ?? "",
|
|
19008
|
+
customerId: value.customerId ?? "",
|
|
19009
|
+
number: value.number,
|
|
19010
|
+
expiry: value.expiry ?? "",
|
|
19011
|
+
cvv: value.cvv ?? "",
|
|
19012
|
+
status: value.status ?? "active",
|
|
19013
|
+
createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
19014
|
+
deletedAt: value.deletedAt ?? ""
|
|
19015
|
+
};
|
|
19016
|
+
}
|
|
19017
|
+
|
|
19018
|
+
// src/repositories/payment-method.repository.ts
|
|
19019
|
+
var import_mongodb22 = require("mongodb");
|
|
19020
|
+
function usePaymentMethodRepo() {
|
|
19021
|
+
const db = import_utils56.useAtlas.getDb();
|
|
19022
|
+
if (!db) {
|
|
19023
|
+
throw new import_utils56.BadRequestError("Unable to connect to server.");
|
|
19024
|
+
}
|
|
19025
|
+
const collection = db.collection("payment-methods");
|
|
19026
|
+
async function createIndex() {
|
|
18799
19027
|
try {
|
|
18800
|
-
|
|
18801
|
-
|
|
18802
|
-
|
|
18803
|
-
}
|
|
18804
|
-
|
|
19028
|
+
await collection.createIndex([{ user: 1 }]);
|
|
19029
|
+
} catch (error) {
|
|
19030
|
+
throw new import_utils56.BadRequestError("Failed to create index on payment method.");
|
|
19031
|
+
}
|
|
19032
|
+
}
|
|
19033
|
+
async function createUniqueIndex() {
|
|
19034
|
+
try {
|
|
19035
|
+
await collection.createIndex(
|
|
19036
|
+
{
|
|
19037
|
+
user: 1,
|
|
19038
|
+
org: 1,
|
|
19039
|
+
name: 1,
|
|
19040
|
+
paymentId: 1,
|
|
19041
|
+
number: 1
|
|
19042
|
+
},
|
|
19043
|
+
{ unique: true }
|
|
19044
|
+
);
|
|
19045
|
+
} catch (error) {
|
|
19046
|
+
throw new import_utils56.BadRequestError(
|
|
19047
|
+
"Failed to create unique index on payment method."
|
|
19048
|
+
);
|
|
19049
|
+
}
|
|
19050
|
+
}
|
|
19051
|
+
async function add(value, session) {
|
|
19052
|
+
try {
|
|
19053
|
+
value = MPaymentMethod(value);
|
|
19054
|
+
await collection.insertOne(value, { session });
|
|
19055
|
+
return "Successfully added payment method.";
|
|
19056
|
+
} catch (error) {
|
|
19057
|
+
import_utils56.logger.log({ level: "error", message: `${error}` });
|
|
19058
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
19059
|
+
if (isDuplicated) {
|
|
19060
|
+
throw new import_utils56.BadRequestError("Payment method already exist.");
|
|
19061
|
+
}
|
|
19062
|
+
throw new import_utils56.InternalServerError("Failed to add payment method.");
|
|
19063
|
+
}
|
|
19064
|
+
}
|
|
19065
|
+
function getByUser(user) {
|
|
19066
|
+
try {
|
|
19067
|
+
user = new import_mongodb22.ObjectId(user);
|
|
19068
|
+
} catch (error) {
|
|
19069
|
+
throw new import_utils56.BadRequestError("Invalid user ID.");
|
|
19070
|
+
}
|
|
19071
|
+
try {
|
|
19072
|
+
return collection.aggregate([
|
|
19073
|
+
{ $match: { user } },
|
|
19074
|
+
{ $sort: { createdAt: -1 } },
|
|
19075
|
+
{
|
|
19076
|
+
$project: {
|
|
19077
|
+
_id: 1,
|
|
19078
|
+
name: 1,
|
|
19079
|
+
type: 1,
|
|
19080
|
+
number: 1,
|
|
19081
|
+
paymentId: 1,
|
|
19082
|
+
customerId: 1,
|
|
19083
|
+
status: 1
|
|
19084
|
+
}
|
|
19085
|
+
}
|
|
19086
|
+
]).toArray();
|
|
19087
|
+
} catch (error) {
|
|
19088
|
+
throw new import_utils56.BadRequestError("Failed to get payment methods.");
|
|
19089
|
+
}
|
|
19090
|
+
}
|
|
19091
|
+
async function getByPaymentMethodId(paymentId) {
|
|
19092
|
+
if (!paymentId) {
|
|
19093
|
+
throw new import_utils56.BadRequestError("Invalid payment method ID.");
|
|
19094
|
+
}
|
|
19095
|
+
try {
|
|
19096
|
+
return await collection.findOne({ paymentId });
|
|
19097
|
+
} catch (error) {
|
|
19098
|
+
throw new import_utils56.BadRequestError("Failed to retrieve payment method.");
|
|
19099
|
+
}
|
|
19100
|
+
}
|
|
19101
|
+
async function deleteById(_id) {
|
|
19102
|
+
try {
|
|
19103
|
+
_id = new import_mongodb22.ObjectId(_id);
|
|
19104
|
+
} catch (error) {
|
|
19105
|
+
throw new import_utils56.BadRequestError("Invalid payment method ID.");
|
|
19106
|
+
}
|
|
19107
|
+
try {
|
|
19108
|
+
await collection.updateOne(
|
|
19109
|
+
{ _id },
|
|
19110
|
+
{ $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } }
|
|
19111
|
+
);
|
|
19112
|
+
return "Successfully deleted payment method.";
|
|
19113
|
+
} catch (error) {
|
|
19114
|
+
throw new import_utils56.BadRequestError("Failed to delete payment method.");
|
|
19115
|
+
}
|
|
19116
|
+
}
|
|
19117
|
+
return {
|
|
19118
|
+
createIndex,
|
|
19119
|
+
add,
|
|
19120
|
+
getByUser,
|
|
19121
|
+
deleteById,
|
|
19122
|
+
createUniqueIndex,
|
|
19123
|
+
getByPaymentMethodId
|
|
19124
|
+
};
|
|
19125
|
+
}
|
|
19126
|
+
|
|
19127
|
+
// src/repositories/organization.repository.ts
|
|
19128
|
+
var import_utils58 = require("@goweekdays/utils");
|
|
19129
|
+
|
|
19130
|
+
// src/models/organization.model.ts
|
|
19131
|
+
var import_utils57 = require("@goweekdays/utils");
|
|
19132
|
+
var import_joi10 = __toESM(require("joi"));
|
|
19133
|
+
var import_mongodb23 = require("mongodb");
|
|
19134
|
+
function MOrg(value) {
|
|
19135
|
+
const schema = import_joi10.default.object({
|
|
19136
|
+
_id: import_joi10.default.string().hex().optional().allow("", null),
|
|
19137
|
+
name: import_joi10.default.string().required(),
|
|
19138
|
+
email: import_joi10.default.string().email().required(),
|
|
19139
|
+
contact: import_joi10.default.string().required(),
|
|
19140
|
+
type: import_joi10.default.string().required(),
|
|
19141
|
+
busInst: import_joi10.default.string().optional().allow("", null),
|
|
19142
|
+
description: import_joi10.default.string().optional().allow("", null),
|
|
19143
|
+
status: import_joi10.default.string().optional().allow("", null),
|
|
19144
|
+
createdAt: import_joi10.default.string().optional().allow("", null),
|
|
19145
|
+
updatedAt: import_joi10.default.string().optional().allow("", null),
|
|
19146
|
+
deletedAt: import_joi10.default.string().optional().allow("", null)
|
|
19147
|
+
});
|
|
19148
|
+
const { error } = schema.validate(value);
|
|
19149
|
+
if (error) {
|
|
19150
|
+
throw new import_utils57.BadRequestError(error.message);
|
|
19151
|
+
}
|
|
19152
|
+
if (value._id) {
|
|
19153
|
+
try {
|
|
19154
|
+
value._id = new import_mongodb23.ObjectId(value._id);
|
|
19155
|
+
} catch (error2) {
|
|
19156
|
+
throw new import_utils57.BadRequestError("Invalid ID.");
|
|
19157
|
+
}
|
|
19158
|
+
}
|
|
19159
|
+
return {
|
|
19160
|
+
_id: value._id,
|
|
19161
|
+
name: value.name,
|
|
19162
|
+
description: value.description,
|
|
19163
|
+
type: value.type,
|
|
19164
|
+
email: value.email,
|
|
19165
|
+
contact: value.contact,
|
|
19166
|
+
status: value.status || "active",
|
|
19167
|
+
createdAt: value.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
19168
|
+
updatedAt: "",
|
|
19169
|
+
deletedAt: ""
|
|
19170
|
+
};
|
|
19171
|
+
}
|
|
19172
|
+
|
|
19173
|
+
// src/repositories/organization.repository.ts
|
|
19174
|
+
var import_mongodb24 = require("mongodb");
|
|
19175
|
+
function useOrgRepo() {
|
|
19176
|
+
const db = import_utils58.useAtlas.getDb();
|
|
19177
|
+
if (!db) {
|
|
19178
|
+
throw new Error("Unable to connect to server.");
|
|
19179
|
+
}
|
|
19180
|
+
const collection = db.collection("organizations");
|
|
19181
|
+
async function createIndex() {
|
|
19182
|
+
try {
|
|
19183
|
+
await collection.createIndex([
|
|
19184
|
+
{ name: 1 },
|
|
19185
|
+
{ description: 1 },
|
|
19186
|
+
{ status: 1 }
|
|
19187
|
+
]);
|
|
19188
|
+
} catch (error) {
|
|
19189
|
+
throw new Error("Failed to create index.");
|
|
19190
|
+
}
|
|
19191
|
+
}
|
|
19192
|
+
async function createTextIndex() {
|
|
19193
|
+
try {
|
|
19194
|
+
await collection.createIndex({
|
|
19195
|
+
name: "text",
|
|
19196
|
+
description: "text"
|
|
19197
|
+
});
|
|
19198
|
+
} catch (error) {
|
|
19199
|
+
throw new Error("Failed to create text index on name and description.");
|
|
19200
|
+
}
|
|
19201
|
+
}
|
|
19202
|
+
async function createUniqueIndex() {
|
|
19203
|
+
try {
|
|
19204
|
+
await collection.createIndex(
|
|
19205
|
+
{
|
|
19206
|
+
name: 1
|
|
19207
|
+
},
|
|
19208
|
+
{ unique: true }
|
|
19209
|
+
);
|
|
19210
|
+
} catch (error) {
|
|
19211
|
+
throw new Error("Failed to create unique index on name.");
|
|
19212
|
+
}
|
|
19213
|
+
}
|
|
19214
|
+
async function add(value, session) {
|
|
19215
|
+
try {
|
|
19216
|
+
value = MOrg(value);
|
|
19217
|
+
const res = await collection.insertOne(value, session);
|
|
19218
|
+
return res.insertedId;
|
|
19219
|
+
} catch (error) {
|
|
19220
|
+
import_utils58.logger.log({
|
|
19221
|
+
level: "error",
|
|
19222
|
+
message: error.message
|
|
19223
|
+
});
|
|
19224
|
+
if (error instanceof import_utils58.AppError) {
|
|
19225
|
+
throw error;
|
|
19226
|
+
} else {
|
|
19227
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
19228
|
+
if (isDuplicated) {
|
|
19229
|
+
throw new import_utils58.BadRequestError("Organization already exist.");
|
|
19230
|
+
}
|
|
19231
|
+
throw new Error("Failed to create organization.");
|
|
19232
|
+
}
|
|
19233
|
+
}
|
|
19234
|
+
}
|
|
19235
|
+
async function getOrgs({
|
|
19236
|
+
search = "",
|
|
19237
|
+
page = 1,
|
|
19238
|
+
limit = 10,
|
|
19239
|
+
sort = {},
|
|
19240
|
+
status = "active"
|
|
19241
|
+
} = {}) {
|
|
19242
|
+
page = page > 0 ? page - 1 : 0;
|
|
19243
|
+
const query = { status };
|
|
19244
|
+
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
19245
|
+
if (search) {
|
|
19246
|
+
query.$text = { $search: search };
|
|
19247
|
+
}
|
|
19248
|
+
try {
|
|
19249
|
+
const items = await collection.aggregate([
|
|
19250
|
+
{ $match: query },
|
|
19251
|
+
{ $sort: sort },
|
|
19252
|
+
{ $skip: page * limit },
|
|
19253
|
+
{ $limit: limit },
|
|
19254
|
+
{
|
|
19255
|
+
$project: {
|
|
19256
|
+
_id: 1,
|
|
19257
|
+
name: 1,
|
|
19258
|
+
description: 1,
|
|
19259
|
+
status: 1,
|
|
19260
|
+
createdAt: 1
|
|
19261
|
+
}
|
|
19262
|
+
}
|
|
19263
|
+
]).toArray();
|
|
19264
|
+
const length = await collection.countDocuments(query);
|
|
19265
|
+
return (0, import_utils58.paginate)(items, page, limit, length);
|
|
19266
|
+
} catch (error) {
|
|
19267
|
+
import_utils58.logger.log({ level: "error", message: `${error}` });
|
|
19268
|
+
throw error;
|
|
19269
|
+
}
|
|
19270
|
+
}
|
|
19271
|
+
async function getById(_id) {
|
|
19272
|
+
try {
|
|
19273
|
+
_id = new import_mongodb24.ObjectId(_id);
|
|
19274
|
+
} catch (error) {
|
|
19275
|
+
throw new import_utils58.BadRequestError("Invalid ID.");
|
|
19276
|
+
}
|
|
19277
|
+
try {
|
|
19278
|
+
const result = await collection.findOne({ _id });
|
|
19279
|
+
if (!result) {
|
|
19280
|
+
throw new import_utils58.BadRequestError("Organization not found.");
|
|
19281
|
+
}
|
|
19282
|
+
return result;
|
|
19283
|
+
} catch (error) {
|
|
19284
|
+
if (error instanceof import_utils58.AppError) {
|
|
19285
|
+
throw error;
|
|
19286
|
+
} else {
|
|
19287
|
+
throw new import_utils58.InternalServerError("Failed to get organization.");
|
|
19288
|
+
}
|
|
19289
|
+
}
|
|
19290
|
+
}
|
|
19291
|
+
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
19292
|
+
const allowedFields = ["name", "description"];
|
|
19293
|
+
if (!allowedFields.includes(field)) {
|
|
19294
|
+
throw new import_utils58.BadRequestError(
|
|
19295
|
+
`Field "${field}" is not allowed to be updated.`
|
|
19296
|
+
);
|
|
19297
|
+
}
|
|
19298
|
+
try {
|
|
19299
|
+
_id = new import_mongodb24.ObjectId(_id);
|
|
19300
|
+
} catch (error) {
|
|
19301
|
+
throw new import_utils58.BadRequestError("Invalid ID.");
|
|
19302
|
+
}
|
|
19303
|
+
try {
|
|
19304
|
+
await collection.updateOne(
|
|
19305
|
+
{ _id },
|
|
19306
|
+
{ $set: { [field]: value } },
|
|
19307
|
+
// Dynamically set the field
|
|
19308
|
+
{ session }
|
|
19309
|
+
);
|
|
19310
|
+
return `Successfully updated user ${field}.`;
|
|
19311
|
+
} catch (error) {
|
|
19312
|
+
throw new import_utils58.InternalServerError(`Failed to update organization ${field}.`);
|
|
19313
|
+
}
|
|
19314
|
+
}
|
|
19315
|
+
async function deleteById(_id) {
|
|
19316
|
+
try {
|
|
19317
|
+
_id = new import_mongodb24.ObjectId(_id);
|
|
19318
|
+
} catch (error) {
|
|
19319
|
+
throw new import_utils58.BadRequestError("Invalid ID.");
|
|
19320
|
+
}
|
|
19321
|
+
try {
|
|
19322
|
+
await collection.updateOne(
|
|
19323
|
+
{ _id },
|
|
19324
|
+
{ $set: { status: "deleted", deletedAt: (/* @__PURE__ */ new Date()).toISOString() } }
|
|
19325
|
+
);
|
|
19326
|
+
return "Successfully deleted organization.";
|
|
19327
|
+
} catch (error) {
|
|
19328
|
+
throw new import_utils58.InternalServerError("Failed to delete organization.");
|
|
19329
|
+
}
|
|
19330
|
+
}
|
|
19331
|
+
return {
|
|
19332
|
+
createIndex,
|
|
19333
|
+
createTextIndex,
|
|
19334
|
+
createUniqueIndex,
|
|
19335
|
+
add,
|
|
19336
|
+
getOrgs,
|
|
19337
|
+
getById,
|
|
19338
|
+
updateFieldById,
|
|
19339
|
+
deleteById
|
|
19340
|
+
};
|
|
19341
|
+
}
|
|
19342
|
+
|
|
19343
|
+
// src/repositories/address.repository.ts
|
|
19344
|
+
var import_utils60 = require("@goweekdays/utils");
|
|
19345
|
+
|
|
19346
|
+
// src/models/address.model.ts
|
|
19347
|
+
var import_utils59 = require("@goweekdays/utils");
|
|
19348
|
+
var import_mongodb25 = require("mongodb");
|
|
19349
|
+
function MAddress(value) {
|
|
19350
|
+
if (value.user) {
|
|
19351
|
+
try {
|
|
19352
|
+
value.user = new import_mongodb25.ObjectId(value.user);
|
|
19353
|
+
} catch (error) {
|
|
19354
|
+
throw new import_utils59.BadRequestError("Invalid user ID.");
|
|
19355
|
+
}
|
|
19356
|
+
}
|
|
19357
|
+
if (value.org) {
|
|
19358
|
+
try {
|
|
19359
|
+
value.org = new import_mongodb25.ObjectId(value.org);
|
|
19360
|
+
} catch (error) {
|
|
19361
|
+
throw new import_utils59.BadRequestError("Invalid org ID.");
|
|
19362
|
+
}
|
|
19363
|
+
}
|
|
19364
|
+
return {
|
|
19365
|
+
type: value.type ?? "billing",
|
|
19366
|
+
user: value.user ?? "",
|
|
19367
|
+
org: value.org ?? "",
|
|
19368
|
+
country: value.country ?? "",
|
|
19369
|
+
address: value.address ?? "",
|
|
19370
|
+
continuedAddress: value.continuedAddress ?? "",
|
|
19371
|
+
city: value.city ?? "",
|
|
19372
|
+
province: value.province ?? "",
|
|
19373
|
+
postalCode: value.postalCode ?? "",
|
|
19374
|
+
taxId: value.taxId ?? ""
|
|
19375
|
+
};
|
|
19376
|
+
}
|
|
19377
|
+
|
|
19378
|
+
// src/repositories/address.repository.ts
|
|
19379
|
+
var import_mongodb26 = require("mongodb");
|
|
19380
|
+
function useAddressRepo() {
|
|
19381
|
+
const db = import_utils60.useAtlas.getDb();
|
|
19382
|
+
if (!db) {
|
|
19383
|
+
throw new import_utils60.BadRequestError("Unable to connect to server.");
|
|
19384
|
+
}
|
|
19385
|
+
const collection = db.collection("addresses");
|
|
19386
|
+
async function createIndex() {
|
|
19387
|
+
try {
|
|
19388
|
+
await collection.createIndex([{ user: 1 }, { type: 1 }]);
|
|
19389
|
+
} catch (error) {
|
|
19390
|
+
throw new import_utils60.BadRequestError("Failed to create index on address.");
|
|
19391
|
+
}
|
|
19392
|
+
}
|
|
19393
|
+
async function add(value, session) {
|
|
19394
|
+
try {
|
|
19395
|
+
value = MAddress(value);
|
|
19396
|
+
const res = await collection.insertOne(value, { session });
|
|
19397
|
+
return res.insertedId;
|
|
19398
|
+
} catch (error) {
|
|
19399
|
+
throw new import_utils60.BadRequestError("Failed to create address.");
|
|
19400
|
+
}
|
|
19401
|
+
}
|
|
19402
|
+
async function getByUserId(user) {
|
|
19403
|
+
try {
|
|
19404
|
+
user = new import_mongodb26.ObjectId(user);
|
|
19405
|
+
} catch (error) {
|
|
19406
|
+
throw new import_utils60.BadRequestError("Invalid user ID.");
|
|
19407
|
+
}
|
|
19408
|
+
try {
|
|
19409
|
+
return await collection.findOne({ user });
|
|
19410
|
+
} catch (error) {
|
|
19411
|
+
throw new import_utils60.BadRequestError("Failed to get address by ID.");
|
|
19412
|
+
}
|
|
19413
|
+
}
|
|
19414
|
+
return {
|
|
19415
|
+
createIndex,
|
|
19416
|
+
add,
|
|
19417
|
+
getByUserId
|
|
19418
|
+
};
|
|
19419
|
+
}
|
|
19420
|
+
|
|
19421
|
+
// src/services/subscription.service.ts
|
|
19422
|
+
function useSubscriptionService() {
|
|
19423
|
+
const { getByUserId: _getByUserId, add } = useSubscriptionRepo();
|
|
19424
|
+
const { getUserById: _getUserByUserId } = useUserRepo();
|
|
19425
|
+
const { initSubscription, checkSubscriptionStatus, cancelSubscription } = useXenditService();
|
|
19426
|
+
const { add: addPaymentMethod, getByPaymentMethodId } = usePaymentMethodRepo();
|
|
19427
|
+
const { add: addOrg } = useOrgRepo();
|
|
19428
|
+
const { add: addAddress } = useAddressRepo();
|
|
19429
|
+
async function createOrgSubscription(value) {
|
|
19430
|
+
const session = import_utils61.useAtlas.getClient()?.startSession();
|
|
19431
|
+
session?.startTransaction();
|
|
19432
|
+
try {
|
|
19433
|
+
const _user = await _getUserByUserId(value.user);
|
|
19434
|
+
if (!_user) {
|
|
19435
|
+
throw new import_utils61.BadRequestError("User not found.");
|
|
19436
|
+
}
|
|
19437
|
+
const payment = await getByPaymentMethodId(value.payment_method_id);
|
|
19438
|
+
if (!payment) {
|
|
19439
|
+
throw new import_utils61.BadRequestError("Payment method not found.");
|
|
19440
|
+
}
|
|
19441
|
+
const org = await addOrg(value.organization, session);
|
|
19442
|
+
payment.user = "";
|
|
19443
|
+
payment.org = org;
|
|
19444
|
+
delete payment._id;
|
|
19445
|
+
console.log(payment);
|
|
19446
|
+
await addPaymentMethod(payment, session);
|
|
19447
|
+
value.billingAddress.org = org;
|
|
19448
|
+
await addAddress(value.billingAddress, session);
|
|
19449
|
+
const subscription = await initSubscription({
|
|
19450
|
+
customer: value.customer_id,
|
|
19451
|
+
paymentMethod: value.payment_method_id,
|
|
19452
|
+
amount: value.amount,
|
|
19453
|
+
description: "GoWeekdays Organization Monthly Subscription."
|
|
19454
|
+
});
|
|
19455
|
+
await add(
|
|
19456
|
+
{
|
|
19457
|
+
org,
|
|
19458
|
+
subscriptionId: subscription.id
|
|
19459
|
+
},
|
|
19460
|
+
session
|
|
19461
|
+
);
|
|
19462
|
+
await session?.commitTransaction();
|
|
19463
|
+
return "Subscription created successfully.";
|
|
19464
|
+
} catch (error) {
|
|
19465
|
+
await session?.abortTransaction();
|
|
19466
|
+
throw error;
|
|
19467
|
+
} finally {
|
|
19468
|
+
session?.endSession();
|
|
19469
|
+
}
|
|
19470
|
+
}
|
|
19471
|
+
async function createAffiliateSubscription({
|
|
19472
|
+
user = "",
|
|
19473
|
+
amount = 0,
|
|
19474
|
+
payment_method_id = "",
|
|
19475
|
+
customer_id = ""
|
|
19476
|
+
} = {}) {
|
|
19477
|
+
const session = import_utils61.useAtlas.getClient()?.startSession();
|
|
19478
|
+
session?.startTransaction();
|
|
19479
|
+
try {
|
|
19480
|
+
const _user = await _getUserByUserId(user);
|
|
19481
|
+
if (!_user) {
|
|
19482
|
+
throw new import_utils61.BadRequestError("User not found.");
|
|
19483
|
+
}
|
|
19484
|
+
const subscription = await initSubscription({
|
|
19485
|
+
customer: customer_id,
|
|
19486
|
+
paymentMethod: payment_method_id,
|
|
19487
|
+
amount,
|
|
19488
|
+
description: "GoWeekdays Affiliate Annual Subscription.",
|
|
19489
|
+
interval: "YEAR"
|
|
19490
|
+
});
|
|
19491
|
+
await add(
|
|
19492
|
+
{
|
|
19493
|
+
user,
|
|
19494
|
+
subscriptionId: subscription.id
|
|
19495
|
+
},
|
|
19496
|
+
session
|
|
19497
|
+
);
|
|
19498
|
+
await session?.commitTransaction();
|
|
19499
|
+
return "Subscription created successfully.";
|
|
19500
|
+
} catch (error) {
|
|
19501
|
+
await session?.abortTransaction();
|
|
19502
|
+
throw error;
|
|
19503
|
+
} finally {
|
|
19504
|
+
session?.endSession();
|
|
19505
|
+
}
|
|
19506
|
+
}
|
|
19507
|
+
async function getByUserId(id) {
|
|
19508
|
+
try {
|
|
19509
|
+
const subscription = await _getByUserId(id);
|
|
19510
|
+
if (!subscription) {
|
|
19511
|
+
throw new import_utils61.BadRequestError("Subscription not found.");
|
|
19512
|
+
}
|
|
19513
|
+
return subscription;
|
|
19514
|
+
} catch (error) {
|
|
19515
|
+
throw error;
|
|
19516
|
+
}
|
|
19517
|
+
}
|
|
19518
|
+
async function getStatusByUser(user) {
|
|
19519
|
+
try {
|
|
19520
|
+
const userSubscription = await getByUserId(user);
|
|
19521
|
+
const status = await checkSubscriptionStatus(
|
|
19522
|
+
userSubscription.subscriptionId
|
|
19523
|
+
);
|
|
19524
|
+
return status;
|
|
19525
|
+
} catch (error) {
|
|
19526
|
+
throw new import_utils61.BadRequestError("Failed to fetch subscription status");
|
|
19527
|
+
}
|
|
19528
|
+
}
|
|
19529
|
+
return {
|
|
19530
|
+
getByUserId,
|
|
19531
|
+
getStatusByUser,
|
|
19532
|
+
createAffiliateSubscription,
|
|
19533
|
+
createOrgSubscription
|
|
19534
|
+
};
|
|
19535
|
+
}
|
|
19536
|
+
|
|
19537
|
+
// src/controllers/subscription.controller.ts
|
|
19538
|
+
var import_joi11 = __toESM(require("joi"));
|
|
19539
|
+
var import_utils62 = require("@goweekdays/utils");
|
|
19540
|
+
function useSubscriptionController() {
|
|
19541
|
+
const { add: _add, getSubscriptions: _getSubscriptions } = useSubscriptionRepo();
|
|
19542
|
+
const {
|
|
19543
|
+
getByUserId: _getByUserId,
|
|
19544
|
+
getStatusByUser: _getStatusByUser,
|
|
19545
|
+
createAffiliateSubscription: _createAffiliateSubscription,
|
|
19546
|
+
createOrgSubscription: _createOrgSubscription
|
|
19547
|
+
} = useSubscriptionService();
|
|
19548
|
+
async function add(req, res, next) {
|
|
19549
|
+
const value = req.body;
|
|
19550
|
+
const validation = import_joi11.default.object({
|
|
19551
|
+
user: import_joi11.default.string().required(),
|
|
19552
|
+
subscriptionId: import_joi11.default.string().required()
|
|
19553
|
+
});
|
|
19554
|
+
const { error } = validation.validate(value);
|
|
19555
|
+
if (error) {
|
|
19556
|
+
next(new import_utils62.BadRequestError(error.message));
|
|
19557
|
+
}
|
|
19558
|
+
try {
|
|
19559
|
+
const value2 = req.body;
|
|
19560
|
+
const id = await _add(value2);
|
|
19561
|
+
res.json({ message: "Successfully added subscription.", id });
|
|
19562
|
+
return;
|
|
19563
|
+
} catch (error2) {
|
|
19564
|
+
next(error2);
|
|
19565
|
+
}
|
|
19566
|
+
}
|
|
19567
|
+
async function getByUserId(req, res, next) {
|
|
19568
|
+
const id = req.params.id;
|
|
19569
|
+
const validation = import_joi11.default.string().required();
|
|
19570
|
+
const { error } = validation.validate(id);
|
|
19571
|
+
if (error) {
|
|
19572
|
+
next(new import_utils62.BadRequestError(error.message));
|
|
19573
|
+
}
|
|
19574
|
+
try {
|
|
19575
|
+
const subscription = await _getByUserId(id);
|
|
19576
|
+
res.json(subscription);
|
|
19577
|
+
return;
|
|
19578
|
+
} catch (error2) {
|
|
19579
|
+
next(error2);
|
|
19580
|
+
}
|
|
19581
|
+
}
|
|
19582
|
+
async function getSubscriptions(req, res, next) {
|
|
19583
|
+
const status = req.query.status ?? "";
|
|
19584
|
+
const search = req.query.search ?? "";
|
|
19585
|
+
const page = Number(req.query.page) ?? 1;
|
|
19586
|
+
const validation = import_joi11.default.object({
|
|
19587
|
+
status: import_joi11.default.string().required(),
|
|
19588
|
+
search: import_joi11.default.string().optional().allow("", null),
|
|
19589
|
+
page: import_joi11.default.number().required()
|
|
19590
|
+
});
|
|
19591
|
+
const { error } = validation.validate({ status, search, page });
|
|
19592
|
+
if (error) {
|
|
19593
|
+
next(new import_utils62.BadRequestError(error.message));
|
|
19594
|
+
}
|
|
19595
|
+
try {
|
|
19596
|
+
const subscriptions = await _getSubscriptions({ status, search, page });
|
|
19597
|
+
res.json(subscriptions);
|
|
19598
|
+
return;
|
|
19599
|
+
} catch (error2) {
|
|
19600
|
+
next(error2);
|
|
18805
19601
|
}
|
|
18806
19602
|
}
|
|
18807
19603
|
async function getSubscriptionStatus(req, res, next) {
|
|
18808
19604
|
const id = req.params.id;
|
|
18809
|
-
const validation =
|
|
19605
|
+
const validation = import_joi11.default.string().required();
|
|
18810
19606
|
const { error } = validation.validate(id);
|
|
18811
19607
|
if (error) {
|
|
18812
|
-
next(new
|
|
19608
|
+
next(new import_utils62.BadRequestError(error.message));
|
|
19609
|
+
}
|
|
19610
|
+
try {
|
|
19611
|
+
const status = await _getStatusByUser(id);
|
|
19612
|
+
res.json({ status });
|
|
19613
|
+
return;
|
|
19614
|
+
} catch (error2) {
|
|
19615
|
+
next(error2);
|
|
19616
|
+
}
|
|
19617
|
+
}
|
|
19618
|
+
async function createAffiliateSubscription(req, res, next) {
|
|
19619
|
+
const amount = req.body.amount ?? 0;
|
|
19620
|
+
const payment_method_id = req.body.payment_method_id ?? "";
|
|
19621
|
+
const customer_id = req.body.customer_id ?? "";
|
|
19622
|
+
const currency = req.body.currency ?? "PHP";
|
|
19623
|
+
const user = req.headers["user"];
|
|
19624
|
+
const validation = import_joi11.default.object({
|
|
19625
|
+
user: import_joi11.default.string().required(),
|
|
19626
|
+
amount: import_joi11.default.number().required(),
|
|
19627
|
+
customer_id: import_joi11.default.string().required(),
|
|
19628
|
+
payment_method_id: import_joi11.default.string().required(),
|
|
19629
|
+
currency: import_joi11.default.string().optional().allow("", null)
|
|
19630
|
+
});
|
|
19631
|
+
const { error } = validation.validate({
|
|
19632
|
+
user,
|
|
19633
|
+
amount,
|
|
19634
|
+
customer_id,
|
|
19635
|
+
payment_method_id,
|
|
19636
|
+
currency
|
|
19637
|
+
});
|
|
19638
|
+
if (error) {
|
|
19639
|
+
next(new import_utils62.BadRequestError(error.message));
|
|
19640
|
+
return;
|
|
19641
|
+
}
|
|
19642
|
+
try {
|
|
19643
|
+
const id = await _createAffiliateSubscription({
|
|
19644
|
+
user,
|
|
19645
|
+
amount,
|
|
19646
|
+
customer_id,
|
|
19647
|
+
payment_method_id
|
|
19648
|
+
});
|
|
19649
|
+
res.json({ message: "Successfully added subscription.", id });
|
|
19650
|
+
return;
|
|
19651
|
+
} catch (error2) {
|
|
19652
|
+
console.log(error2);
|
|
19653
|
+
next(error2);
|
|
19654
|
+
return;
|
|
19655
|
+
}
|
|
19656
|
+
}
|
|
19657
|
+
async function createOrgSubscription(req, res, next) {
|
|
19658
|
+
const value = req.body;
|
|
19659
|
+
value.user = req.headers["user"];
|
|
19660
|
+
const validation = import_joi11.default.object({
|
|
19661
|
+
user: import_joi11.default.string().required(),
|
|
19662
|
+
amount: import_joi11.default.number().required(),
|
|
19663
|
+
customer_id: import_joi11.default.string().required(),
|
|
19664
|
+
payment_method_id: import_joi11.default.string().required(),
|
|
19665
|
+
currency: import_joi11.default.string().optional().allow("", null),
|
|
19666
|
+
organization: import_joi11.default.object({
|
|
19667
|
+
name: import_joi11.default.string().required(),
|
|
19668
|
+
description: import_joi11.default.string().optional().allow("", null),
|
|
19669
|
+
type: import_joi11.default.string().allow("personal", "business").required(),
|
|
19670
|
+
email: import_joi11.default.string().email().required(),
|
|
19671
|
+
contact: import_joi11.default.string().required(),
|
|
19672
|
+
busInst: import_joi11.default.string().optional().allow(null, "")
|
|
19673
|
+
}).required(),
|
|
19674
|
+
billingAddress: import_joi11.default.object({
|
|
19675
|
+
type: import_joi11.default.string().required(),
|
|
19676
|
+
country: import_joi11.default.string().required(),
|
|
19677
|
+
address: import_joi11.default.string().required(),
|
|
19678
|
+
continuedAddress: import_joi11.default.string().optional().allow(null, ""),
|
|
19679
|
+
city: import_joi11.default.string().required(),
|
|
19680
|
+
province: import_joi11.default.string().required(),
|
|
19681
|
+
postalCode: import_joi11.default.string().required(),
|
|
19682
|
+
taxId: import_joi11.default.string().optional().allow(null, "")
|
|
19683
|
+
}).required()
|
|
19684
|
+
});
|
|
19685
|
+
const { error } = validation.validate(value);
|
|
19686
|
+
if (error) {
|
|
19687
|
+
next(new import_utils62.BadRequestError(error.message));
|
|
19688
|
+
return;
|
|
19689
|
+
}
|
|
19690
|
+
try {
|
|
19691
|
+
const id = await _createOrgSubscription(value);
|
|
19692
|
+
res.json({ message: "Successfully added subscription.", id });
|
|
19693
|
+
return;
|
|
19694
|
+
} catch (error2) {
|
|
19695
|
+
next(error2);
|
|
19696
|
+
return;
|
|
19697
|
+
}
|
|
19698
|
+
}
|
|
19699
|
+
return {
|
|
19700
|
+
add,
|
|
19701
|
+
getByUserId,
|
|
19702
|
+
getSubscriptions,
|
|
19703
|
+
getSubscriptionStatus,
|
|
19704
|
+
createAffiliateSubscription,
|
|
19705
|
+
createOrgSubscription
|
|
19706
|
+
};
|
|
19707
|
+
}
|
|
19708
|
+
|
|
19709
|
+
// src/services/payment-method.service.ts
|
|
19710
|
+
var import_utils63 = require("@goweekdays/utils");
|
|
19711
|
+
function usePaymentMethodService() {
|
|
19712
|
+
const { createCustomer, linkPaymentMethodEWallet, linkPaymentMethodCard } = useXenditService();
|
|
19713
|
+
const { getUserById } = useUserRepo();
|
|
19714
|
+
const { add } = usePaymentMethodRepo();
|
|
19715
|
+
const cardRules = {
|
|
19716
|
+
Visa: { length: 16, visibleDigits: 4 },
|
|
19717
|
+
MasterCard: { length: 16, visibleDigits: 4 },
|
|
19718
|
+
"American Express": { length: 15, visibleDigits: 5 },
|
|
19719
|
+
Discover: { length: 16, visibleDigits: 4 },
|
|
19720
|
+
"Diners Club": { length: 14, visibleDigits: 4 },
|
|
19721
|
+
JCB: { length: 16, visibleDigits: 4 },
|
|
19722
|
+
UnionPay: { length: 19, visibleDigits: 4 },
|
|
19723
|
+
Maestro: { length: 19, visibleDigits: 4 }
|
|
19724
|
+
};
|
|
19725
|
+
const detectCardType = (cardNumber) => {
|
|
19726
|
+
const patterns = {
|
|
19727
|
+
Visa: /^4/,
|
|
19728
|
+
MasterCard: /^(5[1-5]|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)/,
|
|
19729
|
+
"American Express": /^3[47]/,
|
|
19730
|
+
Discover: /^(6011|622(12[6-9]|1[3-9]\d|[2-8]\d\d|92[0-5])|64[4-9]|65)/,
|
|
19731
|
+
"Diners Club": /^3(0[0-5]|[68])/,
|
|
19732
|
+
JCB: /^35/,
|
|
19733
|
+
UnionPay: /^62/,
|
|
19734
|
+
Maestro: /^(50|5[6-9]|6\d)/
|
|
19735
|
+
};
|
|
19736
|
+
for (const [type, regex] of Object.entries(patterns)) {
|
|
19737
|
+
if (regex.test(cardNumber))
|
|
19738
|
+
return type;
|
|
19739
|
+
}
|
|
19740
|
+
return "Unknown";
|
|
19741
|
+
};
|
|
19742
|
+
const maskCardNumber = (cardNumber) => {
|
|
19743
|
+
const cardType = detectCardType(cardNumber);
|
|
19744
|
+
const rule = cardRules[cardType] || { visibleDigits: 4 };
|
|
19745
|
+
const visibleDigits = rule.visibleDigits;
|
|
19746
|
+
const maskLength = cardNumber.length - visibleDigits;
|
|
19747
|
+
if (maskLength <= 0)
|
|
19748
|
+
return cardNumber;
|
|
19749
|
+
const maskedPart = "*".repeat(maskLength);
|
|
19750
|
+
const visiblePart = cardNumber.slice(maskLength);
|
|
19751
|
+
return `${maskedPart}${visiblePart}`;
|
|
19752
|
+
};
|
|
19753
|
+
async function linkEWallet({
|
|
19754
|
+
user = "",
|
|
19755
|
+
mobile_number = "",
|
|
19756
|
+
type = "GCASH",
|
|
19757
|
+
success_return_url = "",
|
|
19758
|
+
failure_return_url = "",
|
|
19759
|
+
cancel_return_url = ""
|
|
19760
|
+
} = {}) {
|
|
19761
|
+
const session = import_utils63.useAtlas.getClient()?.startSession();
|
|
19762
|
+
session?.startTransaction();
|
|
19763
|
+
try {
|
|
19764
|
+
const _user = await getUserById(user);
|
|
19765
|
+
if (!_user) {
|
|
19766
|
+
throw new import_utils63.BadRequestError("User not found.");
|
|
19767
|
+
}
|
|
19768
|
+
if (!_user._id) {
|
|
19769
|
+
throw new import_utils63.BadRequestError("Invalid user ID.");
|
|
19770
|
+
}
|
|
19771
|
+
const customer = await createCustomer({
|
|
19772
|
+
mobile_number,
|
|
19773
|
+
email: _user.email,
|
|
19774
|
+
given_names: _user.firstName,
|
|
19775
|
+
surname: _user.lastName
|
|
19776
|
+
});
|
|
19777
|
+
const result = await linkPaymentMethodEWallet({
|
|
19778
|
+
customerId: customer.id,
|
|
19779
|
+
type,
|
|
19780
|
+
success_return_url,
|
|
19781
|
+
failure_return_url,
|
|
19782
|
+
cancel_return_url
|
|
19783
|
+
});
|
|
19784
|
+
await add(
|
|
19785
|
+
{
|
|
19786
|
+
user: _user._id,
|
|
19787
|
+
type,
|
|
19788
|
+
status: "active",
|
|
19789
|
+
paymentId: result.id,
|
|
19790
|
+
customerId: customer.id,
|
|
19791
|
+
name: result.type,
|
|
19792
|
+
number: mobile_number
|
|
19793
|
+
},
|
|
19794
|
+
session
|
|
19795
|
+
);
|
|
19796
|
+
await session?.commitTransaction();
|
|
19797
|
+
return result;
|
|
19798
|
+
} catch (error) {
|
|
19799
|
+
console.log(error);
|
|
19800
|
+
await session?.abortTransaction();
|
|
19801
|
+
throw error;
|
|
19802
|
+
} finally {
|
|
19803
|
+
session?.endSession();
|
|
19804
|
+
}
|
|
19805
|
+
}
|
|
19806
|
+
async function linkCard({
|
|
19807
|
+
user = "",
|
|
19808
|
+
type = "",
|
|
19809
|
+
success_return_url = "",
|
|
19810
|
+
failure_return_url = "",
|
|
19811
|
+
card_number = "",
|
|
19812
|
+
expiry_month = "",
|
|
19813
|
+
expiry_year = "",
|
|
19814
|
+
cvv = "",
|
|
19815
|
+
cardholder_name = "",
|
|
19816
|
+
currency = "PHP"
|
|
19817
|
+
} = {}) {
|
|
19818
|
+
const session = import_utils63.useAtlas.getClient()?.startSession();
|
|
19819
|
+
session?.startTransaction();
|
|
19820
|
+
try {
|
|
19821
|
+
const _user = await getUserById(user);
|
|
19822
|
+
if (!_user) {
|
|
19823
|
+
throw new import_utils63.BadRequestError("User not found.");
|
|
19824
|
+
}
|
|
19825
|
+
if (!_user._id) {
|
|
19826
|
+
throw new import_utils63.BadRequestError("Invalid user ID.");
|
|
19827
|
+
}
|
|
19828
|
+
const result = await linkPaymentMethodCard({
|
|
19829
|
+
card_number,
|
|
19830
|
+
expiry_month,
|
|
19831
|
+
expiry_year,
|
|
19832
|
+
cvv,
|
|
19833
|
+
cardholder_name,
|
|
19834
|
+
currency,
|
|
19835
|
+
success_return_url,
|
|
19836
|
+
failure_return_url
|
|
19837
|
+
});
|
|
19838
|
+
await add(
|
|
19839
|
+
{
|
|
19840
|
+
user: _user._id,
|
|
19841
|
+
type,
|
|
19842
|
+
status: "active",
|
|
19843
|
+
paymentId: result.id,
|
|
19844
|
+
name: result.type,
|
|
19845
|
+
number: maskCardNumber(card_number)
|
|
19846
|
+
},
|
|
19847
|
+
session
|
|
19848
|
+
);
|
|
19849
|
+
await session?.commitTransaction();
|
|
19850
|
+
return result;
|
|
19851
|
+
} catch (error) {
|
|
19852
|
+
console.log(error);
|
|
19853
|
+
await session?.abortTransaction();
|
|
19854
|
+
throw error;
|
|
19855
|
+
} finally {
|
|
19856
|
+
session?.endSession();
|
|
19857
|
+
}
|
|
19858
|
+
}
|
|
19859
|
+
return {
|
|
19860
|
+
linkEWallet,
|
|
19861
|
+
linkCard
|
|
19862
|
+
};
|
|
19863
|
+
}
|
|
19864
|
+
|
|
19865
|
+
// src/controllers/payment-method.controller.ts
|
|
19866
|
+
var import_joi12 = __toESM(require("joi"));
|
|
19867
|
+
var import_utils64 = require("@goweekdays/utils");
|
|
19868
|
+
function usePaymentMethodController() {
|
|
19869
|
+
const { linkEWallet } = usePaymentMethodService();
|
|
19870
|
+
async function linkEWalletController(req, res, next) {
|
|
19871
|
+
const mobile_number = req.body.mobile_number ?? "";
|
|
19872
|
+
const type = req.body.type ?? "";
|
|
19873
|
+
const user = req.headers["user"] ?? "";
|
|
19874
|
+
const success_return_url = req.body.success_return_url ?? "";
|
|
19875
|
+
const failure_return_url = req.body.failure_return_url ?? "";
|
|
19876
|
+
const cancel_return_url = req.body.cancel_return_url ?? "";
|
|
19877
|
+
const validation = import_joi12.default.object({
|
|
19878
|
+
user: import_joi12.default.string().hex().required(),
|
|
19879
|
+
mobile_number: import_joi12.default.string().required(),
|
|
19880
|
+
type: import_joi12.default.string().valid("GCASH", "PAYMAYA").required(),
|
|
19881
|
+
success_return_url: import_joi12.default.string().uri().required(),
|
|
19882
|
+
failure_return_url: import_joi12.default.string().uri().required(),
|
|
19883
|
+
cancel_return_url: import_joi12.default.string().uri().required()
|
|
19884
|
+
});
|
|
19885
|
+
const { error } = validation.validate({
|
|
19886
|
+
user,
|
|
19887
|
+
mobile_number,
|
|
19888
|
+
type,
|
|
19889
|
+
success_return_url,
|
|
19890
|
+
failure_return_url,
|
|
19891
|
+
cancel_return_url
|
|
19892
|
+
});
|
|
19893
|
+
if (error) {
|
|
19894
|
+
next(new import_utils64.BadRequestError(error.message));
|
|
18813
19895
|
}
|
|
18814
19896
|
try {
|
|
18815
|
-
const
|
|
18816
|
-
|
|
19897
|
+
const result = await linkEWallet({
|
|
19898
|
+
user,
|
|
19899
|
+
mobile_number,
|
|
19900
|
+
type,
|
|
19901
|
+
success_return_url,
|
|
19902
|
+
failure_return_url,
|
|
19903
|
+
cancel_return_url
|
|
19904
|
+
});
|
|
19905
|
+
res.json(result);
|
|
18817
19906
|
return;
|
|
18818
19907
|
} catch (error2) {
|
|
18819
19908
|
next(error2);
|
|
18820
19909
|
}
|
|
18821
19910
|
}
|
|
18822
|
-
|
|
18823
|
-
|
|
18824
|
-
const
|
|
18825
|
-
|
|
19911
|
+
const { getByUser: _getByUser } = usePaymentMethodRepo();
|
|
19912
|
+
async function getByUser(req, res, next) {
|
|
19913
|
+
const user = req.headers["user"] ?? "";
|
|
19914
|
+
try {
|
|
19915
|
+
const result = await _getByUser(user);
|
|
19916
|
+
res.json(result);
|
|
19917
|
+
} catch (error) {
|
|
19918
|
+
throw error;
|
|
19919
|
+
}
|
|
19920
|
+
}
|
|
19921
|
+
return {
|
|
19922
|
+
linkEWalletController,
|
|
19923
|
+
getByUser
|
|
19924
|
+
};
|
|
19925
|
+
}
|
|
19926
|
+
|
|
19927
|
+
// src/controllers/address.controller.ts
|
|
19928
|
+
var import_utils65 = require("@goweekdays/utils");
|
|
19929
|
+
var import_joi13 = __toESM(require("joi"));
|
|
19930
|
+
function useAddressController() {
|
|
19931
|
+
const { add: _add, getByUserId: _getByUserId } = useAddressRepo();
|
|
19932
|
+
async function add(req, res, next) {
|
|
19933
|
+
const value = req.body;
|
|
19934
|
+
const validation = import_joi13.default.object({
|
|
19935
|
+
type: import_joi13.default.string().required(),
|
|
19936
|
+
user: import_joi13.default.string().hex().optional().allow("", null),
|
|
19937
|
+
org: import_joi13.default.string().hex().optional().allow("", null),
|
|
19938
|
+
country: import_joi13.default.string().required(),
|
|
19939
|
+
address: import_joi13.default.string().required(),
|
|
19940
|
+
continuedAddress: import_joi13.default.string().optional().allow("", null),
|
|
19941
|
+
city: import_joi13.default.string().required(),
|
|
19942
|
+
province: import_joi13.default.string().required(),
|
|
19943
|
+
postalCode: import_joi13.default.string().required(),
|
|
19944
|
+
taxId: import_joi13.default.string().optional().allow("", null)
|
|
19945
|
+
});
|
|
19946
|
+
const { error } = validation.validate(value);
|
|
19947
|
+
if (error) {
|
|
19948
|
+
next(new import_utils65.BadRequestError(error.message));
|
|
19949
|
+
}
|
|
19950
|
+
try {
|
|
19951
|
+
const value2 = req.body;
|
|
19952
|
+
const id = await _add(value2);
|
|
19953
|
+
res.json({ message: "Successfully added subscription.", id });
|
|
19954
|
+
return;
|
|
19955
|
+
} catch (error2) {
|
|
19956
|
+
next(error2);
|
|
19957
|
+
}
|
|
19958
|
+
}
|
|
19959
|
+
async function getByUserId(req, res, next) {
|
|
19960
|
+
const user = req.params.user;
|
|
19961
|
+
const validation = import_joi13.default.string().hex().required();
|
|
19962
|
+
const { error } = validation.validate(user);
|
|
18826
19963
|
if (error) {
|
|
18827
|
-
next(new
|
|
19964
|
+
next(new import_utils65.BadRequestError(error.message));
|
|
18828
19965
|
}
|
|
18829
19966
|
try {
|
|
18830
|
-
await
|
|
18831
|
-
|
|
19967
|
+
const address = await _getByUserId(user);
|
|
19968
|
+
if (!address) {
|
|
19969
|
+
next(new import_utils65.NotFoundError("Address not found."));
|
|
19970
|
+
return;
|
|
19971
|
+
}
|
|
19972
|
+
res.json(address);
|
|
18832
19973
|
return;
|
|
18833
19974
|
} catch (error2) {
|
|
18834
19975
|
next(error2);
|
|
@@ -18836,10 +19977,339 @@ function useSubscriptionController() {
|
|
|
18836
19977
|
}
|
|
18837
19978
|
return {
|
|
18838
19979
|
add,
|
|
18839
|
-
getByUserId
|
|
18840
|
-
|
|
18841
|
-
|
|
18842
|
-
|
|
19980
|
+
getByUserId
|
|
19981
|
+
};
|
|
19982
|
+
}
|
|
19983
|
+
|
|
19984
|
+
// src/services/organization.service.ts
|
|
19985
|
+
var import_utils68 = require("@goweekdays/utils");
|
|
19986
|
+
|
|
19987
|
+
// src/repositories/member.repository.ts
|
|
19988
|
+
var import_utils67 = require("@goweekdays/utils");
|
|
19989
|
+
|
|
19990
|
+
// src/models/member.model.ts
|
|
19991
|
+
var import_utils66 = require("@goweekdays/utils");
|
|
19992
|
+
var import_joi14 = __toESM(require("joi"));
|
|
19993
|
+
var import_mongodb27 = require("mongodb");
|
|
19994
|
+
function MMember(value) {
|
|
19995
|
+
const schema = import_joi14.default.object({
|
|
19996
|
+
_id: import_joi14.default.string().hex().optional().allow("", null),
|
|
19997
|
+
org: import_joi14.default.string().hex().required(),
|
|
19998
|
+
name: import_joi14.default.string().required(),
|
|
19999
|
+
user: import_joi14.default.string().hex().required(),
|
|
20000
|
+
role: import_joi14.default.string().hex().required(),
|
|
20001
|
+
status: import_joi14.default.string().optional().allow("", null),
|
|
20002
|
+
createdAt: import_joi14.default.string().optional().allow("", null),
|
|
20003
|
+
updatedAt: import_joi14.default.string().optional().allow("", null),
|
|
20004
|
+
deletedAt: import_joi14.default.string().optional().allow("", null)
|
|
20005
|
+
});
|
|
20006
|
+
const { error } = schema.validate(value);
|
|
20007
|
+
if (error) {
|
|
20008
|
+
throw new import_utils66.BadRequestError(error.message);
|
|
20009
|
+
}
|
|
20010
|
+
if (value.org) {
|
|
20011
|
+
try {
|
|
20012
|
+
value.org = new import_mongodb27.ObjectId(value.org);
|
|
20013
|
+
} catch (error2) {
|
|
20014
|
+
throw new import_utils66.BadRequestError("Invalid org ID.");
|
|
20015
|
+
}
|
|
20016
|
+
}
|
|
20017
|
+
if (value.user) {
|
|
20018
|
+
try {
|
|
20019
|
+
value.user = new import_mongodb27.ObjectId(value.user);
|
|
20020
|
+
} catch (error2) {
|
|
20021
|
+
throw new import_utils66.BadRequestError("Invalid user ID.");
|
|
20022
|
+
}
|
|
20023
|
+
}
|
|
20024
|
+
if (value.role) {
|
|
20025
|
+
try {
|
|
20026
|
+
value.role = new import_mongodb27.ObjectId(value.role);
|
|
20027
|
+
} catch (error2) {
|
|
20028
|
+
throw new import_utils66.BadRequestError("Invalid role ID.");
|
|
20029
|
+
}
|
|
20030
|
+
}
|
|
20031
|
+
return {
|
|
20032
|
+
_id: value._id,
|
|
20033
|
+
org: value.org,
|
|
20034
|
+
name: value.name,
|
|
20035
|
+
user: value.user,
|
|
20036
|
+
role: value.role,
|
|
20037
|
+
status: value.status || "active",
|
|
20038
|
+
createdAt: value.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
20039
|
+
updatedAt: "",
|
|
20040
|
+
deletedAt: ""
|
|
20041
|
+
};
|
|
20042
|
+
}
|
|
20043
|
+
|
|
20044
|
+
// src/repositories/member.repository.ts
|
|
20045
|
+
var import_mongodb28 = require("mongodb");
|
|
20046
|
+
function useMemberRepo() {
|
|
20047
|
+
const db = import_utils67.useAtlas.getDb();
|
|
20048
|
+
if (!db) {
|
|
20049
|
+
throw new Error("Unable to connect to server.");
|
|
20050
|
+
}
|
|
20051
|
+
const collection = db.collection("members");
|
|
20052
|
+
async function createIndex() {
|
|
20053
|
+
try {
|
|
20054
|
+
await collection.createIndex([
|
|
20055
|
+
{
|
|
20056
|
+
name: 1
|
|
20057
|
+
},
|
|
20058
|
+
{
|
|
20059
|
+
status: 1
|
|
20060
|
+
}
|
|
20061
|
+
]);
|
|
20062
|
+
} catch (error) {
|
|
20063
|
+
throw new Error("Failed to create index.");
|
|
20064
|
+
}
|
|
20065
|
+
}
|
|
20066
|
+
async function createUniqueIndex() {
|
|
20067
|
+
try {
|
|
20068
|
+
await collection.createIndex(
|
|
20069
|
+
{
|
|
20070
|
+
org: 1,
|
|
20071
|
+
user: 1
|
|
20072
|
+
},
|
|
20073
|
+
{ partialFilterExpression: { deletedAt: "" }, unique: true }
|
|
20074
|
+
);
|
|
20075
|
+
} catch (error) {
|
|
20076
|
+
throw new Error("Failed to create unique index.");
|
|
20077
|
+
}
|
|
20078
|
+
}
|
|
20079
|
+
async function createTextIndex() {
|
|
20080
|
+
try {
|
|
20081
|
+
await collection.createIndex({
|
|
20082
|
+
name: "text"
|
|
20083
|
+
});
|
|
20084
|
+
} catch (error) {
|
|
20085
|
+
throw new Error("Failed to create text index.");
|
|
20086
|
+
}
|
|
20087
|
+
}
|
|
20088
|
+
async function add(value, session) {
|
|
20089
|
+
try {
|
|
20090
|
+
value = MMember(value);
|
|
20091
|
+
await collection.insertOne(value, { session });
|
|
20092
|
+
return "Successfully added member.";
|
|
20093
|
+
} catch (error) {
|
|
20094
|
+
throw new import_utils67.InternalServerError("Failed to create member.");
|
|
20095
|
+
}
|
|
20096
|
+
}
|
|
20097
|
+
async function getById(_id) {
|
|
20098
|
+
try {
|
|
20099
|
+
_id = new import_mongodb28.ObjectId(_id);
|
|
20100
|
+
} catch (error) {
|
|
20101
|
+
throw new import_utils67.BadRequestError("Invalid ID.");
|
|
20102
|
+
}
|
|
20103
|
+
try {
|
|
20104
|
+
return await collection.findOne({ _id });
|
|
20105
|
+
} catch (error) {
|
|
20106
|
+
throw new import_utils67.InternalServerError(
|
|
20107
|
+
"Internal server error, failed to retrieve member."
|
|
20108
|
+
);
|
|
20109
|
+
}
|
|
20110
|
+
}
|
|
20111
|
+
async function getByOrgId(org) {
|
|
20112
|
+
try {
|
|
20113
|
+
org = new import_mongodb28.ObjectId(org);
|
|
20114
|
+
} catch (error) {
|
|
20115
|
+
throw new import_utils67.BadRequestError("Invalid org ID.");
|
|
20116
|
+
}
|
|
20117
|
+
try {
|
|
20118
|
+
return await collection.find({ org }).toArray();
|
|
20119
|
+
} catch (error) {
|
|
20120
|
+
throw new import_utils67.InternalServerError(
|
|
20121
|
+
"Internal server error, failed to retrieve members."
|
|
20122
|
+
);
|
|
20123
|
+
}
|
|
20124
|
+
}
|
|
20125
|
+
async function getOrgsByUserId({
|
|
20126
|
+
search = "",
|
|
20127
|
+
page = 1,
|
|
20128
|
+
limit = 10,
|
|
20129
|
+
sort = {},
|
|
20130
|
+
user = "",
|
|
20131
|
+
status = "active"
|
|
20132
|
+
} = {}) {
|
|
20133
|
+
page = page > 0 ? page - 1 : 0;
|
|
20134
|
+
try {
|
|
20135
|
+
user = new import_mongodb28.ObjectId(user);
|
|
20136
|
+
} catch (error) {
|
|
20137
|
+
throw new import_utils67.BadRequestError("Invalid user ID.");
|
|
20138
|
+
}
|
|
20139
|
+
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
20140
|
+
const query = { user, status };
|
|
20141
|
+
if (search) {
|
|
20142
|
+
query.$text = { $search: search };
|
|
20143
|
+
}
|
|
20144
|
+
try {
|
|
20145
|
+
const items = await collection.aggregate([
|
|
20146
|
+
{ $match: query },
|
|
20147
|
+
{ $sort: sort },
|
|
20148
|
+
{ $skip: page * limit },
|
|
20149
|
+
{
|
|
20150
|
+
$lookup: {
|
|
20151
|
+
from: "orgs",
|
|
20152
|
+
localField: "org",
|
|
20153
|
+
foreignField: "_id",
|
|
20154
|
+
as: "organization"
|
|
20155
|
+
}
|
|
20156
|
+
},
|
|
20157
|
+
{ $unwind: "$organization" },
|
|
20158
|
+
// Flatten the array
|
|
20159
|
+
{
|
|
20160
|
+
$project: { _id: "$organization._id", name: "$organization.name" }
|
|
20161
|
+
}
|
|
20162
|
+
// Extract required fields
|
|
20163
|
+
]).toArray();
|
|
20164
|
+
const length = await collection.countDocuments(query);
|
|
20165
|
+
return (0, import_utils67.paginate)(items, page, limit, length);
|
|
20166
|
+
} catch (error) {
|
|
20167
|
+
throw new import_utils67.InternalServerError(
|
|
20168
|
+
"Internal server error, failed to retrieve organizations."
|
|
20169
|
+
);
|
|
20170
|
+
}
|
|
20171
|
+
}
|
|
20172
|
+
async function updateStatusByUserId(user, status) {
|
|
20173
|
+
try {
|
|
20174
|
+
user = new import_mongodb28.ObjectId(user);
|
|
20175
|
+
} catch (error) {
|
|
20176
|
+
throw new import_utils67.BadRequestError("Invalid user ID.");
|
|
20177
|
+
}
|
|
20178
|
+
try {
|
|
20179
|
+
await collection.updateMany(
|
|
20180
|
+
{ user },
|
|
20181
|
+
{
|
|
20182
|
+
$set: {
|
|
20183
|
+
status
|
|
20184
|
+
}
|
|
20185
|
+
}
|
|
20186
|
+
);
|
|
20187
|
+
return "Successfully updated member status.";
|
|
20188
|
+
} catch (error) {
|
|
20189
|
+
throw new import_utils67.InternalServerError("Failed to update member status.");
|
|
20190
|
+
}
|
|
20191
|
+
}
|
|
20192
|
+
return {
|
|
20193
|
+
createIndex,
|
|
20194
|
+
createUniqueIndex,
|
|
20195
|
+
createTextIndex,
|
|
20196
|
+
add,
|
|
20197
|
+
getById,
|
|
20198
|
+
getByOrgId,
|
|
20199
|
+
getOrgsByUserId,
|
|
20200
|
+
updateStatusByUserId
|
|
20201
|
+
};
|
|
20202
|
+
}
|
|
20203
|
+
|
|
20204
|
+
// src/services/organization.service.ts
|
|
20205
|
+
function useOrgService() {
|
|
20206
|
+
const { add: addOrg } = useOrgRepo();
|
|
20207
|
+
const { addRole } = useRoleRepo();
|
|
20208
|
+
const { add: addMember } = useMemberRepo();
|
|
20209
|
+
const { getUserById } = useUserRepo();
|
|
20210
|
+
async function createOrg({ user = "", name = "", description = "" } = {}) {
|
|
20211
|
+
const session = import_utils68.useAtlas.getClient()?.startSession();
|
|
20212
|
+
session?.startTransaction();
|
|
20213
|
+
try {
|
|
20214
|
+
const _user = await getUserById(user);
|
|
20215
|
+
if (!_user) {
|
|
20216
|
+
throw new import_utils68.NotFoundError("User not found.");
|
|
20217
|
+
}
|
|
20218
|
+
const org = await addOrg(
|
|
20219
|
+
{
|
|
20220
|
+
name,
|
|
20221
|
+
description
|
|
20222
|
+
},
|
|
20223
|
+
session
|
|
20224
|
+
);
|
|
20225
|
+
const role = await addRole(
|
|
20226
|
+
{
|
|
20227
|
+
org,
|
|
20228
|
+
name: "Owner",
|
|
20229
|
+
description: "Owner of the organization",
|
|
20230
|
+
permissions: ["all"]
|
|
20231
|
+
},
|
|
20232
|
+
session
|
|
20233
|
+
);
|
|
20234
|
+
await addMember(
|
|
20235
|
+
{
|
|
20236
|
+
name: `${_user.firstName} ${_user.lastName}`,
|
|
20237
|
+
org,
|
|
20238
|
+
user,
|
|
20239
|
+
role
|
|
20240
|
+
},
|
|
20241
|
+
session
|
|
20242
|
+
);
|
|
20243
|
+
session?.commitTransaction();
|
|
20244
|
+
return "Organization created successfully.";
|
|
20245
|
+
} catch (error) {
|
|
20246
|
+
session?.abortTransaction();
|
|
20247
|
+
throw error;
|
|
20248
|
+
} finally {
|
|
20249
|
+
session?.endSession();
|
|
20250
|
+
}
|
|
20251
|
+
}
|
|
20252
|
+
return {
|
|
20253
|
+
createOrg
|
|
20254
|
+
};
|
|
20255
|
+
}
|
|
20256
|
+
|
|
20257
|
+
// src/controllers/organization.controller.ts
|
|
20258
|
+
var import_utils69 = require("@goweekdays/utils");
|
|
20259
|
+
var import_joi15 = __toESM(require("joi"));
|
|
20260
|
+
function useOrgController() {
|
|
20261
|
+
const { createOrg: _createOrg } = useOrgService();
|
|
20262
|
+
const { getOrgsByUserId: _getOrgsByUserId } = useMemberRepo();
|
|
20263
|
+
async function createOrg(req, res, next) {
|
|
20264
|
+
const value = req.body;
|
|
20265
|
+
const validation = import_joi15.default.object({
|
|
20266
|
+
name: import_joi15.default.string().required(),
|
|
20267
|
+
type: import_joi15.default.string().required(),
|
|
20268
|
+
email: import_joi15.default.string().email().required(),
|
|
20269
|
+
contact: import_joi15.default.string().required(),
|
|
20270
|
+
description: import_joi15.default.string().required(),
|
|
20271
|
+
user: import_joi15.default.string().hex().required()
|
|
20272
|
+
});
|
|
20273
|
+
const { error } = validation.validate(value);
|
|
20274
|
+
if (error) {
|
|
20275
|
+
next(new import_utils69.BadRequestError(error.message));
|
|
20276
|
+
return;
|
|
20277
|
+
}
|
|
20278
|
+
try {
|
|
20279
|
+
const message = await _createOrg(value);
|
|
20280
|
+
res.json({ message });
|
|
20281
|
+
return;
|
|
20282
|
+
} catch (error2) {
|
|
20283
|
+
next(error2);
|
|
20284
|
+
}
|
|
20285
|
+
}
|
|
20286
|
+
async function getOrgsByUserId(req, res, next) {
|
|
20287
|
+
const page = Number(req.query.page) ?? 0;
|
|
20288
|
+
const limit = Number(req.query.limit) ?? 10;
|
|
20289
|
+
const search = req.query.search ?? "";
|
|
20290
|
+
const user = req.headers["user"];
|
|
20291
|
+
const validation = import_joi15.default.object({
|
|
20292
|
+
user: import_joi15.default.string().hex().required(),
|
|
20293
|
+
page: import_joi15.default.number().min(1).optional().allow("", null),
|
|
20294
|
+
limit: import_joi15.default.number().min(1).optional().allow("", null),
|
|
20295
|
+
search: import_joi15.default.string().optional().allow("", null)
|
|
20296
|
+
});
|
|
20297
|
+
const { error } = validation.validate({ user, page, limit, search });
|
|
20298
|
+
if (error) {
|
|
20299
|
+
next(new import_utils69.BadRequestError(error.message));
|
|
20300
|
+
return;
|
|
20301
|
+
}
|
|
20302
|
+
try {
|
|
20303
|
+
const orgs = await _getOrgsByUserId({ user, page, limit, search });
|
|
20304
|
+
res.json(orgs);
|
|
20305
|
+
return;
|
|
20306
|
+
} catch (error2) {
|
|
20307
|
+
next(error2);
|
|
20308
|
+
}
|
|
20309
|
+
}
|
|
20310
|
+
return {
|
|
20311
|
+
createOrg,
|
|
20312
|
+
getOrgsByUserId
|
|
18843
20313
|
};
|
|
18844
20314
|
}
|
|
18845
20315
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -18857,12 +20327,16 @@ function useSubscriptionController() {
|
|
|
18857
20327
|
MAILER_TRANSPORT_HOST,
|
|
18858
20328
|
MAILER_TRANSPORT_PORT,
|
|
18859
20329
|
MAILER_TRANSPORT_SECURE,
|
|
20330
|
+
MAddress,
|
|
18860
20331
|
MCapBldgAct,
|
|
18861
20332
|
MComment,
|
|
18862
20333
|
MEntity,
|
|
18863
20334
|
MFile,
|
|
20335
|
+
MMember,
|
|
18864
20336
|
MONGO_DB,
|
|
18865
20337
|
MONGO_URI,
|
|
20338
|
+
MOrg,
|
|
20339
|
+
MPaymentMethod,
|
|
18866
20340
|
MRole,
|
|
18867
20341
|
MSubscription,
|
|
18868
20342
|
MToken,
|
|
@@ -18887,7 +20361,11 @@ function useSubscriptionController() {
|
|
|
18887
20361
|
SPACES_SECRET_KEY,
|
|
18888
20362
|
VERIFICATION_FORGET_PASSWORD_DURATION,
|
|
18889
20363
|
VERIFICATION_USER_INVITE_DURATION,
|
|
20364
|
+
XENDIT_BASE_URL,
|
|
20365
|
+
XENDIT_SECRET_KEY,
|
|
18890
20366
|
isDev,
|
|
20367
|
+
useAddressController,
|
|
20368
|
+
useAddressRepo,
|
|
18891
20369
|
useAuthController,
|
|
18892
20370
|
useAuthService,
|
|
18893
20371
|
useCapBldgActController,
|
|
@@ -18902,6 +20380,13 @@ function useSubscriptionController() {
|
|
|
18902
20380
|
useFileController,
|
|
18903
20381
|
useFileRepo,
|
|
18904
20382
|
useFileService,
|
|
20383
|
+
useMemberRepo,
|
|
20384
|
+
useOrgController,
|
|
20385
|
+
useOrgRepo,
|
|
20386
|
+
useOrgService,
|
|
20387
|
+
usePaymentMethodController,
|
|
20388
|
+
usePaymentMethodRepo,
|
|
20389
|
+
usePaymentMethodService,
|
|
18905
20390
|
useRoleController,
|
|
18906
20391
|
useRoleRepo,
|
|
18907
20392
|
useRoleService,
|
|
@@ -18917,7 +20402,8 @@ function useSubscriptionController() {
|
|
|
18917
20402
|
useVerificationService,
|
|
18918
20403
|
useWorkflowController,
|
|
18919
20404
|
useWorkflowRepo,
|
|
18920
|
-
useWorkflowService
|
|
20405
|
+
useWorkflowService,
|
|
20406
|
+
useXenditService
|
|
18921
20407
|
});
|
|
18922
20408
|
/*! Bundled license information:
|
|
18923
20409
|
|