import * as db from '../../db/entitycontact.js'; // #region Helpers function requireSelectedCompany(req) { const companyId = req.user.selectedCompanyId; if (companyId == null) { return { error: 'No company selected' }; } return { companyId }; } 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; if (error.message === 'Contact not found') return 404; if (error.message === 'Contact required') return 400; return 500; } // #endregion /** * 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' */ // #region GET /entity/:entityId/contact 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, 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 }); } }); // #endregion // #region POST /entity/:entityId/contact 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, 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 }); } }); // #endregion /** * @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' */ // #region PATCH /entity/:entityId/contact/:id 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, 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 }); } }); // #endregion // #region DELETE /entity/:entityId/contact/:id 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, 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 }); } }); // #endregion }