Commit e7eda683 authored by Jie Yuan's avatar Jie Yuan
Browse files

add service component service

parent 42e1fd5b
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
import { TestBed } from '@angular/core/testing';

import { ServiceComponentService } from './service-component.service';

describe('ServiceComponentService', () => {
  beforeEach(() => TestBed.configureTestingModule({}));

  it('should be created', () => {
    const service: ServiceComponentService = TestBed.get(ServiceComponentService);
    expect(service).toBeTruthy();
  });
});
+95 −0
Original line number Diff line number Diff line
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';


import {ServiceComponent} from './model/service-component';
import {ServiceComponentRecord} from './model/service-component-record';
import {ServiceComponentRow} from './model/service-component-row';
import {AuthService} from './auth.service';
import {SharedService} from './shared-service';




@Injectable()
export class ServiceComponentService {


  selectedServiceComponent: ServiceComponent;
  selectedServiceComponentHost: string;

  constructor(private httpClient: HttpClient, private authSevice: AuthService, private sharedService: SharedService) {  }

  public canEnterToServComponent(): boolean {
    if (this.selectedServiceComponent != null) {
      return true; } else {return false; }
  }
  public setSelectedServComp(swProductRow: ServiceComponentRow) {
    console.log('Selected new serviceCompnent: ', swProductRow);
    this.selectedServiceComponent = new ServiceComponent(swProductRow.servCompName);
    this.selectedServiceComponent.tagAtSite = swProductRow.servCompTagAtSite;
    this.selectedServiceComponent.serviceType = swProductRow.servCompServiceType;
    this.selectedServiceComponent.productionTag = swProductRow.servCompProdTag;
    this.selectedServiceComponent.createdTime = swProductRow.servCompCreatedTime;

    this.selectedServiceComponentHost = swProductRow.hostName;
    /** We fetch the records for this service component **/
    this.getServCompRecords(swProductRow.servCompName, swProductRow.hostName)
      .then(servCompRecords => {
        console.log('Serv comp reslts: ', servCompRecords);
        sessionStorage.setItem('currentServComp', JSON.stringify(this.selectedServiceComponent));
      })
      .catch(error => {
        console.log('Error while tried to fetch service component records ', error);
      });

  }

  public getSelectedServComp() {
    return this.selectedServiceComponent;
  }

  public getSelectedServCompHost(){
    return this.selectedServiceComponentHost;
  }

 

  public getServCompRecords(servCompName: string, servCompHostName: string) {
    console.log('Fetching service component records from backend');
    const promise = new Promise(
      (resolve, reject) => {
        /** Gets the sites from the source specified (GOCDB or DPMT) **/
        const getUrl = this.sharedService.serviceCompURL + servCompHostName + '/' + servCompName;
        if ( sessionStorage.getItem('authHeader') !== '') {
          const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
           'Authorization' : sessionStorage.getItem('authHeader'),
          'Content-Type' : 'application/json'
         } : {}
         );
          this.httpClient.get<ServiceComponentRecord[]>(getUrl, {headers : headers} )
          .subscribe(
            (servComponentRecords: ServiceComponentRecord[]) => {
              const servCompRecords = servComponentRecords;
              servComponentRecords.sort( function(a,b) {
                if( a.lastChangedTime < b.lastChangedTime )
                {
                  return 1;
                }else
                {
                  return 0;
                }
              });
              this.selectedServiceComponent.serviceComponentRecords = servComponentRecords;
              resolve(servCompRecords);
            },
            (error) => {
              reject(error);
            }
          );
        }
      });
    return promise;
  }

}
 No newline at end of file