- Implemented MpReauthNotice component to notify users when reauthorization is needed. - Created MpStatusCard component to display the current status of the Mercado Pago account. - Added utility functions and constants for handling Mercado Pago status and messages. - Introduced hooks for managing the connection and disconnection of Mercado Pago accounts. - Developed a disconnect confirmation modal for user actions. - Added tests for the new functionality and utility functions. - Defined schemas for Mercado Pago account status and callback reasons in shared package.
67 lines
2.9 KiB
TypeScript
67 lines
2.9 KiB
TypeScript
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<typeof mpAccountStatusSchema>;
|
|
|
|
// 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<typeof mpCallbackReasonSchema>;
|
|
|
|
// 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<typeof mpReturnToSchema>;
|
|
|
|
export const MpConnectQuerySchema = z.strictObject({
|
|
returnTo: mpReturnToSchema.default('settings'),
|
|
});
|
|
export type MpConnectQuery = z.output<typeof MpConnectQuerySchema>;
|
|
|
|
// 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<typeof MpCallbackQuerySchema>;
|
|
|
|
export const MpAuthorizationUrlDtoSchema = z.object({
|
|
authorizationUrl: z.string(),
|
|
});
|
|
export type MpAuthorizationUrlDto = z.output<typeof MpAuthorizationUrlDtoSchema>;
|
|
|
|
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<typeof MpStatusDtoSchema>;
|
|
|
|
export const MpDisconnectResultDtoSchema = z.object({
|
|
disconnected: z.boolean(),
|
|
});
|
|
export type MpDisconnectResultDto = z.output<typeof MpDisconnectResultDtoSchema>;
|