diff --git a/src/modules/fare.js b/src/modules/fare.js index b2da72a5..42d35af8 100644 --- a/src/modules/fare.js +++ b/src/modules/fare.js @@ -145,7 +145,7 @@ const updateTaxiFare = async (sTime, isMajor) => { const to = await locationModel.findOne({ _id: item.to }); await acc; - await callTaxiFare + await callTaxiFare(from, to) .catch((err) => { logger.error(err.message); }) @@ -164,6 +164,7 @@ const updateTaxiFare = async (sTime, isMajor) => { ); } }) + .clone() .catch((err) => { logger.error(err.message); }); @@ -189,12 +190,7 @@ const callTaxiFare = async (from, to) => { } return ( await axios.get( - `${ - "https://naveropenapi.apigw.ntruss.com/map-direction/v1/driving?start=" + - from.longitude + - "," + - from.latitude - }&goal=${to.longitude + "," + to.latitude}&options=traoptimal`, + `https://naveropenapi.apigw.ntruss.com/map-direction/v1/driving?start=${from.longitude},${from.latitude}}&goal=${to.longitude},${to.latitude}&options=traoptimal`, { headers: naverMapApi } ) ).data.route.traoptimal[0].summary.taxiFare; diff --git a/src/services/fare.js b/src/services/fare.js index 55d1ad33..3a3718b5 100644 --- a/src/services/fare.js +++ b/src/services/fare.js @@ -45,41 +45,20 @@ const getTaxiFareHandler = async (req, res) => { .json({ error: "fare/getTaxiFareHandler: Wrong location" }); return; } - const isMajor = ( - await taxiFareModel - .findOne( - { from: from._id, to: to._id, time: 0 }, - { isMajor: true }, - (err, docs) => { - if (err) - logger.error( - "Error occured while finding Taxi Fare documents: " + - err.message - ); - } - ) - .lean() - ).isMajor; - // 시간대별 정보 관리 (현재: 카이스트 본원 <-> 대전역) - if (isMajor) { - const taxiFare = await taxiFareModel - .findOne( - { - from: from._id, - to: to._id, - time: sTime, - }, - (err, docs) => { - if (err) - logger.error( - "Error occured while finding Taxi Fare documents: " + - err.message - ); - } - ) - .lean(); + + const fare = await taxiFareModel + .findOne({ from: from._id, to: to._id, time: sTime }, (err, docs) => { + if (err) + logger.error( + "Error occured while finding Taxi Fare documents: " + err.message + ); + }) + .clone() + .lean(); + // 해당 sTime 대로 값이 존재하는 경우 (현재: 카이스트 본원 <-> 대전역) + if (fare) { //만일 초기화 되지 않은 시간대의 정보를 필요로하는 비상시의 경우 대비 - if (!taxiFare || taxiFare.fare <= 0) { + if (fare.fare <= 0) { await callTaxiFare(from, to) .then((fare) => { res.status(200).json({ fare: fare }); @@ -88,15 +67,15 @@ const getTaxiFareHandler = async (req, res) => { logger.error(err.message); }); } else { - res.status(200).json({ fare: taxiFare.fare }); + res.status(200).json({ fare: fare.fare }); } } else { - const taxiFare = await taxiFareModel + const minorTaxiFare = await taxiFareModel .findOne( { from: from._id, to: to._id, - time: 0, + time: 48 * new Date(req.query.time).getDay() + 0, }, (err, docs) => { if (err) @@ -106,9 +85,11 @@ const getTaxiFareHandler = async (req, res) => { ); } ) + .clone() .lean(); + //만일 초기화 되지 않은 시간대의 정보를 필요로하는 비상시의 경우 대비 - if (!taxiFare || taxiFare.fare <= 0) { + if (!minorTaxiFare || minorTaxiFare.fare <= 0) { await callTaxiFare(from, to) .then((fare) => { res.status(200).json({ fare: fare }); @@ -117,7 +98,7 @@ const getTaxiFareHandler = async (req, res) => { logger.error(err.message); }); } else { - res.status(200).json({ fare: taxiFare.fare }); + res.status(200).json({ fare: minorTaxiFare.fare }); } } } catch (err) {