diff --git a/routes/prescription.py b/routes/prescription.py index e8a7fcf5..24c60538 100644 --- a/routes/prescription.py +++ b/routes/prescription.py @@ -818,84 +818,12 @@ def getPrescriptionUpdate(idPrescription): user = User.find(get_jwt_identity()) dbSession.setSchema(user.schema) - p = Prescription.query.get(idPrescription) - if p is None: - return { - "status": "error", - "message": "Prescrição Inexistente!", - }, status.HTTP_400_BAD_REQUEST - - if p.agg: - if user.cpoe(): - query = text( - "INSERT INTO " - + user.schema - + ".presmed \ - SELECT pm.*\ - FROM " - + user.schema - + ".presmed pm\ - WHERE fkprescricao IN (\ - SELECT fkprescricao\ - FROM " - + user.schema - + ".prescricao p\ - WHERE p.nratendimento = :admissionNumber" - + "\ - AND p.idsegmento IS NOT NULL \ - AND " - + "date(:prescDate)\ - BETWEEN p.dtprescricao::date AND COALESCE(p.dtvigencia::date, " - + "date(:prescDate))\ - );" - ) - - db.session.execute( - query, {"admissionNumber": p.admissionNumber, "prescDate": p.date} - ) - else: - query = text( - "INSERT INTO " - + user.schema - + ".presmed \ - SELECT pm.*\ - FROM " - + user.schema - + ".presmed pm\ - WHERE fkprescricao IN (\ - SELECT fkprescricao\ - FROM " - + user.schema - + ".prescricao p\ - WHERE p.nratendimento = :admissionNumber" - + "\ - AND p.idsegmento IS NOT NULL \ - AND (\ - p.dtprescricao::date = " - + "date(:prescDate) OR\ - p.dtvigencia::date = " - + "date(:prescDate)\ - )\ - );" - ) - - db.session.execute( - query, {"admissionNumber": p.admissionNumber, "prescDate": p.date} - ) - else: - query = text( - "INSERT INTO " - + user.schema - + ".presmed \ - SELECT *\ - FROM " - + user.schema - + ".presmed\ - WHERE fkprescricao = :idPrescription" - + ";" + try: + prescription_service.recalculate_prescription( + id_prescription=idPrescription, user=user ) - - db.session.execute(query, {"idPrescription": idPrescription}) + except ValidationError as e: + return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus return tryCommit(db, escape_html(str(idPrescription))) diff --git a/services/prescription_service.py b/services/prescription_service.py index ebb54881..3e7ac178 100644 --- a/services/prescription_service.py +++ b/services/prescription_service.py @@ -458,3 +458,92 @@ def review_prescription(idPrescription, user, review_type, evaluation_time): "reviewedAt": datetime.today().isoformat(), "reviewedBy": db_user.name, } + + +def recalculate_prescription(id_prescription: int, user: User): + p = Prescription.query.get(id_prescription) + if p is None: + raise ValidationError( + "Prescrição inexistente.", + "errors.invalidRegister", + status.HTTP_400_BAD_REQUEST, + ) + + if p.agg: + if user.cpoe(): + prescription_results = get_query_prescriptions_by_agg( + agg_prescription=p, is_cpoe=user.cpoe(), only_id=True + ).all() + + prescription_ids = [] + for item in prescription_results: + prescription_ids.append(item.id) + + db.session.query(Prescription).filter( + Prescription.id.in_(prescription_ids) + ).filter(Prescription.idSegment == None).update( + { + "idHospital": p.idHospital, + "idDepartment": p.idDepartment, + "idSegment": p.idSegment, + }, + synchronize_session="fetch", + ) + + db.session.flush() + + query = text( + f"""INSERT INTO {user.schema}.presmed + SELECT + pm.* + FROM + {user.schema}.presmed pm + WHERE + fkprescricao = ANY(:prescriptionIds) + """ + ) + + db.session.execute(query, {"prescriptionIds": prescription_ids}) + else: + query = text( + "INSERT INTO " + + user.schema + + ".presmed \ + SELECT pm.*\ + FROM " + + user.schema + + ".presmed pm\ + WHERE fkprescricao IN (\ + SELECT fkprescricao\ + FROM " + + user.schema + + ".prescricao p\ + WHERE p.nratendimento = :admissionNumber" + + "\ + AND p.idsegmento IS NOT NULL \ + AND (\ + p.dtprescricao::date = " + + "date(:prescDate) OR\ + p.dtvigencia::date = " + + "date(:prescDate)\ + )\ + );" + ) + + db.session.execute( + query, {"admissionNumber": p.admissionNumber, "prescDate": p.date} + ) + else: + query = text( + "INSERT INTO " + + user.schema + + ".presmed \ + SELECT *\ + FROM " + + user.schema + + ".presmed\ + WHERE fkprescricao = :idPrescription" + + ";" + ) + + db.session.execute(query, {"idPrescription": p.id})