WFDASH-WEBAPP/scripts/migrations/2026-10-28-add-collection-auto-push.ts
Dave 3eea87ec03 feat(collection-confirm): auto-push purple cards on to invoicing, with a 2-day log
After each AI tick, every Collection Confirm card the page shows purple
(cardAiState verified) is confirmed without a person. Customer lines go
to ready for invoicing with the invoice number and date the AI read; stock
orders go to stock booking once every line is collected. Cards with a
problem flag, a photo for another PO, or no readable invoice number/date
stay for review.

Each push is recorded in ai_settings.collection_auto_push_log and shown at
the top of the page for today and yesterday (PO, supplier, items, value).
Off by default behind a Settings > AI switch; needs the manual live step
npm run pb:migrate:collection-auto-push before it will run.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-28 11:36:23 +02:00

46 lines
2 KiB
TypeScript

/**
* One-shot migration: Collection Confirm auto-push.
*
* ai_settings.collection_auto_push_enabled bool (off by default)
* ai_settings.collection_auto_push_log json (what was pushed, last 2 days)
*
* Run with: npm run pb:migrate:collection-auto-push
*
* Idempotent. Manual step on the live PB, before deploying. Until it has
* run the app never auto-pushes (it needs the log to record what it did).
*/
import "./_env-loader";
import PocketBase from "pocketbase";
const PB_URL = process.env.NEXT_PUBLIC_POCKETBASE_URL || process.env.PB_URL || "http://127.0.0.1:8090";
const ADMIN_EMAIL = process.env.PB_ADMIN_EMAIL;
const ADMIN_PASSWORD = process.env.PB_ADMIN_PASSWORD;
const TAG = "[collection-auto-push]";
const FIELDS: Array<Record<string, unknown> & { name: string }> = [
{ name: "collection_auto_push_enabled", type: "bool", required: false, presentable: false },
{ name: "collection_auto_push_log", type: "json", required: false, presentable: false, maxSize: 200000 },
];
async function main() {
if (!ADMIN_EMAIL || !ADMIN_PASSWORD) {
throw new Error("Set PB_ADMIN_EMAIL and PB_ADMIN_PASSWORD (e.g. from .env.local).");
}
const pb = new PocketBase(PB_URL);
await pb.collection("_superusers").authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
console.log(`${TAG} Authenticated against ${PB_URL}`);
const collection = await pb.collections.getOne("ai_settings");
const existing = new Set(collection.fields.map((f: { name: string }) => f.name));
const missing = FIELDS.filter((f) => !existing.has(f.name));
for (const f of FIELDS) if (existing.has(f.name)) console.log(`${TAG} ai_settings.${f.name} already exists — skipping.`);
if (missing.length > 0) {
await pb.collections.update(collection.id, { fields: [...collection.fields, ...missing] });
for (const f of missing) console.log(`${TAG} ai_settings.${f.name} added.`);
}
console.log(`${TAG} Done.`);
}
main().catch((err) => {
console.error(`${TAG} Migration failed:`, err);
process.exit(1);
});