Skip to content

Commit

Permalink
admin: find missing conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Nov 2, 2023
1 parent 5862182 commit a2237b0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
16 changes: 1 addition & 15 deletions routes/admin/drug.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_drug_list():
has_default_unit=request_data.get("hasDefaultUnit", None),
has_price_unit=request_data.get("hasPriceUnit", None),
has_inconsistency=request_data.get("hasInconsistency", None),
has_missing_conversion=request_data.get("hasMissingConversion", None),
attribute_list=request_data.get("attributeList", []),
term=request_data.get("term", None),
id_segment_list=request_data.get("idSegmentList", None),
Expand Down Expand Up @@ -145,21 +146,6 @@ def copy_unit_conversion():
return tryCommit(db, result.rowcount)


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

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

return tryCommit(db, result.rowcount)


@app_admin_drug.route("/admin/drug/copy-attributes", methods=["POST"])
@jwt_required()
def copy_attributes():
Expand Down
56 changes: 54 additions & 2 deletions services/admin/drug_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ def get_drug_list(
has_default_unit=None,
has_price_unit=None,
has_inconsistency=None,
has_missing_conversion=None,
attribute_list=[],
term=None,
limit=10,
offset=0,
id_segment_list=None,
):
SegmentOutlier = db.aliased(Segment)
ConversionsAgg = db.aliased(MeasureUnitConvert)

presc_query = (
db.session.query(
Outlier.idDrug.label("idDrug"), Outlier.idSegment.label("idSegment")
Expand All @@ -29,7 +33,26 @@ def get_drug_list(
.subquery()
)

SegmentOutlier = db.aliased(Segment)
conversions_query = (
db.session.query(
PrescriptionAgg.idDrug.label("idDrug"),
PrescriptionAgg.idSegment.label("idSegment"),
)
.select_from(PrescriptionAgg)
.outerjoin(
ConversionsAgg,
and_(
ConversionsAgg.idSegment == PrescriptionAgg.idSegment,
ConversionsAgg.idDrug == PrescriptionAgg.idDrug,
ConversionsAgg.idMeasureUnit == PrescriptionAgg.idMeasureUnit,
),
)
.filter(PrescriptionAgg.idSegment != None)
.filter(PrescriptionAgg.idMeasureUnit != None)
.filter(ConversionsAgg.factor == None)
.group_by(PrescriptionAgg.idDrug, PrescriptionAgg.idSegment)
.subquery()
)

q = (
db.session.query(
Expand Down Expand Up @@ -68,6 +91,15 @@ def get_drug_list(
.outerjoin(SegmentOutlier, SegmentOutlier.id == presc_query.c.idSegment)
)

if has_missing_conversion:
q = q.outerjoin(
conversions_query,
and_(
conversions_query.c.idDrug == presc_query.c.idDrug,
conversions_query.c.idSegment == presc_query.c.idSegment,
),
).filter(conversions_query.c.idDrug != None)

if has_substance != None:
if has_substance:
q = q.filter(Substance.id != None)
Expand Down Expand Up @@ -218,6 +250,8 @@ def add_default_units(user):
)
schema = user.schema

fix_inconsistency(user)

query = f"""
with unidades as (
select
Expand Down Expand Up @@ -259,7 +293,25 @@ def add_default_units(user):
and ma.fkunidademedida is null
"""

return db.session.execute(query)
insert_units = f"""
insert into {schema}.unidadeconverte
(idsegmento, fkmedicamento, fkunidademedida, fator)
select
m.idsegmento, m.fkmedicamento, m.fkunidademedida, 1
from
{schema}.medatributos m
where
m.fkunidademedida is not null
and m.fkunidademedida != ''
on conflict (idsegmento, fkmedicamento, fkunidademedida)
do nothing
"""

result = db.session.execute(query)

db.session.execute(insert_units)

return result


def copy_unit_conversion(id_segment_origin, id_segment_destiny, user):
Expand Down

0 comments on commit a2237b0

Please sign in to comment.