@goweekdays/core 2.11.2 → 2.11.3

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
+ ## 2.11.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 2bb4eb2: Add orgName to subscription and refactor service logic
8
+
3
9
  ## 2.11.2
4
10
 
5
11
  ### 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,
@@ -8438,34 +8439,38 @@ function useSubscriptionService() {
8438
8439
  if (error) {
8439
8440
  throw new import_utils44.BadRequestError(`Invalid subscription data: ${error.message}`);
8440
8441
  }
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
8442
  const session = import_utils44.useAtlas.getClient()?.startSession();
8464
8443
  if (!session) {
8465
8444
  throw new import_utils44.InternalServerError("Unable to start database session.");
8466
8445
  }
8467
8446
  try {
8468
8447
  session.startTransaction();
8448
+ const org = await getOrgById(value.org);
8449
+ if (!org) {
8450
+ throw new import_utils44.BadRequestError("Organization not found.");
8451
+ }
8452
+ const existingSubscription = await getByOrg(value.org);
8453
+ if (existingSubscription) {
8454
+ throw new import_utils44.BadRequestError("Organization already has a subscription.");
8455
+ }
8456
+ const plan = await getPlanById(value.plan);
8457
+ if (!plan) {
8458
+ throw new import_utils44.BadRequestError("Plan not found.");
8459
+ }
8460
+ const userData = await getUserById(value.user);
8461
+ if (!userData) {
8462
+ throw new import_utils44.BadRequestError("User not found.");
8463
+ }
8464
+ const membership = await getMembershipByApp({
8465
+ user: value.user,
8466
+ org: value.org,
8467
+ app: "org"
8468
+ });
8469
+ if (!membership) {
8470
+ throw new import_utils44.BadRequestError("User is not a member of the organization.");
8471
+ }
8472
+ const nextBillingDate = /* @__PURE__ */ new Date();
8473
+ nextBillingDate.setMonth(nextBillingDate.getMonth() + 1);
8469
8474
  const { subscriptionAmount, currency } = await computeFee({
8470
8475
  seats: value.seats,
8471
8476
  promoCode: value.promoCode,
@@ -8475,6 +8480,7 @@ function useSubscriptionService() {
8475
8480
  const subId = await _add(
8476
8481
  {
8477
8482
  org: value.org,
8483
+ orgName: org.name,
8478
8484
  seats: value.seats,
8479
8485
  paidSeats: value.seats,
8480
8486
  // Initial seats are considered "paid" for proration purposes
@@ -8519,42 +8525,44 @@ function useSubscriptionService() {
8519
8525
  if (error) {
8520
8526
  throw new import_utils44.BadRequestError(error.message);
8521
8527
  }
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
8528
  const session = import_utils44.useAtlas.getClient()?.startSession();
8550
8529
  if (!session) {
8551
8530
  throw new import_utils44.InternalServerError("Unable to start database session.");
8552
8531
  }
8553
- const seatIncreased = value.seats > subscription.paidSeats;
8554
- const additionalSeats = value.seats - subscription.paidSeats;
8555
- const paidSeats = seatIncreased ? value.seats : subscription.paidSeats;
8556
8532
  try {
8557
8533
  session.startTransaction();
8534
+ const subscription = await getByOrg(value.org);
8535
+ if (!subscription) {
8536
+ throw new import_utils44.BadRequestError("Subscription not found");
8537
+ }
8538
+ if (subscription.seats === value.seats) {
8539
+ throw new import_utils44.BadRequestError(
8540
+ "Failed to update subscription, no changes detected."
8541
+ );
8542
+ }
8543
+ const userData = await getUserById(value.user);
8544
+ if (!userData) {
8545
+ throw new import_utils44.BadRequestError("User not found.");
8546
+ }
8547
+ const membership = await getMembershipByApp({
8548
+ user: value.user,
8549
+ org: value.org,
8550
+ app: "org"
8551
+ });
8552
+ if (!membership) {
8553
+ throw new import_utils44.BadRequestError("User is not a member of the organization.");
8554
+ }
8555
+ const { subscriptionAmount, proratedAmount, currency } = await computeFee(
8556
+ {
8557
+ seats: value.seats,
8558
+ promoCode: value.promoCode ?? "",
8559
+ plan: value.plan ?? "",
8560
+ org: value.org
8561
+ }
8562
+ );
8563
+ const seatIncreased = value.seats > subscription.paidSeats;
8564
+ const additionalSeats = value.seats - subscription.paidSeats;
8565
+ const paidSeats = seatIncreased ? value.seats : subscription.paidSeats;
8558
8566
  await updateById(
8559
8567
  subscription._id?.toString() ?? "",
8560
8568
  { seats: value.seats, amount: subscriptionAmount, paidSeats },
@@ -8588,53 +8596,53 @@ function useSubscriptionService() {
8588
8596
  if (error) {
8589
8597
  throw new import_utils44.BadRequestError(error.message);
8590
8598
  }
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
8599
  const session = import_utils44.useAtlas.getClient()?.startSession();
8633
8600
  if (!session) {
8634
8601
  throw new import_utils44.InternalServerError("Unable to start database session.");
8635
8602
  }
8636
8603
  try {
8637
8604
  session.startTransaction();
8605
+ const subscription = await getByOrg(value.org);
8606
+ if (!subscription) {
8607
+ throw new import_utils44.BadRequestError("Subscription not found");
8608
+ }
8609
+ if (subscription.promoCode === value.promoCode) {
8610
+ throw new import_utils44.BadRequestError(
8611
+ "Failed to update subscription, no changes detected."
8612
+ );
8613
+ }
8614
+ let promo = null;
8615
+ if (value.promoCode) {
8616
+ promo = await getPromoByCode(value.promoCode);
8617
+ if (!promo) {
8618
+ throw new import_utils44.BadRequestError("Promo code not found.");
8619
+ }
8620
+ }
8621
+ const userData = await getUserById(value.user);
8622
+ if (!userData) {
8623
+ throw new import_utils44.BadRequestError("User not found.");
8624
+ }
8625
+ const membership = await getMembershipByApp({
8626
+ user: value.user,
8627
+ org: value.org,
8628
+ app: "org"
8629
+ });
8630
+ if (!membership) {
8631
+ throw new import_utils44.BadRequestError("User is not a member of the organization.");
8632
+ }
8633
+ const plan = await getDefaultPlan();
8634
+ if (!plan) {
8635
+ throw new import_utils44.BadRequestError("Plan not found.");
8636
+ }
8637
+ const { subscriptionAmount, currency } = await computeFee(
8638
+ {
8639
+ seats: subscription.seats,
8640
+ promoCode: value.promoCode ?? "",
8641
+ plan: plan._id?.toString() ?? "",
8642
+ org: value.org
8643
+ },
8644
+ true
8645
+ );
8638
8646
  await updateById(
8639
8647
  subscription._id?.toString() ?? "",
8640
8648
  { promoCode: value.promoCode ?? "", amount: subscriptionAmount },