import { z } from 'zod'; // State of the teacher's Mercado Pago account, as the app sees it. // A `revoked` row (the teacher unlinked) is reported as 'none': there is // nothing usable left, but the row is kept so the Mercado Pago user stays // reserved for whoever linked it. export const mpAccountStatusSchema = z.enum(['none', 'active', 'requires_reauth']); export type MpAccountStatus = z.output; // Why the OAuth callback failed. These codes are safe to hand to the browser: // they never describe an internal failure, only a reason the teacher can act on. export const mpCallbackReasonSchema = z.enum([ // 'state' missing, unknown, expired or already used. 'invalid_state', // The teacher rejected the consent screen on Mercado Pago. 'mp_denied', // Mercado Pago refused to exchange the code, or was unreachable. 'mp_error', // That Mercado Pago account is already linked to another teacher. 'mp_account_in_use', ]); export type MpCallbackReason = z.output; // Where the teacher started the flow, so the callback can send them back. export const mpReturnToSchema = z.enum(['onboarding', 'settings']); export type MpReturnTo = z.output; export const MpConnectQuerySchema = z.strictObject({ returnTo: mpReturnToSchema.default('settings'), }); export type MpConnectQuery = z.output; // Deliberately not `strictObject`: this query is written by Mercado Pago, not by // us. Alongside `error` it may send `error_description` and other fields we do // not model, and a strict schema would answer those with a 400 problem body in // the teacher's browser instead of the redirect this route exists to perform. // Unknown keys are stripped and ignored. export const MpCallbackQuerySchema = z.object({ // Mercado Pago may answer with `error` instead of `code` when the teacher // rejects the consent screen, and omits `state` in some failure paths. code: z.string().min(1).optional(), state: z.string().min(1).optional(), error: z.string().min(1).optional(), }); export type MpCallbackQuery = z.output; export const MpAuthorizationUrlDtoSchema = z.object({ authorizationUrl: z.string(), }); export type MpAuthorizationUrlDto = z.output; export const MpStatusDtoSchema = z.object({ status: mpAccountStatusSchema, liveMode: z.boolean(), expiresAt: z.string().nullable(), connectedAt: z.string().nullable(), // Last digits of the Mercado Pago id, so a teacher can tell whether they // linked the account they meant to. Never the full id. mpUserIdSuffix: z.string().nullable(), }); export type MpStatusDto = z.output; export const MpDisconnectResultDtoSchema = z.object({ disconnected: z.boolean(), }); export type MpDisconnectResultDto = z.output;