2018-11-11 12:00:45 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2018-11-12 07:08:57 +01:00
|
|
|
import { Observable } from "rxjs";
|
2018-11-11 12:00:45 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpRequest } from "@angular/common/http";
|
|
|
|
|
|
|
|
|
|
import { environment } from "../../environments/environment";
|
|
|
|
|
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from "@angular/router";
|
|
|
|
|
import { DeviceGroup } from "./shared/device-group";
|
|
|
|
|
import { MaintenanceEntity } from "./shared/maintenance-entity";
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class MaintenanceManagerService {
|
|
|
|
|
|
|
|
|
|
private apiEndpoint = environment.apiUrl + '/api/maintenance';
|
|
|
|
|
private apiEndpointUpcoming = environment.apiUrl + '/api/maintenance/upcoming';
|
|
|
|
|
private cachedMaintenances: Array<DeviceGroup> = [];
|
|
|
|
|
|
|
|
|
|
constructor(private httpClient: HttpClient) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolver for the route
|
|
|
|
|
*
|
|
|
|
|
* @param {ActivatedRouteSnapshot} route
|
|
|
|
|
* @param {RouterStateSnapshot} state
|
|
|
|
|
* @returns {Promise<Array<Fault>>}
|
|
|
|
|
*/
|
|
|
|
|
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<Array<DeviceGroup>> {
|
|
|
|
|
let filter = route.data.filter ? route.data.filter : 'upcoming';
|
|
|
|
|
let url = filter == 'upcoming'
|
|
|
|
|
? this.apiEndpointUpcoming
|
|
|
|
|
: this.apiEndpoint;
|
|
|
|
|
return this.list(url).toPromise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List all faults
|
|
|
|
|
*
|
|
|
|
|
* @returns {Observable<Array<Fault>>}
|
|
|
|
|
*/
|
|
|
|
|
public list(url: string): Observable<Array<DeviceGroup>> {
|
|
|
|
|
return this.httpClient.get<Array<DeviceGroup>>(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get(hash: string): Observable<MaintenanceEntity> {
|
|
|
|
|
return this.httpClient.get<MaintenanceEntity>(`${this.apiEndpoint}/${hash}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public update(maintenance: MaintenanceEntity) {
|
|
|
|
|
return this.httpClient.put<MaintenanceEntity>(`${this.apiEndpoint}/${maintenance.hash}`, maintenance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get maintenances(): Array<DeviceGroup> {
|
|
|
|
|
return this.cachedMaintenances;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set maintenances(maintenances: Array<DeviceGroup>) {
|
|
|
|
|
this.cachedMaintenances = maintenances;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setWip(id: string, page: string) {
|
|
|
|
|
if(confirm("Biztos benne?")) {
|
|
|
|
|
let url = page == 'upcoming'
|
|
|
|
|
? this.apiEndpointUpcoming
|
|
|
|
|
: this.apiEndpoint;
|
|
|
|
|
this.httpClient.post<any>(`${this.apiEndpoint}/${id}/wip`,{}).subscribe(()=>{
|
|
|
|
|
this.list(url).subscribe(result => this.cachedMaintenances = result);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setFin(id: string, page: string) {
|
|
|
|
|
if(confirm("Biztos benne?")) {
|
|
|
|
|
let url = page == 'upcoming'
|
|
|
|
|
? this.apiEndpointUpcoming
|
|
|
|
|
: this.apiEndpoint;
|
|
|
|
|
this.httpClient.post<any>(`${this.apiEndpoint}/${id}/fin`, {}).subscribe(() => {
|
|
|
|
|
this.list(url).subscribe(result => this.cachedMaintenances = result);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|