--- UNTRACKED --- docs/sprint7c-f1-capture-hotfix.md scripts/health/migrate_sprint7c_f1_capture_hotfix.py tests/test_dashboard_v5_sprint7c_f1.py --- RELEVANT DIFF --- diff --git a/scripts/health/assets/health-assets/dashboard-v5-capture.js b/scripts/health/assets/health-assets/dashboard-v5-capture.js index 78e0179..5ff2f7f 100644 --- a/scripts/health/assets/health-assets/dashboard-v5-capture.js +++ b/scripts/health/assets/health-assets/dashboard-v5-capture.js @@ -124,11 +124,12 @@ function closeCurrent(fromHistory = false) { if (hub.open) hub.close(); if (dialog.open) dialog.close(); state.files.forEach(item => URL.revokeObjectURL(item.url)); state.files = []; if (!fromHistory && new URL(location.href).searchParams.has('capture')) history.back(); } const launcher=document.querySelector('#open-capture-hub');if(launcher&&window.matchMedia('(max-width: 680px)').matches)document.body.append(launcher);launcher?.addEventListener('click', () => openHub()); document.querySelector('[data-day-capture]')?.addEventListener('click', () => openHub()); document.querySelectorAll('[data-close-dialog]').forEach(button => button.addEventListener('click', () => closeCurrent())); - hub.querySelectorAll('[data-capture-type]').forEach(button => button.addEventListener('click', async () => {if(['medication','supplement'].includes(button.dataset.captureType))await ensurePlans();openType(button.dataset.captureType)})); + function openMedicationRecord() { hub.close(); updateUrl(null,{replace:true}); if(typeof window.healthRecordOpenTab==='function')window.healthRecordOpenTab('medications'); else status.textContent='Die Medikationsakte ist technisch nicht verfügbar.'; } + hub.querySelectorAll('[data-capture-type]').forEach(button => button.addEventListener('click', async () => {if(button.dataset.captureType==='medication'){openMedicationRecord();return;}if(button.dataset.captureType==='supplement')await ensurePlans();openType(button.dataset.captureType)})); hub.querySelector('[data-capture-document]')?.addEventListener('click',()=>{hub.close();updateUrl(null,{replace:true});window.healthRecordOpenDocumentUpload?.();}); hub.querySelector('[data-open-full-checkin]')?.addEventListener('click', () => { hub.close(); const checkin=document.querySelector('#checkin-dialog'); if(checkin){document.body.append(checkin);checkin.showModal();} updateUrl(null, {replace:true}); }); - window.addEventListener('popstate', async () => { const capture = new URL(location.href).searchParams.get('capture'); closeCurrent(true); if (capture === 'menu') openHub(false); else if (labels[capture]) {if(['medication','supplement'].includes(capture))await ensurePlans();openType(capture, false);} }); - const initial = new URL(location.href).searchParams.get('capture'); if (initial === 'menu') openHub(false); else if (labels[initial]) {if(['medication','supplement'].includes(initial))ensurePlans().then(()=>openType(initial,false));else openType(initial, false);} + window.addEventListener('popstate', async () => { const capture = new URL(location.href).searchParams.get('capture'); closeCurrent(true); if (capture === 'menu') openHub(false); else if(capture==='medication')openMedicationRecord(); else if (labels[capture]) {if(capture==='supplement')await ensurePlans();openType(capture, false);} }); + const initial = new URL(location.href).searchParams.get('capture'); if (initial === 'menu') openHub(false); else if(initial==='medication')openMedicationRecord(); else if (labels[initial]) {if(initial==='supplement')ensurePlans().then(()=>openType(initial,false));else openType(initial, false);} function addSelectedFiles(input) { warning.textContent = ''; diff --git a/scripts/health/assets/health-assets/dashboard-v5-day-controller.js b/scripts/health/assets/health-assets/dashboard-v5-day-controller.js index f7fb734..ad75926 100644 --- a/scripts/health/assets/health-assets/dashboard-v5-day-controller.js +++ b/scripts/health/assets/health-assets/dashboard-v5-day-controller.js @@ -11,7 +11,7 @@ const qa = (selector, root = document) => Array.from(root.querySelectorAll(selector)); const bundle = (() => { try { return JSON.parse(q('#health-dashboard-bundle')?.textContent || '{}'); } catch (_e) { return {}; } })(); let today = validDay(bundle.today) ? bundle.today : ''; - let currentDay = today; + let currentDay = dayRoute(new URLSearchParams(location.search)) || today; let previousView = 'today'; let dayRequestVersion = 0; let dayRequestAbort = null; @@ -265,7 +265,11 @@ q('[data-day-next]').addEventListener('click',()=>openDay({date:shiftDay(currentDay,1),source:'next'})); q('[data-day-calendar]').addEventListener('click',()=>{ selectView('calendar'); ensureCalendar(); calendar.gotoDate(currentDay); }); q('[data-day-compare]').addEventListener('click',()=>{ window.healthRange?.set({from:shiftDay(currentDay,-6),to:currentDay,preset:'custom'}); selectView('explorer'); }); - q('[data-day-symptom]').addEventListener('click',()=>q('#open-checkin')?.click()); + q('[data-day-symptom]').addEventListener('click',()=>{ + const date=currentDay; + if(typeof window.healthOpenSymptomCheckin==='function')window.healthOpenSymptomCheckin(date); + else q('[data-day-status]').textContent='Der bestehende Symptomdialog ist technisch nicht verfügbar.'; + }); q('[data-day-header-open]')?.addEventListener('click',()=>openDay({date:q('[data-day-header-date]').value,source:'header'})); q('[data-day-today]')?.addEventListener('click',()=>openDay({date:today,source:'today'})); q('[data-today-day-open]')?.addEventListener('click',()=>openDay({date:today,source:'today_header'})); diff --git a/scripts/health/assets/health-assets/dashboard-v5-record.js b/scripts/health/assets/health-assets/dashboard-v5-record.js index 4ab3635..b33263e 100644 --- a/scripts/health/assets/health-assets/dashboard-v5-record.js +++ b/scripts/health/assets/health-assets/dashboard-v5-record.js @@ -738,8 +738,10 @@ } const medicationDose = item => { - const actual = [item.actual_dose_value, item.actual_dose_unit].filter(Boolean).join(' '); - const planned = [item.planned_dose_value, item.planned_dose_unit].filter(Boolean).join(' '); + const actualStructured = [item.actual_quantity_value,item.actual_dosage_form].filter(Boolean).join(' ')+(item.actual_strength?` · ${item.actual_strength}`:''); + const plannedStructured = [item.planned_quantity_value,item.planned_dosage_form].filter(Boolean).join(' ')+(item.planned_strength?` · ${item.planned_strength}`:''); + const actual = actualStructured || [item.actual_dose_value, item.actual_dose_unit].filter(Boolean).join(' '); + const planned = plannedStructured || [item.planned_dose_value, item.planned_dose_unit].filter(Boolean).join(' '); if(item.status==='administered') return actual || (item.legacy_dose ? `Legacy-Freitext (nicht als tatsächliche Dosis strukturiert): ${item.legacy_dose}` : 'Tatsächliche Dosis nicht dokumentiert'); if(item.status==='planned') return planned || (item.legacy_dose ? `Legacy-Freitext (nicht strukturiert): ${item.legacy_dose}` : 'Geplante Dosis nicht dokumentiert'); if(item.status==='corrected') { @@ -811,19 +813,76 @@ dialog.addEventListener('close',()=>dialog.remove(),{once:true});dialog.showModal();when.focus(); } + function administrationCaptureDialog(prescription, eventItem = null, captureMode = 'historical') { + const trigger=document.activeElement; + const plannedMode=captureMode==='planned'; + if(plannedMode&&!eventItem)return; + const baseline=plannedMode?eventItem?.administration_preset:prescription.administration_preset; + const dialog=document.createElement('dialog');dialog.className='medication-action-dialog';dialog.setAttribute('aria-labelledby','medication-administration-title'); + const form=document.createElement('form');form.method='dialog'; + const title=element('h2',plannedMode?'Geplanten Termin dokumentieren':'Historische Einnahme/Gabe erfassen');title.id='medication-administration-title'; + form.append(title,element('p','Manuelle Dokumentation ohne Änderung des Verordnungsstatus.','v5-meta')); + if(prescription.status!=='active')form.append(element('p',`Verordnungsstatus bleibt: ${prescription.status==='unknown'?'unbekannt':prescription.status}.`,'v5-meta')); + const when=document.createElement('input');when.type='datetime-local';when.required=true; + const quantity=document.createElement('input');quantity.maxLength=40;quantity.required=true;quantity.inputMode='decimal';quantity.value=baseline?.quantity_value||''; + const dosageForm=document.createElement('input');dosageForm.maxLength=40;dosageForm.required=true;dosageForm.value=baseline?.dosage_form||''; + const strength=document.createElement('input');strength.maxLength=80;strength.required=true;strength.value=baseline?.strength||''; + const route=document.createElement('select');[['unknown','Nicht belegt'],['oral','Oral'],['subcutaneous','Subkutan'],['intravenous','Intravenös'],['intramuscular','Intramuskulär'],['topical','Äußerlich'],['inhaled','Inhalativ'],['other','Andere dokumentierte Route']].forEach(([value,label])=>{const option=element('option',label);option.value=value;route.append(option);});route.value=baseline?.route_normalized||'unknown'; + const routeOriginal=document.createElement('input');routeOriginal.maxLength=60;routeOriginal.value=baseline?.route_original||''; + const region=document.createElement('input');region.maxLength=80;const side=document.createElement('select');[['','Nicht angegeben'],['left','Links'],['right','Rechts'],['unspecified','Seite nicht angegeben']].forEach(([value,label])=>{const option=element('option',label);option.value=value;side.append(option);});const injectionDetail=document.createElement('input');injectionDetail.maxLength=120; + const note=document.createElement('textarea');note.maxLength=300;const deviation=document.createElement('input');deviation.type='checkbox';const duplicate=document.createElement('input');duplicate.type='checkbox'; + form.append(formRow('Medikament',element('strong',prescription.name)),formRow('Zeitpunkt (Europe/Zurich)',when),formRow('Menge',quantity),formRow('Darreichungsform',dosageForm),formRow('Wirkstoffstärke',strength),formRow('Applikationsweg',route),formRow('Applikationsweg – dokumentierte Originalangabe',routeOriginal),formRow('Injektionsregion (optional)',region),formRow('Seite (optional)',side),formRow('Injektionsstelle (optional)',injectionDetail),formRow('Notiz (optional)',note)); + const deviationRow=formRow('Abweichung von der verifizierten Vorauswahl bewusst bestätigen',deviation);deviationRow.hidden=true;form.append(deviationRow,formRow('Möglichen identischen Doppeleintrag nach Prüfung zulassen',duplicate)); + const fieldError=element('p','', 'capture-inline-status');fieldError.setAttribute('role','alert');form.append(fieldError); + const preview=element('section',undefined,'medication-action-preview');preview.hidden=true;preview.setAttribute('aria-live','polite');form.append(preview); + const controls=element('div',undefined,'record-actions');const cancel=button('Abbrechen',()=>dialog.close());const submit=button('Vorschau prüfen',()=>form.requestSubmit());controls.append(cancel,submit);form.append(controls);dialog.append(form);document.body.append(dialog); + const injectionRoutes=new Set(['subcutaneous','intravenous','intramuscular','other']); + const syncInjection=()=>{const enabled=injectionRoutes.has(route.value);[region,side,injectionDetail].forEach(input=>{input.disabled=!enabled;if(!enabled)input.value='';});}; + const differs=()=>Boolean(baseline)&&[quantity.value.trim()!==baseline.quantity_value,dosageForm.value.trim()!==baseline.dosage_form,strength.value.trim()!==baseline.strength,routeOriginal.value.trim()!==(baseline.route_original||''),route.value!==(baseline.route_normalized||'unknown')].some(Boolean); + const syncDeviation=()=>{deviationRow.hidden=!differs();if(deviationRow.hidden)deviation.checked=false;}; + [quantity,dosageForm,strength,routeOriginal,route].forEach(input=>input.addEventListener('input',syncDeviation));route.addEventListener('change',()=>{routeOriginal.value=baseline&&route.value===baseline.route_normalized?(baseline.route_original||''):'';syncInjection();syncDeviation();});syncInjection();syncDeviation(); + let frozen=null;let frozenCsrf=''; + form.addEventListener('input',()=>{fieldError.textContent='';if(frozen){frozen=null;frozenCsrf='';preview.hidden=true;submit.textContent='Vorschau prüfen';}}); + form.addEventListener('submit',async event=>{event.preventDefault();fieldError.textContent='';if(!frozen){ + if(differs()&&!deviation.checked){fieldError.textContent='Die Angaben weichen von der verifizierten Vorauswahl ab. Bitte Abweichung bestätigen.';deviation.focus();return;} + const data={contract:'health.medication_action.v2',mode:captureMode,status:'administered',medication_ref:prescription.id,planned_event_ref:plannedMode?eventItem.id:'',name:prescription.name,quantity_value:quantity.value.trim(),dosage_form:dosageForm.value.trim(),strength:strength.value.trim(),route_original:routeOriginal.value.trim(),route_normalized:route.value,injection_region:region.value.trim(),injection_side:side.value,injection_detail:injectionDetail.value.trim(),note:note.value.trim(),preset_revision:baseline?.preset_revision||'',preview_revision:'',deviation_confirmed:deviation.checked,duplicate_confirmed:duplicate.checked}; + const draft={version:1,action:'capture_entry',capture_type:'medication',request_version:1,idempotency_key:[...crypto.getRandomValues(new Uint8Array(16))].map(value=>value.toString(16).padStart(2,'0')).join(''),occurred_at:when.value,ended_at:null,data,attachments:[],corrects_entry_id:null,withdraws_entry_id:null}; + submit.disabled=true; + try{ + const csrfResponse=await fetch('/api/v1/capture/csrf',{credentials:'same-origin',headers:{'Accept':'application/json'}});if(!csrfResponse.ok)throw new Error('csrf');const csrfResult=await csrfResponse.json();const captureCsrf=String(csrfResult.csrf_token||'');if(!/^[A-Za-z0-9_-]{24,128}$/.test(captureCsrf))throw new Error('csrf'); + const body=new URLSearchParams({csrf_token:captureCsrf,return_to:'v5',payload:JSON.stringify(draft)});const response=await fetch('/health-actions/medication-preview',{method:'POST',credentials:'same-origin',headers:{'Accept':'application/json','Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},body:body.toString()});const result=await response.json().catch(()=>({}));if(!response.ok){fieldError.textContent=result?.error?.message||'Die Angaben sind ungültig. Bitte markierte Felder prüfen.';const field=result?.error?.field;const fieldInputs={medication:quantity,quantity_value:quantity,dosage_form:dosageForm,strength,route_original:routeOriginal,route_normalized:route,injection_region:region,injection_side:side,injection_detail:injectionDetail,note,capture_mode:when,deviation_confirmed:deviation,duplicate_confirmed:duplicate,preset:quantity,preview:submit};fieldInputs[field]?.focus();return;}if(!/^[a-f0-9]{64}$/.test(String(result.preview_revision||'')))throw new Error('preview');draft.data.preview_revision=result.preview_revision;frozen=draft;frozenCsrf=captureCsrf; + }catch(_error){fieldError.textContent='Die Erfassung ist technisch nicht verfügbar. Es wurde nichts vorgemerkt.';return;}finally{submit.disabled=false;} + preview.replaceChildren(element('h3','Vollständige Vorschau'),element('p',`${when.value} · ${prescription.name}`),element('p',`${quantity.value} ${dosageForm.value} · ${strength.value}`),element('p',`Applikationsweg: ${route.options[route.selectedIndex].text}${region.value||side.value||injectionDetail.value?` · ${[region.value,side.options[side.selectedIndex]?.text,injectionDetail.value].filter(Boolean).join(' · ')}`:''}`),element('p',note.value?`Notiz: ${note.value}`:'Keine Notiz.','v5-meta'),element('p',plannedMode?'Mit vorhandenem Plantermin verknüpft.':'Historische manuelle Gabe ohne Plantermin.','v5-meta'));preview.hidden=false;submit.textContent='Verbindlich vormerken';return; + } + submit.disabled=true; + try{ + if(!frozenCsrf){const csrfResponse=await fetch('/api/v1/capture/csrf',{credentials:'same-origin',headers:{'Accept':'application/json'}});if(!csrfResponse.ok)throw new Error('csrf');const csrfResult=await csrfResponse.json();frozenCsrf=String(csrfResult.csrf_token||'');if(!/^[A-Za-z0-9_-]{24,128}$/.test(frozenCsrf))throw new Error('csrf');} + const body=new URLSearchParams({csrf_token:frozenCsrf,return_to:'v5',payload:JSON.stringify(frozen)}); + const response=await fetch('/health-actions/capture',{method:'POST',credentials:'same-origin',headers:{'Accept':'application/json','Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},body:body.toString()}); + const result=await response.json().catch(()=>({})); + if(!response.ok||result.status!=='queued')throw new Error('capture_unavailable'); + fieldError.textContent='Vorgemerkt. Die lokale Verarbeitung steht noch aus.';submit.textContent='Vorgemerkt'; + const statusKey=String(result.status_key||'');const statusToken=String(result.status_token||''); + if(!/^[a-f0-9]{32}$/.test(statusKey)||!/^[a-f0-9]{64}$/.test(statusToken))throw new Error('status_binding'); + let attempts=0;const poll=async()=>{attempts+=1;try{const check=await fetch(`/api/v1/capture/status/${statusKey}/${statusToken}`,{credentials:'same-origin',headers:{'Accept':'application/json'}});const state=await check.json();if(state.status==='processed'){fieldError.textContent='Gespeichert.';setTimeout(()=>{dialog.close();render('medications',{push:false});},300);return;}if(state.status==='rejected'){fieldError.textContent='Die Verarbeitung wurde wegen eines geänderten Datenstands oder eines möglichen Duplikats abgelehnt. Eingaben bleiben erhalten; bitte Vorschau erneut prüfen.';frozen=null;frozenCsrf='';preview.hidden=true;submit.disabled=false;submit.textContent='Vorschau erneut prüfen';return;}}catch(_error){}if(attempts<8)setTimeout(poll,750);};setTimeout(poll,750); + }catch(_error){fieldError.textContent='Die Erfassung ist technisch nicht verfügbar. Eingaben und identische Aktion bleiben erhalten; bitte erneut senden.';frozenCsrf='';submit.disabled=false;submit.textContent='Erneut senden';} + }); + dialog.addEventListener('close',()=>{dialog.remove();if(trigger instanceof HTMLElement)trigger.focus();},{once:true});dialog.showModal();when.focus(); + } + async function renderMedications(filters = {}, current) { const node = card('Medikamente'); const safeFilters=sanitizeFilters('medications',filters); const data = await request(`/api/v1/medications?${query(safeFilters)}`, current); const form=element('form',undefined,'record-filter-form');const from=document.createElement('input');from.type='date';from.value=safeFilters.from||'';const to=document.createElement('input');to.type='date';to.value=safeFilters.to||'';const medication=document.createElement('select');[['','Alle Medikamente'],...(data.prescriptions||[]).map(item=>[item.id,item.name])].forEach(([value,label])=>{const option=element('option',label);option.value=value;option.selected=safeFilters.medication===value;medication.append(option);});const eventStatus=document.createElement('select');[['','Alle Status'],['planned','Geplant'],['administered','Verabreicht / eingenommen'],['missed','Ausgelassen'],['corrected','Korrigiert'],['unknown','Unbekannt']].forEach(([value,label])=>{const option=element('option',label);option.value=value;option.selected=safeFilters.status===value;eventStatus.append(option);});const source=document.createElement('select');[['','Alle Quellen'],...(data.sources||[]).map(value=>[value,sourceLabel(value)])].forEach(([value,label])=>{const option=element('option',label);option.value=value;option.selected=safeFilters.source===value;source.append(option);});form.append(formRow('Von',from),formRow('Bis',to),formRow('Medikament',medication),formRow('Status',eventStatus),formRow('Quelle',source),button('Filter anwenden',()=>form.requestSubmit()));form.addEventListener('submit',event=>{event.preventDefault();render('medications',{push:true,filters:{from:from.value,to:to.value,medication:medication.value,status:eventStatus.value,source:source.value}});});node.append(form); - const currentSection=element('section',undefined,'medication-current');currentSection.append(element('h4','Aktive Verordnungen'));(data.current_prescriptions||[]).forEach(item=>{const article=element('article',undefined,'medication-prescription-row');article.append(element('strong',item.name),element('span','Aktiv','status-chip'),element('p',item.documented_dose||'Dosis nicht strukturiert dokumentiert','v5-meta'));article.append(button('Jetzt als eingenommen / verabreicht dokumentieren',()=>medicationActionDialog(item)));currentSection.append(article);});if(!(data.current_prescriptions||[]).length)appendEmpty(currentSection);node.append(currentSection); - const otherSection=element('section',undefined,'medication-other-prescriptions');otherSection.append(element('h4','Pausierte, beendete oder unklare Verordnungen'));(data.other_prescriptions||[]).forEach(item=>{const article=element('article',undefined,'medication-prescription-row');const label=item.status==='paused'?'Pausiert':item.status==='ended'?'Beendet':'Status unbekannt';article.append(element('strong',item.name),element('span',label,'status-chip'),element('p','Keine Darstellung als aktuell aktive Verordnung.','v5-meta'));otherSection.append(article);});if(!(data.other_prescriptions||[]).length)appendEmpty(otherSection);node.append(otherSection,element('h4','Verlauf')); + const currentSection=element('section',undefined,'medication-current');currentSection.append(element('h4','Aktive Verordnungen'));(data.current_prescriptions||[]).forEach(item=>{const article=element('article',undefined,'medication-prescription-row');article.append(element('strong',item.name),element('span','Aktiv','status-chip'),element('p',item.documented_dose||'Dosis nicht strukturiert dokumentiert','v5-meta'));article.append(button('Historische Einnahme/Gabe erfassen',()=>administrationCaptureDialog(item,null,'historical')));currentSection.append(article);});if(!(data.current_prescriptions||[]).length)appendEmpty(currentSection);node.append(currentSection); + const otherSection=element('section',undefined,'medication-other-prescriptions');otherSection.append(element('h4','Pausierte, beendete oder unklare Verordnungen'));(data.other_prescriptions||[]).forEach(item=>{const article=element('article',undefined,'medication-prescription-row');const label=item.status==='paused'?'Pausiert':item.status==='ended'?'Beendet':'Status unbekannt';article.append(element('strong',item.name),element('span',label,'status-chip'),element('p','Keine Darstellung als aktuell aktive Verordnung. Historische Dokumentation ändert diesen Status nicht.','v5-meta'),button('Historische Einnahme/Gabe erfassen',()=>administrationCaptureDialog(item,null,'historical')));otherSection.append(article);});if(!(data.other_prescriptions||[]).length)appendEmpty(otherSection);node.append(otherSection,element('h4','Verlauf')); const prescriptionsById=new Map((data.prescriptions||[]).map(item=>[item.id,item])); ['planned', 'administered', 'missed', 'corrected', 'unknown'].forEach(kind => { const section = element('section'); section.append(element('h5', ({ planned: 'Geplant', administered: 'Tatsächlich verabreicht / eingenommen', missed: 'Ausgelassen', corrected: 'Korrigiert',unknown:'Status unbekannt' })[kind])); const rows = data[kind] || []; if(!rows.length){section.append(element('p','Keine dokumentierten Angaben.'));node.append(section);return;} - const list=element('div',undefined,'medication-history-list');rows.forEach(item=>{const article=element('article',undefined,'medication-history-row');article.append(element('div',`${item.date} · ${item.name} · ${medicationDose(item)} · ${sourceLabel(item.source)}`));if(item.superseded_by_correction)article.append(element('span','Durch Korrektur ersetzt','status-chip'));const details=document.createElement('details');details.append(element('summary','Details'));const facts=element('dl');[['Status',item.status],['Dokumentierter Folgetermin',item.scheduled_next_date||'Nicht angegeben'],['Geplante Dosis',[item.planned_dose_value,item.planned_dose_unit].filter(Boolean).join(' ')||'Nicht strukturiert dokumentiert'],['Tatsächliche Dosis',[item.actual_dose_value,item.actual_dose_unit].filter(Boolean).join(' ')||'Nicht strukturiert dokumentiert'],['Legacy-Dosisfreitext (uninterpretiert)',item.legacy_dose||'Nicht vorhanden'],['Applikationsweg (Original)',item.route_original||'Nicht angegeben'],['Applikationsweg (Anzeige)',item.route_normalized||'Unbekannt'],['Injektionsstelle',[item.injection_region,item.injection_side,item.injection_detail].filter(Boolean).join(' · ')||'Nicht angegeben'],['Charge',item.lot_number||'Nicht angegeben'],['Notiz',item.note||'Nicht angegeben'],['Korrekturbegründung',item.correction_reason||'Nicht angegeben'],['Quelle',sourceLabel(item.source)]].forEach(([label,value])=>facts.append(element('dt',label),element('dd',value)));details.append(facts);article.append(details);const prescription=prescriptionsById.get(item.prescription_id);if(prescription&&kind==='planned'&&!item.plan_consumed){article.append(button('Als eingenommen / verabreicht dokumentieren',()=>medicationActionDialog(prescription,item,'administered')),button('Auslassung dokumentieren',()=>medicationActionDialog(prescription,item,'missed')));}if(prescription&&!item.superseded_by_correction&&item.correction_preview_revision)article.append(button('Eintrag korrigieren',()=>medicationActionDialog(prescription,item,'corrected')));list.append(article);});section.append(list);node.append(section); + const list=element('div',undefined,'medication-history-list');rows.forEach(item=>{const article=element('article',undefined,'medication-history-row');article.append(element('div',`${item.date} · ${item.name} · ${medicationDose(item)} · ${sourceLabel(item.source)}`));if(item.superseded_by_correction)article.append(element('span','Durch Korrektur ersetzt','status-chip'));const details=document.createElement('details');details.append(element('summary','Details'));const facts=element('dl');[['Status',item.status],['Dokumentierter Folgetermin',item.scheduled_next_date||'Nicht angegeben'],['Geplante Menge',item.planned_quantity_value||'Nicht strukturiert dokumentiert'],['Geplante Darreichungsform',item.planned_dosage_form||'Nicht strukturiert dokumentiert'],['Geplante Wirkstoffstärke',item.planned_strength||'Nicht strukturiert dokumentiert'],['Tatsächliche Menge',item.actual_quantity_value||'Nicht strukturiert dokumentiert'],['Tatsächliche Darreichungsform',item.actual_dosage_form||'Nicht strukturiert dokumentiert'],['Tatsächliche Wirkstoffstärke',item.actual_strength||'Nicht strukturiert dokumentiert'],['Legacy-Dosis Wert/Einheit (uninterpretiert)',[item.actual_dose_value,item.actual_dose_unit].filter(Boolean).join(' ')||[item.planned_dose_value,item.planned_dose_unit].filter(Boolean).join(' ')||'Nicht vorhanden'],['Legacy-Dosisfreitext (uninterpretiert)',item.legacy_dose||'Nicht vorhanden'],['Applikationsweg (Original)',item.route_original||'Nicht angegeben'],['Applikationsweg (Anzeige)',item.route_normalized||'Unbekannt'],['Injektionsstelle',[item.injection_region,item.injection_side,item.injection_detail].filter(Boolean).join(' · ')||'Nicht angegeben'],['Charge',item.lot_number||'Nicht angegeben'],['Notiz',item.note||'Nicht angegeben'],['Korrekturbegründung',item.correction_reason||'Nicht angegeben'],['Quelle',sourceLabel(item.source)]].forEach(([label,value])=>facts.append(element('dt',label),element('dd',value)));details.append(facts);article.append(details);const prescription=prescriptionsById.get(item.prescription_id);if(prescription&&kind==='planned'&&!item.plan_consumed){if(item.administration_preset)article.append(button('Geplanten Termin dokumentieren',()=>administrationCaptureDialog(prescription,item,'planned')));article.append(button('Auslassung dokumentieren',()=>medicationActionDialog(prescription,item,'missed')));}if(prescription&&!item.superseded_by_correction&&item.correction_preview_revision)article.append(button('Eintrag korrigieren',()=>medicationActionDialog(prescription,item,'corrected')));list.append(article);});section.append(list);node.append(section); }); node.append(element('p', `Datenvollständigkeit: ${data.truncated ? `gekürzt (${data.truncated_sections.join(', ')})` : 'vollständige Seite'} · Fehlende Legacy-Felder bleiben unbekannt.`, 'v5-meta')); return node; diff --git a/scripts/health/assets/health-assets/dashboard-v5.js b/scripts/health/assets/health-assets/dashboard-v5.js index 0adfe94..ae52aa2 100644 --- a/scripts/health/assets/health-assets/dashboard-v5.js +++ b/scripts/health/assets/health-assets/dashboard-v5.js @@ -518,17 +518,27 @@ if (status) status.textContent = message; } - function openSymptomCheckin() { + function openSymptomCheckin(date = bundle.today) { const dialog = q("#checkin-dialog"); if (!dialog) { setTaskStatus("Das Symptomformular ist in dieser Ansicht nicht verfügbar."); return false; } + const safeDate = /^\d{4}-\d{2}-\d{2}$/.test(String(date || "")) ? String(date) : bundle.today; + if (dialog.parentElement !== document.body) document.body.append(dialog); + const dateInput = dialog.querySelector("input[name=date]"); + const dateLabel = dialog.querySelector("#capture-date"); + if (dateInput) dateInput.value = safeDate; + if (dateLabel) { + dateLabel.dateTime = safeDate; + dateLabel.textContent = new Intl.DateTimeFormat("de-CH", { dateStyle: "long", timeZone: "Europe/Zurich" }).format(new Date(`${safeDate}T12:00:00Z`)); + } if (!dialog.open) dialog.showModal(); q("select:not(:disabled), input:not([type=hidden]):not(:disabled), textarea:not(:disabled), button:not(:disabled)", dialog)?.focus(); - setTaskStatus("Symptomerfassung geöffnet."); + setTaskStatus(`Symptomerfassung für ${safeDate} geöffnet.`); return true; } + window.healthOpenSymptomCheckin = openSymptomCheckin; async function dispatchTaskAction(action, queueCode = '') { if (action === "checkin") { diff --git a/scripts/health/dashboard_v5/medication_contract.py b/scripts/health/dashboard_v5/medication_contract.py index 1d45091..9f8c6c6 100644 --- a/scripts/health/dashboard_v5/medication_contract.py +++ b/scripts/health/dashboard_v5/medication_contract.py @@ -9,7 +9,9 @@ import sqlite3 from typing import Any, Mapping PUBLIC_CONTRACT_VERSION = "health.medication_history.v1" -ACTION_CONTRACT_VERSION = "health.medication_action.v1" +ACTION_CONTRACT_VERSION_V1 = "health.medication_action.v1" +ACTION_CONTRACT_VERSION = "health.medication_action.v2" +ACTION_CONTRACT_VERSIONS = frozenset({ACTION_CONTRACT_VERSION_V1, ACTION_CONTRACT_VERSION}) ROUTES = frozenset( { "oral", @@ -57,6 +59,12 @@ EVENT_FIELDS = ( "corrected_target_status", "correction_reason", "business_revision", + "planned_quantity_value", + "planned_dosage_form", + "planned_strength", + "actual_quantity_value", + "actual_dosage_form", + "actual_strength", ) PRESCRIPTION_FIELDS = ( "id", @@ -68,9 +76,24 @@ PRESCRIPTION_FIELDS = ( "prescription_status_source", "prescription_status_provenance", "business_revision", + "administration_preset_quantity_value", + "administration_preset_dosage_form", + "administration_preset_strength", + "administration_preset_route_original", + "administration_preset_route", + "administration_preset_source", + "administration_preset_provenance", + "administration_preset_revision", ) +class MedicationFieldError(ValueError): + def __init__(self, field: str, code: str = "invalid") -> None: + super().__init__(f"invalid medication field: {field}") + self.field = field + self.code = code + + def _canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=True, sort_keys=True, separators=(",", ":")) @@ -116,6 +139,81 @@ def event_ref(row: sqlite3.Row | Mapping[str, Any], key: bytes) -> str: )[:24] +def prescription_preset( + row: sqlite3.Row | Mapping[str, Any], key: bytes +) -> dict[str, str] | None: + fields = ( + "administration_preset_quantity_value", + "administration_preset_dosage_form", + "administration_preset_strength", + "administration_preset_source", + "administration_preset_provenance", + "administration_preset_revision", + ) + if any(not _value(row, field) for field in fields): + return None + route = str(_value(row, "administration_preset_route") or "") + route_original = str(_value(row, "administration_preset_route_original") or "") + if route and (route not in ROUTES or not route_original): + return None + revision = str(_value(row, "administration_preset_revision")) + if not REVISION_RE.fullmatch(revision): + return None + revision_values = ( + str(_value(row, "administration_preset_quantity_value")), + str(_value(row, "administration_preset_dosage_form")), + str(_value(row, "administration_preset_strength")), + route_original, + route, + str(_value(row, "administration_preset_source")), + str(_value(row, "administration_preset_provenance")), + ) + expected_revision = hashlib.sha256( + (str(_value(row, "medikament_name")) + "\0" + "\0".join(revision_values)).encode("utf-8") + ).hexdigest() + if not hmac.compare_digest(revision, expected_revision): + return None + return { + "quantity_value": str(_value(row, "administration_preset_quantity_value")), + "dosage_form": str(_value(row, "administration_preset_dosage_form")), + "strength": str(_value(row, "administration_preset_strength")), + "route_original": route_original, + "route_normalized": route, + "preset_revision": _opaque_digest( + key, + "health-medication-administration-preset-v1", + [revision, *revision_values, _value(row, "id"), _value(row, "medikament_name")], + ), + } + + +def planned_event_preset( + row: sqlite3.Row | Mapping[str, Any], key: bytes +) -> dict[str, str] | None: + values = tuple( + str(_value(row, field) or "") + for field in ("planned_quantity_value", "planned_dosage_form", "planned_strength") + ) + if not all(values): + return None + route = str(_value(row, "route_normalized") or "") + route_original = str(_value(row, "route_original") or "") + if route and route not in ROUTES: + return None + return { + "quantity_value": values[0], + "dosage_form": values[1], + "strength": values[2], + "route_original": route_original, + "route_normalized": route, + "preset_revision": _opaque_digest( + key, + "health-medication-planned-administration-preset-v1", + [event_revision(row), event_ref(row, key)], + ), + } + + def _available_fields(connection: sqlite3.Connection, table: str, fields: tuple[str, ...]) -> tuple[str, ...]: actual = {str(row[1]) for row in connection.execute(f'PRAGMA table_info("{table}")')} return tuple(field for field in fields if field in actual) @@ -293,15 +391,67 @@ def resolve_action_preview( and prescription["prescription_status_provenance"] and prescription["business_revision"] ) - if data["status"] != "corrected" and not trusted_active: + is_v2 = data.get("contract") == ACTION_CONTRACT_VERSION + if is_v2: + if data["mode"] == "planned" and not trusted_active: + raise RuntimeError("planned administration requires an explicitly active prescription") + elif data["status"] != "corrected" and not trusted_active: raise RuntimeError("medication prescription is not explicitly active") planned = resolve_event(connection, data["planned_event_ref"]) if data["planned_event_ref"] else None - target = resolve_event(connection, data["correction_target_ref"]) if data["correction_target_ref"] else None + target_ref = data.get("correction_target_ref", "") + target = resolve_event(connection, target_ref) if target_ref else None + if is_v2: + if data["mode"] == "historical" and planned is not None: + raise RuntimeError("historical administration cannot consume a plan") + if data["mode"] == "planned" and planned is None: + raise RuntimeError("planned administration requires a real plan") if planned is not None and str(planned["event_type"] or "").strip().casefold() != "planned": raise RuntimeError("planned event reference changed") consumed = plan_is_consumed(connection, int(planned["id"])) if planned is not None else False if consumed: raise RuntimeError("planned medication event is already consumed") + if is_v2: + key = public_identity_key(connection) + preset = prescription_preset(prescription, key) + baseline: tuple[str, str, str, str, str] | None = None + expected_revision = "" + if data["mode"] == "planned" and planned is not None: + baseline_values = tuple( + str(_value(planned, field) or "") + for field in ( + "planned_quantity_value", "planned_dosage_form", + "planned_strength", "route_original", "route_normalized", + ) + ) + if not all(baseline_values[:3]): + raise RuntimeError("planned medication event lacks a structured administration preset") + baseline = ( + baseline_values[0], baseline_values[1], + baseline_values[2], baseline_values[3], baseline_values[4], + ) + expected_revision = _opaque_digest( + key, + "health-medication-planned-administration-preset-v1", + [event_revision(planned), event_ref(planned, key)], + ) + elif preset is not None: + baseline = ( + preset["quantity_value"], preset["dosage_form"], + preset["strength"], preset["route_original"], preset["route_normalized"], + ) + expected_revision = preset["preset_revision"] + if baseline is None: + if data["preset_revision"]: + raise RuntimeError("administration preset is no longer available") + else: + if not hmac.compare_digest(expected_revision, data["preset_revision"]): + raise RuntimeError("administration preset changed") + actual = ( + data["quantity_value"], data["dosage_form"], + data["strength"], data["route_original"], data["route_normalized"], + ) + if actual != baseline and not data["deviation_confirmed"]: + raise RuntimeError("administration differs from verified preset") if target is not None and connection.execute( "SELECT 1 FROM medication_administrations WHERE corrects_event_id=? LIMIT 1", (int(target["id"]),), @@ -334,13 +484,22 @@ def _text(value: Any, maximum: int, *, required: bool = False) -> str: if ( (required and not cleaned) or len(cleaned) > maximum - or re.search(r"[\x00-\x1f\x7f]|https?://|/|\\|\.hermes", cleaned, re.I) + or re.search(r"[\x00-\x1f\x7f]|https?://|\\|\.hermes", cleaned, re.I) + or cleaned.startswith("/") + or re.search(r"(?:^|/)\.\.(?:/|$)", cleaned) ): raise ValueError("invalid medication text") return cleaned -def validate_action_data(data: Any) -> dict[str, Any]: +def _field_text(field: str, value: Any, maximum: int, *, required: bool = False) -> str: + try: + return _text(value, maximum, required=required) + except ValueError as error: + raise MedicationFieldError(field) from error + + +def _validate_action_data_v1(data: Any) -> dict[str, Any]: required = { "contract", "status", @@ -366,7 +525,7 @@ def validate_action_data(data: Any) -> dict[str, Any]: "deviation_confirmed", "duplicate_confirmed", } - if not isinstance(data, dict) or set(data) != required or data.get("contract") != ACTION_CONTRACT_VERSION: + if not isinstance(data, dict) or set(data) != required or data.get("contract") != ACTION_CONTRACT_VERSION_V1: raise ValueError("invalid medication action shape") status = data["status"] if status not in STATUSES: @@ -425,7 +584,7 @@ def validate_action_data(data: Any) -> dict[str, Any]: elif actual_value or actual_unit or planned_value or planned_unit: raise ValueError("missed or unknown correction cannot assert dose values") return { - "contract": ACTION_CONTRACT_VERSION, + "contract": ACTION_CONTRACT_VERSION_V1, "status": status, "medication_ref": medication, "planned_event_ref": planned, @@ -449,3 +608,76 @@ def validate_action_data(data: Any) -> dict[str, Any]: "deviation_confirmed": data["deviation_confirmed"], "duplicate_confirmed": data["duplicate_confirmed"], } + + +def _validate_action_data_v2(data: Any) -> dict[str, Any]: + required = { + "contract", "mode", "status", "medication_ref", "planned_event_ref", + "name", "quantity_value", "dosage_form", "strength", "route_original", + "route_normalized", "injection_region", "injection_side", "injection_detail", + "note", "preset_revision", "preview_revision", "deviation_confirmed", + "duplicate_confirmed", + } + if not isinstance(data, dict) or set(data) != required or data.get("contract") != ACTION_CONTRACT_VERSION: + raise ValueError("invalid medication action shape") + if data["mode"] not in {"historical", "planned"}: + raise MedicationFieldError("capture_mode") + if data["status"] != "administered": + raise MedicationFieldError("capture_mode") + medication = data["medication_ref"] + planned = data["planned_event_ref"] + if not isinstance(medication, str) or not OPAQUE_RE.fullmatch(medication) or not medication.startswith("medrx_"): + raise MedicationFieldError("medication") + if planned and (not isinstance(planned, str) or not OPAQUE_RE.fullmatch(planned) or not planned.startswith("medevt_")): + raise MedicationFieldError("capture_mode") + if data["mode"] == "historical" and planned: + raise ValueError("historical administration cannot reference a plan") + if data["mode"] == "planned" and not planned: + raise ValueError("planned administration requires a real plan") + for field in ("preview_revision", "preset_revision"): + value = data[field] + if value and (not isinstance(value, str) or not REVISION_RE.fullmatch(value)): + raise MedicationFieldError("preview" if field == "preview_revision" else "preset") + route = data["route_normalized"] + side = data["injection_side"] + if route not in ROUTES: + raise MedicationFieldError("route_normalized") + if side not in (SIDES | {""}): + raise MedicationFieldError("injection_side") + quantity = _field_text("quantity_value", data["quantity_value"], 40, required=True) + dosage_form = _field_text("dosage_form", data["dosage_form"], 40, required=True) + strength = _field_text("strength", data["strength"], 80, required=True) + injection_region = _field_text("injection_region", data["injection_region"], 80) + injection_detail = _field_text("injection_detail", data["injection_detail"], 120) + if (injection_region or side or injection_detail) and route not in INJECTION_ROUTES: + raise MedicationFieldError("route_normalized") + for flag in ("deviation_confirmed", "duplicate_confirmed"): + if type(data[flag]) is not bool: + raise MedicationFieldError(flag) + return { + "contract": ACTION_CONTRACT_VERSION, + "mode": data["mode"], + "status": "administered", + "medication_ref": medication, + "planned_event_ref": planned, + "name": _field_text("medication", data["name"], 120, required=True), + "quantity_value": quantity, + "dosage_form": dosage_form, + "strength": strength, + "route_original": _field_text("route_original", data["route_original"], 60), + "route_normalized": route, + "injection_region": injection_region, + "injection_side": side, + "injection_detail": injection_detail, + "note": _field_text("note", data["note"], 300), + "preset_revision": data["preset_revision"], + "preview_revision": data["preview_revision"], + "deviation_confirmed": data["deviation_confirmed"], + "duplicate_confirmed": data["duplicate_confirmed"], + } + + +def validate_action_data(data: Any) -> dict[str, Any]: + if isinstance(data, dict) and data.get("contract") == ACTION_CONTRACT_VERSION_V1: + return _validate_action_data_v1(data) + return _validate_action_data_v2(data) diff --git a/scripts/health/dashboard_v5/read_api.py b/scripts/health/dashboard_v5/read_api.py index 62ef74c..602130e 100644 --- a/scripts/health/dashboard_v5/read_api.py +++ b/scripts/health/dashboard_v5/read_api.py @@ -86,6 +86,8 @@ from dashboard_v5.medication_contract import ( list_events as medication_event_rows, list_prescriptions as medication_prescription_rows, medication_ref as medication_prescription_ref, + prescription_preset as medication_prescription_preset, + planned_event_preset as medication_planned_event_preset, public_action_context_token as medication_public_context_token, public_identity_key as medication_public_identity_key, plan_is_consumed as medication_plan_is_consumed, @@ -3574,6 +3576,8 @@ def _record_medications( "status_source": source, "status_provenance": provenance, "preview_revision": medication_public_context_token(identity_key, row), + "administration_preset": medication_prescription_preset(row, identity_key), + "historical_capture_allowed": True, } public_prescriptions.append(item) empty["prescriptions"] = public_prescriptions @@ -3663,6 +3667,13 @@ def _record_medications( "planned_dose_unit": safe_metadata_text(row["planned_dose_unit"], 30, allow_empty=True), "actual_dose_value": safe_metadata_text(row["actual_dose_value"], 40, allow_empty=True), "actual_dose_unit": safe_metadata_text(row["actual_dose_unit"], 30, allow_empty=True), + "planned_quantity_value": safe_metadata_text(row["planned_quantity_value"], 40, allow_empty=True), + "planned_dosage_form": safe_metadata_text(row["planned_dosage_form"], 40, allow_empty=True), + "planned_strength": safe_metadata_text(row["planned_strength"], 80, allow_empty=True), + "actual_quantity_value": safe_metadata_text(row["actual_quantity_value"], 40, allow_empty=True), + "actual_dosage_form": safe_metadata_text(row["actual_dosage_form"], 40, allow_empty=True), + "actual_strength": safe_metadata_text(row["actual_strength"], 80, allow_empty=True), + "administration_preset": medication_planned_event_preset(row, identity_key) if bucket == "planned" else None, "route_original": route_original, "route_normalized": route_normalized or "unknown", "injection_region": safe_metadata_text(row["injection_region"], 80, allow_empty=True), @@ -4218,7 +4229,9 @@ def _doctor_report( ) medication_report_fields = ( "date", "name", "status", "effective_status", "scheduled_next_date", "planned_dose_value", - "planned_dose_unit", "actual_dose_value", "actual_dose_unit", "legacy_dose", + "planned_dose_unit", "actual_dose_value", "actual_dose_unit", + "planned_quantity_value", "planned_dosage_form", "planned_strength", + "actual_quantity_value", "actual_dosage_form", "actual_strength", "legacy_dose", "superseded_by_correction", ) medications: dict[str, Any] = { diff --git a/scripts/health/health_dashboard_server.py b/scripts/health/health_dashboard_server.py index 58e878b..e849486 100644 --- a/scripts/health/health_dashboard_server.py +++ b/scripts/health/health_dashboard_server.py @@ -6,6 +6,7 @@ from __future__ import annotations import base64 import binascii import hashlib +import hmac import json import mimetypes import os @@ -35,7 +36,7 @@ from dashboard_v5.observation_contract import validate_action as validate_observ from dashboard_v5.capture_contract import validate_capture_payload from dashboard_v5.medication_schema import assert_schema as assert_medication_schema from dashboard_v5.medication_contract import ( - ACTION_CONTRACT_VERSION as MEDICATION_ACTION_CONTRACT, + ACTION_CONTRACT_VERSIONS as MEDICATION_ACTION_CONTRACTS, resolve_action_preview, ) from dashboard_v5.capture_media import MAX_ORIGINAL_BYTES as MAX_CAPTURE_MEDIA_BYTES, quarantine_bytes @@ -980,9 +981,24 @@ def validate_mapping_submission(form: dict[str, list[str]]) -> tuple[dict[str, o }, return_to -def capture_processing_status(idempotency_key: str) -> str: - if not re.fullmatch(r"[0-9a-f]{32}", idempotency_key): - raise ValueError("invalid status key") +def capture_payload_action_hash(payload: dict[str, Any]) -> str: + canonical = json.dumps(payload, ensure_ascii=True, sort_keys=True, separators=(",", ":")) + return hashlib.sha256(canonical.encode("utf-8")).hexdigest() + + +def capture_status_token(idempotency_key: str, action_hash: str) -> str: + if not re.fullmatch(r"[0-9a-f]{32}", idempotency_key) or not re.fullmatch(r"[0-9a-f]{64}", action_hash): + raise ValueError("invalid capture status binding") + return hmac.new( + load_api_token().encode("utf-8"), + f"health-capture-status-v1\0{idempotency_key}\0{action_hash}".encode("utf-8"), + hashlib.sha256, + ).hexdigest() + + +def capture_processing_status(idempotency_key: str, supplied_token: str) -> str: + if not re.fullmatch(r"[0-9a-f]{32}", idempotency_key) or not re.fullmatch(r"[0-9a-f]{64}", supplied_token): + raise ValueError("invalid status binding") path = ACTION_INBOX / "capture-receipts" / f"{idempotency_key}.json" try: descriptor = os.open(path, os.O_RDONLY | os.O_NOFOLLOW) @@ -1002,8 +1018,12 @@ def capture_processing_status(idempotency_key: str) -> str: os.close(descriptor) record = json.loads(data.decode()) status = record.get("status") - if status not in {"processed", "rejected"}: + action_hash = record.get("action_hash") + if record.get("version") != 2 or status not in {"processed", "rejected"} or not isinstance(action_hash, str): raise OSError("invalid capture receipt") + expected_token = capture_status_token(idempotency_key, action_hash) + if not hmac.compare_digest(expected_token, supplied_token): + raise OSError("capture receipt binding mismatch") return status def open_capture_media( @@ -1193,6 +1213,54 @@ def write_symptom_checkin(day: str, scores: dict[str, int], notes: str) -> str: ) +def _medication_preview_error(error: Exception) -> tuple[int, dict[str, Any]]: + field = getattr(error, "field", None) + allowed_fields = { + "quantity_value", "dosage_form", "strength", "route_original", + "injection_region", "injection_side", "injection_detail", "note", "medication", + "route_normalized", "capture_mode", "deviation_confirmed", + "duplicate_confirmed", "preset", "preview", + } + if isinstance(error, ValueError) and field in allowed_fields: + messages = { + "quantity_value": "Bitte eine gültige Menge eingeben.", + "dosage_form": "Bitte eine gültige Darreichungsform eingeben.", + "strength": "Bitte eine gültige Wirkstoffstärke eingeben.", + "route_original": "Bitte den dokumentierten Applikationsweg prüfen.", + "injection_region": "Bitte die Injektionsregion prüfen.", + "injection_side": "Bitte die Seite der Injektionsstelle prüfen.", + "injection_detail": "Bitte die Injektionsstelle prüfen.", + "route_normalized": "Bitte den Applikationsweg prüfen.", + "capture_mode": "Bitte den Erfassungsmodus oder Plantermin prüfen.", + "deviation_confirmed": "Bitte die Abweichung bewusst bestätigen.", + "duplicate_confirmed": "Bitte einen möglichen Doppeleintrag bewusst bestätigen.", + "preset": "Die Vorauswahl ist nicht mehr aktuell. Bitte neu laden.", + "preview": "Die Vorschau ist nicht mehr aktuell. Bitte erneut prüfen.", + "note": "Bitte die Notiz prüfen.", + "medication": "Bitte das ausgewählte Medikament prüfen.", + } + return 422, {"error": {"kind": "validation", "code": f"{field}_invalid", "field": field, "message": messages.get(field, "Bitte die markierte Angabe prüfen.")}} + reason = str(error) + rules = ( + ("invalid medication action shape", 422, "payload_invalid", "medication", "Die Medikationsangaben sind unvollständig oder ungültig."), + ("invalid medication text", 422, "medication_text_invalid", "medication", "Name, Stärke oder Notiz enthält eine unzulässige Angabe."), + ("requires a real plan", 422, "planned_event_required", "capture_mode", "Für diesen Modus ist kein belastbarer Plantermin verknüpft."), + ("lacks a structured administration preset", 422, "planned_event_unstructured", "capture_mode", "Der Plantermin enthält keine revisionssichere Mengenangabe."), + ("differs from verified preset", 422, "preset_deviation_unconfirmed", "deviation_confirmed", "Die Eingabe weicht von der verifizierten Vorauswahl ab und muss bewusst bestätigt werden."), + ("administration preset changed", 409, "preset_changed", "preset", "Die verifizierte Vorauswahl hat sich geändert. Bitte Vorschau neu laden."), + ("administration preset is no longer available", 409, "preset_unavailable", "preset", "Die verifizierte Vorauswahl ist nicht mehr verfügbar. Bitte neu laden."), + ("stale medication preview", 409, "preview_changed", "preview", "Die Vorschau ist nicht mehr aktuell. Bitte erneut prüfen."), + ("already consumed", 409, "planned_event_consumed", "capture_mode", "Der Plantermin wurde bereits dokumentiert."), + ("explicitly active prescription", 422, "prescription_not_active_for_plan", "capture_mode", "Ein Plantermin kann nur gegen eine ausdrücklich aktive Verordnung dokumentiert werden."), + ) + for fragment, status_code, code, field, message in rules: + if fragment in reason: + return status_code, {"error": {"kind": "validation", "code": code, "field": field, "message": message}} + if isinstance(error, ValueError): + return 422, {"error": {"kind": "validation", "code": "medication_validation_failed", "field": "medication", "message": "Die Medikationsangaben sind ungültig. Bitte die Felder prüfen."}} + return 503, {"error": {"kind": "technical", "code": "medication_preview_unavailable", "field": "", "message": "Die Erfassung ist technisch nicht verfügbar. Es wurde nichts vorgemerkt."}} + + class Handler(BaseHTTPRequestHandler): def send_error( self, @@ -1356,7 +1424,7 @@ class Handler(BaseHTTPRequestHandler): if ( payload["capture_type"] != "medication" or not isinstance(preview_data, dict) - or preview_data.get("contract") != MEDICATION_ACTION_CONTRACT + or preview_data.get("contract") not in MEDICATION_ACTION_CONTRACTS ): raise ValueError("invalid medication preview contract") preview_connection = connect_read_only(API_DB) @@ -1365,8 +1433,6 @@ class Handler(BaseHTTPRequestHandler): medication_preview_revision = resolve_action_preview( preview_connection, payload )[0] - except RuntimeError as error: - raise ValueError("medication preview context unavailable") from error finally: preview_connection.close() return_to = "v5" @@ -1395,8 +1461,12 @@ class Handler(BaseHTTPRequestHandler): else: self.send_error(409) return - except ValueError: - self.send_error(400) + except (ValueError, RuntimeError, sqlite3.Error) as error: + if action_path == MEDICATION_PREVIEW_ROUTE: + status_code, body = _medication_preview_error(error) + self._send_api_json(status_code, body, True) + else: + self.send_error(400) return except QueueFullError: self.send_error(503) @@ -1411,6 +1481,16 @@ class Handler(BaseHTTPRequestHandler): True, ) return + if action_path == CAPTURE_ROUTE and "application/json" in self.headers.get("Accept", ""): + action_hash = capture_payload_action_hash(payload) + status_key = str(payload["idempotency_key"]) + status_token = capture_status_token(status_key, action_hash) + self._send_api_json( + 202, + {"status": "queued", "status_key": status_key, "status_token": status_token}, + True, + ) + return if action_path in {CHECKIN_ROUTE, NUTRITION_MAPPING_ROUTE} and "application/json" in self.headers.get( "Accept", "" ): @@ -1804,13 +1884,13 @@ class Handler(BaseHTTPRequestHandler): if send_body: self.wfile.write(data) return - capture_status = re.fullmatch(r"/api/v1/capture/status/([a-f0-9]{32})", path) + capture_status = re.fullmatch(r"/api/v1/capture/status/([a-f0-9]{32})/([a-f0-9]{64})", path) if capture_status: if query: self._send_api_error(400, "unknown_parameter", send_body) return try: - status = capture_processing_status(capture_status.group(1)) + status = capture_processing_status(capture_status.group(1), capture_status.group(2)) except (OSError, ValueError, UnicodeError, json.JSONDecodeError): self._send_api_error(503, "status_unavailable", send_body) return diff --git a/tests/test_dashboard_v5_sprint7c_f.py b/tests/test_dashboard_v5_sprint7c_f.py index 9eea7d3..0c8cf59 100644 --- a/tests/test_dashboard_v5_sprint7c_f.py +++ b/tests/test_dashboard_v5_sprint7c_f.py @@ -79,8 +79,8 @@ def test_copy_first_migration_preserves_every_legacy_value_and_proves_restore(tm before_master = _table_rows(source, "medikamente") before_events = _table_rows(source, "medication_administrations") report = safe_prepare(source, backup, migrated, restored) - assert report["migration_name"] == "sprint7c_f_additive_medication_history" - assert report["schema_version"] == 7 + assert report["migration_name"] == "sprint7c_f1_medication_capture_hotfix" + assert report["schema_version"] == 8 assert report["dry_run"] == report["idempotency"] == report["restore_proof_status"] == "ok" assert report["medication_master_rows"] == 7 assert report["medication_event_rows"] == 5 __HERMES_CWD_8d46a20096ed__/home/agent/.hermes/repos/HealthManager__HERMES_CWD_8d46a20096ed__