Skip to content

Commit

Permalink
admin: add drug reference
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Sep 12, 2024
1 parent fc40037 commit 903452e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions routes/admin/drug.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ def update_price_factor():
)


@app_admin_drug.route("/admin/drug/ref", methods=["GET"])
@jwt_required()
def get_drug_ref():
user = User.find(get_jwt_identity())
dbSession.setSchema(user.schema)
os.environ["TZ"] = "America/Sao_Paulo"

sctid = request.args.get("sctid", None)

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

return {
"status": "success",
"data": ref,
}, status.HTTP_200_OK


@app_admin_drug.route("/admin/drug/copy-attributes", methods=["POST"])
@jwt_required()
def copy_attributes():
Expand Down
26 changes: 26 additions & 0 deletions services/admin/drug_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from utils import status
from sqlalchemy import and_, or_, func, text
from sqlalchemy.orm import undefer
from typing import List

from models.main import *
Expand Down Expand Up @@ -216,6 +217,31 @@ def get_drug_list(
return q.order_by(Drug.name, Segment.description).limit(limit).offset(offset).all()


def get_drug_ref(sctid: int, user: User):
if not permission_service.has_maintainer_permission(user):
raise ValidationError(
"Usuário não autorizado",
"errors.unauthorizedUser",
status.HTTP_401_UNAUTHORIZED,
)

subst = (
db.session.query(Substance)
.filter(Substance.id == sctid)
.options(undefer(Substance.admin_text))
.first()
)

if subst == None:
raise ValidationError(
"Substância inválido",
"errors.businessRules",
status.HTTP_400_BAD_REQUEST,
)

return {"name": subst.name, "ref": subst.admin_text}


def update_price_factor(id_drug, id_segment, factor, 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:
Expand Down
3 changes: 3 additions & 0 deletions services/admin/unit_conversion_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def get_conversion_list(id_segment, user, show_prediction=False):
units.c.idMeasureUnit,
MeasureUnitConvert.factor,
MeasureUnit.description,
Drug.sctid,
)
.join(active_drugs, Drug.id == active_drugs.c.idDrug)
.join(units, Drug.id == units.c.idDrug)
Expand All @@ -79,6 +80,7 @@ def get_conversion_list(id_segment, user, show_prediction=False):
),
)
.outerjoin(MeasureUnit, MeasureUnit.id == units.c.idMeasureUnit)
.outerjoin(Substance, Drug.sctid == Substance.id)
.order_by(Drug.name, MeasureUnitConvert.factor)
.all()
)
Expand All @@ -93,6 +95,7 @@ def get_conversion_list(id_segment, user, show_prediction=False):
"factor": i[4],
"idSegment": escape_html(id_segment),
"measureUnit": i[5],
"sctid": i.sctid,
}
)

Expand Down

0 comments on commit 903452e

Please sign in to comment.