Align store calls with selected-company scoping and add entity numbers.

Drop companyId from company-scoped store wrappers and routes, add GET /entity/next-number plus optional number on entity create/update, and document dashdbschema API.md lookup in sibling-repos.
This commit is contained in:
seb
2026-08-07 06:22:13 +02:00
parent 2767568f5a
commit e6c24654dd
36 changed files with 496 additions and 189 deletions

View File

@@ -148,7 +148,7 @@
* `PUT /customergroup/order`.
* Entity:
* type: object
* required: [id, customergroupId, firstName, name, createdAt, updatedAt]
* required: [id, customergroupId, firstName, name, number, createdAt, updatedAt]
* properties:
* id:
* type: integer
@@ -159,6 +159,10 @@
* nullable: true
* name:
* type: string
* number:
* type: string
* nullable: true
* description: Customer number (null for suppliers)
* createdAt:
* type: string
* format: date-time

View File

@@ -45,6 +45,7 @@ function parseFullHash(value) {
}
function articleErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Parent not found') return 404;
if (error.message === 'Article not found') return 404;
@@ -230,7 +231,7 @@ export function register(app, basePath = '') {
}
try {
const rows = await db.listExport(req.user.id, companyId, categoryId);
const rows = await db.listExport(req.user.id, categoryId);
const csv = buildArtikelCsv(rows);
@@ -1376,7 +1377,7 @@ export function register(app, basePath = '') {
}
try {
const articles = await db.listSearchIndex(req.user.id, companyId);
const articles = await db.listSearchIndex(req.user.id);
res.status(200).json({ articles });
} catch (error) {
const status = articleErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function brandErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Brand not found') return 404;
@@ -134,7 +135,7 @@ export function register(app, basePath = '') {
}
try {
const brands = await db.list(req.user.id, selected.companyId);
const brands = await db.list(req.user.id);
res.status(200).json({ brands });
} catch (error) {
const status = brandErrorStatus(error);
@@ -152,7 +153,7 @@ export function register(app, basePath = '') {
const { name, gsprpinfo } = req.body ?? {};
try {
const brand = await db.upsert(req.user.id, selected.companyId, {
const brand = await db.upsert(req.user.id, {
name,
gsprpinfo,
});
@@ -228,7 +229,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = brandErrorStatus(error);

View File

@@ -23,6 +23,7 @@ function parsePositiveInt(value, field) {
}
function categoryErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Parent not found') return 404;
@@ -103,7 +104,7 @@ export function register(app, basePath = '') {
}
try {
const result = await db.getTreeForCompany(req.user.id, companyId);
const result = await db.getTreeForCompany(req.user.id);
res.status(200).json(result);
} catch (error) {
const status = categoryErrorStatus(error);
@@ -166,7 +167,7 @@ export function register(app, basePath = '') {
}
try {
const { tree } = await db.getTreeForCompany(req.user.id, companyId);
const { tree } = await db.getTreeForCompany(req.user.id);
const csv = buildKategorienCsv(tree, tree.find((n) => n.parentId == null)?.id ?? null);

View File

@@ -12,6 +12,7 @@ function requireSelectedCompany(req) {
}
function customergroupErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Customer group not found') return 404;
@@ -149,7 +150,7 @@ export function register(app, basePath = '') {
}
try {
const rows = await db.list(req.user.id, selected.companyId);
const rows = await db.list(req.user.id);
res.status(200).json({ customergroups: rows.map(withEditable) });
} catch (error) {
const status = customergroupErrorStatus(error);
@@ -167,7 +168,7 @@ export function register(app, basePath = '') {
const { name } = req.body ?? {};
try {
const customergroup = await db.create(req.user.id, selected.companyId, { name });
const customergroup = await db.create(req.user.id, { name });
res.status(201).json({ customergroup });
} catch (error) {
const status = customergroupErrorStatus(error);
@@ -260,7 +261,7 @@ export function register(app, basePath = '') {
}
try {
const result = await db.order(req.user.id, selected.companyId, { customergroupIds });
const result = await db.order(req.user.id, { customergroupIds });
res.status(200).json(result);
} catch (error) {
const status = customergroupErrorStatus(error);
@@ -396,7 +397,7 @@ export function register(app, basePath = '') {
const { name } = req.body ?? {};
try {
const customergroup = await db.update(req.user.id, selected.companyId, id, { name });
const customergroup = await db.update(req.user.id, id, { name });
res.status(200).json({ customergroup });
} catch (error) {
const status = customergroupErrorStatus(error);
@@ -417,7 +418,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = customergroupErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function deliveryErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Delivery not found') return 404;
@@ -154,7 +155,7 @@ export function register(app, basePath = '') {
}
try {
const deliveries = await db.list(req.user.id, selected.companyId);
const deliveries = await db.list(req.user.id);
res.status(200).json({ deliveries });
} catch (error) {
const status = deliveryErrorStatus(error);
@@ -172,7 +173,7 @@ export function register(app, basePath = '') {
const { number, date, supplierId, lines } = req.body ?? {};
try {
const delivery = await db.create(req.user.id, selected.companyId, {
const delivery = await db.create(req.user.id, {
number,
date,
supplierId,
@@ -361,7 +362,7 @@ export function register(app, basePath = '') {
}
try {
const delivery = await db.getById(req.user.id, selected.companyId, id);
const delivery = await db.getById(req.user.id, id);
res.status(200).json({ delivery });
} catch (error) {
const status = deliveryErrorStatus(error);
@@ -397,7 +398,7 @@ export function register(app, basePath = '') {
}
try {
const delivery = await db.update(req.user.id, selected.companyId, id, patch);
const delivery = await db.update(req.user.id, id, patch);
res.status(200).json({ delivery });
} catch (error) {
const status = deliveryErrorStatus(error);
@@ -418,7 +419,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = deliveryErrorStatus(error);

View File

@@ -9,11 +9,15 @@ function requireSelectedCompany(req) {
}
function entityErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Entity not found') return 404;
if (error.message === 'Customer group not found') return 404;
if (error.message === 'Name required') return 400;
if (error.message === 'Number required') return 400;
if (error.message === 'Number already in use') return 409;
if (error.message === 'Invalid role') return 400;
return 500;
}
@@ -31,7 +35,8 @@ export function register(app, basePath = '') {
* summary: List entities
* description: |
* Lists entities for the selected company. Optional `customergroupId`
* query filters to one group (must belong to the company).
* filters to one group. Optional `role=customer` excludes Lieferanten/POS
* groups (for quote/order pickers). `role` wins when both are set.
* security:
* - cookieAuth: []
* x-markdown: entity/list.md
@@ -41,6 +46,12 @@ export function register(app, basePath = '') {
* required: false
* schema:
* type: integer
* - in: query
* name: role
* required: false
* schema:
* type: string
* enum: [customer]
* responses:
* 200:
* description: Entity list
@@ -54,7 +65,7 @@ export function register(app, basePath = '') {
* items:
* $ref: '#/components/schemas/Entity'
* 400:
* description: No company selected or invalid customergroupId
* description: No company selected, invalid customergroupId, or invalid role
* content:
* application/json:
* schema:
@@ -82,6 +93,7 @@ export function register(app, basePath = '') {
* summary: Create entity
* description: |
* Creates an entity in a company customer group and writes `entityhistory` (`Erstellt`).
* Optional `number`: omit to allocate the next customer number (`type` entity).
* security:
* - cookieAuth: []
* x-markdown: entity/create.md
@@ -100,10 +112,14 @@ export function register(app, basePath = '') {
* firstName:
* type: string
* nullable: true
* number:
* type: string
* description: Customer number; omit to auto-allocate
* example:
* customergroupId: 1
* name: Müller
* firstName: Anna
* number: '10042'
* responses:
* 201:
* description: Created entity
@@ -115,7 +131,7 @@ export function register(app, basePath = '') {
* entity:
* $ref: '#/components/schemas/Entity'
* 400:
* description: Name required or no company selected
* description: Name/number required or no company selected
* content:
* application/json:
* schema:
@@ -138,6 +154,12 @@ export function register(app, basePath = '') {
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 409:
* description: Number already in use
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/entity`, async (req, res) => {
const selected = requireSelectedCompany(req);
@@ -153,9 +175,12 @@ export function register(app, basePath = '') {
}
options.customergroupId = customergroupId;
}
if (Object.prototype.hasOwnProperty.call(req.query, 'role')) {
options.role = req.query.role;
}
try {
const entities = await db.list(req.user.id, selected.companyId, options);
const entities = await db.list(req.user.id, options);
res.status(200).json({ entities });
} catch (error) {
const status = entityErrorStatus(error);
@@ -170,14 +195,18 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: selected.error });
}
const { customergroupId, name, firstName } = req.body ?? {};
const body = req.body ?? {};
const data = {
customergroupId: body.customergroupId,
name: body.name,
firstName: body.firstName,
};
if (Object.prototype.hasOwnProperty.call(body, 'number')) {
data.number = body.number;
}
try {
const entity = await db.create(req.user.id, selected.companyId, {
customergroupId,
name,
firstName,
});
const entity = await db.create(req.user.id, data);
res.status(201).json({ entity });
} catch (error) {
const status = entityErrorStatus(error);
@@ -186,6 +215,72 @@ export function register(app, basePath = '') {
}
});
/**
* @openapi
* /entity/next-number:
* get:
* tags: [Entity]
* summary: Peek next customer number
* description: |
* Returns the next customer number that would be allocated (`type` entity)
* without consuming it. Use on the customer add form.
* security:
* - cookieAuth: []
* x-markdown: entity/next-number.md
* responses:
* 200:
* description: Next number
* content:
* application/json:
* schema:
* type: object
* required: [number]
* properties:
* number:
* type: string
* example:
* number: '1'
* 400:
* description: No company selected
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/entity/next-number`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
try {
const number = await db.peekNextNumber(req.user.id);
res.status(200).json({ number });
} catch (error) {
const status = entityErrorStatus(error);
if (status === 500) console.error('Peek next entity number failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /entity/{id}:
@@ -240,7 +335,9 @@ export function register(app, basePath = '') {
* tags: [Entity]
* summary: Update entity
* description: |
* Patches `name` and/or `firstName`. Writes `entityhistory` when values change.
* Patches fields that are present (`name`, `firstName`, `number`).
* `number` is only changed when the key is present (no auto-allocate).
* Writes `entityhistory` when values change.
* security:
* - cookieAuth: []
* x-markdown: entity/update.md
@@ -262,9 +359,13 @@ export function register(app, basePath = '') {
* firstName:
* type: string
* nullable: true
* number:
* type: string
* nullable: true
* example:
* name: Müller
* firstName: Anna
* number: '10042'
* responses:
* 200:
* description: Updated entity
@@ -276,7 +377,7 @@ export function register(app, basePath = '') {
* entity:
* $ref: '#/components/schemas/Entity'
* 400:
* description: Name required, invalid id, or no company selected
* description: Name/number required, invalid id, or no company selected
* content:
* application/json:
* schema:
@@ -299,6 +400,12 @@ export function register(app, basePath = '') {
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 409:
* description: Number already in use
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* delete:
* tags: [Entity]
* summary: Delete entity
@@ -352,7 +459,7 @@ export function register(app, basePath = '') {
}
try {
const entity = await db.getById(req.user.id, selected.companyId, id);
const entity = await db.getById(req.user.id, id);
res.status(200).json({ entity });
} catch (error) {
const status = entityErrorStatus(error);
@@ -380,9 +487,12 @@ export function register(app, basePath = '') {
if (Object.prototype.hasOwnProperty.call(body, 'firstName')) {
patch.firstName = body.firstName;
}
if (Object.prototype.hasOwnProperty.call(body, 'number')) {
patch.number = body.number;
}
try {
const entity = await db.update(req.user.id, selected.companyId, id, patch);
const entity = await db.update(req.user.id, id, patch);
res.status(200).json({ entity });
} catch (error) {
const status = entityErrorStatus(error);
@@ -403,7 +513,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = entityErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function addressErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Entity not found') return 404;
@@ -183,7 +184,7 @@ export function register(app, basePath = '') {
}
try {
const addresses = await db.list(req.user.id, selected.companyId, entityId);
const addresses = await db.list(req.user.id, entityId);
res.status(200).json({ addresses });
} catch (error) {
const status = addressErrorStatus(error);
@@ -212,7 +213,7 @@ export function register(app, basePath = '') {
}
try {
const address = await db.create(req.user.id, selected.companyId, entityId, data);
const address = await db.create(req.user.id, entityId, data);
res.status(201).json({ address });
} catch (error) {
const status = addressErrorStatus(error);
@@ -379,7 +380,7 @@ export function register(app, basePath = '') {
}
try {
const address = await db.update(req.user.id, selected.companyId, entityId, id, patch);
const address = await db.update(req.user.id, entityId, id, patch);
res.status(200).json({ address });
} catch (error) {
const status = addressErrorStatus(error);
@@ -404,7 +405,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, entityId, id);
await db.remove(req.user.id, entityId, id);
res.sendStatus(204);
} catch (error) {
const status = addressErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function contactErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Entity not found') return 404;
@@ -151,7 +152,7 @@ export function register(app, basePath = '') {
}
try {
const contacts = await db.list(req.user.id, selected.companyId, entityId);
const contacts = await db.list(req.user.id, entityId);
res.status(200).json({ contacts });
} catch (error) {
const status = contactErrorStatus(error);
@@ -174,7 +175,7 @@ export function register(app, basePath = '') {
const { contactType, contact } = req.body ?? {};
try {
const row = await db.create(req.user.id, selected.companyId, entityId, {
const row = await db.create(req.user.id, entityId, {
contactType,
contact,
});
@@ -325,7 +326,7 @@ export function register(app, basePath = '') {
}
try {
const row = await db.update(req.user.id, selected.companyId, entityId, id, patch);
const row = await db.update(req.user.id, entityId, id, patch);
res.status(200).json({ contact: row });
} catch (error) {
const status = contactErrorStatus(error);
@@ -350,7 +351,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, entityId, id);
await db.remove(req.user.id, entityId, id);
res.sendStatus(204);
} catch (error) {
const status = contactErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function orderErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Order not found') return 404;
@@ -157,7 +158,7 @@ export function register(app, basePath = '') {
}
try {
const orders = await db.list(req.user.id, selected.companyId);
const orders = await db.list(req.user.id);
res.status(200).json({ orders });
} catch (error) {
const status = orderErrorStatus(error);
@@ -175,7 +176,7 @@ export function register(app, basePath = '') {
const { number, date, entityId, lines } = req.body ?? {};
try {
const order = await db.create(req.user.id, selected.companyId, {
const order = await db.create(req.user.id, {
number,
date,
entityId,
@@ -365,7 +366,7 @@ export function register(app, basePath = '') {
}
try {
const order = await db.getById(req.user.id, selected.companyId, id);
const order = await db.getById(req.user.id, id);
res.status(200).json({ order });
} catch (error) {
const status = orderErrorStatus(error);
@@ -401,7 +402,7 @@ export function register(app, basePath = '') {
}
try {
const order = await db.update(req.user.id, selected.companyId, id, patch);
const order = await db.update(req.user.id, id, patch);
res.status(200).json({ order });
} catch (error) {
const status = orderErrorStatus(error);
@@ -422,7 +423,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = orderErrorStatus(error);

View File

@@ -13,6 +13,7 @@ function parseFullHash(value) {
}
function picErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Image not found') return 404;
return 500;

View File

@@ -18,6 +18,7 @@ function parsePositiveInt(value, field) {
}
function posterminalErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Posterminal not found') return 404;
@@ -86,7 +87,7 @@ export function register(app, basePath = '') {
}
try {
const posterminals = await db.list(req.user.id, selected.companyId);
const posterminals = await db.list(req.user.id);
res.status(200).json({ posterminals });
} catch (error) {
const status = posterminalErrorStatus(error);
@@ -169,7 +170,7 @@ export function register(app, basePath = '') {
const pairingpin = String(randomInt(100000, 999999));
try {
const posterminal = await db.upsert(req.user.id, selected.companyId, {
const posterminal = await db.upsert(req.user.id, {
name: name.trim(),
pairingpin,
});
@@ -237,7 +238,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, parsed.id);
await db.remove(req.user.id, parsed.id);
res.sendStatus(204);
} catch (error) {
const status = posterminalErrorStatus(error);
@@ -379,7 +380,7 @@ export function register(app, basePath = '') {
}
try {
const result = await db.setOn(req.user.id, selected.companyId, terminal.id, category.id);
const result = await db.setOn(req.user.id, terminal.id, category.id);
res.status(200).json(result);
} catch (error) {
const status = posterminalErrorStatus(error);
@@ -405,7 +406,7 @@ export function register(app, basePath = '') {
}
try {
const result = await db.setOff(req.user.id, selected.companyId, terminal.id, category.id);
const result = await db.setOff(req.user.id, terminal.id, category.id);
res.status(200).json(result);
} catch (error) {
const status = posterminalErrorStatus(error);
@@ -534,7 +535,7 @@ export function register(app, basePath = '') {
}
try {
const result = await db.setOnAll(req.user.id, selected.companyId, terminal.id);
const result = await db.setOnAll(req.user.id, terminal.id);
res.status(200).json(result);
} catch (error) {
const status = posterminalErrorStatus(error);
@@ -555,7 +556,7 @@ export function register(app, basePath = '') {
}
try {
const result = await db.setOffAll(req.user.id, selected.companyId, terminal.id);
const result = await db.setOffAll(req.user.id, terminal.id);
res.status(200).json(result);
} catch (error) {
const status = posterminalErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function quoteErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Quote not found') return 404;
@@ -157,7 +158,7 @@ export function register(app, basePath = '') {
}
try {
const quotes = await db.list(req.user.id, selected.companyId);
const quotes = await db.list(req.user.id);
res.status(200).json({ quotes });
} catch (error) {
const status = quoteErrorStatus(error);
@@ -175,7 +176,7 @@ export function register(app, basePath = '') {
const { number, date, entityId, lines } = req.body ?? {};
try {
const quote = await db.create(req.user.id, selected.companyId, {
const quote = await db.create(req.user.id, {
number,
date,
entityId,
@@ -365,7 +366,7 @@ export function register(app, basePath = '') {
}
try {
const quote = await db.getById(req.user.id, selected.companyId, id);
const quote = await db.getById(req.user.id, id);
res.status(200).json({ quote });
} catch (error) {
const status = quoteErrorStatus(error);
@@ -401,7 +402,7 @@ export function register(app, basePath = '') {
}
try {
const quote = await db.update(req.user.id, selected.companyId, id, patch);
const quote = await db.update(req.user.id, id, patch);
res.status(200).json({ quote });
} catch (error) {
const status = quoteErrorStatus(error);
@@ -422,7 +423,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = quoteErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function supplierErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Supplier not found') return 404;
@@ -132,7 +133,7 @@ export function register(app, basePath = '') {
}
try {
const suppliers = await db.list(req.user.id, selected.companyId);
const suppliers = await db.list(req.user.id);
res.status(200).json({ suppliers });
} catch (error) {
const status = supplierErrorStatus(error);
@@ -150,7 +151,7 @@ export function register(app, basePath = '') {
const { name } = req.body ?? {};
try {
const supplier = await db.create(req.user.id, selected.companyId, { name });
const supplier = await db.create(req.user.id, { name });
res.status(201).json({ supplier });
} catch (error) {
const status = supplierErrorStatus(error);
@@ -278,7 +279,7 @@ export function register(app, basePath = '') {
const { name } = req.body ?? {};
try {
const supplier = await db.update(req.user.id, selected.companyId, id, { name });
const supplier = await db.update(req.user.id, id, { name });
res.status(200).json({ supplier });
} catch (error) {
const status = supplierErrorStatus(error);
@@ -299,7 +300,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = supplierErrorStatus(error);

View File

@@ -9,6 +9,7 @@ function requireSelectedCompany(req) {
}
function unitErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Unit not found') return 404;
@@ -190,7 +191,7 @@ export function register(app, basePath = '') {
}
try {
const units = await db.list(req.user.id, selected.companyId);
const units = await db.list(req.user.id);
res.status(200).json({ units });
} catch (error) {
const status = unitErrorStatus(error);
@@ -208,7 +209,7 @@ export function register(app, basePath = '') {
const { si, basesi, basesifactor, symbol, name } = req.body ?? {};
try {
const unit = await db.upsert(req.user.id, selected.companyId, {
const unit = await db.upsert(req.user.id, {
si,
basesi,
basesifactor,
@@ -235,7 +236,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, si);
await db.remove(req.user.id, si);
res.sendStatus(204);
} catch (error) {
const status = unitErrorStatus(error);