User search and creation
This commit is contained in:
		
							parent
							
								
									5fa9e6efd6
								
							
						
					
					
						commit
						0458e11267
					
				
					 7 changed files with 109 additions and 42 deletions
				
			
		|  | @ -1,9 +1,88 @@ | ||||||
|  | const bodyParser = require(`body-parser`); | ||||||
|  | 
 | ||||||
|  | const CustomErrors = require(`./utilities/errors.JS`); | ||||||
|  | const Messaging = require(`./utilities/messaging.JS`); | ||||||
|  | 
 | ||||||
| class DBManagement {}; | class DBManagement {}; | ||||||
| DBManagement.Activities = require(`./database/management/management_activity.JS`); | DBManagement.Activities = require(`./database/management/management_activity.JS`); | ||||||
| DBManagement.Users = require(`./database/management/management_user.JS`); | DBManagement.Users = require(`./database/management/management_user.JS`); | ||||||
| 
 | 
 | ||||||
|  | class ResponseGenerator {}; | ||||||
|  | ResponseGenerator.Activities = require(`./response/activity.JS`); | ||||||
|  | ResponseGenerator.Users = require(`./response/user.JS`); | ||||||
|  | ResponseGenerator.Log = require(`./response/log.JS`); | ||||||
|  | 
 | ||||||
| class ExerciseTrackerAPI { | class ExerciseTrackerAPI { | ||||||
| 	constructor() { | 	#paths = { | ||||||
|  | 		"users": ["users"], | ||||||
|  | 		"exercises": ["users/:_id/exercises"], | ||||||
|  | 		"logs": ["users/:_id/logs"] | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	constructor(INSTANCE) { | ||||||
|  | 		this[`instance`] = INSTANCE; | ||||||
|  | 		this[`instance`].use(bodyParser.json()); | ||||||
|  | 		this[`instance`].use(bodyParser.urlencoded({extended: true})); | ||||||
|  | 
 | ||||||
|  | 		const init_management = () => { | ||||||
|  | 			this[`management`] = {}; | ||||||
|  | 
 | ||||||
|  | 			Object.keys(DBManagement).forEach((DATABASE) => { | ||||||
|  | 				this.management[DATABASE] = new DBManagement[DATABASE](); | ||||||
|  | 			}); | ||||||
|  | 			this.manageUsers(); | ||||||
|  | 		}; | ||||||
|  | 
 | ||||||
|  | 		init_management(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/* | ||||||
|  | 		Manage the users.  | ||||||
|  | 	*/ | ||||||
|  | 	manageUsers () { | ||||||
|  | 		/*  | ||||||
|  | 			Create new users. | ||||||
|  | 		*/ | ||||||
|  | 		const users_creation = async (REQUEST, RESPONSE) => { | ||||||
|  | 			try { | ||||||
|  | 				if (((REQUEST.body) ? Object.keys(REQUEST.body).length : false) ? Object.keys(REQUEST.body).includes(`username`) : false) { | ||||||
|  | 					this.management[`Users`].create(REQUEST.body[`username`]).then((RESULT) => { | ||||||
|  | 						let FORMATTED = (new ResponseGenerator.Users(RESULT)).format(); | ||||||
|  | 						RESPONSE.send(FORMATTED);	 | ||||||
|  | 					}) | ||||||
|  | 				} else { | ||||||
|  | 					throw new CustomErrors.Data.Missing(REQUEST.body); | ||||||
|  | 				} | ||||||
|  | 			} catch(ERR) { | ||||||
|  | 				Messaging.exception(RESPONSE, ERR); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		/* | ||||||
|  | 			Search for all users.  | ||||||
|  | 		*/ | ||||||
|  | 		const users_display = (REQUEST, RESPONSE) => { | ||||||
|  | 			try { | ||||||
|  | 				let CRITERIA = (((REQUEST.query) ? Object.keys(REQUEST.query).length : false) || ((REQUEST.body) ? Object.keys(REQUEST.body).length : false)) ? (((REQUEST.body) ? Object.keys(REQUEST.body).length : false) ? REQUEST.body : REQUEST.query) : {}; | ||||||
|  | 				 | ||||||
|  | 				this.management[`Users`].search(CRITERIA).then((RESULT) => { | ||||||
|  | 					let FORMATTED = (new ResponseGenerator.Users(RESULT)).format(); | ||||||
|  | 					RESPONSE.send(FORMATTED); | ||||||
|  | 				}) | ||||||
|  | 			} catch(ERR) { | ||||||
|  | 				Messaging.exception(RESPONSE, ERR); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		this.#paths[`users`].forEach((PATH) => { | ||||||
|  | 			this[`instance`].get(`/api/${PATH}`, users_display) | ||||||
|  | 			console.log(`User search is ready on ${PATH}.`); | ||||||
|  | 		}); | ||||||
|  | 
 | ||||||
|  | 		this.#paths[`users`].forEach((PATH) => { | ||||||
|  | 			this[`instance`].post(`/api/${PATH}`, users_creation) | ||||||
|  | 			console.log(`User creation is ready on ${PATH}.`); | ||||||
|  | 		}); | ||||||
| 	} | 	} | ||||||
| }  | }  | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,10 +1,13 @@ | ||||||
| const Entry = require(`./object.JS`); | const Entry = require(`./object.JS`); | ||||||
| 
 | 
 | ||||||
| class Activity extends Entry { | class Activity extends Entry { | ||||||
|     username; |     /* | ||||||
|     description; |     Default properties:  | ||||||
|     duration; |         username | ||||||
|     date; |         description | ||||||
|  |         duration | ||||||
|  |         date | ||||||
|  |     */ | ||||||
| 
 | 
 | ||||||
|     constructor(PROPERTIES) { |     constructor(PROPERTIES) { | ||||||
|         super(PROPERTIES); |         super(PROPERTIES); | ||||||
|  | @ -21,15 +24,8 @@ class Activity extends Entry { | ||||||
|             "description": {"type": String}, |             "description": {"type": String}, | ||||||
|             "date": {"type": Number}, |             "date": {"type": Number}, | ||||||
|             "duration": {"type": Number} |             "duration": {"type": Number} | ||||||
|         } |         }; | ||||||
|         let TEST = new Activity(); |         return (SCHEMA); | ||||||
| 
 |  | ||||||
|         // Verify that the schema is valid.
 |  | ||||||
|         Object.keys(SCHEMA).forEach((KEY) => { |  | ||||||
|             if (!Object.keys(TEST).includes(KEY)) {delete SCHEMA[KEY];}; |  | ||||||
|         }); |  | ||||||
| 
 |  | ||||||
|         return (SCHEMA) |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| /* An object to represent an entry in the database. */ | /* An object to represent an entry in the database. */ | ||||||
| class Entry { | class Entry { | ||||||
|     constructor(PROPERTIES) { |     constructor(PROPERTIES) { | ||||||
|         (PROPERTIES) ? Object.entries(PROPERTIES).forEach(([property, value]) => { |         (PROPERTIES && (Object.keys(PROPERTIES).length)) ? Object.entries(PROPERTIES).forEach(([property, value]) => { | ||||||
|             this[property] = value; |             this[property] = value; | ||||||
|         }) : false; |         }) : false; | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|  | @ -2,11 +2,13 @@ const Entry = require(`./object.JS`); | ||||||
| const Hash = require(`../../utilities/hash.JS`); | const Hash = require(`../../utilities/hash.JS`); | ||||||
| 
 | 
 | ||||||
| class User extends Entry { | class User extends Entry { | ||||||
|     // Default properties
 |     /* | ||||||
|     name; |     Default properties: | ||||||
|     ID; |         name | ||||||
|     activated = false; |         ID | ||||||
|     passcode; |         activated = false | ||||||
|  |         passcode | ||||||
|  |     */ | ||||||
|     #login = false; |     #login = false; | ||||||
| 
 | 
 | ||||||
|     constructor(PROPERTIES) { |     constructor(PROPERTIES) { | ||||||
|  | @ -40,15 +42,8 @@ class User extends Entry { | ||||||
|             "ID": {"type": String, "required": true, "unique": true, "dropDups": true}, |             "ID": {"type": String, "required": true, "unique": true, "dropDups": true}, | ||||||
|             "activated": {"type": Boolean}, |             "activated": {"type": Boolean}, | ||||||
|             "passcode": {"type": String} |             "passcode": {"type": String} | ||||||
|         } |         }; | ||||||
|         let TEST = new User(); |         return (SCHEMA); | ||||||
| 
 |  | ||||||
|         // Verify that the schema is valid.
 |  | ||||||
|         Object.keys(SCHEMA).forEach((KEY) => { |  | ||||||
|             if (!Object.keys(TEST).includes(KEY)) {delete SCHEMA[KEY];}; |  | ||||||
|         }); |  | ||||||
| 
 |  | ||||||
|         return (SCHEMA) |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -43,6 +43,7 @@ class DBManagement { | ||||||
| 		let RESULT = []; | 		let RESULT = []; | ||||||
| 
 | 
 | ||||||
| 		if ((CRITERIA && CRITERIA instanceof Object) ? !(Object.keys(CRITERIA).length) : true) { | 		if ((CRITERIA && CRITERIA instanceof Object) ? !(Object.keys(CRITERIA).length) : true) { | ||||||
|  | 			console.log(`Searching all...`); | ||||||
| 			RESULT = await this.model.find(); | 			RESULT = await this.model.find(); | ||||||
| 		} else if (CRITERIA instanceof Object) { | 		} else if (CRITERIA instanceof Object) { | ||||||
| 			RESULT = await this.model.find(CRITERIA); | 			RESULT = await this.model.find(CRITERIA); | ||||||
|  |  | ||||||
|  | @ -19,7 +19,7 @@ class UsersManagement extends DBManagement { | ||||||
| 		@param {object} OPTIONS the account options | 		@param {object} OPTIONS the account options | ||||||
| 		@return {object} the user data | 		@return {object} the user data | ||||||
| 	*/ | 	*/ | ||||||
| 	async create(USERNAME, OPTIONS, done) { | 	async create(USERNAME, OPTIONS) { | ||||||
| 		await this.state; | 		await this.state; | ||||||
| 
 | 
 | ||||||
| 		/*  | 		/*  | ||||||
|  | @ -47,7 +47,7 @@ class UsersManagement extends DBManagement { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if ((USERNAME) ? USERNAME.trim() : false) { | 		if ((USERNAME) ? USERNAME.trim() : false) { | ||||||
| 			let CURRENT = !(await checkMatch()) | 			let CURRENT = (await checkMatch()) | ||||||
| 			CURRENT = (!CURRENT) ? await createUser() : CURRENT; | 			CURRENT = (!CURRENT) ? await createUser() : CURRENT; | ||||||
| 			 | 			 | ||||||
| 			return (CURRENT); | 			return (CURRENT); | ||||||
|  |  | ||||||
|  | @ -2,9 +2,6 @@ | ||||||
| 	Modify data returned by the database connector scripts. s | 	Modify data returned by the database connector scripts. s | ||||||
| */ | */ | ||||||
| class DBParser { | class DBParser { | ||||||
| 	mappings; |  | ||||||
| 	data; |  | ||||||
| 
 |  | ||||||
| 	constructor(DATA = {}, MAPPINGS = {}) { | 	constructor(DATA = {}, MAPPINGS = {}) { | ||||||
| 		this.data = DATA; | 		this.data = DATA; | ||||||
| 		this.mappings = MAPPINGS; | 		this.mappings = MAPPINGS; | ||||||
|  | @ -20,28 +17,27 @@ class DBParser { | ||||||
| 		/*  | 		/*  | ||||||
| 			Map certain new keys to their orignal counterparts.  | 			Map certain new keys to their orignal counterparts.  | ||||||
| 		*/ | 		*/ | ||||||
| 		const map = (ROW) => { | 		function mapKeys (ORIGINAL = {}, MAPPINGS) { | ||||||
| 			let RESULT = {}; | 			let RESULT = {}; | ||||||
| 			Object.entries(this.mappings).forEach(([NEW, OLD]) => { | 			 | ||||||
| 				if (Object.keys(ROW).includes(OLD)) { | 			Object.entries(MAPPINGS).forEach(([NEW, OLD]) => { | ||||||
| 					RESULT[NEW] = (typeof ROW[OLD]).includes(`str`) ? ROW[OLD].trim() : ROW[OLD]; | 				RESULT[NEW] = (ORIGINAL[OLD] != null ? (typeof ORIGINAL[OLD]).includes(`str`) : false) ? ORIGINAL[OLD].trim() : ORIGINAL[OLD]; | ||||||
| 				} |  | ||||||
| 			}); | 			}); | ||||||
| 			 | 			 | ||||||
| 			return RESULT; | 			return RESULT; | ||||||
| 		}; | 		} | ||||||
| 		 | 		 | ||||||
| 		if (this.data instanceof Array) { | 		if (this.data instanceof Array) { | ||||||
| 			ENTRIES = []; | 			ENTRIES = []; | ||||||
| 	 | 	 | ||||||
| 			(this.data.length) ? this.data.forEach((SELECTED) => { | 			(this.data.length) ? this.data.forEach((SELECTED) => { | ||||||
| 				if (SELECTED) { | 				if (SELECTED) { | ||||||
| 					let CLEANED = map(SELECTED); | 					let CLEANED = mapKeys(SELECTED, this.mappings); | ||||||
| 					(Object.keys(CLEANED).length) ? ENTRIES.push(CLEANED) : false; | 					(Object.keys(CLEANED).length) ? ENTRIES.push(CLEANED) : false; | ||||||
| 				}; | 				}; | ||||||
| 			}) : false; | 			}) : false; | ||||||
| 		} else if (this.data && this.data instanceof Object) { | 		} else if (this.data && this.data instanceof Object) { | ||||||
| 			ENTRIES = map(this.data); | 			ENTRIES = mapKeys(this.data, this.mappings); | ||||||
| 		}; | 		}; | ||||||
| 
 | 
 | ||||||
| 		this.cleaned = ENTRIES; | 		this.cleaned = ENTRIES; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue