|
| 1 | +import type { HttpErrorResponse } from '@angular/common/http' |
| 2 | +import { HttpClient } from '@angular/common/http' |
| 3 | +import { inject, Injectable } from '@angular/core' |
| 4 | +import { BehaviorSubject, catchError, map, type Observable, Subject, takeUntil, tap } from 'rxjs' |
| 5 | +import { ErrorService } from '@seed/services' |
| 6 | +import { SnackBarService } from 'app/core/snack-bar/snack-bar.service' |
| 7 | +import { OrganizationService } from '../organization' |
| 8 | +import type { InventoryGroup, InventoryGroupResponse, InventoryGroupsResponse } from './groups.types' |
| 9 | + |
| 10 | +@Injectable({ providedIn: 'root' }) |
| 11 | +export class GroupsService { |
| 12 | + private _errorService = inject(ErrorService) |
| 13 | + private _groups = new BehaviorSubject<unknown>([]) |
| 14 | + private _httpClient = inject(HttpClient) |
| 15 | + private _organizationService = inject(OrganizationService) |
| 16 | + private _snackBar = inject(SnackBarService) |
| 17 | + private readonly _unsubscribeAll$ = new Subject<void>() |
| 18 | + |
| 19 | + groups$ = this._groups.asObservable() |
| 20 | + orgId: number |
| 21 | + |
| 22 | + constructor() { |
| 23 | + this._organizationService.currentOrganization$ |
| 24 | + .pipe(takeUntil(this._unsubscribeAll$)) |
| 25 | + .subscribe(({ org_id }) => this.orgId = org_id) |
| 26 | + } |
| 27 | + |
| 28 | + list(orgId: number): Observable<InventoryGroup[]> { |
| 29 | + const url = `/api/v3/inventory_groups/?organization_id=${orgId}` |
| 30 | + return this._httpClient.get<InventoryGroupsResponse>(url).pipe( |
| 31 | + map(({ data }) => { |
| 32 | + this._groups.next(data) |
| 33 | + return data |
| 34 | + }), |
| 35 | + catchError((error: HttpErrorResponse) => { |
| 36 | + return this._errorService.handleError(error, 'Error fetching groups') |
| 37 | + }), |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + create(orgId: number, data: InventoryGroup): Observable<InventoryGroup> { |
| 42 | + const url = `/api/v3/inventory_groups/?organization_id=${orgId}` |
| 43 | + return this._httpClient.post<InventoryGroupResponse>(url, data).pipe( |
| 44 | + map(({ data }) => { |
| 45 | + this._snackBar.success('Group created successfully') |
| 46 | + return data |
| 47 | + }), |
| 48 | + catchError((error: HttpErrorResponse) => { |
| 49 | + return this._errorService.handleError(error, 'Error updating group') |
| 50 | + }), |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + update(orgId: number, id: number, data: InventoryGroup): Observable<InventoryGroup> { |
| 55 | + const url = `/api/v3/inventory_groups/${id}/?organization_id=${orgId}` |
| 56 | + return this._httpClient.put<InventoryGroupResponse>(url, data).pipe( |
| 57 | + map(({ data }) => { |
| 58 | + this._snackBar.success('Group updated successfully') |
| 59 | + return data |
| 60 | + }), |
| 61 | + catchError((error: HttpErrorResponse) => { |
| 62 | + return this._errorService.handleError(error, 'Error updating group') |
| 63 | + }), |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + delete(orgId: number, id: number): Observable<unknown> { |
| 68 | + const url = `/api/v3/inventory_groups/${id}/?organization_id=${orgId}` |
| 69 | + return this._httpClient.delete(url).pipe( |
| 70 | + tap(() => { this._snackBar.success('Group deleted successfully') }), |
| 71 | + catchError((error: HttpErrorResponse) => { |
| 72 | + return this._errorService.handleError(error, 'Error deleting group') |
| 73 | + }), |
| 74 | + ) |
| 75 | + } |
| 76 | +} |
0 commit comments