def get_open_cases_to_send(clinic_id, zone, last_sync): """ Given a list of patients, get the open/updated cases since the last sync operation. This returns tuples phone_case objects, and flags that say whether or not they should be created """ to_return = [] case_ids = [] # find all relevant cases for the CHW and send them to the phone, unless # they have already been sent and haven't changed. potential_case_list = PatientCase.view_with_patient("case/open_for_chw_for_phone", key=[clinic_id, zone]) last_sync_date = datetime.min if not last_sync else last_sync.date for case in potential_case_list: # keep a running list of case ids sent down because the phone doesn't # deal well with duplicates. There shouldn't be duplicates, but they # can come up with bugs, so arbitrarily only send down the first case # if there are any duplicates if case.get_id not in case_ids: phone_case = PhoneCase.from_bhoma_case(case) if phone_case is None: # we don't expect to get into this scenario, but it can happen # don't fail hard with null reference below continue previously_synced = case_previously_synced(phone_case.case_id, last_sync) if phone_case and phone_case.is_started() and not phone_case.is_over(): # this is an active case, so send it unless it's a duplicate # or was already synced if phone_case.case_id in case_ids: logging.warning("Found a duplicate case for %s. Will not be sent to phone." % phone_case.case_id) elif previously_synced and phone_case.date_modified < last_sync_date: logging.debug("Case %s already sent to phone and no changes. Won't be sent again" % phone_case.case_id) else: case_ids.append(phone_case.case_id) to_return.append((phone_case, not previously_synced)) return to_return
def cases_for_patient(patient_id): """ Get the list of open cases for a particular patient """ return PatientCase.view_with_patient("case/open_for_patient", key=patient_id).all()
def cases_for_chw(chw): """ From chw clinic zone, get the list of open cases """ key = [chw.current_clinic_id, chw.current_clinic_zone] return PatientCase.view_with_patient("case/open_for_chw", key=key).all()