-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit-template.service.ts
93 lines (88 loc) · 3.9 KB
/
audit-template.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { HttpClient, type HttpErrorResponse } from '@angular/common/http'
import { inject, Injectable } from '@angular/core'
import { catchError, map, type Observable, ReplaySubject, Subject, takeUntil } from 'rxjs'
import { ErrorService } from '@seed/services/error/error.service'
import { UserService } from '../user'
import type {
AuditTemplateConfig,
AuditTemplateConfigCreateResponse,
AuditTemplateConfigResponse,
AuditTemplateReportType,
} from './audit-template.types'
@Injectable({ providedIn: 'root' })
export class AuditTemplateService {
private _httpClient = inject(HttpClient)
private _userService = inject(UserService)
private _errorService = inject(ErrorService)
private readonly _unsubscribeAll$ = new Subject<void>()
private _reportTypes = new ReplaySubject<AuditTemplateReportType[]>(1)
private _auditTemplateConfig = new ReplaySubject<AuditTemplateConfig>(1)
reportTypes$ = this._reportTypes.asObservable()
auditTemplateConfig$ = this._auditTemplateConfig.asObservable()
constructor() {
this._reportTypes.next([
{ name: 'ASHRAE Level 2 Report' }, // cspell:disable-line
{ name: 'Atlanta Report' },
{ name: 'Baltimore Energy Audit Report' },
{ name: 'Berkeley Report' },
{ name: 'BRICR Phase 0/1' },
{ name: 'Brisbane Energy Audit Report' },
{ name: 'DC BEPS Energy Audit Report' }, // cspell:disable-line
{ name: 'DC BEPS RCx Report' }, // cspell:disable-line
{ name: 'Demo City Report' },
{ name: 'Denver Energy Audit Report' },
{ name: 'EE-RLF Template' },
{ name: 'Energy Trust of Oregon Report' },
{ name: 'Los Angeles Report' },
{ name: 'Minneapolis Energy Evaluation Report' },
{ name: 'New York City Energy Efficiency Report' },
{ name: 'Office of Recapitalization Energy Audit Report' },
{ name: 'Open Efficiency Report' },
{ name: 'San Francisco Report' },
{ name: 'St. Louis RCx Report' },
{ name: 'St. Louis Report' },
{ name: 'WA Commerce Clean Buildings - Form D Report' },
{ name: 'WA Commerce Grants Report' },
])
this._userService.currentOrganizationId$.pipe(takeUntil(this._unsubscribeAll$)).subscribe((organizationId) => {
this.getConfigs(organizationId).subscribe()
})
}
getConfigs(org_id: number): Observable<AuditTemplateConfig> {
const url = `/api/v3/audit_template_configs/?organization_id=${org_id}`
return this._httpClient.get<AuditTemplateConfigResponse>(url).pipe(
map((response) => {
this._auditTemplateConfig.next(response.data[0])
return response.data[0]
}),
catchError((error: HttpErrorResponse) => {
// TODO need to figure out error handling
return this._errorService.handleError(error, 'Error fetching audit template configs')
}),
)
}
create(auditTemplateConfig: AuditTemplateConfig): Observable<AuditTemplateConfig> {
const url = `/api/v3/audit_template_configs/?organization_id=${auditTemplateConfig.organization}`
return this._httpClient.post<AuditTemplateConfigCreateResponse>(url, { ...auditTemplateConfig }).pipe(
map((r) => {
this._auditTemplateConfig.next(r.data)
return r.data
}),
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, 'Error updating Audit Template Config')
}),
)
}
update(auditTemplateConfig: AuditTemplateConfig): Observable<AuditTemplateConfig | null> {
const url = `/api/v3/audit_template_configs/${auditTemplateConfig.id}/?organization_id=${auditTemplateConfig.organization}`
return this._httpClient.put<AuditTemplateConfigResponse>(url, { ...auditTemplateConfig }).pipe(
map((r) => {
this._auditTemplateConfig.next(r.data[0])
return r.data[0]
}),
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, 'Error updating Audit Template Config')
}),
)
}
}