Commit 1453ce7e authored by Pearu Sarv's avatar Pearu Sarv
Browse files

TEIS-2349 Update violations on infringement status change

parent 01c592ee
......@@ -22,6 +22,7 @@ import {
DriversInfringementStatus,
CreateOrEditNumberOfInspectedItemsDto,
DriversInspectionInspectedItemsDto,
DriversInfringementExtendedDto,
} from '@teis/features/drivers-inspections/services/drivers-inspections.model';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { distinctUntilChanged, map, take, takeUntil, tap } from 'rxjs/operators';
......@@ -32,6 +33,7 @@ import { ConfirmationOptions } from '@teis/widgets/teis-modal/confirmation/confi
import { FileApi } from '@teis/services/file/file.api';
import { FileReferenceWithPermissionsDto } from '@teis/services/file/file.model';
import { FileService } from '@teis/services/file/file.service';
import { getUpdatedViolations } from './utils';
interface Loading {
driversInspection: boolean;
......@@ -433,7 +435,15 @@ export class DriversInspectionsFacade implements OnDestroy {
}
updateDriversInfringementStatus(id: string, status: DriversInfringementStatus) {
return this.driversInspectionOfficeApi.patchInfringementStatus(id, status);
return this.driversInspectionOfficeApi.patchInfringementStatus(id, status).pipe(tap((infringement: DriversInfringementExtendedDto) => {
this.updateViolationsJustificationStatuses(infringement.category);
}));
}
private updateViolationsJustificationStatuses(updatedViolation: DriversInspectionViolationPublicDto): void {
const violations = this.driversInspectionViolations$.getValue();
const updatedViolations = getUpdatedViolations(violations, updatedViolation);
this.driversInspectionViolations$.next(updatedViolations);
}
setActiveTab(tab: DriversInspectionTab) {
......
......@@ -9,6 +9,7 @@ import {
DriversInfringementStatus,
CreateOrEditNumberOfInspectedItemsDto,
DriversInspectionInspectedItemsDto,
DriversInfringementExtendedDto,
} from '@teis/features/drivers-inspections/services/drivers-inspections.model';
import { ApiOffice } from '@teis/services/api-office';
......@@ -49,7 +50,7 @@ export class DriversInspectionsOfficeApi extends ApiOffice {
}
patchInfringementStatus(id: string, status: DriversInfringementStatus) {
return this.http.patch(`${this.url}/drivers-infringements/${id}/status`, { status: { code: status } });
return this.http.patch<DriversInfringementExtendedDto>(`${this.url}/drivers-infringements/${id}/status`, { status: { code: status } });
}
getNumberOfInspectedItems(id: string) {
......
......@@ -113,6 +113,11 @@ export interface DriversInfringementDto {
status: ClassifierItemLightDto;
}
export interface DriversInfringementExtendedDto extends DriversInfringementDto {
category: DriversInspectionViolationPublicDto;
driversViolation: DriversViolationOfficeDto;
}
export interface DriversInfringementOfficeDto {
dateAndTimeFrom: string;
dateAndTimeUntil: string;
......
import { DriversInspectionViolationPublicDto, DriversViolationPublicDto, DriversViolationTypePublicDto } from './services/drivers-inspections.model';
export const getUpdatedViolations = (violations: DriversInspectionViolationPublicDto[], updatedViolation: DriversInspectionViolationPublicDto): DriversInspectionViolationPublicDto[] => {
const updatedDriversViolationType: DriversViolationTypePublicDto = updatedViolation.driversViolationTypes[0];
return violations.map((violation: DriversInspectionViolationPublicDto) => {
if (updatedViolation.id === violation.id) {
const updatedDriversViolationTypes = getUpdatedDriversViolationTypes(violation.driversViolationTypes, updatedDriversViolationType);
return { ...violation, justificationStatus: updatedViolation.justificationStatus, driversViolationTypes: updatedDriversViolationTypes };
}
return violation;
});
};
const getUpdatedDriversViolationTypes = (driversViolationTypes: DriversViolationTypePublicDto[], updatedDriversViolationType: DriversViolationTypePublicDto): DriversViolationTypePublicDto[] => {
const updatedDriversViolation: DriversViolationPublicDto = updatedDriversViolationType.driversViolations[0];
return driversViolationTypes.map((driversViolationType: DriversViolationTypePublicDto) => {
if (updatedDriversViolationType.id === driversViolationType.id) {
const updatedDriversViolations = getUpdatedDriversViolations(driversViolationType.driversViolations, updatedDriversViolation);
return { ...driversViolationType, justificationStatus: updatedDriversViolationType.justificationStatus, driversViolations: updatedDriversViolations };
}
return driversViolationType;
});
};
const getUpdatedDriversViolations = (driversViolations: DriversViolationPublicDto[], updatedDriversViolation: DriversViolationPublicDto): DriversViolationPublicDto[] => {
return driversViolations.map((driversViolation: DriversViolationPublicDto) => {
if (updatedDriversViolation.id === driversViolation.id) {
return { ...driversViolation, justificationStatus: updatedDriversViolation.justificationStatus };
}
return driversViolation;
});
};
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