2019-11-06 10:27:46 +03:00
|
|
|
const models = require('../../models');
|
2021-10-04 12:02:27 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-11-22 17:20:32 +03:00
|
|
|
const megaService = require('../../services/mega');
|
2019-11-06 10:27:46 +03:00
|
|
|
|
2021-10-04 12:02:27 +03:00
|
|
|
const messages = {
|
|
|
|
emailNotFound: 'Email not found.',
|
|
|
|
retryNotAllowed: 'Only failed emails can be retried'
|
|
|
|
};
|
|
|
|
|
2019-11-06 10:27:46 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'emails',
|
|
|
|
|
2021-02-21 19:59:55 +03:00
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'limit',
|
|
|
|
'fields',
|
|
|
|
'filter',
|
|
|
|
'order',
|
|
|
|
'page'
|
|
|
|
],
|
|
|
|
permissions: true,
|
|
|
|
async query(frame) {
|
|
|
|
return await models.Email.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-06 10:27:46 +03:00
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'fields'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
fields: ['html', 'plaintext', 'subject']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Email.findOne(frame.data, frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
2021-10-04 12:02:27 +03:00
|
|
|
message: tpl(messages.emailNotFound)
|
2019-11-06 10:27:46 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-25 08:04:58 +03:00
|
|
|
return model;
|
2019-11-06 10:27:46 +03:00
|
|
|
});
|
|
|
|
}
|
2019-11-22 17:20:32 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
retry: {
|
|
|
|
data: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Email.findOne(frame.data, frame.options)
|
|
|
|
.then(async (model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
2021-10-04 12:02:27 +03:00
|
|
|
message: tpl(messages.emailNotFound)
|
2019-11-22 17:20:32 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.get('status') !== 'failed') {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
2021-10-04 12:02:27 +03:00
|
|
|
message: tpl(messages.retryNotAllowed)
|
2019-11-22 17:20:32 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-25 08:04:58 +03:00
|
|
|
return await megaService.mega.retryFailedEmail(model);
|
2019-11-22 17:20:32 +03:00
|
|
|
});
|
|
|
|
}
|
2019-11-06 10:27:46 +03:00
|
|
|
}
|
|
|
|
};
|