u
This commit is contained in:
13
README.md
13
README.md
@@ -135,6 +135,18 @@ Backend mapping:
|
||||
|
||||
---
|
||||
|
||||
## `article`
|
||||
|
||||
| Method | Signature | Returns |
|
||||
| --- | --- | --- |
|
||||
| `createUnderParent` | `({ googleAuthId, parentId, name })` | `{ id, parentId, name, sku }` |
|
||||
|
||||
`parentId` is the category id. Creates `article` + `articletext` (company default language) + `categoryarticle` in one transaction. Allocates `sku` via `getnextnumberwithprefixandpadding` (`type` `sku`; creates `numbers` row with empty prefix if missing). Default unit `pc` is ensured if missing (`unitamount` 1, `weight` 0).
|
||||
|
||||
Auth via `resolveCategoryAccess(googleAuthId, parentId)` — `Parent not found` / `Not authorized`.
|
||||
|
||||
---
|
||||
|
||||
## Backend route cheatsheet
|
||||
|
||||
| Route | Store call |
|
||||
@@ -147,6 +159,7 @@ Backend mapping:
|
||||
| `GET /api/category/tree?companyId=` | `category.getTreeForCompany(googleAuthId, companyId)` |
|
||||
| `POST /api/category` | `category.createUnderParent({ googleAuthId, parentId, name })` |
|
||||
| `DELETE /api/category/:id` | `category.delete({ googleAuthId, id })` |
|
||||
| `POST /api/article` | `article.createUnderParent({ googleAuthId, parentId, name })` |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -101,6 +101,16 @@ const MENU = {
|
||||
{ value: 'category.getCategoryArticles', label: 'getCategoryArticles', hint: 'googleAuthId, categoryId → rows' },
|
||||
],
|
||||
},
|
||||
article: {
|
||||
label: 'article',
|
||||
items: [
|
||||
{
|
||||
value: 'article.createUnderParent',
|
||||
label: 'createUnderParent',
|
||||
hint: 'googleAuthId, parentId, name → row',
|
||||
},
|
||||
],
|
||||
},
|
||||
companyuser: {
|
||||
label: 'companyuser',
|
||||
items: [
|
||||
@@ -354,6 +364,24 @@ async function runMethod(client, action) {
|
||||
),
|
||||
];
|
||||
}
|
||||
case 'article.createUnderParent': {
|
||||
const googleAuthId = await askInt('google_auth id');
|
||||
if (googleAuthId == null) return;
|
||||
const parentId = await askInt('category id');
|
||||
if (parentId == null) return;
|
||||
const name = await askText('name', {
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (name == null) return;
|
||||
return [
|
||||
'result',
|
||||
await client.article.createUnderParent({
|
||||
googleAuthId: Number(googleAuthId),
|
||||
parentId: Number(parentId),
|
||||
name,
|
||||
}),
|
||||
];
|
||||
}
|
||||
case 'companyuser.add': {
|
||||
const companyId = await askInt('Company id');
|
||||
if (companyId == null) return;
|
||||
|
||||
@@ -9,6 +9,7 @@ import categoryGetTreeForCompany from './store/category/getTreeForCompany.js';
|
||||
import categoryGetCategoryArticles from './store/category/getCategoryArticles.js';
|
||||
import categoryCreateUnderParent from './store/category/createUnderParent.js';
|
||||
import categoryDelete from './store/category/delete.js';
|
||||
import articleCreateUnderParent from './store/article/createUnderParent.js';
|
||||
import langCreate from './store/lang/create.js';
|
||||
import langDelete from './store/lang/delete.js';
|
||||
import langList from './store/lang/list.js';
|
||||
@@ -50,6 +51,10 @@ export default class LibDashStore {
|
||||
delete: categoryDelete(pool),
|
||||
};
|
||||
|
||||
this.article = {
|
||||
createUnderParent: articleCreateUnderParent(pool),
|
||||
};
|
||||
|
||||
this.lang = {
|
||||
create: langCreate(pool),
|
||||
getByIso639: langGetByIso639(pool),
|
||||
|
||||
5
src/sql/article.js
Normal file
5
src/sql/article.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export const CREATE = `
|
||||
INSERT INTO article (sku, unit_id, unitamount, weight)
|
||||
OUTPUT INSERTED.id
|
||||
VALUES (@sku, @unitId, @unitamount, @weight)
|
||||
`;
|
||||
4
src/sql/articletext.js
Normal file
4
src/sql/articletext.js
Normal file
@@ -0,0 +1,4 @@
|
||||
export const CREATE = `
|
||||
INSERT INTO articletext (article_id, language_id, name, slug)
|
||||
VALUES (@articleId, @languageId, @name, @slug)
|
||||
`;
|
||||
4
src/sql/categoryarticle.js
Normal file
4
src/sql/categoryarticle.js
Normal file
@@ -0,0 +1,4 @@
|
||||
export const ADD = `
|
||||
INSERT INTO categoryarticle (category_id, article_id)
|
||||
VALUES (@categoryId, @articleId)
|
||||
`;
|
||||
11
src/sql/numbers.js
Normal file
11
src/sql/numbers.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export const ENSURE_TYPE = `
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM numbers
|
||||
WHERE company_id = @companyId AND [type] = @type
|
||||
)
|
||||
BEGIN
|
||||
INSERT INTO numbers (company_id, [type])
|
||||
VALUES (@companyId, @type);
|
||||
END
|
||||
`;
|
||||
11
src/sql/unit.js
Normal file
11
src/sql/unit.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export const GET_BY_SI = `
|
||||
SELECT id
|
||||
FROM unit
|
||||
WHERE si = @si
|
||||
`;
|
||||
|
||||
export const CREATE = `
|
||||
INSERT INTO unit (si, basesi, basesifactor)
|
||||
OUTPUT INSERTED.id
|
||||
VALUES (@si, @basesi, @basesifactor)
|
||||
`;
|
||||
73
src/store/article/createUnderParent.js
Normal file
73
src/store/article/createUnderParent.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import { CREATE } from '../../sql/article.js';
|
||||
import { ADD as ADD_CATEGORYARTICLE } from '../../sql/categoryarticle.js';
|
||||
import { CREATE as CREATE_ARTICLETEXT } from '../../sql/articletext.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import ensureDefaultUnit from '../unit/ensureDefault.js';
|
||||
import nextSku from '../numbers/nextSku.js';
|
||||
import request from '../request.js';
|
||||
|
||||
function slugify(name) {
|
||||
return name
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/\p{M}/gu, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
export default function createUnderParent(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
const ensureUnit = ensureDefaultUnit(pool);
|
||||
const allocateSku = nextSku(pool);
|
||||
|
||||
return async ({ googleAuthId, parentId, name }) => {
|
||||
const { companyId, languageId } = await access(googleAuthId, parentId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const unitId = await ensureUnit(tx);
|
||||
const sku = await allocateSku(companyId, tx);
|
||||
const slug = slugify(name);
|
||||
|
||||
const article = await request(pool, tx)
|
||||
.input('sku', sku)
|
||||
.input('unitId', unitId)
|
||||
.input('unitamount', 1)
|
||||
.input('weight', 0)
|
||||
.query(CREATE);
|
||||
|
||||
const id = article.recordset[0].id;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('categoryId', parentId)
|
||||
.input('articleId', id)
|
||||
.query(ADD_CATEGORYARTICLE);
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', id)
|
||||
.input('languageId', languageId)
|
||||
.input('name', name)
|
||||
.input('slug', slug)
|
||||
.query(CREATE_ARTICLETEXT);
|
||||
|
||||
await tx.commit();
|
||||
|
||||
return {
|
||||
id,
|
||||
parentId,
|
||||
name,
|
||||
sku,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
22
src/store/numbers/nextSku.js
Normal file
22
src/store/numbers/nextSku.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import sql from 'mssql';
|
||||
import { ENSURE_TYPE } from '../../sql/numbers.js';
|
||||
import request from '../request.js';
|
||||
|
||||
export default function nextSku(pool) {
|
||||
return async (companyId, tx) => {
|
||||
const type = 'sku';
|
||||
|
||||
await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('type', type)
|
||||
.query(ENSURE_TYPE);
|
||||
|
||||
const req = request(pool, tx);
|
||||
req.input('company_id', sql.Int, companyId);
|
||||
req.input('type', sql.NVarChar(100), type);
|
||||
req.output('value', sql.NVarChar(100));
|
||||
|
||||
const result = await req.execute('getnextnumberwithprefixandpadding');
|
||||
return result.output.value;
|
||||
};
|
||||
}
|
||||
24
src/store/unit/ensureDefault.js
Normal file
24
src/store/unit/ensureDefault.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import { CREATE, GET_BY_SI } from '../../sql/unit.js';
|
||||
import request from '../request.js';
|
||||
|
||||
const DEFAULT_SI = 'pc';
|
||||
|
||||
export default function ensureDefaultUnit(pool) {
|
||||
return async (tx) => {
|
||||
const existing = await request(pool, tx)
|
||||
.input('si', DEFAULT_SI)
|
||||
.query(GET_BY_SI);
|
||||
|
||||
if (existing.recordset[0]) {
|
||||
return existing.recordset[0].id;
|
||||
}
|
||||
|
||||
const created = await request(pool, tx)
|
||||
.input('si', DEFAULT_SI)
|
||||
.input('basesi', DEFAULT_SI)
|
||||
.input('basesifactor', 1)
|
||||
.query(CREATE);
|
||||
|
||||
return created.recordset[0].id;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user