Skip to content

Commit

Permalink
fix time and ave/md trains
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Jan 31, 2024
1 parent 48ee809 commit cd468fa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
16 changes: 12 additions & 4 deletions handlers/train.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const RenfeService = require('../services/renfe')

class Train {
async getTrainTime (target, originText, destinationText, bot) {
const result = await RenfeService.getMDTrain(originText, destinationText)
async getNextMD (target, originText, destinationText, bot) {
const result = await RenfeService.getNextMD(originText, destinationText)

if (result) {
bot.say(target, `El próximo tren MD sale a las ${result.time} de la ${result.origin_stop} con destino ${result.destination_stop}`);
bot.say(target, `Próximo tren MD de ${result.origin_stop} a ${result.destination_stop} sale a las ${result.time}`);
}
}
async getNextAVE (target, originText, destinationText, bot) {
const result = await RenfeService.getNextAVE(originText, destinationText)

if (result) {
bot.say(target, `Próximo AVE de ${result.origin_stop} a ${result.destination_stop} sale a las ${result.time}`);
}
}

}

module.exports = Train
module.exports = Train
24 changes: 12 additions & 12 deletions helpers/sqlManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const sql = require('mssql')
const config = require('../config')
const Token = require("../models/token");

async function getTrainMDTime (origin, destination) {
async function getCloserFutureTrain (origin, destination, type) {
try {
this.pool = await sql.connect(config.sql.connectionString)
const request = this.pool.request()
const result = await request.query(`SELECT TOP 1 CONVERT(VARCHAR(8),A.departure_time, 108) time , A.stop_name origin_stop, B.stop_name destination_stop FROM
const result = await request.query(`SELECT CONVERT(VARCHAR(8),A.departure_time, 108) time , A.stop_name origin_stop, B.stop_name destination_stop , C.start_date, C.end_date, T.service_id, R.route_id FROM
(SELECT ST.trip_id , S.stop_name, ST.stop_id, ST.stop_sequence, ST.departure_time
from stop_times ST
inner join stops S ON S.stop_id = ST.stop_id
Expand All @@ -29,17 +29,17 @@ WHERE
ON A.trip_id = B.trip_id
inner join trips T ON T.trip_id = A.trip_id
inner join routes R ON R.route_id = T.route_id
inner join calendar C ON C.service_id = T.service_id
left join calendar C ON C.service_id = T.service_id
WHERE A.stop_sequence < B.stop_sequence AND
C.start_date <= GETDATE() and
C.end_date >= GETDATE() AND
T.service_id NOT IN (SELECT CD.service_id FROM calendar_dates CD
where CAST (CD.date AS DATE) = CAST(GETDATE() AS DATE)) AND
route_short_name = 'MD'
AND A.departure_time > CONVERT (time, GETDATE())
WHERE A.stop_sequence < B.stop_sequence
AND (C.start_date IS NULL OR C.start_date <= DATEADD(hour, 1, GETDATE()))
and (C.end_date IS NULL OR C.end_date >= DATEADD(hour, 1, GETDATE()))
AND T.service_id NOT IN (SELECT CD.service_id FROM calendar_dates CD
where CD.date = DATEADD(hour, 1, GETDATE()) )
AND route_short_name = '${type}'
AND A.departure_time > CONVERT (time, DATEADD(hour, 1, GETDATE()))
ORDER BY ABS( DATEDIFF(minute, A.departure_time, CONVERT (time, GETDATE())) )`)
ORDER BY ABS( DATEDIFF(minute, A.departure_time, CONVERT (time, DATEADD(hour, 1, GETDATE()))) )`)
//const result = await sql.query
console.log(result.recordset[0])
return result.recordset[0]
Expand All @@ -49,5 +49,5 @@ ORDER BY ABS( DATEDIFF(minute, A.departure_time, CONVERT (time, GETDATE())) )`)
}

module.exports = {
getTrainMDTime
getCloserFutureTrain
}
8 changes: 6 additions & 2 deletions lib/inputParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ class InputParser {
return text.startsWith('!sunset') || text.startsWith('!ocaso') || text.startsWith('!atardecer')
}

isAskingForTrainTime (text) {
return text.toLowerCase().startsWith('!tren')
isAskingForNextMDTrain (text) {
return text.toLowerCase().startsWith('!md')
}

isAskingForNextAveTrain (text) {
return text.toLowerCase().startsWith('!ave')
}
}

Expand Down
8 changes: 6 additions & 2 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ class Messenger {
return handlers.weather.getAemet(target, textSplit.slice(1).join(' '), this.bot)
}

if (textSplit.length === 3 && inputParser.isAskingForTrainTime(textSplit[0])) {
return handlers.train.getTrainTime(target, textSplit[1], textSplit[2], this.bot)
if (textSplit.length === 3 && inputParser.isAskingForNextMDTrain(textSplit[0])) {
return handlers.train.getNextMD(target, textSplit[1], textSplit[2], this.bot)
}

if (textSplit.length === 3 && inputParser.isAskingForNextAveTrain(textSplit[0])) {
return handlers.train.getNextAVE(target, textSplit[1], textSplit[2], this.bot)
}
}

Expand Down
11 changes: 8 additions & 3 deletions services/renfe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const sqlManager = require('../helpers/sqlManager')

async function getMDTrain (origin, destination) {
return sqlManager.getTrainMDTime(origin, destination)
async function getNextMD (origin, destination) {
return sqlManager.getCloserFutureTrain(origin, destination, 'MD')
}

async function getNextAVE (origin, destination) {
return sqlManager.getCloserFutureTrain(origin, destination, 'AVE')
}

module.exports = {
getMDTrain
getNextMD,
getNextAVE
}

0 comments on commit cd468fa

Please sign in to comment.