import type express from 'express'; import { asyncErr, KError } from '../helpers'; import { Account, Alert } from '../models'; import { checkAlert } from '../shared/validators'; import type { IdentifiedRequest, PreloadedRequest } from './routes'; export async function loadAlert( req: IdentifiedRequest, res: express.Response, nextHandler: () => void, alertId: number ) { try { const { id: userId } = req.user; const alert = await Alert.find(userId, alertId); if (!alert) { throw new KError('bank alert not found', 404); } req.preloaded = req.preloaded || {}; req.preloaded.alert = alert; nextHandler(); } catch (err) { asyncErr(res, err, 'when preloading alert'); } } export async function create(req: IdentifiedRequest, res: express.Response) { try { const { id: userId } = req.user; const newAlert = req.body; if ( !newAlert || typeof newAlert.accountId !== 'number' || typeof newAlert.type !== 'string' ) { throw new KError('missing parameters', 400); } const validationError = checkAlert(newAlert); if (validationError) { throw new KError(validationError, 400); } const account = await Account.find(userId, newAlert.accountId); if (!account) { throw new KError('bank account not found', 404); } const alert = await Alert.create(userId, newAlert); res.status(201).json(alert); } catch (err) { asyncErr(res, err, 'when creating an alert'); } } export async function destroy(req: PreloadedRequest, res: express.Response) { try { const { id: userId } = req.user; await Alert.destroy(userId, req.preloaded.alert.id); res.status(204).end(); } catch (err) { asyncErr(res, err, 'when deleting a bank alert'); } } export async function update(req: PreloadedRequest, res: express.Response) { try { const { id: userId } = req.user; const { alert } = req.preloaded; let newAlert = req.body; if (typeof newAlert.type !== 'undefined') { throw new KError("can't update an alert type", 400); } newAlert = Object.assign({}, alert, newAlert); const validationError = checkAlert(newAlert); if (validationError) { throw new KError(validationError, 400); } newAlert = await Alert.update(userId, alert.id, req.body); res.status(200).json(newAlert); } catch (err) { asyncErr(res, err, 'when updating a bank alert'); } }