mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-11 09:53:32 +03:00
c9a5b28669
- code and tests were extracted out to this package - deletes these files - replaces all local requires, and adds it as a dependency
32 lines
969 B
JavaScript
32 lines
969 B
JavaScript
const errors = require('@tryghost/errors');
|
|
const {i18n} = require('../../lib/common');
|
|
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')});
|
|
}
|
|
|
|
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;
|