feat(articlesupplier): implement store methods, schema constraints, and embed in getBySku
This commit is contained in:
156
src/store/articlesupplier/create.js
Normal file
156
src/store/articlesupplier/create.js
Normal file
@@ -0,0 +1,156 @@
|
||||
import {
|
||||
CHECK_EXISTS,
|
||||
CREATE as CREATE_ARTICLE_SUPPLIER,
|
||||
GET_BY_ID_AND_ARTICLE_ID,
|
||||
} from '../../sql/articlesupplier.js';
|
||||
import { CREATE as CREATE_PRICE } from '../../sql/price.js';
|
||||
import request from '../request.js';
|
||||
import resolveArticleBySku from '../article/resolveArticleBySku.js';
|
||||
import ensureLieferantenGroup from '../supplier/ensureLieferantenGroup.js';
|
||||
import taxEnsure from '../tax/ensure.js';
|
||||
import {
|
||||
assertSupplier,
|
||||
optionalString,
|
||||
resolveCompanyDefaultTax,
|
||||
shapeArticleSupplier,
|
||||
} from './helpers.js';
|
||||
|
||||
export default function create(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
|
||||
/**
|
||||
* Assigns a supplier to an article, optionally creating a purchase price.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {object} input - The supplier assignment input.
|
||||
* @param {number} input.entityId - The id of the supplier entity.
|
||||
* @param {string} [input.sku] - The supplier's item number/SKU.
|
||||
* @param {number} [input.pu] - Packaging unit (defaults to 1).
|
||||
* @param {number|null} [input.stock] - Current stock at the supplier.
|
||||
* @param {number|null} [input.netPrice] - Net purchase price.
|
||||
* @param {number} [input.taxId] - The tax id for the purchase price.
|
||||
* @param {number} [input.taxRate] - The tax rate in percent (used if taxId is not provided).
|
||||
* @returns {Promise<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, priceId: number|null, price: {net: number, taxId: number, taxRate?: number}|null}>} The created assignment.
|
||||
* @throws {Error} If input is invalid, article or supplier is not found, or supplier is already assigned.
|
||||
*
|
||||
* @example
|
||||
* const created = await store.articlesupplier.create(42, 'SKU-1', {
|
||||
* entityId: 88,
|
||||
* sku: 'MET-44910',
|
||||
* pu: 1,
|
||||
* netPrice: 4.20,
|
||||
* });
|
||||
* // => { id, articleId, entityId, supplierName, sku, pu, stock, priceId, price }
|
||||
*/
|
||||
return async (googleAuthId, sku, input = {}) => {
|
||||
const entityId = input.entityId;
|
||||
if (entityId == null || !Number.isFinite(entityId)) {
|
||||
throw new Error('Supplier required');
|
||||
}
|
||||
|
||||
const pu = input.pu === undefined || input.pu === null ? 1 : input.pu;
|
||||
if (!Number.isFinite(pu) || pu <= 0) {
|
||||
throw new Error('Invalid pu');
|
||||
}
|
||||
|
||||
const supplierSku = optionalString(input.sku);
|
||||
|
||||
const stock =
|
||||
input.stock === undefined || input.stock === null ? null : input.stock;
|
||||
if (stock !== null && !Number.isFinite(stock)) {
|
||||
throw new Error('Invalid stock');
|
||||
}
|
||||
|
||||
const netPrice =
|
||||
input.netPrice === undefined || input.netPrice === null
|
||||
? null
|
||||
: input.netPrice;
|
||||
if (netPrice !== null && !Number.isFinite(netPrice)) {
|
||||
throw new Error('Invalid price');
|
||||
}
|
||||
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku);
|
||||
|
||||
await assertSupplier(pool, null, companyId, entityId);
|
||||
|
||||
const existingCheck = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('entityId', entityId)
|
||||
.query(CHECK_EXISTS);
|
||||
|
||||
if (existingCheck.recordset[0]) {
|
||||
throw new Error('Supplier already assigned');
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
let priceId = null;
|
||||
|
||||
if (netPrice !== null) {
|
||||
let taxId = input.taxId;
|
||||
if (taxId == null || !Number.isFinite(taxId)) {
|
||||
if (input.taxRate != null && Number.isFinite(input.taxRate)) {
|
||||
taxId = await ensureTax(input.taxRate, tx);
|
||||
} else {
|
||||
const defaultTaxId = await resolveCompanyDefaultTax(
|
||||
pool,
|
||||
tx,
|
||||
companyId,
|
||||
);
|
||||
taxId = defaultTaxId != null ? defaultTaxId : await ensureTax(0, tx);
|
||||
}
|
||||
}
|
||||
|
||||
const customerGroupId = await ensureLieferantenGroup(
|
||||
pool,
|
||||
companyId,
|
||||
tx,
|
||||
);
|
||||
|
||||
const priceRes = await request(pool, tx)
|
||||
.input('taxId', taxId)
|
||||
.input('articleId', articleId)
|
||||
.input('minmultiplier', 1)
|
||||
.input('customerGroupId', customerGroupId)
|
||||
.input('net', netPrice)
|
||||
.query(CREATE_PRICE);
|
||||
|
||||
priceId = priceRes.recordset[0].id;
|
||||
}
|
||||
|
||||
const createdRes = await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('entityId', entityId)
|
||||
.input('priceId', priceId)
|
||||
.input('sku', supplierSku)
|
||||
.input('stock', stock)
|
||||
.input('pu', pu)
|
||||
.query(CREATE_ARTICLE_SUPPLIER);
|
||||
|
||||
const createdId = createdRes.recordset[0].id;
|
||||
|
||||
await tx.commit();
|
||||
|
||||
const loadRes = await pool
|
||||
.request()
|
||||
.input('id', createdId)
|
||||
.input('articleId', articleId)
|
||||
.input('companyId', companyId)
|
||||
.query(GET_BY_ID_AND_ARTICLE_ID);
|
||||
|
||||
return shapeArticleSupplier(loadRes.recordset[0]);
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user