57 lines
1.9 KiB
JavaScript
57 lines
1.9 KiB
JavaScript
import * as mssqlQueries from '../../providers/mssql/queries/optionslist.js';
|
|
import * as sqliteQueries from '../../providers/sqlite3/queries/optionslist.js';
|
|
import resolveArticleBySku from '../article/resolveArticleBySku.js';
|
|
import makeRunner from '../runner.js';
|
|
|
|
export default function getOptionsByArticleSku(provider) {
|
|
const resolve = resolveArticleBySku(provider);
|
|
const isSqlite = provider?.dialect === 'sqlite3';
|
|
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
|
|
|
/**
|
|
* Returns the options list 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, sku: string, name: string|null, count: number, sort: number, mandated: boolean}>>}
|
|
*/
|
|
return async (googleAuthId, sku, tx) => {
|
|
const run = async (runner) => {
|
|
const { articleId, languageId } = await resolve(googleAuthId, sku, runner);
|
|
|
|
const artRes = await runner.query(
|
|
'SELECT optionslist_id FROM article WHERE id = @articleId',
|
|
{ articleId },
|
|
);
|
|
const optionslistId = artRes.rows[0]?.optionslist_id;
|
|
if (!optionslistId) {
|
|
return [];
|
|
}
|
|
|
|
const itemsRes = await runner.query(queries.GET_OPTIONSLIST_ITEMS, {
|
|
optionslistId,
|
|
languageId,
|
|
});
|
|
|
|
return itemsRes.rows.map((row) => ({
|
|
id: row.id,
|
|
articleId: row.articleId,
|
|
sku: row.sku,
|
|
name: row.name ?? null,
|
|
count: Number(row.count ?? 1),
|
|
sort: Number(row.sort ?? 0),
|
|
mandated: Boolean(row.mandated),
|
|
}));
|
|
};
|
|
|
|
if (tx) {
|
|
return run(makeRunner(provider, tx));
|
|
}
|
|
if (typeof provider?.transaction === 'function') {
|
|
return provider.transaction(run);
|
|
}
|
|
return run(makeRunner(provider));
|
|
};
|
|
}
|