@goweekdays/core 2.11.0 → 2.11.2

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
@@ -5396,7 +5396,7 @@ var schemaSubscribe = Joi20.object({
5396
5396
  var schema2 = {
5397
5397
  seats: Joi20.number().integer().min(1).required(),
5398
5398
  paidSeats: Joi20.number().integer().min(0).required(),
5399
- amount: Joi20.number().positive().required(),
5399
+ amount: Joi20.number().positive().required().allow(0),
5400
5400
  promoCode: Joi20.string().optional().allow("", null),
5401
5401
  nextBillingDate: Joi20.date().optional().allow("", null)
5402
5402
  };
@@ -8348,7 +8348,7 @@ function useSubscriptionService() {
8348
8348
  }
8349
8349
  return total;
8350
8350
  }
8351
- async function computeFee(value) {
8351
+ async function computeFee(value, skipProration) {
8352
8352
  const { error } = schemaSubscriptionCompute.validate(value);
8353
8353
  if (error) {
8354
8354
  throw new BadRequestError41(`Invalid subscription data: ${error.message}`);
@@ -8374,10 +8374,10 @@ function useSubscriptionService() {
8374
8374
  if (promo) {
8375
8375
  switch (promo.type) {
8376
8376
  case "fixed":
8377
- monthlyAmount = Math.max(plan.price - (promo.fixedRate ?? 0), 0) * value.seats;
8377
+ monthlyAmount = Math.max(promo.fixedRate ?? 0, 0) * value.seats;
8378
8378
  break;
8379
8379
  case "flat":
8380
- monthlyAmount = Math.max(plan.price - (promo.flatRate ?? 0) / value.seats, 0) * value.seats;
8380
+ monthlyAmount = Math.max((promo.flatRate ?? 0) / value.seats, 0) * value.seats;
8381
8381
  break;
8382
8382
  case "volume": {
8383
8383
  if (promo.tiers && promo.tiers.length > 0) {
@@ -8411,7 +8411,7 @@ function useSubscriptionService() {
8411
8411
  0,
8412
8412
  value.seats - existingSubscription.paidSeats
8413
8413
  );
8414
- if (additionalSeats > 0 && daysRemaining > 0 && daysElapsed > 0) {
8414
+ if (additionalSeats > 0 && daysRemaining > 0 && daysElapsed > 0 && !skipProration) {
8415
8415
  let additionalSeatsAmount = 0;
8416
8416
  if (promo?.type === "volume" && promo.tiers && promo.tiers.length > 0) {
8417
8417
  additionalSeatsAmount = calculateVolumeTierAmount(
@@ -8439,14 +8439,17 @@ function useSubscriptionService() {
8439
8439
  proratedAmount = dailyRate * daysRemaining;
8440
8440
  }
8441
8441
  }
8442
+ const isDecrease = existingSubscription && value.seats < existingSubscription.paidSeats;
8442
8443
  return {
8443
8444
  // The monthly fee for the next billing cycle
8444
8445
  monthlyAmount: Math.round(monthlyAmount * 100) / 100,
8445
8446
  // The prorated amount to charge now for mid-cycle seat additions
8446
8447
  proratedAmount: Math.round(proratedAmount * 100) / 100,
8447
8448
  // The new subscription.amount for updates
8448
- // If no proration (same day change), use monthlyAmount; otherwise add prorated to existing
8449
- subscriptionAmount: existingSubscription ? proratedAmount > 0 ? Math.round((existingSubscription.amount + proratedAmount) * 100) / 100 : Math.round(monthlyAmount * 100) / 100 : Math.round(monthlyAmount * 100) / 100,
8449
+ // - Seat increase with proration: existing amount + prorated
8450
+ // - Seat decrease: keep existing amount (decrease takes effect next cycle)
8451
+ // - Same day change or no proration: use monthlyAmount
8452
+ 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,
8450
8453
  currency: plan.currency
8451
8454
  };
8452
8455
  }
@@ -8557,9 +8560,12 @@ function useSubscriptionService() {
8557
8560
  if (!membership) {
8558
8561
  throw new BadRequestError41("User is not a member of the organization.");
8559
8562
  }
8560
- const { subscriptionAmount, proratedAmount, currency } = await computeFee(
8561
- value
8562
- );
8563
+ const { subscriptionAmount, proratedAmount, currency } = await computeFee({
8564
+ seats: value.seats,
8565
+ promoCode: value.promoCode ?? "",
8566
+ plan: value.plan ?? "",
8567
+ org: value.org
8568
+ });
8563
8569
  const session = useAtlas18.getClient()?.startSession();
8564
8570
  if (!session) {
8565
8571
  throw new InternalServerError22("Unable to start database session.");
@@ -8634,12 +8640,15 @@ function useSubscriptionService() {
8634
8640
  if (!plan) {
8635
8641
  throw new BadRequestError41("Plan not found.");
8636
8642
  }
8637
- const { subscriptionAmount, currency } = await computeFee({
8638
- seats: subscription.seats,
8639
- promoCode: value.promoCode ?? "",
8640
- plan: plan._id?.toString() ?? "",
8641
- org: value.org
8642
- });
8643
+ const { subscriptionAmount, currency } = await computeFee(
8644
+ {
8645
+ seats: subscription.seats,
8646
+ promoCode: value.promoCode ?? "",
8647
+ plan: plan._id?.toString() ?? "",
8648
+ org: value.org
8649
+ },
8650
+ true
8651
+ );
8643
8652
  const session = useAtlas18.getClient()?.startSession();
8644
8653
  if (!session) {
8645
8654
  throw new InternalServerError22("Unable to start database session.");