This commit is contained in:
seb
2026-08-02 19:01:00 +02:00
parent 787e411547
commit b890843445
10 changed files with 480 additions and 0 deletions

View 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;
};
}