@@ -23,14 +23,18 @@ def create_maintenancejob( | |||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
if current_user.Role != "Maintenance" and current_user.Role != "Admin": | |||||
if current_user.Role != "Driver" and current_user.Role != "Admin": | |||||
raise HTTPException( | raise HTTPException( | ||||
status_code=403, detail="You are not authorized to perform this action" | status_code=403, detail="You are not authorized to perform this action" | ||||
) | ) | ||||
maintenancejob_res = create_new_maintenancejob( | maintenancejob_res = create_new_maintenancejob( | ||||
maintenancejob=maintenancejob, maintenanceworker=current_user.Id, db=db | |||||
maintenancejob=maintenancejob, db=db | |||||
) | ) | ||||
if maintenancejob_res == "nodriver": | |||||
raise HTTPException( | |||||
status_code=404, detail="This car has no driver" | |||||
) | |||||
return maintenancejob_res | return maintenancejob_res | ||||
@@ -74,13 +78,7 @@ def get_all_maintenancejobs( | |||||
def get_maintenancejob( | def get_maintenancejob( | ||||
maintenance_job_id: int, | maintenance_job_id: int, | ||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | |||||
): | ): | ||||
if current_user.Role != "Maintenance" and current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
maintenancejob = get_maintenance_job(maintenance_job_id, db) | maintenancejob = get_maintenance_job(maintenance_job_id, db) | ||||
print(maintenancejob) | print(maintenancejob) | ||||
return maintenancejob | return maintenancejob | ||||
@@ -93,11 +91,11 @@ def change_status( | |||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
if current_user.Role != "Maintenance" and current_user.Role != "Admin": | |||||
if current_user.Role != "Maintenance": | |||||
raise HTTPException( | raise HTTPException( | ||||
status_code=403, detail="You are not authorized to perform this action" | status_code=403, detail="You are not authorized to perform this action" | ||||
) | ) | ||||
result = change_maintenance_status(maintenance_job_id, status, db) | |||||
result = change_maintenance_status(maintenance_job_id, status, current_user.Id, db) | |||||
if result is None: | if result is None: | ||||
raise HTTPException(status_code=404, detail="Maintenance job not found") | raise HTTPException(status_code=404, detail="Maintenance job not found") | ||||
return result | return result |
@@ -7,10 +7,11 @@ class MaintenanceJob(Base): | |||||
Id = Column(Integer, primary_key=True, index=True) | Id = Column(Integer, primary_key=True, index=True) | ||||
# a list of weak entities of class CarPart | # a list of weak entities of class CarPart | ||||
CarParts = relationship("CarPart", back_populates="parent") | CarParts = relationship("CarPart", back_populates="parent") | ||||
CreatedBy = relationship("User", back_populates="maintenanceJobs", foreign_keys="MaintenanceJob.MaintenanceWorker") | |||||
FinishedBy = relationship("User", back_populates="maintenanceJobs", foreign_keys="MaintenanceJob.MaintenanceWorker") | |||||
VehicleID = Column(ForeignKey("vehicle.Id"), nullable=False) | VehicleID = Column(ForeignKey("vehicle.Id"), nullable=False) | ||||
Vehicle = relationship("Vehicle", back_populates="maintenanceJobs") | Vehicle = relationship("Vehicle", back_populates="maintenanceJobs") | ||||
VehicleDriverId = Column(ForeignKey("user.Id"), nullable=False) | VehicleDriverId = Column(ForeignKey("user.Id"), nullable=False) | ||||
Description = Column(String, nullable=False) | Description = Column(String, nullable=False) | ||||
Date = Column(DateTime, nullable=False) | Date = Column(DateTime, nullable=False) | ||||
MaintenanceWorker = Column(ForeignKey("user.Id"), nullable=False) | |||||
MaintenanceWorker = Column(ForeignKey("user.Id"), nullable=True) | |||||
Status = Column(String, nullable=False) |
@@ -25,7 +25,7 @@ class User(Base): | |||||
driveTasks = relationship("DriveTask", back_populates="CreatedBy") | driveTasks = relationship("DriveTask", back_populates="CreatedBy") | ||||
vehicle = relationship("Vehicle", back_populates="driver") | vehicle = relationship("Vehicle", back_populates="driver") | ||||
#MaintenancePerson-specific relationships | #MaintenancePerson-specific relationships | ||||
maintenanceJobs = relationship("MaintenanceJob", back_populates="CreatedBy", foreign_keys="MaintenanceJob.MaintenanceWorker") | |||||
maintenanceJobs = relationship("MaintenanceJob", back_populates="FinishedBy", foreign_keys="MaintenanceJob.MaintenanceWorker") | |||||
#FuelingPerson-specific relationships | #FuelingPerson-specific relationships | ||||
fuelingTasks = relationship("FuelingTask", back_populates="CreatedBy", foreign_keys="FuelingTask.CreatedById") | fuelingTasks = relationship("FuelingTask", back_populates="CreatedBy", foreign_keys="FuelingTask.CreatedById") | ||||
@@ -8,14 +8,19 @@ from db.repository.user import get_car_driver | |||||
def create_new_maintenancejob( | def create_new_maintenancejob( | ||||
maintenancejob: CreateMaintenanceJob, maintenanceworker, db: Session | |||||
maintenancejob: CreateMaintenanceJob, db: Session | |||||
): | ): | ||||
vehicledriver = get_car_driver(maintenancejob.VehicleID, db) | |||||
if (vehicledriver != False): | |||||
vehicledriver = vehicledriver.Id | |||||
else: | |||||
return "nodriver" | |||||
maintenancejob_object = MaintenanceJob( | maintenancejob_object = MaintenanceJob( | ||||
MaintenanceWorker=maintenanceworker, | |||||
Description=maintenancejob.Description, | Description=maintenancejob.Description, | ||||
VehicleID=maintenancejob.VehicleID, | VehicleID=maintenancejob.VehicleID, | ||||
Date=maintenancejob.Date, | Date=maintenancejob.Date, | ||||
VehicleDriverId=get_car_driver(maintenancejob.VehicleID, db).Id, | VehicleDriverId=get_car_driver(maintenancejob.VehicleID, db).Id, | ||||
Status="Requested", | |||||
) | ) | ||||
print("OBJECT CREATED") | print("OBJECT CREATED") | ||||
db.add(maintenancejob_object) | db.add(maintenancejob_object) | ||||
@@ -78,19 +83,27 @@ def get_maintenance_job(maintenancejob_id: int, db: Session): | |||||
# print(type(result.CarPartsList)) | # print(type(result.CarPartsList)) | ||||
res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts) | res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts) | ||||
# print(result.TotalCost) | # print(result.TotalCost) | ||||
res["AssignedTo"] = maintenancejob.CreatedBy.__dict__ | |||||
if maintenancejob.Status == "Complete": | |||||
res["FinishedBy"] = maintenancejob.FinishedBy.__dict__ | |||||
res["Vehicle"] = maintenancejob.Vehicle.__dict__ | res["Vehicle"] = maintenancejob.Vehicle.__dict__ | ||||
print("DB Access complete") | print("DB Access complete") | ||||
return maintenancejob | return maintenancejob | ||||
def change_maintenance_status(maintenancejob_id: int, status: str, db: Session): | |||||
def change_maintenance_status(maintenancejob_id: int, status: str, worker_id: int, db: Session): | |||||
maintenancejob = ( | maintenancejob = ( | ||||
db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first() | db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first() | ||||
) | ) | ||||
if maintenancejob is None: | if maintenancejob is None: | ||||
return None | return None | ||||
maintenancejob.Status = status | |||||
db.commit() | |||||
db.refresh(maintenancejob) | |||||
return maintenancejob | |||||
if (status == "Complete"): | |||||
maintenancejob.MaintenanceWorker = worker_id | |||||
maintenancejob.Status = status | |||||
db.commit() | |||||
db.refresh(maintenancejob) | |||||
return maintenancejob | |||||
else: | |||||
return None | |||||
@@ -9,7 +9,6 @@ from schemas.vehicle import OutputVehicle | |||||
class CreateMaintenanceJob(BaseModel): | class CreateMaintenanceJob(BaseModel): | ||||
Description: str = Field(...) | Description: str = Field(...) | ||||
VehicleID: int = Field(...) | VehicleID: int = Field(...) | ||||
VehicleDriverId: int = Field(...) | |||||
Date: datetime = Field(...) | Date: datetime = Field(...) | ||||
@@ -20,4 +19,5 @@ class OutputMaintenanceJob(BaseModel): | |||||
CarPartsList: Optional[List[ShowCarPart]] | CarPartsList: Optional[List[ShowCarPart]] | ||||
TotalCost: int | TotalCost: int | ||||
Vehicle: OutputVehicle | Vehicle: OutputVehicle | ||||
AssignedTo: ShowUser | |||||
FinishedBy: Optional[ShowUser] | |||||
Status: str |