ingest-worker.txt
/*
ALBERT LANE DIGITAL INFRASTRUCTURE
License: ALDI Sovereign License - see https://albertlane.net
Manifest: https://albertlane.net
ALDI VERIFIED No. 17684-273-411-436 SOVEREIGN AUDIT
NO_BEZOS NO_AWS NO_OPENAI NO_INTEL
Provenance: Defensive anti-trafficking relationship analysis only.
No victim PII, no sexual images, no facial recognition on victims.
*/
// Ingest Worker - POST /ingest/{company|event|site|signal}
// Writes to D1 (same schema.sql). No victim PII. Hash phones/emails before storage.
const KINDS = ["company","event","site","signal"];
function jsonResp(obj, status=200){
return new Response(JSON.stringify(obj), {status, headers: {"Content-Type":"application/json"}});
}
function hashId(prefix, ...parts){
const raw = parts.join("|");
let h = 0;
for(let i=0;i<raw.length;i++){ h = ((h<<5)-h + raw.charCodeAt(i))|0; }
return `${prefix}-${Math.abs(h).toString(16)}-${Date.now().toString(36)}`;
}
function normName(n){ return (n||"").toLowerCase().replace(/[^a-z0-9]+/g," ").trim(); }
export default {
async fetch(request, env){
const url = new URL(request.url);
if(request.method === "GET" && url.pathname === "/health"){
return jsonResp({ok:true, worker:"ingest", time:new Date().toISOString()});
}
if(request.method !== "POST") return jsonResp({error:"POST only, use /ingest/{kind}"}, 405);
const parts = url.pathname.split("/").filter(Boolean);
if(parts[0] !== "ingest" || !KINDS.includes(parts[1])) return jsonResp({error:"Use POST /ingest/company|event|site|signal"}, 400);
const kind = parts[1];
let rec;
try{ rec = await request.json(); } catch{ return jsonResp({error:"Invalid JSON"}, 400); }
if(!rec || typeof rec !== "object") return jsonResp({error:"Body must be JSON object"}, 400);
const blocked = ["victim_name","victim_phone","victim_email","victim_photo","victim_address","ssn"];
for(const b of blocked){ if(rec[b]) return jsonResp({error:`Blocked PII field: ${b}. Do not send victim PII.`}, 400); }
try{
if(kind === "company"){
const norm = normName(rec.name||"");
const id = rec.id || hashId("co", norm);
await env.DB.prepare(`INSERT OR REPLACE INTO companies (id,name,normalized_name,ticker,sector,country,website,source_id) VALUES (?,?,?,?,?,?,?,?)`)
.bind(id, rec.name, norm, rec.ticker||null, rec.sector||null, rec.country||null, rec.website||null, rec.source_id||null).run();
await env.DB.prepare(`INSERT OR REPLACE INTO nodes (id,node_type,label,external_key,country) VALUES (?,?,?,?,?)`)
.bind(`node-company-${id}`, "company", rec.name, `company:${id}`, rec.country||null).run();
return jsonResp({ok:true, id});
}
if(kind === "event"){
if(!rec.company_id) return jsonResp({error:"company_id required"}, 400);
const id = rec.id || hashId("evt", rec.company_id, rec.event_type||"", rec.announced_date||"");
await env.DB.prepare(`INSERT OR REPLACE INTO cost_cutting_events (id,company_id,event_type,announced_date,effective_date,description,jobs_affected,estimated_savings_usd,destination_country,destination_city,source_id) VALUES (?,?,?,?,?,?,?,?,?,?,?)`)
.bind(id, rec.company_id, rec.event_type||"other", rec.announced_date||null, rec.effective_date||null, rec.description||null, rec.jobs_affected||null, rec.estimated_savings_usd||null, rec.destination_country||null, rec.destination_city||null, rec.source_id||null).run();
await env.DB.prepare(`INSERT OR REPLACE INTO nodes (id,node_type,label,external_key,city,country,first_seen) VALUES (?,?,?,?,?,?,?)`)
.bind(`node-event-${id}`, "event", (rec.description||"cost-cutting event").slice(0,120), `event:${id}`, rec.destination_city||null, rec.destination_country||null, rec.announced_date||null).run();
return jsonResp({ok:true, id});
}
if(kind === "site"){
const id = rec.id || hashId("site", rec.name||"", rec.city||"", rec.country||"");
await env.DB.prepare(`INSERT OR REPLACE INTO sites (id,company_id,site_type,name,contractor_name,city,region,country,lat,lon,opened_date,closed_date,headcount,is_contractor_operated,source_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
.bind(id, rec.company_id||null, rec.site_type||"other", rec.name, rec.contractor_name||null, rec.city||null, rec.region||null, rec.country, rec.lat||null, rec.lon||null, rec.opened_date||null, rec.closed_date||null, rec.headcount||null, rec.is_contractor_operated?1:0, rec.source_id||null).run();
await env.DB.prepare(`INSERT OR REPLACE INTO nodes (id,node_type,label,external_key,city,region,country,lat,lon,first_seen) VALUES (?,?,?,?,?,?,?,?,?,?)`)
.bind(`node-site-${id}`, "site", rec.name, `site:${id}`, rec.city||null, rec.region||null, rec.country, rec.lat||null, rec.lon||null, rec.opened_date||null).run();
return jsonResp({ok:true, id});
}
if(kind === "signal"){
if(!rec.indicator_code) return jsonResp({error:"indicator_code required"}, 400);
const id = rec.id || hashId("sig", rec.indicator_code, rec.city||"", rec.observed_at||"");
const weight = rec.weight || 0.5;
await env.DB.prepare(`INSERT OR REPLACE INTO trafficking_signals (id,signal_category,indicator_family,indicator_code,weight,description,observed_at,city,region,country,lat,lon,confidence,status,company_id,site_id,source_id,evidence_ref) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
.bind(id, rec.signal_category||"trafficking", rec.indicator_family||"general", rec.indicator_code, weight, rec.description||null, rec.observed_at, rec.city||null, rec.region||null, rec.country||null, rec.lat||null, rec.lon||null, rec.confidence||0.5, rec.status||"unlinked", rec.company_id||null, rec.site_id||null, rec.source_id||null, rec.evidence_ref||null).run();
await env.DB.prepare(`INSERT OR REPLACE INTO nodes (id,node_type,label,external_key,city,region,country,lat,lon,first_seen) VALUES (?,?,?,?,?,?,?,?,?,?)`)
.bind(`node-signal-${id}`, "signal", `${rec.indicator_code} - ${rec.city||""} ${rec.observed_at||""}`, `signal:${id}`, rec.city||null, rec.region||null, rec.country||null, rec.lat||null, rec.lon||null, rec.observed_at||null).run();
return jsonResp({ok:true, id});
}
} catch(e){
return jsonResp({error: String(e.message||e)}, 500);
}
}
};