u
This commit is contained in:
@@ -2,6 +2,8 @@ import * as mssqlArticle from '../../providers/mssql/queries/article.js';
|
||||
import * as sqliteArticle from '../../providers/sqlite3/queries/article.js';
|
||||
import { GET_CHILD_COUNT as MSSQL_GET_CHILD_COUNT } from '../../providers/mssql/queries/articleDelete.js';
|
||||
import { GET_CHILD_COUNT as SQLITE_GET_CHILD_COUNT } from '../../providers/sqlite3/queries/articleDelete.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 queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
@@ -13,6 +15,7 @@ export default function setFatherBySku(provider) {
|
||||
|
||||
const queries = isSqlite ? sqliteArticle : mssqlArticle;
|
||||
const getChildCountSql = isSqlite ? SQLITE_GET_CHILD_COUNT : MSSQL_GET_CHILD_COUNT;
|
||||
const insertHistorySql = isSqlite ? SQLITE_INSERT_HISTORY : MSSQL_INSERT_HISTORY;
|
||||
|
||||
/**
|
||||
* Links an article to a father article (V/K compound) by their SKUs.
|
||||
@@ -30,14 +33,47 @@ export default function setFatherBySku(provider) {
|
||||
*/
|
||||
return async (googleAuthId, sku, fatherSku, tx) => {
|
||||
const runSet = async (runner) => {
|
||||
const { articleId, languageId, companyId } = await resolve(googleAuthId, sku, runner);
|
||||
const { articleId, languageId, companyId, companyuserId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
const childTextRes = await runner.query(
|
||||
'SELECT name FROM articletext WHERE article_id = @articleId AND language_id = @languageId',
|
||||
{ articleId, languageId },
|
||||
);
|
||||
const childName = childTextRes.rows[0]?.name || sku;
|
||||
|
||||
if (fatherSku == null || fatherSku === '') {
|
||||
const prevFatherRes = await runner.query(
|
||||
'SELECT fatherarticle_id FROM article WHERE id = @articleId',
|
||||
{ articleId },
|
||||
);
|
||||
const prevFatherId = prevFatherRes.rows[0]?.fatherarticle_id;
|
||||
|
||||
await runner.query(queries.UPDATE_FATHER_BY_SKU, {
|
||||
sku,
|
||||
companyId,
|
||||
fatherArticleId: null,
|
||||
});
|
||||
|
||||
if (prevFatherId) {
|
||||
const prevFatherTextRes = await runner.query(
|
||||
'SELECT sku FROM article WHERE id = @prevFatherId',
|
||||
{ prevFatherId },
|
||||
);
|
||||
const prevFatherSku = prevFatherTextRes.rows[0]?.sku || String(prevFatherId);
|
||||
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId,
|
||||
companyuserId,
|
||||
text: `Artikel "${sku}" von Vaterartikel "${prevFatherSku}" getrennt`,
|
||||
});
|
||||
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId: prevFatherId,
|
||||
companyuserId,
|
||||
text: `Variante "${childName}" (${sku}) getrennt`,
|
||||
});
|
||||
}
|
||||
|
||||
const unlinked = await load(articleId, languageId, runner);
|
||||
if (!unlinked) {
|
||||
throw new Error('Article not found');
|
||||
@@ -45,8 +81,12 @@ export default function setFatherBySku(provider) {
|
||||
return unlinked;
|
||||
}
|
||||
|
||||
const father = await runner.query(queries.GET_ID_BY_SKU, { sku: fatherSku, companyId });
|
||||
const fatherId = father.rows[0]?.articleId;
|
||||
const father = await runner.query(
|
||||
'SELECT id, is_father, sku FROM article WHERE sku = @sku AND (@companyId IS NULL OR company_id = @companyId)',
|
||||
{ sku: fatherSku, companyId },
|
||||
);
|
||||
const fatherRow = father.rows[0];
|
||||
const fatherId = fatherRow?.id;
|
||||
if (!fatherId) {
|
||||
throw new Error('Father not found');
|
||||
}
|
||||
@@ -54,22 +94,49 @@ export default function setFatherBySku(provider) {
|
||||
throw new Error('Invalid father');
|
||||
}
|
||||
|
||||
const childCount = await runner.query(getChildCountSql, {
|
||||
articleId: fatherId,
|
||||
});
|
||||
const count = childCount.rows[0].count;
|
||||
const wasFather = Boolean(fatherRow.is_father);
|
||||
|
||||
const existingSortsRes = await runner.query(
|
||||
'SELECT fathersort FROM article WHERE fatherarticle_id = @fatherId ORDER BY fathersort ASC',
|
||||
{ fatherId },
|
||||
);
|
||||
const usedSorts = new Set(existingSortsRes.rows.map((r) => Number(r.fathersort)));
|
||||
let fathersort = 0;
|
||||
while (usedSorts.has(fathersort)) {
|
||||
fathersort++;
|
||||
}
|
||||
|
||||
await runner.query(queries.UPDATE_FATHER_BY_SKU, {
|
||||
sku,
|
||||
companyId,
|
||||
fatherArticleId: fatherId,
|
||||
fathersort: count,
|
||||
fathersort,
|
||||
});
|
||||
|
||||
await runner.query(queries.SET_IS_FATHER, {
|
||||
articleId: fatherId,
|
||||
});
|
||||
|
||||
if (!wasFather) {
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId: fatherId,
|
||||
companyuserId,
|
||||
text: 'Artikel zu Vaterartikel konvertiert',
|
||||
});
|
||||
}
|
||||
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId: fatherId,
|
||||
companyuserId,
|
||||
text: `Variante "${childName}" (${sku}) hinzugefügt`,
|
||||
});
|
||||
|
||||
await runner.query(insertHistorySql, {
|
||||
articleId,
|
||||
companyuserId,
|
||||
text: `Artikel "${sku}" als Variante von "${fatherSku}" zugewiesen`,
|
||||
});
|
||||
|
||||
const article = await load(articleId, languageId, runner);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
|
||||
Reference in New Issue
Block a user