backend/src/services/trainService.services.js

24 lines
930 B
JavaScript
Raw Normal View History

const log = require('../utils/log.utils');
const db = require('./dbAccess.services');
const clean = require('../utils/sanitizer.utils');
async function findByHeadcode(headcode) {
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode);
log.out(`trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}`, 'dbug');
const now = new Date();
const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's'];
const shortDay = dayMap[now.getDay()]; // Fetch short day from map
const query = {
headcode: sanitizedHeadcode,
scheduleStartDate: {$lte: now},
scheduleEndDate: {$gte: now},
daysRun: {$in: [shortDay]}
};
// At this point, the returned objects need sorting to ensure the correct object is returned.
// C, N, O, P is the order - C being the preferred, then N, then O and finally P.
return db.query('timetable', query);
}
module.exports = {
findByHeadcode,
};