Skip to content

Commit f5001c0

Browse files
Merge branch 'dist'
2 parents c976807 + e9f6302 commit f5001c0

File tree

3 files changed

+69
-62
lines changed

3 files changed

+69
-62
lines changed

controllers/stat/addDailyStats.js

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,72 @@ const { Stat } = require("../../models/stat");
44
const { RequestError } = require("../../helpers");
55

66
const addStatistics = async (req, res) => {
7-
const { _id: userId } = req.user;
8-
const newDailyStats = req.body;
9-
10-
const oldStatistic = await Stat.findOne({owner: userId, status: "in progress"});
11-
const booksId = oldStatistic.training.active;
12-
const activeBooks = [];
13-
14-
await booksId.forEach(async (item) => {
15-
const book = await Book.findById(item);
16-
activeBooks.push(book);
17-
console.log(book);
18-
console.log("search done");
19-
});
20-
21-
const totalPagesReaded = oldStatistic.statistic.reduce((acc, {pages}) => {
22-
if(!pages) {
23-
return acc;
24-
}
25-
console.log("total pages")
26-
return acc + parseInt(pages);
27-
},0) + parseInt(newDailyStats.pages);
28-
29-
console.log(totalPagesReaded)
30-
31-
32-
const planningPages = 0;
33-
34-
for(let i = 0; i < activeBooks.length; i += 1) {
35-
36-
if((+activeBooks[i] + planningPages) > totalPagesReaded) {
37-
break;
38-
};
39-
40-
if (activeBooks[i].status === "in progress") {
41-
activeBooks[i].status = "finished";
42-
await Book.findByIdAndUpdate(activeBooks[i]._id, { status: "finished"});
43-
};
44-
};
45-
46-
const finishedBooks = activeBooks.filter(book => book.status === "finished");
47-
48-
console.log(finishedBooks)
49-
console.log(activeBooks)
50-
51-
const isTrainingFinished = activeBooks.length === finishedBooks.length;
52-
53-
oldStatistic.statistic.push(newDailyStats);
54-
55-
const result = await Stat.findOneAndUpdate({_id: oldStatistic._id},{statistic: oldStatistic.statistic}, {new: true})
56-
57-
if (isTrainingFinished) {
58-
result.message = "Training succesffuly end";
59-
await Stat.findOneAndUpdate({_id: oldStatistic._id},{status: "done"});
60-
console.log("finish ???")
7+
const { _id: userId } = req.user;
8+
const newDailyStats = req.body;
9+
10+
const oldStatistic = await Stat.findOne({
11+
owner: userId,
12+
status: "in progress",
13+
});
14+
15+
if(!oldStatistic) {
16+
throw RequestError(400, "Not found active training");
17+
}
18+
const booksId = oldStatistic.training.active;
19+
const allBooks = await Book.find({ owner: userId }, " -createdAt -updatedAt");
20+
const activeBooks = allBooks.filter((book) => booksId.includes(book._id));
21+
const totalPagesReaded =
22+
oldStatistic.statistic.reduce((acc, { pages }) => {
23+
if (!pages) {
24+
return acc;
25+
}
26+
return acc + parseInt(pages);
27+
}, 0) + parseInt(newDailyStats.pages);
28+
29+
let activePagesCount = 0;
30+
31+
await (async function () {
32+
for (let i = 0; i < activeBooks.length; i += 1) {
33+
activePagesCount += parseInt(activeBooks[i].pages);
34+
console.log(activePagesCount);
35+
36+
if (activePagesCount > totalPagesReaded) {
37+
break;
38+
}
39+
40+
if (activeBooks[i].status === "in progress") {
41+
activeBooks[i].status = "finished";
42+
await Book.findByIdAndUpdate(activeBooks[i]._id, {
43+
status: "finished",
44+
});
45+
}
6146
}
62-
63-
res.status(201).json(result);
47+
})();
48+
49+
const finishedBooks = activeBooks.filter(
50+
(book) => book.status === "finished"
51+
);
52+
53+
const isTrainingDone = activeBooks.length === finishedBooks.length;
54+
oldStatistic.statistic.push(newDailyStats);
55+
56+
let result = {};
57+
58+
if (isTrainingDone) {
59+
result = await Stat.findOneAndUpdate(
60+
{ _id: oldStatistic._id },
61+
{ statistic: oldStatistic.statistic, status: "done" },
62+
{ new: true }
63+
);
64+
} else {
65+
result = await Stat.findOneAndUpdate(
66+
{ _id: oldStatistic._id },
67+
{ statistic: oldStatistic.statistic },
68+
{ new: true }
69+
);
70+
}
71+
72+
res.status(201).json(result);
6473
};
6574

6675
module.exports = addStatistics;
67-

controllers/stat/getStatistics.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ const getStatistics = async (req, res) => {
55
const { _id } = req.user;
66

77
const result = await Stat.findOne({owner: _id, status: "in progress"})
8-
9-
if (!result) {
10-
throw RequestError(500, "Something went wrong");
11-
}
8+
if(!result) {
9+
throw RequestError(400, "Not found active training");
10+
}
1211

1312
res.status(201).json(result);
1413
};

middlewares/authenticate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const jwt = require('jsonwebtoken');
22

33
const { User } = require('../models/user');
4-
const RequestError = require('../helpers/index');
4+
const { RequestError } = require("../helpers");
55

66
const { SECRET_KEY } = process.env;
77

0 commit comments

Comments
 (0)