@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/dist/index.mjs CHANGED
@@ -21489,6 +21489,7 @@ function useSubscriptionService() {
21489
21489
  const { add: addPayment } = usePaymentRepo();
21490
21490
  const { getByType, incrementByType } = useCounterRepo();
21491
21491
  const { getByNameType } = usePriceRepo();
21492
+ const { getByNumber } = useInvoiceRepo();
21492
21493
  function calculateTieredPricing(seats, tiers, prorationFactor = 1) {
21493
21494
  let totalCost = 0;
21494
21495
  let nonProratedCost = 0;
@@ -22368,13 +22369,115 @@ function useSubscriptionService() {
22368
22369
  session?.endSession();
22369
22370
  }
22370
22371
  }
22372
+ async function processSubscriptionPayment(invoiceNumber, subscriptionId) {
22373
+ const invoice = await getByNumber(invoiceNumber);
22374
+ if (!invoice) {
22375
+ throw new BadRequestError36("Invoice not found.");
22376
+ }
22377
+ if (invoice.status === "paid") {
22378
+ throw new BadRequestError36("Invoice already paid.");
22379
+ }
22380
+ if (!invoice._id) {
22381
+ throw new BadRequestError36("Invoice ID is missing.");
22382
+ }
22383
+ if (!invoice.metadata?.subscriptionId) {
22384
+ throw new BadRequestError36("Subscription ID is missing.");
22385
+ }
22386
+ if (invoice.metadata.subscriptionId.toString() !== subscriptionId) {
22387
+ throw new BadRequestError36("Subscription ID does not match invoice.");
22388
+ }
22389
+ const subscription = await _getById(invoice.metadata.subscriptionId);
22390
+ if (!subscription) {
22391
+ throw new BadRequestError36("Subscription not found.");
22392
+ }
22393
+ if (!subscription._id) {
22394
+ throw new BadRequestError36("Subscription ID is missing.");
22395
+ }
22396
+ if (!subscription.customerId) {
22397
+ throw new BadRequestError36("Customer ID is missing.");
22398
+ }
22399
+ if (!subscription.paymentMethodId) {
22400
+ throw new BadRequestError36("Payment method ID is missing.");
22401
+ }
22402
+ if (!subscription.amount) {
22403
+ throw new BadRequestError36("Subscription amount is missing.");
22404
+ }
22405
+ if (!subscription.currency) {
22406
+ throw new BadRequestError36("Subscription currency is missing.");
22407
+ }
22408
+ const session = useAtlas23.getClient()?.startSession();
22409
+ try {
22410
+ session?.startTransaction();
22411
+ const payment = await pay({
22412
+ customer_id: subscription.customerId,
22413
+ payment_method_id: subscription.paymentMethodId,
22414
+ amount: subscription.amount,
22415
+ currency: subscription.currency,
22416
+ description: "GoWeekdays Subscription Payment"
22417
+ });
22418
+ if (!payment.id) {
22419
+ throw new BadRequestError36("Failed to process payment.");
22420
+ }
22421
+ const paymentMethod = await getPaymentMethodById(
22422
+ subscription.paymentMethodId
22423
+ );
22424
+ if (!paymentMethod) {
22425
+ throw new BadRequestError36("Payment method not found.");
22426
+ }
22427
+ const paymentData = {
22428
+ invoiceId: invoiceNumber,
22429
+ amount: subscription.amount,
22430
+ paymentMethod: paymentMethod.type,
22431
+ status: "completed",
22432
+ metadata: {
22433
+ userId: String(subscription.user),
22434
+ orgId: String(subscription.org),
22435
+ currency: subscription.currency,
22436
+ paymentMethod: paymentMethod.type === "EWALLET" ? paymentMethod.ewallet.channel_code : paymentMethod.type === "DIRECT_DEBIT" ? paymentMethod.direct_debit.channel_code : "",
22437
+ paymentMethodType: paymentMethod.type,
22438
+ paymentMethodId: subscription.paymentMethodId,
22439
+ payment: payment.id,
22440
+ subscriptionId: subscription._id.toString()
22441
+ }
22442
+ };
22443
+ if (!paymentData.metadata.userId) {
22444
+ delete paymentData.metadata.userId;
22445
+ }
22446
+ if (!paymentData.metadata.orgId) {
22447
+ delete paymentData.metadata.orgId;
22448
+ }
22449
+ await addPayment(paymentData, session);
22450
+ await updateStatusByInvoiceNumber(invoiceNumber, "paid", session);
22451
+ await processSuccessfulPayment(
22452
+ {
22453
+ _id: subscription._id.toString(),
22454
+ nextBillingDate: subscription.nextBillingDate
22455
+ },
22456
+ session
22457
+ );
22458
+ await session?.commitTransaction();
22459
+ } catch (error) {
22460
+ await session?.abortTransaction();
22461
+ logger16.log({
22462
+ level: "error",
22463
+ message: `Failed to process subscription payment: ${error}`
22464
+ });
22465
+ if (error instanceof AppError9) {
22466
+ throw error;
22467
+ }
22468
+ throw new BadRequestError36("Failed to process subscription payment.");
22469
+ } finally {
22470
+ session?.endSession();
22471
+ }
22472
+ }
22371
22473
  return {
22372
22474
  getByUserId,
22373
22475
  getStatusByUser,
22374
22476
  createAffiliateSubscription,
22375
22477
  createOrgSubscription,
22376
22478
  processSubscriptions,
22377
- updateSeatsById
22479
+ updateSeatsById,
22480
+ processSubscriptionPayment
22378
22481
  };
22379
22482
  }
22380
22483
 
@@ -22442,7 +22545,8 @@ function useSubscriptionController() {
22442
22545
  getStatusByUser: _getStatusByUser,
22443
22546
  createAffiliateSubscription: _createAffiliateSubscription,
22444
22547
  createOrgSubscription: _createOrgSubscription,
22445
- updateSeatsById: _updateSubscriptionSeats
22548
+ updateSeatsById: _updateSubscriptionSeats,
22549
+ processSubscriptionPayment: _processSubscriptionPayment
22446
22550
  } = useSubscriptionService();
22447
22551
  async function add(req, res, next) {
22448
22552
  const value = req.body;
@@ -22741,6 +22845,26 @@ function useSubscriptionController() {
22741
22845
  return;
22742
22846
  }
22743
22847
  }
22848
+ async function processSubscriptionPayment(req, res, next) {
22849
+ const id = req.params.id ?? "";
22850
+ const invoiceNumber = req.body.invoice ?? "";
22851
+ const { error } = Joi20.object({
22852
+ id: Joi20.string().hex().required(),
22853
+ invoiceNumber: Joi20.string().required()
22854
+ }).validate({ id, invoiceNumber });
22855
+ if (error) {
22856
+ next(new BadRequestError37(error.message));
22857
+ return;
22858
+ }
22859
+ try {
22860
+ const payment = await _processSubscriptionPayment(invoiceNumber, id);
22861
+ res.json(payment);
22862
+ return;
22863
+ } catch (error2) {
22864
+ next(error2);
22865
+ return;
22866
+ }
22867
+ }
22744
22868
  return {
22745
22869
  add,
22746
22870
  getByUserId,
@@ -22757,7 +22881,8 @@ function useSubscriptionController() {
22757
22881
  updatePaymentMethodById,
22758
22882
  addBillingContactById,
22759
22883
  updateBillingContactByAddedAt,
22760
- deleteBillingContactByAddedAt
22884
+ deleteBillingContactByAddedAt,
22885
+ processSubscriptionPayment
22761
22886
  };
22762
22887
  }
22763
22888
 
@@ -24001,7 +24126,7 @@ function useOrderController() {
24001
24126
  // src/services/invoice.service.ts
24002
24127
  import { BadRequestError as BadRequestError49, logger as logger19, useAtlas as useAtlas28 } from "@goweekdays/utils";
24003
24128
  function useInvoiceService() {
24004
- const { getOverdueInvoices, updateStatusByInvoiceNumber } = useInvoiceRepo();
24129
+ const { getOverdueInvoices, updateStatusByInvoiceNumber, getByNumber } = useInvoiceRepo();
24005
24130
  const {
24006
24131
  getById,
24007
24132
  processSuccessfulPayment,