All files / src/services/build build.service.ts

46.51% Statements 20/43
0% Branches 0/4
45.45% Functions 5/11
42.5% Lines 17/40

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                1x     3x 3x 3x 3x 3x   3x 3x                                                           1x                                 1x   1x 1x   1x                                                               1x           1x 1x   1x      
import { HttpEventType } from '@angular/common/http';
import { EventEmitter, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { Build } from '../../models/build';
import { BuildLog } from '../../models/build-log';
import { ApiService, RestParameters } from '../api/api.service';
import { EnvironmentService } from '../environment/environment.service';
 
export interface BuildServiceLogsOptions {
  since?: string;
  tail?: number;
}
 
@Injectable({ providedIn: 'root' })
export class BuildService {
  public basePath: string;
 
  public onCreate = new EventEmitter<Build>();
  public onDelete = new EventEmitter<Build>();
  public onLogs = new EventEmitter<BuildLog[]>();
  public onRead = new EventEmitter<Build[]>();
  public onUpdate = new EventEmitter<Build>();
 
  constructor(private apiService: ApiService, private environmentService: EnvironmentService) {
    this.basePath = this.environmentService.buildApiBaseUrl;
  }
 
  public create(parameters: Partial<Build>, zip?: Blob) {
    const formData = new FormData();
    formData.append('record', JSON.stringify(parameters));
 
    // Only append zip field if supplied.
    if (zip) {
      formData.append('zip', zip);
    }
 
    const observable = this.apiService.request('post', this.basePath, formData, {
      observe: 'events',
      reportProgress: true,
    }) as Observable<any>;
    observable.pipe(
      map((event) => {
        if (event.type === HttpEventType.Response) {
          this.onCreate.emit(event.body.record);
        }
 
        return event;
      }),
    );
 
    return observable;
  }
 
  public async delete(_id: string): Promise<Build> {
    const response = await this.apiService.request('delete', `${this.basePath}/${_id}`, null);
 
    const record = new Build(response.record);
    this.onDelete.emit(record);
 
    return record;
  }
 
  public download(_id: string) {
    return this.apiService.request('get', `${this.basePath}/${_id}`, null, {
      observe: 'events',
      reportProgress: true,
      responseType: 'blob',
    }) as Observable<any>;
  }
 
  public async find(parameters: RestParameters): Promise<Build[]> {
    const response = await this.apiService.request('get', `${this.basePath}`, parameters);
 
    const records = response.records.map((record) => new Build(record));
    this.onRead.emit(records);
 
    return records;
  }
 
  public async findOne(_id: string): Promise<Build> {
    const response = await this.apiService.request('get', `${this.basePath}/${_id}`, null);
 
    const record = new Build(response.record);
    this.onRead.emit([record]);
 
    return record;
  }
 
  public async logs(
    _id: string,
    nodeId: string,
    parameters?: BuildServiceLogsOptions,
  ): Promise<BuildLog[]> {
    const response = await this.apiService.request(
      'get',
      `${this.basePath}/${_id}/logs/${nodeId}`,
      parameters,
    );
 
    const records = response.records.map(
      (record) => new BuildLog({ ...record, buildId: _id, nodeId }),
    );
    this.onLogs.emit(records);
 
    return records;
  }
 
  public async update(parameters: Partial<Build>): Promise<Build> {
    const response = await this.apiService.request(
      'put',
      `${this.basePath}/${parameters._id}`,
      parameters,
    );
 
    const record = new Build(response.record);
    this.onUpdate.emit(record);
 
    return record;
  }
}