"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetFailures = exports.remove = exports.getAll = exports.get = void 0;
const constraints_1 = __importDefault(require("../validation/constraints"));
const errorTypes_1 = __importDefault(require("../utils/errorTypes"));
const MailService = __importStar(require("../services/mailService"));
const Resources = __importStar(require("../utils/resources"));
const strings_1 = require("../utils/strings");
const validator_1 = require("../validation/validator");
const isValidId = validator_1.forConstraint(constraints_1.default.id, false);
async function withValidMailId(req) {
    const id = strings_1.normalizeString(Resources.getData(req).id);
    if (!isValidId(id)) {
        throw { data: 'Invalid mail id.', type: errorTypes_1.default.badRequest };
    }
    return id;
}
async function doGet(req) {
    const id = await withValidMailId(req);
    return await MailService.getMail(id);
}
function get(req, res) {
    doGet(req)
        .then(mail => Resources.success(res, mail))
        .catch(err => Resources.error(res, err));
}
exports.get = get;
async function doGetAll(req) {
    const restParams = await Resources.getValidRestParams('list', null, req);
    return await MailService.getPendingMails(restParams);
}
function getAll(req, res) {
    doGetAll(req)
        .then(({ total, mails }) => {
        res.set('X-Total-Count', total.toString(10));
        return Resources.success(res, mails);
    })
        .catch(err => Resources.error(res, err));
}
exports.getAll = getAll;
async function doRemove(req) {
    const id = await withValidMailId(req);
    await MailService.deleteMail(id);
}
function remove(req, res) {
    doRemove(req)
        .then(() => Resources.success(res, {}))
        .catch(err => Resources.error(res, err));
}
exports.remove = remove;
async function doResetFailures(req) {
    const id = await withValidMailId(req);
    return await MailService.resetFailures(id);
}
function resetFailures(req, res) {
    doResetFailures(req)
        .then(mail => Resources.success(res, mail))
        .catch(err => Resources.error(res, err));
}
exports.resetFailures = resetFailures;
//# sourceMappingURL=mailResource.js.map