mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
829e8ed010
- Having these as destructured from the same package is hindering refactoring now - Events should really only ever be used server-side - i18n should be a shared module for now so it can be used everywhere until we figure out something better - Having them seperate also allows us to lint them properly
40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
const errors = require('@tryghost/errors');
|
|
const i18n = require('../../lib/common/i18n');
|
|
const models = require('../../models');
|
|
const security = require('@tryghost/security');
|
|
|
|
async function accept(invitation) {
|
|
const data = invitation.invitation[0];
|
|
const inviteToken = security.url.decodeBase64(data.token);
|
|
const options = {context: {internal: true}};
|
|
|
|
let invite = await models.Invite.findOne({token: inviteToken, status: 'sent'}, options);
|
|
|
|
if (!invite) {
|
|
throw new errors.NotFoundError({message: i18n.t('errors.api.invites.inviteNotFound')});
|
|
}
|
|
|
|
if (invite.get('expires') < Date.now()) {
|
|
throw new errors.NotFoundError({message: i18n.t('errors.api.invites.inviteExpired')});
|
|
}
|
|
|
|
let user = await models.User.findOne({email: data.email});
|
|
if (user) {
|
|
throw new errors.ValidationError({
|
|
message: i18n.t('errors.api.invites.inviteEmailAlreadyExist.message'),
|
|
context: i18n.t('errors.api.invites.inviteEmailAlreadyExist.context'),
|
|
help: i18n.t('errors.api.invites.inviteEmailAlreadyExist.help')
|
|
});
|
|
}
|
|
|
|
await models.User.add({
|
|
email: data.email,
|
|
name: data.name,
|
|
password: data.password,
|
|
roles: [invite.toJSON().role_id]
|
|
}, options);
|
|
|
|
return invite.destroy(options);
|
|
}
|
|
|
|
module.exports = accept;
|