Add company-scoped delivery store on manifest

This commit is contained in:
seb
2026-08-06 05:32:48 +02:00
parent 4f46967f28
commit 0b7e40ac9c
14 changed files with 2133 additions and 1375 deletions

View File

@@ -0,0 +1,67 @@
import { CREATE_MANIFEST, DELIVERY_TYPE } from '../../sql/delivery.js';
import request from '../request.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import taxEnsure from '../tax/ensure.js';
import {
loadDelivery,
normalizeLines,
parseDeliveryDate,
} from './helpers.js';
import { assertSupplier, insertLines } from './writeLines.js';
export default function create(pool) {
const access = resolveCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
return async (googleAuthId, companyId, body = {}) => {
const number = body.number?.trim?.() ? body.number.trim() : null;
const date = parseDeliveryDate(body.date);
if (!number || !date) {
throw new Error('Invalid delivery');
}
const lines = normalizeLines(body.lines ?? []);
const { rootCategoryId } = await access(googleAuthId, companyId);
const tx = pool.transaction();
await tx.begin();
try {
const supplierId = await assertSupplier(
pool,
tx,
companyId,
body.supplierId ?? null,
);
const created = await request(pool, tx)
.input('companyId', companyId)
.input('number', number)
.input('type', DELIVERY_TYPE)
.input('entityId', supplierId)
.input('date', date)
.query(CREATE_MANIFEST);
const manifestId = created.recordset[0].id;
await insertLines(pool, tx, {
manifestId,
rootCategoryId,
lines,
ensureTax,
});
const delivery = await loadDelivery(pool, tx, companyId, manifestId);
await tx.commit();
return delivery;
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}

View File

@@ -0,0 +1,25 @@
import { DELETE_MANIFEST, DELIVERY_TYPE } from '../../sql/delivery.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function del(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Delivery not found');
}
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.input('type', DELIVERY_TYPE)
.query(DELETE_MANIFEST);
if (!result.rowsAffected[0]) {
throw new Error('Delivery not found');
}
};
}

View File

@@ -0,0 +1,20 @@
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import { loadDelivery } from './helpers.js';
export default function getById(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Delivery not found');
}
await access(googleAuthId, companyId);
const delivery = await loadDelivery(pool, null, companyId, id);
if (!delivery) {
throw new Error('Delivery not found');
}
return delivery;
};
}

View File

@@ -0,0 +1,104 @@
import {
DELIVERY_TYPE,
GET_DELIVERY_HEADER,
GET_DELIVERY_LINES,
} from '../../sql/delivery.js';
import request from '../request.js';
export function shapeSummary(row) {
return {
id: row.id,
number: row.number,
type: DELIVERY_TYPE,
supplierId: row.supplierId ?? null,
date: row.date,
};
}
export function shapeLine(row) {
return {
id: row.id,
articleId: row.articleId ?? null,
sku: row.sku ?? null,
amount: row.amount,
net: row.net,
transactedAmount: row.transactedAmount,
rebate: row.rebate,
tax: row.tax,
comment: row.comment ?? null,
};
}
export async function loadDelivery(pool, tx, companyId, id) {
const header = await request(pool, tx)
.input('id', id)
.input('companyId', companyId)
.input('type', DELIVERY_TYPE)
.query(GET_DELIVERY_HEADER);
const row = header.recordset[0];
if (!row) {
return null;
}
const lines = await request(pool, tx)
.input('manifestId', id)
.query(GET_DELIVERY_LINES);
return {
...shapeSummary(row),
lines: lines.recordset.map(shapeLine),
};
}
export function parseDeliveryDate(value) {
if (value instanceof Date && !Number.isNaN(value.getTime())) {
return value;
}
if (typeof value === 'string' && value.trim()) {
const d = new Date(value);
if (!Number.isNaN(d.getTime())) {
return d;
}
}
return null;
}
export function normalizeLines(lines) {
if (!Array.isArray(lines)) {
throw new Error('Invalid lines');
}
return lines.map((entry) => {
const sku = entry?.sku?.trim?.() ? entry.sku.trim() : null;
if (!sku) {
throw new Error('Invalid lines');
}
const amount = entry?.amount;
const net = entry?.net;
const rebate =
entry?.rebate === undefined || entry?.rebate === null ? 0 : entry.rebate;
const tax = entry?.tax;
const transactedAmount =
entry?.transactedAmount === undefined || entry?.transactedAmount === null
? amount
: entry.transactedAmount;
const comment =
entry?.comment === undefined || entry?.comment === null
? null
: String(entry.comment);
if (
!Number.isFinite(amount) ||
!Number.isFinite(net) ||
!Number.isFinite(rebate) ||
!Number.isFinite(tax) ||
!Number.isFinite(transactedAmount)
) {
throw new Error('Invalid lines');
}
return { sku, amount, net, rebate, tax, transactedAmount, comment };
});
}

View File

@@ -0,0 +1,19 @@
import { DELIVERY_TYPE, LIST_DELIVERIES } from '../../sql/delivery.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import { shapeSummary } from './helpers.js';
export default function list(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId) => {
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('companyId', companyId)
.input('type', DELIVERY_TYPE)
.query(LIST_DELIVERIES);
return result.recordset.map(shapeSummary);
};
}

View File

@@ -0,0 +1,123 @@
import {
DELETE_LINES_BY_MANIFEST,
DELIVERY_TYPE,
GET_DELIVERY_HEADER,
UPDATE_MANIFEST,
} from '../../sql/delivery.js';
import request from '../request.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import taxEnsure from '../tax/ensure.js';
import {
loadDelivery,
normalizeLines,
parseDeliveryDate,
shapeSummary,
} from './helpers.js';
import { assertSupplier, insertLines } from './writeLines.js';
export default function update(pool) {
const access = resolveCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
return async (googleAuthId, companyId, id, patch = {}) => {
if (!Number.isFinite(id)) {
throw new Error('Delivery not found');
}
const hasNumber = Object.prototype.hasOwnProperty.call(patch, 'number');
const hasDate = Object.prototype.hasOwnProperty.call(patch, 'date');
const hasSupplierId = Object.prototype.hasOwnProperty.call(
patch,
'supplierId',
);
const hasLines = Object.prototype.hasOwnProperty.call(patch, 'lines');
if (!hasNumber && !hasDate && !hasSupplierId && !hasLines) {
throw new Error('Invalid delivery');
}
let normalizedLines;
if (hasLines) {
normalizedLines = normalizeLines(patch.lines);
}
const { rootCategoryId } = await access(googleAuthId, companyId);
const tx = pool.transaction();
await tx.begin();
try {
const existing = await request(pool, tx)
.input('id', id)
.input('companyId', companyId)
.input('type', DELIVERY_TYPE)
.query(GET_DELIVERY_HEADER);
const current = existing.recordset[0];
if (!current) {
throw new Error('Delivery not found');
}
const summary = shapeSummary(current);
let number = summary.number;
if (hasNumber) {
number = patch.number?.trim?.() ? patch.number.trim() : null;
if (!number) {
throw new Error('Invalid delivery');
}
}
let date = summary.date;
if (hasDate) {
date = parseDeliveryDate(patch.date);
if (!date) {
throw new Error('Invalid delivery');
}
}
let supplierId = summary.supplierId;
if (hasSupplierId) {
supplierId = await assertSupplier(
pool,
tx,
companyId,
patch.supplierId ?? null,
);
}
await request(pool, tx)
.input('id', id)
.input('companyId', companyId)
.input('type', DELIVERY_TYPE)
.input('number', number)
.input('entityId', supplierId)
.input('date', date)
.query(UPDATE_MANIFEST);
if (hasLines) {
await request(pool, tx)
.input('manifestId', id)
.query(DELETE_LINES_BY_MANIFEST);
await insertLines(pool, tx, {
manifestId: id,
rootCategoryId,
lines: normalizedLines,
ensureTax,
});
}
const delivery = await loadDelivery(pool, tx, companyId, id);
await tx.commit();
return delivery;
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}

View File

@@ -0,0 +1,66 @@
import {
CREATE_LINE,
RESOLVE_ARTICLE_ID_BY_SKU,
} from '../../sql/delivery.js';
import { GET_SUPPLIER_BY_ID, SUPPLIER_GROUP_NAME } from '../../sql/entity.js';
import request from '../request.js';
export async function assertSupplier(pool, tx, companyId, supplierId) {
if (supplierId == null) {
return null;
}
if (!Number.isFinite(supplierId)) {
throw new Error('Supplier not found');
}
const result = await request(pool, tx)
.input('id', supplierId)
.input('companyId', companyId)
.input('groupName', SUPPLIER_GROUP_NAME)
.query(GET_SUPPLIER_BY_ID);
if (!result.recordset[0]) {
throw new Error('Supplier not found');
}
return supplierId;
}
async function resolveArticleId(pool, tx, rootCategoryId, sku) {
const result = await request(pool, tx)
.input('rootCategoryId', rootCategoryId)
.input('sku', sku)
.query(RESOLVE_ARTICLE_ID_BY_SKU);
const articleId = result.recordset[0]?.id;
if (!articleId) {
throw new Error('Article not found');
}
return articleId;
}
export async function insertLines(
pool,
tx,
{ manifestId, rootCategoryId, lines, ensureTax },
) {
for (const line of lines) {
const articleId = await resolveArticleId(
pool,
tx,
rootCategoryId,
line.sku,
);
const taxId = await ensureTax(line.tax, tx);
await request(pool, tx)
.input('manifestId', manifestId)
.input('articleId', articleId)
.input('amount', line.amount)
.input('net', line.net)
.input('transactedAmount', line.transactedAmount)
.input('comment', line.comment)
.input('rebate', line.rebate)
.input('taxId', taxId)
.query(CREATE_LINE);
}
}