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-08-23 20:08:53 +02:00
|
|
|
import { handleJSONWithData, RequestData } from "../utils/resources";
|
|
|
|
import { normalizeString, parseInteger } from "../shared/utils/strings";
|
|
|
|
import { forConstraint } from "../shared/validation/validator";
|
2022-09-14 16:33:43 +02:00
|
|
|
import type { Request, Response } from "express";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { isString, Mail, MailId } from "../types";
|
2022-09-20 19:09:49 +02:00
|
|
|
import { HttpHeader } from "../shared/utils/http";
|
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)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Missing mail id.", type: ErrorTypes.badRequest };
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const id = normalizeString(data.id);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
if (!isValidId(id)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Invalid mail id.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
return parseInteger(id) as MailId;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const get = handleJSONWithData(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
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-08-23 20:08:53 +02:00
|
|
|
async function doGetAll(
|
|
|
|
req: Request
|
|
|
|
): Promise<{ total: number; mails: Mail[] }> {
|
|
|
|
const restParams = await Resources.getValidRestParams("list", null, req);
|
2020-04-10 00:43:15 +02:00
|
|
|
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)
|
2022-08-23 20:08:53 +02:00
|
|
|
.then(({ total, mails }) => {
|
2022-09-20 19:09:49 +02:00
|
|
|
res.set(HttpHeader.X_TOTAL_COUNT, total.toString(10));
|
2020-04-10 00:43:15 +02:00
|
|
|
return Resources.success(res, mails);
|
|
|
|
})
|
2022-08-23 20:08:53 +02:00
|
|
|
.catch((err) => Resources.error(res, err));
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const remove = handleJSONWithData(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
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-08-23 20:08:53 +02:00
|
|
|
export const resetFailures = handleJSONWithData(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
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
|
|
|
});
|