import { Injectable } from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { IJob, IUIJob, JobLog, RptOption, toJob } from '../../job/models/job.model'; import { AppFile } from '../models/shared.model'; import { UpdateJobOps } from '../../job/actions/job.actions'; @Injectable() export class JobService { private readonly jobURL = '/jobs'; constructor( private http: HttpClient ) { } loadJobs(ops: any): Observable { let _ops = new HttpParams() .set('clientId', ops?.clientId || '') .set('jpo', ops?.jobsByPilot || 'false') .set('status', ops?.status || ''); if (ops?.byTime?.length === 2) { for (const time of ops.byTime) { if (time) { _ops = _ops.append('byTime', time.toISOString()); } } } else { _ops = _ops.append('byTime', ops?.byTime[0] || ''); } return this.http.get(this.jobURL, { params: _ops }); } getJob(id: number, withItems: boolean = false, withLines?: boolean): Observable { let httpParams = new HttpParams().set('withItems', withItems.toString()); if (withLines) httpParams = httpParams.append('withLines', withItems.toString()); return this.http.get(`${this.jobURL}/${id}`, { params: httpParams }).pipe(map(res => toJob(res))); } saveJob(updateOps: UpdateJobOps): Observable { return updateOps.job._id !== 0 ? this.updateJob(updateOps) : this.createJob(updateOps.job); } createJob(job: IJob): Observable { return this.http.post(this.jobURL, job).pipe(map(j => toJob(j))); } private updateJob(updateOps: UpdateJobOps): Observable { return this.http.put(`${this.jobURL}/${updateOps.job._id}`, updateOps); } deleteJob(job: IJob) { return this.http.delete(`${this.jobURL}/${job._id}`); } downloadObs(options?: any) { return this.http.post(`/exports/downloadObs`, options, { responseType: 'arraybuffer' }).pipe( map(res => { return new Blob([res], { type: 'plain/text' }); })); } printAppReport(options) { return this.http.post(`${this.jobURL}/printAppReport`, options, { responseType: 'arraybuffer' }).pipe( map(res => { return new Blob([res], { type: 'application/pdf' }); })); } preAppReport(options) { return this.http.post(`${this.jobURL}/preAppReport`, options); } preLoadReport(options) { return this.http.post(`${this.jobURL}/preLoadReport`, options); } getRptVars(options) { return this.http.post(`${this.jobURL}/getRptVars`, options); } setRptVars(options) { return this.http.post(`${this.jobURL}/setRptVars`, options); } downloadJob(options: any) { return this.http.post(`/exports/downloadJob`, options, { responseType: 'arraybuffer' }).pipe( map(res => { return new Blob([res], { type: 'application/zip' }); })); } downloadJobwMap(options: any) { return this.http.post(`/exports/downloadMap`, options, { responseType: 'arraybuffer' }).pipe( map(res => { return new Blob([res], { type: 'application/zip' }); })); } loadJobData(options: any) { return this.http.post(`${this.jobURL}/getdata`, options); } getUploadedFiles(options: any) { return this.http.post(`${this.jobURL}/getUploadedFiles`, options, { params: new HttpParams().set('loader', 'false') }); } deleteAppFile(options: any) { return this.http.post(`${this.jobURL}/deleteAppFile`, options, { params: new HttpParams().set('loader', 'false') }); } fetchInvReadyJobs(excludeIds?: string[]): Observable { return this.http.post(`${this.jobURL}/fetchInvReadyJobs`, { excludeIds }); } downloadAppFile(fname) { let httpParams = new HttpParams().set('file', fname); return this.http.get('/exports/downloadAppfile', { params: httpParams, responseType: 'arraybuffer' }).pipe( map(res => { return new Blob([res], { type: 'application/zip' }); }));; } getImportStatus(id: string): Observable { const options = { appId: id }; return this.http.post(`${this.jobURL}/importStatus`, options, { params: new HttpParams().set('loader', 'false') }); } getImportingStatus(ops) { return this.http.post(`${this.jobURL}/importingStatus`, ops, { params: new HttpParams().set('loader', 'false') }); } cancelImporting(appIds) { return this.http.post<[any]>('/imports/cancelImport', { appIds: appIds }, { params: new HttpParams().set('loader', 'true') }); } getJobLogs(options: any) { return this.http.post(`${this.jobURL}/getJobLogs`, options, { params: new HttpParams().set('loader', 'false') }).pipe( map(res => { for (const it of res) it.date = new Date(it.date); return res; })); } assign(options: any): Observable { return this.http.post(`${this.jobURL}/assign`, options) } getAssignments(options: any): any { return this.http.post(`${this.jobURL}/assignments`, options, { params: new HttpParams().set('loader', 'false') }) } getRptOps(jobId: number) { return this.http.post(`${this.jobURL}/reportOps`, { jobId: jobId }); } saveReport(rid, rptJson) { return this.http.post(`${this.jobURL}/saveReport`, { rid: rid, content: rptJson }); } countByClient(id: any): Observable { return this.http.post(`${this.jobURL}/countByClient`, { clientId: id }); } getLines(options: any): Observable<[LineResult]> { return this.http.post<[LineResult]>('/geoutils/genLines', options); } saveJobMapOps(options) { return this.http.post(`${this.jobURL}/saveMapOps`, options); } searchJobs(options): Observable { return this.http.post(`${this.jobURL}/searchJobs`, options, { params: new HttpParams().set('loader', 'false') }); } getAppFiles(jobId: number) { return this.http.post(`${this.jobURL}/appFiles`, { jobId: jobId }); } getFilesData(fileId: string, params?: { limit?: number, startingAfter?: string, endingBefore?: string, returnAll?: boolean }) { const body: any = { fileId: fileId }; if (params) { if (params.limit !== undefined) body.limit = params.limit; if (params.startingAfter !== undefined) body.startingAfter = params.startingAfter; if (params.endingBefore !== undefined) body.endingBefore = params.endingBefore; if (params.returnAll !== undefined) body.returnAll = params.returnAll; } return this.http.post(`${this.jobURL}/filesdata`, body); } } export interface LineResult { id: string; isNew: boolean; lines: any[]; heading: number; }