Backups: subscription info improvements

This commit is contained in:
trevor-signal 2025-06-25 14:56:52 -04:00 committed by GitHub
commit 2e9dae6b1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 146 additions and 78 deletions

View file

@ -1414,17 +1414,50 @@ const backupFileHeadersSchema = z.object({
type BackupFileHeadersType = z.infer<typeof backupFileHeadersSchema>;
// See: https://docs.stripe.com/currencies?presentment-currency=US
const ZERO_DECIMAL_CURRENCIES = new Set([
'bif',
'clp',
'djf',
'gnf',
'jpy',
'kmf',
'krw',
'mga',
'pyg',
'rwf',
'vnd',
'vuv',
'xaf',
'xof',
'xpf',
]);
const secondsTimestampToDate = z.coerce
.number()
.transform(sec => new Date(sec * 1_000));
const subscriptionResponseSchema = z.object({
subscription: z
.object({
level: z.number(),
billingCycleAnchor: z.coerce.date().optional(),
endOfCurrentPeriod: z.coerce.date().optional(),
billingCycleAnchor: secondsTimestampToDate.optional(),
endOfCurrentPeriod: secondsTimestampToDate.optional(),
active: z.boolean(),
cancelAtPeriodEnd: z.boolean().optional(),
currency: z.string().optional(),
amount: z.number().nonnegative().optional(),
})
.transform(data => {
const result = { ...data };
if (result.currency && result.amount) {
result.amount = ZERO_DECIMAL_CURRENCIES.has(
result.currency.toLowerCase()
)
? result.amount
: result.amount / 100;
}
return result;
})
.nullish(),
});