This commit is contained in:
seb
2026-09-29 16:16:05 +02:00
parent 26d2f83d64
commit ae5d5cc6f8
40 changed files with 2139 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
import { LIST_BY_ARTICLE_ID as MSSQL_QUERY } from '../../providers/mssql/queries/articleattributevalue.js';
import { LIST_BY_ARTICLE_ID as SQLITE_QUERY } from '../../providers/sqlite3/queries/articleattributevalue.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function listByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const sql = provider?.dialect === 'sqlite3' ? SQLITE_QUERY : MSSQL_QUERY;
/**
* Lists the attribute values assigned to 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, attributeId: number, attributeName: string, exclusive: boolean, attributeValueId: number, value: string, sort: number}>>}
*/
return async (googleAuthId, sku, tx) => {
const runner = makeRunner(provider, tx);
const { articleId } = await resolve(googleAuthId, sku, runner);
const result = await runner.query(sql, { articleId });
return result.rows.map((row) => ({
id: row.id,
articleId: row.articleId,
attributeId: row.attributeId,
attributeName: row.attributeName,
exclusive: Boolean(row.exclusive),
attributeValueId: row.attributeValueId,
value: row.value,
sort: row.sort,
}));
};
}

View File

@@ -0,0 +1,115 @@
import {
DELETE_BY_ARTICLE_ID as MSSQL_DELETE,
INSERT_ROW as MSSQL_INSERT,
GET_VALUE_DETAIL as MSSQL_GET_DETAIL,
} from '../../providers/mssql/queries/articleattributevalue.js';
import {
DELETE_BY_ARTICLE_ID as SQLITE_DELETE,
INSERT_ROW as SQLITE_INSERT,
GET_VALUE_DETAIL as SQLITE_GET_DETAIL,
} from '../../providers/sqlite3/queries/articleattributevalue.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function setByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
const insertSql = isSqlite ? SQLITE_INSERT : MSSQL_INSERT;
const getDetailSql = isSqlite ? SQLITE_GET_DETAIL : MSSQL_GET_DETAIL;
/**
* Replaces all attribute values for an article by its SKU.
* Validates company ownership and enforces the `exclusive` flag on attributes.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {Array<number|{attributeValueId: number, sort?: number}>} values - Attribute value IDs or items.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, attributeId: number, attributeName: string, exclusive: boolean, attributeValueId: number, value: string, sort: number}>>}
*/
return async (googleAuthId, sku, values = [], tx) => {
if (!Array.isArray(values)) {
throw new Error('Invalid attribute values array');
}
const runSet = async (runner) => {
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
const validated = [];
const seenValues = new Set();
const exclusiveAttributesSeen = new Map(); // attributeId -> attributeName
for (let i = 0; i < values.length; i++) {
const item = values[i];
const valId = typeof item === 'object' && item !== null ? Number(item.attributeValueId) : Number(item);
if (!Number.isInteger(valId) || valId <= 0) {
throw new Error('Invalid attributeValueId');
}
if (seenValues.has(valId)) {
throw new Error('Duplicate attributeValueId');
}
seenValues.add(valId);
const detailRes = await runner.query(getDetailSql, { attributeValueId: valId });
const detail = detailRes.rows[0];
if (!detail || detail.companyId !== companyId) {
throw new Error(`Attribute value not found: ${valId}`);
}
const isExclusive = Boolean(detail.exclusive);
if (isExclusive) {
if (exclusiveAttributesSeen.has(detail.attributeId)) {
throw new Error(`Attribute "${detail.attributeName}" is exclusive and can only have one value`);
}
exclusiveAttributesSeen.set(detail.attributeId, detail.attributeName);
}
const sort = typeof item === 'object' && item !== null && item.sort != null && Number.isInteger(item.sort)
? item.sort
: i;
validated.push({
attributeId: detail.attributeId,
attributeName: detail.attributeName,
exclusive: isExclusive,
attributeValueId: valId,
value: detail.value,
sort,
});
}
await runner.query(deleteSql, { articleId });
const inserted = [];
for (const item of validated) {
const res = await runner.query(insertSql, {
articleId,
attributeValueId: item.attributeValueId,
sort: item.sort,
});
const id = res.rows[0]?.id ?? res.insertId;
inserted.push({
id,
articleId,
attributeId: item.attributeId,
attributeName: item.attributeName,
exclusive: item.exclusive,
attributeValueId: item.attributeValueId,
value: item.value,
sort: item.sort,
});
}
return inserted;
};
if (tx) {
return runSet(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runSet);
}
return runSet(makeRunner(provider));
};
}