2022-08-04 15:31:01 +02:00
|
|
|
import CONSTRAINTS from "../shared/validation/constraints";
|
2020-04-10 00:43:15 +02:00
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import * as MailService from "../services/mailService";
|
|
|
|
import * as Resources from "../utils/resources";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {handleJSONWithData, RequestData} from "../utils/resources";
|
2022-08-04 18:40:38 +02:00
|
|
|
import {normalizeString, parseInteger} from "../shared/utils/strings";
|
|
|
|
import {forConstraint} from "../shared/validation/validator";
|
2020-04-10 00:43:15 +02:00
|
|
|
import {Request, Response} from "express";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {isString, Mail, MailId} from "../types";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const isValidId = forConstraint(CONSTRAINTS.id, false);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function withValidMailId(data: RequestData): Promise<MailId> {
|
|
|
|
if (!isString(data.id)) {
|
|
|
|
throw {data: 'Missing mail id.', type: ErrorTypes.badRequest};
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = normalizeString(data.id);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
if (!isValidId(id)) {
|
|
|
|
throw {data: 'Invalid mail id.', type: ErrorTypes.badRequest};
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
return parseInteger(id) as MailId;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export const get = handleJSONWithData(async data => {
|
|
|
|
const id = await withValidMailId(data);
|
2020-04-10 00:43:15 +02:00
|
|
|
return await MailService.getMail(id);
|
2022-07-21 18:39:33 +02:00
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function doGetAll(req: Request): Promise<{ total: number, mails: Mail[] }> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const restParams = await Resources.getValidRestParams('list', null, req);
|
|
|
|
return await MailService.getPendingMails(restParams);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export function getAll(req: Request, res: Response): void {
|
2020-04-10 00:43:15 +02:00
|
|
|
doGetAll(req)
|
|
|
|
.then(({total, mails}) => {
|
|
|
|
res.set('X-Total-Count', total.toString(10));
|
|
|
|
return Resources.success(res, mails);
|
|
|
|
})
|
|
|
|
.catch(err => Resources.error(res, err))
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export const remove = handleJSONWithData(async data => {
|
|
|
|
const id = await withValidMailId(data);
|
2020-04-10 00:43:15 +02:00
|
|
|
await MailService.deleteMail(id);
|
2022-07-21 18:39:33 +02:00
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export const resetFailures = handleJSONWithData(async data => {
|
|
|
|
const id = await withValidMailId(data);
|
2020-04-10 00:43:15 +02:00
|
|
|
return await MailService.resetFailures(id);
|
2022-07-21 18:39:33 +02:00
|
|
|
});
|