2016-10-06 12:16:59 +02:00
import { Meteor } from 'meteor/meteor' ;
2016-06-23 11:40:35 +09:00
import Posts from "meteor/nova:posts" ;
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users' ;
2016-06-23 15:11:56 +09:00
import Categories from "./collection.js" ;
2016-06-23 11:40:35 +09:00
2015-08-13 10:19:00 +09:00
Meteor . methods ( {
2016-04-14 11:47:56 +09:00
"categories.deleteById" : function ( categoryId ) {
2015-10-04 12:17:51 +09:00
check ( categoryId , String ) ;
2016-07-21 09:39:40 +09:00
const currentUser = this . userId && Users . findOne ( this . userId ) ;
2015-10-04 12:17:51 +09:00
2016-07-21 09:39:40 +09:00
if ( Users . canDo ( currentUser , "categories.remove.all" ) ) {
2015-08-13 10:19:00 +09:00
// delete category
Categories . remove ( categoryId ) ;
// find any direct children of this category and make them root categories
Categories . find ( { parentId : categoryId } ) . forEach ( function ( category ) {
Categories . update ( category . _id , { $unset : { parentId : "" } } ) ;
} ) ;
2016-04-14 11:47:56 +09:00
// find any posts with this category and remove it
2015-08-13 10:19:00 +09:00
var postsUpdated = Posts . update ( { categories : { $in : [ categoryId ] } } , { $pull : { categories : categoryId } } , { multi : true } ) ;
2015-10-04 12:17:51 +09:00
return postsUpdated ;
2015-08-13 10:19:00 +09:00
}
}
2016-04-14 15:54:50 +09:00
} ) ;
2016-10-06 12:16:59 +02:00
// assign smart methods on startup so the method code generated takes care of categories' custom fields (extended schema) -> prevent bug on create/edit categories with custom fields
Meteor . startup ( ( ) => {
Categories . smartMethods ( {
createName : "categories.new" ,
editName : "categories.edit"
} ) ;
2015-08-13 10:19:00 +09:00
} ) ;