57 lines
2.2 KiB
JavaScript
57 lines
2.2 KiB
JavaScript
/// <reference path="../pb_data/types.d.ts" />
|
|
|
|
/**
|
|
* Auto-promotion: when a NEW layoutMappings record is created, check for
|
|
* another record with the SAME fingerprintHash created by a DIFFERENT
|
|
* owner. If one exists, both are independent confirmations of the same
|
|
* layout - promote both to "shared" immediately, so a third user's
|
|
* matching upload applies it with zero manual work.
|
|
*
|
|
* "A different owner" is this project's own honest stand-in for the
|
|
* underlying spec's "a different practice" - there is no Practice/
|
|
* Client entity in this schema (Phase 4, not started), so a different
|
|
* PocketBase user account is the closest real signal available. See
|
|
* CLAUDE.md's "Layout mapper" entry for the full reasoning.
|
|
*
|
|
* Runs via e.app (bypasses every collection rule, same pattern
|
|
* ensure_default_project.pb.js already uses) since a normal user's own
|
|
* session could never update someone ELSE's mapping record through the
|
|
* API - this hook is the only path (besides a real superuser session)
|
|
* that can ever flip a mapping to "shared".
|
|
*
|
|
* Both records are kept and both flipped, rather than merging into one
|
|
* and deleting the other - simpler, and avoids leaving a dangling
|
|
* relation from any documents.layoutMappingUsed that might already
|
|
* point at either one.
|
|
*/
|
|
onRecordAfterCreateSuccess((e) => {
|
|
let mappings = e.app.findCollectionByNameOrId('layoutMappings')
|
|
let match = null
|
|
try {
|
|
match = e.app.findFirstRecordByFilter(
|
|
mappings,
|
|
'fingerprintHash = {:hash} && owner != {:owner} && id != {:id} && status != "retired"',
|
|
{ hash: e.record.get('fingerprintHash'), owner: e.record.get('owner'), id: e.record.id },
|
|
)
|
|
} catch (err) {
|
|
// findFirstRecordByFilter throws when nothing matches - no other
|
|
// user has created an equivalent mapping yet, nothing to promote.
|
|
match = null
|
|
}
|
|
|
|
if (match) {
|
|
let now = new Date().toISOString()
|
|
e.record.set('status', 'shared')
|
|
e.record.set('promotedReason', 'second_user_confirmation')
|
|
e.record.set('promotedAt', now)
|
|
e.app.save(e.record)
|
|
|
|
match.set('status', 'shared')
|
|
match.set('promotedReason', 'second_user_confirmation')
|
|
match.set('promotedAt', now)
|
|
e.app.save(match)
|
|
}
|
|
|
|
e.next()
|
|
}, 'layoutMappings')
|