diff --git a/src/services/trainService.services.js b/src/services/trainService.services.js index 39f3c72..d7379e7 100644 --- a/src/services/trainService.services.js +++ b/src/services/trainService.services.js @@ -20,9 +20,13 @@ async function findByHeadcodeToday(headcode) { let preparedData = []; for (const trainService in trainData) { // Search for PIS Code for each service - let tiplocList = await getPublicStops(trainData[trainService]['stops']); - const pisDetail = await pis.findByTiplocArray(tiplocList); - trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None'; + const tiplocList = await getPublicStops(trainData[trainService]['stops']); + if (tiplocList.length) { + const pisDetail = await pis.findByTiplocArray(tiplocList); + trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None'; + } else { + trainData[trainService]['pis'] = '0015' // Not in Service code + } preparedData.push(trainData[trainService]); } return preparedData; @@ -47,7 +51,6 @@ async function getPublicStops(data) { async function parseTrains(data, date = new Date() { // Parses an array returned from a db-query and removes any service entries that don't apply to {date} // {date} shoule be a datetime object and defaults to now if not provided - as a general rule it should always be provided for clarity - // At this point, the returned objects need sorting to ensure the correct object is returned. // C, N, O, P is the order - C being the preferred, then N, then O and finally P. // If stpindicator == "C" && trainUid (isSame as N, O or P entry) => DO NOT DISPLAY SERVICE ("C" records do not contain the headcode (signalling_id)) so an additional lookup needs to be performed for the trainUid. @@ -56,6 +59,18 @@ async function parseTrains(data, date = new Date() { // Obviously, remember that several trains can share a headcode, but the trainUid is unique for each service. // Probably best to loop through each return here and add to a new [], then if another entry supersedes it, overwrite it in the []. Then pass to the below part of the function to process the data for sending. // Maybe break in to a new function parseTrains() which can take in any dbquery array and remove any services that are not meant to be displayed based on the data provided. Maybe have a date as an input so it can be used in the future to fetch trains for a given date in the future - due to database cleaning in db-manager, past dates would need adjustments to how the database is cleaned leaving outdated services available. - const parsedDate = data; + const parsedData = data; return parsedData; + let trainUids = [] + for (const i in data) { + const trainUid = data[i]['trainUid'] + if (!trainUids.includes(trainUid)) { + data.push(trainUid) + } + } + let parsedData = [] + for (const i in trainUids) { + result = await findByTrainUid(trainUids[i]) + parsedData.push(result) + } };