import { TimeAwaySummaryItem } from "../models/TimeAwaySummaryItem" ; import { ITimeAwaySummaryDataProvider } from './ITimeAwaySummaryDataProvider' ; import * as moment from 'moment' ; import { WeekType , Phase , Constants } from '../../../libraries/index' ; import { sp } from "../../../pnp-preset" ; export default class TimeAwaySummaryDataProvider implements ITimeAwaySummaryDataProvider { public getTimeAwaySummaryList ( weekType , phase , statusFilter ) { const promise = [ ] ; const date = this . _getFirstDay ( weekType , phase ) ; let days = weekType === WeekType . FiveDays ? 5 : 7 ; while ( days -- > 0 ) { let start = date . toDate ( ) ; let end = date . add ( 1 , 'days' ) . toDate ( ) ; const filter = statusFilter ? `OData__ModerationStatus eq 'Approved' and Start lt '${ end . toJSON ( ) }' and End gt '${ start . toJSON ( ) }'` : `Start lt '${ end . toJSON ( ) }' and End gt '${ start . toJSON ( ) }'` ; promise . push ( sp . web . lists . getByTitle ( Constants . TimeAwayListTitle ) . items . select ( 'First_x0020_Name' , 'Last_x0020_Name' , 'Start' , 'End' ) . filter ( filter ) . orderBy ( 'Last_x0020_Name' ) . get ( ) ) ; } return Promise . all ( promise ) . then ( ( responses ) => { return responses . map ( ( response , index ) => { let day = this . _getFirstDay ( weekType , phase ) . add ( index , 'days' ) ; let nextDay = this . _getFirstDay ( weekType , phase ) . add ( index + 1 , 'days' ) ; return { WeekDay : day . format ( 'dddd' ) , MonthDate : day . format ( 'D' ) , Month : day . format ( 'MMM' ) , PersonNameArray : response . map ( ( item ) => { let start = moment ( item . Start ) ; let end = moment ( item . End ) ; let timestring = "" ; let current = day . clone ( ) ; if ( start <= day ) { timestring = "(12:00am" ; } else { timestring = "(" + start . format ( "h:mma" ) ; } if ( end >= nextDay ) { timestring += " - 11:59pm)" ; } else { timestring += ` - ${ end . format ( "h:mma" ) })` ; } if ( moment . duration ( end . diff ( start ) ) . asHours ( ) >= 8 && moment . duration ( end . diff ( current . hours ( 6 ) ) ) . asHours ( ) >= 8 ) { timestring = "(All Day)" ; } return item . First_x0020_Name + ' ' + item . Last_x0020_Name + ' ' + timestring ; } ) } as TimeAwaySummaryItem ; } ) ; } ) ; } private _getFirstDay ( weekType , phase ) { return moment ( ) . isoWeekday ( phase === Phase . ThisWeek ? 1 : 8 ) . add ( weekType === WeekType . FiveDays ? 0 : - 2 , 'days' ) . set ( { hour : 0 , minute : 0 , second : 0 } ) ; } }