Files
dashlibstorage/src/store/unit/delete.js
seb 53a20c4353 refactor: introduce database provider abstraction, isolate MSSQL layer
- Add provider contract (src/providers/provider.js): query() -> {rows,
  rowsAffected, insertId}, callback transaction(fn), exec() for stored
  procs, initialize/purge lifecycle
- Add MssqlProvider (src/providers/mssql/index.js) wrapping an mssql
  ConnectionPool with normalized results, tx commit/rollback and
  constraint-error classifiers (unique 2627/2601, FK 547)
- LibDashStore constructor now takes a provider instead of a pool
  (breaking); adds LibDashStore.create() async factory; keeps pool as
  transitional bridge until store modules are converted (see TODO.md)
- Move MSSQL-specific code into the provider: src/sql/* ->
  src/providers/mssql/queries/*, src/schema.js ->
  src/providers/mssql/schema.js, purge SQL -> purgeSql.js
- Update scripts, generate-schema-graph.js and package.json exports
  (./providers/mssql, ./providers/provider)
- Add TODO.md with the remaining provider/SQLite migration plan

No SQL or behavioral changes.
2026-09-20 13:51:01 +02:00

43 lines
1.1 KiB
JavaScript

import { DELETE_BY_COMPANY_AND_SI } from '../../providers/mssql/queries/unit.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
export default function del(pool) {
const access = resolveSelectedCompanyAccess(pool);
/**
* Deletes a unit for the user's selected company by its SI code.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} si - The SI code of the unit to delete.
* @returns {Promise<void>}
* @throws {Error} If the unit is not found.
*
* @example
* await store.unit.delete(42, 'l');
*/
return async (googleAuthId, si) => {
if (!si?.trim()) {
throw new Error('Unit not found');
}
const { companyId } = await access(googleAuthId);
try {
const result = await pool
.request()
.input('companyId', companyId)
.input('si', si.trim())
.query(DELETE_BY_COMPANY_AND_SI);
if (!result.rowsAffected[0]) {
throw new Error('Unit not found');
}
} catch (err) {
if (err.number === 547) {
throw new Error('Unit in use');
}
throw err;
}
};
}