2017-03-23 16:27:59 +09:00
import { newMutation , editMutation , removeMutation , GraphQLSchema , Utils } from 'meteor/vulcan:core' ;
import Users from 'meteor/vulcan:users' ;
2016-11-04 10:28:54 +09:00
2016-11-22 18:14:51 -05:00
const performCheck = ( mutation , user , document ) => {
2017-02-02 15:15:51 +01:00
if ( ! mutation . check ( user , document ) ) throw new Error ( Utils . encodeIntlError ( { id : ` app.mutation_not_allowed ` , value : ` " ${ mutation . name } " on _id " ${ document . _id } " ` } ) ) ;
2016-11-22 18:14:51 -05:00
} ;
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
} ,
} ;
2017-02-01 16:37:06 +01:00
GraphQLSchema . addMutation ( 'increasePostViewCount(postId: String): Float' ) ;
export default mutations ;