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

@@ -8,6 +8,7 @@ import companyList from './store/company/list.js';
import categoryGetTreeForCompany from './store/category/getTreeForCompany.js';
import categoryGetCategoryArticles from './store/category/getCategoryArticles.js';
import categoryCreateUnderParent from './store/category/createUnderParent.js';
import categoryRename from './store/category/rename.js';
import categoryDelete from './store/category/delete.js';
import articleDelete from './store/article/delete.js';
import articleDeleteImage from './store/article/deleteImage.js';
@@ -17,6 +18,10 @@ import articleCreateUnderParent from './store/article/createUnderParent.js';
import articleGetBySku from './store/article/getBySku.js';
import articleUpdateBySku from './store/article/updateBySku.js';
import articleSetPricesBySku from './store/article/setPricesBySku.js';
import articleSetEansBySku from './store/article/setEansBySku.js';
import articleSetBrandIdBySku from './store/article/setBrandIdBySku.js';
import articleSetWeightBySku from './store/article/setWeightBySku.js';
import articleSetCategoryIdsBySku from './store/article/setCategoryIdsBySku.js';
import unitList from './store/unit/list.js';
import unitUpsert from './store/unit/upsert.js';
import unitDelete from './store/unit/delete.js';
@@ -67,6 +72,7 @@ export default class LibDashStore {
getTreeForCompany: categoryGetTreeForCompany(pool),
getCategoryArticles: categoryGetCategoryArticles(pool),
createUnderParent: categoryCreateUnderParent(pool),
rename: categoryRename(pool),
delete: categoryDelete(pool),
};
@@ -75,6 +81,10 @@ export default class LibDashStore {
getBySku: articleGetBySku(pool),
updateBySku: articleUpdateBySku(pool),
setPricesBySku: articleSetPricesBySku(pool),
setEansBySku: articleSetEansBySku(pool),
setBrandIdBySku: articleSetBrandIdBySku(pool),
setWeightBySku: articleSetWeightBySku(pool),
setCategoryIdsBySku: articleSetCategoryIdsBySku(pool),
addImage: articleAddImage(pool),
deleteImage: articleDeleteImage(pool),
orderImages: articleOrderImages(pool),

View File

@@ -98,6 +98,18 @@ LEFT JOIN companyuser cu
ON cu.company_id = co.id AND cu.google_auth_id = @googleAuthId
`;
export const UPDATE_WEIGHT = `
UPDATE article
SET weight = @weight
WHERE id = @articleId
`;
export const UPDATE_BRAND = `
UPDATE article
SET brand_id = @brandId
WHERE id = @articleId
`;
export const UPDATE_UNIT = `
UPDATE article
SET unit_id = @unitId
@@ -121,3 +133,29 @@ INSERT INTO article (sku, unit_id, unitamount, weight)
OUTPUT INSERTED.id
VALUES (@sku, @unitId, @unitamount, @weight)
`;
export const DELETE_EANS_BY_ARTICLE = `
DELETE FROM articleean
WHERE article_id = @articleId
`;
export const ADD_EAN = `
INSERT INTO articleean (article_id, ean)
VALUES (@articleId, @ean)
`;
export const DELETE_CATEGORIES_BY_ARTICLE = `
DELETE FROM categoryarticle
WHERE article_id = @articleId
`;
export const ADD_CATEGORY = `
INSERT INTO categoryarticle (category_id, article_id)
VALUES (@categoryId, @articleId)
`;
export const GET_BRAND_BY_COMPANY_AND_ID = `
SELECT id
FROM brand
WHERE id = @brandId AND company_id = @companyId
`;

View File

@@ -2,3 +2,9 @@ export const CREATE = `
INSERT INTO categorytext (category_id, language_id, name, slug)
VALUES (@categoryId, @languageId, @name, @slug)
`;
export const UPDATE_BY_CATEGORY_AND_LANGUAGE = `
UPDATE categorytext
SET name = @name, slug = @slug
WHERE category_id = @categoryId AND language_id = @languageId
`;

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

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

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

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

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