@@ -4,64 +4,72 @@ const { Stat } = require("../../models/stat");
4
4
const { RequestError } = require ( "../../helpers" ) ;
5
5
6
6
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
+ }
61
46
}
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 ) ;
64
73
} ;
65
74
66
75
module . exports = addStatistics ;
67
-
0 commit comments