Merge branch 'lavoro/goldrake-i7' into lavoro/goldrake
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import { getLongevityDb } from '../../../../lib/longevity/db';
|
||||
import { verificaMisura } from '../../../../lib/longevity/misure';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
const json = (status: number, body: object) =>
|
||||
new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } });
|
||||
|
||||
/**
|
||||
* La decisione del trainer su una misura fuori range (issue #7): confermarla o scartarla,
|
||||
* dalla scheda cliente del gestionale (le pagine arrivano con #2, questo endpoint no).
|
||||
* `canAccessPath` (auth.ts) gia' chiude /api/longevity/gestionale/** a chi non e' trainer
|
||||
* o admin: qui non c'e' nessun controllo di ruolo da ripetere.
|
||||
*
|
||||
* Il codice cliente per il redirect si legge SEMPRE dalla misura nel database, mai da un
|
||||
* campo del form — stesso principio di questionario.ts (il client_code non si fida di chi
|
||||
* scrive la richiesta), applicato qui al lato trainer: un id di misura non e' un fascicolo
|
||||
* che si possa dirottare scrivendo un altro codice nel form.
|
||||
*/
|
||||
export const POST: APIRoute = async ({ request, locals }) => {
|
||||
const form = await request.formData();
|
||||
|
||||
const misuraIdRaw = form.get('misura_id');
|
||||
const misuraId = typeof misuraIdRaw === 'string' ? Number(misuraIdRaw) : NaN;
|
||||
if (!Number.isInteger(misuraId)) return json(404, { error: 'misura inesistente' });
|
||||
|
||||
const db = getLongevityDb();
|
||||
|
||||
// Si legge il client_code PRIMA di chiamare verificaMisura: serve per il redirect anche
|
||||
// quando la verifica viene rifiutata (gia' verificata, nel range...) — l'unico caso senza
|
||||
// una scheda cliente su cui tornare e' proprio l'id che non esiste.
|
||||
const riga = db.prepare(`SELECT client_code FROM misure WHERE id = ?`).get(misuraId) as
|
||||
{ client_code: string } | undefined;
|
||||
if (!riga) return json(404, { error: `misura ${misuraId} inesistente` });
|
||||
|
||||
const tornaA = (query: string): Response => new Response(null, {
|
||||
status: 303,
|
||||
headers: { Location: `/longevity/gestionale/cliente/${riga.client_code}${query}` },
|
||||
});
|
||||
|
||||
const esitoRaw = form.get('esito');
|
||||
const esito: 'confermata' | 'scartata' | null =
|
||||
esitoRaw === 'confermata' || esitoRaw === 'scartata' ? esitoRaw : null;
|
||||
if (esito === null) {
|
||||
return tornaA(`?errore=${encodeURIComponent('Esito non valido: deve essere confermata o scartata.')}`);
|
||||
}
|
||||
|
||||
const risultato = verificaMisura(db, { misuraId, esito, operatore: locals.user!.username });
|
||||
if (!risultato.ok) return tornaA(`?errore=${encodeURIComponent(risultato.errore)}`);
|
||||
return tornaA(`?verificata=${esito}`);
|
||||
};
|
||||
Reference in New Issue
Block a user