MEGAINFRA-WEBAPP/scripts/migrations/2026-10-17-worker-identity.mts
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

21 lines
2.1 KiB
TypeScript

/** Add worker identity uniqueness and recoverable removal metadata. No worker rows are modified. */
import PocketBase from "pocketbase";
import {writeFile,mkdir} from "node:fs/promises";
import {dirname} from "node:path";
const pb=new PocketBase(process.env.NEXT_PUBLIC_POCKETBASE_URL);
if(process.env.POCKETBASE_ADMIN_TOKEN)pb.authStore.save(process.env.POCKETBASE_ADMIN_TOKEN);
else await pb.collection("_superusers").authWithPassword(process.env.POCKETBASE_ADMIN_EMAIL!,process.env.POCKETBASE_ADMIN_PASSWORD!);
const schema=await pb.collections.getOne("employees");
const additions=[{name:"badge_key",type:"text",max:100},{name:"deleted_at",type:"date"},{name:"deleted_employee_no",type:"text",max:100}];
for(const field of additions){const existing=schema.fields.find((item:{name:string})=>item.name===field.name);if(existing&&existing.type!==field.type)throw new Error(`Unexpected field type: ${field.name}`);}
const missing=additions.filter(field=>!schema.fields.some((item:{name:string})=>item.name===field.name));
const index=`CREATE UNIQUE INDEX idx_employees_badge_key ON \`${schema.name}\` (badge_key) WHERE badge_key != ''`;
const hasIndex=schema.indexes.some((sql:string)=>sql.includes("idx_employees_badge_key"));
console.log(JSON.stringify({mode:process.argv.includes("--apply")?"apply":"dry-run",fields:missing.map(f=>f.name),addIndex:!hasIndex}));
if(process.argv.includes("--apply")&&(missing.length||!hasIndex)){
const backup=process.argv[process.argv.indexOf("--backup")+1];if(!process.argv.includes("--backup")||!backup)throw new Error("--backup PATH required");
await mkdir(dirname(backup),{recursive:true});await writeFile(backup,JSON.stringify(schema,null,2),{flag:"wx"});
await pb.collections.update(schema.id,{fields:[...schema.fields,...missing],indexes:[...schema.indexes,...(hasIndex?[]:[index])]});
const check=await pb.collections.getOne(schema.id);if(!check.indexes.some((sql:string)=>sql.includes("idx_employees_badge_key"))||additions.some(f=>!check.fields.some((v:{name:string})=>v.name===f.name)))throw new Error("Schema verification failed");
console.log("Verified worker identity fields and partial unique index; no worker records changed.");
}