-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle.service.ts
75 lines (69 loc) · 2.65 KB
/
cycle.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
import type { HttpErrorResponse } from '@angular/common/http'
import { HttpClient } from '@angular/common/http'
import { inject, Injectable } from '@angular/core'
import type { Observable } from 'rxjs'
import { BehaviorSubject, catchError, map, tap } from 'rxjs'
import { OrganizationService } from '@seed/api/organization'
import { ErrorService } from '@seed/services/error/error.service'
import { SnackbarService } from 'app/core/snackbar/snackbar.service'
import type { Cycle, CycleResponse, CyclesResponse } from './cycle.types'
@Injectable({ providedIn: 'root' })
export class CycleService {
private _httpClient = inject(HttpClient)
private _organizationService = inject(OrganizationService)
private _snackBar = inject(SnackbarService)
private _errorService = inject(ErrorService)
private _cycles = new BehaviorSubject<Cycle[]>([])
orgId: number
cycles$ = this._cycles.asObservable()
get(): void {
// fetch current organization
this._organizationService.currentOrganization$.subscribe(({ org_id }) => {
this.orgId = org_id
const url = `/api/v3/cycles/?organization_id=${org_id}`
// fetch cycles
this._httpClient
.get<CyclesResponse>(url)
.pipe(
map((response) => response.cycles),
tap((cycles) => {
this._cycles.next(cycles)
}),
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, 'Error fetching cycles')
}),
)
.subscribe()
})
}
post({ data, orgId }): Observable<CycleResponse | null> {
const url = `/api/v3/cycles/?organization_id=${orgId}`
return this._httpClient.post<CycleResponse>(url, data).pipe(
tap((response) => {
this._snackBar.success(`Created Cycle ${response.cycles.name}`)
}),
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, 'Error creating cycle')
}),
)
}
put({ data, id, orgId }): Observable<CycleResponse | null> {
const url = `/api/v3/cycles/${id}/?organization_id=${orgId}`
return this._httpClient.put<CycleResponse>(url, data).pipe(
tap((response) => {
this._snackBar.success(`Updated Cycle ${response.cycles.name}`)
}),
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, 'Error updating cycle')
}),
)
}
delete(id: number, orgId: number) {
const url = `/api/v3/cycles/${id}/?organization_id=${orgId}`
return this._httpClient.delete(url).pipe(
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, 'Error deleting cycle')
}),
)
}
}