'use strict'; // Customer-owned normalized model, not a Telnect webhook or an AI-generated response. const sample={callId:'synthetic-call-001',customerId:'synthetic-customer-001',customerMatch:'confirmed',proposedAction:'Skicka den efterfrågade integrationschecklistan.',sourceRef:'synthetic-transcript-001'}; function prepareDraft(input,store){ if(!input||input.customerMatch!=='confirmed'||!['callId','customerId','proposedAction','sourceRef'].every(key=>typeof input[key]==='string'&&input[key].trim()))return {status:'needs_review',reason:'missing_or_uncertain_input'}; // In production use a persistent unique constraint scoped to the authenticated tenant. const key=JSON.stringify([input.customerId,input.callId]); if(store.has(key))return {status:'existing',draft:store.get(key)}; const draft={key,customerId:input.customerId,callId:input.callId,action:input.proposedAction,sourceRef:input.sourceRef,status:'awaiting_human_review'}; store.set(key,draft);return {status:'created',draft}; } if(require.main===module){const assert=require('node:assert/strict'),store=new Map();const first=prepareDraft(sample,store);assert.equal(first.status,'created');assert.equal(prepareDraft(sample,store).status,'existing');assert.equal(prepareDraft({...sample,customerMatch:'uncertain'},store).status,'needs_review');assert.equal(store.size,1);console.log(JSON.stringify({label:'Syntetiskt lokalt exempel. Ingen AI, telefoni eller CRM har anropats.',result:first,checks:'Utkast, återspelning och osäker kundmatchning godkända.'},null,2));} module.exports={prepareDraft,sample};