i18next + react-i18next con es-AR (fuente), es-419, pt-BR y en; el idioma sale del dispositivo (fallback a inglés) y se puede cambiar en Ajustes. La elección se guarda en localStorage y, para el profe, en users.locale. Las validaciones de shared pasan a claves y los errores de la API se traducen por code (con params para límites de plan y cupo). Migrados: layout, Ajustes, auth, PWA y lib/format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
322 lines
9.9 KiB
TypeScript
322 lines
9.9 KiB
TypeScript
import type { ProblemDetails } from '@gruperly/shared';
|
|
import { notFoundProblem } from './problem-details';
|
|
import { PROBLEM_DOMAIN } from './problem-domain';
|
|
|
|
export function validationProblem(params: {
|
|
detail?: string;
|
|
instance?: string;
|
|
errors?: Record<string, string[]>;
|
|
}): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/validation`,
|
|
title: 'Bad Request',
|
|
status: 400,
|
|
detail: params.detail ?? 'Invalid request data',
|
|
instance: params.instance,
|
|
errors: params.errors,
|
|
};
|
|
}
|
|
|
|
export function conflictProblem(params: {
|
|
detail: string;
|
|
code?: string;
|
|
instance?: string;
|
|
params?: ProblemDetails['params'];
|
|
}): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/conflict`,
|
|
title: 'Conflict',
|
|
status: 409,
|
|
detail: params.detail,
|
|
instance: params.instance,
|
|
code: params.code,
|
|
...(params.params ? { params: params.params } : {}),
|
|
};
|
|
}
|
|
|
|
export function forbiddenProblem(params: {
|
|
detail: string;
|
|
code?: string;
|
|
instance?: string;
|
|
}): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/forbidden`,
|
|
title: 'Forbidden',
|
|
status: 403,
|
|
detail: params.detail,
|
|
instance: params.instance,
|
|
code: params.code,
|
|
};
|
|
}
|
|
|
|
export function databaseUnavailableProblem(params?: {
|
|
instance?: string;
|
|
}): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/database-unavailable`,
|
|
title: 'Service Unavailable',
|
|
status: 503,
|
|
detail: 'Database health check failed',
|
|
instance: params?.instance,
|
|
code: 'database_unavailable',
|
|
};
|
|
}
|
|
|
|
export function noGroupAccessProblem(): ProblemDetails {
|
|
return forbiddenProblem({
|
|
detail: 'You do not have access to this group.',
|
|
code: 'group_access_denied',
|
|
});
|
|
}
|
|
|
|
export function notFoundResourceProblem(resourceName: string, id: string): ProblemDetails {
|
|
return notFoundProblem(`${resourceName} ${id}`);
|
|
}
|
|
|
|
export function mpNotConnectedProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'No tenés una cuenta de Mercado Pago vinculada.',
|
|
code: 'mp_not_connected',
|
|
});
|
|
}
|
|
|
|
export function mpRequiresReauthProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Tu cuenta de Mercado Pago necesita volver a vincularse.',
|
|
code: 'mp_requires_reauth',
|
|
});
|
|
}
|
|
|
|
export function mpAccountInUseProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Esa cuenta de Mercado Pago ya está vinculada a otro profesor.',
|
|
code: 'mp_account_in_use',
|
|
});
|
|
}
|
|
|
|
/** The server is missing MP_CLIENT_ID / MP_CLIENT_SECRET / MP_REDIRECT_URI. */
|
|
export function mpNotConfiguredProblem(): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/mercadopago-not-configured`,
|
|
title: 'Service Unavailable',
|
|
status: 503,
|
|
detail: 'La vinculación con Mercado Pago no está disponible en este momento.',
|
|
code: 'mp_not_configured',
|
|
};
|
|
}
|
|
|
|
/** Mercado Pago refused the request, or could not be reached. */
|
|
export function mpProviderErrorProblem(): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/mercadopago-unavailable`,
|
|
title: 'Bad Gateway',
|
|
status: 502,
|
|
detail: 'No pudimos completar la vinculación con Mercado Pago.',
|
|
code: 'mp_provider_error',
|
|
};
|
|
}
|
|
|
|
/** The server is missing the variables of the active WhatsApp provider. */
|
|
export function whatsappNotConfiguredProblem(): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/whatsapp-not-configured`,
|
|
title: 'Service Unavailable',
|
|
status: 503,
|
|
detail: 'La vinculación con WhatsApp no está disponible en este momento.',
|
|
code: 'whatsapp_not_configured',
|
|
};
|
|
}
|
|
|
|
export function whatsappNotConnectedProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'No tenés un WhatsApp vinculado.',
|
|
code: 'whatsapp_not_connected',
|
|
});
|
|
}
|
|
|
|
export function whatsappNumberInUseProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Ese número de WhatsApp ya está vinculado a otro profesor.',
|
|
code: 'whatsapp_number_in_use',
|
|
});
|
|
}
|
|
|
|
export function whatsappLinkingNotStartedProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Empezá la vinculación de WhatsApp antes de pedir el código.',
|
|
code: 'whatsapp_linking_not_started',
|
|
});
|
|
}
|
|
|
|
/** The provider links by QR or redirect only (Kapso). */
|
|
export function whatsappPairingUnsupportedProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Este proveedor de WhatsApp no permite vincular con el número de teléfono.',
|
|
code: 'whatsapp_pairing_unsupported',
|
|
});
|
|
}
|
|
|
|
/** The session is not ready to hand out a code yet; retryable in seconds. */
|
|
export function whatsappPairingNotReadyProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'WhatsApp todavía está preparando la vinculación. Intentá de nuevo en unos segundos.',
|
|
code: 'whatsapp_pairing_not_ready',
|
|
});
|
|
}
|
|
|
|
/** The WhatsApp gateway refused the request, or could not be reached. */
|
|
export function whatsappProviderErrorProblem(): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/whatsapp-unavailable`,
|
|
title: 'Bad Gateway',
|
|
status: 502,
|
|
detail: 'No pudimos comunicarnos con WhatsApp. Intentá de nuevo en unos minutos.',
|
|
code: 'whatsapp_provider_error',
|
|
};
|
|
}
|
|
|
|
export function onboardingAlreadyCompletedProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Ya completaste el onboarding de Gruperly.',
|
|
code: 'onboarding_already_completed',
|
|
});
|
|
}
|
|
|
|
export function capacityReachedProblem(capacity: number | null): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: capacity
|
|
? `El grupo alcanzó su cupo máximo de ${capacity} miembros.`
|
|
: 'El grupo alcanzó su cupo máximo de miembros.',
|
|
code: 'group_capacity_reached',
|
|
...(capacity ? { params: { capacity } } : {}),
|
|
});
|
|
}
|
|
|
|
export function alreadyWaitlistedProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Este número de teléfono ya está en la lista de espera del grupo.',
|
|
code: 'already_waitlisted',
|
|
});
|
|
}
|
|
|
|
export function attendeeHasPaymentsProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Este miembro tiene cobros asociados. Gestiona o cancela sus cobros antes de quitarlo del grupo.',
|
|
code: 'attendee_has_payments',
|
|
});
|
|
}
|
|
|
|
export function groupWithoutPriceProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Definí el precio del grupo antes de generar los cobros.',
|
|
code: 'group_without_price',
|
|
});
|
|
}
|
|
|
|
export function noAttendeesToBillProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'No hay miembros activos con un cobro pendiente para este grupo.',
|
|
code: 'no_attendees_to_bill',
|
|
});
|
|
}
|
|
function paymentRequiredProblem(params: {
|
|
detail: string;
|
|
code: string;
|
|
params?: ProblemDetails['params'];
|
|
}): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/plan`,
|
|
title: 'Payment Required',
|
|
status: 402,
|
|
detail: params.detail,
|
|
code: params.code,
|
|
...(params.params ? { params: params.params } : {}),
|
|
};
|
|
}
|
|
|
|
export type PlanLimitKind = 'groups' | 'attendees';
|
|
|
|
/** The Free plan does not allow one more group or attendee. */
|
|
export function planLimitReachedProblem(limit: PlanLimitKind, max: number): ProblemDetails {
|
|
return paymentRequiredProblem({
|
|
detail:
|
|
limit === 'groups'
|
|
? `El plan Free permite hasta ${max} ${max === 1 ? 'grupo' : 'grupos'}. Pasate a Pro para crear más.`
|
|
: `El plan Free permite hasta ${max} alumnos activos. Pasate a Pro para sumar más.`,
|
|
code: 'plan_limit_reached',
|
|
params: { limit, max },
|
|
});
|
|
}
|
|
|
|
/** A Pro-only feature: charging students with Mercado Pago or WhatsApp automations. */
|
|
export function planFeatureRequiredProblem(feature: 'mpCharges' | 'whatsappAutomations'): ProblemDetails {
|
|
return paymentRequiredProblem({
|
|
detail:
|
|
feature === 'mpCharges'
|
|
? 'Cobrar con Mercado Pago está disponible en el plan Pro.'
|
|
: 'Los mensajes automáticos de WhatsApp están disponibles en el plan Pro.',
|
|
code: 'plan_feature_required',
|
|
params: { feature },
|
|
});
|
|
}
|
|
|
|
/** Free plan with more usage than its limits: only reads and deletes. */
|
|
export function planReadOnlyProblem(): ProblemDetails {
|
|
return paymentRequiredProblem({
|
|
detail:
|
|
'Tu cuenta está en solo lectura porque superás los límites del plan Free. Pasate a Pro o reducí el uso para volver a editar.',
|
|
code: 'plan_read_only',
|
|
});
|
|
}
|
|
|
|
/** The billing provider of the professor's country is not configured. */
|
|
export function billingNotAvailableProblem(): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/billing-not-available`,
|
|
title: 'Service Unavailable',
|
|
status: 503,
|
|
detail: 'La suscripción a Pro todavía no está disponible en tu país.',
|
|
code: 'billing_not_available',
|
|
};
|
|
}
|
|
|
|
/** The billing provider (Mercado Pago or Stripe) refused the request, or could not be reached. */
|
|
export function billingProviderErrorProblem(): ProblemDetails {
|
|
return {
|
|
type: `${PROBLEM_DOMAIN}/problems/billing-provider-error`,
|
|
title: 'Bad Gateway',
|
|
status: 502,
|
|
detail: 'No pudimos comunicarnos con el procesador de pagos. Intentá de nuevo en unos minutos.',
|
|
code: 'billing_provider_error',
|
|
};
|
|
}
|
|
|
|
/** Mercado Pago makes the payer log in with this exact email, so it cannot be left out. */
|
|
export function payerEmailRequiredProblem(): ProblemDetails {
|
|
return validationProblem({
|
|
detail: 'Ingresá el email de tu cuenta de Mercado Pago.',
|
|
errors: { payerEmail: ['Ingresá el email de tu cuenta de Mercado Pago.'] },
|
|
});
|
|
}
|
|
|
|
export function subscriptionAlreadyActiveProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Ya tenés una suscripción a Pro activa.',
|
|
code: 'subscription_already_active',
|
|
});
|
|
}
|
|
|
|
export function noActiveSubscriptionProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'No tenés una suscripción para cancelar.',
|
|
code: 'no_active_subscription',
|
|
});
|
|
}
|
|
|
|
export function planNotSelectedProblem(): ProblemDetails {
|
|
return conflictProblem({
|
|
detail: 'Elegí un plan antes de crear tu primer grupo.',
|
|
code: 'plan_not_selected',
|
|
});
|
|
}
|