fix(payment): consume first recharge offer after payment
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { PaymentPlan, PaymentPlansResponse } from "@/data/dto/payment";
|
||||
import { PaymentPlan, type PaymentPlansResponse } from "@/data/dto/payment";
|
||||
|
||||
import type { PaymentState } from "./payment-state";
|
||||
|
||||
@@ -64,15 +64,17 @@ export function hydratePlansResponseState(
|
||||
PaymentState,
|
||||
| "plans"
|
||||
| "isFirstRecharge"
|
||||
| "firstRechargeOffer"
|
||||
| "selectedPlanId"
|
||||
| "autoRenew"
|
||||
| "errorMessage"
|
||||
> {
|
||||
const plans = normalizeFirstRechargePlans(
|
||||
response.plans,
|
||||
response.isFirstRecharge,
|
||||
);
|
||||
return {
|
||||
...hydratePlansState(response.plans, selectedPlanId),
|
||||
...hydratePlansState(plans, selectedPlanId),
|
||||
isFirstRecharge: response.isFirstRecharge,
|
||||
firstRechargeOffer: response.firstRechargeOffer,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -101,15 +103,54 @@ export function refreshPlansResponseState(
|
||||
PaymentState,
|
||||
| "plans"
|
||||
| "isFirstRecharge"
|
||||
| "firstRechargeOffer"
|
||||
| "selectedPlanId"
|
||||
| "autoRenew"
|
||||
| "errorMessage"
|
||||
> {
|
||||
const plans = normalizeFirstRechargePlans(
|
||||
response.plans,
|
||||
response.isFirstRecharge,
|
||||
);
|
||||
return {
|
||||
...refreshPlansState(response.plans, selectedPlanId),
|
||||
...refreshPlansState(plans, selectedPlanId),
|
||||
isFirstRecharge: response.isFirstRecharge,
|
||||
firstRechargeOffer: response.firstRechargeOffer,
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeFirstRechargePlans(
|
||||
plans: readonly PaymentPlan[],
|
||||
isFirstRecharge: boolean,
|
||||
): PaymentPlan[] {
|
||||
if (isFirstRecharge) return [...plans];
|
||||
return plans.map(consumeFirstRechargePlan);
|
||||
}
|
||||
|
||||
export function consumeFirstRechargePlan(plan: PaymentPlan): PaymentPlan {
|
||||
const originalAmountCents = plan.originalAmountCents;
|
||||
return PaymentPlan.from({
|
||||
...plan.toJson(),
|
||||
amountCents: originalAmountCents ?? plan.amountCents,
|
||||
originalAmountCents: null,
|
||||
isFirstRechargeOffer: false,
|
||||
firstRechargeDiscountPercent: null,
|
||||
promotionType: null,
|
||||
});
|
||||
}
|
||||
|
||||
export function consumeFirstRechargeState(
|
||||
context: PaymentState,
|
||||
): Pick<
|
||||
PaymentState,
|
||||
| "plans"
|
||||
| "isFirstRecharge"
|
||||
| "selectedPlanId"
|
||||
| "autoRenew"
|
||||
| "errorMessage"
|
||||
> {
|
||||
const plans = context.plans.map(consumeFirstRechargePlan);
|
||||
return {
|
||||
...refreshPlansState(plans, context.selectedPlanId),
|
||||
isFirstRecharge: false,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user