Skip to content

Commit

Permalink
admin: add default units
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Oct 14, 2023
1 parent 75c8a80 commit 88c93a6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions routes/admin/drug.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,18 @@ def update_price_factor():
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, {"idSegment": id_segment, "idDrug": id_drug, "factor": factor})


@app_admin_drug.route("/admin/drug/add-default-units", methods=["POST"])
@jwt_required()
def add_default_units():
user = User.find(get_jwt_identity())
dbSession.setSchema(user.schema)
os.environ["TZ"] = "America/Sao_Paulo"

try:
result = drug_service.add_default_units(user=user)
except ValidationError as e:
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, result.rowcount)
54 changes: 54 additions & 0 deletions services/admin/drug_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,57 @@ def update_price_factor(id_drug, id_segment, factor, user):
conversion.factor = factor

db.session.flush()


def add_default_units(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

query = f"""
with unidades as (
select
fkmedicamento,
idsegmento,
min(fkunidademedida) as fkunidademedida
from (
select
pagg.fkmedicamento,
pagg.idsegmento,
pagg.fkunidademedida
from
{schema}.prescricaoagg pagg
where
pagg.fkmedicamento in (
select fkmedicamento from {schema}.medatributos m where m.fkunidademedida is null
)
group by
pagg.fkmedicamento,
pagg.idsegmento,
pagg.fkunidademedida
) a
where
fkunidademedida is not null
group by
fkmedicamento,
idsegmento
having count(*) = 1
)
update
{schema}.medatributos ma
set
fkunidademedida = unidades.fkunidademedida
from
unidades
where
ma.fkmedicamento = unidades.fkmedicamento
and ma.idsegmento = unidades.idsegmento
and ma.fkunidademedida is null
"""

return db.session.execute(query)

0 comments on commit 88c93a6

Please sign in to comment.