Add pipeline for partial PIS match
Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
parent
7503722d84
commit
45fdf1904d
1
app.js
1
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",
|
||||
|
@ -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 = {
|
||||
|
Loading…
Reference in New Issue
Block a user