import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { environment } from '../../../../environments/environment' ; import { Activity } from '../models/activity' ; import { DateService } from '../../../services/date.service' ; import { ActivityType } from '../models/activity-type' ; import { ActivitySearchCriteria } from '../models/activity-search-criteria' ; const baseUrl = environment . baseCrmServiceUrl ; const recentActivitiesDays = 60 ; @ Injectable ( { providedIn : 'root' } ) export class ActivityService { constructor ( private http , private dateService ) { } fetchActivitiesByCriteria ( criteria ) { let minDate = criteria . minDate ; let maxDate = criteria . maxDate ; let orderBy = criteria . orderBy ; let orderType = criteria . orderType ; let contactId = criteria . contactId ; let dealId = criteria . dealId ; let salesOwnerId = criteria . salesOwnerId ; let url = `${ baseUrl }/activities/search?dealId=${ dealId }&contactId=${ contactId }&minDate=${ minDate }&
              maxDate=${ maxDate }&orderBy=${ orderBy }&orderType=${ orderType }&salesOwnerId=${ salesOwnerId }` ; console . log ( "Fetch activities by deal URL is " + url ) ; return this . http . get < Activity [ ] > ( url ) ; } fetchActivitiesByDealId ( dealId ) { let minDate = this . dateService . getDaysBackwardAsNumber ( recentActivitiesDays ) ; let orderBy = 'startDate' ; let orderType = 'DESC' ; let url = `${ baseUrl }/activities/search?dealId=${ dealId }&minDate=${ minDate }&orderBy=${ orderBy }&orderType=${ orderType }` ; console . log ( "Fetch activities by deal URL is " + url ) ; return this . http . get < Activity [ ] > ( url ) ; } fetchRecentActivitiesByContactId ( contactId ) { let minDate = this . dateService . getDaysBackwardAsNumber ( recentActivitiesDays ) ; let orderBy = 'startDate' ; let orderType = 'DESC' ; let status = 'COMPLETED' ; let url = `${ baseUrl }/activities/search?contactId=${ contactId }&minDate=${ minDate }&orderBy=${ orderBy }&orderType=${ orderType }&status=${ status }` ; console . log ( "Fetch activities by contact URL is " + url ) ; return this . http . get < Activity [ ] > ( url ) ; } fetchAllActivityTypes ( ) { let url = `${ baseUrl }/activitytypes` ; return this . http . get < ActivityType [ ] > ( url ) ; } createActivity ( activity ) { let url = `${ baseUrl }/activities` ; return this . http . post < Activity > ( url , activity ) ; } updateActivity ( activity ) { let url = `${ baseUrl }/activities/${ activity . id }` ; return this . http . put < Activity > ( url , activity ) ; } fetchActivityById ( activityId ) { let url = `${ baseUrl }/activities/${ activityId }` ; return this . http . get < Activity > ( url ) ; } isOverdue ( activity ) { let overdue = false ; if ( activity && activity . type && activity . type . usesStatus ) { if ( activity . status == 'PENDING' ) { if ( activity . startDate ) { let localDueDate = this . dateService . convertToLocal ( activity . startDate ) ; let now = Date . now ( ) ; overdue = ( now > localDueDate ) ; } } } return overdue ; } }