From bb0d186b6268c6a80cbe357cfee47006342855a4 Mon Sep 17 00:00:00 2001 From: Jissin Mathew Date: Wed, 17 Jun 2026 17:01:34 +0530 Subject: [PATCH] Fix Completing a Job Throws an Exception MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes: - Refactored ServiceManagementService::updateJobStatus() to use tracked job card references instead of raw pointers, ensuring consistent state updates. - Added proper null checks and error handling for current job retrieval to prevent unexpected termination. - Updated logic to mark tracked job records as MODIFIED when status transitions occur (STARTED → IN_PROGRESS, IN_PROGRESS → COMPLETED). - Simplified control flow and indentation for better readability and maintainability. Fixes #2076 --- .../datastores/DataStore.h | 5 ++ .../services/ServiceManagementService.cpp | 74 +++++++++---------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h index 021d831..7093599 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.h @@ -220,6 +220,11 @@ void DataStore::refreshCache(util::Map>& cac if (oldIndex != -1) { TrackedRecord& oldRecord = oldCache.getValueAt(oldIndex); + if (oldRecord.state == RecordState::MODIFIED) + { + cache.insert(id, oldRecord); + continue; + } *oldRecord.data = *refreshedRecord.data; oldRecord.slotIndex = refreshedRecord.slotIndex; oldRecord.state = refreshedRecord.state; diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/ServiceManagementService.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/ServiceManagementService.cpp index f20c3d6..2518859 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/ServiceManagementService.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/services/ServiceManagementService.cpp @@ -880,44 +880,44 @@ Returns: void ServiceManagementService::updateJobStatus(const std::string& jobID) { DataStoreLockGuard lock(m_dataStore); - AuthenticationManagementService authenticationManagementService; - PaymentManagementService paymentManagementService; - bool jobStatusUpdated = false, serviceBookingCompleted; - JobCard* currentJob; - User* currentTechnician = authenticationManagementService.getAuthenticatedUser(); - if (currentTechnician == nullptr) - { - throw std::runtime_error("Unable to fetch current technician."); - } - util::Map currentAssignedJobs = getJobCards(currentTechnician->getId()); - if (currentAssignedJobs.getSize() == 0) - { - throw std::runtime_error("No job cards assigned to the technician."); - } + AuthenticationManagementService authenticationManagementService; + PaymentManagementService paymentManagementService; + bool jobStatusUpdated = false, serviceBookingCompleted; + User* currentTechnician = authenticationManagementService.getAuthenticatedUser(); + if (currentTechnician == nullptr) + { + throw std::runtime_error("Unable to fetch current technician."); + } + util::Map currentAssignedJobs = getJobCards(currentTechnician->getId()); + if (currentAssignedJobs.getSize() == 0) + { + throw std::runtime_error("No job cards assigned to the technician."); + } auto& trackedJobCards = m_dataStore.getJobCards(); auto& trackedServiceBookings = m_dataStore.getServiceBookings(); - if (currentAssignedJobs.find(jobID) != -1) - { + if (currentAssignedJobs.find(jobID) != -1) + { int jobIndex = trackedJobCards.find(jobID); if (jobIndex == -1) { throw std::runtime_error("Unable to fetch current job."); } - currentJob = currentAssignedJobs.getValueAt(currentAssignedJobs.find(jobID)); - if (currentJob == nullptr) - { - throw std::runtime_error("Unable to fetch current job."); - } - if (currentJob->getStatus() == util::ServiceJobStatus::STARTED) - { - currentJob->setStatus(util::ServiceJobStatus::IN_PROGRESS); - trackedJobCards.getValueAt(jobIndex).state = RecordState::MODIFIED; - jobStatusUpdated = true; - } + auto& trackedCurrentJob = trackedJobCards.getValueAt(jobIndex); + JobCard* currentJob = trackedCurrentJob.data; + if (currentJob == nullptr) + { + throw std::runtime_error("Unable to fetch current job."); + } + if (currentJob->getStatus() == util::ServiceJobStatus::STARTED) + { + currentJob->setStatus(util::ServiceJobStatus::IN_PROGRESS); + trackedCurrentJob.state = RecordState::MODIFIED; + jobStatusUpdated = true; + } else if (currentJob->getStatus() == util::ServiceJobStatus::IN_PROGRESS) { currentJob->setStatus(util::ServiceJobStatus::COMPLETED); - trackedJobCards.getValueAt(jobIndex).state = RecordState::MODIFIED; + trackedCurrentJob.state = RecordState::MODIFIED; jobStatusUpdated = true; serviceBookingCompleted = hasCompletedAllJobs(currentJob->getBookingId(), currentAssignedJobs); if (serviceBookingCompleted) @@ -931,15 +931,15 @@ void ServiceManagementService::updateJobStatus(const std::string& jobID) sendNotification(currentJob->getBooking()->getCustomer(), title, message); } } - } - else - { - throw std::runtime_error("Failed to update job status. Job may already be completed."); - } - if (!jobStatusUpdated) - { - throw std::runtime_error("Failed to update job status. Job may already be completed."); - } + } + else + { + throw std::runtime_error("Failed to update job status. Job may already be completed."); + } + if (!jobStatusUpdated) + { + throw std::runtime_error("Failed to update job status. Job may already be completed."); + } m_dataStore.saveJobCards(); m_dataStore.saveServiceBookings(); } \ No newline at end of file