125 lines
4.8 KiB
JavaScript
125 lines
4.8 KiB
JavaScript
|
|
// Create the `mechanics` PocketBase collection.
|
||
|
|
//
|
||
|
|
// A mechanic is a PERSON on the payroll with an employee number and a scannable badge.
|
||
|
|
// They are NOT plant items and they are NOT operators: an operator is allocated TO a
|
||
|
|
// machine, a mechanic is not. Separate collection, separate register, no shared keys.
|
||
|
|
//
|
||
|
|
// The employee code is the identity here. It is what the QR encodes, so it must be
|
||
|
|
// unique — the seed refuses a duplicate rather than printing two badges with the same
|
||
|
|
// code, which would be two people indistinguishable to a scanner.
|
||
|
|
//
|
||
|
|
// Idempotent: safe to re-run.
|
||
|
|
//
|
||
|
|
// Usage: node scripts/setup-mechanics-collections.mjs
|
||
|
|
import PocketBase from "pocketbase";
|
||
|
|
import { readFileSync, existsSync } from "node:fs";
|
||
|
|
import { fileURLToPath } from "node:url";
|
||
|
|
import { dirname, resolve } from "node:path";
|
||
|
|
|
||
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||
|
|
const ROOT = resolve(__dirname, "..");
|
||
|
|
|
||
|
|
const envPath = resolve(ROOT, ".env.local");
|
||
|
|
if (!existsSync(envPath)) {
|
||
|
|
console.error("✗ .env.local not found. Copy .env.example to .env.local and fill it in.");
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
for (const line of readFileSync(envPath, "utf8").split("\n")) {
|
||
|
|
const t = line.trim();
|
||
|
|
if (!t || t.startsWith("#")) continue;
|
||
|
|
const i = t.indexOf("=");
|
||
|
|
if (i > 0) process.env[t.slice(0, i).trim()] = t.slice(i + 1).trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
const PB_URL = process.env.NEXT_PUBLIC_PB_URL;
|
||
|
|
const EMAIL = process.env.PB_ADMIN;
|
||
|
|
const PASS = process.env.PB_PASS;
|
||
|
|
const missing = [];
|
||
|
|
if (!PB_URL) missing.push("NEXT_PUBLIC_PB_URL");
|
||
|
|
if (!EMAIL) missing.push("PB_ADMIN");
|
||
|
|
if (!PASS) missing.push("PB_PASS");
|
||
|
|
if (missing.length) {
|
||
|
|
console.error(`✗ Missing required env: ${missing.join(", ")}`);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const pb = new PocketBase(PB_URL);
|
||
|
|
pb.autoCancellation(false);
|
||
|
|
|
||
|
|
console.log(`→ Connecting to ${PB_URL} as ${EMAIL}`);
|
||
|
|
await pb.collection("_superusers").authWithPassword(EMAIL, PASS);
|
||
|
|
console.log(" ✓ authenticated");
|
||
|
|
|
||
|
|
const reference = await pb.collections.getOne("monthly_income");
|
||
|
|
const RULES = {
|
||
|
|
listRule: reference.listRule,
|
||
|
|
viewRule: reference.viewRule,
|
||
|
|
createRule: reference.createRule ?? null,
|
||
|
|
updateRule: reference.updateRule ?? null,
|
||
|
|
deleteRule: reference.deleteRule ?? null,
|
||
|
|
};
|
||
|
|
console.log(` · rules mirrored from monthly_income (list="${RULES.listRule}")`);
|
||
|
|
|
||
|
|
async function ensureCollection(name, fields) {
|
||
|
|
const existing = await pb.collections.getFullList({ filter: `name = "${name}"`, perPage: 1 });
|
||
|
|
if (existing.length) {
|
||
|
|
const have = new Set(existing[0].fields.map((f) => f.name));
|
||
|
|
const absent = fields.filter((f) => !have.has(f.name));
|
||
|
|
if (!absent.length) {
|
||
|
|
console.log(` · ${name} already present (${existing[0].fields.length} fields)`);
|
||
|
|
} else {
|
||
|
|
await updateSchema(name, (cur) => [...cur, ...absent], `added to ${name}: ${absent.map((f) => f.name).join(", ")}`);
|
||
|
|
}
|
||
|
|
return existing[0].id;
|
||
|
|
}
|
||
|
|
const created = await pb.send("/api/collections", {
|
||
|
|
method: "POST",
|
||
|
|
headers: { "Content-Type": "application/json" },
|
||
|
|
body: JSON.stringify({
|
||
|
|
name,
|
||
|
|
type: "base",
|
||
|
|
fields,
|
||
|
|
listRule: RULES.listRule ?? "",
|
||
|
|
viewRule: RULES.viewRule ?? "",
|
||
|
|
createRule: RULES.createRule ?? "",
|
||
|
|
updateRule: RULES.updateRule ?? "",
|
||
|
|
deleteRule: RULES.deleteRule ?? "",
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
console.log(` ✓ created ${name} (${created.id}) with ${created.fields.length} fields`);
|
||
|
|
return created.id;
|
||
|
|
}
|
||
|
|
|
||
|
|
async function updateSchema(collectionName, transform, label) {
|
||
|
|
const coll = await pb.collections.getOne(collectionName);
|
||
|
|
const next = transform([...coll.fields]);
|
||
|
|
if (JSON.stringify(next) === JSON.stringify(coll.fields)) {
|
||
|
|
console.log(` · ${collectionName} schema already current`);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
await pb.send(`/api/collections/${coll.name}`, {
|
||
|
|
method: "PATCH",
|
||
|
|
headers: { "Content-Type": "application/json" },
|
||
|
|
body: JSON.stringify({ fields: next }),
|
||
|
|
});
|
||
|
|
console.log(` ✓ ${label}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log("\n→ Ensuring collections");
|
||
|
|
|
||
|
|
await ensureCollection("mechanics", [
|
||
|
|
{ name: "name", type: "text", required: true },
|
||
|
|
// The identity a scanner reads. Unique across the register — the seed enforces it,
|
||
|
|
// because two badges sharing a code are two people a scanner cannot tell apart.
|
||
|
|
{ name: "employee_code", type: "text", required: true },
|
||
|
|
{ name: "role", type: "text" },
|
||
|
|
// Optional photo for the badge. Stored as a file on the record, never in the QR —
|
||
|
|
// a photo in a QR would bloat it past what a cheap scanner reads reliably.
|
||
|
|
{ name: "photo", type: "file", maxSelect: 1, maxSize: 2097152 },
|
||
|
|
{ name: "source", type: "text" },
|
||
|
|
{ name: "created", type: "date", onCreate: true, presentable: false },
|
||
|
|
{ name: "updated", type: "date", onUpdate: true, presentable: false },
|
||
|
|
]);
|
||
|
|
|
||
|
|
console.log("\n✓ mechanics ready. Next: npm run seed && node scripts/book-invoice.mjs");
|
||
|
|
process.exit(0);
|