Add customergroup and entity HTTP APIs with address and contact routes.
Expose store-backed CRUD for customer groups, entities, nested addresses, and contacts, matching the documented dashdbschema contracts.
This commit is contained in:
314
src/api/routes/customergroup.js
Normal file
314
src/api/routes/customergroup.js
Normal file
@@ -0,0 +1,314 @@
|
||||
import * as db from '../../db/customergroup.js';
|
||||
|
||||
function requireSelectedCompany(req) {
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return { error: 'No company selected' };
|
||||
}
|
||||
return { companyId };
|
||||
}
|
||||
|
||||
function customergroupErrorStatus(error) {
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Company not found') return 404;
|
||||
if (error.message === 'Customer group not found') return 404;
|
||||
if (error.message === 'Name required') return 400;
|
||||
if (error.message === 'Customer group in use') return 409;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers customergroup routes for the authenticated user's selected company.
|
||||
* @param {import('express').Application} app
|
||||
* @param {string} [basePath='']
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /customergroup:
|
||||
* get:
|
||||
* tags: [Customergroup]
|
||||
* summary: List customer groups
|
||||
* description: Lists customer groups for the authenticated user's selected company.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: customergroup/list.md
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Customer group list
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* customergroups:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Customergroup'
|
||||
* 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'
|
||||
* post:
|
||||
* tags: [Customergroup]
|
||||
* summary: Create customer group
|
||||
* description: Creates a customer group for the selected company.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: customergroup/create.md
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [name]
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* example:
|
||||
* name: Kunden
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created customer group
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* customergroup:
|
||||
* $ref: '#/components/schemas/Customergroup'
|
||||
* 400:
|
||||
* description: Name required or 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}/customergroup`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const customergroups = await db.list(req.user.id, selected.companyId);
|
||||
res.status(200).json({ customergroups });
|
||||
} catch (error) {
|
||||
const status = customergroupErrorStatus(error);
|
||||
if (status === 500) console.error('List customergroups failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post(`${basePath}/customergroup`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const { name } = req.body ?? {};
|
||||
|
||||
try {
|
||||
const customergroup = await db.create(req.user.id, selected.companyId, { name });
|
||||
res.status(201).json({ customergroup });
|
||||
} catch (error) {
|
||||
const status = customergroupErrorStatus(error);
|
||||
if (status === 500) console.error('Create customergroup failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /customergroup/{id}:
|
||||
* patch:
|
||||
* tags: [Customergroup]
|
||||
* summary: Update customer group
|
||||
* description: Renames a customer group for the selected company.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: customergroup/update.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [name]
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* example:
|
||||
* name: Stammkunden
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated customer group
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* customergroup:
|
||||
* $ref: '#/components/schemas/Customergroup'
|
||||
* 400:
|
||||
* description: Name required, invalid id, or 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: Customer group or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* delete:
|
||||
* tags: [Customergroup]
|
||||
* summary: Delete customer group
|
||||
* description: |
|
||||
* Deletes the customer group (cascades entities). Fails with
|
||||
* `Customer group in use` for the POS group or other NO ACTION references.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: customergroup/delete.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 204:
|
||||
* description: Deleted
|
||||
* 400:
|
||||
* description: No company selected or invalid id
|
||||
* 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: Customer group not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 409:
|
||||
* description: Customer group in use
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.patch(`${basePath}/customergroup/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
const { name } = req.body ?? {};
|
||||
|
||||
try {
|
||||
const customergroup = await db.update(req.user.id, selected.companyId, id, { name });
|
||||
res.status(200).json({ customergroup });
|
||||
} catch (error) {
|
||||
const status = customergroupErrorStatus(error);
|
||||
if (status === 500) console.error('Update customergroup failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete(`${basePath}/customergroup/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
await db.remove(req.user.id, selected.companyId, id);
|
||||
res.sendStatus(204);
|
||||
} catch (error) {
|
||||
const status = customergroupErrorStatus(error);
|
||||
if (status === 500) console.error('Delete customergroup failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user