| 
									
										
										
										
											2020-10-16 22:41:38 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const categories = require('../categories'); | 
					
						
							|  |  |  | const events = require('../events'); | 
					
						
							| 
									
										
										
										
											2020-12-28 19:52:46 -05:00
										 |  |  | const privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2020-10-16 22:41:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | const categoriesAPI = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 19:52:46 -05:00
										 |  |  | categoriesAPI.get = async function (caller, data) { | 
					
						
							|  |  |  | 	const [userPrivileges, category] = await Promise.all([ | 
					
						
							|  |  |  | 		privileges.categories.get(data.cid, caller.uid), | 
					
						
							|  |  |  | 		categories.getCategoryData(data.cid), | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 	if (!category || !userPrivileges.read) { | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return category; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-16 22:41:38 -04:00
										 |  |  | categoriesAPI.create = async function (caller, data) { | 
					
						
							|  |  |  | 	const response = await categories.create(data); | 
					
						
							|  |  |  | 	const categoryObjs = await categories.getCategories([response.cid], caller.uid); | 
					
						
							|  |  |  | 	return categoryObjs[0]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | categoriesAPI.update = async function (caller, data) { | 
					
						
							|  |  |  | 	await categories.update(data); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | categoriesAPI.delete = async function (caller, data) { | 
					
						
							|  |  |  | 	const name = await categories.getCategoryField(data.cid, 'name'); | 
					
						
							|  |  |  | 	await categories.purge(data.cid, caller.uid); | 
					
						
							|  |  |  | 	await events.log({ | 
					
						
							|  |  |  | 		type: 'category-purge', | 
					
						
							|  |  |  | 		uid: caller.uid, | 
					
						
							|  |  |  | 		ip: caller.ip, | 
					
						
							|  |  |  | 		cid: data.cid, | 
					
						
							|  |  |  | 		name: name, | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |