def deletar_vendedor(id: int, current_user: models.UsuarioResposta = Depends( util.get_current_user)): cur = connection_db.cur query = "select * from vendedor where id=%s" cur.execute(query, [id]) result = cur.fetchall() if not result: raise HTTPException(status_code=404, detail="Operation failed. Seller not found!") cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() query = "UPDATE vendedor SET data_deletacao=%s WHERE id=%s" cur.execute(query, [gDate, id]) conn.commit() util.create_log("Deletação", "vendedor", current_user.id, current_user.email, result[0][2]) return {"Successful operation. Delete seller!"}
def deletar_rota(id: int, current_user: models.UsuarioResposta = Depends( util.get_current_user)): routeDB = util.findExistedRoute(id) if not routeDB: raise HTTPException(status_code=404, detail="Operation failed. Route not found!") seller_route = routeDB[0].get('vendedor_id') if seller_route: raise HTTPException( status_code=400, detail= "Operation failed. Exist a seller associated with this route!") cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() query = "UPDATE rota SET data_deletacao=%s WHERE id=%s and vendedor_id is null" cur.execute(query, [gDate, id]) result = conn.commit() util.create_log("Edição", "rota", current_user.id, current_user.email, routeDB[0].get('nome')) return {"Successful operation. Delete route!"}
def login(form_data: OAuth2PasswordRequestForm = Depends()): userDB = util.findExistedUser(form_data.username) if not userDB: raise HTTPException(status_code=404, detail="User not found") user = models.UsuarioSenha(**userDB[0]) authorized = util.verify_password(form_data.password, user.senha) if not authorized: raise HTTPException(status_code=400, detail="E-mail or password invalid!") access_token_expires = util.timedelta(minutes=int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES"))) access_token = util.create_access_token( data={"sub": form_data.username, "is_adm": user.is_adm}, expires_delta=access_token_expires, ) util.create_log("Login", "login", int(userDB[0].get('id')), userDB[0].get('email'), "") results = { "access_token": access_token, "token_type": "bearer", "expired_in": int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES"))*60, "user_info": user } return results
def editar_cliente(id: int, cliente: models.Cliente): clientDb = util.findExistedClient(cliente.geolocalização, id) result = util.findClientContainsRoute(cliente.geolocalização) cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() if result.get('is_contains') == True: query = "UPDATE cliente SET nome=%s, geolocalizacao=%s, rota_id=%s, vendedor_id=%s, data_atualizacao=%s WHERE id=%s" cur.execute(query, [cliente.nome, cliente.geolocalização, result.get('rota_id'), result.get('vendedor_id'), gDate, id]) conn.commit() else: query = "UPDATE cliente SET nome=%s, geolocalizacao=%s, rota_id=%s, vendedor_id=%s, data_atualizacao=%s WHERE id=%s" cur.execute(query, [cliente.nome, cliente.geolocalização, 1, None, gDate, id]) conn.commit() util.create_log("Edição", "cliente", None, None, cliente.nome) return { **cliente.dict(), }
def editar_rota(id: int, rota: models.Rota, current_user: models.UsuarioResposta = Depends( util.get_current_user)): routeDB = util.findExistedRoute(id) if not routeDB: raise HTTPException(status_code=404, detail="Operation failed. Route not found!") util.findRouteIntersection(rota.bounds, routeDB[0].get('bounds')) cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() query = "UPDATE rota SET bounds=%s, nome=%s, data_atualizacao=%s WHERE id=%s" cur.execute(query, [rota.bounds, rota.nome, gDate, id]) conn.commit() util.create_log("Edição", "rota", current_user.id, current_user.email, rota.nome) return {**rota.dict()}
def criar_cliente(cliente: models.Cliente): clientDb = util.findExistedClient(cliente.geolocalização, None) if clientDb: raise HTTPException(status_code=400, detail="Client already exist!") result = util.findClientContainsRoute(cliente.geolocalização) cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() if result.get('is_contains') == True: cur.execute("INSERT INTO cliente (nome, geolocalizacao, rota_id, vendedor_id, data_criacao) VALUES(%s, %s, %s, %s, %s)", ( cliente.nome, cliente.geolocalização, result.get('rota_id'), result.get('vendedor_id'), gDate)) conn.commit() else: cur.execute("INSERT INTO cliente (nome, geolocalizacao, rota_id, vendedor_id, data_criacao) VALUES(%s, %s, %s, %s, %s)", ( cliente.nome, cliente.geolocalização, 1, None, gDate)) conn.commit() util.create_log("Criação", "cliente", None, None, cliente.nome) return { **cliente.dict(), }
def desassociar_vendedor_rota(rota_id: int, vendedor_id: int, current_user: models.UsuarioResposta = Depends( util.get_current_user)): routeDB = util.findExistedRoute(rota_id) if not routeDB: raise HTTPException(status_code=404, detail="Operation failed. Route not found!") sellerDB = util.findExistedSellerById(vendedor_id) if not sellerDB: raise HTTPException(status_code=404, detail="Operation failed. Seller not found!") cur = connection_db.cur conn = connection_db.conn query = "SELECT * from rota where id=%s and vendedor_id=%s and data_deletacao is null" cur.execute(query, [rota_id, vendedor_id]) result = cur.fetchall() if not result: raise HTTPException( status_code=404, detail= "Operation failed. Not exist association between the route and the seller!" ) gDate = datetime.now() try: query = "UPDATE rota SET vendedor_id=%s, data_atualizacao=%s WHERE id=%s and data_deletacao is null" cur.execute(query, [None, gDate, rota_id]) conn.commit() except: raise HTTPException( status_code=404, detail=("Operation failed! Not desassociated seller")) util.create_log("Desassociação Vendedor", "rota", current_user.id, current_user.email, routeDB[0].get('nome')) return {"Successful operation. Seller desassociated with route!"}
def criar_usuario(usuario: models.Usuario, current_user: models.UsuarioResposta = Depends(util.get_current_user)): userDB = util.findExistedUser(usuario.email) if userDB: raise HTTPException(status_code=400, detail="User email already exist!") cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() cur.execute("INSERT INTO usuario (email, senha, nome, is_adm, status, data_criacao) VALUES(%s, %s, %s, %s, %s, %s)", ( usuario.email, util.hashed_password(usuario.senha), usuario.nome, usuario.is_adm, usuario.status, gDate)) conn.commit() util.create_log("Criação", "usuario", current_user.id , current_user.email, usuario.email) return { **usuario.dict(), }
def criar_rota(rota: models.Rota, current_user: models.UsuarioResposta = Depends( util.get_current_user)): util.findRouteIntersection(rota.bounds, []) cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() cur.execute( "INSERT INTO rota (bounds, nome, data_criacao) VALUES(%s, %s, %s)", (rota.bounds, rota.nome, gDate)) conn.commit() util.create_log("Criação", "rota", current_user.id, current_user.email, rota.nome) return {**rota.dict()}
def criar_vendedor(vendedor: models.Vendedor, current_user: models.UsuarioResposta = Depends( util.get_current_user)): sellDB = util.findExistedSeller(vendedor.email) if sellDB: raise HTTPException(status_code=400, detail="Seller already exist!") cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() cur.execute( "INSERT INTO vendedor (nome, email, data_criacao) VALUES(%s, %s, %s)", (vendedor.nome, vendedor.email, gDate)) conn.commit() util.create_log("Criação", "vendedor", current_user.id, current_user.email, vendedor.email) return {**vendedor.dict()}
def associar_vendedor_rota(rota_id: int, vendedor_id: int, current_user: models.UsuarioResposta = Depends( util.get_current_user)): routeDB = util.findExistedRoute(rota_id) if not routeDB: raise HTTPException(status_code=404, detail="Operation failed. Route not found!") sellerDB = util.findExistedSellerById(vendedor_id) if not sellerDB: raise HTTPException(status_code=404, detail="Operation failed. Seller not found!") cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() try: query = "UPDATE rota SET vendedor_id=%s, data_atualizacao=%s WHERE id=%s and data_deletacao is null" cur.execute(query, [vendedor_id, gDate, rota_id]) conn.commit() except: raise HTTPException( status_code=404, detail=("Operation failed. Key (vendedor_id)=(" + str(vendedor_id) + ") is not present in table vendedor")) query = "UPDATE cliente SET vendedor_id=%s, data_atualizacao=%s WHERE rota_id=%s and data_deletacao is null" cur.execute(query, [vendedor_id, gDate, rota_id]) conn.commit() util.create_log("Associação Vendedor", "rota", current_user.id, current_user.email, routeDB[0].get('nome')) return {"Successful operation. Seller associated with route!"}
def deletar_cliente(id: int): clientDB = util.findExistedClient([], id) if not clientDB: raise HTTPException(status_code=404, detail="Operation failed. Client not found!") cur = connection_db.cur conn = connection_db.conn gDate = datetime.now() query = "UPDATE cliente SET data_deletacao=%s WHERE id=%s" cur.execute(query,[gDate, id]) conn.commit() util.create_log("Deletacão", "cliente", None, None, clientDB[0].get('nome')) return { "Successful operation. Delete client!" }