def create(*, db_session, worker_in: WorkerCreate) -> Worker: """Creates an worker.""" flag, msg = _check_business_hour(worker_in.business_hour) if flag: log.error(f"{worker_in.code} , {msg}") raise HTTPException(status_code=400, detail=f"{worker_in.code} , {msg}") team = team_service.get_by_code(db_session=db_session, code=worker_in.team.code) if worker_in.location is not None: worker_in.location.org_id = worker_in.org_id location_obj = location_service.get_or_create_by_code( db_session=db_session, location_in=worker_in.location) else: location_obj = None if worker_in.dispatch_user: u = auth_service.get_by_email(db_session=db_session, email=worker_in.dispatch_user.email) else: u = None contact = Worker(**worker_in.dict( exclude={"flex_form_data", "team", "location", "dispatch_user"}), team=team, location=location_obj, dispatch_user=u, flex_form_data=worker_in.flex_form_data) db_session.add(contact) db_session.commit() return contact
def update_user(email: str, role: str): """Updates a user's roles.""" from dispatch.database.core import SessionLocal from dispatch.auth import service as user_service from dispatch.auth.models import UserUpdate db_session = SessionLocal() user = user_service.get_by_email(email=email, db_session=db_session) if not user: click.secho(f"No user found. Email: {email}", fg="red") return user_service.update(user=user, user_in=UserUpdate(id=user.id, role=role), db_session=db_session) click.secho("User successfully updated.", fg="green")
def reset_user_password(email: str, password: str): """Resets a user's password.""" from dispatch.database import SessionLocal from dispatch.auth import service as user_service from dispatch.auth.models import UserUpdate db_session = SessionLocal() user = user_service.get_by_email(email=email, db_session=db_session) if not user: click.secho(f"No user found. Email: {email}", fg="red") return user_service.update( user=user, user_in=UserUpdate(id=user.id, password=password), db_session=db_session ) click.secho("User successfully updated.", fg="green")
def update( *, db_session, worker: Worker, worker_in: WorkerUpdate, ) -> Worker: flag, msg = _check_business_hour(worker_in.business_hour) if flag: log.error(f"{worker_in.code} , {msg}") raise HTTPException(status_code=400, detail=f"{worker_in.code} , {msg}") update_data = worker_in.dict( exclude={"flex_form_data", "team", "location", "dispatch_user"}, ) if worker.team.code != worker_in.team.code: team = team_service.get_by_code(db_session=db_session, code=worker_in.team.code) worker.team = team if worker_in.location and (not worker.location or worker_in.location.location_code != worker.location.location_code): location_obj = location_service.get_or_create_by_code( db_session=db_session, location_in=worker_in.location) worker.location = location_obj if worker_in.dispatch_user and ( (worker.dispatch_user is None) or (worker.dispatch_user.email != worker_in.dispatch_user.email)): u = auth_service.get_by_email(db_session=db_session, email=worker_in.dispatch_user.email) worker.dispatch_user = u for field, field_value in update_data.items(): setattr(worker, field, field_value) worker.flex_form_data = worker_in.flex_form_data db_session.add(worker) db_session.commit() return worker
def create(*, db_session, location_in: LocationCreate) -> Location: location = location_in if type(location_in) != Location: if location_in.team is not None: team = get_by_code(db_session=db_session, code=location_in.team.code) else: team = None if location_in.dispatch_user is not None: user = auth_service.get_by_email( db_session=db_session, email=location_in.dispatch_user.email) else: user = None location = Location(**location_in.dict( exclude={"team", "dispatch_user"}), team=team, dispatch_user=user) db_session.add(location) db_session.commit() return location
def create_location( *, db_session: Session = Depends(get_db), location_in: LocationCreate, current_user: DispatchUser = Depends(get_current_user), ): """ Create a new location. """ location_in.org_id = current_user.org_id location = get_by_location_code(db_session=db_session, location_code=location_in.location_code) if location: raise HTTPException( status_code=400, detail= f"The location with this location_code ({location_in.location_code}) already exists.", ) if current_user.role == UserRoles.CUSTOMER: location_in.dispatch_user = auth_service.get_by_email( db_session=db_session, email=current_user.email) location = create(db_session=db_session, location_in=location_in) return location