2023-05-06 21:54:49 +01:00
|
|
|
// Finds PIS Codes using DB Lookups
|
|
|
|
|
|
|
|
const db = require('../services/dbAccess.services');
|
|
|
|
const log = require('../utils/log.utils');
|
|
|
|
const clean = require('../utils/sanitizer.utils');
|
|
|
|
|
|
|
|
async function findPisByOrigDest(start,end) {
|
|
|
|
log.out(`pisServices.findPisByOrigDest: Searching for PIS for Orig: ${start}, Dest: ${end}`, 'dbug');
|
|
|
|
const firstCrs = clean.cleanApiEndpointTxt(start.toLowerCase());
|
|
|
|
const lastCrs = clean.cleanApiEndpointTxt(end.toLowerCase());
|
|
|
|
const query = {
|
|
|
|
stops: {
|
|
|
|
$all: [
|
|
|
|
{ $elemMatch: { $eq: firstCrs } },
|
|
|
|
{ $elemMatch: { $eq: lastCrs } }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
$expr: {
|
|
|
|
$and: [
|
|
|
|
{ $eq: [{ $arrayElemAt: [ '$stops', -1 ] }, lastCrs] },
|
|
|
|
{ $eq: [{ $arrayElemAt: [ '$stops', 0 ] }, firstCrs] }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//const oldQuery = {$and:[{$expr:{$eq:[{$first:"$stops"},firstCrs]}},{$expr:{$eq:[{$last:"$stops"},lastCrs]}}]}
|
|
|
|
const search = db.query('pis', query);
|
2023-05-24 20:42:32 +01:00
|
|
|
return await search;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findPisByCode(code) {
|
2023-05-25 21:27:37 +01:00
|
|
|
log.out(`pisServices.findPisByCode: Searching for PIS code: ${code}`, 'dbug');
|
2023-05-24 20:42:32 +01:00
|
|
|
const cleanCode = clean.removeNonNumeric(code);
|
|
|
|
const query = {
|
2023-05-24 21:30:57 +01:00
|
|
|
'code': parseInt(cleanCode)
|
2023-05-24 20:42:32 +01:00
|
|
|
};
|
2023-05-24 21:30:57 +01:00
|
|
|
const search = db.query('pis', query);
|
2023-05-24 20:42:32 +01:00
|
|
|
return await search;
|
2023-05-06 21:54:49 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 21:27:37 +01:00
|
|
|
async function findRandom() {
|
|
|
|
log.out('pisServices.findRandom: Finding five random PIS Codes', 'dbug');
|
2023-05-25 21:52:23 +01:00
|
|
|
const query = { // Doesn't work, need aggregation
|
2023-05-25 21:27:37 +01:00
|
|
|
$sample: {
|
|
|
|
size: 5
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const results = db.query('pis', query);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:54:49 +01:00
|
|
|
// Hopefully at some point, I will also be able to implement a find PIS code by headcode option.
|
|
|
|
|
|
|
|
module.exports = {
|
2023-05-24 20:42:32 +01:00
|
|
|
findPisByOrigDest,
|
2023-05-25 21:27:37 +01:00
|
|
|
findPisByCode,
|
|
|
|
findRandom
|
2023-05-06 21:54:49 +01:00
|
|
|
};
|