diff --git a/README.md b/README.md index 05b7b54..1c5a027 100644 --- a/README.md +++ b/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 })` | --- diff --git a/scripts/test-api.js b/scripts/test-api.js index a7bfefc..97e1a1f 100644 --- a/scripts/test-api.js +++ b/scripts/test-api.js @@ -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; diff --git a/src/index.js b/src/index.js index 973a245..9ba6d24 100644 --- a/src/index.js +++ b/src/index.js @@ -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), diff --git a/src/sql/article.js b/src/sql/article.js new file mode 100644 index 0000000..52a9ade --- /dev/null +++ b/src/sql/article.js @@ -0,0 +1,5 @@ +export const CREATE = ` +INSERT INTO article (sku, unit_id, unitamount, weight) +OUTPUT INSERTED.id +VALUES (@sku, @unitId, @unitamount, @weight) +`; diff --git a/src/sql/articletext.js b/src/sql/articletext.js new file mode 100644 index 0000000..c42540e --- /dev/null +++ b/src/sql/articletext.js @@ -0,0 +1,4 @@ +export const CREATE = ` +INSERT INTO articletext (article_id, language_id, name, slug) +VALUES (@articleId, @languageId, @name, @slug) +`; diff --git a/src/sql/categoryarticle.js b/src/sql/categoryarticle.js new file mode 100644 index 0000000..de1b192 --- /dev/null +++ b/src/sql/categoryarticle.js @@ -0,0 +1,4 @@ +export const ADD = ` +INSERT INTO categoryarticle (category_id, article_id) +VALUES (@categoryId, @articleId) +`; diff --git a/src/sql/numbers.js b/src/sql/numbers.js new file mode 100644 index 0000000..4654302 --- /dev/null +++ b/src/sql/numbers.js @@ -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 +`; diff --git a/src/sql/unit.js b/src/sql/unit.js new file mode 100644 index 0000000..5ff8b46 --- /dev/null +++ b/src/sql/unit.js @@ -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) +`; diff --git a/src/store/article/createUnderParent.js b/src/store/article/createUnderParent.js new file mode 100644 index 0000000..f6acf79 --- /dev/null +++ b/src/store/article/createUnderParent.js @@ -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; + } + }; +} diff --git a/src/store/numbers/nextSku.js b/src/store/numbers/nextSku.js new file mode 100644 index 0000000..eb46582 --- /dev/null +++ b/src/store/numbers/nextSku.js @@ -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; + }; +} diff --git a/src/store/unit/ensureDefault.js b/src/store/unit/ensureDefault.js new file mode 100644 index 0000000..f981bf0 --- /dev/null +++ b/src/store/unit/ensureDefault.js @@ -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; + }; +}