This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,13 +1,24 @@
import { CREATE_MANIFEST, ORDER_TYPE } from '../../providers/mssql/queries/manifestDoc.js';
import request from '../request.js';
import {
CREATE_MANIFEST as MSSQL_CREATE_MANIFEST,
ORDER_TYPE as MSSQL_ORDER_TYPE,
} from '../../providers/mssql/queries/manifestDoc.js';
import {
CREATE_MANIFEST as SQLITE_CREATE_MANIFEST,
ORDER_TYPE as SQLITE_ORDER_TYPE,
} from '../../providers/sqlite3/queries/manifestDoc.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import taxEnsure from '../tax/ensure.js';
import { insertLines, normalizeLines, parseDate } from '../manifestLines.js';
import { assertCustomerEntity, loadDoc } from '../manifestDoc/helpers.js';
import makeRunner from '../runner.js';
export default function create(pool) {
const access = resolveSelectedCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
export default function create(provider) {
const access = resolveSelectedCompanyAccess(provider);
const ensureTax = taxEnsure(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const createManifestSql = isSqlite ? SQLITE_CREATE_MANIFEST : MSSQL_CREATE_MANIFEST;
const orderType = isSqlite ? SQLITE_ORDER_TYPE : MSSQL_ORDER_TYPE;
/**
* Creates a new order for the user's selected company with its lines.
@@ -19,6 +30,7 @@ export default function create(pool) {
* @param {number} [body.entityId] - The id of the customer entity.
* @param {string} [body.externalNumber] - An external (customer) reference number.
* @param {Array<{sku?: string, name?: string, pu?: number, amount: number, net: number, rebate?: number, taxrate: number, comment?: string|null}>} [body.lines] - The order lines.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, lines: Array<{id: number, sku: string|null, name: string|null, pu: number, amount: number, net: number, rebate: number, taxrate: number, comment: string|null}>}>} The created order.
* @throws {Error} If the order is invalid.
*
@@ -26,7 +38,7 @@ export default function create(pool) {
* const order = await store.order.create(42, { number: 'B-2026-001', date: '2026-09-15', entityId: 12, lines: [...] });
* // => { id, number, type, entityId, externalNumber, date, lines: [...] }
*/
return async (googleAuthId, body = {}) => {
return async (googleAuthId, body = {}, tx) => {
const number = body.number?.trim?.() ? body.number.trim() : null;
const date = parseDate(body.date);
if (!number || !date) {
@@ -37,50 +49,49 @@ export default function create(pool) {
: null;
const lines = normalizeLines(body.lines ?? []);
const { companyId } = await access(googleAuthId);
const tx = pool.transaction();
await tx.begin();
const runCreate = async (runner) => {
const { companyId } = await access(googleAuthId, runner);
try {
const entityId = await assertCustomerEntity(
pool,
tx,
runner,
companyId,
body.entityId ?? null,
);
const created = await request(pool, tx)
.input('companyId', companyId)
.input('number', number)
.input('type', ORDER_TYPE)
.input('entityId', entityId)
.input('externalNumber', externalNumber)
.input('date', date)
.query(CREATE_MANIFEST);
const created = await runner.query(createManifestSql, {
companyId,
number,
type: orderType,
entityId,
externalNumber,
date,
});
const manifestId = created.recordset[0].id;
const manifestId = created.rows[0]?.id ?? created.insertId;
await insertLines(pool, tx, {
await insertLines(runner, {
manifestId,
lines,
ensureTax,
});
const doc = await loadDoc(pool, tx, {
type: ORDER_TYPE,
const doc = await loadDoc(runner, {
type: orderType,
companyId,
id: manifestId,
});
await tx.commit();
return doc;
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
};
if (tx) {
return runCreate(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runCreate);
}
return runCreate(makeRunner(provider));
};
}

View File

@@ -1,36 +1,49 @@
import { DELETE_MANIFEST, ORDER_TYPE } from '../../providers/mssql/queries/manifestDoc.js';
import {
DELETE_MANIFEST as MSSQL_DELETE,
ORDER_TYPE as MSSQL_ORDER_TYPE,
} from '../../providers/mssql/queries/manifestDoc.js';
import {
DELETE_MANIFEST as SQLITE_DELETE,
ORDER_TYPE as SQLITE_ORDER_TYPE,
} from '../../providers/sqlite3/queries/manifestDoc.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function del(pool) {
const access = resolveSelectedCompanyAccess(pool);
export default function del(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
const orderType = isSqlite ? SQLITE_ORDER_TYPE : MSSQL_ORDER_TYPE;
/**
* Deletes an order by its id within the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The id of the order to delete.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<void>}
* @throws {Error} If the order is not found.
*
* @example
* await store.order.delete(42, 5);
*/
return async (googleAuthId, id) => {
return async (googleAuthId, id, tx) => {
if (!Number.isFinite(id)) {
throw new Error('Order not found');
}
const { companyId } = await access(googleAuthId);
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const result = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.input('type', ORDER_TYPE)
.query(DELETE_MANIFEST);
const result = await runner.query(deleteSql, {
id,
companyId,
type: orderType,
});
if (!result.rowsAffected[0]) {
if (!result.rowsAffected) {
throw new Error('Order not found');
}
};
}

View File

@@ -1,15 +1,20 @@
import { ORDER_TYPE } from '../../providers/mssql/queries/manifestDoc.js';
import { ORDER_TYPE as MSSQL_ORDER_TYPE } from '../../providers/mssql/queries/manifestDoc.js';
import { ORDER_TYPE as SQLITE_ORDER_TYPE } from '../../providers/sqlite3/queries/manifestDoc.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import { loadDoc } from '../manifestDoc/helpers.js';
import makeRunner from '../runner.js';
export default function getById(pool) {
const access = resolveSelectedCompanyAccess(pool);
export default function getById(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const orderType = isSqlite ? SQLITE_ORDER_TYPE : MSSQL_ORDER_TYPE;
/**
* Returns a single order by its id within the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The id of the order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, lines: Array<{id: number, sku: string|null, name: string|null, pu: number, amount: number, net: number, rebate: number, taxrate: number, comment: string|null}>}>} The order with its lines.
* @throws {Error} If the order is not found.
*
@@ -17,17 +22,19 @@ export default function getById(pool) {
* const order = await store.order.getById(42, 5);
* // => { id, number, type, entityId, externalNumber, date, lines: [...] }
*/
return async (googleAuthId, id) => {
return async (googleAuthId, id, tx) => {
if (!Number.isFinite(id)) {
throw new Error('Order not found');
}
const { companyId } = await access(googleAuthId);
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const doc = await loadDoc(pool, null, { type: ORDER_TYPE, companyId, id });
const doc = await loadDoc(runner, { type: orderType, companyId, id });
if (!doc) {
throw new Error('Order not found');
}
return doc;
};
}

View File

@@ -1,29 +1,42 @@
import { LIST_MANIFESTS, ORDER_TYPE } from '../../providers/mssql/queries/manifestDoc.js';
import {
LIST_MANIFESTS as MSSQL_LIST,
ORDER_TYPE as MSSQL_ORDER_TYPE,
} from '../../providers/mssql/queries/manifestDoc.js';
import {
LIST_MANIFESTS as SQLITE_LIST,
ORDER_TYPE as SQLITE_ORDER_TYPE,
} from '../../providers/sqlite3/queries/manifestDoc.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import { shapeSummary } from '../manifestDoc/helpers.js';
import makeRunner from '../runner.js';
export default function list(pool) {
const access = resolveSelectedCompanyAccess(pool);
export default function list(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const sql = isSqlite ? SQLITE_LIST : MSSQL_LIST;
const orderType = isSqlite ? SQLITE_ORDER_TYPE : MSSQL_ORDER_TYPE;
/**
* Lists the orders of the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string}>>} The orders.
*
* @example
* const orders = await store.order.list(42);
* // => [{ id, number, type, entityId, externalNumber, date }]
*/
return async (googleAuthId) => {
const { companyId } = await access(googleAuthId);
return async (googleAuthId, tx) => {
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const result = await pool
.request()
.input('companyId', companyId)
.input('type', ORDER_TYPE)
.query(LIST_MANIFESTS);
const result = await runner.query(sql, {
companyId,
type: orderType,
});
return result.recordset.map((row) => shapeSummary(ORDER_TYPE, row));
return result.rows.map((row) => shapeSummary(orderType, row));
};
}

View File

@@ -1,18 +1,30 @@
import {
DELETE_LINES_BY_MANIFEST,
GET_MANIFEST_HEADER,
ORDER_TYPE,
UPDATE_MANIFEST,
DELETE_LINES_BY_MANIFEST as MSSQL_DELETE_LINES,
GET_MANIFEST_HEADER as MSSQL_GET_HEADER,
ORDER_TYPE as MSSQL_ORDER_TYPE,
UPDATE_MANIFEST as MSSQL_UPDATE_MANIFEST,
} from '../../providers/mssql/queries/manifestDoc.js';
import request from '../request.js';
import {
DELETE_LINES_BY_MANIFEST as SQLITE_DELETE_LINES,
GET_MANIFEST_HEADER as SQLITE_GET_HEADER,
ORDER_TYPE as SQLITE_ORDER_TYPE,
UPDATE_MANIFEST as SQLITE_UPDATE_MANIFEST,
} from '../../providers/sqlite3/queries/manifestDoc.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import taxEnsure from '../tax/ensure.js';
import { insertLines, normalizeLines, parseDate } from '../manifestLines.js';
import { assertCustomerEntity, loadDoc, shapeSummary } from '../manifestDoc/helpers.js';
import makeRunner from '../runner.js';
export default function update(pool) {
const access = resolveSelectedCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
export default function update(provider) {
const access = resolveSelectedCompanyAccess(provider);
const ensureTax = taxEnsure(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteLinesSql = isSqlite ? SQLITE_DELETE_LINES : MSSQL_DELETE_LINES;
const getHeaderSql = isSqlite ? SQLITE_GET_HEADER : MSSQL_GET_HEADER;
const orderType = isSqlite ? SQLITE_ORDER_TYPE : MSSQL_ORDER_TYPE;
const updateManifestSql = isSqlite ? SQLITE_UPDATE_MANIFEST : MSSQL_UPDATE_MANIFEST;
/**
* Updates an order's header and/or lines.
@@ -25,6 +37,7 @@ export default function update(pool) {
* @param {number} [patch.entityId] - The new customer entity id.
* @param {string} [patch.externalNumber] - The new external (customer) reference number.
* @param {Array<{sku?: string, name?: string, pu?: number, amount: number, net: number, rebate?: number, taxrate: number, comment?: string|null}>} [patch.lines] - The new order lines.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, lines: Array<{id: number, sku: string|null, name: string|null, pu: number, amount: number, net: number, rebate: number, taxrate: number, comment: string|null}>}>} The updated order.
* @throws {Error} If the order is not found.
*
@@ -32,7 +45,7 @@ export default function update(pool) {
* const order = await store.order.update(42, 5, { number: 'B-2026-002', lines: [...] });
* // => { id, number, type, entityId, date, lines: [...] }
*/
return async (googleAuthId, id, patch = {}) => {
return async (googleAuthId, id, patch = {}, tx) => {
if (!Number.isFinite(id)) {
throw new Error('Order not found');
}
@@ -52,24 +65,21 @@ export default function update(pool) {
normalizedLines = normalizeLines(patch.lines);
}
const { companyId } = await access(googleAuthId);
const runUpdate = async (runner) => {
const { companyId } = await access(googleAuthId, runner);
const tx = pool.transaction();
await tx.begin();
const existing = await runner.query(getHeaderSql, {
id,
companyId,
type: orderType,
});
try {
const existing = await request(pool, tx)
.input('id', id)
.input('companyId', companyId)
.input('type', ORDER_TYPE)
.query(GET_MANIFEST_HEADER);
const current = existing.recordset[0];
const current = existing.rows[0];
if (!current) {
throw new Error('Order not found');
}
const summary = shapeSummary(ORDER_TYPE, current);
const summary = shapeSummary(orderType, current);
let number = summary.number;
if (hasNumber) {
@@ -90,8 +100,7 @@ export default function update(pool) {
let entityId = summary.entityId;
if (hasEntityId) {
entityId = await assertCustomerEntity(
pool,
tx,
runner,
companyId,
patch.entityId ?? null,
);
@@ -104,38 +113,39 @@ export default function update(pool) {
: null;
}
await request(pool, tx)
.input('id', id)
.input('companyId', companyId)
.input('type', ORDER_TYPE)
.input('number', number)
.input('entityId', entityId)
.input('externalNumber', externalNumber)
.input('date', date)
.query(UPDATE_MANIFEST);
await runner.query(updateManifestSql, {
id,
companyId,
type: orderType,
number,
entityId,
externalNumber,
date,
});
if (hasLines) {
await request(pool, tx)
.input('manifestId', id)
.query(DELETE_LINES_BY_MANIFEST);
await runner.query(deleteLinesSql, {
manifestId: id,
});
await insertLines(pool, tx, {
await insertLines(runner, {
manifestId: id,
lines: normalizedLines,
ensureTax,
});
}
const doc = await loadDoc(pool, tx, { type: ORDER_TYPE, companyId, id });
await tx.commit();
const doc = await loadDoc(runner, { type: orderType, companyId, id });
return doc;
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
};
if (tx) {
return runUpdate(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runUpdate);
}
return runUpdate(makeRunner(provider));
};
}