Ghost/core/server/api/canary/email.js
Kevin Ansfield 6a057fad99
Added /emails/:id/retry/ endpoint for retrying failed emails (#11410)
We want to allow admin users to trigger a retry of failed emails without having to go through the unpublish/republish dance.

- fixed resource identifier in email permissions migration so email permissions are added correctly
- added new email permissions migration so that beta releases can be upgraded without rollback (will be a no-op for any non-beta upgrades)
- added `/emails/:id/retry/` canary Admin API endpoint
  - follows same URL pattern as theme activation
  - only triggers mega service retry endpoint if the email has a `'failed'` status
2019-11-22 14:20:32 +00:00

61 lines
1.8 KiB
JavaScript

const models = require('../../models');
const common = require('../../lib/common');
const megaService = require('../../services/mega');
module.exports = {
docName: 'emails',
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) {
throw new common.errors.NotFoundError({
message: common.i18n.t('errors.models.email.emailNotFound')
});
}
return model.toJSON(frame.options);
});
}
},
retry: {
data: [
'id'
],
permissions: true,
query(frame) {
return models.Email.findOne(frame.data, frame.options)
.then(async (model) => {
if (!model) {
throw new common.errors.NotFoundError({
message: common.i18n.t('errors.models.email.emailNotFound')
});
}
if (model.get('status') !== 'failed') {
throw new common.errors.IncorrectUsageError({
message: common.i18n.t('errors.models.email.retryNotAllowed')
});
}
const result = await megaService.mega.retryFailedEmail(model);
return result.toJSON(frame.options);
});
}
}
};