feat(admin): implement pricing overrides for plans and update pricing logic
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { fetchGeoInfo } from '@/lib/geoip';
|
||||
import { db } from '@/lib/prisma';
|
||||
import { resolvePlanPrice } from '@/modules/plan/services/plan-pricing.service';
|
||||
import { parsePlanRules } from '@/modules/plan/services/plan-rules.service';
|
||||
import type { AppContext } from '@/types/hono';
|
||||
|
||||
@@ -9,13 +11,26 @@ export async function listPlansHandler(c: AppContext) {
|
||||
},
|
||||
});
|
||||
|
||||
const countryParam = c.req.query('country');
|
||||
let countryCode: string | undefined;
|
||||
|
||||
if (countryParam) {
|
||||
countryCode = countryParam;
|
||||
} else {
|
||||
const ip = c.req.header('x-forwarded-for')?.split(',')[0]?.trim() || '127.0.0.1';
|
||||
const geo = await fetchGeoInfo(ip);
|
||||
countryCode = geo?.countryCode || undefined;
|
||||
}
|
||||
|
||||
return c.json(
|
||||
plans.map((plan) => {
|
||||
const rules = parsePlanRules(plan.rules);
|
||||
const resolved = resolvePlanPrice(Number(plan.price), rules, countryCode);
|
||||
return {
|
||||
code: plan.code,
|
||||
name: plan.name,
|
||||
price: Number(plan.price),
|
||||
price: resolved.amount,
|
||||
currency: resolved.currency,
|
||||
features: rules.features,
|
||||
limits: rules.limits,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { PlanRules } from '@repo/api-contract';
|
||||
|
||||
export type PlanPrice = {
|
||||
amount: number;
|
||||
currency: string;
|
||||
};
|
||||
|
||||
export function resolvePlanPrice(
|
||||
defaultPrice: number,
|
||||
rules: PlanRules,
|
||||
countryCode?: string
|
||||
): PlanPrice {
|
||||
if (!countryCode) {
|
||||
return { amount: defaultPrice, currency: 'USD' };
|
||||
}
|
||||
|
||||
if (rules.version === 'v2' && rules.pricing?.overrides) {
|
||||
const override = rules.pricing.overrides[countryCode];
|
||||
if (override) {
|
||||
return { amount: override.amount, currency: override.currency };
|
||||
}
|
||||
}
|
||||
|
||||
return { amount: defaultPrice, currency: 'USD' };
|
||||
}
|
||||
Reference in New Issue
Block a user