58 lines
No EOL
1.4 KiB
JavaScript
58 lines
No EOL
1.4 KiB
JavaScript
const DBManagement = require(`./management.JS`);
|
|
const Hash = require(`../../utilities/hash.JS`);
|
|
const CustomErrors = require(`../../utilities/errors.JS`);
|
|
|
|
const User = require(`../entry/user.JS`);
|
|
const Log = require(`../entry/log.JS`);
|
|
|
|
|
|
class UsersManagement extends DBManagement {
|
|
constructor() {
|
|
super(`localhost:27017`, `Activity-Tracker`);
|
|
this.configure(`Users`, User.generateTemplate());
|
|
};
|
|
|
|
/*
|
|
Create a user. The ID will be automatically generated.
|
|
|
|
@param {string} USERNAME the user name
|
|
@param {object} OPTIONS the account options
|
|
@return {object} the user data
|
|
*/
|
|
async create(USERNAME, OPTIONS) {
|
|
await this.state;
|
|
|
|
/*
|
|
Check if a user has been created.
|
|
|
|
@return {object} the found user
|
|
*/
|
|
const checkMatch = async () => {
|
|
let RESULTS = await this.search({"name": USERNAME}, 1);
|
|
return ((RESULTS.length) ? RESULTS[0] : false)
|
|
}
|
|
|
|
const createUser = async () => {
|
|
let ID = await (new Hash(USERNAME)).convert(`String`);
|
|
let CREATED = new User({'name': USERNAME.trim(), 'ID': ID});
|
|
|
|
let ENTRY = new this.model(CREATED);
|
|
try {
|
|
await ENTRY.save();
|
|
} catch(ERROR) {
|
|
throw new CustomErrors.DBProblem(ERROR.message);
|
|
}
|
|
|
|
return (CREATED);
|
|
}
|
|
|
|
if ((USERNAME) ? USERNAME.trim() : false) {
|
|
let CURRENT = (await checkMatch())
|
|
CURRENT = (!CURRENT) ? await createUser() : CURRENT;
|
|
|
|
return (CURRENT);
|
|
};
|
|
};
|
|
}
|
|
|
|
module.exports = UsersManagement; |