import type { BillingCharge, BillingSubscription } from '@generated/prisma/client'; import type { BillingChargeDto, BillingReturnTo, CountryCode } from '@gruperly/shared'; import { countryCodeSchema, DEFAULT_COUNTRY } from '@gruperly/shared'; export function toBillingChargeDto(charge: BillingCharge): BillingChargeDto { return { id: charge.id, amount: Number(charge.amount.toString()), currency: charge.currency, status: charge.status, paidAt: charge.paidAt?.toISOString() ?? null, createdAt: charge.createdAt.toISOString(), }; } /** The professor's country as stored on the user; Argentina until they pick one. */ export function countryOf(user: { country: string | null } | null): CountryCode { const parsed = countryCodeSchema.safeParse(user?.country); return parsed.success ? parsed.data : DEFAULT_COUNTRY; } /** * Where the billing provider sends the professor after authorizing (or * abandoning): a public API route that bounces to the web * (`GET /billing/return`), because Mercado Pago refuses a `back_url` on * localhost and the web is not tunneled in local development. */ export function buildSubscriptionBackUrl(apiOrigin: string, returnTo: BillingReturnTo): string { return `${apiOrigin}/api/v1/billing/return?to=${returnTo}`; } /** The web page the professor lands on after the provider's checkout. */ export function buildSubscriptionReturnUrl(webOrigin: string, returnTo: BillingReturnTo): string { return `${webOrigin}/${returnTo === 'onboarding' ? 'onboarding' : 'billing'}?sub=return`; } export type SubscriptionRecord = BillingSubscription;