chore(pocketbase): track live quotes hook and add git-based hook sync
quotes.pb.js is copied byte-for-byte from the live WFDASH PocketBase. DEPLOY lists the hooks the VPS cron syncs into the hooks volume; the audit hooks stay undeployed for now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
fd158d8fda
commit
72c714f46e
2 changed files with 71 additions and 0 deletions
8
DEPLOY
Normal file
8
DEPLOY
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Hook files the VPS sync copies into the live WFDASH PocketBase.
|
||||||
|
# One filename per line (relative to this folder). Anything not listed here
|
||||||
|
# is NOT deployed, and a *.js file on the server that is not listed here is
|
||||||
|
# removed on the next sync. See scripts/pb-hooks/README.md.
|
||||||
|
#
|
||||||
|
# Not deployed yet: audit_log.pb.js + audit_log_utils.js — add BOTH lines
|
||||||
|
# together when the audit trail is switched on (utils is required by the hook).
|
||||||
|
quotes.pb.js
|
||||||
63
quotes.pb.js
Executable file
63
quotes.pb.js
Executable file
|
|
@ -0,0 +1,63 @@
|
||||||
|
onRecordCreate((e) => {
|
||||||
|
if (e.record.getString("quote_number") !== "") {
|
||||||
|
e.next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
var now = new Date();
|
||||||
|
var year = now.getFullYear();
|
||||||
|
var month = String(now.getMonth() + 1).padStart(2, '0');
|
||||||
|
var prefix = "Q" + year + month;
|
||||||
|
|
||||||
|
let attempts = 0;
|
||||||
|
let candidate;
|
||||||
|
|
||||||
|
do {
|
||||||
|
// findRecordsByFilter accepts sort as the 3rd arg.
|
||||||
|
// findFirstRecordByFilter's 3rd arg is params (dbx.Params),
|
||||||
|
// which is why the old "-quote_number" sort string threw
|
||||||
|
// "could not convert -quote_number to dbx.Params".
|
||||||
|
const records = $app.findRecordsByFilter(
|
||||||
|
"quotes",
|
||||||
|
"quote_number != ''",
|
||||||
|
"-quote_number",
|
||||||
|
1,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
let newNum = 1;
|
||||||
|
if (records && records.length > 0) {
|
||||||
|
var raw = records[0].getString("quote_number");
|
||||||
|
// Extract trailing digits (handles Q202607011,
|
||||||
|
// legacy Q-2026-07-011, Q1782862717620, etc.).
|
||||||
|
var m = raw.match(/(\d+)$/);
|
||||||
|
if (m) {
|
||||||
|
newNum = parseInt(m[1], 10) + 1 + attempts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
candidate = prefix + String(newNum).padStart(3, '0');
|
||||||
|
attempts++;
|
||||||
|
|
||||||
|
// check if this candidate already exists
|
||||||
|
var exists;
|
||||||
|
try {
|
||||||
|
exists = $app.findFirstRecordByFilter(
|
||||||
|
"quotes",
|
||||||
|
"quote_number = {:num}",
|
||||||
|
{ num: candidate }
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
exists = null; // not found throws in some versions
|
||||||
|
}
|
||||||
|
} while (exists && attempts < 5);
|
||||||
|
|
||||||
|
e.record.set("quote_number", candidate);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Auto-numbering error:", err);
|
||||||
|
e.record.set("quote_number", "Q" + Date.now()); // fallback that's at least unique
|
||||||
|
}
|
||||||
|
|
||||||
|
e.next();
|
||||||
|
}, "quotes");
|
||||||
Loading…
Reference in a new issue