Skip to content

Commit

Permalink
front: handle stops in viriato imports
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainValls committed Nov 15, 2024
1 parent f7f1991 commit d6f032b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions front/src/applications/operationalStudies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSc
import type { ArrayElement } from 'utils/types';

export interface Step {
id: string;
uic: number;
chCode?: string;
yard?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,22 @@ const ImportTrainScheduleConfig = ({
startDate: string
): Step[] =>
ocpTTs
.map((ocpTT, index): Step | null => {
.map((ocpTT): Step | null => {
const ocpRef = ocpTT.getAttribute('ocpRef');
const times = ocpTT.getElementsByTagName('times')[0];
let departureTime = times?.getAttribute('departure') || '';
let arrivalTime = times?.getAttribute('arrival') || '';

const isLastOcpTT = index === ocpTTs.length - 1;

if (isLastOcpTT) {
arrivalTime = cleanTimeFormat(departureTime) || cleanTimeFormat(arrivalTime); // For the last sequence, arrival equals departure
departureTime = cleanTimeFormat(arrivalTime) || cleanTimeFormat(departureTime);
} else if (index !== 0) {
arrivalTime = times?.getAttribute('arrival') || times?.getAttribute('departure') || '';
arrivalTime = cleanTimeFormat(arrivalTime);
departureTime = times?.getAttribute('departure') || times?.getAttribute('arrival') || '';
departureTime = cleanTimeFormat(departureTime);
}
arrivalTime = times?.getAttribute('arrival') || times?.getAttribute('departure') || '';
arrivalTime = cleanTimeFormat(arrivalTime);
departureTime = times?.getAttribute('departure') || times?.getAttribute('arrival') || '';
departureTime = cleanTimeFormat(departureTime);

if (!ocpRef) {
console.error('ocpRef is null or undefined');
return null;
}

const operationalPoint = cichDict[ocpRef];

if (!operationalPoint) {
Expand All @@ -203,13 +197,21 @@ const ImportTrainScheduleConfig = ({
const formattedArrivalTime = `${startDate} ${arrivalTime}`;
const formattedDepartureTime = `${startDate} ${departureTime}`;

let stopFor = 0;
if (arrivalTime && departureTime) {
const arrivalDate = new Date(`${startDate}T${arrivalTime}`);
const departureDate = new Date(`${startDate}T${departureTime}`);
stopFor = Math.round((departureDate.getTime() - arrivalDate.getTime()) / 1000);
}

return {
id: nextId(),
uic,
chCode,
name: ocpRef,
arrivalTime: cleanTimeFormat(formattedArrivalTime),
departureTime: cleanTimeFormat(formattedDepartureTime),
arrivalTime: formattedArrivalTime,
departureTime: formattedDepartureTime,
duration: stopFor > 0 ? stopFor : undefined,
} as Step;
})
.filter((step): step is Step => step !== null);
Expand Down

0 comments on commit d6f032b

Please sign in to comment.