backend/src/services/trainService.services.js

123 lines
3.8 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);
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);
console.log(JSON.stringify(queryData));
let trainData = await parseTrains(queryData);
let preparedData = [];
for (const 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']);
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
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) {
console.log(i);
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;
}
}
console.log(JSON.stringify(stpIndicators));
let preparedData = [];
for (const serviceDetail of queryData) {
const trainUid = serviceDetail['trainUid'];
const thisStpIndicators = stpIndicators[trainUid];
const stpIndicator = serviceDetail['stpIndicator'];
if (stpIndicator === 'C') {
continue;
}
if (stpIndicator === 'N' && !thisStpIndicators.hasC) {
preparedData.push(serviceDetail);
} else if (stpIndicator === 'O' && !thisStpIndicators.hasC && !thisStpIndicators.hasN) {
preparedData.push(serviceDetail);
} else if (stpIndicator === 'P' && !thisStpIndicators.hasC && !thisStpIndicators.hasN && !thisStpIndicators.hasO) {
preparedData.push(serviceDetail);
}
}
return preparedData;
}