@goweekdays/core 2.11.13 → 2.11.15

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.15
4
+
5
+ ### Patch Changes
6
+
7
+ - dfa139d: Remove section comments from subscription.service.ts
8
+
9
+ ## 2.11.14
10
+
11
+ ### Patch Changes
12
+
13
+ - 9fd2d52: Improve promo usage and subscription transaction handling
14
+
3
15
  ## 2.11.13
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -957,7 +957,7 @@ type TSubscriptionTransactionMetadata = {
957
957
  additionalSeats?: number;
958
958
  seats?: number;
959
959
  paidSeats?: number;
960
- plan?: string;
960
+ plan?: string | ObjectId;
961
961
  promoCode?: string;
962
962
  nextPromoCode?: string;
963
963
  billingPeriodStart?: Date | string;
package/dist/index.js CHANGED
@@ -6068,6 +6068,15 @@ function modelSubscriptionTransaction(data) {
6068
6068
  throw new import_utils29.BadRequestError("Invalid createdBy ID.");
6069
6069
  }
6070
6070
  }
6071
+ if (data.metadata) {
6072
+ if (data.metadata.plan && typeof data.metadata.plan === "string" && data.metadata.plan.length === 24) {
6073
+ try {
6074
+ data.metadata.plan = new import_mongodb17.ObjectId(data.metadata.plan);
6075
+ } catch (error2) {
6076
+ throw new import_utils29.BadRequestError("Invalid plan ID in metadata.");
6077
+ }
6078
+ }
6079
+ }
6071
6080
  return {
6072
6081
  _id: data._id,
6073
6082
  subscription: data.subscription,
@@ -7878,7 +7887,7 @@ var schemaPromoUsage = import_joi34.default.object({
7878
7887
  function modelPromoUsage(value) {
7879
7888
  const { error } = schemaPromoUsage.validate(value);
7880
7889
  if (error) {
7881
- throw new Error(`Invalid Promo Usage model: ${error.message}`);
7890
+ throw new import_utils40.BadRequestError(`Invalid Promo Usage model: ${error.message}`);
7882
7891
  }
7883
7892
  if (value._id && typeof value._id === "string") {
7884
7893
  try {
@@ -7961,6 +7970,9 @@ function usePromoUsageRepo() {
7961
7970
  delCachedData();
7962
7971
  return "Successfully added promo usage.";
7963
7972
  } catch (error) {
7973
+ if (error instanceof import_utils41.AppError) {
7974
+ throw error;
7975
+ }
7964
7976
  throw new import_utils41.InternalServerError("Failed to add promo usage.");
7965
7977
  }
7966
7978
  }
@@ -8401,7 +8413,6 @@ function useSubscriptionService() {
8401
8413
  const { getById: getOrgById, updateStatusById: updateOrgStatusById } = useOrgRepo();
8402
8414
  const { getByCode: getPromoByCode } = usePromoRepo();
8403
8415
  const {
8404
- countByPromoId,
8405
8416
  add: addPromoUsage,
8406
8417
  updateStatusByOrgId: updatePromoUsageStatusByOrgId
8407
8418
  } = usePromoUsageRepo();
@@ -8516,7 +8527,7 @@ function useSubscriptionService() {
8516
8527
  "Cannot change promo code while increasing seats. Perform actions separately."
8517
8528
  );
8518
8529
  }
8519
- const effectivePromoCode = isPromoChange ? value.promoCode : existingSubscription?.promoCode;
8530
+ const effectivePromoCode = isNew ? value.promoCode : isPromoChange ? value.promoCode : existingSubscription?.promoCode;
8520
8531
  const promo = effectivePromoCode ? await getPromoByCode(effectivePromoCode) : null;
8521
8532
  const monthlyAmount = computeMonthlyAmount(value.seats, plan.price, promo);
8522
8533
  let proratedAmount = 0;
@@ -8725,6 +8736,29 @@ function useSubscriptionService() {
8725
8736
  session
8726
8737
  );
8727
8738
  }
8739
+ if (!seatIncreased) {
8740
+ await addTransaction(
8741
+ {
8742
+ type: "remove-seat",
8743
+ description: `Removed ${subscription.seats - value.seats} seats.`,
8744
+ amount: 0,
8745
+ currency,
8746
+ subscription: subscription._id?.toString() ?? "",
8747
+ createdBy: value.user,
8748
+ createdByName: `${userData.firstName} ${userData.lastName}`,
8749
+ metadata: {
8750
+ seats: subscription.seats - value.seats,
8751
+ paidSeats,
8752
+ plan: value.plan ?? "",
8753
+ promoCode: subscription.promoCode ?? "",
8754
+ nextPromoCode: subscription.nextPromoCode ?? "",
8755
+ billingPeriodStart: subscription.billingPeriodStart,
8756
+ nextBillingDate: subscription.nextBillingDate
8757
+ }
8758
+ },
8759
+ session
8760
+ );
8761
+ }
8728
8762
  await session.commitTransaction();
8729
8763
  return "Successfully updated subscription seats.";
8730
8764
  } catch (error2) {
@@ -8777,12 +8811,6 @@ function useSubscriptionService() {
8777
8811
  if (!plan) {
8778
8812
  throw new import_utils44.BadRequestError("Plan not found.");
8779
8813
  }
8780
- const { subscriptionAmount, currency } = await computeFee({
8781
- seats: subscription.seats,
8782
- promoCode: value.promoCode ?? "",
8783
- plan: plan._id?.toString() ?? "",
8784
- org: value.org
8785
- });
8786
8814
  await updateById(
8787
8815
  subscription._id?.toString() ?? "",
8788
8816
  { nextPromoCode: value.promoCode ?? "" },
@@ -8796,7 +8824,7 @@ function useSubscriptionService() {
8796
8824
  {
8797
8825
  promo: promo._id,
8798
8826
  org: value.org,
8799
- usedBy: value.user
8827
+ usedBy: userData.email
8800
8828
  },
8801
8829
  session
8802
8830
  );
@@ -8901,9 +8929,11 @@ function useSubscriptionService() {
8901
8929
  }
8902
8930
  for (let index = 0; index < subscriptions.length; index++) {
8903
8931
  const subscription = subscriptions[index];
8904
- const today = /* @__PURE__ */ new Date();
8905
- const nextBillingDate = new Date(subscription.updatedAt ?? "");
8906
- if (nextBillingDate.getFullYear() === today.getFullYear() && nextBillingDate.getMonth() === today.getMonth() && nextBillingDate.getDate() === today.getDate() || subscription.retry && subscription.retry >= retry) {
8932
+ const now = /* @__PURE__ */ new Date();
8933
+ if (now < new Date(subscription.nextBillingDate)) {
8934
+ continue;
8935
+ }
8936
+ if ((subscription.retry ?? 0) >= retry) {
8907
8937
  continue;
8908
8938
  }
8909
8939
  try {