Refactored invites module into a class

refs #12537
This commit is contained in:
Naz 2021-01-14 19:42:38 +13:00
parent 01855389a5
commit 6d2dfff5b5
4 changed files with 88 additions and 69 deletions

View File

@ -1,7 +1,7 @@
const Promise = require('bluebird');
const {i18n} = require('../../lib/common');
const errors = require('@tryghost/errors');
const invites = require('../../services/invites/invites');
const invites = require('../../services/invites');
const models = require('../../models');
const api = require('./index');
const ALLOWED_INCLUDES = [];

View File

@ -1,7 +1,7 @@
const Promise = require('bluebird');
const {i18n} = require('../../lib/common');
const errors = require('@tryghost/errors');
const invites = require('../../services/invites/invites');
const invites = require('../../services/invites');
const models = require('../../models');
const api = require('./index');
const ALLOWED_INCLUDES = [];

View File

@ -0,0 +1,14 @@
const settingsCache = require('../settings/cache');
const {i18n} = require('../../lib/common');
const mailService = require('../../services/mail');
const logging = require('../../../shared/logging');
const urlUtils = require('../../../shared/url-utils');
const Invites = require('./invites');
module.exports = new Invites({
settingsCache,
i18n,
logging,
mailService,
urlUtils
});

View File

@ -1,77 +1,82 @@
const security = require('@tryghost/security');
const mailService = require('../../services/mail');
const urlUtils = require('../../../shared/url-utils');
const settingsCache = require('../../services/settings/cache');
const logging = require('../../../shared/logging');
const {i18n} = require('../../lib/common');
const add = ({api, InviteModel, invites, options, user}) => {
let invite;
let emailData;
class Invites {
constructor({settingsCache, i18n, logging, mailService, urlUtils}) {
this.settingsCache = settingsCache;
this.i18n = i18n;
this.logging = logging;
this.mailService = mailService;
this.urlUtils = urlUtils;
}
return InviteModel.findOne({email: invites[0].email}, options)
.then((existingInvite) => {
if (!existingInvite) {
return;
}
add({api, InviteModel, invites, options, user}) {
let invite;
let emailData;
return existingInvite.destroy(options);
})
.then(() => {
return InviteModel.add(invites[0], options);
})
.then((createdInvite) => {
invite = createdInvite;
return InviteModel.findOne({email: invites[0].email}, options)
.then((existingInvite) => {
if (!existingInvite) {
return;
}
const adminUrl = urlUtils.urlFor('admin', true);
return existingInvite.destroy(options);
})
.then(() => {
return InviteModel.add(invites[0], options);
})
.then((createdInvite) => {
invite = createdInvite;
emailData = {
blogName: settingsCache.get('title'),
invitedByName: user.name,
invitedByEmail: user.email,
resetLink: urlUtils.urlJoin(adminUrl, 'signup', security.url.encodeBase64(invite.get('token')), '/')
};
const adminUrl = this.urlUtils.urlFor('admin', true);
return mailService.utils.generateContent({data: emailData, template: 'invite-user'});
})
.then((emailContent) => {
const payload = {
mail: [{
message: {
to: invite.get('email'),
subject: i18n.t('common.api.users.mail.invitedByName', {
invitedByName: emailData.invitedByName,
blogName: emailData.blogName
}),
html: emailContent.html,
text: emailContent.text
},
options: {}
}]
};
emailData = {
blogName: this.settingsCache.get('title'),
invitedByName: user.name,
invitedByEmail: user.email,
resetLink: this.urlUtils.urlJoin(adminUrl, 'signup', security.url.encodeBase64(invite.get('token')), '/')
};
return api.mail.send(payload, {context: {internal: true}});
})
.then(() => {
return InviteModel.edit({
status: 'sent'
}, Object.assign({id: invite.id}, options));
})
.then((editedInvite) => {
return editedInvite;
})
.catch((err) => {
if (err && err.errorType === 'EmailError') {
const errorMessage = i18n.t('errors.api.invites.errorSendingEmail.error', {
message: err.message
});
const helpText = i18n.t('errors.api.invites.errorSendingEmail.help');
err.message = `${errorMessage} ${helpText}`;
logging.warn(err.message);
}
return this.mailService.utils.generateContent({data: emailData, template: 'invite-user'});
})
.then((emailContent) => {
const payload = {
mail: [{
message: {
to: invite.get('email'),
subject: this.i18n.t('common.api.users.mail.invitedByName', {
invitedByName: emailData.invitedByName,
blogName: emailData.blogName
}),
html: emailContent.html,
text: emailContent.text
},
options: {}
}]
};
return Promise.reject(err);
});
};
return api.mail.send(payload, {context: {internal: true}});
})
.then(() => {
return InviteModel.edit({
status: 'sent'
}, Object.assign({id: invite.id}, options));
})
.then((editedInvite) => {
return editedInvite;
})
.catch((err) => {
if (err && err.errorType === 'EmailError') {
const errorMessage = this.i18n.t('errors.api.invites.errorSendingEmail.error', {
message: err.message
});
const helpText = this.i18n.t('errors.api.invites.errorSendingEmail.help');
err.message = `${errorMessage} ${helpText}`;
this.logging.warn(err.message);
}
module.exports.add = add;
return Promise.reject(err);
});
}
}
module.exports = Invites;