import { TimeAwaySummaryItem } from "../models/TimeAwaySummaryItem" ; import { IWebPartContext } from '@microsoft/sp-webpart-base' ; import * as _ from "lodash" ; import { ITimeAwaySummaryDataProvider } from './ITimeAwaySummaryDataProvider' ; import { ITimeAwaySummaryWebPartProps } from '../ITimeAwaySummaryWebPartProps' ; import * as moment from 'moment' ; import { IMyTimeAwayItem , ApprovalStatus } from "../../../libraries/models/timeAwayModel" ; import { WeekType , Phase , MockTimeAwayList } from '../../../libraries/index' ; export default class MockTimeAwaySummaryDataProvider implements ITimeAwaySummaryDataProvider { private _webPartContext : IWebPartContext ; constructor ( webPartProps , webPartContext ) { this . webPartContext = webPartContext ; } public set webPartContext ( value ) { this . _webPartContext = value ; } public get webPartContext ( ) : IWebPartContext { return this . _webPartContext ; } public getTimeAwaySummaryList ( weekType , phase , statusFilter ) { let results = [ ] ; const date = this . _getFirstDay ( weekType , phase ) ; let days = weekType === WeekType . FiveDays ? 5 : 7 ; let myTimeAwayItems = MockTimeAwayList . getMockTimeAwayListItems ( ) ; let weekItems = [ ] ; let calculatorItems = [ ] ; while ( days -- > 0 ) { let start = date . toDate ( ) ; let end = date . add ( 1 , 'days' ) . toDate ( ) ; if ( statusFilter ) { calculatorItems = _ . orderBy ( _ . filter ( myTimeAwayItems , ( item ) => { return item . status === ApprovalStatus . Approved && item . start < end && item . end > start ; } ) , ( item ) => { return item . lastName ; } , [ 'asc' ] ) ; } else { calculatorItems = _ . orderBy ( _ . filter ( myTimeAwayItems , ( item ) => { return item . start < end && item . end > start ; } ) , ( item ) => { return item . lastName ; } , [ 'asc' ] ) ; } weekItems . push ( calculatorItems ) ; } weekItems . map ( ( items , index ) => { let day = this . _getFirstDay ( weekType , phase ) . add ( index , 'days' ) ; let nextDay = this . _getFirstDay ( weekType , phase ) . add ( index + 1 , 'days' ) ; let timeAwaySummaryItem = new TimeAwaySummaryItem ( ) ; timeAwaySummaryItem . WeekDay = day . format ( 'dddd' ) ; timeAwaySummaryItem . MonthDate = day . format ( 'D' ) ; timeAwaySummaryItem . Month = day . format ( 'MMM' ) ; timeAwaySummaryItem . PersonNameArray = items . map ( ( item ) => { let start1 = moment ( item . start ) ; let end1 = moment ( item . end ) ; let timestring = "" ; let current = day . clone ( ) ; if ( start1 <= day ) { timestring = "(12:00am" ; } else { timestring = "(" + start1 . format ( "h:mma" ) ; } if ( end1 >= nextDay ) { timestring += " - 11:59pm)" ; } else { timestring += ` - ${ end1 . format ( "h:mma" ) })` ; } if ( moment . duration ( end1 . diff ( start1 ) ) . asHours ( ) >= 8 && moment . duration ( end1 . diff ( current . hours ( 6 ) ) ) . asHours ( ) >= 8 ) { timestring = "(All Day)" ; } return item . firstName + ' ' + item . lastName + ' ' + timestring ; } ) ; results . push ( timeAwaySummaryItem ) ; } ) ; return Promise . resolve ( results ) ; } 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 } ) ; } }