MEGAINFRA-WEBAPP/scripts/import-buildsite-fuel.ts
DEls 0a7138f60c fix: harden workforce, plant and financials
Improve workforce scanning, site transfers and timesheet exports.
Audit plant imports, duplicate identities, rates and dashboard totals.
Repair delivery PO selection, periodic rechecks and receiving guards.
Align financial ledgers, supplier periods and mobile layouts.
Add the site map preview, shared theme controls and audit evidence.

Verify 3,455 application tests, six fuel export tests, TypeScript and
an optimized production build. Clean up a leaked test timer and keep
local worktrees and generated reports out of ESLint traversal.

Changed-file lint passes; repository-wide lint retains 33 existing
errors in unchanged files.
2026-10-19 13:18:31 +02:00

31 lines
2.8 KiB
TypeScript

import fs from "node:fs/promises";
import path from "node:path";
import { pbAdmin } from "../src/lib/pb";
import { runFuelImport } from "../src/lib/fuel-import";
import { createFuelImportStore } from "../src/lib/fuel-import-store";
// Defaults to read-only. Commit requires explicit sites and the reviewed file hash.
async function main(){
const args=process.argv.slice(2),options:Record<string,string>={};let commit=false;
if(args.includes("--help")){console.log("node --env-file=.env.local --import tsx scripts/import-buildsite-fuel.ts --file PATH --sites SITE_ID,SITE_ID --out NEW_RESULT.json [--commit --expected-file-hash SHA256]");return;}
for(let i=0;i<args.length;i++){
if(args[i]==="--commit"){commit=true;continue;}
if(!["--file","--sites","--out","--expected-file-hash"].includes(args[i])||!args[i+1])throw new Error("Unknown option or missing value: "+args[i]);
options[args[i].slice(2)]=args[++i];
}
if(!options.file||!options.sites||!options.out)throw new Error("--file, --sites and --out are required");
if(commit&&!/^[a-f0-9]{64}$/.test(options["expected-file-hash"]??""))throw new Error("Commit requires --expected-file-hash from a reviewed preview");
const sites=new Set(options.sites.split(",").map(s=>s.trim()).filter(Boolean));
if(!sites.size)throw new Error("At least one explicit site ID is required");
const bytes=await fs.readFile(options.file),file=new File([new Uint8Array(bytes)],path.basename(options.file),{type:options.file.toLowerCase().endsWith(".csv")?"text/csv":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
const out=path.resolve(options.out);await fs.mkdir(path.dirname(out),{recursive:true});
const handle=await fs.open(out,"wx");
try{
const result=await runFuelImport(createFuelImportStore(await pbAdmin()),file,{mode:commit?"import":"preview",allowSite:()=>true,importSiteIds:sites,expectedFileHash:options["expected-file-hash"],actor:"agent:buildsite"});
await handle.writeFile(JSON.stringify(result,null,2)+"\n");
console.log(JSON.stringify({mode:commit?"import":"preview",result_file:out,documentId:result.documentId,totalRows:result.totalRows,newRows:result.newRows,imported:result.imported,duplicates:result.duplicates,linked:result.linked,reviewCount:result.reviewCount,errors:result.errors.length,newVehicles:result.newVehicles,fileHash:result.fileHash,status:result.status},null,2));
if(result.errors.length||result.reviewCount)process.exitCode=2;
}catch(error){await handle.writeFile(JSON.stringify({error:error instanceof Error?error.message:"Import failed",mode:commit?"import":"preview",...((error&&typeof error==="object"&&"documentId" in error)?{documentId:error.documentId}:{})},null,2)+"\n");throw error;}
finally{await handle.close();}
}
main().catch(error=>{console.error(error instanceof Error?error.message:"Fuel import failed");process.exitCode=1;});