214 lines
6.7 KiB
JavaScript
214 lines
6.7 KiB
JavaScript
import * as db from '../../db/account.js';
|
|
|
|
function requireSelectedCompany(req) {
|
|
const companyId = req.user?.selectedCompanyId;
|
|
if (companyId == null) {
|
|
return { error: 'No company selected' };
|
|
}
|
|
return { companyId };
|
|
}
|
|
|
|
function parsePositiveInt(value, field) {
|
|
const id = Number(value);
|
|
if (!Number.isInteger(id) || id <= 0) {
|
|
return { error: `${field} is required` };
|
|
}
|
|
return { id };
|
|
}
|
|
|
|
function accountErrorStatus(error) {
|
|
if (error.message === 'No company selected') return 400;
|
|
if (error.message === 'Not authorized') return 403;
|
|
if (error.message?.endsWith(' not found')) return 404;
|
|
if (error.message?.endsWith(' required')) return 400;
|
|
return 500;
|
|
}
|
|
|
|
/**
|
|
* Registers account routes for the authenticated user's selected company.
|
|
* @param {import('express').Application} app
|
|
* @param {string} [basePath='']
|
|
*/
|
|
export function register(app, basePath = '') {
|
|
/**
|
|
* @openapi
|
|
* /account:
|
|
* get:
|
|
* tags: [Account]
|
|
* summary: List accounts
|
|
* description: Lists payment accounts for the selected company.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* responses:
|
|
* 200:
|
|
* description: List of accounts
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* accounts:
|
|
* type: array
|
|
* items:
|
|
* $ref: '#/components/schemas/Account'
|
|
* 400:
|
|
* description: No company selected
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
* 401:
|
|
* description: Unauthorized
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
app.get(`${basePath}/account`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
try {
|
|
const accounts = await db.list(req.user.id);
|
|
res.status(200).json({ accounts });
|
|
} catch (error) {
|
|
const status = accountErrorStatus(error);
|
|
if (status === 500) console.error('List accounts failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /account:
|
|
* post:
|
|
* tags: [Account]
|
|
* summary: Create account
|
|
* description: Creates a payment account for the selected company.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* required: [type, name]
|
|
* properties:
|
|
* type:
|
|
* type: string
|
|
* name:
|
|
* type: string
|
|
* responses:
|
|
* 201:
|
|
* description: Created account
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* account:
|
|
* $ref: '#/components/schemas/Account'
|
|
* 400:
|
|
* description: Validation error 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'
|
|
*/
|
|
app.post(`${basePath}/account`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
try {
|
|
const account = await db.create(req.user.id, req.body ?? {});
|
|
res.status(201).json({ account });
|
|
} catch (error) {
|
|
const status = accountErrorStatus(error);
|
|
if (status === 500) console.error('Create account failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @openapi
|
|
* /account/{id}:
|
|
* delete:
|
|
* tags: [Account]
|
|
* summary: Delete account
|
|
* description: Deletes an account of the selected company. Its payments are removed as well.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Account ID
|
|
* responses:
|
|
* 204:
|
|
* description: Account deleted
|
|
* 400:
|
|
* description: 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: Account not found
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
app.delete(`${basePath}/account/:id`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const parsedId = parsePositiveInt(req.params.id, 'id');
|
|
if (parsedId.error) {
|
|
return res.status(400).json({ error: parsedId.error });
|
|
}
|
|
|
|
try {
|
|
await db.remove(req.user.id, parsedId.id);
|
|
res.status(204).end();
|
|
} catch (error) {
|
|
const status = accountErrorStatus(error);
|
|
if (status === 500) console.error('Delete account failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
}
|