|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- from datetime import datetime
- from sqlalchemy.orm import Session
-
- from schemas.drivetask import CreateTask
- from db.models.drivetask import DriveTask
- from db.repository.user import get_user_by_id
-
-
- def create_new_task(task: CreateTask, db: Session):
- driver = get_user_by_id(task.DriverId, "Driver", db)
- if driver is None:
- return "notdriver"
- elif driver.Role != "Driver":
- return "notdriver"
- task_object = DriveTask(
- DriverId=task.DriverId,
- Description=task.Description,
- Status="Pending",
- StartLocation=task.StartLocation,
- EndLocation=task.EndLocation,
- )
- db.add(task_object)
- db.commit()
- db.refresh(task_object)
- return task_object
-
-
- def change_task_status(task_id: int, status: str, distance: int, db: Session):
- task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
- if not task:
- return "notaskfound"
- if status == "In Progress":
- # see if there are any other tasks in progress by this driver, if yes, cancel
- task.StartDateTime = datetime.now()
- if status == "Completed":
- task.DistanceCovered = distance
- task.EndDateTime = datetime.now()
- task.Status = status
- db.commit()
- db.refresh(task)
- return task
-
-
- def get_task_driver(task_id: int, db: Session):
- task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
- if not task:
- return "notaskfound"
- driver = get_user_by_id(task.DriverId, "Any", db)
- if not driver:
- return "notdriver"
- return driver
-
-
- def get_tasks_by_driver(driver_id: int, db: Session):
- driver = get_user_by_id(driver_id, "Any", db)
- if not driver:
- return "notdriver"
- if driver.Role != "Driver":
- return "notdriver"
- tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
- return tasks
-
-
- def get_task_by_id(task_id: int, db: Session):
- task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
- task.Driver = task.CreatedBy.__dict__
- task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle
- if not task:
- return "notaskfound"
- return task
-
-
- def get_all_tasks(status: str, db: Session):
- if status != "Any":
- tasks = db.query(DriveTask).filter(DriveTask.Status == status).all()
- else:
- tasks = db.query(DriveTask).all()
- for task in tasks:
- task.Driver = task.CreatedBy.__dict__
- task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle
- return tasks
-
-
- def edit_task(task_id: int, task: CreateTask, db: Session):
- tasks = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
- if not tasks:
- return "notaskfound"
- tasks.Description = task.Description
- tasks.StartLocation = task.StartLocation
- tasks.EndLocation = task.EndLocation
- db.commit()
- db.refresh(tasks)
- return tasks
-
-
- def get_active_route_by_driver(driver_id: int, db: Session):
- driver = get_user_by_id(driver_id, "Driver", db)
- if not driver:
- return "notdriver"
- tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
- for task in tasks:
- if task.Status == "In Progress":
- return task
- return "noroute"
-
-
- def get_my_routes(driver_id: int, db: Session):
- driver = get_user_by_id(driver_id, "Driver", db)
- if not driver:
- return "notdriver"
- tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
- return tasks
|