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));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import {
|
||||
DELETE_BY_ID_AND_ARTICLE_ID,
|
||||
DELETE_PRICE,
|
||||
GET_BY_ID_AND_ARTICLE_ID,
|
||||
} from '../../providers/mssql/queries/articlesupplier.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 resolveArticleBySku from '../article/resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function del(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
export default function del(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
|
||||
/**
|
||||
* Deletes a supplier assignment and cleans up its associated purchase price.
|
||||
@@ -15,55 +14,52 @@ export default function del(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {number} id - The id of the articlesupplier record.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If the supplier assignment is not found.
|
||||
*
|
||||
* @example
|
||||
* await store.articlesupplier.delete(42, 'SKU-1', 12);
|
||||
*/
|
||||
return async (googleAuthId, sku, id) => {
|
||||
return async (googleAuthId, sku, id, tx) => {
|
||||
if (id == null || !Number.isFinite(id)) {
|
||||
throw new Error('Supplier assignment not found');
|
||||
}
|
||||
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku);
|
||||
const runDelete = async (runner) => {
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const existingRes = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('articleId', articleId)
|
||||
.input('companyId', companyId)
|
||||
.query(GET_BY_ID_AND_ARTICLE_ID);
|
||||
const existingRes = await runner.query(queries.GET_BY_ID_AND_ARTICLE_ID, {
|
||||
id,
|
||||
articleId,
|
||||
companyId,
|
||||
});
|
||||
|
||||
const existing = existingRes.recordset[0];
|
||||
if (!existing) {
|
||||
throw new Error('Supplier assignment not found');
|
||||
}
|
||||
const existing = existingRes.rows[0];
|
||||
if (!existing) {
|
||||
throw new Error('Supplier assignment not found');
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('articleId', articleId)
|
||||
.query(DELETE_BY_ID_AND_ARTICLE_ID);
|
||||
await runner.query(queries.DELETE_BY_ID_AND_ARTICLE_ID, {
|
||||
id,
|
||||
articleId,
|
||||
});
|
||||
|
||||
if (existing.priceId != null) {
|
||||
await request(pool, tx)
|
||||
.input('id', existing.priceId)
|
||||
.input('articleId', articleId)
|
||||
.query(DELETE_PRICE);
|
||||
await runner.query(queries.DELETE_PRICE, {
|
||||
id: existing.priceId,
|
||||
articleId,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
if (tx) {
|
||||
return runDelete(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runDelete);
|
||||
}
|
||||
return runDelete(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { GET_SUPPLIER_BY_ID, SUPPLIER_GROUP_NAME } from '../../providers/mssql/queries/entity.js';
|
||||
import { GET_COMPANY_DEFAULT_TAX } from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import request from '../request.js';
|
||||
import {
|
||||
GET_SUPPLIER_BY_ID as MSSQL_GET_SUPPLIER_BY_ID,
|
||||
SUPPLIER_GROUP_NAME as MSSQL_SUPPLIER_GROUP_NAME,
|
||||
} from '../../providers/mssql/queries/entity.js';
|
||||
import {
|
||||
GET_SUPPLIER_BY_ID as SQLITE_GET_SUPPLIER_BY_ID,
|
||||
SUPPLIER_GROUP_NAME as SQLITE_SUPPLIER_GROUP_NAME,
|
||||
} from '../../providers/sqlite3/queries/entity.js';
|
||||
import { GET_COMPANY_DEFAULT_TAX as MSSQL_DEFAULT_TAX } from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import { GET_COMPANY_DEFAULT_TAX as SQLITE_DEFAULT_TAX } from '../../providers/sqlite3/queries/articlesupplier.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export function optionalString(value) {
|
||||
if (value == null || value === '') return null;
|
||||
@@ -30,26 +38,58 @@ export function shapeArticleSupplier(row) {
|
||||
};
|
||||
}
|
||||
|
||||
export async function assertSupplier(pool, tx, companyId, supplierId) {
|
||||
export async function assertSupplier(runnerOrPool, txOrCompanyId, maybeCompanyId, maybeSupplierId) {
|
||||
let runner;
|
||||
let companyId;
|
||||
let supplierId;
|
||||
|
||||
if (maybeSupplierId !== undefined) {
|
||||
runner = makeRunner(runnerOrPool, txOrCompanyId);
|
||||
companyId = maybeCompanyId;
|
||||
supplierId = maybeSupplierId;
|
||||
} else {
|
||||
runner = makeRunner(runnerOrPool);
|
||||
companyId = txOrCompanyId;
|
||||
supplierId = maybeCompanyId;
|
||||
}
|
||||
|
||||
if (supplierId == null || !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);
|
||||
const isSqlite = runner?.dialect === 'sqlite3';
|
||||
const getSupplierSql = isSqlite ? SQLITE_GET_SUPPLIER_BY_ID : MSSQL_GET_SUPPLIER_BY_ID;
|
||||
const groupName = isSqlite ? SQLITE_SUPPLIER_GROUP_NAME : MSSQL_SUPPLIER_GROUP_NAME;
|
||||
|
||||
if (!result.recordset[0]) {
|
||||
const result = await runner.query(getSupplierSql, {
|
||||
id: supplierId,
|
||||
companyId,
|
||||
groupName,
|
||||
});
|
||||
|
||||
if (!result.rows[0]) {
|
||||
throw new Error('Supplier not found');
|
||||
}
|
||||
return supplierId;
|
||||
}
|
||||
|
||||
export async function resolveCompanyDefaultTax(pool, tx, companyId) {
|
||||
const result = await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.query(GET_COMPANY_DEFAULT_TAX);
|
||||
return result.recordset[0]?.taxrate ?? null;
|
||||
export async function resolveCompanyDefaultTax(runnerOrPool, txOrCompanyId, maybeCompanyId) {
|
||||
let runner;
|
||||
let companyId;
|
||||
|
||||
if (maybeCompanyId !== undefined) {
|
||||
runner = makeRunner(runnerOrPool, txOrCompanyId);
|
||||
companyId = maybeCompanyId;
|
||||
} else {
|
||||
runner = makeRunner(runnerOrPool);
|
||||
companyId = txOrCompanyId;
|
||||
}
|
||||
|
||||
const isSqlite = runner?.dialect === 'sqlite3';
|
||||
const defaultTaxSql = isSqlite ? SQLITE_DEFAULT_TAX : MSSQL_DEFAULT_TAX;
|
||||
|
||||
const result = await runner.query(defaultTaxSql, {
|
||||
companyId,
|
||||
});
|
||||
return result.rows[0]?.taxrate ?? null;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import { LIST_BY_ARTICLE_ID } from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import { LIST_BY_ARTICLE_ID as MSSQL_QUERY } from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import { LIST_BY_ARTICLE_ID as SQLITE_QUERY } from '../../providers/sqlite3/queries/articlesupplier.js';
|
||||
import resolveArticleBySku from '../article/resolveArticleBySku.js';
|
||||
import { shapeArticleSupplier } from './helpers.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function listByArticleSku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
export default function listByArticleSku(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_QUERY : MSSQL_QUERY;
|
||||
|
||||
/**
|
||||
* Lists the supplier assignments for an article by its SKU.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {object} [tx] - Optional transaction.
|
||||
* @returns {Promise<Array<{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 supplier assignments.
|
||||
* @throws {Error} If the article is not found or the user is not authorized.
|
||||
*
|
||||
@@ -17,15 +21,16 @@ export default function listByArticleSku(pool) {
|
||||
* const suppliers = await store.articlesupplier.listByArticleSku(42, 'SKU-1');
|
||||
* // => [{ id, articleId, entityId, supplierName, sku, pu, stock, priceId, price }]
|
||||
*/
|
||||
return async (googleAuthId, sku) => {
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku);
|
||||
return async (googleAuthId, sku, tx) => {
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('companyId', companyId)
|
||||
.query(LIST_BY_ARTICLE_ID);
|
||||
const result = await runner.query(sql, {
|
||||
articleId,
|
||||
companyId,
|
||||
});
|
||||
|
||||
return result.recordset.map(shapeArticleSupplier);
|
||||
return result.rows.map(shapeArticleSupplier);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { LIST_BY_ENTITY_AND_SKUS } from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import { LIST_BY_ENTITY_AND_SKUS as MSSQL_QUERY } from '../../providers/mssql/queries/articlesupplier.js';
|
||||
import { LIST_BY_ENTITY_AND_SKUS as SQLITE_QUERY } from '../../providers/sqlite3/queries/articlesupplier.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import { assertSupplier } from './helpers.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function parseJsonArray(raw) {
|
||||
if (!raw) return [];
|
||||
@@ -12,8 +14,9 @@ function parseJsonArray(raw) {
|
||||
}
|
||||
}
|
||||
|
||||
export default function listByEntityAndSkus(pool) {
|
||||
const resolveAccess = resolveSelectedCompanyAccess(pool);
|
||||
export default function listByEntityAndSkus(provider) {
|
||||
const resolveAccess = resolveSelectedCompanyAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_QUERY : MSSQL_QUERY;
|
||||
|
||||
/**
|
||||
* Lists article-supplier assignments for a given supplier (entityId) and SKU list,
|
||||
@@ -22,6 +25,7 @@ export default function listByEntityAndSkus(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number|{entityId?: number, supplierId?: number, skus?: string[]|string}} entityIdOrOptions - The supplier entity id or an options object.
|
||||
* @param {string[]|string} [maybeSkus] - The list of SKUs to filter by (or single SKU string).
|
||||
* @param {object} [tx] - Optional transaction.
|
||||
* @returns {Promise<Array<{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, article: {id: number, sku: string, name: string|null, slug: string|null, photos: Array<{sort: number, thumbhash: string, hash: string, thumbw: number, thumbh: number}>, articlepic: Array<{sort: number, thumbhash: string, hash: string, thumbw: number, thumbh: number}>, eans: string[], articleean: string[]}}>>} The supplier assignments with article details.
|
||||
* @throws {Error} If user is not authorized, entityId is missing, or supplier does not exist.
|
||||
*
|
||||
@@ -29,7 +33,7 @@ export default function listByEntityAndSkus(pool) {
|
||||
* const items = await store.articlesupplier.listByEntityAndSkus(42, 5, ['SKU-1', 'SKU-2']);
|
||||
* // => [{ id, articleId, entityId, supplierName, sku, pu, stock, sort, price, article: { id, sku, name, slug, photos, eans } }]
|
||||
*/
|
||||
return async (googleAuthId, entityIdOrOptions, maybeSkus) => {
|
||||
return async (googleAuthId, entityIdOrOptions, maybeSkus, tx) => {
|
||||
let entityId;
|
||||
let skus;
|
||||
|
||||
@@ -68,19 +72,19 @@ export default function listByEntityAndSkus(pool) {
|
||||
skusJson = JSON.stringify(Array.from(new Set(cleanList)));
|
||||
}
|
||||
|
||||
const { companyId, languageId } = await resolveAccess(googleAuthId);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId, languageId } = await resolveAccess(googleAuthId, runner);
|
||||
|
||||
await assertSupplier(pool, null, companyId, parsedEntityId);
|
||||
await assertSupplier(runner, companyId, parsedEntityId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', parsedEntityId)
|
||||
.input('companyId', companyId)
|
||||
.input('languageId', languageId)
|
||||
.input('skusJson', skusJson)
|
||||
.query(LIST_BY_ENTITY_AND_SKUS);
|
||||
const result = await runner.query(sql, {
|
||||
entityId: parsedEntityId,
|
||||
companyId,
|
||||
languageId,
|
||||
skusJson,
|
||||
});
|
||||
|
||||
return result.recordset.map((row) => {
|
||||
return result.rows.map((row) => {
|
||||
const rawPhotos = parseJsonArray(row.photosJson);
|
||||
const photos = rawPhotos.map((p) => ({
|
||||
sort: p.sort,
|
||||
@@ -128,3 +132,4 @@ export default function listByEntityAndSkus(pool) {
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import {
|
||||
LIST_IDS_BY_ARTICLE,
|
||||
SET_TEMP_SORTS,
|
||||
UPDATE_SORT,
|
||||
} from '../../providers/mssql/queries/articlesupplier.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 resolveArticleBySku from '../article/resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function order(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
export default function order(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
|
||||
/**
|
||||
* Reorders the supplier assignments of an article by its SKU.
|
||||
@@ -16,6 +15,7 @@ export default function order(pool) {
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {object} input - The reorder input.
|
||||
* @param {Array<number>} input.ids - The articlesupplier ids in their new order.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{ids: Array<number>}>} The applied order.
|
||||
* @throws {Error} If the order is invalid.
|
||||
*
|
||||
@@ -23,62 +23,58 @@ export default function order(pool) {
|
||||
* const result = await store.articlesupplier.order(42, 'SKU-1', { ids: [12, 10, 15] });
|
||||
* // => { ids: [12, 10, 15] }
|
||||
*/
|
||||
return async (googleAuthId, sku, { ids } = {}) => {
|
||||
return async (googleAuthId, sku, { ids } = {}, tx) => {
|
||||
if (!Array.isArray(ids)) {
|
||||
throw new Error('Invalid supplier order');
|
||||
}
|
||||
|
||||
const { articleId } = await resolve(googleAuthId, sku);
|
||||
const runOrder = async (runner) => {
|
||||
const { articleId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const current = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.query(LIST_IDS_BY_ARTICLE);
|
||||
const current = await runner.query(queries.LIST_IDS_BY_ARTICLE, {
|
||||
articleId,
|
||||
});
|
||||
|
||||
const existing = new Set(current.recordset.map((r) => r.id));
|
||||
const existing = new Set(current.rows.map((r) => r.id));
|
||||
|
||||
if (ids.length !== existing.size) {
|
||||
throw new Error('Invalid supplier order');
|
||||
}
|
||||
|
||||
const seen = new Set();
|
||||
for (const id of ids) {
|
||||
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
|
||||
if (ids.length !== existing.size) {
|
||||
throw new Error('Invalid supplier order');
|
||||
}
|
||||
seen.add(id);
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
const seen = new Set();
|
||||
for (const id of ids) {
|
||||
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
|
||||
throw new Error('Invalid supplier order');
|
||||
}
|
||||
seen.add(id);
|
||||
}
|
||||
|
||||
try {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.query(SET_TEMP_SORTS);
|
||||
await runner.query(queries.SET_TEMP_SORTS, {
|
||||
articleId,
|
||||
});
|
||||
|
||||
for (let sort = 0; sort < ids.length; sort++) {
|
||||
const updateRes = await request(pool, tx)
|
||||
.input('id', ids[sort])
|
||||
.input('articleId', articleId)
|
||||
.input('sort', sort)
|
||||
.query(UPDATE_SORT);
|
||||
const updateRes = await runner.query(queries.UPDATE_SORT, {
|
||||
id: ids[sort],
|
||||
articleId,
|
||||
sort,
|
||||
});
|
||||
|
||||
if (!updateRes.rowsAffected[0]) {
|
||||
if (!updateRes.rowsAffected) {
|
||||
throw new Error('Invalid supplier order');
|
||||
}
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
|
||||
return { ids };
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return runOrder(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runOrder);
|
||||
}
|
||||
return runOrder(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import {
|
||||
DELETE_PRICE,
|
||||
GET_BY_ID_AND_ARTICLE_ID,
|
||||
UPDATE as UPDATE_ARTICLE_SUPPLIER,
|
||||
UPDATE_PRICE,
|
||||
} 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';
|
||||
@@ -14,10 +10,15 @@ import {
|
||||
resolveCompanyDefaultTax,
|
||||
shapeArticleSupplier,
|
||||
} from './helpers.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function update(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
export default function update(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;
|
||||
|
||||
/**
|
||||
* Updates an existing supplier assignment and its associated purchase price.
|
||||
@@ -33,6 +34,7 @@ export default function update(pool) {
|
||||
* @param {number|null} [patch.netPrice] - Net purchase price.
|
||||
* @param {number} [patch.taxId] - The tax id for the purchase price.
|
||||
* @param {number} [patch.taxRate] - The tax rate in percent.
|
||||
* @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 updated assignment.
|
||||
* @throws {Error} If input is invalid or supplier assignment is not found.
|
||||
*
|
||||
@@ -44,7 +46,7 @@ export default function update(pool) {
|
||||
* });
|
||||
* // => { id, articleId, entityId, supplierName, sku, pu, stock, sort, priceId, price }
|
||||
*/
|
||||
return async (googleAuthId, sku, id, patch = {}) => {
|
||||
return async (googleAuthId, sku, id, patch = {}, tx) => {
|
||||
if (id == null || !Number.isFinite(id)) {
|
||||
throw new Error('Supplier assignment not found');
|
||||
}
|
||||
@@ -73,24 +75,20 @@ export default function update(pool) {
|
||||
}
|
||||
}
|
||||
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku);
|
||||
const runUpdate = async (runner) => {
|
||||
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const existingRes = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('articleId', articleId)
|
||||
.input('companyId', companyId)
|
||||
.query(GET_BY_ID_AND_ARTICLE_ID);
|
||||
const existingRes = await runner.query(queries.GET_BY_ID_AND_ARTICLE_ID, {
|
||||
id,
|
||||
articleId,
|
||||
companyId,
|
||||
});
|
||||
|
||||
const existing = existingRes.recordset[0];
|
||||
if (!existing) {
|
||||
throw new Error('Supplier assignment not found');
|
||||
}
|
||||
const existing = existingRes.rows[0];
|
||||
if (!existing) {
|
||||
throw new Error('Supplier assignment not found');
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
let priceId = existing.priceId;
|
||||
|
||||
if (patch.netPrice !== undefined) {
|
||||
@@ -99,20 +97,20 @@ export default function update(pool) {
|
||||
const oldPriceId = priceId;
|
||||
priceId = null;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('articleId', articleId)
|
||||
.input('sku', existing.sku)
|
||||
.input('stock', existing.stock)
|
||||
.input('pu', existing.pu)
|
||||
.input('sort', existing.sort)
|
||||
.input('priceId', null)
|
||||
.query(UPDATE_ARTICLE_SUPPLIER);
|
||||
await runner.query(queries.UPDATE, {
|
||||
id,
|
||||
articleId,
|
||||
sku: existing.sku,
|
||||
stock: existing.stock,
|
||||
pu: existing.pu,
|
||||
sort: existing.sort,
|
||||
priceId: null,
|
||||
});
|
||||
|
||||
await request(pool, tx)
|
||||
.input('id', oldPriceId)
|
||||
.input('articleId', articleId)
|
||||
.query(DELETE_PRICE);
|
||||
await runner.query(queries.DELETE_PRICE, {
|
||||
id: oldPriceId,
|
||||
articleId,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// netPrice is a valid number
|
||||
@@ -120,49 +118,47 @@ export default function update(pool) {
|
||||
let taxId = patch.taxId;
|
||||
if (taxId == null || !Number.isFinite(taxId)) {
|
||||
if (patch.taxRate != null && Number.isFinite(patch.taxRate)) {
|
||||
taxId = await ensureTax(patch.taxRate, tx);
|
||||
taxId = await ensureTax(patch.taxRate, runner);
|
||||
} else {
|
||||
taxId = existing.priceTaxId;
|
||||
}
|
||||
}
|
||||
|
||||
await request(pool, tx)
|
||||
.input('id', priceId)
|
||||
.input('articleId', articleId)
|
||||
.input('net', patch.netPrice)
|
||||
.input('taxId', taxId)
|
||||
.query(UPDATE_PRICE);
|
||||
await runner.query(queries.UPDATE_PRICE, {
|
||||
id: priceId,
|
||||
articleId,
|
||||
net: patch.netPrice,
|
||||
taxId,
|
||||
});
|
||||
} else {
|
||||
// create new price row
|
||||
let taxId = patch.taxId;
|
||||
if (taxId == null || !Number.isFinite(taxId)) {
|
||||
if (patch.taxRate != null && Number.isFinite(patch.taxRate)) {
|
||||
taxId = await ensureTax(patch.taxRate, tx);
|
||||
taxId = await ensureTax(patch.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', patch.netPrice)
|
||||
.query(CREATE_PRICE);
|
||||
const priceRes = await runner.query(createPriceSql, {
|
||||
taxId,
|
||||
articleId,
|
||||
minmultiplier: 1,
|
||||
customerGroupId,
|
||||
net: patch.netPrice,
|
||||
});
|
||||
|
||||
priceId = priceRes.recordset[0].id;
|
||||
priceId = priceRes.rows[0]?.id ?? priceRes.insertId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -178,33 +174,32 @@ export default function update(pool) {
|
||||
const nextPu = patch.pu !== undefined ? patch.pu : existing.pu;
|
||||
const nextSort = patch.sort !== undefined ? patch.sort : existing.sort;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('articleId', articleId)
|
||||
.input('sku', nextSku)
|
||||
.input('stock', nextStock)
|
||||
.input('pu', nextPu)
|
||||
.input('sort', nextSort)
|
||||
.input('priceId', priceId)
|
||||
.query(UPDATE_ARTICLE_SUPPLIER);
|
||||
await runner.query(queries.UPDATE, {
|
||||
id,
|
||||
articleId,
|
||||
sku: nextSku,
|
||||
stock: nextStock,
|
||||
pu: nextPu,
|
||||
sort: nextSort,
|
||||
priceId,
|
||||
});
|
||||
|
||||
await tx.commit();
|
||||
const loadRes = await runner.query(queries.GET_BY_ID_AND_ARTICLE_ID, {
|
||||
id,
|
||||
articleId,
|
||||
companyId,
|
||||
});
|
||||
|
||||
const loadRes = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.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 runUpdate(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runUpdate);
|
||||
}
|
||||
return runUpdate(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user