Skip to content

Commit

Permalink
Refactor: fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
ybmin committed Aug 13, 2024
1 parent 147fc5c commit de3329f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 46 deletions.
10 changes: 3 additions & 7 deletions src/modules/fare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -164,6 +164,7 @@ const updateTaxiFare = async (sTime, isMajor) => {
);
}
})
.clone()
.catch((err) => {
logger.error(err.message);
});
Expand All @@ -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;
Expand Down
59 changes: 20 additions & 39 deletions src/services/fare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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)
Expand All @@ -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 });
Expand All @@ -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) {
Expand Down

0 comments on commit de3329f

Please sign in to comment.