sqlite
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
import {
|
||||
GET_ID_BY_SKU,
|
||||
SET_FATHER_SORT,
|
||||
SET_IS_FATHER,
|
||||
UPDATE_FATHER_BY_SKU,
|
||||
} from '../../providers/mssql/queries/article.js';
|
||||
import { GET_CHILD_COUNT } from '../../providers/mssql/queries/articleDelete.js';
|
||||
import request from '../request.js';
|
||||
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 queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function setFatherBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
export default function setFatherBySku(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const load = queryBySku(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const queries = isSqlite ? sqliteArticle : mssqlArticle;
|
||||
const getChildCountSql = isSqlite ? SQLITE_GET_CHILD_COUNT : MSSQL_GET_CHILD_COUNT;
|
||||
|
||||
/**
|
||||
* Links an article to a father article (V/K compound) by their SKUs.
|
||||
@@ -19,6 +20,7 @@ export default function setFatherBySku(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The child article SKU.
|
||||
* @param {string|null} fatherSku - The father article SKU, or `null` to unlink.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<object>} The updated child article.
|
||||
* @throws {Error} If the article or father is not found, or the link is invalid.
|
||||
*
|
||||
@@ -26,73 +28,67 @@ export default function setFatherBySku(pool) {
|
||||
* const article = await store.article.setFatherBySku(42, '42001190-1', '42001190');
|
||||
* // => { id, sku, name, fatherSku: '42001190', ... }
|
||||
*/
|
||||
return async (googleAuthId, sku, fatherSku) => {
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
return async (googleAuthId, sku, fatherSku, tx) => {
|
||||
const runSet = async (runner) => {
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku, runner);
|
||||
|
||||
if (fatherSku == null || fatherSku === '') {
|
||||
await request(pool)
|
||||
.input('sku', sku)
|
||||
.input('fatherArticleId', null)
|
||||
.query(UPDATE_FATHER_BY_SKU);
|
||||
const unlinked = await load(articleId, languageId);
|
||||
if (!unlinked) {
|
||||
throw new Error('Article not found');
|
||||
if (fatherSku == null || fatherSku === '') {
|
||||
await runner.query(queries.UPDATE_FATHER_BY_SKU, {
|
||||
sku,
|
||||
fatherArticleId: null,
|
||||
});
|
||||
const unlinked = await load(articleId, languageId, runner);
|
||||
if (!unlinked) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
return unlinked;
|
||||
}
|
||||
return unlinked;
|
||||
}
|
||||
|
||||
const father = await pool
|
||||
.request()
|
||||
.input('sku', fatherSku)
|
||||
.query(GET_ID_BY_SKU);
|
||||
const fatherId = father.recordset[0]?.articleId;
|
||||
if (!fatherId) {
|
||||
throw new Error('Father not found');
|
||||
}
|
||||
if (fatherId === articleId) {
|
||||
throw new Error('Invalid father');
|
||||
}
|
||||
const father = await runner.query(queries.GET_ID_BY_SKU, { sku: fatherSku });
|
||||
const fatherId = father.rows[0]?.articleId;
|
||||
if (!fatherId) {
|
||||
throw new Error('Father not found');
|
||||
}
|
||||
if (fatherId === articleId) {
|
||||
throw new Error('Invalid father');
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
await runner.query(queries.UPDATE_FATHER_BY_SKU, {
|
||||
sku,
|
||||
fatherArticleId: fatherId,
|
||||
});
|
||||
|
||||
try {
|
||||
await request(pool, tx)
|
||||
.input('sku', sku)
|
||||
.input('fatherArticleId', fatherId)
|
||||
.query(UPDATE_FATHER_BY_SKU);
|
||||
await runner.query(queries.SET_IS_FATHER, {
|
||||
articleId: fatherId,
|
||||
});
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', fatherId)
|
||||
.query(SET_IS_FATHER);
|
||||
|
||||
const childCount = await request(pool, tx)
|
||||
.input('articleId', fatherId)
|
||||
.query(GET_CHILD_COUNT);
|
||||
const count = childCount.recordset[0].count;
|
||||
const childCount = await runner.query(getChildCountSql, {
|
||||
articleId: fatherId,
|
||||
});
|
||||
const count = childCount.rows[0].count;
|
||||
|
||||
// The child's sort is its 0-based position among the father's children.
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('fatherArticleId', fatherId)
|
||||
.input('fathersort', count - 1)
|
||||
.query(SET_FATHER_SORT);
|
||||
await runner.query(queries.SET_FATHER_SORT, {
|
||||
articleId,
|
||||
fatherArticleId: fatherId,
|
||||
fathersort: count - 1,
|
||||
});
|
||||
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
const article = await load(articleId, languageId, runner);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
return article;
|
||||
};
|
||||
|
||||
return article;
|
||||
if (tx) {
|
||||
return runSet(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runSet);
|
||||
}
|
||||
return runSet(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user