import { Injectable } from '@angular/core' ; import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { IKeyResult } from '@gauzy/contracts' ; import { Observable , throwError } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { firstValueFrom } from 'rxjs' ; import { ToastrService } from './toastr.service' ; import { API_PREFIX } from '../constants/app.constants' ; interface IKeyResultResponse { items : IKeyResult [ ] ; count : number ; } @ Injectable ( { providedIn : 'root' } ) export class KeyResultService { private readonly API_URL = `${ API_PREFIX }/key-results` ; constructor ( private _http , private toastrService ) { } createKeyResult ( keyResult ) { return firstValueFrom ( this . _http . post < IKeyResult > ( `${ this . API_URL }` , keyResult ) . pipe ( catchError ( ( error ) => this . errorHandler ( error ) ) ) ) ; } createBulkKeyResult ( keyResults ) { return firstValueFrom ( this . _http . post < IKeyResult [ ] > ( `${ this . API_URL }/bulk` , keyResults ) . pipe ( catchError ( ( error ) => this . errorHandler ( error ) ) ) ) ; } async update ( id , keyResult ) { return firstValueFrom ( this . _http . put < IKeyResult > ( `${ this . API_URL }/${ id }` , keyResult ) ) ; } findKeyResult ( id ) { return firstValueFrom ( this . _http . get < IKeyResultResponse > ( `${ this . API_URL }/${ id }` ) . pipe ( catchError ( ( error ) => this . errorHandler ( error ) ) ) ) ; } getAllKeyResults ( keyResult ) { return this . _http . get < IKeyResultResponse > ( `${ this . API_URL }/${ keyResult }` ) . pipe ( catchError ( ( error ) => this . errorHandler ( error ) ) ) ; } delete ( id ) { return firstValueFrom ( this . _http . delete ( `${ this . API_URL }/${ id }` ) ) ; } errorHandler ( error ) { this . toastrService . danger ( error . message , 'Error' ) ; return throwError ( error . message ) ; } }