Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: handle stops in viriato imports #9725

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,21 @@ 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];
const isLastOcp = ocpTT === ocpTTs.at(-1);
const ocpType = ocpTT.getAttribute('ocpType');
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);
}
let arrivalTime = ocpType === 'pass' ? departureTime : times?.getAttribute('arrival') || '';
arrivalTime = cleanTimeFormat(arrivalTime);
departureTime = cleanTimeFormat(departureTime);

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

const operationalPoint = cichDict[ocpRef];

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

let stopFor: number | undefined;

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

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