]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { SortMeta } from 'primeng/api' |
2 | import { Observable } from 'rxjs' | |
db400f44 | 3 | import { catchError, map } from 'rxjs/operators' |
5cd80545 C |
4 | import { HttpClient, HttpParams } from '@angular/common/http' |
5 | import { Injectable } from '@angular/core' | |
67ed6552 C |
6 | import { RestExtractor, RestPagination, RestService } from '@app/core' |
7 | import { Job, ResultList } from '@shared/models' | |
63c4db6d | 8 | import { environment } from '../../../../environments/environment' |
42712121 | 9 | import { JobStateClient } from '../../../../types/job-state-client.type' |
1061c73f | 10 | import { JobTypeClient } from '../../../../types/job-type-client.type' |
5cd80545 C |
11 | |
12 | @Injectable() | |
13 | export class JobService { | |
63c4db6d | 14 | private static BASE_JOB_URL = environment.apiUrl + '/api/v1/jobs' |
5cd80545 C |
15 | |
16 | constructor ( | |
17 | private authHttp: HttpClient, | |
18 | private restService: RestService, | |
19 | private restExtractor: RestExtractor | |
20 | ) {} | |
21 | ||
42712121 | 22 | getJobs (jobState: JobStateClient, jobType: JobTypeClient, pagination: RestPagination, sort: SortMeta): Observable<ResultList<Job>> { |
5cd80545 C |
23 | let params = new HttpParams() |
24 | params = this.restService.addRestGetParams(params, pagination, sort) | |
25 | ||
1061c73f C |
26 | if (jobType !== 'all') params = params.append('jobType', jobType) |
27 | ||
42712121 | 28 | return this.authHttp.get<ResultList<Job>>(JobService.BASE_JOB_URL + '/' + jobState, { params }) |
db400f44 | 29 | .pipe( |
94831479 C |
30 | map(res => { |
31 | return this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ]) | |
32 | }), | |
db400f44 | 33 | map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData)), |
94831479 | 34 | map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId)), |
db400f44 C |
35 | catchError(err => this.restExtractor.handleError(err)) |
36 | ) | |
5cd80545 | 37 | } |
cd83ea1b C |
38 | |
39 | private prettyPrintData (obj: Job) { | |
94a5ff8a | 40 | const data = JSON.stringify(obj.data, null, 2) |
cd83ea1b | 41 | |
94a5ff8a | 42 | return Object.assign(obj, { data }) |
cd83ea1b | 43 | } |
94831479 C |
44 | |
45 | private buildUniqId (obj: Job) { | |
46 | return Object.assign(obj, { uniqId: `${obj.id}-${obj.type}` }) | |
47 | } | |
5cd80545 | 48 | } |