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;
|
||||
};
|
||||
}
|
||||
74
src/store/article/setCategoryIdsBySku.js
Normal file
74
src/store/article/setCategoryIdsBySku.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import {
|
||||
ADD_CATEGORY,
|
||||
DELETE_CATEGORIES_BY_ARTICLE,
|
||||
} from '../../sql/article.js';
|
||||
import request from '../request.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
|
||||
export default function setCategoryIdsBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const categoryAccess = resolveCategoryAccess(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, categoryIds) => {
|
||||
if (!Array.isArray(categoryIds)) {
|
||||
throw new Error('Invalid categoryIds');
|
||||
}
|
||||
const seen = new Set();
|
||||
const normalized = [];
|
||||
for (const categoryId of categoryIds) {
|
||||
if (!Number.isFinite(categoryId) || seen.has(categoryId)) {
|
||||
throw new Error('Invalid categoryIds');
|
||||
}
|
||||
seen.add(categoryId);
|
||||
normalized.push(categoryId);
|
||||
}
|
||||
|
||||
const { articleId, languageId, companyId } = await resolve(
|
||||
googleAuthId,
|
||||
sku,
|
||||
);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
for (const categoryId of normalized) {
|
||||
// Ensures the category belongs to the same company as the article.
|
||||
const access = await categoryAccess(googleAuthId, categoryId);
|
||||
if (access.companyId !== companyId) {
|
||||
throw new Error('Invalid categoryIds');
|
||||
}
|
||||
}
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.query(DELETE_CATEGORIES_BY_ARTICLE);
|
||||
|
||||
for (const categoryId of normalized) {
|
||||
await request(pool, tx)
|
||||
.input('categoryId', categoryId)
|
||||
.input('articleId', articleId)
|
||||
.query(ADD_CATEGORY);
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
return article;
|
||||
};
|
||||
}
|
||||
65
src/store/article/setEansBySku.js
Normal file
65
src/store/article/setEansBySku.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import {
|
||||
ADD_EAN,
|
||||
DELETE_EANS_BY_ARTICLE,
|
||||
} from '../../sql/article.js';
|
||||
import request from '../request.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
|
||||
export default function setEansBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, eans) => {
|
||||
if (!Array.isArray(eans)) {
|
||||
throw new Error('Invalid eans');
|
||||
}
|
||||
const normalized = [];
|
||||
const seen = new Set();
|
||||
for (const ean of eans) {
|
||||
if (typeof ean !== 'string' || !ean.trim()) {
|
||||
throw new Error('Invalid eans');
|
||||
}
|
||||
const clean = ean.trim();
|
||||
if (seen.has(clean)) {
|
||||
throw new Error('Invalid eans');
|
||||
}
|
||||
seen.add(clean);
|
||||
normalized.push(clean);
|
||||
}
|
||||
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.query(DELETE_EANS_BY_ARTICLE);
|
||||
|
||||
for (const ean of normalized) {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('ean', ean)
|
||||
.query(ADD_EAN);
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
return article;
|
||||
};
|
||||
}
|
||||
33
src/store/article/setWeightBySku.js
Normal file
33
src/store/article/setWeightBySku.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { UPDATE_WEIGHT } from '../../sql/article.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
|
||||
export default function setWeightBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, weight) => {
|
||||
if (!Number.isFinite(weight) || weight < 0) {
|
||||
throw new Error('Invalid weight');
|
||||
}
|
||||
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('weight', weight)
|
||||
.query(UPDATE_WEIGHT);
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
41
src/store/category/rename.js
Normal file
41
src/store/category/rename.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import { UPDATE_BY_CATEGORY_AND_LANGUAGE } from '../../sql/categorytext.js';
|
||||
import resolveCategoryAccess from './resolveCategoryAccess.js';
|
||||
|
||||
function slugify(name) {
|
||||
return name
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/\p{M}/gu, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
export default function rename(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
|
||||
return async ({ googleAuthId, id, name }) => {
|
||||
if (!name?.trim()) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
const cleaned = name.trim();
|
||||
|
||||
const { languageId } = await access(googleAuthId, id);
|
||||
|
||||
const slug = slugify(cleaned);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('categoryId', id)
|
||||
.input('languageId', languageId)
|
||||
.input('name', cleaned)
|
||||
.input('slug', slug)
|
||||
.query(UPDATE_BY_CATEGORY_AND_LANGUAGE);
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
throw new Error('Category not found');
|
||||
}
|
||||
|
||||
return { id, name: cleaned };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user