Commit e647b1f1 authored by Pearu Sarv's avatar Pearu Sarv
Browse files

TEIS-2497 Replace hardcoded inspection types

parent 5cee797c
import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { DriversInspectionsFacade } from '@teis/features/drivers-inspections/drivers-inspections-facade.service';
import { InspectedItem, NumberOfInspectedItemsDto, CreateOrEditNumberOfInspectedItemsDto } from '@teis/features/drivers-inspections/services/drivers-inspections.model';
import { InspectedItem, NumberOfInspectedItemsDto, CreateOrEditNumberOfInspectedItemsDto, DriversInspectionType } from '@teis/features/drivers-inspections/services/drivers-inspections.model';
import { triggerValidation } from '@teis/utils';
import { ModalService } from '@teis/widgets/teis-modal/modal.service';
import { Subject } from 'rxjs';
......@@ -17,6 +17,7 @@ export class AddOrEditNumberOfInspectedItemsComponent implements OnInit {
@Input() numberOfInspectedItems: NumberOfInspectedItemsDto;
@Input() item: InspectedItem;
@Input() inspectionType: DriversInspectionType;
private destroy$ = new Subject<boolean>();
......@@ -32,8 +33,7 @@ export class AddOrEditNumberOfInspectedItemsComponent implements OnInit {
this.numbersForm = this.fb.group(
{
generallyWithoutM: [this.numberOfInspectedItems?.generallyWithoutM ?? 0],
// TODO: remove hardcode when BE changed
...(true
...(this.inspectionType === DriversInspectionType.GOODS
? { goodsAM: [this.numberOfInspectedItems?.goodsAM ?? 0], goodsDM: [this.numberOfInspectedItems?.goodsDM ?? 0], goodsSM: [this.numberOfInspectedItems?.goodsSM ?? 0] }
: {
passengersAM: [this.numberOfInspectedItems?.passengersAM ?? 0],
......@@ -48,7 +48,7 @@ export class AddOrEditNumberOfInspectedItemsComponent implements OnInit {
ownPurposesSM: [this.numberOfInspectedItems?.ownPurposesDM ?? 0],
},
{
validators: [numberOfInspectedItemsSumsValidator],
validators: [numberOfInspectedItemsSumsValidator(this.inspectionType)],
}
);
}
......
import { FormGroup, ValidationErrors } from '@angular/forms';
import { DriversInspectionType } from '@teis/features/drivers-inspections/services/drivers-inspections.model';
const sumsMatch = (group: FormGroup, type: 'AM' | 'DM' | 'SM'): boolean => {
const goodsValue = group.value[`goods${type}`] ?? 0;
......@@ -21,19 +22,21 @@ const isValidForType = (group: FormGroup, type: 'AM' | 'DM' | 'SM') => {
return true;
};
export const numberOfInspectedItemsSumsValidator = (group: FormGroup): ValidationErrors | null => {
const invalidSums = [];
if (!isValidForType(group, 'AM')) {
invalidSums.push('AM');
}
if (!isValidForType(group, 'DM')) {
invalidSums.push('DM');
}
if (!isValidForType(group, 'SM')) {
invalidSums.push('SM');
}
if (invalidSums.length) {
return { sum_error: `(${invalidSums.join(', ')})` };
}
return null;
export const numberOfInspectedItemsSumsValidator = (inspectionType: DriversInspectionType) => {
return (group: FormGroup): ValidationErrors | null => {
const invalidSums = [];
if (!isValidForType(group, 'AM')) {
invalidSums.push('AM');
}
if (!isValidForType(group, 'DM')) {
invalidSums.push('DM');
}
if (!isValidForType(group, 'SM')) {
invalidSums.push('NM');
}
if (invalidSums.length) {
return { [`sum_error_${inspectionType}`]: `(${invalidSums.join(', ')})` };
}
return null;
};
};
......@@ -32,7 +32,7 @@ export class NumberOfInspectedItemsTableComponent implements OnInit {
}
openEditInspectedItemsModal(numberOfInspectedItems: NumberOfInspectedItemsDto): void {
this.modal.openWithComponent(AddOrEditNumberOfInspectedItemsComponent, { numberOfInspectedItems });
this.modal.openWithComponent(AddOrEditNumberOfInspectedItemsComponent, { numberOfInspectedItems, inspectionType: this.inspectionType });
}
itemHasManagePermission(numberOfInspectedItems: NumberOfInspectedItemsDto): boolean {
......
import { Component, OnInit } from '@angular/core';
import { DriversInspectionsFacade } from '@teis/features/drivers-inspections/drivers-inspections-facade.service';
import { DriversInspectionPermission, InspectedItem } from '@teis/features/drivers-inspections/services/drivers-inspections.model';
import { DriversInspectionPermission, DriversInspectionType, InspectedItem } from '@teis/features/drivers-inspections/services/drivers-inspections.model';
import { PermissionDataObjectType } from '@teis/services/permissions/permissions.model';
import { ModalService } from '@teis/widgets/teis-modal/modal.service';
import { map, shareReplay } from 'rxjs/operators';
import { map, shareReplay, take } from 'rxjs/operators';
import { AddOrEditNumberOfInspectedItemsComponent } from '../add-or-edit-number-of-inspected-items/add-or-edit-number-of-inspected-items.component';
import { calculateTotalSum } from '../number-of-inspected-items-table/utils';
......@@ -32,7 +32,9 @@ export class NumberOfInspectedItemsComponent implements OnInit {
}
openAddInspectedItemsModal(item: InspectedItem) {
this.modal.openWithComponent(AddOrEditNumberOfInspectedItemsComponent, { item });
this.inspectionType$.pipe(take(1)).subscribe((inspectionType) => {
this.modal.openWithComponent(AddOrEditNumberOfInspectedItemsComponent, { item, inspectionType });
});
}
}
......@@ -1167,7 +1167,8 @@
"edit_number_of_inspected_items_WORKINGDAYS": "Kontrollitud tööpäevade arvude muutmine EN",
"edit_number_of_inspected_items_success_DRIVERS": "Kontrollitud juhtide arvude muutmine edukas EN",
"edit_number_of_inspected_items_success_WORKINGDAYS": "Kontrollitud tööpäevade arvude muutmine edukas EN",
"sum_error": "Veose ja sõitjate veo arvude summa peab võrduma tasulise veo ja oma tarbeks veo arvude summaga EN",
"sum_error_GOODS": "Veose veo arvud peavad võrduma tasulise veo ja oma tarbeks veo arvude summaga EN",
"sum_error_PASSENGERS": "Sõitjate veo arvud peavad võrduma tasulise veo ja oma tarbeks veo arvude summaga EN",
"controlled_period": "Kontrollitud ajavahemik EN",
"number_of_drivers": "Juhtide arv kontrollitud ettevõttes EN",
"number_of_vehicles": "Sõidukite arv kontrollitud ettevõttes EN",
......
......@@ -1167,7 +1167,8 @@
"edit_number_of_inspected_items_WORKINGDAYS": "Kontrollitud tööpäevade arvude muutmine",
"edit_number_of_inspected_items_success_DRIVERS": "Kontrollitud juhtide arvude muutmine edukas",
"edit_number_of_inspected_items_success_WORKINGDAYS": "Kontrollitud tööpäevade arvude muutmine edukas",
"sum_error": "Veose ja sõitjate veo arvude summa peab võrduma tasulise veo ja oma tarbeks veo arvude summaga ",
"sum_error_GOODS": "Veose veo arvud peavad võrduma tasulise veo ja oma tarbeks veo arvude summaga ",
"sum_error_PASSENGERS": "Sõitjate veo arvud peavad võrduma tasulise veo ja oma tarbeks veo arvude summaga ",
"controlled_period": "Kontrollitud ajavahemik",
"number_of_drivers": "Juhtide arv kontrollitud ettevõttes",
"number_of_vehicles": "Sõidukite arv kontrollitud ettevõttes",
......
......@@ -1167,7 +1167,8 @@
"edit_number_of_inspected_items_WORKINGDAYS": "Kontrollitud tööpäevade arvude muutmine RU",
"edit_number_of_inspected_items_success_DRIVERS": "Kontrollitud juhtide arvude muutmine edukas RU",
"edit_number_of_inspected_items_success_WORKINGDAYS": "Kontrollitud tööpäevade arvude muutmine edukas RU",
"sum_error": "Veose ja sõitjate veo arvude summa peab võrduma tasulise veo ja oma tarbeks veo arvude summaga RU",
"sum_error_GOODS": "Veose veo arvud peavad võrduma tasulise veo ja oma tarbeks veo arvude summaga RU",
"sum_error_PASSENGERS": "Sõitjate veo arvud peavad võrduma tasulise veo ja oma tarbeks veo arvude summaga RU",
"controlled_period": "Kontrollitud ajavahemik RU",
"number_of_drivers": "Juhtide arv kontrollitud ettevõttes RU",
"number_of_vehicles": "Sõidukite arv kontrollitud ettevõttes RU",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment