Add ledger, address/contact sort, and tighter schema-graph layout.
Replace manifestarticle.transactedamount with an append-only ledger, add sort on entity address/contact, and allow same-rank FKs with crossing-aware exits in the graph.
This commit is contained in:
@@ -22,7 +22,6 @@ export function shapeLine(row) {
|
||||
sku: row.sku ?? null,
|
||||
amount: row.amount,
|
||||
net: row.net,
|
||||
transactedAmount: row.transactedAmount,
|
||||
rebate: row.rebate,
|
||||
tax: row.tax,
|
||||
comment: row.comment ?? null,
|
||||
@@ -80,10 +79,6 @@ export function normalizeLines(lines) {
|
||||
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
|
||||
@@ -93,12 +88,11 @@ export function normalizeLines(lines) {
|
||||
!Number.isFinite(amount) ||
|
||||
!Number.isFinite(net) ||
|
||||
!Number.isFinite(rebate) ||
|
||||
!Number.isFinite(tax) ||
|
||||
!Number.isFinite(transactedAmount)
|
||||
!Number.isFinite(tax)
|
||||
) {
|
||||
throw new Error('Invalid lines');
|
||||
}
|
||||
|
||||
return { sku, amount, net, rebate, tax, transactedAmount, comment };
|
||||
return { sku, amount, net, rebate, tax, comment };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -57,7 +57,6 @@ export async function insertLines(
|
||||
.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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { CREATE } from '../../sql/entityaddress.js';
|
||||
import { CREATE, GET_NEXT_SORT } from '../../sql/entityaddress.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
@@ -14,6 +14,7 @@ function shape(row) {
|
||||
city: row.city ?? null,
|
||||
county: row.county ?? null,
|
||||
addressType: row.addressType,
|
||||
sort: row.sort,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -38,6 +39,17 @@ export default function create(pool) {
|
||||
await access(googleAuthId, companyId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
let sort = input.sort;
|
||||
if (sort === undefined || sort === null) {
|
||||
const next = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.query(GET_NEXT_SORT);
|
||||
sort = next.recordset[0].sort;
|
||||
} else if (!Number.isFinite(sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
@@ -49,6 +61,7 @@ export default function create(pool) {
|
||||
.input('city', optionalString(input.city))
|
||||
.input('county', optionalString(input.county))
|
||||
.input('addressType', addressType)
|
||||
.input('sort', sort)
|
||||
.query(CREATE);
|
||||
|
||||
return shape(result.recordset[0]);
|
||||
|
||||
@@ -14,6 +14,7 @@ function shape(row) {
|
||||
city: row.city ?? null,
|
||||
county: row.county ?? null,
|
||||
addressType: row.addressType,
|
||||
sort: row.sort,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ function shape(row) {
|
||||
city: row.city ?? null,
|
||||
county: row.county ?? null,
|
||||
addressType: row.addressType,
|
||||
sort: row.sort,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -81,6 +82,14 @@ export default function update(pool) {
|
||||
}
|
||||
}
|
||||
|
||||
let sort = current.sort;
|
||||
if (Object.prototype.hasOwnProperty.call(patch, 'sort')) {
|
||||
if (!Number.isFinite(patch.sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
sort = patch.sort;
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
@@ -93,6 +102,7 @@ export default function update(pool) {
|
||||
.input('city', city)
|
||||
.input('county', county)
|
||||
.input('addressType', addressType)
|
||||
.input('sort', sort)
|
||||
.query(UPDATE);
|
||||
|
||||
const row = result.recordset[0];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { CREATE } from '../../sql/entitycontact.js';
|
||||
import { CREATE, GET_NEXT_SORT } from '../../sql/entitycontact.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
@@ -8,6 +8,7 @@ function shape(row) {
|
||||
entityId: row.entityId,
|
||||
contactType: row.contactType,
|
||||
contact: row.contact,
|
||||
sort: row.sort,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,11 +28,23 @@ export default function create(pool) {
|
||||
await access(googleAuthId, companyId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
let sort = input.sort;
|
||||
if (sort === undefined || sort === null) {
|
||||
const next = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.query(GET_NEXT_SORT);
|
||||
sort = next.recordset[0].sort;
|
||||
} else if (!Number.isFinite(sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.input('contactType', contactType)
|
||||
.input('contact', contact)
|
||||
.input('sort', sort)
|
||||
.query(CREATE);
|
||||
|
||||
return shape(result.recordset[0]);
|
||||
|
||||
@@ -8,6 +8,7 @@ function shape(row) {
|
||||
entityId: row.entityId,
|
||||
contactType: row.contactType,
|
||||
contact: row.contact,
|
||||
sort: row.sort,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ function shape(row) {
|
||||
entityId: row.entityId,
|
||||
contactType: row.contactType,
|
||||
contact: row.contact,
|
||||
sort: row.sort,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -50,12 +51,21 @@ export default function update(pool) {
|
||||
throw new Error('Contact required');
|
||||
}
|
||||
|
||||
let sort = current.sort;
|
||||
if (Object.prototype.hasOwnProperty.call(patch, 'sort')) {
|
||||
if (!Number.isFinite(patch.sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
sort = patch.sort;
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('entityId', entityId)
|
||||
.input('contactType', contactType)
|
||||
.input('contact', contact)
|
||||
.input('sort', sort)
|
||||
.query(UPDATE);
|
||||
|
||||
const row = result.recordset[0];
|
||||
|
||||
Reference in New Issue
Block a user