2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const {i18n} = require('../../lib/common');
|
2019-07-17 13:28:16 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const security = require('../../lib/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) {
|
2020-04-30 22:26:12 +03:00
|
|
|
throw new errors.NotFoundError({message: i18n.t('errors.api.invites.inviteNotFound')});
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (invite.get('expires') < Date.now()) {
|
2020-04-30 22:26:12 +03:00
|
|
|
throw new errors.NotFoundError({message: i18n.t('errors.api.invites.inviteExpired')});
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|