blob: 282ce6876c9db61b42545eeb97f5d107155e2e09 [file] [log] [blame]
import { Injectable, EventEmitter } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { NotificationSearchFilter } from '../model/notification-search-filter';
import { SessionContext } from '../common/session-context';
import { BaseHttpService } from './base-http.service';
import { Notification } from '../model/notification';
import { FilterSelection } from '../model/filter-selection';
import { TerritoryResponsibility } from '../model/territory-responsibility';
@Injectable()
export class NotificationService extends BaseHttpService {
public static ALL = 'active';
public static CURRENT = 'current';
public static FUTURE = 'future';
public static OPEN = 'open';
public static FINISHED = 'past';
public itemAdded$: EventEmitter<Notification>;
public itemChanged$: EventEmitter<Notification>;
constructor(
private _http: Http,
private _sessionContext: SessionContext
) {
super();
this.itemAdded$ = new EventEmitter();
this.itemChanged$ = new EventEmitter();
}
public getPager(totalItems: number, currentPage: number = 1, pageSize: number = 20) {
let totalPages = Math.ceil(totalItems / pageSize);
let startPage: number, endPage: number;
if (totalPages <= 20) {
// less than 10 total pages so show all
startPage = 1;
endPage = totalPages;
} else {
// more than 10 total pages so calculate start and end pages
if (currentPage <= 6) {
startPage = 1;
endPage = 20;
} else if (currentPage + 4 >= totalPages) {
startPage = totalPages - 9;
endPage = totalPages;
} else {
startPage = currentPage - 5;
endPage = currentPage + 4;
}
}
// calculate start and end item indexes
let startIndex = (currentPage - 1) * pageSize;
let endIndex = Math.min(startIndex + pageSize - 1, totalItems - 1);
// create an array of pages to ng-repeat in the pager control
let pages = this.range(startPage, endPage);
// return object with all pager properties required by the view
return {
totalItems: totalItems,
currentPage: currentPage,
pageSize: pageSize,
totalPages: totalPages,
startPage: startPage,
endPage: endPage,
startIndex: startIndex,
endIndex: endIndex,
pages: pages
};
}
private range(j, k) {
return Array
.apply(null, Array((k - j) + 1))
.map(function(discard, n){ return n + j; });
}
public getNotifications(notificationType: string, notificationSearchFilter?: NotificationSearchFilter): Observable<Notification[]> {
const headers = new Headers();
const url = super.getBaseUrl() + '/notifications/' + notificationType;
const filter = notificationSearchFilter || {};
this.createCommonHeaders(headers, this._sessionContext);
return this._http.post(url, JSON.stringify(filter), { headers: headers })
.map(res => super.extractData(res, this._sessionContext))
.catch(super.handleErrorPromise);
}
public getNotification(notificationId: number): Observable<Notification> {
const headers = new Headers();
const url = super.getBaseUrl() + '/notification/' + notificationId;
this.createCommonHeaders(headers, this._sessionContext);
return this._http.get(url, { headers: headers })
.map(res => super.extractData(res, this._sessionContext))
.catch(super.handleErrorPromise);
}
public getAssignedUserSuggestions(): Observable<string[]> {
const headers = new Headers();
this.createCommonHeaders(headers, this._sessionContext);
return this._http.get(super.getBaseUrl() + '/assignedUserSuggestions/', { headers: headers })
.map(res => super.extractData(res, this._sessionContext))
.catch(super.handleErrorPromise);
}
public getNotificationVersions(incidetId: number): Observable<Notification[]> {
const headers = new Headers();
this.createCommonHeaders(headers, this._sessionContext);
return this._http.get(super.getBaseUrl() + '/notificationsByIncident/' + incidetId, { headers: headers })
.map(res => super.extractData(res, this._sessionContext))
.catch(super.handleErrorPromise);
}
public createNotification(notification: Notification): Observable<Notification> {
const headers = new Headers();
const url = super.getBaseUrl() + '/notifications/create/';
this.createCommonHeaders(headers, this._sessionContext);
return this._http.put(url, JSON.stringify(notification), { headers: headers })
.map(res => {
const newNotification = super.extractData(res, this._sessionContext);
this.itemAdded$.emit(newNotification);
return newNotification;
})
.catch(super.handleErrorPromise);
}
public updateNotification(notification: Notification): Observable<Notification> {
const headers = new Headers();
const url = super.getBaseUrl() + '/notifications/update/';
this.createCommonHeaders(headers, this._sessionContext);
return this._http.post(url, JSON.stringify(notification), { headers: headers })
.map(res => {
const changedNotification = super.extractData(res, this._sessionContext);
this.itemChanged$.emit(changedNotification);
return changedNotification;
})
.catch(super.handleErrorPromise);
}
public getAllNotifications(): Observable<Notification[]> {
return this.getNotifications(NotificationService.ALL);
}
public getCurrentNotifications(): Observable<Notification[]> {
return this.getNotifications(NotificationService.CURRENT);
}
public getFutureNotifications(notificationSearchFilter: NotificationSearchFilter): Observable<Notification[]> {
return this.getNotifications(NotificationService.FUTURE, notificationSearchFilter);
}
public getOpenNotifications(notificationSearchFilter: NotificationSearchFilter): Observable<Notification[]> {
return this.getNotifications(NotificationService.OPEN, notificationSearchFilter);
}
public getFinishedNotifications(notificationSearchFilter: NotificationSearchFilter): Observable<Notification[]> {
return this.getNotifications(NotificationService.FINISHED, notificationSearchFilter);
}
}