Add pipeline for partial PIS match

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-10-12 21:56:58 +01:00
parent 7503722d84
commit 45fdf1904d
2 changed files with 51 additions and 4 deletions

1
app.js
View File

@ -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",

View File

@ -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 = {