| 
									
										
										
										
											2018-10-23 16:21:53 +03:00
										 |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const { isMongoId } = require('validator'); | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MongoIds { | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.knownIds = []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   check(id, title) { | 
					
						
							|  |  |  |     if (!isMongoId(id)) { | 
					
						
							|  |  |  |       throw new Error(`Expected a valid ObjectId for ${title}, but got ${id}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const idIndex = _.findIndex(this.knownIds, existing => id === existing); | 
					
						
							|  |  |  |     if (idIndex !== -1) { | 
					
						
							|  |  |  |       throw new Error(`
 | 
					
						
							|  |  |  |     All challenges must have a unique id. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The id for ${title} is already assigned | 
					
						
							|  |  |  |     `);
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |     this.knownIds = [...this.knownIds, id]; | 
					
						
							| 
									
										
										
										
											2018-01-19 14:03:17 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 16:21:53 +03:00
										 |  |  | module.exports = MongoIds; |