feat(tip): support tiered coffee gifts
This commit is contained in:
@@ -1,27 +1,37 @@
|
||||
import type { LoginStatus } from "@/data/dto/auth";
|
||||
import type { PaymentPlan } from "@/data/dto/payment";
|
||||
|
||||
const TIP_COFFEE_PLAN_ID = "tip_coffee";
|
||||
const TIP_COFFEE_ORDER_TYPE = "tip_coffee";
|
||||
import {
|
||||
getTipCoffeeOption,
|
||||
type TipCoffeeType,
|
||||
} from "@/lib/tip/tip_coffee";
|
||||
|
||||
export function findTipCoffeePlan(
|
||||
plans: readonly PaymentPlan[],
|
||||
coffeeType: TipCoffeeType,
|
||||
): PaymentPlan | null {
|
||||
const option = getTipCoffeeOption(coffeeType);
|
||||
|
||||
return (
|
||||
plans.find((plan) => plan.planId === TIP_COFFEE_PLAN_ID) ??
|
||||
plans.find((plan) => plan.orderType === TIP_COFFEE_ORDER_TYPE) ??
|
||||
plans.find((plan) => plan.planId === option.planId) ??
|
||||
(coffeeType === "small"
|
||||
? (plans.find((plan) => plan.planId === "tip_coffee") ?? null)
|
||||
: null) ??
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
export function formatTipPrice(plan: PaymentPlan | null): string {
|
||||
if (!plan) return "US$ 5";
|
||||
export function formatTipPrice(
|
||||
plan: PaymentPlan | null,
|
||||
coffeeType: TipCoffeeType,
|
||||
): string {
|
||||
const option = getTipCoffeeOption(coffeeType);
|
||||
const amountCents = plan?.amountCents ?? option.amountCents;
|
||||
const currency = plan?.currency.trim().toUpperCase() || "USD";
|
||||
|
||||
const amount = plan.amountCents / 100;
|
||||
const amount = amountCents / 100;
|
||||
const formattedAmount = Number.isInteger(amount)
|
||||
? String(amount)
|
||||
: amount.toFixed(2).replace(/\.?0+$/, "");
|
||||
const currency = plan.currency.trim().toUpperCase();
|
||||
|
||||
if (currency === "USD") return `US$ ${formattedAmount}`;
|
||||
if (currency.length > 0) return `${currency} ${formattedAmount}`;
|
||||
|
||||
Reference in New Issue
Block a user