From 45fdf1904d3ef259485c1c54f1a47206e4298024 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 21:56:58 +0100 Subject: [PATCH] Add pipeline for partial PIS match Signed-off-by: Fred Boniface --- app.js | 1 - src/services/pis.services.ts | 54 ++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 3e8e348..2485165 100644 --- a/app.js +++ b/app.js @@ -72,7 +72,6 @@ app.use("/api/v2/timetable", tt2Rtr); // API Version 2 app.use("/api/v2/user", user2Rtr); // API Version 2 app.use("/misc", miscRtr); // Non public-api endpoints (Stats, Issue, etc.) - app.use("/api/v1/auth/test", authenticate, (req, res) => res.status(200).json({ status: "ok", diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index b974cf3..e9b3449 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -4,6 +4,7 @@ const db = require("../services/dbAccess.services"); const clean = require("../utils/sanitizer.utils"); import { logger } from "../utils/logger.utils"; +import { queryAggregate } from "./dbAccess.services"; const supported = ["GW", "UK"]; @@ -47,19 +48,66 @@ async function findByTiplocArray(tiplocArray: string[]) { }; const res = await db.query("pis", query); // If res is empty then try to find partial match - /* if (res.length) { + if (res.length) { return res; } else { let partial = await findPartialMatchByTiploc(tiplocArray); return partial - } */ - return await res; + } } async function findPartialMatchByTiploc(tiplocArray: string[]) { // Do some magic here, similar to findPisByOrigDest but // ensuring that the stops in the array match the last // x number of items in the array. + + const pipeline = [ + { + $addFields: { + reversedStops: { + $reverseArray: "$stops", + }, + reversedQueryArray: { + $reverseArray: tiplocArray, + }, + }, + }, + { + $match: { + $expr: { + $gt: [ + { + $indexOfArray: [ + "$reversedStops", + { + $arrayElemAt: ["$reversedQueryArray", 0], + } + ], + }, + -1, + ], + }, + }, + }, + { + $project: { + code: 1, + skipStops: { + $subtract: [ + { + $size: "$stops", + }, + { + $size: tiplocArray, + }, + ], + }, + }, + }, + ]; + + const res = await queryAggregate('pis', pipeline) + console.log(JSON.stringify(res)) } module.exports = {