2016-11-07 11:47:33 +09:00
import Telescope , { newMutation , editMutation , removeMutation } from 'meteor/nova:lib' ;
2016-11-04 10:28:54 +09:00
import Users from 'meteor/nova:users' ;
2016-11-22 18:14:51 -05:00
const performCheck = ( mutation , user , document ) => {
if ( ! mutation . check ( user , document ) ) throw new Error ( ` Sorry, you don't have the rights to perform the mutation ${ mutation . name } on document _id = ${ document . _id } ` ) ;
} ;
const mutations = {
2016-11-14 17:17:44 +09:00
2016-11-22 18:14:51 -05:00
new : {
name : 'postsNew' ,
check ( user , document ) {
if ( ! user ) return false ;
return Users . canDo ( user , 'posts.new' ) ;
} ,
mutation ( root , { document } , context ) {
performCheck ( this , context . currentUser , document ) ;
return newMutation ( {
collection : context . Posts ,
document : document ,
currentUser : context . currentUser ,
2016-11-24 15:47:51 +09:00
validate : true ,
context ,
2016-11-22 18:14:51 -05:00
} ) ;
} ,
2016-11-14 17:17:44 +09:00
2016-11-07 11:47:33 +09:00
} ,
2016-11-04 15:49:42 +09:00
2016-11-22 18:14:51 -05:00
edit : {
name : 'postsEdit' ,
check ( user , document ) {
if ( ! user || ! document ) return false ;
return Users . owns ( user , document ) ? Users . canDo ( user , 'posts.edit.own' ) : Users . canDo ( user , ` posts.edit.all ` ) ;
} ,
mutation ( root , { documentId , set , unset } , context ) {
2016-11-14 17:17:44 +09:00
2016-11-22 18:14:51 -05:00
const document = context . Posts . findOne ( documentId ) ;
performCheck ( this , context . currentUser , document ) ;
return editMutation ( {
collection : context . Posts ,
documentId : documentId ,
set : set ,
unset : unset ,
currentUser : context . currentUser ,
2016-11-24 15:47:51 +09:00
validate : true ,
context ,
2016-11-22 18:14:51 -05:00
} ) ;
} ,
2016-11-14 17:17:44 +09:00
2016-11-04 14:38:31 +09:00
} ,
2016-11-22 18:14:51 -05:00
remove : {
name : 'postsRemove' ,
check ( user , document ) {
if ( ! user || ! document ) return false ;
return Users . owns ( user , document ) ? Users . canDo ( user , 'posts.remove.own' ) : Users . canDo ( user , ` posts.remove.all ` ) ;
} ,
mutation ( root , { documentId } , context ) {
const document = context . Posts . findOne ( documentId ) ;
performCheck ( this , context . currentUser , document ) ;
return removeMutation ( {
collection : context . Posts ,
documentId : documentId ,
currentUser : context . currentUser ,
2016-11-24 15:47:51 +09:00
validate : true ,
context ,
2016-11-22 18:14:51 -05:00
} ) ;
} ,
2016-11-04 14:38:31 +09:00
2016-11-04 10:28:54 +09:00
} ,
} ;
2016-11-22 18:14:51 -05:00
export default mutations ;