2018-10-12 20:44:06 +03:00
|
|
|
const Promise = require('bluebird');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
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');
|
2018-10-12 20:44:06 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const api = require('./index');
|
2018-12-17 19:45:07 +03:00
|
|
|
const ALLOWED_INCLUDES = [];
|
2018-10-12 20:44:06 +03:00
|
|
|
const UNSAFE_ATTRS = ['role_id'];
|
|
|
|
|
|
|
|
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({
|
|
|
|
message: i18n.t('errors.api.invites.inviteNotFound')
|
2018-10-12 20:44:06 +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({
|
|
|
|
message: i18n.t('errors.api.invites.inviteNotFound')
|
2020-04-13 13:21:47 +03:00
|
|
|
}));
|
|
|
|
});
|
2018-10-12 20:44:06 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'email'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
role_id: {
|
2018-12-10 17:14:33 +03:00
|
|
|
required: true
|
2018-10-12 20:44:06 +03:00
|
|
|
},
|
|
|
|
email: {
|
2018-12-10 17:14:33 +03:00
|
|
|
required: true
|
2018-10-12 20:44:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
});
|
2018-10-12 20:44:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|