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:
@@ -28,7 +28,7 @@ export default function createUnderParent(pool) {
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const unitId = await ensureUnit(tx);
|
||||
const unitId = await ensureUnit(tx, languageId, companyId);
|
||||
const sku = await allocateSku(companyId, tx);
|
||||
const slug = slugify(name);
|
||||
|
||||
|
||||
@@ -14,7 +14,13 @@ function shapeArticleDetail(row, categoryIds, photos, eans, prices) {
|
||||
slug: row.slug,
|
||||
unitamount: row.unitamount,
|
||||
weight: row.weight,
|
||||
unit: { si: row.unitSi },
|
||||
unit: {
|
||||
si: row.unitSi,
|
||||
basesi: row.unitBasesi,
|
||||
basesifactor: row.unitBasesifactor,
|
||||
symbol: row.unitSymbol ?? null,
|
||||
name: row.unitName ?? null,
|
||||
},
|
||||
brand: row.brandName
|
||||
? {
|
||||
name: row.brandName,
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import {
|
||||
UPDATE_UNIT,
|
||||
UPDATE_UNITAMOUNT,
|
||||
UPDATE_UNIT_AND_AMOUNT,
|
||||
} from '../../sql/article.js';
|
||||
import { UPDATE_BY_ARTICLE_AND_LANGUAGE } from '../../sql/articletext.js';
|
||||
import { GET_BY_SI } from '../../sql/unit.js';
|
||||
import request from '../request.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
@@ -17,37 +23,78 @@ export default function updateBySku(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, { name }) => {
|
||||
if (!name?.trim()) {
|
||||
return async (googleAuthId, sku, patch = {}) => {
|
||||
const hasName = Object.prototype.hasOwnProperty.call(patch, 'name');
|
||||
const hasUnitamount = Object.prototype.hasOwnProperty.call(patch, 'unitamount');
|
||||
const hasUnitSi = Object.prototype.hasOwnProperty.call(patch, 'unitSi');
|
||||
|
||||
if (!hasName && !hasUnitamount && !hasUnitSi) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
if (hasName && !patch.name?.trim()) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
|
||||
if (hasUnitamount && (!Number.isFinite(patch.unitamount) || patch.unitamount <= 0)) {
|
||||
throw new Error('Invalid unitamount');
|
||||
}
|
||||
|
||||
const { articleId, languageId, companyId } = await resolve(googleAuthId, sku);
|
||||
|
||||
let unitId;
|
||||
if (hasUnitSi) {
|
||||
if (!patch.unitSi?.trim()) {
|
||||
throw new Error('Unit not found');
|
||||
}
|
||||
const unit = await pool
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('si', patch.unitSi.trim())
|
||||
.query(GET_BY_SI);
|
||||
unitId = unit.recordset[0]?.id;
|
||||
if (!unitId) {
|
||||
throw new Error('Unit not found');
|
||||
}
|
||||
}
|
||||
|
||||
const slug = slugify(name);
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const update = await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('languageId', languageId)
|
||||
.input('name', name.trim())
|
||||
.input('slug', slug)
|
||||
.query(UPDATE_BY_ARTICLE_AND_LANGUAGE);
|
||||
if (hasName) {
|
||||
const slug = slugify(patch.name);
|
||||
const update = await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('languageId', languageId)
|
||||
.input('name', patch.name.trim())
|
||||
.input('slug', slug)
|
||||
.query(UPDATE_BY_ARTICLE_AND_LANGUAGE);
|
||||
|
||||
if (!update.rowsAffected[0]) {
|
||||
throw new Error('Article not found');
|
||||
if (!update.rowsAffected[0]) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
}
|
||||
|
||||
if (hasUnitSi && hasUnitamount) {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('unitId', unitId)
|
||||
.input('unitamount', patch.unitamount)
|
||||
.query(UPDATE_UNIT_AND_AMOUNT);
|
||||
} else if (hasUnitSi) {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('unitId', unitId)
|
||||
.query(UPDATE_UNIT);
|
||||
} else if (hasUnitamount) {
|
||||
await request(pool, tx)
|
||||
.input('articleId', articleId)
|
||||
.input('unitamount', patch.unitamount)
|
||||
.query(UPDATE_UNITAMOUNT);
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
return article;
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
@@ -56,5 +103,12 @@ export default function updateBySku(pool) {
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
return article;
|
||||
};
|
||||
}
|
||||
|
||||
31
src/store/unit/delete.js
Normal file
31
src/store/unit/delete.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { DELETE_BY_COMPANY_AND_SI } from '../../sql/unit.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
|
||||
export default function del(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, si) => {
|
||||
if (!si?.trim()) {
|
||||
throw new Error('Unit not found');
|
||||
}
|
||||
|
||||
await access(googleAuthId, companyId);
|
||||
|
||||
try {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('si', si.trim())
|
||||
.query(DELETE_BY_COMPANY_AND_SI);
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
throw new Error('Unit not found');
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.number === 547) {
|
||||
throw new Error('Unit in use');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,24 +1,49 @@
|
||||
import { CREATE, GET_BY_SI } from '../../sql/unit.js';
|
||||
import {
|
||||
CREATE as CREATE_UNITTEXT,
|
||||
GET_BY_UNIT_AND_LANGUAGE,
|
||||
} from '../../sql/unittext.js';
|
||||
import request from '../request.js';
|
||||
|
||||
const DEFAULT_SI = 'pc';
|
||||
const DEFAULT_SYMBOL = 'pc';
|
||||
const DEFAULT_NAME = 'Piece';
|
||||
|
||||
export default function ensureDefaultUnit(pool) {
|
||||
return async (tx) => {
|
||||
return async (tx, languageId, companyId) => {
|
||||
const existing = await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('si', DEFAULT_SI)
|
||||
.query(GET_BY_SI);
|
||||
|
||||
if (existing.recordset[0]) {
|
||||
return existing.recordset[0].id;
|
||||
let unitId = existing.recordset[0]?.id;
|
||||
|
||||
if (!unitId) {
|
||||
const created = await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('si', DEFAULT_SI)
|
||||
.input('basesi', DEFAULT_SI)
|
||||
.input('basesifactor', 1)
|
||||
.query(CREATE);
|
||||
unitId = created.recordset[0].id;
|
||||
}
|
||||
|
||||
const created = await request(pool, tx)
|
||||
.input('si', DEFAULT_SI)
|
||||
.input('basesi', DEFAULT_SI)
|
||||
.input('basesifactor', 1)
|
||||
.query(CREATE);
|
||||
if (languageId != null) {
|
||||
const text = await request(pool, tx)
|
||||
.input('unitId', unitId)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_BY_UNIT_AND_LANGUAGE);
|
||||
|
||||
return created.recordset[0].id;
|
||||
if (!text.recordset[0]) {
|
||||
await request(pool, tx)
|
||||
.input('unitId', unitId)
|
||||
.input('languageId', languageId)
|
||||
.input('symbol', DEFAULT_SYMBOL)
|
||||
.input('name', DEFAULT_NAME)
|
||||
.query(CREATE_UNITTEXT);
|
||||
}
|
||||
}
|
||||
|
||||
return unitId;
|
||||
};
|
||||
}
|
||||
|
||||
19
src/store/unit/list.js
Normal file
19
src/store/unit/list.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import { LIST_BY_COMPANY } from '../../sql/unit.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import shapeUnit from './shapeUnit.js';
|
||||
|
||||
export default function list(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId) => {
|
||||
const { languageId } = await access(googleAuthId, companyId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('languageId', languageId)
|
||||
.query(LIST_BY_COMPANY);
|
||||
|
||||
return result.recordset.map(shapeUnit);
|
||||
};
|
||||
}
|
||||
11
src/store/unit/shapeUnit.js
Normal file
11
src/store/unit/shapeUnit.js
Normal file
@@ -0,0 +1,11 @@
|
||||
function shapeUnit(row) {
|
||||
return {
|
||||
si: row.si,
|
||||
basesi: row.basesi,
|
||||
basesifactor: row.basesifactor,
|
||||
symbol: row.symbol ?? null,
|
||||
name: row.name ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export default shapeUnit;
|
||||
115
src/store/unit/upsert.js
Normal file
115
src/store/unit/upsert.js
Normal file
@@ -0,0 +1,115 @@
|
||||
import {
|
||||
CREATE as CREATE_UNIT,
|
||||
GET_BY_SI,
|
||||
GET_FULL_BY_SI,
|
||||
UPDATE_BY_SI,
|
||||
} from '../../sql/unit.js';
|
||||
import {
|
||||
CREATE as CREATE_UNITTEXT,
|
||||
GET_BY_UNIT_AND_LANGUAGE,
|
||||
UPDATE_BY_UNIT_AND_LANGUAGE,
|
||||
} from '../../sql/unittext.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import request from '../request.js';
|
||||
import shapeUnit from './shapeUnit.js';
|
||||
|
||||
function validateUnit({ si, basesi, basesifactor, symbol, name }) {
|
||||
if (!si?.trim() || !basesi?.trim() || !symbol?.trim() || !name?.trim()) {
|
||||
throw new Error('Invalid unit');
|
||||
}
|
||||
if (!Number.isFinite(basesifactor) || basesifactor <= 0) {
|
||||
throw new Error('Invalid unit');
|
||||
}
|
||||
|
||||
return {
|
||||
si: si.trim(),
|
||||
basesi: basesi.trim(),
|
||||
basesifactor,
|
||||
symbol: symbol.trim(),
|
||||
name: name.trim(),
|
||||
};
|
||||
}
|
||||
|
||||
export default function upsert(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, input) => {
|
||||
const unit = validateUnit(input);
|
||||
const { languageId } = await access(googleAuthId, companyId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const existing = await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('si', unit.si)
|
||||
.query(GET_BY_SI);
|
||||
|
||||
let unitId = existing.recordset[0]?.id;
|
||||
|
||||
if (!unitId) {
|
||||
const created = await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('si', unit.si)
|
||||
.input('basesi', unit.basesi)
|
||||
.input('basesifactor', unit.basesifactor)
|
||||
.query(CREATE_UNIT);
|
||||
unitId = created.recordset[0].id;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('unitId', unitId)
|
||||
.input('languageId', languageId)
|
||||
.input('symbol', unit.symbol)
|
||||
.input('name', unit.name)
|
||||
.query(CREATE_UNITTEXT);
|
||||
} else {
|
||||
await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('si', unit.si)
|
||||
.input('basesi', unit.basesi)
|
||||
.input('basesifactor', unit.basesifactor)
|
||||
.query(UPDATE_BY_SI);
|
||||
|
||||
const text = await request(pool, tx)
|
||||
.input('unitId', unitId)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_BY_UNIT_AND_LANGUAGE);
|
||||
|
||||
if (text.recordset[0]) {
|
||||
await request(pool, tx)
|
||||
.input('unitId', unitId)
|
||||
.input('languageId', languageId)
|
||||
.input('symbol', unit.symbol)
|
||||
.input('name', unit.name)
|
||||
.query(UPDATE_BY_UNIT_AND_LANGUAGE);
|
||||
} else {
|
||||
await request(pool, tx)
|
||||
.input('unitId', unitId)
|
||||
.input('languageId', languageId)
|
||||
.input('symbol', unit.symbol)
|
||||
.input('name', unit.name)
|
||||
.query(CREATE_UNITTEXT);
|
||||
}
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('si', unit.si)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_FULL_BY_SI);
|
||||
|
||||
return shapeUnit(result.recordset[0]);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user