sqlite
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
import {
|
||||
CHECK_EXISTS,
|
||||
CREATE as CREATE_ARTICLE_SUPPLIER,
|
||||
GET_BY_ID_AND_ARTICLE_ID,
|
||||
GET_NEXT_SORT,
|
||||
} from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import { CREATE as CREATE_PRICE } from '../../providers/mssql/queries/price.js';
|
||||
import request from '../request.js';
|
||||
import * as mssqlQueries from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import * as sqliteQueries from '../../providers/sqlite3/queries/articlesupplier.js';
|
||||
import { CREATE as MSSQL_CREATE_PRICE } from '../../providers/mssql/queries/price.js';
|
||||
import { CREATE as SQLITE_CREATE_PRICE } from '../../providers/sqlite3/queries/price.js';
|
||||
import resolveArticleBySku from '../article/resolveArticleBySku.js';
|
||||
import ensureLieferantenGroup from '../supplier/ensureLieferantenGroup.js';
|
||||
import taxEnsure from '../tax/ensure.js';
|
||||
@@ -15,10 +11,15 @@ import {
|
||||
resolveCompanyDefaultTax,
|
||||
shapeArticleSupplier,
|
||||
} from './helpers.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function create(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
export default function create(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const ensureTax = taxEnsure(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
const createPriceSql = isSqlite ? SQLITE_CREATE_PRICE : MSSQL_CREATE_PRICE;
|
||||
|
||||
/**
|
||||
* Assigns a supplier to an article, optionally creating a purchase price.
|
||||
@@ -34,6 +35,7 @@ export default function create(pool) {
|
||||
* @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).
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, articleId: number, entityId: number, supplierName: string|null, sku: string|null, pu: number, stock: number|null, sort: number, 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.
|
||||
*
|
||||
@@ -46,7 +48,7 @@ export default function create(pool) {
|
||||
* });
|
||||
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, priceId, price }
|
||||
*/
|
||||
return async (googleAuthId, sku, input = {}) => {
|
||||
return async (googleAuthId, sku, input = {}, tx) => {
|
||||
const entityId = input.entityId;
|
||||
if (entityId == null || !Number.isFinite(entityId)) {
|
||||
throw new Error('Supplier required');
|
||||
@@ -79,30 +81,26 @@ export default function create(pool) {
|
||||
throw new Error('Invalid price');
|
||||
}
|
||||
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku);
|
||||
const runCreate = async (runner) => {
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
await assertSupplier(pool, null, companyId, entityId);
|
||||
await assertSupplier(runner, companyId, entityId);
|
||||
|
||||
const existingCheck = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('entityId', entityId)
|
||||
.query(CHECK_EXISTS);
|
||||
const existingCheck = await runner.query(queries.CHECK_EXISTS, {
|
||||
articleId,
|
||||
entityId,
|
||||
});
|
||||
|
||||
if (existingCheck.recordset[0]) {
|
||||
throw new Error('Supplier already assigned');
|
||||
}
|
||||
if (existingCheck.rows[0]) {
|
||||
throw new Error('Supplier already assigned');
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
let sort = input.sort;
|
||||
if (sort === undefined || sort === null) {
|
||||
const sortRes = await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.query(GET_NEXT_SORT);
|
||||
sort = sortRes.recordset[0].sort;
|
||||
const sortRes = await runner.query(queries.GET_NEXT_SORT, {
|
||||
articleId,
|
||||
});
|
||||
sort = sortRes.rows[0].sort;
|
||||
}
|
||||
|
||||
let priceId = null;
|
||||
@@ -111,63 +109,60 @@ export default function create(pool) {
|
||||
let taxId = input.taxId;
|
||||
if (taxId == null || !Number.isFinite(taxId)) {
|
||||
if (input.taxRate != null && Number.isFinite(input.taxRate)) {
|
||||
taxId = await ensureTax(input.taxRate, tx);
|
||||
taxId = await ensureTax(input.taxRate, runner);
|
||||
} else {
|
||||
const defaultTaxId = await resolveCompanyDefaultTax(
|
||||
pool,
|
||||
tx,
|
||||
runner,
|
||||
companyId,
|
||||
);
|
||||
taxId = defaultTaxId != null ? defaultTaxId : await ensureTax(0, tx);
|
||||
taxId = defaultTaxId != null ? defaultTaxId : await ensureTax(0, runner);
|
||||
}
|
||||
}
|
||||
|
||||
const customerGroupId = await ensureLieferantenGroup(
|
||||
pool,
|
||||
runner,
|
||||
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);
|
||||
const priceRes = await runner.query(createPriceSql, {
|
||||
taxId,
|
||||
articleId,
|
||||
minmultiplier: 1,
|
||||
customerGroupId,
|
||||
net: netPrice,
|
||||
});
|
||||
|
||||
priceId = priceRes.recordset[0].id;
|
||||
priceId = priceRes.rows[0]?.id ?? priceRes.insertId;
|
||||
}
|
||||
|
||||
const createdRes = await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('entityId', entityId)
|
||||
.input('priceId', priceId)
|
||||
.input('sku', supplierSku)
|
||||
.input('stock', stock)
|
||||
.input('pu', pu)
|
||||
.input('sort', sort)
|
||||
.query(CREATE_ARTICLE_SUPPLIER);
|
||||
const createdRes = await runner.query(queries.CREATE, {
|
||||
articleId,
|
||||
entityId,
|
||||
priceId,
|
||||
sku: supplierSku,
|
||||
stock,
|
||||
pu,
|
||||
sort,
|
||||
});
|
||||
|
||||
const createdId = createdRes.recordset[0].id;
|
||||
const createdId = createdRes.rows[0]?.id ?? createdRes.insertId;
|
||||
|
||||
await tx.commit();
|
||||
const loadRes = await runner.query(queries.GET_BY_ID_AND_ARTICLE_ID, {
|
||||
id: createdId,
|
||||
articleId,
|
||||
companyId,
|
||||
});
|
||||
|
||||
const loadRes = await pool
|
||||
.request()
|
||||
.input('id', createdId)
|
||||
.input('articleId', articleId)
|
||||
.input('companyId', companyId)
|
||||
.query(GET_BY_ID_AND_ARTICLE_ID);
|
||||
return shapeArticleSupplier(loadRes.rows[0]);
|
||||
};
|
||||
|
||||
return shapeArticleSupplier(loadRes.recordset[0]);
|
||||
} 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));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user