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

100% Statements 31/31
90% Branches 9/10
100% Functions 13/13
100% Lines 28/28

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 72 732x 2x 2x 2x   2x   2x         2x 14x 14x 14x     6x   5x 5x 1x   4x     1x 1x           3x 2x   1x 1x           2x   1x 1x           2x   1x 1x           1x                    
import { InworkApiService } from './inwork-api.service';
import { AuthService } from '../auth/auth.service';
import { Injectable, inject } from '@angular/core';
import { LoggerService } from '@drevo-web/core';
import { InworkItem, InworkItemDto } from '@drevo-web/shared';
import { EMPTY, Observable, catchError, map, of } from 'rxjs';
 
const INWORK_MODULE = 'articles';
 
@Injectable({
    providedIn: 'root',
})
export class InworkService {
    private readonly inworkApiService = inject(InworkApiService);
    private readonly authService = inject(AuthService);
    private readonly logger = inject(LoggerService).withContext('InworkService');
 
    getActiveEditor(title: string): Observable<string | undefined> {
        return this.inworkApiService.check(INWORK_MODULE, title).pipe(
            map(response => {
                const editor = response.editor || undefined;
                if (editor && editor === this.authService.currentUser?.name) {
                    return undefined;
                }
                return editor;
            }),
            catchError(err => {
                this.logger.error('Failed to check inwork status', err);
                return of(undefined);
            }),
        );
    }
 
    getInworkList(): Observable<InworkItem[]> {
        return this.inworkApiService.getList().pipe(
            map(items => items.map(dto => this.mapItem(dto))),
            catchError(err => {
                this.logger.error('Failed to get inwork list', err);
                return EMPTY;
            }),
        );
    }
 
    markEditing(title: string, versionId: number): Observable<void> {
        return this.inworkApiService.markEditing(INWORK_MODULE, title, versionId).pipe(
            catchError(err => {
                this.logger.error('Failed to mark article as editing', err);
                return EMPTY;
            }),
        );
    }
 
    clearEditing(title: string): Observable<void> {
        return this.inworkApiService.clearEditing(INWORK_MODULE, title).pipe(
            catchError(err => {
                this.logger.error('Failed to clear editing mark', err);
                return EMPTY;
            }),
        );
    }
 
    private mapItem(dto: InworkItemDto): InworkItem {
        return {
            id: dto.id,
            module: dto.module,
            title: dto.title,
            author: dto.author,
            lastTime: dto.lasttime,
            age: dto.age,
        };
    }
}