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 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; } 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']; if (!trainUids.includes(trainUid)) { trainUids.push(trainUid); } } let parsedData = []; for (const i in trainUids) { const result = await findByTrainUid(trainUids[i]); parsedData.push(result); } 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; }