Skip to content

Commit

Permalink
Merge pull request #376 from noharm-ai/develop
Browse files Browse the repository at this point in the history
fix exception prioritization filter
  • Loading branch information
marceloarocha authored Sep 13, 2024
2 parents b01dcff + 66c1431 commit 6d2028a
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 16 deletions.
10 changes: 0 additions & 10 deletions models/appendix.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ class Notes(db.Model):
update = db.Column("update_at", db.DateTime, nullable=True)
user = db.Column("update_by", db.BigInteger, nullable=True)

def getDefaultNote(sctid):
defaultSchema = "hsc_test"

db_session = Session(db)
db_session.connection(
execution_options={"schema_translate_map": {None: defaultSchema}}
)
note = db_session.query(Notes).filter_by(idDrug=sctid, idSegment=5).first()
return note.notes if note else None


class Memory(db.Model):
__tablename__ = "memoria"
Expand Down
1 change: 1 addition & 0 deletions models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Substance(db.Model):
handling = deferred(
db.Column("manejo", postgresql.JSONB(none_as_null=True), nullable=True)
)
admin_text = deferred(db.Column("curadoria", db.String, nullable=True))


class SubstanceClass(db.Model):
Expand Down
5 changes: 4 additions & 1 deletion models/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,10 @@ def getPatients(
)

if len(idPatient) > 0:
q = q.filter(Prescription.idPatient.in_(idPatient))
try:
q = q.filter(Prescription.idPatient.in_([int(i) for i in idPatient]))
except ValueError:
q = q.filter(Prescription.idPatient == None)

if len(intervals) > 0:
q = q.filter(
Expand Down
1 change: 1 addition & 0 deletions routes/admin/admin_substance.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def get_substances():
handling_option=data.get("handlingOption", "filled"),
handling_type_list=data.get("handlingTypeList", []),
has_class=data.get("hasClass", None),
has_admin_text=data.get("hasAdminText", None),
)

return {"status": "success", "data": list}, status.HTTP_200_OK
Expand Down
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
3 changes: 0 additions & 3 deletions routes/outlier.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ def getOutliers(idSegment=1, idDrug=1):
defaultNote = None
if d and d[0].sctid:
relations = Relation.findBySctid(d[0].sctid, user)
defaultNote = (
Notes.getDefaultNote(d[0].sctid) if not user.permission() else None
)

if drugAttr is None:
drugAttr = DrugAttributes()
Expand Down
11 changes: 10 additions & 1 deletion services/admin/admin_substance_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def get_substances(
name=None,
idClassList=[],
has_class=None,
has_admin_text=None,
class_name=None,
handling_option="filled",
handling_type_list=[],
Expand Down Expand Up @@ -54,8 +55,14 @@ def get_substances(
else:
q = q.filter(Substance.idclass == None)

if has_admin_text != None:
if has_admin_text:
q = q.filter(Substance.admin_text != None)
else:
q = q.filter(Substance.admin_text == None)

q = (
q.options(undefer(Substance.handling))
q.options(undefer(Substance.handling), undefer(Substance.admin_text))
.order_by(Substance.name)
.limit(limit)
.offset(offset)
Expand Down Expand Up @@ -99,6 +106,7 @@ def upsert_substance(data: dict, user):
subs.active = data.get("active", None)
subs.link = data.get("link", None)
subs.handling = data.get("handling", None)
subs.admin_text = data.get("adminText", None)

if not subs.handling:
subs.handling = None
Expand Down Expand Up @@ -126,4 +134,5 @@ def _to_dto(s: Substance):
"active": s.active,
"link": s.link,
"handling": s.handling,
"adminText": s.admin_text,
}
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
11 changes: 10 additions & 1 deletion services/drug_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from models.main import db
from sqlalchemy import asc, distinct
from sqlalchemy.orm import undefer

from models.appendix import *
from models.prescription import *
Expand Down Expand Up @@ -167,7 +168,15 @@ def get_attributes(id_segment, id_drug, user):
attr = DrugAttributes.query.get((id_drug, id_segment))
drug_ref = None
if drug.sctid != None and permission_service.has_maintainer_permission(user):
drug_ref = Notes.getDefaultNote(drug.sctid)
subst = (
db.session.query(Substance)
.filter(Substance.id == drug.sctid)
.options(undefer(Substance.admin_text))
.first()
)

if subst != None:
drug_ref = subst.admin_text

if attr == None:
return {"drugRef": drug_ref}
Expand Down

0 comments on commit 6d2028a

Please sign in to comment.