Highlight upgrades in package shop
This commit is contained in:
40
resources/js/admin/mobile/lib/packageShop.ts
Normal file
40
resources/js/admin/mobile/lib/packageShop.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { Package } from '../../api';
|
||||
|
||||
export function classifyPackageTier(price: number, activePrice: number | null): {
|
||||
isUpgrade: boolean;
|
||||
isDowngrade: boolean;
|
||||
} {
|
||||
if (activePrice === null) {
|
||||
return { isUpgrade: false, isDowngrade: false };
|
||||
}
|
||||
|
||||
return {
|
||||
isUpgrade: price > activePrice,
|
||||
isDowngrade: price < activePrice,
|
||||
};
|
||||
}
|
||||
|
||||
export function selectRecommendedPackageId(
|
||||
packages: Package[],
|
||||
feature: string | null,
|
||||
activePrice: number | null
|
||||
): number | null {
|
||||
if (!feature) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const candidates = packages.filter((pkg) => pkg.features?.[feature]);
|
||||
if (candidates.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const upgradeCandidates =
|
||||
activePrice === null
|
||||
? candidates
|
||||
: candidates.filter((pkg) => pkg.price > activePrice);
|
||||
|
||||
const pool = upgradeCandidates.length ? upgradeCandidates : candidates;
|
||||
const sorted = [...pool].sort((a, b) => a.price - b.price);
|
||||
|
||||
return sorted[0]?.id ?? null;
|
||||
}
|
||||
Reference in New Issue
Block a user