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:
@@ -124,6 +124,77 @@
|
||||
* updatedAt:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* Customergroup:
|
||||
* type: object
|
||||
* required: [id, name]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* name:
|
||||
* type: string
|
||||
* Entity:
|
||||
* type: object
|
||||
* required: [id, customergroupId, firstName, name, createdAt, updatedAt]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* customergroupId:
|
||||
* type: integer
|
||||
* firstName:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* name:
|
||||
* type: string
|
||||
* createdAt:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* updatedAt:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* EntityAddress:
|
||||
* type: object
|
||||
* required: [id, entityId, addressType]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* entityId:
|
||||
* type: integer
|
||||
* firstName:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* name:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* street:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* streetNumber:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* zip:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* city:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* county:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* addressType:
|
||||
* type: string
|
||||
* EntityContact:
|
||||
* type: object
|
||||
* required: [id, entityId, contactType, contact]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* entityId:
|
||||
* type: integer
|
||||
* contactType:
|
||||
* type: string
|
||||
* description: e.g. email, phone
|
||||
* contact:
|
||||
* type: string
|
||||
* DeliverySummary:
|
||||
* type: object
|
||||
* required: [id, number, type, supplierId, date]
|
||||
|
||||
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 });
|
||||
}
|
||||
});
|
||||
}
|
||||
414
src/api/routes/entity.js
Normal file
414
src/api/routes/entity.js
Normal file
@@ -0,0 +1,414 @@
|
||||
import * as db from '../../db/entity.js';
|
||||
|
||||
function requireSelectedCompany(req) {
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return { error: 'No company selected' };
|
||||
}
|
||||
return { companyId };
|
||||
}
|
||||
|
||||
function entityErrorStatus(error) {
|
||||
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;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers entity routes for the authenticated user's selected company.
|
||||
* @param {import('express').Application} app
|
||||
* @param {string} [basePath='']
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /entity:
|
||||
* get:
|
||||
* tags: [Entity]
|
||||
* summary: List entities
|
||||
* description: |
|
||||
* Lists entities for the selected company. Optional `customergroupId`
|
||||
* query filters to one group (must belong to the company).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/list.md
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: customergroupId
|
||||
* required: false
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Entity list
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* entities:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Entity'
|
||||
* 400:
|
||||
* description: No company selected or invalid customergroupId
|
||||
* 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 or customer group not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* post:
|
||||
* tags: [Entity]
|
||||
* summary: Create entity
|
||||
* description: |
|
||||
* Creates an entity in a company customer group and writes `entityhistory` (`Erstellt`).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/create.md
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [customergroupId, name]
|
||||
* properties:
|
||||
* customergroupId:
|
||||
* type: integer
|
||||
* name:
|
||||
* type: string
|
||||
* firstName:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* example:
|
||||
* customergroupId: 1
|
||||
* name: Müller
|
||||
* firstName: Anna
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created entity
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* entity:
|
||||
* $ref: '#/components/schemas/Entity'
|
||||
* 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 or customer group not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/entity`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const options = {};
|
||||
if (Object.prototype.hasOwnProperty.call(req.query, 'customergroupId')) {
|
||||
const customergroupId = Number(req.query.customergroupId);
|
||||
if (!Number.isInteger(customergroupId) || customergroupId <= 0) {
|
||||
return res.status(400).json({ error: 'customergroupId is invalid' });
|
||||
}
|
||||
options.customergroupId = customergroupId;
|
||||
}
|
||||
|
||||
try {
|
||||
const entities = await db.list(req.user.id, selected.companyId, options);
|
||||
res.status(200).json({ entities });
|
||||
} catch (error) {
|
||||
const status = entityErrorStatus(error);
|
||||
if (status === 500) console.error('List entities failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post(`${basePath}/entity`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const { customergroupId, name, firstName } = req.body ?? {};
|
||||
|
||||
try {
|
||||
const entity = await db.create(req.user.id, selected.companyId, {
|
||||
customergroupId,
|
||||
name,
|
||||
firstName,
|
||||
});
|
||||
res.status(201).json({ entity });
|
||||
} catch (error) {
|
||||
const status = entityErrorStatus(error);
|
||||
if (status === 500) console.error('Create entity failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /entity/{id}:
|
||||
* get:
|
||||
* tags: [Entity]
|
||||
* summary: Get entity
|
||||
* description: Returns one entity for the selected company.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/get.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Entity
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* entity:
|
||||
* $ref: '#/components/schemas/Entity'
|
||||
* 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: Entity or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* patch:
|
||||
* tags: [Entity]
|
||||
* summary: Update entity
|
||||
* description: |
|
||||
* Patches `name` and/or `firstName`. Writes `entityhistory` when values change.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/update.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* firstName:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* example:
|
||||
* name: Müller
|
||||
* firstName: Anna
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated entity
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* entity:
|
||||
* $ref: '#/components/schemas/Entity'
|
||||
* 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: Entity or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* delete:
|
||||
* tags: [Entity]
|
||||
* summary: Delete entity
|
||||
* description: Deletes the entity (`entityhistory` cascades; related addresses/contacts cascade).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/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: Entity not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/entity/: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 {
|
||||
const entity = await db.getById(req.user.id, selected.companyId, id);
|
||||
res.status(200).json({ entity });
|
||||
} catch (error) {
|
||||
const status = entityErrorStatus(error);
|
||||
if (status === 500) console.error('Get entity failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.patch(`${basePath}/entity/: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 body = req.body ?? {};
|
||||
const patch = {};
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'name')) {
|
||||
patch.name = body.name;
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'firstName')) {
|
||||
patch.firstName = body.firstName;
|
||||
}
|
||||
|
||||
try {
|
||||
const entity = await db.update(req.user.id, selected.companyId, id, patch);
|
||||
res.status(200).json({ entity });
|
||||
} catch (error) {
|
||||
const status = entityErrorStatus(error);
|
||||
if (status === 500) console.error('Update entity failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete(`${basePath}/entity/: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 = entityErrorStatus(error);
|
||||
if (status === 500) console.error('Delete entity failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
415
src/api/routes/entityaddress.js
Normal file
415
src/api/routes/entityaddress.js
Normal file
@@ -0,0 +1,415 @@
|
||||
import * as db from '../../db/entityaddress.js';
|
||||
|
||||
function requireSelectedCompany(req) {
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return { error: 'No company selected' };
|
||||
}
|
||||
return { companyId };
|
||||
}
|
||||
|
||||
function addressErrorStatus(error) {
|
||||
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 === 'Address not found') return 404;
|
||||
if (error.message === 'Address type required') return 400;
|
||||
return 500;
|
||||
}
|
||||
|
||||
const ADDRESS_FIELDS = [
|
||||
'addressType',
|
||||
'firstName',
|
||||
'name',
|
||||
'street',
|
||||
'streetNumber',
|
||||
'zip',
|
||||
'city',
|
||||
'county',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers entity address routes nested under `/entity/:entityId/address`.
|
||||
* @param {import('express').Application} app
|
||||
* @param {string} [basePath='']
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /entity/{entityId}/address:
|
||||
* get:
|
||||
* tags: [Entity]
|
||||
* summary: List entity addresses
|
||||
* description: Lists addresses for an entity of the selected company.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/address/list.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Address list
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* addresses:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/EntityAddress'
|
||||
* 400:
|
||||
* description: No company selected or invalid entityId
|
||||
* 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: Entity or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* post:
|
||||
* tags: [Entity]
|
||||
* summary: Create entity address
|
||||
* description: Creates an address for the entity. `addressType` is required.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/address/create.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [addressType]
|
||||
* properties:
|
||||
* addressType:
|
||||
* type: string
|
||||
* firstName:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* name:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* street:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* streetNumber:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* zip:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* city:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* county:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* example:
|
||||
* addressType: billing
|
||||
* street: Hauptstraße
|
||||
* streetNumber: '12'
|
||||
* zip: '10115'
|
||||
* city: Berlin
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created address
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* address:
|
||||
* $ref: '#/components/schemas/EntityAddress'
|
||||
* 400:
|
||||
* description: Address type required, invalid entityId, 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: Entity or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/entity/:entityId/address`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const addresses = await db.list(req.user.id, selected.companyId, entityId);
|
||||
res.status(200).json({ addresses });
|
||||
} catch (error) {
|
||||
const status = addressErrorStatus(error);
|
||||
if (status === 500) console.error('List entity addresses failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post(`${basePath}/entity/:entityId/address`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
|
||||
const body = req.body ?? {};
|
||||
const data = {};
|
||||
for (const key of ADDRESS_FIELDS) {
|
||||
if (Object.prototype.hasOwnProperty.call(body, key)) {
|
||||
data[key] = body[key];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const address = await db.create(req.user.id, selected.companyId, entityId, data);
|
||||
res.status(201).json({ address });
|
||||
} catch (error) {
|
||||
const status = addressErrorStatus(error);
|
||||
if (status === 500) console.error('Create entity address failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /entity/{entityId}/address/{id}:
|
||||
* patch:
|
||||
* tags: [Entity]
|
||||
* summary: Update entity address
|
||||
* description: Patches address fields that are present.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/address/update.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* addressType:
|
||||
* type: string
|
||||
* firstName:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* name:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* street:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* streetNumber:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* zip:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* city:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* county:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* example:
|
||||
* city: München
|
||||
* zip: '80331'
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated address
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* address:
|
||||
* $ref: '#/components/schemas/EntityAddress'
|
||||
* 400:
|
||||
* description: Address type required, invalid ids, 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: Address, entity, or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* delete:
|
||||
* tags: [Entity]
|
||||
* summary: Delete entity address
|
||||
* description: Deletes one address of the entity.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/address/delete.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 204:
|
||||
* description: Deleted
|
||||
* 400:
|
||||
* description: No company selected or invalid ids
|
||||
* 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: Address or entity not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.patch(`${basePath}/entity/:entityId/address/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
const body = req.body ?? {};
|
||||
const patch = {};
|
||||
for (const key of ADDRESS_FIELDS) {
|
||||
if (Object.prototype.hasOwnProperty.call(body, key)) {
|
||||
patch[key] = body[key];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const address = await db.update(req.user.id, selected.companyId, entityId, id, patch);
|
||||
res.status(200).json({ address });
|
||||
} catch (error) {
|
||||
const status = addressErrorStatus(error);
|
||||
if (status === 500) console.error('Update entity address failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete(`${basePath}/entity/:entityId/address/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
await db.remove(req.user.id, selected.companyId, entityId, id);
|
||||
res.sendStatus(204);
|
||||
} catch (error) {
|
||||
const status = addressErrorStatus(error);
|
||||
if (status === 500) console.error('Delete entity address failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
361
src/api/routes/entitycontact.js
Normal file
361
src/api/routes/entitycontact.js
Normal file
@@ -0,0 +1,361 @@
|
||||
import * as db from '../../db/entitycontact.js';
|
||||
|
||||
function requireSelectedCompany(req) {
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return { error: 'No company selected' };
|
||||
}
|
||||
return { companyId };
|
||||
}
|
||||
|
||||
function contactErrorStatus(error) {
|
||||
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 === 'Contact not found') return 404;
|
||||
if (error.message === 'Contact required') return 400;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers entity contact routes nested under `/entity/:entityId/contact`.
|
||||
* @param {import('express').Application} app
|
||||
* @param {string} [basePath='']
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /entity/{entityId}/contact:
|
||||
* get:
|
||||
* tags: [Entity]
|
||||
* summary: List entity contacts
|
||||
* description: Lists contacts for an entity of the selected company (`contactType` e.g. `email`, `phone`).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/contact/list.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Contact list
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* contacts:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/EntityContact'
|
||||
* 400:
|
||||
* description: No company selected or invalid entityId
|
||||
* 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: Entity or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* post:
|
||||
* tags: [Entity]
|
||||
* summary: Create entity contact
|
||||
* description: Creates a contact for the entity. `contactType` and `contact` are required.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/contact/create.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [contactType, contact]
|
||||
* properties:
|
||||
* contactType:
|
||||
* type: string
|
||||
* description: e.g. email, phone
|
||||
* contact:
|
||||
* type: string
|
||||
* example:
|
||||
* contactType: email
|
||||
* contact: anna@example.com
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created contact
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* contact:
|
||||
* $ref: '#/components/schemas/EntityContact'
|
||||
* 400:
|
||||
* description: Contact required, invalid entityId, 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: Entity or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/entity/:entityId/contact`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const contacts = await db.list(req.user.id, selected.companyId, entityId);
|
||||
res.status(200).json({ contacts });
|
||||
} catch (error) {
|
||||
const status = contactErrorStatus(error);
|
||||
if (status === 500) console.error('List entity contacts failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post(`${basePath}/entity/:entityId/contact`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
|
||||
const { contactType, contact } = req.body ?? {};
|
||||
|
||||
try {
|
||||
const row = await db.create(req.user.id, selected.companyId, entityId, {
|
||||
contactType,
|
||||
contact,
|
||||
});
|
||||
res.status(201).json({ contact: row });
|
||||
} catch (error) {
|
||||
const status = contactErrorStatus(error);
|
||||
if (status === 500) console.error('Create entity contact failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /entity/{entityId}/contact/{id}:
|
||||
* patch:
|
||||
* tags: [Entity]
|
||||
* summary: Update entity contact
|
||||
* description: Patches `contactType` and/or `contact`.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/contact/update.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* contactType:
|
||||
* type: string
|
||||
* contact:
|
||||
* type: string
|
||||
* example:
|
||||
* contact: anna.mueller@example.com
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated contact
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* contact:
|
||||
* $ref: '#/components/schemas/EntityContact'
|
||||
* 400:
|
||||
* description: Contact required, invalid ids, 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: Contact, entity, or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* delete:
|
||||
* tags: [Entity]
|
||||
* summary: Delete entity contact
|
||||
* description: Deletes one contact of the entity.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: entity/contact/delete.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: entityId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 204:
|
||||
* description: Deleted
|
||||
* 400:
|
||||
* description: No company selected or invalid ids
|
||||
* 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: Contact or entity not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.patch(`${basePath}/entity/:entityId/contact/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
const body = req.body ?? {};
|
||||
const patch = {};
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'contactType')) {
|
||||
patch.contactType = body.contactType;
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'contact')) {
|
||||
patch.contact = body.contact;
|
||||
}
|
||||
|
||||
try {
|
||||
const row = await db.update(req.user.id, selected.companyId, entityId, id, patch);
|
||||
res.status(200).json({ contact: row });
|
||||
} catch (error) {
|
||||
const status = contactErrorStatus(error);
|
||||
if (status === 500) console.error('Update entity contact failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete(`${basePath}/entity/:entityId/contact/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const entityId = Number(req.params.entityId);
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(entityId) || entityId <= 0) {
|
||||
return res.status(400).json({ error: 'entityId is required' });
|
||||
}
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
await db.remove(req.user.id, selected.companyId, entityId, id);
|
||||
res.sendStatus(204);
|
||||
} catch (error) {
|
||||
const status = contactErrorStatus(error);
|
||||
if (status === 500) console.error('Delete entity contact failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
21
src/db/customergroup.js
Normal file
21
src/db/customergroup.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
export async function list(googleAuthId, companyId) {
|
||||
const store = await getStore();
|
||||
return store.customergroup.list(googleAuthId, companyId);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, companyId, data) {
|
||||
const store = await getStore();
|
||||
return store.customergroup.create(googleAuthId, companyId, data);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, companyId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.customergroup.update(googleAuthId, companyId, id, data);
|
||||
}
|
||||
|
||||
export async function remove(googleAuthId, companyId, id) {
|
||||
const store = await getStore();
|
||||
return store.customergroup.delete(googleAuthId, companyId, id);
|
||||
}
|
||||
26
src/db/entity.js
Normal file
26
src/db/entity.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
export async function list(googleAuthId, companyId, options) {
|
||||
const store = await getStore();
|
||||
return store.entity.list(googleAuthId, companyId, options);
|
||||
}
|
||||
|
||||
export async function getById(googleAuthId, companyId, id) {
|
||||
const store = await getStore();
|
||||
return store.entity.getById(googleAuthId, companyId, id);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, companyId, data) {
|
||||
const store = await getStore();
|
||||
return store.entity.create(googleAuthId, companyId, data);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, companyId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.entity.update(googleAuthId, companyId, id, data);
|
||||
}
|
||||
|
||||
export async function remove(googleAuthId, companyId, id) {
|
||||
const store = await getStore();
|
||||
return store.entity.delete(googleAuthId, companyId, id);
|
||||
}
|
||||
21
src/db/entityaddress.js
Normal file
21
src/db/entityaddress.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
export async function list(googleAuthId, companyId, entityId) {
|
||||
const store = await getStore();
|
||||
return store.entityaddress.list(googleAuthId, companyId, entityId);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, companyId, entityId, data) {
|
||||
const store = await getStore();
|
||||
return store.entityaddress.create(googleAuthId, companyId, entityId, data);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, companyId, entityId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.entityaddress.update(googleAuthId, companyId, entityId, id, data);
|
||||
}
|
||||
|
||||
export async function remove(googleAuthId, companyId, entityId, id) {
|
||||
const store = await getStore();
|
||||
return store.entityaddress.delete(googleAuthId, companyId, entityId, id);
|
||||
}
|
||||
21
src/db/entitycontact.js
Normal file
21
src/db/entitycontact.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
export async function list(googleAuthId, companyId, entityId) {
|
||||
const store = await getStore();
|
||||
return store.entitycontact.list(googleAuthId, companyId, entityId);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, companyId, entityId, data) {
|
||||
const store = await getStore();
|
||||
return store.entitycontact.create(googleAuthId, companyId, entityId, data);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, companyId, entityId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.entitycontact.update(googleAuthId, companyId, entityId, id, data);
|
||||
}
|
||||
|
||||
export async function remove(googleAuthId, companyId, entityId, id) {
|
||||
const store = await getStore();
|
||||
return store.entitycontact.delete(googleAuthId, companyId, entityId, id);
|
||||
}
|
||||
@@ -21,6 +21,10 @@ const STORE_NAMESPACES = [
|
||||
'posterminal',
|
||||
'supplier',
|
||||
'delivery',
|
||||
'customergroup',
|
||||
'entity',
|
||||
'entityaddress',
|
||||
'entitycontact',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user