u
This commit is contained in:
@@ -11,6 +11,7 @@ import categoryCreateUnderParent from './store/category/createUnderParent.js';
|
||||
import categoryDelete from './store/category/delete.js';
|
||||
import articleCreateUnderParent from './store/article/createUnderParent.js';
|
||||
import articleGetBySku from './store/article/getBySku.js';
|
||||
import articleUpdateBySku from './store/article/updateBySku.js';
|
||||
import langCreate from './store/lang/create.js';
|
||||
import langDelete from './store/lang/delete.js';
|
||||
import langList from './store/lang/list.js';
|
||||
@@ -55,6 +56,7 @@ export default class LibDashStore {
|
||||
this.article = {
|
||||
createUnderParent: articleCreateUnderParent(pool),
|
||||
getBySku: articleGetBySku(pool),
|
||||
updateBySku: articleUpdateBySku(pool),
|
||||
};
|
||||
|
||||
this.lang = {
|
||||
|
||||
@@ -13,6 +13,7 @@ OUTER APPLY (
|
||||
ORDER BY ap.sort ASC, ap.id ASC
|
||||
) first_pic
|
||||
LEFT JOIN pic ON pic.id = first_pic.pic_id
|
||||
LEFT JOIN article father ON father.id = a.fatherarticle_id
|
||||
`;
|
||||
|
||||
export const ARTICLE_LIST_COLUMNS = `
|
||||
@@ -43,13 +44,15 @@ export const ARTICLE_LIST_COLUMNS = `
|
||||
FROM article child
|
||||
WHERE child.fatherarticle_id = a.id
|
||||
) AS childCount,
|
||||
a.fatherarticle_id AS fatherArticleId,
|
||||
father.sku AS fatherSku,
|
||||
pic.thumbw AS thumbW,
|
||||
pic.thumbh AS thumbH,
|
||||
pic.thumbhash AS thumbHash
|
||||
`;
|
||||
|
||||
export const GET_CATEGORY_ID_BY_SKU = `
|
||||
SELECT TOP 1 ca.category_id AS categoryId
|
||||
SELECT TOP 1 ca.category_id AS categoryId, a.id AS articleId
|
||||
FROM article a
|
||||
INNER JOIN categoryarticle ca ON ca.article_id = a.id
|
||||
WHERE a.sku = @sku
|
||||
|
||||
@@ -2,3 +2,9 @@ export const CREATE = `
|
||||
INSERT INTO articletext (article_id, language_id, name, slug)
|
||||
VALUES (@articleId, @languageId, @name, @slug)
|
||||
`;
|
||||
|
||||
export const UPDATE_BY_ARTICLE_AND_LANGUAGE = `
|
||||
UPDATE articletext
|
||||
SET name = @name, slug = @slug
|
||||
WHERE article_id = @articleId AND language_id = @languageId
|
||||
`;
|
||||
|
||||
77
src/store/article/updateBySku.js
Normal file
77
src/store/article/updateBySku.js
Normal file
@@ -0,0 +1,77 @@
|
||||
import { GET_BY_SKU, 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';
|
||||
|
||||
function slugify(name) {
|
||||
return name
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/\p{M}/gu, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
export default function updateBySku(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
|
||||
return async (googleAuthId, sku, { name }) => {
|
||||
if (!name?.trim()) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
|
||||
const categoryLookup = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.query(GET_CATEGORY_ID_BY_SKU);
|
||||
|
||||
const row = categoryLookup.recordset[0];
|
||||
if (!row?.categoryId || !row?.articleId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const { languageId, posCustomerGroupId } = await access(
|
||||
googleAuthId,
|
||||
row.categoryId,
|
||||
);
|
||||
|
||||
const slug = slugify(name);
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const update = await request(pool, tx)
|
||||
.input('articleId', row.articleId)
|
||||
.input('languageId', languageId)
|
||||
.input('name', name.trim())
|
||||
.input('slug', slug)
|
||||
.query(UPDATE_BY_ARTICLE_AND_LANGUAGE);
|
||||
|
||||
if (!update.rowsAffected[0]) {
|
||||
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);
|
||||
|
||||
const article = result.recordset[0];
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
return article;
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user