bd sync: 2026-01-12 16:57:37
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
export function resolveMaxCount(values: number[]): number {
|
||||
if (!Array.isArray(values) || values.length === 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return Math.max(...values, 1);
|
||||
}
|
||||
|
||||
export function resolveTimelineHours(timestamps: string[], fallbackHours = 12): number {
|
||||
if (!Array.isArray(timestamps) || timestamps.length < 2) {
|
||||
return fallbackHours;
|
||||
}
|
||||
|
||||
const times = timestamps
|
||||
.map((value) => new Date(value).getTime())
|
||||
.filter((value) => Number.isFinite(value));
|
||||
|
||||
if (times.length < 2) {
|
||||
return fallbackHours;
|
||||
}
|
||||
|
||||
const min = Math.min(...times);
|
||||
const max = Math.max(...times);
|
||||
const diff = Math.max(0, max - min);
|
||||
const hours = diff / (1000 * 60 * 60);
|
||||
|
||||
return Math.max(1, Math.round(hours));
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
export type PendingCheckout = {
|
||||
packageId: number | null;
|
||||
checkoutSessionId?: string | null;
|
||||
startedAt: number;
|
||||
};
|
||||
|
||||
export const PENDING_CHECKOUT_TTL_MS = 1000 * 60 * 30;
|
||||
export const CHECKOUT_STORAGE_KEY = 'admin.billing.checkout.pending.v1';
|
||||
|
||||
export function isCheckoutExpired(
|
||||
pending: PendingCheckout,
|
||||
now = Date.now(),
|
||||
ttl = PENDING_CHECKOUT_TTL_MS,
|
||||
): boolean {
|
||||
return now - pending.startedAt > ttl;
|
||||
}
|
||||
|
||||
export function loadPendingCheckout(
|
||||
now = Date.now(),
|
||||
ttl = PENDING_CHECKOUT_TTL_MS,
|
||||
): PendingCheckout | null {
|
||||
if (typeof window === 'undefined') {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const raw = window.sessionStorage.getItem(CHECKOUT_STORAGE_KEY);
|
||||
if (! raw) {
|
||||
return null;
|
||||
}
|
||||
const parsed = JSON.parse(raw) as PendingCheckout;
|
||||
if (typeof parsed?.startedAt !== 'number') {
|
||||
return null;
|
||||
}
|
||||
const packageId =
|
||||
typeof parsed.packageId === 'number' && Number.isFinite(parsed.packageId)
|
||||
? parsed.packageId
|
||||
: null;
|
||||
const checkoutSessionId = typeof parsed.checkoutSessionId === 'string' ? parsed.checkoutSessionId : null;
|
||||
if (now - parsed.startedAt > ttl) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
packageId,
|
||||
checkoutSessionId,
|
||||
startedAt: parsed.startedAt,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function storePendingCheckout(next: PendingCheckout | null): void {
|
||||
if (typeof window === 'undefined') {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (! next) {
|
||||
window.sessionStorage.removeItem(CHECKOUT_STORAGE_KEY);
|
||||
} else {
|
||||
window.sessionStorage.setItem(CHECKOUT_STORAGE_KEY, JSON.stringify(next));
|
||||
}
|
||||
} catch {
|
||||
// Ignore storage errors.
|
||||
}
|
||||
}
|
||||
|
||||
export function shouldClearPendingCheckout(
|
||||
pending: PendingCheckout,
|
||||
activePackageId: number | null,
|
||||
now = Date.now(),
|
||||
ttl = PENDING_CHECKOUT_TTL_MS,
|
||||
): boolean {
|
||||
if (isCheckoutExpired(pending, now, ttl)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pending.packageId && activePackageId && pending.packageId === activePackageId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,146 +0,0 @@
|
||||
import type { Package } from '../../api';
|
||||
|
||||
type PackageChange = {
|
||||
isUpgrade: boolean;
|
||||
isDowngrade: boolean;
|
||||
};
|
||||
|
||||
export type PackageComparisonRow =
|
||||
| {
|
||||
id: string;
|
||||
type: 'limit';
|
||||
limitKey: 'max_photos' | 'max_guests' | 'gallery_days';
|
||||
}
|
||||
| {
|
||||
id: string;
|
||||
type: 'feature';
|
||||
featureKey: string;
|
||||
};
|
||||
|
||||
function normalizePackageFeatures(pkg: Package | null): string[] {
|
||||
if (!pkg?.features) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (Array.isArray(pkg.features)) {
|
||||
return pkg.features.filter((feature): feature is string => typeof feature === 'string' && feature.trim().length > 0);
|
||||
}
|
||||
|
||||
if (typeof pkg.features === 'object') {
|
||||
return Object.entries(pkg.features)
|
||||
.filter(([, enabled]) => enabled)
|
||||
.map(([key]) => key);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
export function getEnabledPackageFeatures(pkg: Package): string[] {
|
||||
return normalizePackageFeatures(pkg);
|
||||
}
|
||||
|
||||
function collectFeatures(pkg: Package | null): Set<string> {
|
||||
return new Set(normalizePackageFeatures(pkg));
|
||||
}
|
||||
|
||||
function compareLimit(candidate: number | null, active: number | null): number {
|
||||
if (active === null) {
|
||||
return candidate === null ? 0 : -1;
|
||||
}
|
||||
|
||||
if (candidate === null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (candidate > active) return 1;
|
||||
if (candidate < active) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export function classifyPackageChange(pkg: Package, active: Package | null): PackageChange {
|
||||
if (!active) {
|
||||
return { isUpgrade: false, isDowngrade: false };
|
||||
}
|
||||
|
||||
const activeFeatures = collectFeatures(active);
|
||||
const candidateFeatures = collectFeatures(pkg);
|
||||
|
||||
const hasFeatureUpgrade = Array.from(candidateFeatures).some((feature) => !activeFeatures.has(feature));
|
||||
const hasFeatureDowngrade = Array.from(activeFeatures).some((feature) => !candidateFeatures.has(feature));
|
||||
|
||||
const limitKeys: Array<keyof Package> = ['max_photos', 'max_guests', 'gallery_days'];
|
||||
let hasLimitUpgrade = false;
|
||||
let hasLimitDowngrade = false;
|
||||
|
||||
limitKeys.forEach((key) => {
|
||||
const candidateLimit = pkg[key] ?? null;
|
||||
const activeLimit = active[key] ?? null;
|
||||
const delta = compareLimit(candidateLimit, activeLimit);
|
||||
if (delta > 0) {
|
||||
hasLimitUpgrade = true;
|
||||
} else if (delta < 0) {
|
||||
hasLimitDowngrade = true;
|
||||
}
|
||||
});
|
||||
|
||||
const hasUpgrade = hasFeatureUpgrade || hasLimitUpgrade;
|
||||
const hasDowngrade = hasFeatureDowngrade || hasLimitDowngrade;
|
||||
|
||||
if (hasUpgrade && !hasDowngrade) {
|
||||
return { isUpgrade: true, isDowngrade: false };
|
||||
}
|
||||
|
||||
if (hasDowngrade) {
|
||||
return { isUpgrade: false, isDowngrade: true };
|
||||
}
|
||||
|
||||
return { isUpgrade: false, isDowngrade: false };
|
||||
}
|
||||
|
||||
export function selectRecommendedPackageId(
|
||||
packages: Package[],
|
||||
feature: string | null,
|
||||
activePackage: Package | null
|
||||
): number | null {
|
||||
if (!feature) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const candidates = packages.filter((pkg) => normalizePackageFeatures(pkg).includes(feature));
|
||||
if (candidates.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const upgrades = candidates.filter((pkg) => classifyPackageChange(pkg, activePackage).isUpgrade);
|
||||
const pool = upgrades.length ? upgrades : candidates;
|
||||
const sorted = [...pool].sort((a, b) => a.price - b.price);
|
||||
|
||||
return sorted[0]?.id ?? null;
|
||||
}
|
||||
|
||||
export function buildPackageComparisonRows(packages: Package[]): PackageComparisonRow[] {
|
||||
const limitRows: PackageComparisonRow[] = [
|
||||
{ id: 'limit.max_photos', type: 'limit', limitKey: 'max_photos' },
|
||||
{ id: 'limit.max_guests', type: 'limit', limitKey: 'max_guests' },
|
||||
{ id: 'limit.gallery_days', type: 'limit', limitKey: 'gallery_days' },
|
||||
];
|
||||
|
||||
const featureKeys = new Set<string>();
|
||||
packages.forEach((pkg) => {
|
||||
normalizePackageFeatures(pkg).forEach((key) => {
|
||||
if (key !== 'photos') {
|
||||
featureKeys.add(key);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const featureRows = Array.from(featureKeys)
|
||||
.sort((a, b) => a.localeCompare(b))
|
||||
.map((featureKey) => ({
|
||||
id: `feature.${featureKey}`,
|
||||
type: 'feature' as const,
|
||||
featureKey,
|
||||
}));
|
||||
|
||||
return [...limitRows, ...featureRows];
|
||||
}
|
||||
@@ -15,8 +15,7 @@ const t = (key: string, options?: Record<string, unknown> | string) => {
|
||||
return template
|
||||
.replace('{{used}}', String(options?.used ?? '{{used}}'))
|
||||
.replace('{{limit}}', String(options?.limit ?? '{{limit}}'))
|
||||
.replace('{{remaining}}', String(options?.remaining ?? '{{remaining}}'))
|
||||
.replace('{{count}}', String(options?.count ?? '{{count}}'));
|
||||
.replace('{{remaining}}', String(options?.remaining ?? '{{remaining}}'));
|
||||
};
|
||||
|
||||
describe('packageSummary helpers', () => {
|
||||
@@ -54,12 +53,6 @@ describe('packageSummary helpers', () => {
|
||||
expect(result[0].value).toBe('30 of 120 remaining');
|
||||
});
|
||||
|
||||
it('falls back to remaining count when remaining exceeds limit', () => {
|
||||
const result = getPackageLimitEntries({ max_photos: 120, remaining_photos: 180 }, t);
|
||||
|
||||
expect(result[0].value).toBe('Remaining 180');
|
||||
});
|
||||
|
||||
it('formats event usage copy', () => {
|
||||
const result = formatEventUsage(3, 10, t);
|
||||
|
||||
|
||||
@@ -138,12 +138,6 @@ const formatLimitWithRemaining = (limit: number | null, remaining: number | null
|
||||
|
||||
if (remaining !== null && remaining >= 0) {
|
||||
const normalizedRemaining = Number.isFinite(remaining) ? Math.max(0, Math.round(remaining)) : remaining;
|
||||
if (normalizedRemaining > limit) {
|
||||
return t('mobileBilling.usage.remaining', {
|
||||
count: normalizedRemaining,
|
||||
defaultValue: 'Remaining {{count}}',
|
||||
});
|
||||
}
|
||||
return t('mobileBilling.usage.remainingOf', {
|
||||
remaining: normalizedRemaining,
|
||||
limit,
|
||||
|
||||
Reference in New Issue
Block a user