stacker.news/lib/territory.js

31 lines
974 B
JavaScript
Raw Normal View History

import { TERRITORY_GRACE_DAYS, TERRITORY_PERIOD_COST } from './constants'
import { datePivot, diffDays } from './time'
2024-01-03 02:05:49 +00:00
export function nextBilling (relativeTo, billingType) {
if (!relativeTo || billingType === 'ONCE') return null
2024-01-03 02:05:49 +00:00
const pivot = billingType === 'MONTHLY'
2024-01-03 02:05:49 +00:00
? { months: 1 }
: { years: 1 }
return datePivot(new Date(relativeTo), pivot)
2024-01-03 02:05:49 +00:00
}
export function purchasedType (sub) {
if (!sub?.billPaidUntil) return 'ONCE'
2024-03-21 01:56:40 +00:00
return diffDays(new Date(sub.billedLastAt), new Date(sub.billPaidUntil)) >= 364 ? 'YEARLY' : 'MONTHLY'
}
2024-01-03 02:05:49 +00:00
export function proratedBillingCost (sub, newBillingType) {
if (!sub ||
sub.billingType === 'ONCE' ||
sub.billingType === newBillingType.toUpperCase()) return null
2024-01-03 02:05:49 +00:00
return TERRITORY_PERIOD_COST(newBillingType) - TERRITORY_PERIOD_COST(purchasedType(sub))
2024-01-03 02:05:49 +00:00
}
export function nextBillingWithGrace (sub) {
if (!sub) return null
return datePivot(new Date(sub.billPaidUntil), { days: TERRITORY_GRACE_DAYS })
2024-01-03 02:05:49 +00:00
}