/// // Projects (client companies), per-asset project link + branded QR flag, // admin feature toggles, and a per-project asset count view. const SIGNED_IN = "@request.auth.id != ''"; migrate((app) => { const projects = new Collection({ type: "base", name: "projects", listRule: SIGNED_IN, // Public so the AR page can show the client logo via `expand=project` viewRule: "", createRule: SIGNED_IN, updateRule: SIGNED_IN, deleteRule: null, fields: [ { type: "text", name: "name", required: true, max: 120 }, { type: "file", name: "logo", maxSelect: 1, maxSize: 2097152, mimeTypes: ["image/png", "image/webp"] }, { type: "select", name: "logo_bg", required: true, maxSelect: 1, values: ["light", "dark"] }, { type: "text", name: "brand_color", pattern: "^#[0-9a-fA-F]{6}$" }, { type: "text", name: "description", max: 500 }, { type: "select", name: "status", required: true, maxSelect: 1, values: ["active", "archived"] }, { type: "autodate", name: "created", onCreate: true, onUpdate: false }, { type: "autodate", name: "updated", onCreate: true, onUpdate: true }, ], indexes: ["CREATE INDEX idx_projects_status ON projects (status)"], }); app.save(projects); const assets = app.findCollectionByNameOrId("ar_assets"); assets.fields.add(new Field({ type: "relation", name: "project", collectionId: projects.id, maxSelect: 1, cascadeDelete: false })); assets.fields.add(new Field({ type: "bool", name: "branded_qr" })); app.save(assets); const settings = new Collection({ type: "base", name: "app_settings", // Public read: the AR page needs `brand_button` listRule: "", viewRule: "", createRule: null, updateRule: SIGNED_IN, deleteRule: null, fields: [ { type: "bool", name: "logo_required" }, { type: "bool", name: "allow_svg_logos" }, { type: "bool", name: "branded_qr_default" }, { type: "bool", name: "brand_button" }, ], }); app.save(settings); const defaults = new Record(settings); defaults.set("branded_qr_default", true); defaults.set("brand_button", true); app.save(defaults); const counts = new Collection({ type: "view", name: "project_asset_counts", listRule: SIGNED_IN, viewRule: SIGNED_IN, viewQuery: "SELECT p.id AS id, COUNT(a.id) AS asset_count FROM projects p LEFT JOIN ar_assets a ON a.project = p.id GROUP BY p.id", }); app.save(counts); }, (app) => { app.delete(app.findCollectionByNameOrId("project_asset_counts")); app.delete(app.findCollectionByNameOrId("app_settings")); const assets = app.findCollectionByNameOrId("ar_assets"); assets.fields.removeByName("project"); assets.fields.removeByName("branded_qr"); app.save(assets); app.delete(app.findCollectionByNameOrId("projects")); });