This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,17 +1,27 @@
import { CREATE } from '../../providers/mssql/queries/article.js';
import { ADD as ADD_CATEGORYARTICLE } from '../../providers/mssql/queries/categoryarticle.js';
import { CREATE as CREATE_ARTICLETEXT } from '../../providers/mssql/queries/articletext.js';
import { INSERT as INSERT_HISTORY } from '../../providers/mssql/queries/articlehistory.js';
import { CREATE as MSSQL_CREATE_ARTICLE } from '../../providers/mssql/queries/article.js';
import { CREATE as SQLITE_CREATE_ARTICLE } from '../../providers/sqlite3/queries/article.js';
import { ADD as MSSQL_ADD_CATEGORYARTICLE } from '../../providers/mssql/queries/categoryarticle.js';
import { ADD as SQLITE_ADD_CATEGORYARTICLE } from '../../providers/sqlite3/queries/categoryarticle.js';
import { CREATE as MSSQL_CREATE_ARTICLETEXT } from '../../providers/mssql/queries/articletext.js';
import { CREATE as SQLITE_CREATE_ARTICLETEXT } from '../../providers/sqlite3/queries/articletext.js';
import { INSERT as MSSQL_INSERT_HISTORY } from '../../providers/mssql/queries/articlehistory.js';
import { INSERT as SQLITE_INSERT_HISTORY } from '../../providers/sqlite3/queries/articlehistory.js';
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
import ensureDefaultUnit from '../unit/ensureDefault.js';
import nextSku from '../numbers/nextSku.js';
import request from '../request.js';
import makeRunner from '../runner.js';
import slugify from '../slugify.js';
export default function createUnderParent(pool) {
const access = resolveCategoryAccess(pool);
const ensureUnit = ensureDefaultUnit(pool);
const allocateSku = nextSku(pool);
export default function createUnderParent(provider) {
const access = resolveCategoryAccess(provider);
const ensureUnit = ensureDefaultUnit(provider);
const allocateSku = nextSku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const createArticleSql = isSqlite ? SQLITE_CREATE_ARTICLE : MSSQL_CREATE_ARTICLE;
const addCategoryArticleSql = isSqlite ? SQLITE_ADD_CATEGORYARTICLE : MSSQL_ADD_CATEGORYARTICLE;
const createArticleTextSql = isSqlite ? SQLITE_CREATE_ARTICLETEXT : MSSQL_CREATE_ARTICLETEXT;
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
/**
* Creates a new article under a given parent category, allocating a SKU and
@@ -21,54 +31,51 @@ export default function createUnderParent(pool) {
* @param {number} input.googleAuthId - The id of the Google auth user.
* @param {number} input.parentId - The id of the parent category.
* @param {string} input.name - The article name.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, parentId: number, name: string, sku: string}>} The created article.
*
* @example
* const article = await store.article.createUnderParent({ googleAuthId: 42, parentId: 5, name: 'Cola' });
* // => { id, parentId, name, sku }
*/
return async ({ googleAuthId, parentId, name }) => {
const { companyId, languageId, companyuserId } = await access(
googleAuthId,
parentId
);
return async ({ googleAuthId, parentId, name }, tx) => {
const runCreate = async (runner) => {
const { companyId, languageId, companyuserId } = await access(
googleAuthId,
parentId,
runner,
);
const tx = pool.transaction();
await tx.begin();
try {
const unitId = await ensureUnit(tx, languageId, companyId);
const sku = await allocateSku(companyId, tx);
const unitId = await ensureUnit(runner, languageId, companyId);
const sku = await allocateSku(companyId, runner);
const slug = slugify(name);
const article = await request(pool, tx)
.input('sku', sku)
.input('unitId', unitId)
.input('unitamount', 1)
.input('weight', 0)
.query(CREATE);
const article = await runner.query(createArticleSql, {
sku,
unitId,
unitamount: 1,
weight: 0,
});
const id = article.recordset[0].id;
const id = article.rows[0]?.id ?? article.insertId;
await request(pool, tx)
.input('categoryId', parentId)
.input('articleId', id)
.query(ADD_CATEGORYARTICLE);
await runner.query(addCategoryArticleSql, {
categoryId: parentId,
articleId: id,
});
await request(pool, tx)
.input('articleId', id)
.input('languageId', languageId)
.input('name', name)
.input('slug', slug)
.query(CREATE_ARTICLETEXT);
await runner.query(createArticleTextSql, {
articleId: id,
languageId,
name,
slug,
});
await request(pool, tx)
.input('articleId', id)
.input('companyuserId', companyuserId)
.input('text', `Artikel "${name}" angelegt`)
.query(INSERT_HISTORY);
await tx.commit();
await runner.query(insertHistorySql, {
articleId: id,
companyuserId,
text: `Artikel "${name}" angelegt`,
});
return {
id,
@@ -76,13 +83,15 @@ export default function createUnderParent(pool) {
name,
sku,
};
} 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));
};
}