2016-12-13 11:32:23 +09:00
import { newMutation , editMutation , removeMutation } from 'meteor/nova:lib' ;
2016-11-27 19:12:54 +09:00
import Users from './collection' ; // circular dependency?
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 = {
new : {
name : 'usersNew' ,
check ( user , document ) {
if ( ! user ) return false ;
return Users . canDo ( user , 'users.new' ) ;
} ,
mutation ( root , { document } , context ) {
performCheck ( this , context . currentUser , document ) ;
return newMutation ( {
collection : context . Users ,
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-08 18:22:17 +01:00
} ,
2016-11-22 18:14:51 -05:00
edit : {
name : 'usersEdit' ,
check ( user , document ) {
if ( ! user || ! document ) return false ;
return Users . owns ( user , document ) ? Users . canDo ( user , 'users.edit.own' ) : Users . canDo ( user , ` users.edit.all ` ) ;
} ,
mutation ( root , { documentId , set , unset } , context ) {
const document = context . Users . findOne ( documentId ) ;
performCheck ( this , context . currentUser , document ) ;
2016-11-14 20:50:11 +01:00
2016-11-22 18:14:51 -05:00
return editMutation ( {
collection : context . Users ,
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 20:50:11 +01:00
2016-11-08 18:22:17 +01:00
} ,
2016-11-22 18:14:51 -05:00
remove : {
2016-11-08 18:22:17 +01:00
2016-11-22 18:14:51 -05:00
name : 'usersRemove' ,
check ( user , document ) {
if ( ! user || ! document ) return false ;
return Users . owns ( user , document ) ? Users . canDo ( user , 'users.remove.own' ) : Users . canDo ( user , ` users.remove.all ` ) ;
} ,
mutation ( root , { documentId } , context ) {
2016-11-14 20:50:11 +01:00
2016-11-22 18:14:51 -05:00
const document = context . Users . findOne ( documentId ) ;
performCheck ( this , context . currentUser , document ) ;
return removeMutation ( {
collection : context . Users ,
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-14 20:50:11 +01:00
2016-11-08 18:22:17 +01:00
} ,
} ;
2016-11-22 18:14:51 -05:00
export default mutations ;