longevity: registraMisure, unico varco con validazione sul registro
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
import type Database from 'better-sqlite3';
|
||||||
|
import { esisteTest } from './registro';
|
||||||
|
|
||||||
|
export type Fonte = 'manuale' | 'questionario' | 'wellness_tower' | 'vald' | 'calibre' | 'stress_index';
|
||||||
|
|
||||||
|
export type MisuraIn = {
|
||||||
|
test_id: string;
|
||||||
|
valore_num?: number;
|
||||||
|
valore_txt?: string;
|
||||||
|
unita?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function apriSessione(
|
||||||
|
db: Database.Database,
|
||||||
|
s: {
|
||||||
|
client_code: string; data: string; tipo: 'checkup' | 'questionario';
|
||||||
|
eta_alla_data?: number; operatore?: string; quest_version?: string;
|
||||||
|
}
|
||||||
|
): number {
|
||||||
|
const r = db.prepare(
|
||||||
|
`INSERT INTO sessioni (client_code, data, tipo, eta_alla_data, operatore, quest_version)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)`
|
||||||
|
).run(s.client_code, s.data, s.tipo, s.eta_alla_data ?? null, s.operatore ?? null, s.quest_version ?? null);
|
||||||
|
return Number(r.lastInsertRowid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* L'unico punto in cui si scrive nella tabella `misure`.
|
||||||
|
* Un test_id sconosciuto fa fallire l'intero lotto: meglio un errore subito che una
|
||||||
|
* sessione scritta a metà. Un valore fuori dal range atteso invece si scrive e si
|
||||||
|
* marca — scartarlo perderebbe un dato vero, clamparlo lo falserebbe.
|
||||||
|
*/
|
||||||
|
export function registraMisure(
|
||||||
|
db: Database.Database,
|
||||||
|
sessioneId: number,
|
||||||
|
fonte: Fonte,
|
||||||
|
misure: MisuraIn[]
|
||||||
|
): { scritte: number; fuoriRange: string[] } {
|
||||||
|
const sess = db.prepare(`SELECT client_code FROM sessioni WHERE id = ?`).get(sessioneId) as
|
||||||
|
{ client_code: string } | undefined;
|
||||||
|
if (!sess) throw new Error(`sessione ${sessioneId} inesistente`);
|
||||||
|
|
||||||
|
for (const m of misure) {
|
||||||
|
if (!esisteTest(db, m.test_id)) {
|
||||||
|
throw new Error(`test_id non nel registro: ${m.test_id}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const range = db.prepare(`SELECT range_min, range_max FROM registro_test WHERE test_id = ?`);
|
||||||
|
const ins = db.prepare(
|
||||||
|
`INSERT INTO misure (sessione_id, client_code, test_id, valore_num, valore_txt, unita, fonte, fuori_range)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||||
|
);
|
||||||
|
const fuoriRange: string[] = [];
|
||||||
|
|
||||||
|
const tx = db.transaction(() => {
|
||||||
|
for (const m of misure) {
|
||||||
|
const r = range.get(m.test_id) as { range_min: number | null; range_max: number | null };
|
||||||
|
let fuori = 0;
|
||||||
|
if (m.valore_num !== undefined) {
|
||||||
|
if ((r.range_min !== null && m.valore_num < r.range_min) ||
|
||||||
|
(r.range_max !== null && m.valore_num > r.range_max)) {
|
||||||
|
fuori = 1;
|
||||||
|
fuoriRange.push(m.test_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ins.run(sessioneId, sess.client_code, m.test_id,
|
||||||
|
m.valore_num ?? null, m.valore_txt ?? null, m.unita ?? null, fonte, fuori);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
tx();
|
||||||
|
|
||||||
|
return { scritte: misure.length, fuoriRange };
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { createLongevityDb } from '../../src/lib/longevity/db';
|
||||||
|
import { seedRegistro } from '../../src/lib/longevity/registro';
|
||||||
|
import { apriSessione, registraMisure } from '../../src/lib/longevity/misure';
|
||||||
|
|
||||||
|
function dbPronto() {
|
||||||
|
const db = createLongevityDb(':memory:');
|
||||||
|
seedRegistro(db);
|
||||||
|
db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0001', 'F')`).run();
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('registrazione delle misure', () => {
|
||||||
|
it('scrive le misure di una sessione', () => {
|
||||||
|
const db = dbPronto();
|
||||||
|
const s = apriSessione(db, { client_code: 'ISL-0001', data: '2026-08-21', tipo: 'questionario', quest_version: 'v1.0' });
|
||||||
|
const esito = registraMisure(db, s, 'questionario', [
|
||||||
|
{ test_id: 'q_ore_sonno', valore_num: 7.5 },
|
||||||
|
{ test_id: 'q_riposato', valore_num: 8 },
|
||||||
|
]);
|
||||||
|
expect(esito.scritte).toBe(2);
|
||||||
|
const n = db.prepare(`SELECT COUNT(*) n FROM misure WHERE sessione_id = ?`).get(s) as { n: number };
|
||||||
|
expect(n.n).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rifiuta un test sconosciuto e non scrive niente del lotto', () => {
|
||||||
|
const db = dbPronto();
|
||||||
|
const s = apriSessione(db, { client_code: 'ISL-0001', data: '2026-08-21', tipo: 'checkup' });
|
||||||
|
expect(() =>
|
||||||
|
registraMisure(db, s, 'manuale', [
|
||||||
|
{ test_id: 'q_ore_sonno', valore_num: 7 },
|
||||||
|
{ test_id: 'test_inventato', valore_num: 1 },
|
||||||
|
])
|
||||||
|
).toThrow(/test_inventato/);
|
||||||
|
const n = db.prepare(`SELECT COUNT(*) n FROM misure`).get() as { n: number };
|
||||||
|
expect(n.n).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('un valore fuori dal range atteso si scrive e si marca', () => {
|
||||||
|
const db = dbPronto();
|
||||||
|
const s = apriSessione(db, { client_code: 'ISL-0001', data: '2026-08-21', tipo: 'questionario' });
|
||||||
|
const esito = registraMisure(db, s, 'questionario', [{ test_id: 'q_ore_sonno', valore_num: 26 }]);
|
||||||
|
expect(esito.fuoriRange).toEqual(['q_ore_sonno']);
|
||||||
|
const row = db.prepare(`SELECT fuori_range FROM misure WHERE test_id = 'q_ore_sonno'`).get() as { fuori_range: number };
|
||||||
|
expect(row.fuori_range).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('la sessione porta la versione del questionario', () => {
|
||||||
|
const db = dbPronto();
|
||||||
|
const s = apriSessione(db, { client_code: 'ISL-0001', data: '2026-08-21', tipo: 'questionario', quest_version: 'v1.0' });
|
||||||
|
const row = db.prepare(`SELECT quest_version FROM sessioni WHERE id = ?`).get(s) as { quest_version: string };
|
||||||
|
expect(row.quest_version).toBe('v1.0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('il client_code della misura viene dalla sessione, non da chi chiama', () => {
|
||||||
|
const db = dbPronto();
|
||||||
|
db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0002', 'M')`).run();
|
||||||
|
const s = apriSessione(db, { client_code: 'ISL-0002', data: '2026-08-21', tipo: 'checkup' });
|
||||||
|
registraMisure(db, s, 'manuale', [{ test_id: 'q_riposato', valore_num: 5 }]);
|
||||||
|
const row = db.prepare(`SELECT client_code FROM misure`).get() as { client_code: string };
|
||||||
|
expect(row.client_code).toBe('ISL-0002');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user