Add company-scoped unit list/upsert/delete and article unit patches.

Units are isolated per company; article detail exposes full unit shape, and unused units can be deleted safely via FK NO ACTION.
This commit is contained in:
seb
2026-08-01 06:12:24 +02:00
parent 54700a145e
commit 12e83a7ffb
16 changed files with 519 additions and 44 deletions

View File

@@ -98,6 +98,24 @@ LEFT JOIN companyuser cu
ON cu.company_id = co.id AND cu.google_auth_id = @googleAuthId
`;
export const UPDATE_UNIT = `
UPDATE article
SET unit_id = @unitId
WHERE id = @articleId
`;
export const UPDATE_UNITAMOUNT = `
UPDATE article
SET unitamount = @unitamount
WHERE id = @articleId
`;
export const UPDATE_UNIT_AND_AMOUNT = `
UPDATE article
SET unit_id = @unitId, unitamount = @unitamount
WHERE id = @articleId
`;
export const CREATE = `
INSERT INTO article (sku, unit_id, unitamount, weight)
OUTPUT INSERTED.id

View File

@@ -6,6 +6,10 @@ export const ARTICLE_DETAIL_COLUMNS = `
a.unitamount,
a.weight,
u.si AS unitSi,
u.basesi AS unitBasesi,
u.basesifactor AS unitBasesifactor,
ut.symbol AS unitSymbol,
ut.name AS unitName,
b.name AS brandName,
brand_pic.thumbw AS brandThumbW,
brand_pic.thumbh AS brandThumbH,
@@ -42,6 +46,8 @@ FROM article a
LEFT JOIN articletext at
ON at.article_id = a.id AND at.language_id = @languageId
LEFT JOIN unit u ON u.id = a.unit_id
LEFT JOIN unittext ut
ON ut.unit_id = u.id AND ut.language_id = @languageId
LEFT JOIN brand b ON b.id = a.brand_id
OUTER APPLY (
SELECT TOP 1 bp.pic_id
@@ -61,6 +67,8 @@ FROM article a
LEFT JOIN articletext at
ON at.article_id = a.id AND at.language_id = @languageId
LEFT JOIN unit u ON u.id = a.unit_id
LEFT JOIN unittext ut
ON ut.unit_id = u.id AND ut.language_id = @languageId
LEFT JOIN brand b ON b.id = a.brand_id
OUTER APPLY (
SELECT TOP 1 bp.pic_id

View File

@@ -1,11 +1,50 @@
export const UNIT_COLUMNS = `
u.si,
u.basesi,
u.basesifactor,
ut.symbol,
ut.name
`;
export const GET_BY_SI = `
SELECT id
FROM unit
WHERE si = @si
WHERE company_id = @companyId AND si = @si
`;
export const GET_FULL_BY_SI = `
SELECT
u.id,
${UNIT_COLUMNS}
FROM unit u
LEFT JOIN unittext ut
ON ut.unit_id = u.id AND ut.language_id = @languageId
WHERE u.company_id = @companyId AND u.si = @si
`;
export const LIST_BY_COMPANY = `
SELECT
${UNIT_COLUMNS}
FROM unit u
LEFT JOIN unittext ut
ON ut.unit_id = u.id AND ut.language_id = @languageId
WHERE u.company_id = @companyId
ORDER BY ut.symbol, ut.name, u.si
`;
export const CREATE = `
INSERT INTO unit (si, basesi, basesifactor)
INSERT INTO unit (company_id, si, basesi, basesifactor)
OUTPUT INSERTED.id
VALUES (@si, @basesi, @basesifactor)
VALUES (@companyId, @si, @basesi, @basesifactor)
`;
export const UPDATE_BY_SI = `
UPDATE unit
SET basesi = @basesi, basesifactor = @basesifactor
WHERE company_id = @companyId AND si = @si
`;
export const DELETE_BY_COMPANY_AND_SI = `
DELETE FROM unit
WHERE company_id = @companyId AND si = @si
`;

16
src/sql/unittext.js Normal file
View File

@@ -0,0 +1,16 @@
export const CREATE = `
INSERT INTO unittext (unit_id, language_id, symbol, name)
VALUES (@unitId, @languageId, @symbol, @name)
`;
export const UPDATE_BY_UNIT_AND_LANGUAGE = `
UPDATE unittext
SET symbol = @symbol, name = @name
WHERE unit_id = @unitId AND language_id = @languageId
`;
export const GET_BY_UNIT_AND_LANGUAGE = `
SELECT id
FROM unittext
WHERE unit_id = @unitId AND language_id = @languageId
`;