bettersight-statement-extra.../pocketbase/pb_migrations/11_add_categorization_rules.js

44 lines
1.9 KiB
JavaScript

/// <reference path="../pb_data/types.d.ts" />
/**
* Schema for transaction categorization (see CLAUDE.md's "Transaction
* categorization" entry) - one new collection, categorizationRules.
*
* Project-scoped, not fingerprint/shared like layoutMappings - a client's
* own chart-of-accounts mapping has no cross-user "two independent
* confirmations" promotion scenario, so no hook is needed here (unlike
* promote_shared_layout_mapping.pb.js). Scoped by `owner` directly on
* every rule (the same choice `documents` makes despite also carrying a
* `project` relation), not by reaching through the project relation the
* way layoutMappingVersions has to reach through layoutMapping.owner.
*
* `project` mirrors documents.project's own shape exactly (required,
* maxSelect 1) - this is the very relation migration 1's own comment
* anticipated this feature would need.
*/
migrate((app) => {
let users = app.findCollectionByNameOrId('users')
let projects = app.findCollectionByNameOrId('projects')
let categorizationRules = new Collection({
type: 'base',
name: 'categorizationRules',
listRule: 'owner = @request.auth.id',
viewRule: 'owner = @request.auth.id',
createRule: 'owner = @request.auth.id',
updateRule: 'owner = @request.auth.id',
deleteRule: 'owner = @request.auth.id',
fields: [
{ type: 'relation', name: 'owner', collectionId: users.id, required: true, maxSelect: 1 },
{ type: 'relation', name: 'project', collectionId: projects.id, required: true, maxSelect: 1 },
{ type: 'text', name: 'pattern', required: true },
{ type: 'text', name: 'accountingCategory', required: true },
{ type: 'autodate', name: 'created', onCreate: true },
{ type: 'autodate', name: 'updated', onCreate: true, onUpdate: true },
],
})
app.save(categorizationRules)
}, (app) => {
try { app.delete(app.findCollectionByNameOrId('categorizationRules')) } catch (e) {}
})