/// /** * Schema for the layout mapper (Phase 3, pulled forward - see CLAUDE.md's * "Layout mapper" entry). Two new collections: * * layoutMappings - one saved column mapping for a statement layout the * keyword/geometry detector (and layout_quirks) couldn't resolve on * their own. `owner` stays set after promotion to `shared` (provenance, * not access control at that point). `bankLabel`/`accountTypeLabel` are * free-text display-only grouping for the (not-yet-built) Layouts page - * there is no real Client/Practice entity in this schema (Phase 4, not * started), so this can't be a relation to one. * * The one deliberate deviation from every other collection's strict * owner-only rules: `listRule`/`viewRule` also allow `status = "shared"`, * since the whole point of a shared mapping is that OTHER users' uploads * can find and apply it. `updateRule` stays owner-only regardless - the * only ways a mapping's own status actually flips to "shared" are the * promote_shared_layout_mapping.pb.js hook (runs via e.app, bypasses * rules) and a real superuser session (also bypasses rules) - a normal * user can never promote their own or anyone else's mapping through the * API (see migration 6, which tightens `updateRule` after this one * shipped - a real gap found by testing, not caught here). `deleteRule` * lets an owner delete/retire their own still-private * mapping, but not a mapping that's already shared (pulling it out from * under other users who may already depend on it). * * layoutMappingVersions - a separate collection, not an embedded array * on layoutMappings, because the Layouts page (CLAUDE.md ยง6.1 of the * underlying spec) needs independently listable/comparable/roll-back- * able versions, and PocketBase has no first-class way to query/filter * into elements of a JSON array. Immutable history - `updateRule`/ * `deleteRule` are both `null` (superuser-only; normally nobody touches * one directly - a "rollback" creates a NEW version record instead of * editing an old one). */ migrate((app) => { let users = app.findCollectionByNameOrId('users') let documents = app.findCollectionByNameOrId('documents') let layoutMappings = new Collection({ type: 'base', name: 'layoutMappings', listRule: 'owner = @request.auth.id || status = "shared"', viewRule: 'owner = @request.auth.id || status = "shared"', createRule: 'owner = @request.auth.id', updateRule: 'owner = @request.auth.id', deleteRule: 'owner = @request.auth.id && status != "shared"', fields: [ { type: 'relation', name: 'owner', collectionId: users.id, required: true, maxSelect: 1 }, { type: 'text', name: 'name', required: true }, { type: 'text', name: 'bankLabel' }, { type: 'text', name: 'accountTypeLabel' }, { type: 'json', name: 'fingerprint', required: true }, { type: 'text', name: 'fingerprintHash', required: true }, { type: 'json', name: 'columnMap', required: true }, { type: 'select', name: 'dateFormat', maxSelect: 1, required: true, values: ['textual', 'dmy', 'mdy', 'ymd'], }, { type: 'json', name: 'signConvention' }, { type: 'json', name: 'ignoredRowRules' }, { type: 'select', name: 'status', maxSelect: 1, required: true, values: ['private', 'shared', 'stale', 'retired'], }, { type: 'number', name: 'version', required: true }, { type: 'relation', name: 'sourceDocument', collectionId: documents.id, maxSelect: 1 }, { type: 'number', name: 'usageCount' }, { type: 'number', name: 'failureCount' }, { type: 'date', name: 'lastUsedAt' }, { type: 'date', name: 'lastFailureAt' }, { type: 'select', name: 'promotedReason', maxSelect: 1, values: ['second_user_confirmation', 'superuser_approved'], }, { type: 'date', name: 'promotedAt' }, { type: 'text', name: 'approvedBySuperuser' }, { type: 'autodate', name: 'created', onCreate: true }, { type: 'autodate', name: 'updated', onCreate: true, onUpdate: true }, ], }) app.save(layoutMappings) let layoutMappingVersions = new Collection({ type: 'base', name: 'layoutMappingVersions', listRule: 'layoutMapping.owner = @request.auth.id || layoutMapping.status = "shared"', viewRule: 'layoutMapping.owner = @request.auth.id || layoutMapping.status = "shared"', createRule: 'layoutMapping.owner = @request.auth.id', updateRule: null, deleteRule: null, fields: [ { type: 'relation', name: 'layoutMapping', collectionId: layoutMappings.id, required: true, maxSelect: 1 }, { type: 'number', name: 'versionNumber', required: true }, { type: 'json', name: 'columnMap', required: true }, { type: 'select', name: 'dateFormat', maxSelect: 1, required: true, values: ['textual', 'dmy', 'mdy', 'ymd'], }, { type: 'json', name: 'signConvention' }, { type: 'json', name: 'ignoredRowRules' }, { type: 'relation', name: 'createdBy', collectionId: users.id, maxSelect: 1 }, { type: 'select', name: 'source', maxSelect: 1, required: true, values: ['user', 'tool', 'shared_library'], }, { type: 'text', name: 'note' }, { type: 'autodate', name: 'created', onCreate: true }, ], }) app.save(layoutMappingVersions) }, (app) => { try { app.delete(app.findCollectionByNameOrId('layoutMappingVersions')) } catch (e) {} try { app.delete(app.findCollectionByNameOrId('layoutMappings')) } catch (e) {} })