Skip to content

Commit

Permalink
Merge pull request #532 from sparcs-kaist/#529-예상-택시-비용-쿼리-오류-해결
Browse files Browse the repository at this point in the history
예상 택시 비용 쿼리 오류 해결
  • Loading branch information
kmc7468 authored Aug 20, 2024
2 parents 147fc5c + d1efc81 commit ed177db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 77 deletions.
23 changes: 4 additions & 19 deletions src/modules/fare.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const initializeDatabase = async () => {
{ fare: true }
)
.lean()
).fare;
)?.fare;
const fare = prevTaxiFare
? prevTaxiFare
: await callTaxiFare(from, to);
Expand Down Expand Up @@ -145,22 +145,12 @@ const updateTaxiFare = async (sTime, isMajor) => {
const to = await locationModel.findOne({ _id: item.to });

await acc;
await callTaxiFare
.catch((err) => {
logger.error(err.message);
})
await callTaxiFare(from, to)
.then(async (fare) => {
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 }
);
}
})
Expand Down Expand Up @@ -189,12 +179,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
4 changes: 2 additions & 2 deletions src/sampleGenerator/sampleData.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
{
"koName": "대전복합터미널",
"enName": "Daejeon Terminal Complex",
"longitude": 127.350161,
"latitude": 36.362785
"longitude": 127.436880,
"latitude": 36.349766
},
{
"koName": "만년중학교",
Expand Down
79 changes: 23 additions & 56 deletions src/services/fare.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ 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;
}

const from = await locationModel
.findOne({
_id: { $eq: req.query.from },
Expand All @@ -40,46 +40,21 @@ 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;
} else if (req.query.from === req.query.to) {
// 프론트엔드에서 예상 택시비를 숨기기 위해 0원을 반환
return res.status(200).json({ fare: 0 });
}
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 })
.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,27 +63,19 @@ 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
.findOne(
{
from: from._id,
to: to._id,
time: 0,
},
(err, docs) => {
if (err)
logger.error(
"Error occured while finding Taxi Fare documents: " +
err.message
);
}
)
const minorTaxiFare = await taxiFareModel
.findOne({
from: from._id,
to: to._id,
time: 48 * new Date(req.query.time).getDay() + 0,
})
.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 +84,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 ed177db

Please sign in to comment.