@7365admin1/core 3.17.0 → 3.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -12489,7 +12489,8 @@ function useVerificationService() {
12489
12489
  email,
12490
12490
  orgId,
12491
12491
  siteId,
12492
- siteName
12492
+ siteName,
12493
+ inviteType
12493
12494
  }) {
12494
12495
  const schema2 = import_joi11.default.object({
12495
12496
  email: import_joi11.default.string().email().lowercase().required(),
@@ -12526,11 +12527,11 @@ function useVerificationService() {
12526
12527
  };
12527
12528
  try {
12528
12529
  const org = await getOrgById(orgId);
12529
- if (org) {
12530
- value.type = "service-provider-create-org" /* SERVICE_PROVIDER_CREATE_ORG */;
12530
+ if (inviteType === "organization-invite") {
12531
+ value.type = "service-provider-invite" /* SERVICE_PROVIDER_INVITE */;
12531
12532
  subject = "Service Provider Organization Invite" /* _SERVICE_PROVIDER_ORGANIZATION_INVITE */;
12532
12533
  } else {
12533
- value.type = "service-provider-invite" /* SERVICE_PROVIDER_INVITE */;
12534
+ value.type = "service-provider-create-org" /* SERVICE_PROVIDER_CREATE_ORG */;
12534
12535
  subject = "Service Provider Invite" /* _SERVICE_PROVIDER_INVITE */;
12535
12536
  }
12536
12537
  const res = await _add(value);
@@ -15100,25 +15101,47 @@ function useVerificationController() {
15100
15101
  email: import_joi16.default.string().email().required(),
15101
15102
  orgId: import_joi16.default.string().hex().required(),
15102
15103
  siteId: import_joi16.default.string().hex().required(),
15103
- siteName: import_joi16.default.string().required()
15104
+ siteName: import_joi16.default.string().required(),
15105
+ inviteType: import_joi16.default.string().valid("create-org", "organization-invite").required()
15104
15106
  });
15105
15107
  const { error } = validation.validate(payload);
15106
15108
  if (error) {
15107
- import_node_server_utils32.logger.log({ level: "error", message: `controller - ${error.message}` });
15109
+ import_node_server_utils32.logger.log({
15110
+ level: "error",
15111
+ message: `controller - ${error.message}`
15112
+ });
15108
15113
  next(new import_node_server_utils32.BadRequestError(`Invalid input: ${error.message}`));
15109
15114
  return;
15110
15115
  }
15116
+ const {
15117
+ email,
15118
+ orgId,
15119
+ siteId,
15120
+ siteName,
15121
+ inviteType
15122
+ } = payload;
15111
15123
  try {
15112
- await _createServiceProviderInvite(payload);
15124
+ await _createServiceProviderInvite({
15125
+ email,
15126
+ orgId,
15127
+ siteId,
15128
+ siteName,
15129
+ inviteType
15130
+ });
15113
15131
  const cookieOptions = {
15114
15132
  domain: DOMAIN,
15115
15133
  secure: true,
15116
15134
  maxAge: 4 * 60 * 60 * 1e3
15117
15135
  };
15118
- res.cookie("service-provider-email", payload.email, cookieOptions).json({ message: "Successfully invited service provider." });
15136
+ res.cookie("service-provider-email", email, cookieOptions).json({
15137
+ message: "Successfully invited service provider."
15138
+ });
15119
15139
  return;
15120
15140
  } catch (error2) {
15121
- import_node_server_utils32.logger.log({ level: "error", message: `controller - ${error2.message}` });
15141
+ import_node_server_utils32.logger.log({
15142
+ level: "error",
15143
+ message: `controller - ${error2.message}`
15144
+ });
15122
15145
  next(error2);
15123
15146
  return;
15124
15147
  }
@@ -15477,418 +15500,67 @@ function useFileController() {
15477
15500
  }
15478
15501
 
15479
15502
  // src/controllers/organization.controller.ts
15480
- var import_node_server_utils35 = require("@7365admin1/node-server-utils");
15481
- var import_joi18 = __toESM(require("joi"));
15482
- function useOrgController() {
15483
- const { getOrgsByMembership } = useMemberRepo();
15484
- const {
15485
- getByName: _getByName,
15486
- getById: _getById,
15487
- getByEmail: _getByEmail,
15488
- getAll: _getAll,
15489
- add: _add,
15490
- update: _update,
15491
- getOrgsByEmail: _getOrgsByEmail,
15492
- getAdminOrgForResident: _getAdminOrgForResident,
15493
- completeOnboardingById: _completeOnboardingById
15494
- } = useOrgRepo();
15495
- async function add(req, res, next) {
15496
- const validation = import_joi18.default.object({
15497
- name: import_joi18.default.string().required(),
15498
- type: import_joi18.default.string().required(),
15499
- nature: import_joi18.default.string().valid(...allowedNatures).required(),
15500
- email: import_joi18.default.string().email().optional().allow("", null),
15501
- contact: import_joi18.default.string().optional().allow("", null),
15502
- terms: import_joi18.default.string().optional().allow("", null),
15503
- policies: import_joi18.default.string().optional().allow("", null)
15504
- });
15505
- const { error } = validation.validate(req.body);
15506
- if (error) {
15507
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15508
- next(new import_node_server_utils35.BadRequestError(error.message));
15509
- return;
15510
- }
15511
- try {
15512
- await _add(req.body);
15513
- res.status(201).json({ message: "Successfully created organization." });
15514
- return;
15515
- } catch (error2) {
15516
- import_node_server_utils35.logger.log({ level: "error", message: error2.message });
15517
- next(error2);
15518
- return;
15519
- }
15520
- }
15521
- async function getAll(req, res, next) {
15522
- const validation = import_joi18.default.object({
15523
- search: import_joi18.default.string().optional().allow("", null),
15524
- page: import_joi18.default.number().integer().min(1).allow("", null).default(1),
15525
- limit: import_joi18.default.number().integer().min(1).max(100).allow("", null).default(10),
15526
- nature: import_joi18.default.string().valid(...allowedNatures).optional().allow("", null),
15527
- sort: import_joi18.default.string().optional().allow("", null)
15528
- });
15529
- const query = { ...req.query };
15530
- const { error } = validation.validate(query);
15531
- if (error) {
15532
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15533
- next(new import_node_server_utils35.BadRequestError(error.message));
15534
- return;
15535
- }
15536
- const search = req.query.search ?? "";
15537
- const page = parseInt(req.query.page ?? "1");
15538
- const limit = parseInt(req.query.limit ?? "10");
15539
- const nature = req.query.nature ?? "";
15540
- try {
15541
- const data = await _getAll({
15542
- search,
15543
- page,
15544
- limit,
15545
- nature
15546
- });
15547
- res.json(data);
15548
- return;
15549
- } catch (error2) {
15550
- import_node_server_utils35.logger.log({ level: "error", message: error2.message });
15551
- next(error2);
15552
- return;
15553
- }
15554
- }
15555
- async function addOnboardingOrg(req, res, next) {
15556
- const validation = import_joi18.default.object({
15557
- name: import_joi18.default.string().required(),
15558
- type: import_joi18.default.string().required(),
15559
- nature: import_joi18.default.string().valid(...allowedNatures).required(),
15560
- email: import_joi18.default.string().email().optional().allow("", null),
15561
- contact: import_joi18.default.string().optional().allow("", null)
15562
- });
15563
- const { error } = validation.validate(req.body);
15564
- if (error) {
15565
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15566
- next(new import_node_server_utils35.BadRequestError(error.message));
15567
- return;
15568
- }
15569
- try {
15570
- const _id = await _add(req.body);
15571
- const data = await _getById(_id);
15572
- res.status(201).json({
15573
- message: "Successfully created organization.",
15574
- data
15575
- });
15576
- return;
15577
- } catch (error2) {
15578
- import_node_server_utils35.logger.log({ level: "error", message: error2.message });
15579
- next(error2);
15580
- return;
15581
- }
15582
- }
15583
- async function getOrgsByUserId(req, res, next) {
15584
- const validation = import_joi18.default.object({
15585
- search: import_joi18.default.string().optional().allow("", null),
15586
- page: import_joi18.default.number().integer().min(1).allow("", null).default(1),
15587
- limit: import_joi18.default.number().integer().min(1).max(100).allow("", null).default(10),
15588
- user: import_joi18.default.string().hex().required(),
15589
- type: import_joi18.default.string().optional().allow("", null)
15590
- });
15591
- const query = { ...req.query };
15592
- query.user = req.params.user;
15593
- const { error } = validation.validate(query);
15594
- if (error) {
15595
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15596
- next(new import_node_server_utils35.BadRequestError(error.message));
15597
- return;
15598
- }
15599
- const search = req.query.search ?? "";
15600
- const page = parseInt(req.query.page ?? "1");
15601
- const limit = parseInt(req.query.limit ?? "10");
15602
- const user = req.params.user;
15603
- const type = req.query.type ?? "";
15604
- try {
15605
- const data = await getOrgsByMembership({
15606
- search,
15607
- page,
15608
- limit,
15609
- user,
15610
- type
15611
- });
15612
- res.json(data);
15613
- return;
15614
- } catch (error2) {
15615
- import_node_server_utils35.logger.log({ level: "error", message: error2.message });
15616
- next(error2);
15617
- return;
15618
- }
15619
- }
15620
- async function getByName(req, res, next) {
15621
- const validation = import_joi18.default.string().required();
15622
- const name = req.params.name;
15623
- const { error } = validation.validate(name);
15624
- if (error) {
15625
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15626
- next(new import_node_server_utils35.BadRequestError(error.message));
15627
- return;
15628
- }
15629
- try {
15630
- const data = await _getByName(name);
15631
- res.json(data);
15632
- return;
15633
- } catch (error2) {
15634
- next(error2);
15635
- return;
15636
- }
15637
- }
15638
- async function getById(req, res, next) {
15639
- const validation = import_joi18.default.string().hex().required();
15640
- const _id = req.params.id;
15641
- const { error } = validation.validate(_id);
15642
- if (error) {
15643
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15644
- next(new import_node_server_utils35.BadRequestError(error.message));
15645
- return;
15646
- }
15647
- try {
15648
- const data = await _getById(_id);
15649
- res.json(data);
15650
- return;
15651
- } catch (error2) {
15652
- import_node_server_utils35.logger.log({ level: "error", message: error2.message });
15653
- next(error2);
15654
- return;
15655
- }
15656
- }
15657
- async function getByEmail(req, res, next) {
15658
- const validation = import_joi18.default.string().required();
15659
- const email = req.params.email;
15660
- const { error } = validation.validate(email);
15661
- if (error) {
15662
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15663
- next(new import_node_server_utils35.BadRequestError(error.message));
15664
- return;
15665
- }
15666
- try {
15667
- const data = await _getByEmail(email);
15668
- if (!data) {
15669
- next(new import_node_server_utils35.NotFoundError("Organization not found."));
15670
- return;
15671
- }
15672
- res.json(data);
15673
- return;
15674
- } catch (error2) {
15675
- next(error2);
15676
- return;
15677
- }
15678
- }
15679
- async function getOrgsByEmail(req, res, next) {
15680
- const validation = import_joi18.default.object({
15681
- email: import_joi18.default.string().email().required()
15682
- });
15683
- const query = {
15684
- email: req.params.email
15685
- };
15686
- const { error } = validation.validate(query);
15687
- if (error) {
15688
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15689
- next(new import_node_server_utils35.BadRequestError(error.message));
15690
- return;
15691
- }
15692
- const email = req.params.email;
15693
- try {
15694
- const data = await _getOrgsByEmail(email);
15695
- res.json(data);
15696
- return;
15697
- } catch (error2) {
15698
- import_node_server_utils35.logger.log({ level: "error", message: error2.message });
15699
- next(error2);
15700
- return;
15701
- }
15702
- }
15703
- async function update(req, res, next) {
15704
- const validation = import_joi18.default.object({
15705
- name: import_joi18.default.string().optional(),
15706
- type: import_joi18.default.string().optional(),
15707
- nature: import_joi18.default.string().valid(...allowedNatures).optional(),
15708
- email: import_joi18.default.string().email().allow("", null).optional(),
15709
- contact: import_joi18.default.string().allow("", null).optional(),
15710
- terms: import_joi18.default.string().optional().allow("", null),
15711
- policies: import_joi18.default.string().optional().allow("", null)
15712
- });
15713
- const { error } = validation.validate(req.body);
15714
- if (error) {
15715
- next(new import_node_server_utils35.BadRequestError(error.message));
15716
- return;
15717
- }
15718
- const id = req.params.id;
15719
- try {
15720
- await _update(id, req.body);
15721
- const data = await _getById(id);
15722
- res.json({
15723
- message: "Organization updated successfully",
15724
- data
15725
- });
15726
- } catch (err) {
15727
- next(err);
15728
- }
15729
- }
15730
- async function getAdminOrgForResident(_req, res, next) {
15731
- try {
15732
- const data = await _getAdminOrgForResident();
15733
- res.status(200).json(data);
15734
- } catch (error) {
15735
- import_node_server_utils35.logger.log({ level: "error", message: error.message });
15736
- next(error);
15737
- }
15738
- }
15739
- return {
15740
- add,
15741
- addOnboardingOrg,
15742
- getAll,
15743
- getOrgsByUserId,
15744
- getByName,
15745
- getById,
15746
- getByEmail,
15747
- update,
15748
- getOrgsByEmail,
15749
- getAdminOrgForResident
15750
- };
15751
- }
15503
+ var import_node_server_utils37 = require("@7365admin1/node-server-utils");
15504
+ var import_joi20 = __toESM(require("joi"));
15752
15505
 
15753
- // src/controllers/organization-v2.controller.ts
15754
- var import_joi19 = __toESM(require("joi"));
15506
+ // src/repositories/subscription.repo.ts
15755
15507
  var import_node_server_utils36 = require("@7365admin1/node-server-utils");
15756
- function useOrgControllerV2() {
15757
- const { getAll: _getAll, getOrganizationsWithSubscription: _getOrganizationsWithSubscription } = useOrgRepo();
15758
- async function getAll(req, res, next) {
15759
- const validation = import_joi19.default.object({
15760
- search: import_joi19.default.string().optional().allow("", null),
15761
- page: import_joi19.default.number().integer().min(1).allow("", null).default(1),
15762
- limit: import_joi19.default.number().integer().min(1).max(100).allow("", null).default(10),
15763
- nature: import_joi19.default.string().valid(...allowedNatures).optional().allow("", null),
15764
- status: import_joi19.default.string().trim().valid("active", "suspended", "deleted").optional().empty("").default("active")
15765
- });
15766
- const query = { ...req.query };
15767
- const { error, value } = validation.validate(query, {
15768
- convert: true,
15769
- stripUnknown: true
15770
- });
15771
- if (error) {
15772
- import_node_server_utils36.logger.log({ level: "error", message: error.message });
15773
- next(new import_node_server_utils36.BadRequestError(error.message));
15774
- return;
15775
- }
15776
- const search = value.search ?? "";
15777
- const page = typeof value.page === "number" ? value.page : parseInt(String(value.page ?? "1"), 10);
15778
- const limit = typeof value.limit === "number" ? value.limit : parseInt(String(value.limit ?? "10"), 10);
15779
- const nature = value.nature ?? "";
15780
- const status = value.status;
15781
- try {
15782
- const data = await _getAll({
15783
- search,
15784
- page,
15785
- limit,
15786
- nature,
15787
- status
15788
- });
15789
- res.json(data);
15790
- return;
15791
- } catch (error2) {
15792
- import_node_server_utils36.logger.log({ level: "error", message: error2.message });
15793
- next(error2);
15794
- return;
15795
- }
15796
- }
15797
- async function getOrganizationsWithSubscription(req, res, next) {
15798
- const validation = import_joi19.default.object({
15799
- search: import_joi19.default.string().optional().allow("", null),
15800
- page: import_joi19.default.number().integer().min(1).default(1),
15801
- limit: import_joi19.default.number().integer().min(1).max(100).default(10),
15802
- status: import_joi19.default.string().trim().valid("active", "suspended", "deleted").default("active"),
15803
- type: import_joi19.default.string().optional().allow("", null),
15804
- billingCycle: import_joi19.default.string().optional().allow("", null)
15805
- });
15806
- const { error, value } = validation.validate(
15807
- req.query,
15808
- {
15809
- convert: true,
15810
- stripUnknown: true
15811
- }
15812
- );
15813
- if (error) {
15814
- next(new import_node_server_utils36.BadRequestError(error.message));
15815
- return;
15816
- }
15817
- try {
15818
- const data = await _getOrganizationsWithSubscription({
15819
- search: value.search ?? "",
15820
- page: value.page ?? 1,
15821
- limit: value.limit ?? 10,
15822
- status: value.status ?? "active",
15823
- type: value.type ?? "",
15824
- billingCycle: value.billingCycle ?? ""
15825
- });
15826
- res.json(data);
15827
- } catch (error2) {
15828
- next(error2);
15829
- }
15830
- }
15831
- return {
15832
- getAll,
15833
- getOrganizationsWithSubscription
15834
- };
15835
- }
15836
15508
 
15837
15509
  // src/models/subscription.model.ts
15838
- var import_node_server_utils37 = require("@7365admin1/node-server-utils");
15839
- var import_joi20 = __toESM(require("joi"));
15510
+ var import_node_server_utils35 = require("@7365admin1/node-server-utils");
15511
+ var import_joi18 = __toESM(require("joi"));
15840
15512
  var import_mongodb22 = require("mongodb");
15841
15513
  var SubscriptionType = /* @__PURE__ */ ((SubscriptionType2) => {
15842
15514
  SubscriptionType2["ORGANIZATION"] = "organization";
15843
15515
  SubscriptionType2["AFFILIATE"] = "affiliate";
15844
15516
  return SubscriptionType2;
15845
15517
  })(SubscriptionType || {});
15846
- var schema = import_joi20.default.object({
15847
- user: import_joi20.default.string().hex().required(),
15848
- amount: import_joi20.default.number().min(0).required(),
15849
- payment_method_card_number: import_joi20.default.string().optional().allow("", null),
15850
- payment_method_cardholder_name: import_joi20.default.string().optional().allow("", null),
15851
- payment_method_expiry_month: import_joi20.default.string().optional().allow("", null),
15852
- payment_method_expiry_year: import_joi20.default.string().optional().allow("", null),
15853
- payment_method_cvv: import_joi20.default.string().optional().allow("", null),
15854
- payment_method_type: import_joi20.default.string().optional().allow("", null),
15855
- currency: import_joi20.default.string().optional().allow("", null),
15856
- seats: import_joi20.default.number().optional().min(0).allow(null),
15518
+ var schema = import_joi18.default.object({
15519
+ user: import_joi18.default.string().hex().required(),
15520
+ amount: import_joi18.default.number().min(0).required(),
15521
+ payment_method_card_number: import_joi18.default.string().optional().allow("", null),
15522
+ payment_method_cardholder_name: import_joi18.default.string().optional().allow("", null),
15523
+ payment_method_expiry_month: import_joi18.default.string().optional().allow("", null),
15524
+ payment_method_expiry_year: import_joi18.default.string().optional().allow("", null),
15525
+ payment_method_cvv: import_joi18.default.string().optional().allow("", null),
15526
+ payment_method_type: import_joi18.default.string().optional().allow("", null),
15527
+ currency: import_joi18.default.string().optional().allow("", null),
15528
+ seats: import_joi18.default.number().optional().min(0).allow(null),
15857
15529
  organization: orgSchema.optional().allow({}),
15858
- billingAddress: import_joi20.default.object({
15859
- type: import_joi20.default.string().required(),
15860
- country: import_joi20.default.string().required(),
15861
- address: import_joi20.default.string().required(),
15862
- continuedAddress: import_joi20.default.string().optional().allow("", null),
15863
- city: import_joi20.default.string().required(),
15864
- province: import_joi20.default.string().optional().allow("", null),
15865
- postalCode: import_joi20.default.string().required(),
15866
- taxId: import_joi20.default.string().optional().allow("", null)
15530
+ billingAddress: import_joi18.default.object({
15531
+ type: import_joi18.default.string().required(),
15532
+ country: import_joi18.default.string().required(),
15533
+ address: import_joi18.default.string().required(),
15534
+ continuedAddress: import_joi18.default.string().optional().allow("", null),
15535
+ city: import_joi18.default.string().required(),
15536
+ province: import_joi18.default.string().optional().allow("", null),
15537
+ postalCode: import_joi18.default.string().required(),
15538
+ taxId: import_joi18.default.string().optional().allow("", null)
15867
15539
  }).required(),
15868
- promoCode: import_joi20.default.string().optional().allow("", null)
15540
+ promoCode: import_joi18.default.string().optional().allow("", null)
15869
15541
  });
15870
15542
  function MSubscription(value) {
15871
- const schema2 = import_joi20.default.object({
15872
- _id: import_joi20.default.string().hex().optional().allow("", null),
15873
- user: import_joi20.default.string().hex().optional().allow("", null),
15874
- org: import_joi20.default.string().hex().optional().allow("", null),
15875
- amount: import_joi20.default.number().min(0).required(),
15876
- currency: import_joi20.default.string().required(),
15877
- description: import_joi20.default.string().optional().allow("", null),
15878
- promoCode: import_joi20.default.string().optional().allow("", null),
15879
- type: import_joi20.default.string().valid(...Object.values(SubscriptionType)).optional().allow(null, ""),
15880
- paidSeats: import_joi20.default.number().optional().min(0).allow("", null),
15881
- currentSeats: import_joi20.default.number().optional().min(0).allow("", null),
15882
- maxSeats: import_joi20.default.number().optional().min(0).allow("", null),
15883
- status: import_joi20.default.string().optional().allow("", null),
15884
- billingCycle: import_joi20.default.string().valid("monthly", "yearly").required(),
15543
+ const schema2 = import_joi18.default.object({
15544
+ _id: import_joi18.default.string().hex().optional().allow("", null),
15545
+ user: import_joi18.default.string().hex().optional().allow("", null),
15546
+ org: import_joi18.default.string().hex().optional().allow("", null),
15547
+ amount: import_joi18.default.number().min(0).required(),
15548
+ currency: import_joi18.default.string().required(),
15549
+ description: import_joi18.default.string().optional().allow("", null),
15550
+ promoCode: import_joi18.default.string().optional().allow("", null),
15551
+ type: import_joi18.default.string().valid(...Object.values(SubscriptionType)).optional().allow(null, ""),
15552
+ paidSeats: import_joi18.default.number().optional().min(0).allow("", null),
15553
+ currentSeats: import_joi18.default.number().optional().min(0).allow("", null),
15554
+ maxSeats: import_joi18.default.number().optional().min(0).allow("", null),
15555
+ status: import_joi18.default.string().optional().allow("", null),
15556
+ billingCycle: import_joi18.default.string().valid("monthly", "yearly").required(),
15885
15557
  // Ensure valid values
15886
- nextBillingDate: import_joi20.default.date().optional(),
15887
- lastPaymentStatus: import_joi20.default.string().optional().allow("", null),
15888
- failedAttempts: import_joi20.default.number().optional().allow("", null),
15889
- createdAt: import_joi20.default.date().optional(),
15890
- updatedAt: import_joi20.default.string().optional().allow("", null),
15891
- deletedAt: import_joi20.default.string().optional().allow("", null)
15558
+ nextBillingDate: import_joi18.default.date().optional(),
15559
+ lastPaymentStatus: import_joi18.default.string().optional().allow("", null),
15560
+ failedAttempts: import_joi18.default.number().optional().allow("", null),
15561
+ createdAt: import_joi18.default.date().optional(),
15562
+ updatedAt: import_joi18.default.string().optional().allow("", null),
15563
+ deletedAt: import_joi18.default.string().optional().allow("", null)
15892
15564
  }).custom((value2, helpers) => {
15893
15565
  if (!value2.user && !value2.org) {
15894
15566
  return helpers.error("any.invalid", {
@@ -15899,7 +15571,7 @@ function MSubscription(value) {
15899
15571
  });
15900
15572
  const { error } = schema2.validate(value);
15901
15573
  if (error) {
15902
- throw new import_node_server_utils37.BadRequestError(error.details[0].message);
15574
+ throw new import_node_server_utils35.BadRequestError(error.details[0].message);
15903
15575
  }
15904
15576
  if (value._id)
15905
15577
  value._id = new import_mongodb22.ObjectId(value._id);
@@ -15939,13 +15611,12 @@ function MSubscription(value) {
15939
15611
  }
15940
15612
 
15941
15613
  // src/repositories/subscription.repo.ts
15942
- var import_node_server_utils38 = require("@7365admin1/node-server-utils");
15943
15614
  var import_mongodb23 = require("mongodb");
15944
- var import_joi21 = __toESM(require("joi"));
15615
+ var import_joi19 = __toESM(require("joi"));
15945
15616
  function useSubscriptionRepo() {
15946
- const db = import_node_server_utils38.useAtlas.getDb();
15617
+ const db = import_node_server_utils36.useAtlas.getDb();
15947
15618
  if (!db) {
15948
- throw new import_node_server_utils38.BadRequestError("Unable to connect to server.");
15619
+ throw new import_node_server_utils36.BadRequestError("Unable to connect to server.");
15949
15620
  }
15950
15621
  const namespace_collection = "subscriptions";
15951
15622
  const collection = db.collection(namespace_collection);
@@ -15960,7 +15631,7 @@ function useSubscriptionRepo() {
15960
15631
  { key: { failedAttempts: 1 } }
15961
15632
  ]);
15962
15633
  } catch (error) {
15963
- throw new import_node_server_utils38.BadRequestError("Failed to create index on subscription.");
15634
+ throw new import_node_server_utils36.BadRequestError("Failed to create index on subscription.");
15964
15635
  }
15965
15636
  }
15966
15637
  async function createUniqueIndex() {
@@ -15970,96 +15641,96 @@ function useSubscriptionRepo() {
15970
15641
  { unique: true }
15971
15642
  );
15972
15643
  } catch (error) {
15973
- throw new import_node_server_utils38.BadRequestError(
15644
+ throw new import_node_server_utils36.BadRequestError(
15974
15645
  "Failed to create unique index on subscription."
15975
15646
  );
15976
15647
  }
15977
15648
  }
15978
- const { delNamespace, setCache, getCache, delCache } = (0, import_node_server_utils38.useCache)(namespace_collection);
15649
+ const { delNamespace, setCache, getCache, delCache } = (0, import_node_server_utils36.useCache)(namespace_collection);
15979
15650
  async function add(value, session) {
15980
15651
  try {
15981
15652
  value = MSubscription(value);
15982
15653
  const res = await collection.insertOne(value, { session });
15983
15654
  delNamespace().then(() => {
15984
- import_node_server_utils38.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
15655
+ import_node_server_utils36.logger.info(`Cache cleared for namespace: ${namespace_collection}`);
15985
15656
  }).catch((err) => {
15986
- import_node_server_utils38.logger.error(
15657
+ import_node_server_utils36.logger.error(
15987
15658
  `Failed to clear cache for namespace: ${namespace_collection}`,
15988
15659
  err
15989
15660
  );
15990
15661
  });
15991
15662
  return res.insertedId;
15992
15663
  } catch (error) {
15993
- import_node_server_utils38.logger.log({ level: "error", message: `${error}` });
15664
+ import_node_server_utils36.logger.log({ level: "error", message: `${error}` });
15994
15665
  const isDuplicated = error.message.includes("duplicate");
15995
15666
  if (isDuplicated) {
15996
- throw new import_node_server_utils38.BadRequestError("Subscription already exists.");
15667
+ throw new import_node_server_utils36.BadRequestError("Subscription already exists.");
15997
15668
  }
15998
- throw new import_node_server_utils38.BadRequestError("Failed to create subscription.");
15669
+ throw new import_node_server_utils36.BadRequestError("Failed to create subscription.");
15999
15670
  }
16000
15671
  }
16001
15672
  async function getById(_id) {
16002
15673
  try {
16003
15674
  _id = new import_mongodb23.ObjectId(_id);
16004
15675
  } catch (error) {
16005
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
15676
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16006
15677
  }
16007
15678
  try {
16008
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id });
15679
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id });
16009
15680
  const cachedData = await getCache(cacheKey);
16010
15681
  if (cachedData) {
16011
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15682
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16012
15683
  return cachedData;
16013
15684
  }
16014
15685
  const data = await collection.findOne({ _id });
16015
15686
  setCache(cacheKey, data, 15 * 60).then(() => {
16016
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15687
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16017
15688
  }).catch((err) => {
16018
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15689
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16019
15690
  });
16020
15691
  return data;
16021
15692
  } catch (error) {
16022
- throw new import_node_server_utils38.BadRequestError("Failed to get subscription by ID.");
15693
+ throw new import_node_server_utils36.BadRequestError("Failed to get subscription by ID.");
16023
15694
  }
16024
15695
  }
16025
15696
  async function getByUserId(user) {
16026
15697
  try {
16027
15698
  user = new import_mongodb23.ObjectId(user);
16028
15699
  } catch (error) {
16029
- throw new import_node_server_utils38.BadRequestError("Invalid user ID format.");
15700
+ throw new import_node_server_utils36.BadRequestError("Invalid user ID format.");
16030
15701
  }
16031
15702
  try {
16032
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { user });
15703
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { user });
16033
15704
  const cachedData = await getCache(cacheKey);
16034
15705
  if (cachedData) {
16035
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15706
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16036
15707
  return cachedData;
16037
15708
  }
16038
15709
  const data = await collection.findOne({ user });
16039
15710
  setCache(cacheKey, data, 15 * 60).then(() => {
16040
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15711
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16041
15712
  }).catch((err) => {
16042
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15713
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16043
15714
  });
16044
15715
  return data;
16045
15716
  } catch (error) {
16046
- throw new import_node_server_utils38.BadRequestError("Failed to get subscription by ID.");
15717
+ throw new import_node_server_utils36.BadRequestError("Failed to get subscription by ID.");
16047
15718
  }
16048
15719
  }
16049
15720
  async function getByAffiliateUserId(user) {
16050
15721
  try {
16051
15722
  user = new import_mongodb23.ObjectId(user);
16052
15723
  } catch (error) {
16053
- throw new import_node_server_utils38.BadRequestError("Invalid user ID format.");
15724
+ throw new import_node_server_utils36.BadRequestError("Invalid user ID format.");
16054
15725
  }
16055
15726
  try {
16056
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, {
15727
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, {
16057
15728
  user,
16058
15729
  type: "affiliate"
16059
15730
  });
16060
15731
  const cachedData = await getCache(cacheKey);
16061
15732
  if (cachedData) {
16062
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15733
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16063
15734
  return cachedData;
16064
15735
  }
16065
15736
  const data = await collection.findOne({
@@ -16067,29 +15738,29 @@ function useSubscriptionRepo() {
16067
15738
  type: "affiliate"
16068
15739
  });
16069
15740
  setCache(cacheKey, data, 15 * 60).then(() => {
16070
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15741
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16071
15742
  }).catch((err) => {
16072
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15743
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16073
15744
  });
16074
15745
  return data;
16075
15746
  } catch (error) {
16076
- throw new import_node_server_utils38.BadRequestError("Failed to get subscription by ID.");
15747
+ throw new import_node_server_utils36.BadRequestError("Failed to get subscription by ID.");
16077
15748
  }
16078
15749
  }
16079
15750
  async function getByOrgId(org) {
16080
15751
  try {
16081
15752
  org = new import_mongodb23.ObjectId(org);
16082
15753
  } catch (error) {
16083
- throw new import_node_server_utils38.BadRequestError("Invalid org ID format.");
15754
+ throw new import_node_server_utils36.BadRequestError("Invalid org ID format.");
16084
15755
  }
16085
15756
  try {
16086
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, {
15757
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, {
16087
15758
  org,
16088
15759
  type: "organization"
16089
15760
  });
16090
15761
  const cachedData = await getCache(cacheKey);
16091
15762
  if (cachedData) {
16092
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15763
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16093
15764
  return cachedData;
16094
15765
  }
16095
15766
  const data = await collection.findOne({
@@ -16097,32 +15768,32 @@ function useSubscriptionRepo() {
16097
15768
  type: "organization"
16098
15769
  });
16099
15770
  setCache(cacheKey, data, 15 * 60).then(() => {
16100
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15771
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16101
15772
  }).catch((err) => {
16102
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15773
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16103
15774
  });
16104
15775
  return data;
16105
15776
  } catch (error) {
16106
- throw new import_node_server_utils38.BadRequestError("Failed to get subscription by ID.");
15777
+ throw new import_node_server_utils36.BadRequestError("Failed to get subscription by ID.");
16107
15778
  }
16108
15779
  }
16109
15780
  async function getBySubscriptionId(subscriptionId) {
16110
15781
  try {
16111
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { subscriptionId });
15782
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { subscriptionId });
16112
15783
  const cachedData = await getCache(cacheKey);
16113
15784
  if (cachedData) {
16114
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15785
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16115
15786
  return cachedData;
16116
15787
  }
16117
15788
  const data = await collection.findOne({ subscriptionId });
16118
15789
  setCache(cacheKey, data, 15 * 60).then(() => {
16119
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15790
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16120
15791
  }).catch((err) => {
16121
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15792
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16122
15793
  });
16123
15794
  return data;
16124
15795
  } catch (error) {
16125
- throw new import_node_server_utils38.BadRequestError(
15796
+ throw new import_node_server_utils36.BadRequestError(
16126
15797
  "Failed to get subscription by subscription ID."
16127
15798
  );
16128
15799
  }
@@ -16143,10 +15814,10 @@ function useSubscriptionRepo() {
16143
15814
  query.$text = { $search: search };
16144
15815
  cacheOptions.search = search;
16145
15816
  }
16146
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, cacheOptions);
15817
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, cacheOptions);
16147
15818
  const cachedData = await getCache(cacheKey);
16148
15819
  if (cachedData) {
16149
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15820
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16150
15821
  return cachedData;
16151
15822
  }
16152
15823
  try {
@@ -16157,15 +15828,15 @@ function useSubscriptionRepo() {
16157
15828
  { $limit: limit }
16158
15829
  ]).toArray();
16159
15830
  const length = await collection.countDocuments(query);
16160
- const data = (0, import_node_server_utils38.paginate)(items, page, limit, length);
15831
+ const data = (0, import_node_server_utils36.paginate)(items, page, limit, length);
16161
15832
  setCache(cacheKey, data, 15 * 60).then(() => {
16162
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15833
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16163
15834
  }).catch((err) => {
16164
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15835
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16165
15836
  });
16166
15837
  return data;
16167
15838
  } catch (error) {
16168
- import_node_server_utils38.logger.log({ level: "error", message: `${error}` });
15839
+ import_node_server_utils36.logger.log({ level: "error", message: `${error}` });
16169
15840
  throw error;
16170
15841
  }
16171
15842
  }
@@ -16173,19 +15844,19 @@ function useSubscriptionRepo() {
16173
15844
  try {
16174
15845
  _id = new import_mongodb23.ObjectId(_id);
16175
15846
  } catch (error) {
16176
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
15847
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16177
15848
  }
16178
15849
  try {
16179
15850
  await collection.updateOne({ _id }, { $set: { status } });
16180
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id });
15851
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id });
16181
15852
  delCache(cacheKey).then(() => {
16182
- import_node_server_utils38.logger.info(`Cache deleted for key: ${cacheKey}`);
15853
+ import_node_server_utils36.logger.info(`Cache deleted for key: ${cacheKey}`);
16183
15854
  }).catch((err) => {
16184
- import_node_server_utils38.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
15855
+ import_node_server_utils36.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16185
15856
  });
16186
15857
  return "Successfully updated subscription status.";
16187
15858
  } catch (error) {
16188
- throw new import_node_server_utils38.BadRequestError("Failed to update subscription status.");
15859
+ throw new import_node_server_utils36.BadRequestError("Failed to update subscription status.");
16189
15860
  }
16190
15861
  }
16191
15862
  async function getDueSubscriptions(BATCH_SIZE = 100) {
@@ -16209,10 +15880,10 @@ function useSubscriptionRepo() {
16209
15880
  }
16210
15881
  ]
16211
15882
  };
16212
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, cacheOptions);
15883
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, cacheOptions);
16213
15884
  const cachedData = await getCache(cacheKey);
16214
15885
  if (cachedData) {
16215
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15886
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16216
15887
  return cachedData;
16217
15888
  }
16218
15889
  try {
@@ -16234,13 +15905,13 @@ function useSubscriptionRepo() {
16234
15905
  ]
16235
15906
  }).sort({ nextBillingDate: 1 }).limit(BATCH_SIZE).toArray();
16236
15907
  setCache(cacheKey, data, 15 * 60).then(() => {
16237
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15908
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16238
15909
  }).catch((err) => {
16239
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15910
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16240
15911
  });
16241
15912
  return data;
16242
15913
  } catch (error) {
16243
- throw new import_node_server_utils38.BadRequestError("Failed to get due subscriptions.");
15914
+ throw new import_node_server_utils36.BadRequestError("Failed to get due subscriptions.");
16244
15915
  }
16245
15916
  }
16246
15917
  async function getFailedSubscriptions(BATCH_SIZE = 100) {
@@ -16248,10 +15919,10 @@ function useSubscriptionRepo() {
16248
15919
  lastPaymentStatus: "failed",
16249
15920
  status: "active"
16250
15921
  };
16251
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, cacheOptions);
15922
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, cacheOptions);
16252
15923
  const cachedData = await getCache(cacheKey);
16253
15924
  if (cachedData) {
16254
- import_node_server_utils38.logger.info(`Cache hit for key: ${cacheKey}`);
15925
+ import_node_server_utils36.logger.info(`Cache hit for key: ${cacheKey}`);
16255
15926
  return cachedData;
16256
15927
  }
16257
15928
  try {
@@ -16260,13 +15931,13 @@ function useSubscriptionRepo() {
16260
15931
  status: "active"
16261
15932
  }).limit(BATCH_SIZE).toArray();
16262
15933
  setCache(cacheKey, data, 15 * 60).then(() => {
16263
- import_node_server_utils38.logger.info(`Cache set for key: ${cacheKey}`);
15934
+ import_node_server_utils36.logger.info(`Cache set for key: ${cacheKey}`);
16264
15935
  }).catch((err) => {
16265
- import_node_server_utils38.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
15936
+ import_node_server_utils36.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
16266
15937
  });
16267
15938
  return data;
16268
15939
  } catch (error) {
16269
- throw new import_node_server_utils38.BadRequestError("Failed to get failed subscriptions.");
15940
+ throw new import_node_server_utils36.BadRequestError("Failed to get failed subscriptions.");
16270
15941
  }
16271
15942
  }
16272
15943
  async function findOrgSubscriptionsForStatusSync() {
@@ -16280,7 +15951,7 @@ function useSubscriptionRepo() {
16280
15951
  ).toArray();
16281
15952
  return data;
16282
15953
  } catch (error) {
16283
- throw new import_node_server_utils38.BadRequestError(
15954
+ throw new import_node_server_utils36.BadRequestError(
16284
15955
  "Failed to list organization subscriptions for status sync."
16285
15956
  );
16286
15957
  }
@@ -16299,24 +15970,24 @@ function useSubscriptionRepo() {
16299
15970
  ).toArray();
16300
15971
  return data;
16301
15972
  } catch (error) {
16302
- throw new import_node_server_utils38.BadRequestError(
15973
+ throw new import_node_server_utils36.BadRequestError(
16303
15974
  "Failed to list organization subscriptions with overdue next billing date."
16304
15975
  );
16305
15976
  }
16306
15977
  }
16307
15978
  async function processSuccessfulPayment(value, session) {
16308
- const schema2 = import_joi21.default.object({
16309
- _id: import_joi21.default.string().hex().required(),
16310
- nextBillingDate: import_joi21.default.date().required()
15979
+ const schema2 = import_joi19.default.object({
15980
+ _id: import_joi19.default.string().hex().required(),
15981
+ nextBillingDate: import_joi19.default.date().required()
16311
15982
  });
16312
15983
  const { error } = schema2.validate(value);
16313
15984
  if (error) {
16314
- throw new import_node_server_utils38.BadRequestError(error.message);
15985
+ throw new import_node_server_utils36.BadRequestError(error.message);
16315
15986
  }
16316
15987
  try {
16317
15988
  value._id = new import_mongodb23.ObjectId(value._id);
16318
15989
  } catch (error2) {
16319
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
15990
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16320
15991
  }
16321
15992
  const date = value.nextBillingDate;
16322
15993
  try {
@@ -16334,31 +16005,31 @@ function useSubscriptionRepo() {
16334
16005
  },
16335
16006
  { session }
16336
16007
  );
16337
- import_node_server_utils38.logger.info(`${res.modifiedCount} subscription updated.`);
16338
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id: value._id });
16008
+ import_node_server_utils36.logger.info(`${res.modifiedCount} subscription updated.`);
16009
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id: value._id });
16339
16010
  delCache(cacheKey).then(() => {
16340
- import_node_server_utils38.logger.info(`Cache deleted for key: ${cacheKey}`);
16011
+ import_node_server_utils36.logger.info(`Cache deleted for key: ${cacheKey}`);
16341
16012
  }).catch((err) => {
16342
- import_node_server_utils38.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16013
+ import_node_server_utils36.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16343
16014
  });
16344
16015
  return "Successfully updated subscription.";
16345
16016
  } catch (error2) {
16346
- import_node_server_utils38.logger.error(`${error2}`);
16347
- throw new import_node_server_utils38.BadRequestError("Failed to update subscription.");
16017
+ import_node_server_utils36.logger.error(`${error2}`);
16018
+ throw new import_node_server_utils36.BadRequestError("Failed to update subscription.");
16348
16019
  }
16349
16020
  }
16350
16021
  async function markSubscriptionAsFailed({ _id, failed }, session) {
16351
- const schema2 = import_joi21.default.object({
16352
- _id: import_joi21.default.string().hex().required()
16022
+ const schema2 = import_joi19.default.object({
16023
+ _id: import_joi19.default.string().hex().required()
16353
16024
  });
16354
16025
  const { error } = schema2.validate({ _id });
16355
16026
  if (error) {
16356
- throw new import_node_server_utils38.BadRequestError(error.message);
16027
+ throw new import_node_server_utils36.BadRequestError(error.message);
16357
16028
  }
16358
16029
  try {
16359
16030
  _id = new import_mongodb23.ObjectId(_id);
16360
16031
  } catch (error2) {
16361
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
16032
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16362
16033
  }
16363
16034
  const updateOptions = {
16364
16035
  $inc: { failedAttempts: 1 }
@@ -16370,29 +16041,29 @@ function useSubscriptionRepo() {
16370
16041
  const result = await collection.updateOne({ _id }, updateOptions, {
16371
16042
  session
16372
16043
  });
16373
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id });
16044
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id });
16374
16045
  delCache(cacheKey).then(() => {
16375
- import_node_server_utils38.logger.info(`Cache deleted for key: ${cacheKey}`);
16046
+ import_node_server_utils36.logger.info(`Cache deleted for key: ${cacheKey}`);
16376
16047
  }).catch((err) => {
16377
- import_node_server_utils38.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16048
+ import_node_server_utils36.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16378
16049
  });
16379
16050
  return result;
16380
16051
  } catch (error2) {
16381
- throw new import_node_server_utils38.BadRequestError("Failed to update subscription.");
16052
+ throw new import_node_server_utils36.BadRequestError("Failed to update subscription.");
16382
16053
  }
16383
16054
  }
16384
16055
  async function markSubscriptionAsCanceled(_id, session) {
16385
- const schema2 = import_joi21.default.object({
16386
- _id: import_joi21.default.string().hex().required()
16056
+ const schema2 = import_joi19.default.object({
16057
+ _id: import_joi19.default.string().hex().required()
16387
16058
  });
16388
16059
  const { error } = schema2.validate({ _id });
16389
16060
  if (error) {
16390
- throw new import_node_server_utils38.BadRequestError(error.message);
16061
+ throw new import_node_server_utils36.BadRequestError(error.message);
16391
16062
  }
16392
16063
  try {
16393
16064
  _id = new import_mongodb23.ObjectId(_id);
16394
16065
  } catch (error2) {
16395
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
16066
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16396
16067
  }
16397
16068
  try {
16398
16069
  const result = await collection.updateOne(
@@ -16400,29 +16071,29 @@ function useSubscriptionRepo() {
16400
16071
  { $set: { status: "canceled" } },
16401
16072
  { session }
16402
16073
  );
16403
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id });
16074
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id });
16404
16075
  delCache(cacheKey).then(() => {
16405
- import_node_server_utils38.logger.info(`Cache deleted for key: ${cacheKey}`);
16076
+ import_node_server_utils36.logger.info(`Cache deleted for key: ${cacheKey}`);
16406
16077
  }).catch((err) => {
16407
- import_node_server_utils38.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16078
+ import_node_server_utils36.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16408
16079
  });
16409
16080
  return result;
16410
16081
  } catch (error2) {
16411
- throw new import_node_server_utils38.BadRequestError("Failed to update subscription.");
16082
+ throw new import_node_server_utils36.BadRequestError("Failed to update subscription.");
16412
16083
  }
16413
16084
  }
16414
16085
  async function updateStatusById(_id, status, session) {
16415
- const schema2 = import_joi21.default.object({
16416
- _id: import_joi21.default.string().hex().required()
16086
+ const schema2 = import_joi19.default.object({
16087
+ _id: import_joi19.default.string().hex().required()
16417
16088
  });
16418
16089
  const { error } = schema2.validate({ _id });
16419
16090
  if (error) {
16420
- throw new import_node_server_utils38.BadRequestError(error.message);
16091
+ throw new import_node_server_utils36.BadRequestError(error.message);
16421
16092
  }
16422
16093
  try {
16423
16094
  _id = new import_mongodb23.ObjectId(_id);
16424
16095
  } catch (error2) {
16425
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
16096
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16426
16097
  }
16427
16098
  try {
16428
16099
  const result = await collection.updateOne(
@@ -16430,15 +16101,15 @@ function useSubscriptionRepo() {
16430
16101
  { $set: { status } },
16431
16102
  { session }
16432
16103
  );
16433
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id });
16104
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id });
16434
16105
  delCache(cacheKey).then(() => {
16435
- import_node_server_utils38.logger.info(`Cache deleted for key: ${cacheKey}`);
16106
+ import_node_server_utils36.logger.info(`Cache deleted for key: ${cacheKey}`);
16436
16107
  }).catch((err) => {
16437
- import_node_server_utils38.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16108
+ import_node_server_utils36.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16438
16109
  });
16439
16110
  return result;
16440
16111
  } catch (error2) {
16441
- throw new import_node_server_utils38.BadRequestError("Failed to update subscription status.");
16112
+ throw new import_node_server_utils36.BadRequestError("Failed to update subscription status.");
16442
16113
  }
16443
16114
  }
16444
16115
  async function updateSeatsById({
@@ -16449,13 +16120,13 @@ function useSubscriptionRepo() {
16449
16120
  amount,
16450
16121
  promoCode
16451
16122
  }, session) {
16452
- const schema2 = import_joi21.default.object({
16453
- _id: import_joi21.default.string().hex().required(),
16454
- currentSeats: import_joi21.default.number().required().min(1),
16455
- maxSeats: import_joi21.default.number().required().min(0),
16456
- paidSeats: import_joi21.default.number().optional().min(0),
16457
- amount: import_joi21.default.number().required().min(0),
16458
- promoCode: import_joi21.default.string().optional().allow("", null)
16123
+ const schema2 = import_joi19.default.object({
16124
+ _id: import_joi19.default.string().hex().required(),
16125
+ currentSeats: import_joi19.default.number().required().min(1),
16126
+ maxSeats: import_joi19.default.number().required().min(0),
16127
+ paidSeats: import_joi19.default.number().optional().min(0),
16128
+ amount: import_joi19.default.number().required().min(0),
16129
+ promoCode: import_joi19.default.string().optional().allow("", null)
16459
16130
  });
16460
16131
  const { error } = schema2.validate({
16461
16132
  _id,
@@ -16466,12 +16137,12 @@ function useSubscriptionRepo() {
16466
16137
  promoCode
16467
16138
  });
16468
16139
  if (error) {
16469
- throw new import_node_server_utils38.BadRequestError(error.message);
16140
+ throw new import_node_server_utils36.BadRequestError(error.message);
16470
16141
  }
16471
16142
  try {
16472
16143
  _id = new import_mongodb23.ObjectId(_id);
16473
16144
  } catch (error2) {
16474
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
16145
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16475
16146
  }
16476
16147
  const data = {
16477
16148
  currentSeats,
@@ -16490,33 +16161,33 @@ function useSubscriptionRepo() {
16490
16161
  { $set: data },
16491
16162
  { session }
16492
16163
  );
16493
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id });
16164
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id });
16494
16165
  delCache(cacheKey).then(() => {
16495
- import_node_server_utils38.logger.info(`Cache deleted for key: ${cacheKey}`);
16166
+ import_node_server_utils36.logger.info(`Cache deleted for key: ${cacheKey}`);
16496
16167
  }).catch((err) => {
16497
- import_node_server_utils38.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16168
+ import_node_server_utils36.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16498
16169
  });
16499
16170
  return result;
16500
16171
  } catch (error2) {
16501
- throw new import_node_server_utils38.BadRequestError("Failed to update subscription seats.");
16172
+ throw new import_node_server_utils36.BadRequestError("Failed to update subscription seats.");
16502
16173
  }
16503
16174
  }
16504
16175
  async function updateMaxSeatsById({
16505
16176
  _id,
16506
16177
  seats
16507
16178
  }, session) {
16508
- const schema2 = import_joi21.default.object({
16509
- _id: import_joi21.default.string().hex().required(),
16510
- seats: import_joi21.default.number().required().min(1)
16179
+ const schema2 = import_joi19.default.object({
16180
+ _id: import_joi19.default.string().hex().required(),
16181
+ seats: import_joi19.default.number().required().min(1)
16511
16182
  });
16512
16183
  const { error } = schema2.validate({ _id, seats });
16513
16184
  if (error) {
16514
- throw new import_node_server_utils38.BadRequestError(error.message);
16185
+ throw new import_node_server_utils36.BadRequestError(error.message);
16515
16186
  }
16516
16187
  try {
16517
16188
  _id = new import_mongodb23.ObjectId(_id);
16518
16189
  } catch (error2) {
16519
- throw new import_node_server_utils38.BadRequestError("Invalid subscription ID format.");
16190
+ throw new import_node_server_utils36.BadRequestError("Invalid subscription ID format.");
16520
16191
  }
16521
16192
  try {
16522
16193
  const result = await collection.updateOne(
@@ -16524,15 +16195,15 @@ function useSubscriptionRepo() {
16524
16195
  { $set: { maxSeats: seats } },
16525
16196
  { session }
16526
16197
  );
16527
- const cacheKey = (0, import_node_server_utils38.makeCacheKey)(namespace_collection, { _id });
16198
+ const cacheKey = (0, import_node_server_utils36.makeCacheKey)(namespace_collection, { _id });
16528
16199
  delCache(cacheKey).then(() => {
16529
- import_node_server_utils38.logger.info(`Cache deleted for key: ${cacheKey}`);
16200
+ import_node_server_utils36.logger.info(`Cache deleted for key: ${cacheKey}`);
16530
16201
  }).catch((err) => {
16531
- import_node_server_utils38.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16202
+ import_node_server_utils36.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
16532
16203
  });
16533
16204
  return result;
16534
16205
  } catch (error2) {
16535
- throw new import_node_server_utils38.BadRequestError("Failed to update subscription paid seats.");
16206
+ throw new import_node_server_utils36.BadRequestError("Failed to update subscription paid seats.");
16536
16207
  }
16537
16208
  }
16538
16209
  return {
@@ -16559,6 +16230,397 @@ function useSubscriptionRepo() {
16559
16230
  };
16560
16231
  }
16561
16232
 
16233
+ // src/controllers/organization.controller.ts
16234
+ function useOrgController() {
16235
+ const { getOrgsByMembership } = useMemberRepo();
16236
+ const {
16237
+ getByName: _getByName,
16238
+ getById: _getById,
16239
+ getByEmail: _getByEmail,
16240
+ getAll: _getAll,
16241
+ add: _add,
16242
+ update: _update,
16243
+ getOrgsByEmail: _getOrgsByEmail,
16244
+ getAdminOrgForResident: _getAdminOrgForResident,
16245
+ completeOnboardingById: _completeOnboardingById,
16246
+ updateStatusById: _updateStatusById
16247
+ } = useOrgRepo();
16248
+ const {
16249
+ getByOrgId: _getSubscriptionByOrgId,
16250
+ updateStatusById: _updateSubscriptionStatusById
16251
+ } = useSubscriptionRepo();
16252
+ async function add(req, res, next) {
16253
+ const validation = import_joi20.default.object({
16254
+ name: import_joi20.default.string().required(),
16255
+ type: import_joi20.default.string().required(),
16256
+ nature: import_joi20.default.string().valid(...allowedNatures).required(),
16257
+ email: import_joi20.default.string().email().optional().allow("", null),
16258
+ contact: import_joi20.default.string().optional().allow("", null),
16259
+ terms: import_joi20.default.string().optional().allow("", null),
16260
+ policies: import_joi20.default.string().optional().allow("", null)
16261
+ });
16262
+ const { error } = validation.validate(req.body);
16263
+ if (error) {
16264
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16265
+ next(new import_node_server_utils37.BadRequestError(error.message));
16266
+ return;
16267
+ }
16268
+ try {
16269
+ await _add(req.body);
16270
+ res.status(201).json({ message: "Successfully created organization." });
16271
+ return;
16272
+ } catch (error2) {
16273
+ import_node_server_utils37.logger.log({ level: "error", message: error2.message });
16274
+ next(error2);
16275
+ return;
16276
+ }
16277
+ }
16278
+ async function getAll(req, res, next) {
16279
+ const validation = import_joi20.default.object({
16280
+ search: import_joi20.default.string().optional().allow("", null),
16281
+ page: import_joi20.default.number().integer().min(1).allow("", null).default(1),
16282
+ limit: import_joi20.default.number().integer().min(1).max(100).allow("", null).default(10),
16283
+ nature: import_joi20.default.string().valid(...allowedNatures).optional().allow("", null),
16284
+ sort: import_joi20.default.string().optional().allow("", null)
16285
+ });
16286
+ const query = { ...req.query };
16287
+ const { error } = validation.validate(query);
16288
+ if (error) {
16289
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16290
+ next(new import_node_server_utils37.BadRequestError(error.message));
16291
+ return;
16292
+ }
16293
+ const search = req.query.search ?? "";
16294
+ const page = parseInt(req.query.page ?? "1");
16295
+ const limit = parseInt(req.query.limit ?? "10");
16296
+ const nature = req.query.nature ?? "";
16297
+ try {
16298
+ const data = await _getAll({
16299
+ search,
16300
+ page,
16301
+ limit,
16302
+ nature
16303
+ });
16304
+ res.json(data);
16305
+ return;
16306
+ } catch (error2) {
16307
+ import_node_server_utils37.logger.log({ level: "error", message: error2.message });
16308
+ next(error2);
16309
+ return;
16310
+ }
16311
+ }
16312
+ async function addOnboardingOrg(req, res, next) {
16313
+ const validation = import_joi20.default.object({
16314
+ name: import_joi20.default.string().required(),
16315
+ type: import_joi20.default.string().required(),
16316
+ nature: import_joi20.default.string().valid(...allowedNatures).required(),
16317
+ email: import_joi20.default.string().email().optional().allow("", null),
16318
+ contact: import_joi20.default.string().optional().allow("", null)
16319
+ });
16320
+ const { error } = validation.validate(req.body);
16321
+ if (error) {
16322
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16323
+ next(new import_node_server_utils37.BadRequestError(error.message));
16324
+ return;
16325
+ }
16326
+ try {
16327
+ const _id = await _add(req.body);
16328
+ const data = await _getById(_id);
16329
+ res.status(201).json({
16330
+ message: "Successfully created organization.",
16331
+ data
16332
+ });
16333
+ return;
16334
+ } catch (error2) {
16335
+ import_node_server_utils37.logger.log({ level: "error", message: error2.message });
16336
+ next(error2);
16337
+ return;
16338
+ }
16339
+ }
16340
+ async function getOrgsByUserId(req, res, next) {
16341
+ const validation = import_joi20.default.object({
16342
+ search: import_joi20.default.string().optional().allow("", null),
16343
+ page: import_joi20.default.number().integer().min(1).allow("", null).default(1),
16344
+ limit: import_joi20.default.number().integer().min(1).max(100).allow("", null).default(10),
16345
+ user: import_joi20.default.string().hex().required(),
16346
+ type: import_joi20.default.string().optional().allow("", null)
16347
+ });
16348
+ const query = { ...req.query };
16349
+ query.user = req.params.user;
16350
+ const { error } = validation.validate(query);
16351
+ if (error) {
16352
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16353
+ next(new import_node_server_utils37.BadRequestError(error.message));
16354
+ return;
16355
+ }
16356
+ const search = req.query.search ?? "";
16357
+ const page = parseInt(req.query.page ?? "1");
16358
+ const limit = parseInt(req.query.limit ?? "10");
16359
+ const user = req.params.user;
16360
+ const type = req.query.type ?? "";
16361
+ try {
16362
+ const data = await getOrgsByMembership({
16363
+ search,
16364
+ page,
16365
+ limit,
16366
+ user,
16367
+ type
16368
+ });
16369
+ res.json(data);
16370
+ return;
16371
+ } catch (error2) {
16372
+ import_node_server_utils37.logger.log({ level: "error", message: error2.message });
16373
+ next(error2);
16374
+ return;
16375
+ }
16376
+ }
16377
+ async function getByName(req, res, next) {
16378
+ const validation = import_joi20.default.string().required();
16379
+ const name = req.params.name;
16380
+ const { error } = validation.validate(name);
16381
+ if (error) {
16382
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16383
+ next(new import_node_server_utils37.BadRequestError(error.message));
16384
+ return;
16385
+ }
16386
+ try {
16387
+ const data = await _getByName(name);
16388
+ res.json(data);
16389
+ return;
16390
+ } catch (error2) {
16391
+ next(error2);
16392
+ return;
16393
+ }
16394
+ }
16395
+ async function getById(req, res, next) {
16396
+ const validation = import_joi20.default.string().hex().required();
16397
+ const _id = req.params.id;
16398
+ const { error } = validation.validate(_id);
16399
+ if (error) {
16400
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16401
+ next(new import_node_server_utils37.BadRequestError(error.message));
16402
+ return;
16403
+ }
16404
+ try {
16405
+ const data = await _getById(_id);
16406
+ res.json(data);
16407
+ return;
16408
+ } catch (error2) {
16409
+ import_node_server_utils37.logger.log({ level: "error", message: error2.message });
16410
+ next(error2);
16411
+ return;
16412
+ }
16413
+ }
16414
+ async function getByEmail(req, res, next) {
16415
+ const validation = import_joi20.default.string().required();
16416
+ const email = req.params.email;
16417
+ const { error } = validation.validate(email);
16418
+ if (error) {
16419
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16420
+ next(new import_node_server_utils37.BadRequestError(error.message));
16421
+ return;
16422
+ }
16423
+ try {
16424
+ const data = await _getByEmail(email);
16425
+ if (!data) {
16426
+ next(new import_node_server_utils37.NotFoundError("Organization not found."));
16427
+ return;
16428
+ }
16429
+ res.json(data);
16430
+ return;
16431
+ } catch (error2) {
16432
+ next(error2);
16433
+ return;
16434
+ }
16435
+ }
16436
+ async function getOrgsByEmail(req, res, next) {
16437
+ const validation = import_joi20.default.object({
16438
+ email: import_joi20.default.string().email().required()
16439
+ });
16440
+ const query = {
16441
+ email: req.params.email
16442
+ };
16443
+ const { error } = validation.validate(query);
16444
+ if (error) {
16445
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16446
+ next(new import_node_server_utils37.BadRequestError(error.message));
16447
+ return;
16448
+ }
16449
+ const email = req.params.email;
16450
+ try {
16451
+ const data = await _getOrgsByEmail(email);
16452
+ res.json(data);
16453
+ return;
16454
+ } catch (error2) {
16455
+ import_node_server_utils37.logger.log({ level: "error", message: error2.message });
16456
+ next(error2);
16457
+ return;
16458
+ }
16459
+ }
16460
+ async function update(req, res, next) {
16461
+ const validation = import_joi20.default.object({
16462
+ name: import_joi20.default.string().optional(),
16463
+ type: import_joi20.default.string().optional(),
16464
+ nature: import_joi20.default.string().valid(...allowedNatures).optional(),
16465
+ email: import_joi20.default.string().email().allow("", null).optional(),
16466
+ contact: import_joi20.default.string().allow("", null).optional(),
16467
+ terms: import_joi20.default.string().optional().allow("", null),
16468
+ policies: import_joi20.default.string().optional().allow("", null)
16469
+ });
16470
+ const { error } = validation.validate(req.body);
16471
+ if (error) {
16472
+ next(new import_node_server_utils37.BadRequestError(error.message));
16473
+ return;
16474
+ }
16475
+ const id = req.params.id;
16476
+ try {
16477
+ await _update(id, req.body);
16478
+ const data = await _getById(id);
16479
+ res.json({
16480
+ message: "Organization updated successfully",
16481
+ data
16482
+ });
16483
+ } catch (err) {
16484
+ next(err);
16485
+ }
16486
+ }
16487
+ async function getAdminOrgForResident(_req, res, next) {
16488
+ try {
16489
+ const data = await _getAdminOrgForResident();
16490
+ res.status(200).json(data);
16491
+ } catch (error) {
16492
+ import_node_server_utils37.logger.log({ level: "error", message: error.message });
16493
+ next(error);
16494
+ }
16495
+ }
16496
+ async function updateStatus(req, res, next) {
16497
+ const validation = import_joi20.default.object({
16498
+ status: import_joi20.default.string().valid("active", "suspended").required()
16499
+ });
16500
+ const { error } = validation.validate(req.body);
16501
+ if (error) {
16502
+ next(new import_node_server_utils37.BadRequestError(error.message));
16503
+ return;
16504
+ }
16505
+ const orgId = req.params.id;
16506
+ const { status } = req.body;
16507
+ try {
16508
+ await _updateStatusById(orgId, status);
16509
+ const subscription = await _getSubscriptionByOrgId(orgId);
16510
+ if (subscription) {
16511
+ await _updateSubscriptionStatusById(
16512
+ subscription._id,
16513
+ status
16514
+ );
16515
+ }
16516
+ const data = await _getById(orgId);
16517
+ res.json({
16518
+ message: "Organization status updated successfully.",
16519
+ data
16520
+ });
16521
+ } catch (err) {
16522
+ next(err);
16523
+ }
16524
+ }
16525
+ return {
16526
+ add,
16527
+ addOnboardingOrg,
16528
+ getAll,
16529
+ getOrgsByUserId,
16530
+ getByName,
16531
+ getById,
16532
+ getByEmail,
16533
+ update,
16534
+ getOrgsByEmail,
16535
+ getAdminOrgForResident,
16536
+ updateStatus
16537
+ };
16538
+ }
16539
+
16540
+ // src/controllers/organization-v2.controller.ts
16541
+ var import_joi21 = __toESM(require("joi"));
16542
+ var import_node_server_utils38 = require("@7365admin1/node-server-utils");
16543
+ function useOrgControllerV2() {
16544
+ const { getAll: _getAll, getOrganizationsWithSubscription: _getOrganizationsWithSubscription } = useOrgRepo();
16545
+ async function getAll(req, res, next) {
16546
+ const validation = import_joi21.default.object({
16547
+ search: import_joi21.default.string().optional().allow("", null),
16548
+ page: import_joi21.default.number().integer().min(1).allow("", null).default(1),
16549
+ limit: import_joi21.default.number().integer().min(1).max(100).allow("", null).default(10),
16550
+ nature: import_joi21.default.string().valid(...allowedNatures).optional().allow("", null),
16551
+ status: import_joi21.default.string().trim().valid("active", "suspended", "deleted").optional().empty("").default("active")
16552
+ });
16553
+ const query = { ...req.query };
16554
+ const { error, value } = validation.validate(query, {
16555
+ convert: true,
16556
+ stripUnknown: true
16557
+ });
16558
+ if (error) {
16559
+ import_node_server_utils38.logger.log({ level: "error", message: error.message });
16560
+ next(new import_node_server_utils38.BadRequestError(error.message));
16561
+ return;
16562
+ }
16563
+ const search = value.search ?? "";
16564
+ const page = typeof value.page === "number" ? value.page : parseInt(String(value.page ?? "1"), 10);
16565
+ const limit = typeof value.limit === "number" ? value.limit : parseInt(String(value.limit ?? "10"), 10);
16566
+ const nature = value.nature ?? "";
16567
+ const status = value.status;
16568
+ try {
16569
+ const data = await _getAll({
16570
+ search,
16571
+ page,
16572
+ limit,
16573
+ nature,
16574
+ status
16575
+ });
16576
+ res.json(data);
16577
+ return;
16578
+ } catch (error2) {
16579
+ import_node_server_utils38.logger.log({ level: "error", message: error2.message });
16580
+ next(error2);
16581
+ return;
16582
+ }
16583
+ }
16584
+ async function getOrganizationsWithSubscription(req, res, next) {
16585
+ const validation = import_joi21.default.object({
16586
+ search: import_joi21.default.string().optional().allow("", null),
16587
+ page: import_joi21.default.number().integer().min(1).default(1),
16588
+ limit: import_joi21.default.number().integer().min(1).max(100).default(10),
16589
+ status: import_joi21.default.string().trim().valid("active", "suspended", "deleted").default("active"),
16590
+ type: import_joi21.default.string().optional().allow("", null),
16591
+ billingCycle: import_joi21.default.string().optional().allow("", null)
16592
+ });
16593
+ const { error, value } = validation.validate(
16594
+ req.query,
16595
+ {
16596
+ convert: true,
16597
+ stripUnknown: true
16598
+ }
16599
+ );
16600
+ if (error) {
16601
+ next(new import_node_server_utils38.BadRequestError(error.message));
16602
+ return;
16603
+ }
16604
+ try {
16605
+ const data = await _getOrganizationsWithSubscription({
16606
+ search: value.search ?? "",
16607
+ page: value.page ?? 1,
16608
+ limit: value.limit ?? 10,
16609
+ status: value.status ?? "active",
16610
+ type: value.type ?? "",
16611
+ billingCycle: value.billingCycle ?? ""
16612
+ });
16613
+ res.json(data);
16614
+ } catch (error2) {
16615
+ next(error2);
16616
+ }
16617
+ }
16618
+ return {
16619
+ getAll,
16620
+ getOrganizationsWithSubscription
16621
+ };
16622
+ }
16623
+
16562
16624
  // src/services/subscription.service.ts
16563
16625
  var import_node_server_utils50 = require("@7365admin1/node-server-utils");
16564
16626
 
@@ -25607,6 +25669,7 @@ var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
25607
25669
  var BuildingStatus = /* @__PURE__ */ ((BuildingStatus2) => {
25608
25670
  BuildingStatus2["ACTIVE"] = "active";
25609
25671
  BuildingStatus2["PENDING"] = "pending";
25672
+ BuildingStatus2["DELETED"] = "deleted";
25610
25673
  return BuildingStatus2;
25611
25674
  })(BuildingStatus || {});
25612
25675
  var objectIdSchema = import_joi41.default.alternatives().try(
@@ -26967,12 +27030,6 @@ function useVehicleService() {
26967
27030
  const org = await _getById(orgId);
26968
27031
  if (!org)
26969
27032
  throw new import_node_server_utils77.BadRequestError("Org not found");
26970
- const allowedNatures2 = "property_management_agency" /* PROPERTY_MANAGEMENT_AGENCY */;
26971
- if (!allowedNatures2.includes(org.nature)) {
26972
- throw new import_node_server_utils77.BadRequestError(
26973
- "Only property management can approve vehicles."
26974
- );
26975
- }
26976
27033
  const vehicle = await _getVehicleById(id);
26977
27034
  const plate = vehicle.plates.find((p) => p._id.toString() === id);
26978
27035
  const _plateNumber = plate?.plateNumber;
@@ -28181,6 +28238,21 @@ function useBuildingRepo() {
28181
28238
  );
28182
28239
  async function createIndexes() {
28183
28240
  try {
28241
+ const indexes = await collection.indexes();
28242
+ const legacyUniqueIndexKeys = [
28243
+ JSON.stringify({ name: 1 }),
28244
+ JSON.stringify({ site: 1, name: 1 }),
28245
+ JSON.stringify({ site: 1, block: 1 })
28246
+ ];
28247
+ await Promise.all(
28248
+ indexes.map(async (index) => {
28249
+ const key = JSON.stringify(index.key);
28250
+ const isLegacyUniqueIndex = index.unique && legacyUniqueIndexKeys.includes(key) && index.partialFilterExpression?.status !== "active" /* ACTIVE */;
28251
+ if (index.name && isLegacyUniqueIndex) {
28252
+ await collection.dropIndex(index.name);
28253
+ }
28254
+ })
28255
+ );
28184
28256
  await collection.createIndexes([
28185
28257
  { key: { name: "text" }, name: "text-index" },
28186
28258
  // { key: { name: 1 }, unique: true, name: "unique-name-index" },
@@ -28194,6 +28266,14 @@ function useBuildingRepo() {
28194
28266
  partialFilterExpression: {
28195
28267
  status: "active" /* ACTIVE */
28196
28268
  }
28269
+ },
28270
+ {
28271
+ key: { site: 1, name: 1 },
28272
+ unique: true,
28273
+ name: "unique-site-name-active",
28274
+ partialFilterExpression: {
28275
+ status: "active" /* ACTIVE */
28276
+ }
28197
28277
  }
28198
28278
  ]);
28199
28279
  } catch (error) {
@@ -28201,9 +28281,45 @@ function useBuildingRepo() {
28201
28281
  }
28202
28282
  }
28203
28283
  const { getBySiteBuildingLevel: _getBySiteBuildingLevel } = useBuildingUnitRepo();
28284
+ async function assertBuildingDoesNotExist({
28285
+ site,
28286
+ name,
28287
+ block,
28288
+ excludeId,
28289
+ session
28290
+ }) {
28291
+ const duplicates = [];
28292
+ if (name) {
28293
+ duplicates.push({ name });
28294
+ }
28295
+ if (block) {
28296
+ duplicates.push({ block });
28297
+ }
28298
+ if (!duplicates.length) {
28299
+ return;
28300
+ }
28301
+ const existing = await collection.findOne(
28302
+ {
28303
+ site,
28304
+ status: "active" /* ACTIVE */,
28305
+ ...excludeId && { _id: { $ne: excludeId } },
28306
+ $or: duplicates
28307
+ },
28308
+ { session }
28309
+ );
28310
+ if (existing) {
28311
+ throw new import_node_server_utils83.BadRequestError("Building already exists.");
28312
+ }
28313
+ }
28204
28314
  async function add(value, session) {
28205
28315
  try {
28206
28316
  value = MBuilding(value);
28317
+ await assertBuildingDoesNotExist({
28318
+ site: value.site,
28319
+ name: value.name,
28320
+ block: value.block,
28321
+ session
28322
+ });
28207
28323
  const res = await collection.insertOne(value, { session });
28208
28324
  delCachedData();
28209
28325
  return res.insertedId;
@@ -28242,6 +28358,21 @@ function useBuildingRepo() {
28242
28358
  throw new import_node_server_utils83.BadRequestError("Invalid level ID format.");
28243
28359
  }
28244
28360
  }
28361
+ if (value.name || value.block) {
28362
+ const currentBuilding = await collection.findOne(
28363
+ { _id },
28364
+ { session }
28365
+ );
28366
+ if (currentBuilding) {
28367
+ await assertBuildingDoesNotExist({
28368
+ site: currentBuilding.site,
28369
+ name: value.name,
28370
+ block: value.block,
28371
+ excludeId: _id,
28372
+ session
28373
+ });
28374
+ }
28375
+ }
28245
28376
  if (value.name)
28246
28377
  await buildingUnitCollection.updateMany(
28247
28378
  { building: _id, status: "active" /* ACTIVE */ },
@@ -28471,7 +28602,8 @@ function useBuildingRepo() {
28471
28602
  try {
28472
28603
  const res = await collection.updateOne(
28473
28604
  { _id },
28474
- { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } }
28605
+ { $set: { status: "deleted" /* DELETED */, deletedAt: /* @__PURE__ */ new Date() } },
28606
+ { session }
28475
28607
  );
28476
28608
  delCachedData();
28477
28609
  return res;
@@ -29365,18 +29497,19 @@ function useBuildingService() {
29365
29497
  }
29366
29498
  }
29367
29499
  const buildingUnitData = {};
29368
- if (building.name !== data.name) {
29500
+ if (data.name && building.name !== data.name) {
29369
29501
  buildingUnitData.buildingName = data.name;
29370
29502
  }
29371
- if (building.block !== data.block) {
29503
+ if (data.block && building.block !== data.block) {
29372
29504
  buildingUnitData.block = data.block;
29373
29505
  }
29374
- await updateByBuildingId(id, buildingUnitData, session);
29375
29506
  if (building.name === data.name) {
29376
29507
  delete data.name;
29377
29508
  }
29378
- console.log("data", data);
29379
29509
  const result = await _updateById(id, data, session);
29510
+ if (Object.keys(buildingUnitData).length) {
29511
+ await updateByBuildingId(id, buildingUnitData, session);
29512
+ }
29380
29513
  await session.commitTransaction();
29381
29514
  return result;
29382
29515
  } catch (error) {
@@ -57193,7 +57326,7 @@ function useNewDashboardRepo() {
57193
57326
  const periodRange = getDateRange(period);
57194
57327
  const incidentCollection = db.collection(incidents_namespace_collection);
57195
57328
  const visitorCollection = db.collection(visitors_namespace_collection);
57196
- const nfcPatrolLogCollection = db.collection("nfc-patrol-logs");
57329
+ const patrolLogCollection = db.collection("patrol.logs");
57197
57330
  const [
57198
57331
  workOrderReport,
57199
57332
  yesterdayWorkOrderReport,
@@ -57320,7 +57453,7 @@ function useNewDashboardRepo() {
57320
57453
  checkOut: null,
57321
57454
  status: { $ne: "deleted" }
57322
57455
  }),
57323
- nfcPatrolLogCollection.aggregate([
57456
+ patrolLogCollection.aggregate([
57324
57457
  {
57325
57458
  $match: {
57326
57459
  site: { $in: [siteIdObj, siteId] },
@@ -57328,23 +57461,23 @@ function useNewDashboardRepo() {
57328
57461
  }
57329
57462
  },
57330
57463
  {
57331
- $unwind: "$checkPoints"
57464
+ $unwind: "$cameras"
57332
57465
  },
57333
57466
  {
57334
57467
  $facet: {
57335
57468
  total: [{ $count: "count" }],
57336
57469
  completed: [
57337
- { $match: { "checkPoints.status": "Completed" } },
57470
+ { $match: { "cameras.status": "Completed" } },
57338
57471
  { $count: "count" }
57339
57472
  ],
57340
57473
  skipped: [
57341
- { $match: { "checkPoints.status": "Skipped" } },
57474
+ { $match: { "cameras.status": "Skipped" } },
57342
57475
  { $count: "count" }
57343
57476
  ]
57344
57477
  }
57345
57478
  }
57346
57479
  ]).toArray(),
57347
- nfcPatrolLogCollection.aggregate([
57480
+ patrolLogCollection.aggregate([
57348
57481
  {
57349
57482
  $match: {
57350
57483
  site: { $in: [siteIdObj, siteId] },
@@ -57352,23 +57485,23 @@ function useNewDashboardRepo() {
57352
57485
  }
57353
57486
  },
57354
57487
  {
57355
- $unwind: "$checkPoints"
57488
+ $unwind: "$cameras"
57356
57489
  },
57357
57490
  {
57358
57491
  $facet: {
57359
57492
  total: [{ $count: "count" }],
57360
57493
  completed: [
57361
- { $match: { "checkPoints.status": "Completed" } },
57494
+ { $match: { "cameras.status": "Completed" } },
57362
57495
  { $count: "count" }
57363
57496
  ],
57364
57497
  skipped: [
57365
- { $match: { "checkPoints.status": "Skipped" } },
57498
+ { $match: { "cameras.status": "Skipped" } },
57366
57499
  { $count: "count" }
57367
57500
  ]
57368
57501
  }
57369
57502
  }
57370
57503
  ]).toArray(),
57371
- nfcPatrolLogCollection.aggregate([
57504
+ patrolLogCollection.aggregate([
57372
57505
  {
57373
57506
  $match: {
57374
57507
  site: { $in: [siteIdObj, siteId] },
@@ -57376,17 +57509,17 @@ function useNewDashboardRepo() {
57376
57509
  }
57377
57510
  },
57378
57511
  {
57379
- $unwind: "$checkPoints"
57512
+ $unwind: "$cameras"
57380
57513
  },
57381
57514
  {
57382
57515
  $facet: {
57383
57516
  total: [{ $count: "count" }],
57384
57517
  completed: [
57385
- { $match: { "checkPoints.status": "Completed" } },
57518
+ { $match: { "cameras.status": "Completed" } },
57386
57519
  { $count: "count" }
57387
57520
  ],
57388
57521
  skipped: [
57389
- { $match: { "checkPoints.status": "Skipped" } },
57522
+ { $match: { "cameras.status": "Skipped" } },
57390
57523
  { $count: "count" }
57391
57524
  ]
57392
57525
  }
@@ -57417,78 +57550,127 @@ function useNewDashboardRepo() {
57417
57550
  const tTotal = tPatrolFacet.total[0]?.count ?? 0;
57418
57551
  const tCompleted = tPatrolFacet.completed[0]?.count ?? 0;
57419
57552
  const todayCompliance = tTotal > 0 ? tCompleted / tTotal * 100 : 0;
57420
- const todayString = import_moment.default.tz("Asia/Singapore").format("YYYY-MM-DD");
57421
- const dayIndex = import_moment.default.tz("Asia/Singapore").day();
57422
- const routes = await db.collection("nfc-patrol-routes").find({
57423
- site: { $in: [siteIdObj, siteId] },
57424
- days: { $in: [dayIndex] },
57425
- status: { $ne: "Inactive" }
57426
- }).toArray();
57427
- const expandedRoutes = [];
57428
- for (const route of routes) {
57429
- if (route.startTimes && Array.isArray(route.startTimes)) {
57430
- for (const startTime of route.startTimes) {
57553
+ let activePatrolItems = [];
57554
+ if (period === "today" /* TODAY */) {
57555
+ const todayString = import_moment.default.tz("Asia/Singapore").format("YYYY-MM-DD");
57556
+ const dayIndex = import_moment.default.tz("Asia/Singapore").day();
57557
+ const repeatDay = dayIndex === 0 ? 7 : dayIndex;
57558
+ const routes = await db.collection("patrol.route").find({
57559
+ site: { $in: [siteIdObj, siteId] },
57560
+ repeat: { $in: [repeatDay, String(repeatDay)] },
57561
+ status: { $ne: "deleted" }
57562
+ }).toArray();
57563
+ const expandedRoutes = [];
57564
+ for (const route of routes) {
57565
+ if (route.start) {
57431
57566
  expandedRoutes.push({
57432
57567
  route,
57433
- startTime,
57568
+ startTime: route.start,
57434
57569
  itemIndex: 0
57435
57570
  });
57436
57571
  }
57437
57572
  }
57438
- }
57439
- expandedRoutes.sort((a, b) => a.startTime.localeCompare(b.startTime));
57440
- const limitedRoutes = expandedRoutes.slice(0, 4);
57441
- limitedRoutes.forEach((item, idx) => {
57442
- item.itemIndex = idx + 1;
57443
- });
57444
- const activePatrolItems = await Promise.all(
57445
- limitedRoutes.map(async ({ route, startTime, itemIndex }) => {
57446
- const log = await db.collection("nfc-patrol-logs").findOne({
57447
- site: { $in: [siteIdObj, siteId] },
57448
- date: todayString,
57449
- "route._id": { $in: [route._id, route._id.toString()] },
57450
- "route.startTime": startTime
57451
- });
57452
- let person = "Unassigned";
57453
- if (log && log.createdBy) {
57454
- const userDoc = await db.collection("users").findOne({
57455
- _id: (0, import_node_server_utils199.toObjectId)(log.createdBy)
57573
+ expandedRoutes.sort((a, b) => a.startTime.localeCompare(b.startTime));
57574
+ const limitedRoutes = expandedRoutes.slice(0, 4);
57575
+ limitedRoutes.forEach((item, idx) => {
57576
+ item.itemIndex = idx + 1;
57577
+ });
57578
+ activePatrolItems = await Promise.all(
57579
+ limitedRoutes.map(async ({ route, startTime, itemIndex }) => {
57580
+ const log = await db.collection("patrol.logs").findOne({
57581
+ site: { $in: [siteIdObj, siteId] },
57582
+ route: { $in: [route._id, route._id.toString()] },
57583
+ createdAt: { $gte: today, $lte: todayEnd }
57456
57584
  });
57457
- if (userDoc && userDoc.name) {
57458
- person = userDoc.name;
57585
+ let person = "Unassigned";
57586
+ if (log && log.assignee && log.assignee.length > 0) {
57587
+ const userIds = log.assignee.map((id) => (0, import_node_server_utils199.toObjectId)(id));
57588
+ const userDocs = await db.collection("members").find({ _id: { $in: userIds } }).toArray();
57589
+ if (userDocs && userDocs.length > 0) {
57590
+ person = userDocs.map((u) => u.name || u.email || "").filter(Boolean).join(", ");
57591
+ }
57592
+ } else if (log && log.createdBy) {
57593
+ const userDoc = await db.collection("members").findOne({
57594
+ _id: (0, import_node_server_utils199.toObjectId)(log.createdBy)
57595
+ });
57596
+ if (userDoc && userDoc.name) {
57597
+ person = userDoc.name;
57598
+ }
57459
57599
  }
57460
- }
57461
- let status = "pending";
57462
- if (log) {
57463
- status = "completed";
57464
- } else {
57465
- const routeTime = import_moment.default.tz(
57466
- `${todayString} ${startTime}`,
57467
- "YYYY-MM-DD HH:mm",
57468
- "Asia/Singapore"
57469
- );
57470
- const nowSg = import_moment.default.tz("Asia/Singapore");
57471
- const diffMinutes = nowSg.diff(routeTime, "minutes");
57472
- if (diffMinutes >= 0) {
57473
- if (diffMinutes <= 120) {
57474
- status = "on paused";
57600
+ let status = "pending";
57601
+ if (log) {
57602
+ if (log.status) {
57603
+ status = Array.isArray(log.status) ? log.status.join(", ") : log.status;
57475
57604
  } else {
57476
- status = "incomplete";
57605
+ status = "completed";
57477
57606
  }
57478
57607
  } else {
57479
- status = "pending";
57608
+ const routeTime = import_moment.default.tz(
57609
+ `${todayString} ${startTime}`,
57610
+ "YYYY-MM-DD HH:mm",
57611
+ "Asia/Singapore"
57612
+ );
57613
+ const nowSg = import_moment.default.tz("Asia/Singapore");
57614
+ const diffMinutes = nowSg.diff(routeTime, "minutes");
57615
+ if (diffMinutes >= 0) {
57616
+ if (diffMinutes <= 120) {
57617
+ status = "on paused";
57618
+ } else {
57619
+ status = "incomplete";
57620
+ }
57621
+ } else {
57622
+ status = "pending";
57623
+ }
57480
57624
  }
57481
- }
57482
- return {
57483
- id: `${route._id.toString()}_${startTime}`,
57484
- title: route.name,
57485
- subtitle: `ID${String(itemIndex).padStart(3, "0")}`,
57486
- person,
57487
- status,
57488
- time: startTime
57489
- };
57490
- })
57491
- );
57625
+ return {
57626
+ id: `${route._id.toString()}_${startTime}`,
57627
+ title: route.name,
57628
+ subtitle: `ID${String(itemIndex).padStart(3, "0")}`,
57629
+ person,
57630
+ status,
57631
+ time: startTime
57632
+ };
57633
+ })
57634
+ );
57635
+ } else {
57636
+ const logs = await db.collection("patrol.logs").find({
57637
+ site: { $in: [siteIdObj, siteId] },
57638
+ createdAt: periodRange
57639
+ }).sort({ createdAt: -1 }).limit(4).toArray();
57640
+ activePatrolItems = await Promise.all(
57641
+ logs.map(async (log) => {
57642
+ let person = "Unassigned";
57643
+ if (log.assignee && log.assignee.length > 0) {
57644
+ const userIds = log.assignee.map((id) => (0, import_node_server_utils199.toObjectId)(id));
57645
+ const userDocs = await db.collection("members").find({ _id: { $in: userIds } }).toArray();
57646
+ if (userDocs && userDocs.length > 0) {
57647
+ person = userDocs.map((u) => u.name || u.email || "").filter(Boolean).join(", ");
57648
+ }
57649
+ } else if (log.createdBy) {
57650
+ const userDoc = await db.collection("members").findOne({
57651
+ _id: (0, import_node_server_utils199.toObjectId)(log.createdBy)
57652
+ });
57653
+ if (userDoc && userDoc.name) {
57654
+ person = userDoc.name;
57655
+ }
57656
+ }
57657
+ let status = "completed";
57658
+ if (log.status) {
57659
+ status = Array.isArray(log.status) ? log.status.join(", ") : log.status;
57660
+ }
57661
+ const logTime = (0, import_moment.default)(log.createdAt).tz("Asia/Singapore").format("HH:mm");
57662
+ const logDate = (0, import_moment.default)(log.createdAt).tz("Asia/Singapore").format("DD/MM/YYYY");
57663
+ return {
57664
+ id: log._id.toString(),
57665
+ title: log.name,
57666
+ subtitle: logDate,
57667
+ person,
57668
+ status,
57669
+ time: logTime
57670
+ };
57671
+ })
57672
+ );
57673
+ }
57492
57674
  const data = {
57493
57675
  openWorkOrder: {
57494
57676
  count: wFacet.total[0]?.count ?? 0,
@@ -65349,6 +65531,74 @@ function usePostPrelovedRepo() {
65349
65531
  throw error;
65350
65532
  }
65351
65533
  }
65534
+ async function getByOwnerId(ownerId, {
65535
+ search = "",
65536
+ page = 1,
65537
+ limit = 10,
65538
+ filter,
65539
+ site,
65540
+ status,
65541
+ category
65542
+ }, session) {
65543
+ page = page > 0 ? page - 1 : 0;
65544
+ let ownerObjectId;
65545
+ try {
65546
+ ownerObjectId = new import_mongodb137.ObjectId(ownerId);
65547
+ } catch {
65548
+ throw new import_node_server_utils235.BadRequestError("Invalid user ID format.");
65549
+ }
65550
+ if (site) {
65551
+ try {
65552
+ site = new import_mongodb137.ObjectId(site);
65553
+ } catch {
65554
+ throw new import_node_server_utils235.BadRequestError("Invalid site ID format.");
65555
+ }
65556
+ }
65557
+ let categoryIds = null;
65558
+ if (Array.isArray(category) && category.length > 0) {
65559
+ categoryIds = category.map((cat) => {
65560
+ try {
65561
+ return new import_mongodb137.ObjectId(cat);
65562
+ } catch {
65563
+ throw new import_node_server_utils235.BadRequestError("Invalid category ID format.");
65564
+ }
65565
+ });
65566
+ }
65567
+ const query = {
65568
+ status: { $ne: "deleted" /* DELETED */ },
65569
+ createdBy: ownerObjectId,
65570
+ ...site && { site },
65571
+ ...search && {
65572
+ $or: [
65573
+ { title: { $regex: search, $options: "i" } },
65574
+ { description: { $regex: search, $options: "i" } }
65575
+ ]
65576
+ },
65577
+ ...status && {
65578
+ status: { $in: Array.isArray(status) ? status : [status] }
65579
+ },
65580
+ ...categoryIds && { category: { $in: categoryIds } }
65581
+ };
65582
+ const sortObj = buildSortObj(filter);
65583
+ try {
65584
+ const items = await collection.aggregate(
65585
+ [
65586
+ { $match: query },
65587
+ USER_LOOKUP,
65588
+ USER_UNWIND,
65589
+ CATEGORY_LOOKUP,
65590
+ { $sort: sortObj },
65591
+ { $skip: page * limit },
65592
+ { $limit: limit }
65593
+ ],
65594
+ { session }
65595
+ ).toArray();
65596
+ const length = await collection.countDocuments(query, { session });
65597
+ return (0, import_node_server_utils235.paginate)(items, page, limit, length);
65598
+ } catch (error) {
65599
+ throw error;
65600
+ }
65601
+ }
65352
65602
  async function updateById(_id, value, session) {
65353
65603
  try {
65354
65604
  _id = new import_mongodb137.ObjectId(_id);
@@ -65419,6 +65669,7 @@ function usePostPrelovedRepo() {
65419
65669
  add,
65420
65670
  getById,
65421
65671
  getAll,
65672
+ getByOwnerId,
65422
65673
  updateById,
65423
65674
  deleteById,
65424
65675
  updateStatus
@@ -65628,6 +65879,7 @@ function usePostPrelovedController() {
65628
65879
  const {
65629
65880
  getById: _getById,
65630
65881
  getAll: _getAll,
65882
+ getByOwnerId: _getByOwnerId,
65631
65883
  updateById: _updateById,
65632
65884
  deleteById: _deleteById,
65633
65885
  updateStatus: _updateStatus
@@ -65731,6 +65983,49 @@ function usePostPrelovedController() {
65731
65983
  return;
65732
65984
  }
65733
65985
  }
65986
+ async function getByOwnerId(req, res, next) {
65987
+ const validation = import_joi135.default.object({
65988
+ ownerId: import_joi135.default.string().hex().length(24).required(),
65989
+ page: import_joi135.default.number().integer().min(1).allow("", null).default(1),
65990
+ limit: import_joi135.default.number().integer().min(1).max(100).allow("", null).default(10),
65991
+ search: import_joi135.default.string().optional().allow("", null),
65992
+ filter: import_joi135.default.string().valid("recent", "price-low-high", "price-high-low").optional().allow("", null),
65993
+ site: import_joi135.default.string().hex().length(24).optional().allow("", null),
65994
+ status: import_joi135.default.alternatives().try(
65995
+ import_joi135.default.array().items(import_joi135.default.string().valid(...Object.values(PostStatus))),
65996
+ import_joi135.default.string().valid(...Object.values(PostStatus))
65997
+ ).optional().allow(null),
65998
+ category: import_joi135.default.array().items(import_joi135.default.string().hex().length(24)).single().optional().allow(null)
65999
+ });
66000
+ const { error, value } = validation.validate(
66001
+ { ...req.query, ownerId: req.params.ownerId },
66002
+ { abortEarly: false }
66003
+ );
66004
+ if (error) {
66005
+ const messages = error.details.map((d) => d.message).join(", ");
66006
+ import_node_server_utils238.logger.log({ level: "error", message: messages });
66007
+ next(new import_node_server_utils238.BadRequestError(messages));
66008
+ return;
66009
+ }
66010
+ const { ownerId, page, limit, search, filter, site, status, category } = value;
66011
+ try {
66012
+ const data = await _getByOwnerId(ownerId, {
66013
+ page,
66014
+ limit,
66015
+ search,
66016
+ filter,
66017
+ site,
66018
+ status: status ? Array.isArray(status) ? status : [status] : void 0,
66019
+ category: category ?? void 0
66020
+ });
66021
+ res.status(200).json(data);
66022
+ return;
66023
+ } catch (error2) {
66024
+ import_node_server_utils238.logger.log({ level: "error", message: error2.message });
66025
+ next(error2);
66026
+ return;
66027
+ }
66028
+ }
65734
66029
  async function updateById(req, res, next) {
65735
66030
  const _id = req.params.id;
65736
66031
  const payload = { _id, ...req.body };
@@ -65801,6 +66096,7 @@ function usePostPrelovedController() {
65801
66096
  add,
65802
66097
  getById,
65803
66098
  getAll,
66099
+ getByOwnerId,
65804
66100
  updateById,
65805
66101
  deleteById,
65806
66102
  updateStatus