u
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { GET_BY_SKU, GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import { GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
|
||||
export default function getBySku(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku) => {
|
||||
const categoryLookup = await pool
|
||||
@@ -15,23 +17,13 @@ export default function getBySku(pool) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const { languageId, posCustomerGroupId } = await access(
|
||||
googleAuthId,
|
||||
categoryId,
|
||||
);
|
||||
const { languageId } = await access(googleAuthId, categoryId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.input('languageId', languageId)
|
||||
.input('posCustomerGroupId', posCustomerGroupId)
|
||||
.query(GET_BY_SKU);
|
||||
|
||||
const row = result.recordset[0];
|
||||
if (!row) {
|
||||
const article = await load(sku, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
return row;
|
||||
return article;
|
||||
};
|
||||
}
|
||||
|
||||
66
src/store/article/queryBySku.js
Normal file
66
src/store/article/queryBySku.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
GET_CATEGORY_IDS_BY_ARTICLE_ID,
|
||||
GET_DETAIL_BY_SKU,
|
||||
GET_EANS_BY_ARTICLE_ID,
|
||||
GET_PHOTOS_BY_ARTICLE_ID,
|
||||
GET_PRICES_BY_ARTICLE_ID,
|
||||
} from '../../sql/articleDetail.js';
|
||||
|
||||
function shapeArticleDetail(row, categoryIds, photos, eans, prices) {
|
||||
return {
|
||||
id: row.id,
|
||||
sku: row.sku,
|
||||
name: row.name,
|
||||
slug: row.slug,
|
||||
unitamount: row.unitamount,
|
||||
weight: row.weight,
|
||||
unit: { si: row.unitSi },
|
||||
brand: row.brandName
|
||||
? {
|
||||
name: row.brandName,
|
||||
thumbW: row.brandThumbW,
|
||||
thumbH: row.brandThumbH,
|
||||
thumbHash: row.brandThumbHash,
|
||||
}
|
||||
: null,
|
||||
categoryIds: categoryIds.map((r) => r.categoryId),
|
||||
photos,
|
||||
eans: eans.map((r) => r.ean),
|
||||
prices,
|
||||
optionsCount: row.optionsCount,
|
||||
articleListCount: row.articleListCount,
|
||||
childCount: row.childCount,
|
||||
fatherArticleId: row.fatherArticleId,
|
||||
fatherSku: row.fatherSku,
|
||||
};
|
||||
}
|
||||
|
||||
export default function queryBySku(pool) {
|
||||
return async (sku, languageId) => {
|
||||
const detail = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_DETAIL_BY_SKU);
|
||||
|
||||
const row = detail.recordset[0];
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [categories, photos, eans, prices] = await Promise.all([
|
||||
pool.request().input('articleId', row.id).query(GET_CATEGORY_IDS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', row.id).query(GET_PHOTOS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', row.id).query(GET_EANS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', row.id).query(GET_PRICES_BY_ARTICLE_ID),
|
||||
]);
|
||||
|
||||
return shapeArticleDetail(
|
||||
row,
|
||||
categories.recordset,
|
||||
photos.recordset,
|
||||
eans.recordset,
|
||||
prices.recordset,
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import { GET_BY_SKU, GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import { GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import { UPDATE_BY_ARTICLE_AND_LANGUAGE } from '../../sql/articletext.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import request from '../request.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
|
||||
function slugify(name) {
|
||||
return name
|
||||
@@ -15,6 +16,7 @@ function slugify(name) {
|
||||
|
||||
export default function updateBySku(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, { name }) => {
|
||||
if (!name?.trim()) {
|
||||
@@ -31,10 +33,7 @@ export default function updateBySku(pool) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const { languageId, posCustomerGroupId } = await access(
|
||||
googleAuthId,
|
||||
row.categoryId,
|
||||
);
|
||||
const { languageId } = await access(googleAuthId, row.categoryId);
|
||||
|
||||
const slug = slugify(name);
|
||||
const tx = pool.transaction();
|
||||
@@ -52,18 +51,13 @@ export default function updateBySku(pool) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const result = await request(pool, tx)
|
||||
.input('sku', sku)
|
||||
.input('languageId', languageId)
|
||||
.input('posCustomerGroupId', posCustomerGroupId)
|
||||
.query(GET_BY_SKU);
|
||||
await tx.commit();
|
||||
|
||||
const article = result.recordset[0];
|
||||
const article = await load(sku, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
return article;
|
||||
} catch (err) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user