Exercise Tracker Problem

Tell us what’s happening:
All the functionalities work, most identical to the example of the challenge, but most of the test not work. I left the code, but if any know a bundle for testing locally the project i be grateful.

Your code so far

let express = require(‘express’);
let cors = require(‘cors’);
let bodyParser = require(‘body-parser’);

let app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ ‘extended’: false }));

app.set(‘port’, process.env.PORT || 3000);

app.get(’/’, function (req, res) {
res.sendFile(process.cwd() + ‘/views/exercises.html’);
});

let users = ;
let users_excersies = ;

app.post("/api/exercise/new-user", function (req, res) {
let user_name = req.body.user;

if (user_name !== undefined) {

    let user_exist = false;

    for (let i = 0; i < users.length; i++) {
        if (users[i].username === user_name) {
            user_exist = true;
            break;
        }
    }

    if (!user_exist) {
        let user = { "username": user_name, "_id": users.length.toString() };
        users.push(user);
        res.json(user);
    }
    else {
        res.json({ "error": "User exist" });
    }
}
else {
    res.json({ "error": "Not a valid username" });
}

});

app.get("/api/exercise/users", function (req, res) {
res.json(users);
});

app.post("/api/exercise/add", function (req, res) {

let user_id = req.body.user_id;
let description = req.body.description;
let duration = req.body.duration;
let date = req.body.date;

if (user_id !== undefined && description !== undefined && duration !== undefined) {

    let user_excercise = {};

    let user_exist = false;
    for (let i = 0; i < users.length; i++) {
        if (users[i]._id === user_id) {
            user_exist = true;
            user_excercise = { "username": users[i].username, "_id": users[i]._id };
            break;
        }
    }

    let duration_is_number = true;
    if (isNaN(parseInt(duration * 1))) {
        duration_is_number = false;
    }
    else {
        duration = parseInt(duration);
    }

    if (date === undefined || (new Date(date)).toString() === "Invalid Date") {
        date = new Date();
    }
    else {
        date = new Date(date);
    }

    if (user_exist) {
        if (duration_is_number) {
            user_excercise.description = description;
            user_excercise.date = date;
            user_excercise.duration = duration;
            users_excersies.push(user_excercise);
            res.json(user_excercise);
        }
        else {
            res.json({ "error": "Duration value need be a number" });
        }
    }
    else {
        res.json({ "error": "The User ID note exist" });
    }
}
else {
    res.json({ "error": "Not all the required fields are filled" });
}

});

function Logs(user_id, from, to, limit) {

let log_exercises = [];
let min_date;
let max_date;

let user_ex = users_excersies.filter(exercise => exercise._id === user_id);

if (user_ex.length > 0) {
    min_date = user_ex[0].date;
    max_date = user_ex[0].date;

    for (let i = 0; i < user_ex.length; i++) {

        if (user_ex[i].date < min_date) {
            min_date = user_ex[i].date;
        }

        if (user_ex[i].date > max_date) {
            max_date = user_ex[i].date;
        }
    }

    if (from === undefined || from < min_date) {
        from = min_date;
    }

    if (to === undefined || to > max_date) {
        to = max_date;
    }

    let temp = 0;

    for (let i = 0; i < user_ex.length; i++) {
        for (let n = i + 1; n < user_ex.length; n++) {
            if (user_ex[i].date > user_ex[n].date) {
                temp = user_ex[n];
                user_ex[n] = user_ex[i];
                user_ex[i] = temp;
            }
        }
    }

    if (limit === undefined || isNaN(limit)) {
        limit = user_ex.length;
    }

    for (let i = 0; i < user_ex.length; i++) {
        if (user_ex[i].date >= from && user_ex[i].date <= to) {
            log_exercises.push(user_ex[i]);
            limit--;
        }

        if (limit === 0) {
            break;
        }
    }
}

let end_log =
{
    "_id": user_id.toString(),
    "username": users.filter(u => u._id === user_id.toString())[0].username,
    "from": from,
    "to": to,
    "count": log_exercises.length,
    "log": log_exercises.map(ex => ({
        description: ex.description,
        duration: ex.duration,
        date: ex.date
    }))
};

return end_log;

}

app.get("/api/exercise/log", function (req, res) {
let from = req.query.from;
let to = req.query.to;
let userId = req.query.userId;
let limit = req.query.limit;

let user = [];

for (let i = 0; i < users.length; i++) {
    if (users[i]._id === userId) {
        user.push(users[i]);
    }
}

if (userId !== undefined && user.length > 0) {

    if (from !== undefined) {
        from = new Date(from);

        if (from.toString() === "Invalid Date") {
            from = undefined;
        }
    }

    if (to !== undefined) {
        to = new Date(to);

        if (to.toString() === "Invalid Date") {
            to = undefined;
        }
    }

    if (limit !== undefined) {
        limit = parseInt(limit * 1);
    }

    res.json(Logs(userId, from, to, limit));
}
else {
    res.json({ "error": "Need send a valid User ID" });
}

});

app.use(function (req, res, next) {
res.status(404);
res.send(‘NOT FOUND’);
});

app.listen(app.get(‘port’), function () {
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge: