1+ import { send } from "@/utils/errors" ;
2+ import { TextModel } from "@/models/text.model" ;
3+ import { UserModel } from "@/models/user.model" ;
4+
5+
6+
7+ // Response with all texts with their genres.
8+ export const getTimeBenchmarks = ( request , response ) => {
9+ send ( response , async ( ) => {
10+ const texts = await TextModel . find ( ) . populate ( "genres" ) . populate ( "writer" ) ;
11+ let phases = {
12+ 1 : {
13+ max : Number . MIN_VALUE ,
14+ min : Number . MAX_VALUE ,
15+ total : 0
16+ } ,
17+ 2 : {
18+ max : Number . MIN_VALUE ,
19+ min : Number . MAX_VALUE ,
20+ total : 0
21+ } ,
22+ 3 : {
23+ max : Number . MIN_VALUE ,
24+ min : Number . MAX_VALUE ,
25+ total : 0
26+ } ,
27+ 4 : {
28+ max : Number . MIN_VALUE ,
29+ min : Number . MAX_VALUE ,
30+ total : 0
31+ } ,
32+ 5 : {
33+ max : Number . MIN_VALUE ,
34+ min : Number . MAX_VALUE ,
35+ total : 0
36+ } ,
37+ 6 : {
38+ max : Number . MIN_VALUE ,
39+ min : Number . MAX_VALUE ,
40+ total : 0
41+ } ,
42+ 7 : {
43+ max : Number . MIN_VALUE ,
44+ min : Number . MAX_VALUE ,
45+ total : 0
46+ } ,
47+ 8 : {
48+ max : Number . MIN_VALUE ,
49+ min : Number . MAX_VALUE ,
50+ total : 0
51+ } ,
52+ 9 : {
53+ max : Number . MIN_VALUE ,
54+ min : Number . MAX_VALUE ,
55+ total : 0
56+ } ,
57+ overall :{
58+ max : Number . MIN_VALUE ,
59+ min : Number . MAX_VALUE ,
60+ total : 0
61+ }
62+ }
63+ texts . forEach ( ( text ) => {
64+ let thisDate = new Date ( text . createdAt )
65+ for ( let i = 1 ; i < 9 ; i ++ ) {
66+ const nextDate = text . datesPerPhase [ i + 1 ] !== null ? new Date ( text . datesPerPhase [ i + 1 ] ) : null
67+ const timeInPhase = getHours ( thisDate , nextDate )
68+ phases [ i ] = {
69+ min : phases [ i ] . min > timeInPhase ? ( ( timeInPhase !== - 1 ) ? timeInPhase : phases [ i ] . min ) : phases [ i ] . min ,
70+ max : phases [ i ] . max < timeInPhase ? ( ( timeInPhase !== - 1 ) ? timeInPhase : phases [ i ] . max ) : phases [ i ] . max ,
71+ total : ( timeInPhase !== - 1 ) ? timeInPhase + phases [ i ] . total : phases [ i ] . total
72+ }
73+ thisDate = nextDate
74+ }
75+ const startDate = new Date ( text . createdAt )
76+ const endDate = text . datesPerPhase [ 9 ] !== null ? new Date ( text . datesPerPhase [ i + 1 ] ) : null
77+ const totalTime = getHours ( startDate , endDate )
78+ phases [ 'overall' ] = {
79+ min : phases [ 'overall' ] . min > totalTime ? ( ( totalTime !== - 1 ) ? totalTime : phases [ 'overall' ] . min ) : phases [ 'overall' ] . min ,
80+ max : phases [ 'overall' ] . max < totalTime ? ( ( totalTime !== - 1 ) ? totalTime : phases [ 'overall' ] . max ) : phases [ 'overall' ] . max ,
81+ total : ( totalTime !== - 1 ) ? totalTime + phases [ 'overall' ] . total : phases [ 'overall' ] . total
82+ }
83+ } )
84+ for ( let i = 1 ; i < 9 ; i ++ ) {
85+ phases [ i ] = {
86+ ...phases [ i ] ,
87+ avg : phases [ i ] . total / getLengthOfNotNull ( texts , i + 1 )
88+ }
89+ }
90+ phases [ 'overall' ] = {
91+ ...phases [ 'overall' ] ,
92+ avg : phases [ 'overall' ] . total / getLengthOfNotNull ( texts , 9 )
93+ }
94+
95+ console . log ( phases )
96+ return phases ;
97+ } ) ;
98+ } ;
99+
100+ // Response with a particular text based on its id.
101+ export const getText = ( request , response ) => {
102+ send ( response , async ( ) => {
103+ const { id } = request . params ;
104+ const reader = await TextModel . find ( { _id :id } ) . populate ( "genres" ) ;
105+ return reader ;
106+ } ) ;
107+ } ;
108+
109+ const getHours = ( date1 , date2 ) => {
110+ if ( date1 !== null && date2 !== null ) {
111+ const milliseconds = Math . abs ( date2 - date1 ) ;
112+ return milliseconds / 36e5 ;
113+ }
114+ return - 1
115+ }
116+
117+ const getLengthOfNotNull = ( texts , phase ) => {
118+ let length = 0
119+ texts . forEach ( ( text ) => {
120+ if ( text . datesPerPhase [ phase ] !== null ) length ++
121+ } )
122+ return length
123+ }
0 commit comments