feat: implement plan upgrade logic with prorated pricing and MercadoPago integration
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
import dayjs from "dayjs";
|
||||
import { calculateProratedUpgradeAmount } from "../PlanSubscriptions.pricing";
|
||||
import PlanSubscriptionsList from "../PlanSubscriptons";
|
||||
import { MP_SUBS_STATUS } from "../PlanSubscriptions.Adapter.Mongoose";
|
||||
import PlansList from "../../Plans/Plans";
|
||||
|
||||
jest.mock("../../../index", () => ({
|
||||
io: {
|
||||
to: jest.fn(() => ({ emit: jest.fn() })),
|
||||
},
|
||||
}));
|
||||
|
||||
describe("PlanSubscriptions Logic", () => {
|
||||
it("should calculate correct endDate for 1, 3, 6, 12 months", () => {
|
||||
@@ -40,4 +50,71 @@ describe("PlanSubscriptions Logic", () => {
|
||||
const final12 = total12 - (total12 * (30 / 100)); // 12000 - 3600 = 8400
|
||||
expect(final12).toBe(8400);
|
||||
});
|
||||
|
||||
it("should calculate only the prorated difference for paid plan upgrades", () => {
|
||||
const currentPlanPrice = 1000;
|
||||
const requestedPlanPrice = 2500;
|
||||
const remainingDays = 15;
|
||||
|
||||
const proratedAmount = calculateProratedUpgradeAmount(
|
||||
currentPlanPrice,
|
||||
requestedPlanPrice,
|
||||
remainingDays
|
||||
);
|
||||
|
||||
expect(proratedAmount).toBe(750);
|
||||
});
|
||||
|
||||
it("should keep pending paid subscription plan instead of masking it as free", async () => {
|
||||
const paidPlan = {
|
||||
id: "paid-plan-id",
|
||||
name: "Intermedio",
|
||||
description: "",
|
||||
features: [],
|
||||
code: "intermediate",
|
||||
price: 1000,
|
||||
annualPrice: 0,
|
||||
limitOrganizations: 1,
|
||||
limitEmployees: 1,
|
||||
limitServices: 1,
|
||||
limitAppointments: 1,
|
||||
limitClients: 1,
|
||||
limitRepeats: 1,
|
||||
mailNotifications: false,
|
||||
smsNotifications: false,
|
||||
wapNotifications: false,
|
||||
bot: false,
|
||||
active: true,
|
||||
dateLimit: false,
|
||||
payments: false,
|
||||
};
|
||||
const subscription = {
|
||||
id: "subscription-id",
|
||||
planId: paidPlan.id,
|
||||
startDate: new Date(),
|
||||
endDate: new Date(),
|
||||
isActive: false,
|
||||
autoRenew: false,
|
||||
mpStatus: MP_SUBS_STATUS.PENDING,
|
||||
mpDateCreated: new Date(),
|
||||
};
|
||||
|
||||
const originalPlanSuscriptions = PlanSubscriptionsList.planSuscriptions;
|
||||
const findOneSpy = jest.spyOn(PlansList, "findOne").mockResolvedValue(paidPlan);
|
||||
PlanSubscriptionsList.planSuscriptions = {
|
||||
findOne: jest.fn().mockResolvedValue(subscription),
|
||||
} as any;
|
||||
|
||||
try {
|
||||
const result = await PlanSubscriptionsList.getSubscriptionByUser({ sessionUser: "user-id" });
|
||||
|
||||
expect(result?.plan).toEqual(paidPlan);
|
||||
expect(result?.mpStatus).toBe(MP_SUBS_STATUS.PENDING);
|
||||
expect(findOneSpy).toHaveBeenCalledTimes(1);
|
||||
expect(findOneSpy).toHaveBeenCalledWith({ _id: subscription.planId });
|
||||
} finally {
|
||||
PlanSubscriptionsList.planSuscriptions = originalPlanSuscriptions;
|
||||
findOneSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user