backend/src/services/trainService.services.js

126 lines
4.0 KiB
JavaScript
Raw Normal View History

const log = require('../utils/log.utils');
const db = require('./dbAccess.services');
const clean = require('../utils/sanitizer.utils');
const pis = require('../services/pis.services');
async function findByHeadcodeToday(headcode) {
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase();
log.out(`trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}`, 'dbug');
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]}
};
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
2023-06-06 22:08:30 +01:00
const tiplocList = await getPublicStops(trainData[trainService]['stops']);
//console.log(tiplocList.length); console.log(tiplocList);
2023-06-06 22:08:30 +01:00
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.
2023-06-06 22:08:30 +01:00
}
preparedData.push(trainData[trainService]);
}
return preparedData;
}
module.exports = {
findByHeadcodeToday,
};
/* Internal Functions, not to be exported */
async function getPublicStops(data) {
let tiplocList = [];
for (const publicStop in data) {
if (data[publicStop]['isPublic']) {
tiplocList.push(data[publicStop]['tiploc']);
}
}
return tiplocList;
}
async function parseTrains(data) { // Takes a single days data from a headcode query and returns only relevant services
let trainUids = [];
for (const i of data) {
const trainUid = i['trainUid'];
2023-06-06 22:08:30 +01:00
if (!trainUids.includes(trainUid)) {
trainUids.push(trainUid);
2023-06-06 22:08:30 +01:00
}
}
let parsedData = [];
2023-06-06 22:08:30 +01:00
for (const i in trainUids) {
const result = await findByTrainUid(trainUids[i]);
parsedData.push(result);
2023-06-06 22:08:30 +01:00
}
return parsedData;
}
async function findByTrainUid(uid, date = new Date()) { // Date defaults to today
const query = {
trainUid: uid,
scheduleStartDate: {$lte: date},
scheduleEndDate: {$gte: date}
};
const queryData = await db.query('timetable', query);
if (queryData.length === 0) {
return [];
}
let stpIndicators = {};
for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid'];
const stpIndicator = serviceDetail['stpIndicator'];
if (!stpIndicators[trainUid]) {
stpIndicators[trainUid] = {
hasC: false,
hasN: false,
hasO: false,
hasP: false
};
}
if (stpIndicator === 'C') {
stpIndicators[trainUid].hasC = true;
}
if (stpIndicator === 'N') {
stpIndicators[trainUid].hasN = true;
}
if (stpIndicator === 'O') {
stpIndicators[trainUid].hasO = true;
}
if (stpIndicator === 'P') {
stpIndicators[trainUid].hasP = true;
}
}
let preparedData = [];
for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid'];
const thisStpIndicators = stpIndicators[trainUid];
const stpIndicator = serviceDetail['stpIndicator'];
if (stpIndicator === 'C') {
return;
//continue;
}
if (stpIndicator === 'N' && !thisStpIndicators.hasC) {
return serviceDetail;
//preparedData.push(serviceDetail);
} else if (stpIndicator === 'O' && !thisStpIndicators.hasC && !thisStpIndicators.hasN) {
return serviceDetail;
//preparedData.push(serviceDetail);
} else if (stpIndicator === 'P' && !thisStpIndicators.hasC && !thisStpIndicators.hasN && !thisStpIndicators.hasO) {
return serviceDetail;
//preparedData.push(serviceDetail);
}
}
return preparedData;
}