From de3329f699e329b078302a881cdb53c22dab5f6d Mon Sep 17 00:00:00 2001 From: ybmin Date: Tue, 13 Aug 2024 21:50:21 +0900 Subject: [PATCH 1/7] Refactor: fix error --- src/modules/fare.js | 10 +++----- src/services/fare.js | 59 +++++++++++++++----------------------------- 2 files changed, 23 insertions(+), 46 deletions(-) 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) { From efe24c612d078f8ed5f89d8e07b8094e452e38f4 Mon Sep 17 00:00:00 2001 From: ybmin Date: Tue, 13 Aug 2024 23:38:30 +0900 Subject: [PATCH 2/7] Fix: callback remove --- src/modules/fare.js | 10 +--------- src/services/fare.js | 28 ++++++---------------------- 2 files changed, 7 insertions(+), 31 deletions(-) diff --git a/src/modules/fare.js b/src/modules/fare.js index 42d35af8..6b162f4c 100644 --- a/src/modules/fare.js +++ b/src/modules/fare.js @@ -153,18 +153,10 @@ const updateTaxiFare = async (sTime, isMajor) => { if (fare) { await taxiFareModel.updateOne( { from: item.from, to: item.to, time: sTime }, - { fare: fare }, - (err, docs) => { - if (err) - logger.error( - "Error occured while updating Taxi Fare document: " + - err.message - ); - } + { fare: fare } ); } }) - .clone() .catch((err) => { logger.error(err.message); }); diff --git a/src/services/fare.js b/src/services/fare.js index 3a3718b5..5150ca0b 100644 --- a/src/services/fare.js +++ b/src/services/fare.js @@ -47,13 +47,7 @@ const getTaxiFareHandler = async (req, res) => { } 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() + .findOne({ from: from._id, to: to._id, time: sTime }) .lean(); // 해당 sTime 대로 값이 존재하는 경우 (현재: 카이스트 본원 <-> 대전역) if (fare) { @@ -71,21 +65,11 @@ const getTaxiFareHandler = async (req, res) => { } } else { const minorTaxiFare = await taxiFareModel - .findOne( - { - from: from._id, - to: to._id, - time: 48 * new Date(req.query.time).getDay() + 0, - }, - (err, docs) => { - if (err) - logger.error( - "Error occured while finding Taxi Fare documents: " + - err.message - ); - } - ) - .clone() + .findOne({ + from: from._id, + to: to._id, + time: 48 * new Date(req.query.time).getDay() + 0, + }) .lean(); //만일 초기화 되지 않은 시간대의 정보를 필요로하는 비상시의 경우 대비 From ef7c98c733dbb47dd0c861fc3c1e345d9addb69e Mon Sep 17 00:00:00 2001 From: ybmin Date: Tue, 13 Aug 2024 23:48:00 +0900 Subject: [PATCH 3/7] Feat: double catch --- src/modules/fare.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/modules/fare.js b/src/modules/fare.js index 6b162f4c..0c577b4f 100644 --- a/src/modules/fare.js +++ b/src/modules/fare.js @@ -156,9 +156,6 @@ const updateTaxiFare = async (sTime, isMajor) => { { fare: fare } ); } - }) - .catch((err) => { - logger.error(err.message); }); await new Promise((resolve) => setTimeout(() => resolve, 200)); return acc; From 0eed8ab37e23e7c67fd29a4e349146f887905de2 Mon Sep 17 00:00:00 2001 From: ybmin Date: Tue, 20 Aug 2024 23:04:18 +0900 Subject: [PATCH 4/7] Feat: null safety catch --- src/modules/fare.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/fare.js b/src/modules/fare.js index 0c577b4f..b86e9d9c 100644 --- a/src/modules/fare.js +++ b/src/modules/fare.js @@ -60,7 +60,7 @@ const initializeDatabase = async () => { { fare: true } ) .lean() - ).fare; + )?.fare; const fare = prevTaxiFare ? prevTaxiFare : await callTaxiFare(from, to); From 9e6af15679c3fd4c243af2aa3a094e5952b7f232 Mon Sep 17 00:00:00 2001 From: static Date: Tue, 20 Aug 2024 23:09:22 +0900 Subject: [PATCH 5/7] Fix: invalid location in sampleData.json --- src/sampleGenerator/sampleData.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sampleGenerator/sampleData.json b/src/sampleGenerator/sampleData.json index 546812cd..b2e84816 100644 --- a/src/sampleGenerator/sampleData.json +++ b/src/sampleGenerator/sampleData.json @@ -45,8 +45,8 @@ { "koName": "대전복합터미널", "enName": "Daejeon Terminal Complex", - "longitude": 127.350161, - "latitude": 36.362785 + "longitude": 127.436880, + "latitude": 36.349766 }, { "koName": "만년중학교", From d5c83464fab5303206b7dd39e56cbf5214e277df Mon Sep 17 00:00:00 2001 From: static Date: Tue, 20 Aug 2024 23:14:24 +0900 Subject: [PATCH 6/7] Refactor: return 0 when locations are same in getTaxiFareHandler --- src/services/fare.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/services/fare.js b/src/services/fare.js index 5150ca0b..1b8d599a 100644 --- a/src/services/fare.js +++ b/src/services/fare.js @@ -24,11 +24,15 @@ const getTaxiFareHandler = async (req, res) => { naverMapApi["X-NCP-APIGW-API-KEY"] === false || naverMapApi["X-NCP-APIGW-API-KEY-ID"] === false ) { - res.status(503).json({ + return res.status(503).json({ error: "fare/getTaxiFareHandler: Naver Map API credential not found", }); - return; } + + if (req.query.from === req.query.to) { + return res.status(200).json({ fare: 0 }); + } + const from = await locationModel .findOne({ _id: { $eq: req.query.from }, @@ -40,10 +44,9 @@ const getTaxiFareHandler = async (req, res) => { const sTime = scaledTime(new Date(req.query.time)); if (!from || !to) { - res + return res .status(400) .json({ error: "fare/getTaxiFareHandler: Wrong location" }); - return; } const fare = await taxiFareModel From d1efc8133326e0b292e5062bdd293cc4d8ae5a8e Mon Sep 17 00:00:00 2001 From: static Date: Tue, 20 Aug 2024 23:21:09 +0900 Subject: [PATCH 7/7] Refactor: refactoring --- src/modules/fare.js | 6 +++--- src/services/fare.js | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/modules/fare.js b/src/modules/fare.js index b86e9d9c..58132763 100644 --- a/src/modules/fare.js +++ b/src/modules/fare.js @@ -146,9 +146,6 @@ const updateTaxiFare = async (sTime, isMajor) => { await acc; await callTaxiFare(from, to) - .catch((err) => { - logger.error(err.message); - }) .then(async (fare) => { if (fare) { await taxiFareModel.updateOne( @@ -156,6 +153,9 @@ const updateTaxiFare = async (sTime, isMajor) => { { fare: fare } ); } + }) + .catch((err) => { + logger.error(err.message); }); await new Promise((resolve) => setTimeout(() => resolve, 200)); return acc; diff --git a/src/services/fare.js b/src/services/fare.js index 1b8d599a..d2bdca5b 100644 --- a/src/services/fare.js +++ b/src/services/fare.js @@ -29,10 +29,6 @@ const getTaxiFareHandler = async (req, res) => { }); } - if (req.query.from === req.query.to) { - return res.status(200).json({ fare: 0 }); - } - const from = await locationModel .findOne({ _id: { $eq: req.query.from }, @@ -47,6 +43,9 @@ const getTaxiFareHandler = async (req, res) => { return res .status(400) .json({ error: "fare/getTaxiFareHandler: Wrong location" }); + } else if (req.query.from === req.query.to) { + // 프론트엔드에서 예상 택시비를 숨기기 위해 0원을 반환 + return res.status(200).json({ fare: 0 }); } const fare = await taxiFareModel