feat(mp): add Mercado Pago reauthorization notice and status card components

- 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.
This commit is contained in:
Jose Selesan
2026-10-07 12:00:38 -03:00
parent e03f510874
commit 21a4c69f38
64 changed files with 3154 additions and 425 deletions

View File

@@ -0,0 +1,87 @@
import type { MpAccountStatus, MpCallbackReason } from '@gruperly/shared'
/* Copy de Mercado Pago en un solo lugar: el paso del onboarding, la tarjeta de
Ajustes y el aviso global tienen que decir exactamente lo mismo. Las funciones
son puras para poder cubrirlas con tests sin renderizar nada. */
export const MP_PROVIDER_NAME = 'Mercado Pago'
export const MP_CONNECT_ERROR_MESSAGE =
'No pudimos abrir Mercado Pago. Revisá tu conexión e intentá de nuevo.'
/** El resultado de la vinculación, tal como vuelve en `?mp=` de la URL. */
export type MpCallbackFeedback =
| { kind: 'success' }
| { kind: 'error'; reason: MpCallbackReason }
/** Traduce `?mp=` de la URL a la retroalimentación que se muestra en pantalla. */
export function readMpCallbackFeedback(
mp: string | undefined,
reason: string | undefined,
): MpCallbackFeedback | null {
if (mp === 'success') return { kind: 'success' }
if (mp !== 'error') return null
// Un `reason` desconocido o ausente se trata como un fallo genérico: el
// backend manda el código, nunca el mensaje.
const known: readonly MpCallbackReason[] = [
'invalid_state',
'mp_denied',
'mp_error',
'mp_account_in_use',
]
return {
kind: 'error',
reason: known.includes(reason as MpCallbackReason) ? (reason as MpCallbackReason) : 'mp_error',
}
}
const REASON_MESSAGES: Record<MpCallbackReason, string> = {
invalid_state: 'La vinculación venció o ya se había usado. Volvé a intentarlo.',
mp_denied: 'No autorizaste la conexión en Mercado Pago.',
mp_error: 'Mercado Pago no pudo completar la vinculación.',
mp_account_in_use: 'Esa cuenta de Mercado Pago ya está vinculada a otro profesor.',
}
export function mpCallbackErrorMessage(reason: MpCallbackReason): string {
return REASON_MESSAGES[reason]
}
export function mpStatusLabel(status: MpAccountStatus): string {
switch (status) {
case 'active':
return 'Vinculada'
case 'requires_reauth':
return 'Necesita vincularse de nuevo'
case 'none':
return 'Sin vincular'
}
}
export function mpStatusBadgeVariant(
status: MpAccountStatus,
): 'success' | 'warning' | 'neutral' {
switch (status) {
case 'active':
return 'success'
case 'requires_reauth':
return 'warning'
case 'none':
return 'neutral'
}
}
/** `2026-10-05T12:00:00.000Z` → `5 oct 2026`. */
export function formatMpConnectionDate(iso: string | null): string | null {
if (!iso) return null
const date = new Date(iso)
if (Number.isNaN(date.getTime())) return null
return new Intl.DateTimeFormat('es', { day: 'numeric', month: 'short', year: 'numeric' }).format(
date,
)
}
/** Cuándo conviene avisarle al profesor que vuelva a vincular. */
export function shouldShowMpReauthNotice(status: MpAccountStatus): boolean {
return status === 'requires_reauth'
}