diff --git a/routes/admin/drug.py b/routes/admin/drug.py index 0ce7b23d..a67d41e5 100644 --- a/routes/admin/drug.py +++ b/routes/admin/drug.py @@ -97,3 +97,23 @@ def add_default_units(): return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus return tryCommit(db, result.rowcount) + + +@app_admin_drug.route("/admin/drug/copy-unit-conversion", methods=["POST"]) +@jwt_required() +def copy_unit_conversion(): + data = request.get_json() + user = User.find(get_jwt_identity()) + dbSession.setSchema(user.schema) + os.environ["TZ"] = "America/Sao_Paulo" + + try: + result = drug_service.copy_unit_conversion( + user=user, + id_segment_origin=data.get("idSegmentOrigin", None), + id_segment_destiny=data.get("idSegmentDestiny", None), + ) + except ValidationError as e: + return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus + + return tryCommit(db, result.rowcount) diff --git a/services/admin/drug_service.py b/services/admin/drug_service.py index 663defba..e9b17d0f 100644 --- a/services/admin/drug_service.py +++ b/services/admin/drug_service.py @@ -204,3 +204,62 @@ def add_default_units(user): """ return db.session.execute(query) + + +def copy_unit_conversion(id_segment_origin, id_segment_destiny, user): + roles = user.config["roles"] if user.config and "roles" in user.config else [] + if RoleEnum.ADMIN.value not in roles and RoleEnum.TRAINING.value not in roles: + raise ValidationError( + "Usuário não autorizado", + "errors.unauthorizedUser", + status.HTTP_401_UNAUTHORIZED, + ) + schema = user.schema + + if id_segment_origin == None or id_segment_destiny == None: + raise ValidationError( + "Segmento Inválido", "errors.invalidRecord", status.HTTP_400_BAD_REQUEST + ) + + if id_segment_origin == id_segment_destiny: + raise ValidationError( + "Segmento origem deve ser diferente do segmento destino", + "errors.invalidRecord", + status.HTTP_400_BAD_REQUEST, + ) + + query = f""" + with conversao_origem as ( + select + ma.fkmedicamento, ma.idsegmento, ma.fkunidademedida + from + {schema}.medatributos ma + inner join {schema}.medatributos madestino on ( + ma.fkmedicamento = madestino.fkmedicamento + and madestino.idsegmento = :idSegmentDestiny + and ma.fkunidademedida = madestino.fkunidademedida + ) + where + ma.idsegmento = :idSegmentOrigin + ) + insert into {schema}.unidadeconverte (idsegmento, fkunidademedida, fator, fkmedicamento) ( + select + :idSegmentDestiny as idsegmento, + u.fkunidademedida, + u.fator, + u.fkmedicamento + from + {schema}.unidadeconverte u + inner join conversao_origem on ( + u.fkmedicamento = conversao_origem.fkmedicamento + and u.idsegmento = conversao_origem.idsegmento + ) + ) + on conflict (fkunidademedida, idsegmento, fkmedicamento) + do update set fator = excluded.fator + """ + + return db.session.execute( + query, + {"idSegmentOrigin": id_segment_origin, "idSegmentDestiny": id_segment_destiny}, + )