|
| 1 | +import type { HttpErrorResponse } from '@angular/common/http' |
| 2 | +import { HttpClient } from '@angular/common/http' |
| 3 | +import { inject, Injectable } from '@angular/core' |
| 4 | +import type { Observable } from 'rxjs' |
| 5 | +import { BehaviorSubject, catchError, map, tap } from 'rxjs' |
| 6 | +import { ErrorService } from '@seed/services' |
| 7 | +import { SnackBarService } from 'app/core/snack-bar/snack-bar.service' |
| 8 | +import { UserService } from '../user' |
| 9 | +import type { Program, ProgramData, ProgramResponse, ProgramsResponse, ProgramUpsertPayload } from './program.types' |
| 10 | + |
| 11 | +@Injectable({ providedIn: 'root' }) |
| 12 | +export class ProgramService { |
| 13 | + private _httpClient = inject(HttpClient) |
| 14 | + private _programs = new BehaviorSubject<Program[]>([]) |
| 15 | + // private _programs = new ReplaySubject<Program[]>(1) |
| 16 | + private _errorService = inject(ErrorService) |
| 17 | + private _snackBar = inject(SnackBarService) |
| 18 | + private _userService = inject(UserService) |
| 19 | + programs$ = this._programs |
| 20 | + orgId: number |
| 21 | + |
| 22 | + constructor() { |
| 23 | + this._userService.currentOrganizationId$ |
| 24 | + .pipe( |
| 25 | + tap((orgId) => { |
| 26 | + this.list(orgId) |
| 27 | + }), |
| 28 | + ) |
| 29 | + .subscribe() |
| 30 | + } |
| 31 | + |
| 32 | + list(orgId: number) { |
| 33 | + const url = `/api/v3/compliance_metrics/?organization_id=${orgId}` |
| 34 | + this._httpClient |
| 35 | + .get<ProgramsResponse>(url) |
| 36 | + .pipe( |
| 37 | + map(({ compliance_metrics }) => { |
| 38 | + this.programs$.next(compliance_metrics) |
| 39 | + return compliance_metrics |
| 40 | + }), |
| 41 | + catchError((error: HttpErrorResponse) => { |
| 42 | + return this._errorService.handleError(error, 'Error fetching Programs') |
| 43 | + }), |
| 44 | + ) |
| 45 | + .subscribe() |
| 46 | + } |
| 47 | + |
| 48 | + create(orgId: number, data: ProgramUpsertPayload): Observable<ProgramResponse> { |
| 49 | + const url = `/api/v3/compliance_metrics/?organization_id=${orgId}` |
| 50 | + const payload = this._normalizePayload(data) |
| 51 | + return this._httpClient.post<ProgramResponse>(url, payload).pipe( |
| 52 | + tap(() => { |
| 53 | + this.list(orgId) |
| 54 | + this._snackBar.success('Successfully created Program') |
| 55 | + }), |
| 56 | + catchError((error: HttpErrorResponse) => { |
| 57 | + return this._errorService.handleError(error, 'Error creating Program') |
| 58 | + }), |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + update(orgId: number, programId: number, data: ProgramUpsertPayload): Observable<ProgramResponse> { |
| 63 | + const url = `/api/v3/compliance_metrics/${programId}/?organization_id=${orgId}` |
| 64 | + const payload = this._normalizePayload(data) |
| 65 | + return this._httpClient.put<ProgramResponse>(url, payload).pipe( |
| 66 | + tap(() => { |
| 67 | + this.list(orgId) |
| 68 | + this._snackBar.success('Successfully updated Program') |
| 69 | + }), |
| 70 | + catchError((error: HttpErrorResponse) => { |
| 71 | + return this._errorService.handleError(error, 'Error updating Program') |
| 72 | + }), |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + delete(orgId: number, programId: number): Observable<ProgramResponse> { |
| 77 | + const url = `/api/v3/compliance_metrics/${programId}/?organization_id=${orgId}` |
| 78 | + return this._httpClient.delete<ProgramResponse>(url).pipe( |
| 79 | + tap(() => { |
| 80 | + this.list(orgId) |
| 81 | + this._snackBar.success('Successfully deleted Program') |
| 82 | + }), |
| 83 | + catchError((error: HttpErrorResponse) => { |
| 84 | + return this._errorService.handleError(error, 'Error deleting Program') |
| 85 | + }), |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + evaluate(orgId: number, programId: number, aliId: number = null): Observable<ProgramData> { |
| 90 | + let url = `/api/v3/compliance_metrics/${programId}/evaluate/?organization_id=${orgId}` |
| 91 | + if (aliId) url += `&access_level_instance_id=${aliId}` |
| 92 | + |
| 93 | + return this._httpClient.get<{ data: ProgramData }>(url).pipe( |
| 94 | + map(({ data }) => data), |
| 95 | + catchError((error: HttpErrorResponse) => { |
| 96 | + return this._errorService.handleError(error, 'Error evaluating Program') |
| 97 | + }), |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + private _normalizePayload(data: ProgramUpsertPayload): ProgramUpsertPayload { |
| 102 | + const payload = { ...data } as ProgramUpsertPayload & Partial<Program> |
| 103 | + delete payload.organization_id |
| 104 | + delete payload.id |
| 105 | + delete payload.energy_bool |
| 106 | + delete payload.emission_bool |
| 107 | + |
| 108 | + return { |
| 109 | + ...payload, |
| 110 | + actual_emission_column: payload.actual_emission_column ?? null, |
| 111 | + actual_energy_column: payload.actual_energy_column ?? null, |
| 112 | + cycles: payload.cycles ?? [], |
| 113 | + emission_metric_type: payload.emission_metric_type ?? '', |
| 114 | + energy_metric_type: payload.energy_metric_type ?? '', |
| 115 | + filter_group: payload.filter_group ?? null, |
| 116 | + target_emission_column: payload.target_emission_column ?? null, |
| 117 | + target_energy_column: payload.target_energy_column ?? null, |
| 118 | + x_axis_columns: payload.x_axis_columns ?? [], |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments