All files / app/services/inwork inwork-api.service.ts

100% Statements 20/20
100% Branches 8/8
100% Functions 9/9
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 723x 3x 3x 3x     3x         3x 10x 10x 10x     4x   4x               4x           3x             3x           1x                 1x       1x                 1x      
import { environment } from '../../../environments/environment';
import { HttpClient, HttpContext, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { SKIP_ERROR_NOTIFICATION } from '@drevo-web/core';
import { ApiResponse, InworkCheckResponseDto, InworkItemDto } from '@drevo-web/shared';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
 
@Injectable({
    providedIn: 'root',
})
export class InworkApiService {
    private readonly apiUrl = environment.apiUrl;
    private readonly http = inject(HttpClient);
    private readonly skipErrorContext = new HttpContext().set(SKIP_ERROR_NOTIFICATION, true);
 
    check(module: string, title: string): Observable<InworkCheckResponseDto> {
        const params = new HttpParams().set('module', module).set('title', title);
 
        return this.http
            .get<ApiResponse<InworkCheckResponseDto>>(`${this.apiUrl}/api/inwork/check`, {
                params,
                withCredentials: true,
                context: this.skipErrorContext,
            })
            .pipe(
                map(response => {
                    return response.data ?? { editor: undefined };
                }),
            );
    }
 
    getList(): Observable<InworkItemDto[]> {
        return this.http
            .get<ApiResponse<InworkItemDto[]>>(`${this.apiUrl}/api/inwork/list`, {
                withCredentials: true,
                context: this.skipErrorContext,
            })
            .pipe(
                map(response => {
                    return response.data ?? [];
                }),
            );
    }
 
    markEditing(module: string, title: string, versionId: number): Observable<void> {
        return this.http
            .post<ApiResponse<undefined>>(
                `${this.apiUrl}/api/inwork/mark`,
                { module, title, versionId },
                {
                    withCredentials: true,
                    context: this.skipErrorContext,
                },
            )
            .pipe(map(() => undefined));
    }
 
    clearEditing(module: string, title: string): Observable<void> {
        return this.http
            .post<ApiResponse<undefined>>(
                `${this.apiUrl}/api/inwork/clear`,
                { module, title },
                {
                    withCredentials: true,
                    context: this.skipErrorContext,
                },
            )
            .pipe(map(() => undefined));
    }
}