@goweekdays/core 2.11.2 → 2.11.4

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,17 @@
1
1
  # @goweekdays/core
2
2
 
3
+ ## 2.11.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 2697ef3: Refactor subscription amount calculation logic
8
+
9
+ ## 2.11.3
10
+
11
+ ### Patch Changes
12
+
13
+ - 2bb4eb2: Add orgName to subscription and refactor service logic
14
+
3
15
  ## 2.11.2
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -831,6 +831,7 @@ declare function usePlanController(): {
831
831
  type TSubscription = {
832
832
  _id?: ObjectId;
833
833
  org: string | ObjectId;
834
+ orgName?: string;
834
835
  seats: number;
835
836
  paidSeats: number;
836
837
  amount: number;
package/dist/index.js CHANGED
@@ -5453,6 +5453,7 @@ var schema2 = {
5453
5453
  var schemaSubscription = import_joi20.default.object({
5454
5454
  ...schema2,
5455
5455
  org: import_joi20.default.string().hex().length(24).required(),
5456
+ orgName: import_joi20.default.string().optional().allow("", null),
5456
5457
  currency: import_joi20.default.string().length(3).required(),
5457
5458
  billingCycle: import_joi20.default.string().valid("monthly", "yearly").required()
5458
5459
  });
@@ -5491,6 +5492,7 @@ function modelSubscription(data) {
5491
5492
  return {
5492
5493
  _id: data._id,
5493
5494
  org: data.org,
5495
+ orgName: data.orgName ?? "",
5494
5496
  seats: data.seats,
5495
5497
  paidSeats: data.paidSeats,
5496
5498
  amount: data.amount,
@@ -8286,7 +8288,6 @@ function usePromoController() {
8286
8288
  // src/resources/subscription/subscription.service.ts
8287
8289
  function useSubscriptionService() {
8288
8290
  const {
8289
- getById,
8290
8291
  updateById,
8291
8292
  getByStatus,
8292
8293
  updateStatusById,
@@ -8420,16 +8421,25 @@ function useSubscriptionService() {
8420
8421
  }
8421
8422
  }
8422
8423
  const isDecrease = existingSubscription && value.seats < existingSubscription.paidSeats;
8424
+ const isNoChange = existingSubscription && value.seats === existingSubscription.paidSeats;
8425
+ const isIncrease = existingSubscription && value.seats > existingSubscription.paidSeats;
8426
+ let subscriptionAmount;
8427
+ if (!existingSubscription) {
8428
+ subscriptionAmount = Math.round(monthlyAmount * 100) / 100;
8429
+ } else if (proratedAmount > 0) {
8430
+ subscriptionAmount = Math.round((existingSubscription.amount + proratedAmount) * 100) / 100;
8431
+ } else if (isIncrease) {
8432
+ subscriptionAmount = Math.round(monthlyAmount * 100) / 100;
8433
+ } else {
8434
+ subscriptionAmount = Math.round(existingSubscription.amount * 100) / 100;
8435
+ }
8423
8436
  return {
8424
8437
  // The monthly fee for the next billing cycle
8425
8438
  monthlyAmount: Math.round(monthlyAmount * 100) / 100,
8426
8439
  // The prorated amount to charge now for mid-cycle seat additions
8427
8440
  proratedAmount: Math.round(proratedAmount * 100) / 100,
8428
8441
  // The new subscription.amount for updates
8429
- // - Seat increase with proration: existing amount + prorated
8430
- // - Seat decrease: keep existing amount (decrease takes effect next cycle)
8431
- // - Same day change or no proration: use monthlyAmount
8432
- subscriptionAmount: existingSubscription ? proratedAmount > 0 ? Math.round((existingSubscription.amount + proratedAmount) * 100) / 100 : isDecrease ? Math.round(existingSubscription.amount * 100) / 100 : Math.round(monthlyAmount * 100) / 100 : Math.round(monthlyAmount * 100) / 100,
8442
+ subscriptionAmount,
8433
8443
  currency: plan.currency
8434
8444
  };
8435
8445
  }
@@ -8438,34 +8448,38 @@ function useSubscriptionService() {
8438
8448
  if (error) {
8439
8449
  throw new import_utils44.BadRequestError(`Invalid subscription data: ${error.message}`);
8440
8450
  }
8441
- const existingSubscription = await getByOrg(value.org);
8442
- if (existingSubscription) {
8443
- throw new import_utils44.BadRequestError("Organization already has a subscription.");
8444
- }
8445
- const plan = await getPlanById(value.plan);
8446
- if (!plan) {
8447
- throw new import_utils44.BadRequestError("Plan not found.");
8448
- }
8449
- const userData = await getUserById(value.user);
8450
- if (!userData) {
8451
- throw new import_utils44.BadRequestError("User not found.");
8452
- }
8453
- const membership = await getMembershipByApp({
8454
- user: value.user,
8455
- org: value.org,
8456
- app: "org"
8457
- });
8458
- if (!membership) {
8459
- throw new import_utils44.BadRequestError("User is not a member of the organization.");
8460
- }
8461
- const nextBillingDate = /* @__PURE__ */ new Date();
8462
- nextBillingDate.setMonth(nextBillingDate.getMonth() + 1);
8463
8451
  const session = import_utils44.useAtlas.getClient()?.startSession();
8464
8452
  if (!session) {
8465
8453
  throw new import_utils44.InternalServerError("Unable to start database session.");
8466
8454
  }
8467
8455
  try {
8468
8456
  session.startTransaction();
8457
+ const org = await getOrgById(value.org);
8458
+ if (!org) {
8459
+ throw new import_utils44.BadRequestError("Organization not found.");
8460
+ }
8461
+ const existingSubscription = await getByOrg(value.org);
8462
+ if (existingSubscription) {
8463
+ throw new import_utils44.BadRequestError("Organization already has a subscription.");
8464
+ }
8465
+ const plan = await getPlanById(value.plan);
8466
+ if (!plan) {
8467
+ throw new import_utils44.BadRequestError("Plan not found.");
8468
+ }
8469
+ const userData = await getUserById(value.user);
8470
+ if (!userData) {
8471
+ throw new import_utils44.BadRequestError("User not found.");
8472
+ }
8473
+ const membership = await getMembershipByApp({
8474
+ user: value.user,
8475
+ org: value.org,
8476
+ app: "org"
8477
+ });
8478
+ if (!membership) {
8479
+ throw new import_utils44.BadRequestError("User is not a member of the organization.");
8480
+ }
8481
+ const nextBillingDate = /* @__PURE__ */ new Date();
8482
+ nextBillingDate.setMonth(nextBillingDate.getMonth() + 1);
8469
8483
  const { subscriptionAmount, currency } = await computeFee({
8470
8484
  seats: value.seats,
8471
8485
  promoCode: value.promoCode,
@@ -8475,6 +8489,7 @@ function useSubscriptionService() {
8475
8489
  const subId = await _add(
8476
8490
  {
8477
8491
  org: value.org,
8492
+ orgName: org.name,
8478
8493
  seats: value.seats,
8479
8494
  paidSeats: value.seats,
8480
8495
  // Initial seats are considered "paid" for proration purposes
@@ -8519,42 +8534,44 @@ function useSubscriptionService() {
8519
8534
  if (error) {
8520
8535
  throw new import_utils44.BadRequestError(error.message);
8521
8536
  }
8522
- const subscription = await getByOrg(value.org);
8523
- if (!subscription) {
8524
- throw new import_utils44.BadRequestError("Subscription not found");
8525
- }
8526
- if (subscription.seats === value.seats) {
8527
- throw new import_utils44.BadRequestError(
8528
- "Failed to update subscription, no changes detected."
8529
- );
8530
- }
8531
- const userData = await getUserById(value.user);
8532
- if (!userData) {
8533
- throw new import_utils44.BadRequestError("User not found.");
8534
- }
8535
- const membership = await getMembershipByApp({
8536
- user: value.user,
8537
- org: value.org,
8538
- app: "org"
8539
- });
8540
- if (!membership) {
8541
- throw new import_utils44.BadRequestError("User is not a member of the organization.");
8542
- }
8543
- const { subscriptionAmount, proratedAmount, currency } = await computeFee({
8544
- seats: value.seats,
8545
- promoCode: value.promoCode ?? "",
8546
- plan: value.plan ?? "",
8547
- org: value.org
8548
- });
8549
8537
  const session = import_utils44.useAtlas.getClient()?.startSession();
8550
8538
  if (!session) {
8551
8539
  throw new import_utils44.InternalServerError("Unable to start database session.");
8552
8540
  }
8553
- const seatIncreased = value.seats > subscription.paidSeats;
8554
- const additionalSeats = value.seats - subscription.paidSeats;
8555
- const paidSeats = seatIncreased ? value.seats : subscription.paidSeats;
8556
8541
  try {
8557
8542
  session.startTransaction();
8543
+ const subscription = await getByOrg(value.org);
8544
+ if (!subscription) {
8545
+ throw new import_utils44.BadRequestError("Subscription not found");
8546
+ }
8547
+ if (subscription.seats === value.seats) {
8548
+ throw new import_utils44.BadRequestError(
8549
+ "Failed to update subscription, no changes detected."
8550
+ );
8551
+ }
8552
+ const userData = await getUserById(value.user);
8553
+ if (!userData) {
8554
+ throw new import_utils44.BadRequestError("User not found.");
8555
+ }
8556
+ const membership = await getMembershipByApp({
8557
+ user: value.user,
8558
+ org: value.org,
8559
+ app: "org"
8560
+ });
8561
+ if (!membership) {
8562
+ throw new import_utils44.BadRequestError("User is not a member of the organization.");
8563
+ }
8564
+ const { subscriptionAmount, proratedAmount, currency } = await computeFee(
8565
+ {
8566
+ seats: value.seats,
8567
+ promoCode: value.promoCode ?? "",
8568
+ plan: value.plan ?? "",
8569
+ org: value.org
8570
+ }
8571
+ );
8572
+ const seatIncreased = value.seats > subscription.paidSeats;
8573
+ const additionalSeats = value.seats - subscription.paidSeats;
8574
+ const paidSeats = seatIncreased ? value.seats : subscription.paidSeats;
8558
8575
  await updateById(
8559
8576
  subscription._id?.toString() ?? "",
8560
8577
  { seats: value.seats, amount: subscriptionAmount, paidSeats },
@@ -8588,53 +8605,53 @@ function useSubscriptionService() {
8588
8605
  if (error) {
8589
8606
  throw new import_utils44.BadRequestError(error.message);
8590
8607
  }
8591
- const subscription = await getByOrg(value.org);
8592
- if (!subscription) {
8593
- throw new import_utils44.BadRequestError("Subscription not found");
8594
- }
8595
- if (subscription.promoCode === value.promoCode) {
8596
- throw new import_utils44.BadRequestError(
8597
- "Failed to update subscription, no changes detected."
8598
- );
8599
- }
8600
- let promo = null;
8601
- if (value.promoCode) {
8602
- promo = await getPromoByCode(value.promoCode);
8603
- if (!promo) {
8604
- throw new import_utils44.BadRequestError("Promo code not found.");
8605
- }
8606
- }
8607
- const userData = await getUserById(value.user);
8608
- if (!userData) {
8609
- throw new import_utils44.BadRequestError("User not found.");
8610
- }
8611
- const membership = await getMembershipByApp({
8612
- user: value.user,
8613
- org: value.org,
8614
- app: "org"
8615
- });
8616
- if (!membership) {
8617
- throw new import_utils44.BadRequestError("User is not a member of the organization.");
8618
- }
8619
- const plan = await getDefaultPlan();
8620
- if (!plan) {
8621
- throw new import_utils44.BadRequestError("Plan not found.");
8622
- }
8623
- const { subscriptionAmount, currency } = await computeFee(
8624
- {
8625
- seats: subscription.seats,
8626
- promoCode: value.promoCode ?? "",
8627
- plan: plan._id?.toString() ?? "",
8628
- org: value.org
8629
- },
8630
- true
8631
- );
8632
8608
  const session = import_utils44.useAtlas.getClient()?.startSession();
8633
8609
  if (!session) {
8634
8610
  throw new import_utils44.InternalServerError("Unable to start database session.");
8635
8611
  }
8636
8612
  try {
8637
8613
  session.startTransaction();
8614
+ const subscription = await getByOrg(value.org);
8615
+ if (!subscription) {
8616
+ throw new import_utils44.BadRequestError("Subscription not found");
8617
+ }
8618
+ if (subscription.promoCode === value.promoCode) {
8619
+ throw new import_utils44.BadRequestError(
8620
+ "Failed to update subscription, no changes detected."
8621
+ );
8622
+ }
8623
+ let promo = null;
8624
+ if (value.promoCode) {
8625
+ promo = await getPromoByCode(value.promoCode);
8626
+ if (!promo) {
8627
+ throw new import_utils44.BadRequestError("Promo code not found.");
8628
+ }
8629
+ }
8630
+ const userData = await getUserById(value.user);
8631
+ if (!userData) {
8632
+ throw new import_utils44.BadRequestError("User not found.");
8633
+ }
8634
+ const membership = await getMembershipByApp({
8635
+ user: value.user,
8636
+ org: value.org,
8637
+ app: "org"
8638
+ });
8639
+ if (!membership) {
8640
+ throw new import_utils44.BadRequestError("User is not a member of the organization.");
8641
+ }
8642
+ const plan = await getDefaultPlan();
8643
+ if (!plan) {
8644
+ throw new import_utils44.BadRequestError("Plan not found.");
8645
+ }
8646
+ const { subscriptionAmount, currency } = await computeFee(
8647
+ {
8648
+ seats: subscription.seats,
8649
+ promoCode: value.promoCode ?? "",
8650
+ plan: plan._id?.toString() ?? "",
8651
+ org: value.org
8652
+ },
8653
+ true
8654
+ );
8638
8655
  await updateById(
8639
8656
  subscription._id?.toString() ?? "",
8640
8657
  { promoCode: value.promoCode ?? "", amount: subscriptionAmount },