1 import { SortMeta } from 'primeng/api'
2 import { Observable } from 'rxjs'
3 import { catchError, map } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService } from '@app/core'
7 import { Job, ResultList } from '@shared/models'
8 import { environment } from '../../../../environments/environment'
9 import { JobStateClient } from '../../../../types/job-state-client.type'
10 import { JobTypeClient } from '../../../../types/job-type-client.type'
13 export class JobService {
14 private static BASE_JOB_URL = environment.apiUrl + '/api/v1/jobs'
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
23 jobState?: JobStateClient
24 jobType: JobTypeClient
25 pagination: RestPagination
27 }): Observable<ResultList<Job>> {
28 const { jobState, jobType, pagination, sort } = options
30 let params = new HttpParams()
31 params = this.restService.addRestGetParams(params, pagination, sort)
33 if (jobType !== 'all') params = params.append('jobType', jobType)
35 return this.authHttp.get<ResultList<Job>>(JobService.BASE_JOB_URL + `/${jobState || ''}`, { params })
37 map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ], 'precise')),
38 map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData)),
39 map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId)),
40 catchError(err => this.restExtractor.handleError(err))
44 private prettyPrintData (obj: Job) {
45 const data = JSON.stringify(obj.data, null, 2)
47 return Object.assign(obj, { data })
50 private buildUniqId (obj: Job) {
51 return Object.assign(obj, { uniqId: `${obj.id}-${obj.type}` })