feat: Create activity log
This commit is contained in:
parent
9443810678
commit
8152c172a9
3 changed files with 78 additions and 11 deletions
|
@ -16,7 +16,7 @@ class ExerciseTrackerAPI {
|
||||||
#paths = {
|
#paths = {
|
||||||
"users": ["users"],
|
"users": ["users"],
|
||||||
"exercises": ["users/:ID/exercises"],
|
"exercises": ["users/:ID/exercises"],
|
||||||
"logs": ["users/:_id/logs"]
|
"logs": ["users/:ID/logs"]
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(INSTANCE) {
|
constructor(INSTANCE) {
|
||||||
|
@ -53,7 +53,9 @@ class ExerciseTrackerAPI {
|
||||||
};
|
};
|
||||||
|
|
||||||
let RESULT = await this.management[`Activities`].create(Object.assign({}, REQUEST.body, {"username": CORRESPONDING_USERS[0].name}));
|
let RESULT = await this.management[`Activities`].create(Object.assign({}, REQUEST.body, {"username": CORRESPONDING_USERS[0].name}));
|
||||||
let FORMATTED = await (new ResponseGenerator.Activities(Object.assign({},RESULT, {"ID": REQUEST.params[`ID`]}))).format();
|
let FORMATTED = new ResponseGenerator.Activities(Object.assign({},RESULT, {"ID": REQUEST.params[`ID`]}));
|
||||||
|
FORMATTED[`data`][`date`] = FORMATTED.convert_date();
|
||||||
|
FORMATTED = FORMATTED.format();
|
||||||
RESPONSE.send(FORMATTED);
|
RESPONSE.send(FORMATTED);
|
||||||
} else {
|
} else {
|
||||||
throw new CustomErrors.Data.Missing(REQUEST.body);
|
throw new CustomErrors.Data.Missing(REQUEST.body);
|
||||||
|
@ -63,10 +65,53 @@ class ExerciseTrackerAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Display a log of activities.
|
||||||
|
*/
|
||||||
|
const activity_log = async (REQUEST, RESPONSE) => {
|
||||||
|
const find_users = async () => {
|
||||||
|
let USERS = await this.management[`Users`].search({"ID": REQUEST.params[`ID`]});
|
||||||
|
if (!USERS.length || !USERS) {
|
||||||
|
throw new CustomErrors.Data.Incorrect(REQUEST.params);
|
||||||
|
};
|
||||||
|
return (USERS)
|
||||||
|
}
|
||||||
|
|
||||||
|
const find_activities = async (USERNAME, CRITERIA) => {
|
||||||
|
let FILTER = {};
|
||||||
|
if (CRITERIA[`from`] || CRITERIA[`to`]) {
|
||||||
|
[`from`, `to`].forEach((KEY) => {
|
||||||
|
if (CRITERIA[KEY]) {CRITERIA[KEY] = (new Date(CRITERIA[KEY])).getTime();};
|
||||||
|
})
|
||||||
|
|
||||||
|
FILTER[`date`] = {};
|
||||||
|
if (CRITERIA[`from`]) {FILTER[`date`][`$gte`] = CRITERIA[`from`]};
|
||||||
|
if (CRITERIA[`to`]) {FILTER[`date`][`$lte`] = CRITERIA[`to`]};
|
||||||
|
}
|
||||||
|
|
||||||
|
let RESULT = await this.management[`Activities`].search_User(USERNAME, FILTER, (CRITERIA[`limit`] ? CRITERIA[`limit`] : 0));
|
||||||
|
return (RESULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
let USERS = await find_users();
|
||||||
|
let LOG = Object.assign({}, await find_activities(USERS[0].name, (((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) : {}), {"ID": REQUEST.params[`ID`]});
|
||||||
|
let FORMATTED = (new ResponseGenerator.Log(LOG)).format();
|
||||||
|
RESPONSE.send(FORMATTED);
|
||||||
|
} catch(ERR) {
|
||||||
|
Messaging.exception(RESPONSE, ERR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.#paths[`exercises`].forEach((PATH) => {
|
this.#paths[`exercises`].forEach((PATH) => {
|
||||||
this[`instance`].post(`/api/${PATH}`, activity_creation)
|
this[`instance`].post(`/api/${PATH}`, activity_creation)
|
||||||
console.log(`Activity creation is ready on ${PATH}.`);
|
console.log(`Activity creation is ready on ${PATH}.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.#paths[`logs`].forEach((PATH) => {
|
||||||
|
this[`instance`].get(`/api/${PATH}`, activity_log);
|
||||||
|
console.log(`Activity log generation is ready on ${PATH}.`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
const Entry = require(`./object.JS`);
|
const Entry = require(`./object.JS`);
|
||||||
|
|
||||||
class Log extends Entry {
|
class Log extends Entry {
|
||||||
// Default properties
|
/* Default properties
|
||||||
name;
|
name;
|
||||||
ID;
|
ID;
|
||||||
count;
|
count;
|
||||||
activities;
|
activities;
|
||||||
|
*/
|
||||||
|
|
||||||
constructor(PROPERTIES) {
|
constructor(PROPERTIES) {
|
||||||
super(PROPERTIES);
|
super(PROPERTIES);
|
||||||
|
@ -23,11 +24,13 @@ class Log extends Entry {
|
||||||
const clean_activities = () => {
|
const clean_activities = () => {
|
||||||
if ((this.activities instanceof Array) ? this.activities.length : false) {
|
if ((this.activities instanceof Array) ? this.activities.length : false) {
|
||||||
for (let INDEX = 0; INDEX < this.activities.length ; INDEX++) {
|
for (let INDEX = 0; INDEX < this.activities.length ; INDEX++) {
|
||||||
[`username`].forEach((TBD) => {
|
[`username`, `_id`, `__v`].forEach((TBD) => {
|
||||||
if (Object.keys(this.activities[INDEX].includes(TBD))) {
|
delete this.activities[INDEX][`_doc`][TBD];
|
||||||
delete this.activities[INDEX][TBD]
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (this.activities[INDEX][`_doc`][`date`]) {
|
||||||
|
this.activities[INDEX][`_doc`][`date`] = (new Date(this.activities[INDEX][`_doc`][`date`])).toDateString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const DBManagement = require(`./management.JS`);
|
const DBManagement = require(`./management.JS`);
|
||||||
|
|
||||||
const Activity = require(`../entry/activity.JS`);
|
const Activity = require(`../entry/activity.JS`);
|
||||||
|
const Log = require(`../entry/log.JS`);
|
||||||
|
|
||||||
class ActivityManagement extends DBManagement {
|
class ActivityManagement extends DBManagement {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -37,6 +38,24 @@ class ActivityManagement extends DBManagement {
|
||||||
let CURRENT = await createActivity();
|
let CURRENT = await createActivity();
|
||||||
return (CURRENT);
|
return (CURRENT);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Search for all activities for a given user.
|
||||||
|
|
||||||
|
@param {String} USERNAME the username
|
||||||
|
@param {Object} CRITERIA the search criteria
|
||||||
|
@param {Number} SIZE the size
|
||||||
|
@return {Object} the log
|
||||||
|
*/
|
||||||
|
async search_User (USERNAME, CRITERIA, SIZE) {
|
||||||
|
let RESULT = await this.search(Object.assign({}, CRITERIA, {"username": USERNAME}), SIZE);
|
||||||
|
|
||||||
|
const convertData = () => {
|
||||||
|
let LOG = new Log({"name": USERNAME, "activities": (RESULT instanceof Array ? RESULT.length : false) ? RESULT : []});
|
||||||
|
return (LOG);
|
||||||
|
}
|
||||||
|
|
||||||
|
return(convertData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ActivityManagement;
|
module.exports = ActivityManagement;
|
Loading…
Add table
Add a link
Reference in a new issue