refactor: remove dead files, dedupe formatBytes and document numbering helpers

- Delete unused adminEvents-enhanced.js, backupService.original.js,
  databaseBackup.example.js, s3Storage.example.js, ThemeCustomizer.tsx
- Extract shared formatBytes to utils/formatBytes.js (was copied 4x)
- Centralize formatNumberInTemplate + next-document-number logic in
  utils/documentSequences.js (was copied in invoice/quote/contract services)
This commit is contained in:
Paul Nothaft
2026-07-03 07:25:07 +02:00
parent b04ef216f5
commit eb71fcf209
15 changed files with 59 additions and 1655 deletions
+29 -1
View File
@@ -30,6 +30,34 @@
const { db } = require('../database/db');
const { AppError } = require('./errors');
const { getAppSetting } = require('./appSettings');
/**
* Render a document-number format template.
* Tokens: {YEAR}, {MONTH}, {SEQ:04d} (zero-padded), {SEQ}.
*/
function formatNumberInTemplate(format, year, seq) {
return format
.replace(/\{YEAR\}/g, String(year))
.replace(/\{MONTH\}/g, String(new Date().getMonth() + 1).padStart(2, '0'))
.replace(/\{SEQ:(\d+)d\}/g, (_, pad) => String(seq).padStart(parseInt(pad, 10), '0'))
.replace(/\{SEQ\}/g, String(seq));
}
/**
* Claim + format the next document number for `kind` in the current
* year, using the admin-configurable format stored under `settingKey`.
*
* Reads the setting through `trx` when present — getAppSetting on the
* global db inside an open transaction deadlocks the single-connection
* SQLite pool.
*/
async function nextDocumentNumber(kind, settingKey, defaultFormat, trx) {
const format = (await getAppSetting(settingKey, null, trx || db)) || defaultFormat;
const year = new Date().getFullYear();
const seq = await claimNextSequence(kind, year, trx);
return formatNumberInTemplate(format, year, seq);
}
/**
* Claim the next sequence value for (kind, year). Returns the new
@@ -90,4 +118,4 @@ async function claimNextSequence(kind, year, trx) {
return await db.transaction(async (innerTrx) => exec(innerTrx));
}
module.exports = { claimNextSequence };
module.exports = { claimNextSequence, formatNumberInTemplate, nextDocumentNumber };
+18
View File
@@ -0,0 +1,18 @@
/**
* Human-readable byte size, e.g. 1536 -> "1.5 KB".
* Falsy/undefined input returns '0 Bytes'.
*/
function formatBytes(bytes, decimals = 2) {
if (!bytes) {
return '0 Bytes';
}
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
}
module.exports = { formatBytes };