30 lines
1.3 KiB
JavaScript
30 lines
1.3 KiB
JavaScript
// Generated, do not edit. sha256:85ed9f94869ff2f4ce59b8e785f0225bd901edca3d1e5c1790edb2c8a7b0b6fe
|
|
import * as db from '../db.js';
|
|
import { toSearchName } from '../../shared/search-name.js';
|
|
import { assertName } from './_shared.js';
|
|
import { trackDelete } from './change-log.js';
|
|
import { fail } from '../../shared/app-error.js';
|
|
|
|
export function listLabels() { return db.all(`SELECT * FROM Label ORDER BY name`); }
|
|
|
|
export function labelUsage() {
|
|
return db.all(`SELECT Label.id AS labelId, COUNT(WalletTransaction.id) AS walletTransactions
|
|
FROM Label
|
|
LEFT JOIN WalletTransaction ON WalletTransaction.labelId = Label.id
|
|
GROUP BY Label.id
|
|
ORDER BY Label.id`);
|
|
}
|
|
|
|
export async function createLabel({ name }) {
|
|
const naam = assertName('Labelnaam', name);
|
|
db.run(`INSERT INTO Label (name, searchName) VALUES (?,?)`, [naam, toSearchName(naam)]);
|
|
const id = db.lastId(); await db.persist(); return id;
|
|
}
|
|
|
|
export async function deleteLabel(id) {
|
|
const used = db.one(`SELECT (SELECT COUNT(*) FROM WalletTransaction WHERE labelId=?1) AS n`, [id]).n;
|
|
if (used > 0) fail('labels.inUse', { n: used }, `The label is used by ${used} item(s).`);
|
|
trackDelete('Label', id, () => db.run(`DELETE FROM Label WHERE id=?`, [id]));
|
|
await db.persist();
|
|
}
|