Untitled

 avatar
unknown
plain_text
2 years ago
4.2 kB
4
Indexable
import { Injectable } from '@angular/core';
import { Params, Router } from '@angular/router';
import { AppState } from '@app/store/state/app.state';
import { Line, MenuSelectedPlant, Plant } from '@gestamp/core-components';
import { PlantSelectionData, selectedPlantActions } from '@gestamp/core-services';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { BehaviorSubject } from 'rxjs';
import { CommonService, ToastSeverities } from '../common/common.service';

@Injectable({
  providedIn: 'root'
})
export class PlantSelectionService {

  public plants$: BehaviorSubject<Plant[]> = new BehaviorSubject<Plant[]>([]);
  public platformRole$: BehaviorSubject<string> = new BehaviorSubject<string>(null);

  constructor(
    private readonly router: Router,
    private readonly commonService: CommonService,
    private readonly translateService: TranslateService,
    private readonly store: Store<AppState>
  ) { }

  public async prepareAndChangePlant(plants?: Plant[], plantCode?: string, platformRole?: string, params?: Params) {
    const selectedData: MenuSelectedPlant = {
      plant: {
        id: plantCode, code: plantCode, name: plantCode, timeZone: null, lines: null, role: null
      },
      line: null,
      cell: null
    };
    const changedPlant = this.prepareChangedPlantSelectionData(plants, selectedData, platformRole);
    if (changedPlant) {
      this.store.dispatch(selectedPlantActions.setCurrentPlantData({ plantSelectionData: changedPlant, showToast: true }));
      this.showChangeCurrentPlantSelectionToast(changedPlant);
      // this.menuService.setAllowedMenuForUser([]);

      await this.commonService.reloadPage();

      this.router.navigate(['/forms/form-data-history'], { queryParams: { PlantCode: plantCode, FormId: params.formId, RegistryId: params.registryIdUrl } });
    } else {
      this.commonService.riseToast([
        {
          severity: ToastSeverities.Error,
          summary: '',
          detail: this.translateService.instant('common.toasts.change_plant_target_error')
        }
      ]);
      localStorage.removeItem('PlantCodeURL');
      localStorage.removeItem('FormIdURL');
      localStorage.removeItem('FormTagURL');
      localStorage.removeItem('RegistryIdURL');
    }
  }

  public prepareChangedPlantSelectionData(plants: Plant[], selectedData: MenuSelectedPlant, platformRole?: string): PlantSelectionData {
    const selectedPlant = plants.find((plant: Plant) => plant.id === selectedData.plant.id);
    let selectedLine;

    if (selectedData.line) {
      selectedLine = selectedPlant.lines.find((line: Line) => line.id === selectedData.line.id);
    }
    return selectedPlant ? this.preparePlantSelectionData(selectedPlant, selectedLine, platformRole) : null;
  }

  public preparePlantSelectionData(plant: Plant, line: Line = null, platformRole?: string): PlantSelectionData {
    const seriesMeasurement = this.formatSeriesMeasurement(plant, line);
    const roleOfUser = this.resolveRoleOfUser(plant, line, platformRole);

    return {
      plantId: plant.id,
      SeriesDb: plant.code,
      SeriesMeasurement: seriesMeasurement,
      plantAbbreviation: plant.code,
      plantName: plant.name,
      plantCountry: null,
      plantTimeZone: plant.timeZone,
      selectedLine: line,
      role: roleOfUser
    };
  }

  public showChangeCurrentPlantSelectionToast(data: PlantSelectionData): void {
    const summary: string = this.translateService.instant('header.plant_line_change');

    const detail: string =
      this.translateService.instant('header.plant_change_to') +
      ` ${data.plantName}` +
      `${(data.selectedLine ? ' - ' + data.selectedLine.name : '')}`;

    this.commonService.riseToast([{ severity: ToastSeverities.Info, summary, detail }]);
  }

  private formatSeriesMeasurement(plant: Plant, line: Line): string {
    return plant.code + `.${line ? line.name : plant.name}`;
  }

  private resolveRoleOfUser(plant: Plant, line: Line, platformRole?: string): string {
    return (line && line.role) || (plant && plant.role) || platformRole;
  }

}
Editor is loading...