def check_shop_manager(db: Session, shop_id: str, manager_id: str): current_shop = crud_shop.get_shop(db=db, shop_id=shop_id) if crud_channel_manager.get_channel_manager( db=db, channel_id=current_shop.channel_id, manager_id=manager_id) is None: return True return False
async def update_shop_sim( shop_id:str, sim_number:str, current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Update sim number of shop ''' current_shop=crud_shop.get_shop(db=db,shop_id=shop_id) if current_user.role=="manager": if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel" ) if crud_sim.get_sim_by_number(db=db,sim_number=sim_number) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Sim number Not Found" ) if current_shop is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not found" ) crud_shop.update_shop_sim(db=db,sim_number=sim_number,shop_id=shop_id) return {"message":"update success"}
def delete_channel_of_manager( manager_id: str, channel_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["channel_manager"]), db: Session = Depends(deps.get_db) ): manager=crud_user.get_user(db=db,user_id=manager_id) if manager is None or manager.role!="manager" : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id" ) for id in channel_id: if crud_channel.get_channel(db=db,channel_id=id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid channel id" ) for id in channel_id: if crud_channel_manager.get_channel_manager(db=db,channel_id=id,manager_id=manager_id) is None: if crud_channel.get_channel(db=db,channel_id=id): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Channel not asign to manager" ) for id in channel_id: crud_channel_manager.delete_channel_manager(db=db,manager_id=manager_id,channel_id=id) return {"message":"delete success"}
async def delete_executors_of_shop( executors_id: List[str], shop_id:str, current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Delete one or many executors to shop with shop id and list of executor id ''' invalid_list=[] if crud_shop.get_shop(db=db,shop_id=shopid) is None: raise UnicornException( messages="SHOP ID NOT FOUND", name=shopid ) current_shop=crud_shop.get_shop(db=db,shop_id=shop_id) if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise UnicornException( messages="SHOP ID NOT FOUND", name=shopid ) if len(executors_id) ==0: raise UnicornException( messages="INVALID EXECUTOR ID LIST", name=executors_id ) for id in executors_id: if crud_user.get_user(db=db,user_id=id)is None : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR NOT EXIST", name=invalid_list ) for id in executors_id: if crud_user.get_user(db=db,user_id=id).role!="executor" : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="USER IS NOT EXECUTOR", name=invalid_list ) for id in executors_id: if crud_shop_executor.get_shop_executor(db=db,shop_id=shop_id,executor_id=id) is None: invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR NOT BELONG TO SHOP", name=invalid_list ) for id in executors_id: crud_shop_executor.delete_shop_executor(db=db,shop_id=shop_id,executor_id=id) return {"message":"update success"}
async def add_many_executor_to_shop(executors_id: List[str], shop_id: str, token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): ''' Asign one or many executors to shop with shop id and list of executor id ''' try: check_sercurity_scopes(token=token, scopes=settings.ADD_EXECUTOR_SHOP_SCOPE) payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) current_shop = crud_shop.get_shop(db=db, shop_id=shop_id) if payload.get("role") == "manager": if crud_channel_manager.get_channel_manager( db=db, channel_id=current_shop.channel_id, manager_id=payload.get("id")) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel") if len(executors_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor id list must be not None") if current_shop is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist" + shop_id) for id in executors_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "executor": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid executor id") for id in executors_id: if not crud_shop_executor.get_shop_executor( db=db, shop_id=shop_id, executor_id=id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor already exist in shop") for id in executors_id: crud_shop_executor.create_new_shop_executor(db=db, shop_id=shop_id, executor_id=id) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "update success"}
async def add_many_executor_to_many_shop( executors_id: List[str], shop_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Asign one or many executors to one or many shops with list of executors and shops request ''' if len(executors_id) ==0 : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for executor id list" ) if len(shop_id) ==0 : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for shop id list" ) for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if current_shop is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist"+id ) if current_user.role=="manager": for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop doesn't belong to your channel" ) for id in executors_id: if crud_user.get_user(db=db,user_id=id) is None or crud_user.get_user(db=db,user_id=id).role!="executor": raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="User is Not executor" ) for eid in executors_id: for sid in shop_id: if not crud_shop_executor.get_shop_executor(db=db,shop_id=sid,executor_id=eid) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor already exist in shop" ) for eid in executors_id: for sid in shop_id: crud_shop_executor.create_new_shop_executor(db=db,shop_id=sid,executor_id=eid) return {"message":"update success"}
async def update_shop_sim(shop_id: str, sim_number: str, token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): ''' Update sim number of shop ''' try: current_user_id = check_sercurity_scopes( token=token, scopes=settings.UPDATE_SHOP_SIM_SCOPE) payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) current_shop = crud_shop.get_shop(db=db, shop_id=shop_id) if payload.get("role") == "manager": if crud_channel_manager.get_channel_manager( db=db, channel_id=current_shop.channel_id, manager_id=payload.get("id")) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel") if crud_sim.get_sim_by_number(db=db, sim_number=sim_number) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Sim number Not Found") if current_shop is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not found") crud_shop.update_shop_sim(db=db, sim_number=sim_number, shop_id=shop_id) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "update success"}
def delete_channel_of_manager(manager_id: str, channel_id: List[str], token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): try: check_sercurity_scopes(token=token, scopes=settings.DELETE_CHANNEL_MANAGER_SCOPE) manager = crud_user.get_user(db=db, user_id=manager_id) if manager is None or manager.role != "manager": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id") for id in channel_id: if crud_channel.get_channel(db=db, channel_id=id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid channel id") for id in channel_id: if crud_channel_manager.get_channel_manager( db=db, channel_id=id, manager_id=manager_id) is None: if crud_channel.get_channel(db=db, channel_id=id): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Channel not asign to manager") for id in channel_id: crud_channel_manager.delete_channel_manager(db=db, manager_id=manager_id, channel_id=id) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "delete success"}
async def delete_executors_of_shop( executors_id: List[str], shop_id:str, current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Delete one or many executors to shop with shop id and list of executor id ''' current_shop=crud_shop.get_shop(db=db,shop_id=shop_id) if current_user.role=="manager": if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel" ) if len(executors_id) ==0: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor id list must be not None" ) if current_shop is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist"+shop_id ) for id in executors_id: if crud_user.get_user(db=db,user_id=id)is None or crud_user.get_user(db=db,user_id=id).role!="executor" : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid executor id" ) for id in executors_id: if crud_shop_executor.get_shop_executor(db=db,shop_id=shop_id,executor_id=id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor not exist in shop" ) for id in executors_id: crud_shop_executor.delete_shop_executor(db=db,shop_id=shop_id,executor_id=id) return {"message":"update success"}
def delete_manager_from_channel( channel_id:str, managers_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["read_channel"]), db: Session = Depends(deps.get_db) ): invalid_list=[] if crud_channel.get_channel(db=db,channel_id=channel_id) is None: raise UnicornException( messages="CHANNEL ID NOT FOUND", name=channel_id ) if len(managers_id) ==0: raise UnicornException( messages="INVALID MANAGER ID LIST", name=managers_id ) for id in managers_id: if crud_user.get_user(db=db,user_id=id) is None or crud_user.get_user(db=db,user_id=id).role!="manager": invalid_list.append(id) if len(invalid_list) !=0: raise UnicornException( messages="INVALID MANAGER ID LIST", name=invalid_list ) for id in managers_id: if crud_channel_manager.get_channel_manager(db=db,channel_id=channel_id,manager_id=id) is None: invalid_list.append(id) if len(invalid_list) !=0: raise UnicornException( messages="MANAGER ID NOT FOUND IN CHANNEL", name=invalid_list ) for id in managers_id: crud_channel_manager.delete_channel_manager(db=db,manager_id=id,channel_id=channel_id) return {"message":"update success"}
def add_manager_from_channel(channel_id: str, managers_id: List[str], token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): try: check_sercurity_scopes(token=token, scopes=settings.ADD_CHANNEL_MANAGER_SCOPE) if len(managers_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "manager": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if not crud_channel_manager.get_channel_manager( db=db, channel_id=channel_id, manager_id=id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Manager already asign to channel") for id in managers_id: crud_channel_manager.create_channel_manager(db=db, manager_id=id, channel_id=channel_id) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "add success"}
def add_manager_from_channel(channel_id: str, managers_id: List[str], current_user=Security( deps.get_current_active_user, scopes=["read_channel"]), db: Session = Depends(deps.get_db)): if len(managers_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "manager": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if not crud_channel_manager.get_channel_manager( db=db, channel_id=channel_id, manager_id=id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Manager already asign to channel") for id in managers_id: crud_channel_manager.create_channel_manager(db=db, manager_id=id, channel_id=channel_id) return {"message": "add success"}
async def update_shop_sim( shop_id:str, sim_number:str, current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Update sim number of shop ''' current_shop=crud_shop.get_shop(db=db,shop_id=shop_id) if current_shop is None: raise UnicornException( messages="SHOP ID NOT FOUND", name=shop_id) if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise UnicornException( messages="SHOP NOT BELONG TO YOUR CHANNEL", name=shop_id) if crud_sim.get_sim_by_number(db=db,sim_number=sim_number) is None: raise UnicornException( messages="SIM NOT FOUND", name=sim_number) crud_shop.update_shop_sim(db=db,sim_number=sim_number,shop_id=shop_id) return {"message":"update success"}
async def add_many_executor_to_many_shop(executors_id: List[str], shop_id: List[str], token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): ''' Asign one or many executors to one or many shops with list of executors and shops request ''' try: current_user_id = check_sercurity_scopes( token=token, scopes=settings.ADD_EXECUTOR_SHOP_SCOPE) if len(executors_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for executor id list") if len(shop_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for shop id list") for id in shop_id: current_shop = crud_shop.get_shop(db=db, shop_id=id) if current_shop is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist" + id) if crud_channel_manager.get_channel_manager( db=db, channel_id=current_shop.channel_id, manager_id=current_user_id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop doesn't belong to your channel") for id in executors_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "executor": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="User is Not executor") for eid in executors_id: for sid in shop_id: if not crud_shop_executor.get_shop_executor( db=db, shop_id=sid, executor_id=eid) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor already exist in shop") for eid in executors_id: for sid in shop_id: crud_shop_executor.create_new_shop_executor(db=db, shop_id=sid, executor_id=eid) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "update success"}
async def add_many_executor_to_many_shop( executors_id: List[str], shop_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Asign one or many executors to one or many shops with list of executors and shops request ''' invalid_list=[] if len(executors_id) ==0 : raise UnicornException( messages="EXECUTOR ID LIST IS EMPTY", name=executors_id ) if len(shop_id) ==0 : raise UnicornException( messages="SHOP ID LIST IS EMPTY", name=executors_id ) for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if current_shop is None: invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="SOME SHOP NOT FOUND", name=invalid_list ) for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="SOME SHOP NOT BELONG TO YOUR CHANNEL", name=invalid_list ) for id in executors_id: if crud_user.get_user(db=db,user_id=id)is None : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR NOT EXIST", name=invalid_list ) for id in executors_id: if crud_user.get_user(db=db,user_id=id).role!="executor" : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="USER IS NOT EXECUTOR", name=invalid_list ) for eid in executors_id: for sid in shop_id: invalid_list.append([eid,sid]) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR&SHOP ALREADY ASIGN", name=invalid_list ) for eid in executors_id: for sid in shop_id: crud_shop_executor.create_new_shop_executor(db=db,shop_id=sid,executor_id=eid) return {"message":"update success"}