u
This commit is contained in:
52
src/store/article/setBrandIdBySku.js
Normal file
52
src/store/article/setBrandIdBySku.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import {
|
||||
GET_BRAND_BY_COMPANY_AND_ID,
|
||||
UPDATE_BRAND,
|
||||
} from '../../sql/article.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
|
||||
export default function setBrandIdBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, brandId) => {
|
||||
const { articleId, languageId, companyId } = await resolve(
|
||||
googleAuthId,
|
||||
sku,
|
||||
);
|
||||
|
||||
if (brandId !== null && !Number.isFinite(brandId)) {
|
||||
throw new Error('Brand not found');
|
||||
}
|
||||
|
||||
let resolvedBrandId = null;
|
||||
if (brandId !== null) {
|
||||
const brand = await pool
|
||||
.request()
|
||||
.input('brandId', brandId)
|
||||
.input('companyId', companyId)
|
||||
.query(GET_BRAND_BY_COMPANY_AND_ID);
|
||||
if (!brand.recordset[0]) {
|
||||
throw new Error('Brand not found');
|
||||
}
|
||||
resolvedBrandId = brandId;
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('brandId', resolvedBrandId)
|
||||
.query(UPDATE_BRAND);
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
return article;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user