TimetableAPI-Upgrade #64

Merged
fred.boniface merged 36 commits from TimetableAPI-Upgrade into main 2024-02-11 15:53:17 +00:00
3 changed files with 75 additions and 11 deletions
Showing only changes of commit 89cd3630d4 - Show all commits

View File

@ -4,6 +4,8 @@ const pis = require("../services/pis.services");
import { logger } from "../utils/logger.utils"; import { logger } from "../utils/logger.utils";
import type { TrainServices, Service, Stop } from "@owlboard/ts-types";
// This function is deprecated and should no longer be used. // This function is deprecated and should no longer be used.
// It will be removed in a later version // It will be removed in a later version
async function findByHeadcodeToday(headcode: string) { async function findByHeadcodeToday(headcode: string) {
@ -42,7 +44,7 @@ async function findByHeadcodeToday(headcode: string) {
} }
// Finds a train by its headcode value // Finds a train by its headcode value
async function findByHeadcode(date: string | Date, headcode: string) { async function findByHeadcode(date: string | Date, headcode: string): Promise<TrainServices[]> {
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase();
logger.debug( logger.debug(
`trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}` `trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}`
@ -80,13 +82,13 @@ async function findByHeadcode(date: string | Date, headcode: string) {
}, },
}, },
]; ];
const queryData = await db.queryAggregate("timetable", pipeline); const queryData: Service[] = await db.queryAggregate("timetable", pipeline);
let filteredData = filterServices(queryData); let filteredData = filterServices(queryData);
return await filteredData; return await filteredData;
} }
// Finds a train by its trainUid value // Finds a train by its trainUid value
async function findByTrainUid(uid: string, date: Date | string = new Date()) { async function findByTrainUid(uid: string, date: Date | string = new Date()): Promise<TrainServices | null> {
let queryDate; let queryDate;
if (date === "now") { if (date === "now") {
queryDate = new Date(); queryDate = new Date();
@ -100,7 +102,7 @@ async function findByTrainUid(uid: string, date: Date | string = new Date()) {
}; };
const queryData = await db.query("timetable", query); const queryData = await db.query("timetable", query);
if (queryData.length === 0) { if (queryData.length === 0) {
return []; return queryData;
} }
let services; let services;
services = await filterServices(queryData); services = await filterServices(queryData);
@ -128,7 +130,7 @@ module.exports = {
/* Accepts the 'stops' array from a db query and produces an /* Accepts the 'stops' array from a db query and produces an
array of only public stops as TIPLOCs. */ array of only public stops as TIPLOCs. */
async function getPublicStops(data) { async function getPublicStops(data: Stop[]): Promise<string[]> {
let tiplocList = []; let tiplocList = [];
for (const publicStop in data) { for (const publicStop in data) {
if (data[publicStop]["isPublic"]) { if (data[publicStop]["isPublic"]) {
@ -141,24 +143,26 @@ async function getPublicStops(data) {
/* Takes a single days data from a headcode query and requeries /* Takes a single days data from a headcode query and requeries
using the trainUid, required to ensure any cancellations are using the trainUid, required to ensure any cancellations are
accounted for */ accounted for */
async function parseTrains(data) { async function parseTrains(data: TrainServices[]): Promise<TrainServices[]> {
let trainUids = []; let trainUids: string[] = [];
for (const i of data) { for (const i of data) {
const trainUid = i["trainUid"]; const trainUid = i["trainUid"];
if (!trainUids.includes(trainUid)) { if (!trainUids.includes(trainUid)) {
trainUids.push(trainUid); trainUids.push(trainUid);
} }
} }
let parsedData = []; let parsedData: TrainServices[] = [];
for (const i in trainUids) { for (const i in trainUids) {
const result = await findByTrainUid(trainUids[i]); const result: TrainServices | null = await findByTrainUid(trainUids[i]);
parsedData.push(result); if (result) {
parsedData.push(result);
}
} }
return parsedData; return parsedData;
} }
// Filters services based on their STP Indicator // Filters services based on their STP Indicator
async function filterServices(data) { async function filterServices(data: Service[]): Promise<TrainServices[]> {
let stpIndicators = {}, let stpIndicators = {},
filteredServices = []; filteredServices = [];
for (const serviceDetail of data) { for (const serviceDetail of data) {

View File

@ -0,0 +1,40 @@
import { logger } from "../utils/logger.utils";
import { findByTiplocArray } from "../services/pis.services"
import { queryAggregate } from "./dbAccess.services";
import { getFindByHeadcodePipeline } from "../utils/trainService.utils"
import { removeNonAlphanumeric } from "../utils/sanitizer.utils"
import type { TrainServices, Service, Stop } from "@owlboard/ts-types";
import type { Document } from "mongodb";
export async function findByHeadcode(headcode: string, date: Date | string): Promise<TrainServices[]> {
const sanitizedHeadcode = removeNonAlphanumeric(headcode);
logger.debug(`trainServices.findByHeadcode: Searching for trains by headcode: ${headcode}`)
// If 'now' then generate a new Date now, else use the provided date, then set time to 1200.
const searchDate = (date === "now") ? new Date() : new Date(date);
searchDate.setHours(12, 0, 0);
// Get the 'shortDay'
const shortDay = getShortDay(searchDate)
const query = {
headcode: sanitizedHeadcode,
scheduleStartDate: { $lte: searchDate },
scheduleEndDate: { $gte: searchDate },
daysRun: { $in: [shortDay] },
};
const pipeline = getFindByHeadcodePipeline(query)
// Before proceeding, I need to check the return from the database to ensure that it
// is of type Service[] - for this use the Aggregate function in Compass.
const result: Service[] = await queryAggregate("timetable", pipeline) as Service[]
// The Filter Services function needs writing - possibly moving to utils as it is long.
}
// Outputs the standard 'shortday' string from a Date.
function getShortDay(day: Date): string {
const dayMap = ["su", "m", "t", "w", "th", "f", "s"];
const shortDay = dayMap[day.getDay()];
return shortDay
}

View File

@ -0,0 +1,20 @@
export function getFindByHeadcodePipeline(query: any) {
return [
{
$match: query,
},
{
$project: {
operator: 1,
stops: {
$concatArrays: [
[{ $first: "$stops" }],
[{ $arrayElemAt: ["$stops", -1] }],
],
},
trainUid: 1,
stpIndicator: 1,
},
},
];
}