ffffng/server/resources/mailResource.js

100 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-06-11 12:08:50 +02:00
'use strict';
const Constraints = require('../validation/constraints')
2018-12-17 22:49:54 +01:00
const ErrorTypes = require('../utils/errorTypes')
const Logger = require('../logger')
const MailService = require('../services/mailService')
const Resources = require('../utils/resources')
const Strings = require('../utils/strings')
const Validator = require('../validation/validator')
const isValidId = Validator.forConstraint(Constraints.id);
function withValidMailId(req, res, callback) {
const id = Strings.normalizeString(Resources.getData(req).id);
if (!isValidId(id)) {
return callback({data: 'Invalid mail id.', type: ErrorTypes.badRequest});
2016-06-11 12:08:50 +02:00
}
2018-12-17 22:49:54 +01:00
callback(null, id);
}
module.exports = {
get (req, res) {
withValidMailId(req, res, function (err, id) {
if (err) {
return Resources.error(res, err);
}
MailService.getMail(id, function (err, mail) {
2016-06-11 12:08:50 +02:00
if (err) {
2018-12-17 22:49:54 +01:00
Logger.tag('mails', 'admin').error('Error getting mail:', err);
return Resources.error(res, {data: 'Internal error.', type: ErrorTypes.internalError});
}
if (!mail) {
return Resources.error(res, {data: 'Mail not found.', type: ErrorTypes.notFound});
2016-06-11 12:08:50 +02:00
}
2018-12-17 22:49:54 +01:00
return Resources.success(res, mail);
});
});
},
getAll (req, res) {
Resources.getValidRestParams('list', null, req, function (err, restParams) {
if (err) {
return Resources.error(res, err);
}
return MailService.getPendingMails(
restParams,
function (err, mails, total) {
2016-06-11 12:08:50 +02:00
if (err) {
2018-12-17 22:49:54 +01:00
Logger.tag('mails', 'admin').error('Could not get pending mails:', err);
2016-06-11 12:08:50 +02:00
return Resources.error(res, {data: 'Internal error.', type: ErrorTypes.internalError});
}
2018-12-17 22:49:54 +01:00
res.set('X-Total-Count', total);
return Resources.success(res, mails);
2016-06-11 12:08:50 +02:00
}
2018-12-17 22:49:54 +01:00
);
});
},
2016-06-11 12:08:50 +02:00
2018-12-17 22:49:54 +01:00
delete (req, res) {
withValidMailId(req, res, function (err, id) {
if (err) {
return Resources.error(res, err);
}
2016-06-11 12:08:50 +02:00
2018-12-17 22:49:54 +01:00
MailService.deleteMail(id, function (err) {
2016-06-11 12:08:50 +02:00
if (err) {
2018-12-17 22:49:54 +01:00
Logger.tag('mails', 'admin').error('Error deleting mail:', err);
return Resources.error(res, {data: 'Internal error.', type: ErrorTypes.internalError});
2016-06-11 12:08:50 +02:00
}
2018-12-17 22:49:54 +01:00
return Resources.success(res);
2016-06-11 12:08:50 +02:00
});
2018-12-17 22:49:54 +01:00
});
},
2016-06-11 12:08:50 +02:00
2018-12-17 22:49:54 +01:00
resetFailures (req, res) {
withValidMailId(req, res, function (err, id) {
if (err) {
return Resources.error(res, err);
}
MailService.resetFailures(id, function (err, mail) {
2016-06-11 12:08:50 +02:00
if (err) {
2018-12-17 22:49:54 +01:00
Logger.tag('mails', 'admin').error('Error resetting failure count:', err);
return Resources.error(res, {data: 'Internal error.', type: ErrorTypes.internalError});
2016-06-11 12:08:50 +02:00
}
2018-12-17 22:49:54 +01:00
return Resources.success(res, mail);
2016-06-11 12:08:50 +02:00
});
2018-12-17 22:49:54 +01:00
});
}
}