Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix update schedule detail #32

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions server/src/controllers/schedule.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ScheduleController {

updateSchedule = async (req, res, next) => {
try {
const id = req.params.id;
// const id = req.params.id;
const {
tour_id, schedule_date, range_time, note, status
} = req.body
Expand All @@ -89,15 +89,14 @@ class ScheduleController {

const schedule = await Schedule.findOne({
where: {
id,
tour_id
}
})
if (!schedule) {
return res.status(404).json({ message: "Not found schedule for updating!" })
}

for (const scheduleItem of schedule_detail) {
for (const scheduleItem of schedule.schedule_detail) {
for (const detail of scheduleItem.detail) {
const name = detail.name;

Expand Down Expand Up @@ -144,50 +143,52 @@ class ScheduleController {

updateScheduleDetails = async (req, res, next) => {
try {
const { tour_id, schedule_detail: updatedScheduleDetails } = req.body;

const schedule = await Schedule.findOne({
where: { tour_id }
});
const { tour_id, schedule_detail } = req.body;

const schedule = await Schedule.findOne({ where: { tour_id } });

if (!schedule) {
return res.status(404).json({ message: "Schedule not found for the specified tour" });
return res.status(404).json({ message: "Schedule not found!" });
}

let status_response
const scheduleDetail = schedule.schedule_detail;
for (const updatedSchedule of updatedScheduleDetails) {
const { date, detail } = updatedSchedule;

const currentSchedule = scheduleDetail.find(s => s.date === date);
if (!currentSchedule) {
continue;
}

for (const updatedDetail of detail) {
const { range_time, note, status } = updatedDetail;
status_response = status
const currentDetail = currentSchedule.detail.find(d => d.range_time === range_time);
if (currentDetail) {
currentDetail.note = note !== undefined ? note : currentDetail.note;
currentDetail.status = status !== undefined ? status : currentDetail.status;
}
let scheduleDetails = schedule.schedule_detail;
schedule.schedule_detail = [];
await schedule.save()

for (const detailUpdate of schedule_detail) {
const { date, detail } = detailUpdate;
for (const item of detail) {
const { range_time, note, status } = item;

scheduleDetails = scheduleDetails.map(sd => {
if (sd.date === date) {
sd.detail = sd.detail.map(d => {
if (d.range_time === range_time) {
if (note !== undefined) d.note = note;
if (status !== undefined) d.status = status;
}
return d;
});
}
return sd;
});
}
}

schedule.schedule_detail = scheduleDetail;
schedule.schedule_detail = scheduleDetails;
await schedule.save();

return res.status(200).json({
message: "Schedule updated successfully",
schedule,
status: status_response
message: "Schedule updated successfully!",
schedule
});

} catch (error) {
return res.status(500).json({ message: error.message });
}
};



deleteSchedule = async(req, res, next) => {
try {
Expand All @@ -204,7 +205,6 @@ class ScheduleController {
const { city } = req.query
const apiKey = process.env.API_KEY_WEATHER
const encodedCity = encodeURIComponent(city);
console.log(`apiKey:::`, city)
const weatherURL = `https://api.openweathermap.org/data/2.5/weather?q=${encodedCity}&units=metric&APPID=${apiKey}`;
try {
const response = await fetch(weatherURL)
Expand Down
20 changes: 12 additions & 8 deletions server/src/controllers/tour.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,25 @@ class TourController {
}
}

getScheduleByIdTour = async(req, res, next) => {
const tour_id = req.params.tour_id
getScheduleByIdTour = async (req, res, next) => {
const tour_id = req.params.tour_id;
try {
const schedule_tour = await Schedule.findOne({
where: { tour_id: tour_id }
})
});

if (!schedule_tour) return res.status(404).json({ message: "Not found schedule of tour!" });

console.log('Fetched Schedule Details:', schedule_tour.schedule_detail); // Ghi log dữ liệu đã truy xuất

if (!schedule_tour) return res.status(404).json({ message: "Not found schedule of tour!" })
return res.status(200).json({
schedule_tour: schedule_tour
})
} catch(error) {
return res.status(500).json({ message: error.message })
});
} catch (error) {
return res.status(500).json({ message: error.message });
}
}
};


getAllTours = async(req, res, next) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const scheduleController = require("../../controllers/schedule.controller")
const { authenticate } = require("../../middlewares/authenticate")

router.post("/", authenticate, asyncHandler(scheduleController.createSchedule))
router.put("/:id", authenticate, asyncHandler(scheduleController.updateScheduleDetails))
router.put("/", authenticate, asyncHandler(scheduleController.updateScheduleDetails))
router.get("/weather", asyncHandler(scheduleController.getWeatherData))

module.exports = router
Loading