Add find PIS by code functionality

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-05-24 20:42:32 +01:00
parent b119f126b9
commit 11ece9c80e
4 changed files with 31 additions and 3 deletions

View File

@ -11,7 +11,18 @@ async function byOrigDest(req, res, next){
} }
} }
async function byCode(req, res, next){
try {
let code = req.params.code;
res.json(await pis.findPisByCode(code));
} catch (err) {
console.error('Unknown Error', err.message);
next(err);
}
}
module.exports = { module.exports = {
byOrigDest byOrigDest,
byCode
}; };

View File

@ -3,5 +3,6 @@ const router = express.Router();
const pisController = require('../controllers/pis.controllers'); const pisController = require('../controllers/pis.controllers');
router.get('/origdest/:start/:end', pisController.byOrigDest); router.get('/origdest/:start/:end', pisController.byOrigDest);
router.get('/code/:code', pisController.byCode);
module.exports = router; module.exports = router;

View File

@ -24,11 +24,22 @@ async function findPisByOrigDest(start,end) {
}; };
//const oldQuery = {$and:[{$expr:{$eq:[{$first:"$stops"},firstCrs]}},{$expr:{$eq:[{$last:"$stops"},lastCrs]}}]} //const oldQuery = {$and:[{$expr:{$eq:[{$first:"$stops"},firstCrs]}},{$expr:{$eq:[{$last:"$stops"},lastCrs]}}]}
const search = db.query('pis', query); const search = db.query('pis', query);
return search; return await search;
}
async function findPisByCode(code) {
log.out(`pisServices.findPisByCode: Searching for PIS code: ${code}`);
const cleanCode = clean.removeNonNumeric(code);
const query = {
code: cleanCode
};
const search = db.query('pis', query);
return await search;
} }
// Hopefully at some point, I will also be able to implement a find PIS code by headcode option. // Hopefully at some point, I will also be able to implement a find PIS code by headcode option.
module.exports = { module.exports = {
findPisByOrigDest findPisByOrigDest,
findPisByCode
}; };

View File

@ -8,6 +8,10 @@ function removeNonAlpha(inputString) { // Should be able to replace sanitizer m
return inputString.replace(/[^a-zA-Z]/g, ''); return inputString.replace(/[^a-zA-Z]/g, '');
} }
function removeNonNumeric(inputString) {
return inputString.replace(/[^0-9]/g, '');
}
const cleanApiEndpointTxt = removeNonAlpha; const cleanApiEndpointTxt = removeNonAlpha;
const cleanApiEndpointNum = removeNonAlphanumeric; const cleanApiEndpointNum = removeNonAlphanumeric;
@ -26,6 +30,7 @@ module.exports = {
cleanApiEndpointNum, cleanApiEndpointNum,
removeNonAlpha, removeNonAlpha,
removeNonAlphanumeric, removeNonAlphanumeric,
removeNonNumeric,
cleanNrcc, cleanNrcc,
getDomainFromEmail, getDomainFromEmail,
}; };