From a2237b060b87961e138b4cff4e9cee8c1091fb39 Mon Sep 17 00:00:00 2001 From: Marcelo Arocha Date: Thu, 2 Nov 2023 20:07:58 -0300 Subject: [PATCH] admin: find missing conversions --- routes/admin/drug.py | 16 +--------- services/admin/drug_service.py | 56 ++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/routes/admin/drug.py b/routes/admin/drug.py index b8f15fd2..8edb9b07 100644 --- a/routes/admin/drug.py +++ b/routes/admin/drug.py @@ -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), @@ -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(): diff --git a/services/admin/drug_service.py b/services/admin/drug_service.py index 821cbf48..3f55dd86 100644 --- a/services/admin/drug_service.py +++ b/services/admin/drug_service.py @@ -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") @@ -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( @@ -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) @@ -218,6 +250,8 @@ def add_default_units(user): ) schema = user.schema + fix_inconsistency(user) + query = f""" with unidades as ( select @@ -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):