2019-08-09 17:11:24 +03:00
|
|
|
const Promise = require('bluebird');
|
2021-10-06 11:49:20 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-01-14 09:42:38 +03:00
|
|
|
const invites = require('../../services/invites');
|
2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const api = require('./index');
|
|
|
|
const ALLOWED_INCLUDES = [];
|
|
|
|
const UNSAFE_ATTRS = ['role_id'];
|
|
|
|
|
2021-10-06 11:49:20 +03:00
|
|
|
const messages = {
|
|
|
|
inviteNotFound: 'Invite not found.'
|
|
|
|
};
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'invites',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'page',
|
|
|
|
'limit',
|
|
|
|
'fields',
|
|
|
|
'filter',
|
|
|
|
'order',
|
|
|
|
'debug'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Invite.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'include'
|
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'email'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Invite.findOne(frame.data, frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-10-06 11:49:20 +03:00
|
|
|
message: tpl(messages.inviteNotFound)
|
2019-08-09 17:11:24 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: ALLOWED_INCLUDES
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
frame.options.require = true;
|
|
|
|
|
|
|
|
return models.Invite.destroy(frame.options)
|
2020-04-13 13:21:47 +03:00
|
|
|
.then(() => null)
|
|
|
|
.catch(models.Invite.NotFoundError, () => {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-10-06 11:49:20 +03:00
|
|
|
message: tpl(messages.inviteNotFound)
|
2020-04-13 13:21:47 +03:00
|
|
|
}));
|
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'email'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
role_id: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
email: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
|
|
|
query(frame) {
|
2021-01-14 09:25:16 +03:00
|
|
|
return invites.add({
|
|
|
|
api,
|
|
|
|
InviteModel: models.Invite,
|
|
|
|
invites: frame.data.invites,
|
|
|
|
options: frame.options,
|
|
|
|
user: {
|
|
|
|
name: frame.user.get('name'),
|
|
|
|
email: frame.user.get('email')
|
|
|
|
}
|
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|