2023-05-31 20:04:00 +01:00
|
|
|
const log = require('../utils/log.utils');
|
|
|
|
const db = require('./dbAccess.services');
|
|
|
|
const clean = require('../utils/sanitizer.utils');
|
2023-06-01 00:10:14 +01:00
|
|
|
const pis = require('../services/pis.services');
|
2023-05-31 20:04:00 +01:00
|
|
|
|
2023-06-01 12:40:43 +01:00
|
|
|
async function findByHeadcodeToday(headcode) {
|
2023-06-07 21:27:28 +01:00
|
|
|
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase();
|
2023-06-07 21:53:56 +01:00
|
|
|
log.out('trainServiceServices.findByHeadcode: Searching for headcode ' +
|
|
|
|
sanitizedHeadcode, 'dbug');
|
2023-05-31 20:04:00 +01:00
|
|
|
const now = new Date();
|
|
|
|
const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's'];
|
|
|
|
const shortDay = dayMap[now.getDay()]; // Fetch short day from map
|
|
|
|
const query = {
|
|
|
|
headcode: sanitizedHeadcode,
|
|
|
|
scheduleStartDate: {$lte: now},
|
|
|
|
scheduleEndDate: {$gte: now},
|
|
|
|
daysRun: {$in: [shortDay]}
|
|
|
|
};
|
2023-06-01 12:40:43 +01:00
|
|
|
const queryData = await db.query('timetable', query);
|
2023-06-07 01:18:02 +01:00
|
|
|
let trainData = await parseTrains(queryData);
|
2023-06-01 12:40:43 +01:00
|
|
|
let preparedData = [];
|
2023-06-07 12:05:35 +01:00
|
|
|
for (let trainService in trainData) {
|
2023-07-01 21:07:18 +01:00
|
|
|
if (pis.supported.includes(trainData?.operator)) {
|
2023-06-01 12:40:43 +01:00
|
|
|
// Search for PIS Code for each service
|
2023-07-01 21:07:18 +01:00
|
|
|
const tiplocList = await getPublicStops(trainData[trainService]['stops']);
|
|
|
|
//console.log(tiplocList.length); console.log(tiplocList);
|
|
|
|
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
|
|
|
|
// '0015' is a string becuase 0015 is not a valid number..
|
|
|
|
}
|
2023-06-06 22:08:30 +01:00
|
|
|
}
|
2023-06-01 12:40:43 +01:00
|
|
|
preparedData.push(trainData[trainService]);
|
|
|
|
}
|
|
|
|
return preparedData;
|
|
|
|
}
|
|
|
|
|
2023-06-19 21:34:14 +01:00
|
|
|
async function findByHeadcode(date, headcode) {
|
|
|
|
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase();
|
|
|
|
log.out('trainServiceServices.findByHeadcode: Searching for headcode ' +
|
|
|
|
sanitizedHeadcode, 'dbug');
|
|
|
|
let searchDate;
|
|
|
|
if (date === 'now') {
|
|
|
|
searchDate = new Date();
|
|
|
|
} else {
|
|
|
|
searchDate = new Date(date);
|
|
|
|
}
|
|
|
|
const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's'];
|
|
|
|
const shortDay = dayMap[searchDate.getDay()]; // Fetch short day from map
|
|
|
|
const query = {
|
|
|
|
headcode: sanitizedHeadcode,
|
|
|
|
scheduleStartDate: {$lte: searchDate},
|
|
|
|
scheduleEndDate: {$gte: searchDate},
|
|
|
|
daysRun: {$in: [shortDay]}
|
|
|
|
};
|
|
|
|
const queryData = await db.query('timetable', query);
|
|
|
|
let trainData = await parseTrains(queryData);
|
|
|
|
let preparedData = [];
|
|
|
|
for (let trainService in trainData) {
|
|
|
|
// Search for PIS Code for each service
|
|
|
|
const tiplocList = await getPublicStops(trainData[trainService]['stops']);
|
|
|
|
//console.log(tiplocList.length); console.log(tiplocList);
|
|
|
|
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
|
|
|
|
// '0015' is a string otherwise it is interpreted as octal 13.
|
|
|
|
}
|
|
|
|
preparedData.push(trainData[trainService]);
|
|
|
|
}
|
|
|
|
return preparedData;
|
|
|
|
}
|
|
|
|
|
2023-06-04 18:56:42 +01:00
|
|
|
module.exports = {
|
|
|
|
findByHeadcodeToday,
|
2023-06-19 21:34:14 +01:00
|
|
|
findByHeadcode
|
2023-06-04 18:56:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Internal Functions, not to be exported */
|
|
|
|
|
2023-06-07 21:53:56 +01:00
|
|
|
/* Accepts the 'stops' array from a db query and produces an
|
|
|
|
array of only public stops as TIPLOCs. */
|
2023-06-01 12:40:43 +01:00
|
|
|
async function getPublicStops(data) {
|
|
|
|
let tiplocList = [];
|
|
|
|
for (const publicStop in data) {
|
|
|
|
if (data[publicStop]['isPublic']) {
|
|
|
|
tiplocList.push(data[publicStop]['tiploc']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tiplocList;
|
2023-05-31 20:04:00 +01:00
|
|
|
}
|
|
|
|
|
2023-06-07 21:53:56 +01:00
|
|
|
/* Takes a single days data from a headcode query and requeries
|
|
|
|
using the trainUid, required to ensure any cancellations are
|
|
|
|
accounted for */
|
|
|
|
async function parseTrains(data) {
|
2023-06-07 01:18:02 +01:00
|
|
|
let trainUids = [];
|
2023-06-07 11:23:30 +01:00
|
|
|
for (const i of data) {
|
|
|
|
const trainUid = i['trainUid'];
|
2023-06-06 22:08:30 +01:00
|
|
|
if (!trainUids.includes(trainUid)) {
|
2023-06-07 11:23:30 +01:00
|
|
|
trainUids.push(trainUid);
|
2023-06-06 22:08:30 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 01:18:02 +01:00
|
|
|
let parsedData = [];
|
2023-06-06 22:08:30 +01:00
|
|
|
for (const i in trainUids) {
|
2023-06-07 01:18:02 +01:00
|
|
|
const result = await findByTrainUid(trainUids[i]);
|
|
|
|
parsedData.push(result);
|
2023-06-06 22:08:30 +01:00
|
|
|
}
|
2023-06-07 01:18:02 +01:00
|
|
|
return parsedData;
|
|
|
|
}
|
|
|
|
|
2023-06-07 21:53:56 +01:00
|
|
|
/* Queries using a trainUid and filters all schedules that
|
|
|
|
have been over-ridden by overlays, cancellations and new
|
|
|
|
schedules. */
|
|
|
|
async function findByTrainUid(uid, date = new Date()) {
|
2023-06-07 01:18:02 +01:00
|
|
|
const query = {
|
|
|
|
trainUid: uid,
|
|
|
|
scheduleStartDate: {$lte: date},
|
|
|
|
scheduleEndDate: {$gte: date}
|
|
|
|
};
|
|
|
|
const queryData = await db.query('timetable', query);
|
|
|
|
if (queryData.length === 0) {
|
2023-06-07 11:23:30 +01:00
|
|
|
return [];
|
2023-06-07 01:18:02 +01:00
|
|
|
}
|
2023-06-07 11:23:30 +01:00
|
|
|
let stpIndicators = {};
|
2023-06-07 01:18:02 +01:00
|
|
|
for (const serviceDetail of queryData) {
|
|
|
|
const trainUid = serviceDetail['trainUid'];
|
|
|
|
const stpIndicator = serviceDetail['stpIndicator'];
|
2023-06-07 11:23:30 +01:00
|
|
|
if (!stpIndicators[trainUid]) {
|
|
|
|
stpIndicators[trainUid] = {
|
|
|
|
hasC: false,
|
2023-06-07 01:18:02 +01:00
|
|
|
hasN: false,
|
2023-06-07 11:23:30 +01:00
|
|
|
hasO: false,
|
|
|
|
hasP: false
|
2023-06-07 01:18:02 +01:00
|
|
|
};
|
|
|
|
}
|
2023-06-07 11:23:30 +01:00
|
|
|
if (stpIndicator === 'C') {
|
|
|
|
stpIndicators[trainUid].hasC = true;
|
|
|
|
}
|
|
|
|
if (stpIndicator === 'N') {
|
|
|
|
stpIndicators[trainUid].hasN = true;
|
|
|
|
}
|
2023-06-07 01:18:02 +01:00
|
|
|
if (stpIndicator === 'O') {
|
2023-06-07 11:23:30 +01:00
|
|
|
stpIndicators[trainUid].hasO = true;
|
|
|
|
}
|
|
|
|
if (stpIndicator === 'P') {
|
|
|
|
stpIndicators[trainUid].hasP = true;
|
2023-06-07 01:18:02 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 11:23:30 +01:00
|
|
|
let preparedData = [];
|
2023-06-07 01:18:02 +01:00
|
|
|
for (const serviceDetail of queryData) {
|
|
|
|
const trainUid = serviceDetail['trainUid'];
|
2023-06-07 11:23:30 +01:00
|
|
|
const thisStpIndicators = stpIndicators[trainUid];
|
2023-06-07 01:18:02 +01:00
|
|
|
const stpIndicator = serviceDetail['stpIndicator'];
|
|
|
|
|
|
|
|
if (stpIndicator === 'C') {
|
2023-06-07 21:27:28 +01:00
|
|
|
return;
|
2023-06-07 01:18:02 +01:00
|
|
|
}
|
2023-06-07 21:53:56 +01:00
|
|
|
if (stpIndicator === 'N' &&
|
|
|
|
!thisStpIndicators.hasC) {
|
2023-06-07 21:27:28 +01:00
|
|
|
return serviceDetail;
|
2023-06-07 21:53:56 +01:00
|
|
|
|
|
|
|
} else if (stpIndicator === 'O' &&
|
|
|
|
!thisStpIndicators.hasC &&
|
|
|
|
!thisStpIndicators.hasN) {
|
2023-06-07 21:27:28 +01:00
|
|
|
return serviceDetail;
|
2023-06-07 21:53:56 +01:00
|
|
|
|
|
|
|
} else if (stpIndicator === 'P' &&
|
|
|
|
!thisStpIndicators.hasC &&
|
|
|
|
!thisStpIndicators.hasN &&
|
|
|
|
!thisStpIndicators.hasO) {
|
2023-06-07 21:27:28 +01:00
|
|
|
return serviceDetail;
|
2023-06-07 01:18:02 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 11:23:30 +01:00
|
|
|
return preparedData;
|
|
|
|
}
|