@goweekdays/core 0.0.21 → 0.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @goweekdays/core
2
2
 
3
+ ## 0.0.22
4
+
5
+ ### Patch Changes
6
+
7
+ - cf1bf01: add invoice payment
8
+
3
9
  ## 0.0.21
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -979,6 +979,7 @@ declare function useSubscriptionService(): {
979
979
  seats: number;
980
980
  amount: number;
981
981
  }) => Promise<void>;
982
+ processSubscriptionPayment: (invoiceNumber: string, subscriptionId: string) => Promise<void>;
982
983
  };
983
984
 
984
985
  declare function useSubscriptionController(): {
@@ -998,6 +999,7 @@ declare function useSubscriptionController(): {
998
999
  addBillingContactById: (req: Request, res: Response, next: NextFunction) => Promise<void>;
999
1000
  updateBillingContactByAddedAt: (req: Request, res: Response, next: NextFunction) => Promise<void>;
1000
1001
  deleteBillingContactByAddedAt: (req: Request, res: Response, next: NextFunction) => Promise<void>;
1002
+ processSubscriptionPayment: (req: Request, res: Response, next: NextFunction) => Promise<void>;
1001
1003
  };
1002
1004
 
1003
1005
  declare const CustomerSchema: z.ZodEffects<z.ZodObject<{
package/dist/index.js CHANGED
@@ -21492,6 +21492,7 @@ function useSubscriptionService() {
21492
21492
  const { add: addPayment } = usePaymentRepo();
21493
21493
  const { getByType, incrementByType } = useCounterRepo();
21494
21494
  const { getByNameType } = usePriceRepo();
21495
+ const { getByNumber } = useInvoiceRepo();
21495
21496
  function calculateTieredPricing(seats, tiers, prorationFactor = 1) {
21496
21497
  let totalCost = 0;
21497
21498
  let nonProratedCost = 0;
@@ -22371,13 +22372,115 @@ function useSubscriptionService() {
22371
22372
  session?.endSession();
22372
22373
  }
22373
22374
  }
22375
+ async function processSubscriptionPayment(invoiceNumber, subscriptionId) {
22376
+ const invoice = await getByNumber(invoiceNumber);
22377
+ if (!invoice) {
22378
+ throw new import_utils69.BadRequestError("Invoice not found.");
22379
+ }
22380
+ if (invoice.status === "paid") {
22381
+ throw new import_utils69.BadRequestError("Invoice already paid.");
22382
+ }
22383
+ if (!invoice._id) {
22384
+ throw new import_utils69.BadRequestError("Invoice ID is missing.");
22385
+ }
22386
+ if (!invoice.metadata?.subscriptionId) {
22387
+ throw new import_utils69.BadRequestError("Subscription ID is missing.");
22388
+ }
22389
+ if (invoice.metadata.subscriptionId.toString() !== subscriptionId) {
22390
+ throw new import_utils69.BadRequestError("Subscription ID does not match invoice.");
22391
+ }
22392
+ const subscription = await _getById(invoice.metadata.subscriptionId);
22393
+ if (!subscription) {
22394
+ throw new import_utils69.BadRequestError("Subscription not found.");
22395
+ }
22396
+ if (!subscription._id) {
22397
+ throw new import_utils69.BadRequestError("Subscription ID is missing.");
22398
+ }
22399
+ if (!subscription.customerId) {
22400
+ throw new import_utils69.BadRequestError("Customer ID is missing.");
22401
+ }
22402
+ if (!subscription.paymentMethodId) {
22403
+ throw new import_utils69.BadRequestError("Payment method ID is missing.");
22404
+ }
22405
+ if (!subscription.amount) {
22406
+ throw new import_utils69.BadRequestError("Subscription amount is missing.");
22407
+ }
22408
+ if (!subscription.currency) {
22409
+ throw new import_utils69.BadRequestError("Subscription currency is missing.");
22410
+ }
22411
+ const session = import_utils69.useAtlas.getClient()?.startSession();
22412
+ try {
22413
+ session?.startTransaction();
22414
+ const payment = await pay({
22415
+ customer_id: subscription.customerId,
22416
+ payment_method_id: subscription.paymentMethodId,
22417
+ amount: subscription.amount,
22418
+ currency: subscription.currency,
22419
+ description: "GoWeekdays Subscription Payment"
22420
+ });
22421
+ if (!payment.id) {
22422
+ throw new import_utils69.BadRequestError("Failed to process payment.");
22423
+ }
22424
+ const paymentMethod = await getPaymentMethodById(
22425
+ subscription.paymentMethodId
22426
+ );
22427
+ if (!paymentMethod) {
22428
+ throw new import_utils69.BadRequestError("Payment method not found.");
22429
+ }
22430
+ const paymentData = {
22431
+ invoiceId: invoiceNumber,
22432
+ amount: subscription.amount,
22433
+ paymentMethod: paymentMethod.type,
22434
+ status: "completed",
22435
+ metadata: {
22436
+ userId: String(subscription.user),
22437
+ orgId: String(subscription.org),
22438
+ currency: subscription.currency,
22439
+ paymentMethod: paymentMethod.type === "EWALLET" ? paymentMethod.ewallet.channel_code : paymentMethod.type === "DIRECT_DEBIT" ? paymentMethod.direct_debit.channel_code : "",
22440
+ paymentMethodType: paymentMethod.type,
22441
+ paymentMethodId: subscription.paymentMethodId,
22442
+ payment: payment.id,
22443
+ subscriptionId: subscription._id.toString()
22444
+ }
22445
+ };
22446
+ if (!paymentData.metadata.userId) {
22447
+ delete paymentData.metadata.userId;
22448
+ }
22449
+ if (!paymentData.metadata.orgId) {
22450
+ delete paymentData.metadata.orgId;
22451
+ }
22452
+ await addPayment(paymentData, session);
22453
+ await updateStatusByInvoiceNumber(invoiceNumber, "paid", session);
22454
+ await processSuccessfulPayment(
22455
+ {
22456
+ _id: subscription._id.toString(),
22457
+ nextBillingDate: subscription.nextBillingDate
22458
+ },
22459
+ session
22460
+ );
22461
+ await session?.commitTransaction();
22462
+ } catch (error) {
22463
+ await session?.abortTransaction();
22464
+ import_utils69.logger.log({
22465
+ level: "error",
22466
+ message: `Failed to process subscription payment: ${error}`
22467
+ });
22468
+ if (error instanceof import_utils69.AppError) {
22469
+ throw error;
22470
+ }
22471
+ throw new import_utils69.BadRequestError("Failed to process subscription payment.");
22472
+ } finally {
22473
+ session?.endSession();
22474
+ }
22475
+ }
22374
22476
  return {
22375
22477
  getByUserId,
22376
22478
  getStatusByUser,
22377
22479
  createAffiliateSubscription,
22378
22480
  createOrgSubscription,
22379
22481
  processSubscriptions,
22380
- updateSeatsById
22482
+ updateSeatsById,
22483
+ processSubscriptionPayment
22381
22484
  };
22382
22485
  }
22383
22486
 
@@ -22445,7 +22548,8 @@ function useSubscriptionController() {
22445
22548
  getStatusByUser: _getStatusByUser,
22446
22549
  createAffiliateSubscription: _createAffiliateSubscription,
22447
22550
  createOrgSubscription: _createOrgSubscription,
22448
- updateSeatsById: _updateSubscriptionSeats
22551
+ updateSeatsById: _updateSubscriptionSeats,
22552
+ processSubscriptionPayment: _processSubscriptionPayment
22449
22553
  } = useSubscriptionService();
22450
22554
  async function add(req, res, next) {
22451
22555
  const value = req.body;
@@ -22744,6 +22848,26 @@ function useSubscriptionController() {
22744
22848
  return;
22745
22849
  }
22746
22850
  }
22851
+ async function processSubscriptionPayment(req, res, next) {
22852
+ const id = req.params.id ?? "";
22853
+ const invoiceNumber = req.body.invoice ?? "";
22854
+ const { error } = import_joi20.default.object({
22855
+ id: import_joi20.default.string().hex().required(),
22856
+ invoiceNumber: import_joi20.default.string().required()
22857
+ }).validate({ id, invoiceNumber });
22858
+ if (error) {
22859
+ next(new import_utils70.BadRequestError(error.message));
22860
+ return;
22861
+ }
22862
+ try {
22863
+ const payment = await _processSubscriptionPayment(invoiceNumber, id);
22864
+ res.json(payment);
22865
+ return;
22866
+ } catch (error2) {
22867
+ next(error2);
22868
+ return;
22869
+ }
22870
+ }
22747
22871
  return {
22748
22872
  add,
22749
22873
  getByUserId,
@@ -22760,7 +22884,8 @@ function useSubscriptionController() {
22760
22884
  updatePaymentMethodById,
22761
22885
  addBillingContactById,
22762
22886
  updateBillingContactByAddedAt,
22763
- deleteBillingContactByAddedAt
22887
+ deleteBillingContactByAddedAt,
22888
+ processSubscriptionPayment
22764
22889
  };
22765
22890
  }
22766
22891
 
@@ -23987,7 +24112,7 @@ function useOrderController() {
23987
24112
  // src/services/invoice.service.ts
23988
24113
  var import_utils83 = require("@goweekdays/utils");
23989
24114
  function useInvoiceService() {
23990
- const { getOverdueInvoices, updateStatusByInvoiceNumber } = useInvoiceRepo();
24115
+ const { getOverdueInvoices, updateStatusByInvoiceNumber, getByNumber } = useInvoiceRepo();
23991
24116
  const {
23992
24117
  getById,
23993
24118
  processSuccessfulPayment,