Skip to content

Commit

Permalink
admin copy conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Oct 16, 2023
1 parent 3166e1c commit f370ae8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions routes/admin/drug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
59 changes: 59 additions & 0 deletions services/admin/drug_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)

0 comments on commit f370ae8

Please sign in to comment.