import { getGameCharacters, findCharacterByName } from './characters' import { checkSecurityToken } from './security-token' import { isValidCharName } from './store' /** * Comprueba que un regalo se puede enviar: personaje de origen tuyo, personaje * de destino existente y token de seguridad correcto. * * La usan las DOS rutas: `/api/gift/check` (el botón "Mostrar Regalos", que * valida antes de enseñar el catálogo, como el original) y `/api/gift/send`, que * vuelve a validarlo porque el cliente puede saltarse el primer paso. * * Devuelve el nombre CANÓNICO del destino: el usuario puede escribirlo en * mayúsculas, minúsculas o mezcladas, y al correo tiene que ir el de la BD. */ export async function checkGift( accountId: number, body: { source?: string; destination?: string; security_token?: string }, ): Promise<{ source: string; destination: string } | { error: string }> { const source = String(body.source ?? '').trim() const destination = String(body.destination ?? '').trim() const token = String(body.security_token ?? '').trim() if (!source || !destination || !token) return { error: 'missingFields' } if (!isValidCharName(destination)) return { error: 'invalidDestination' } const chars = await getGameCharacters(accountId) if (!chars.find((c) => c.name === source)) return { error: 'invalidSource' } const dest = await findCharacterByName(destination) if (!dest) return { error: 'destinationNotFound' } if (!(await checkSecurityToken(accountId, token))) return { error: 'invalidToken' } return { source, destination: dest.name } }