Esempio n. 1
0
def test_no_events_9_months_passed():

    treatment_start = '2020-04-10'

    test_contract = {
            "os_payable": 0.1,
            "no_os_payable": 0.2,
            "pfs_payable": 0.3,
            "no_pfs_payable": 0.4,
            "treatment_start": treatment_start
    }

    events = []

    assert check_contract_status(test_contract, events) == test_contract["pfs_payable"]
Esempio n. 2
0
def test_patient_progressed_before_month_9_contract_running_12_months():

    treatment_start = '2020-01-01'
    event_date = parser.parse('2020-03-10').date()

    test_contract = {
            "os_payable": 0.1,
            "no_os_payable": 0.2,
            "pfs_payable": 0.3,
            "no_pfs_payable": 0.4,
            "treatment_start": treatment_start
    }

    events = [(0, 0, event_date, "progressed")]

    assert check_contract_status(test_contract, events) == test_contract["os_payable"]
Esempio n. 3
0
def test_patient_dead_after_month_9():

    treatment_start = '2020-03-10'
    event_date = parser.parse('2021-01-10').date()

    test_contract = {
            "os_payable": 0.1,
            "no_os_payable": 0.2,
            "pfs_payable": 0.3,
            "no_pfs_payable": 0.4,
            "treatment_start": treatment_start
    }

    events = [(0, 0, event_date, "dead")]

    assert check_contract_status(test_contract, events) == test_contract["pfs_payable"]
Esempio n. 4
0
def test_patient_progressed_before_and_dead_after_month_9():

    treatment_start = '2020-01-01'
    event_1_date = parser.parse('2020-04-10').date()
    event_2_date = parser.parse('2020-12-10').date()

    test_contract = {
            "os_payable": 0.1,
            "no_os_payable": 0.2,
            "pfs_payable": 0.3,
            "no_pfs_payable": 0.4,
            "treatment_start": treatment_start
    }

    events = [(0, 0, event_1_date, "progressed"), (0, 0, event_2_date, "dead")]

    assert check_contract_status(test_contract, events) == test_contract["no_os_payable"]
Esempio n. 5
0
def create_new_event():

    patient_id = request.json["patient_id"]
    event_name = request.json["event_name"]
    event_ts = request.json["event_ts"]

    event_type = db.session.query(PatientEventType) \
        .filter(PatientEventType.event_name == event_name) \
        .first()

    statement = patient_events.insert() \
        .values(patient_id=patient_id, event_id=event_type.id, event_ts=parser.parse(event_ts))
    db.session.execute(statement)
    db.session.commit()

    # Check contract status
    contract = db.session.query(Contract) \
        .filter(Contract.patient_id == patient_id) \
        .first()

    patient_events_list = db.session.query(patient_events, PatientEventType.event_name) \
        .join(Patient) \
        .join(PatientEventType) \
        .filter(Patient.id == patient_id) \
        .all()

    payable_amount = check_contract_status(contract.to_dict(),
                                           patient_events_list)

    if payable_amount == -1:
        pass
    else:
        start_to_event_months = relativedelta.relativedelta(
            parser.parse(event_ts).replace(tzinfo=None),
            contract.treatment_start).months
        contract.amount = start_to_event_months * (contract.product.baseprice *
                                                   payable_amount)
        contract.status = 'finished'
        db.session.commit()

    return Response('{"status": "ok"}', 200)
Esempio n. 6
0
def create_contract():

    try:
        treatment_start = parser.parse(request.json['treatment_start']).date()

        # Find or create insurer
        insurer_name = request.json['insurer']
        match = db.session.query(Insurer) \
            .filter(Insurer.name == insurer_name) \
            .first()

        if match:
            insurer = match
        else:
            insurer = Insurer(name=insurer_name)

        # Find or create manufacturer
        manufacturer_name = request.json['manufacturer']
        match = db.session.query(Producer) \
            .filter(Producer.name == manufacturer_name) \
            .first()

        if match:
            manufacturer = match
        else:
            manufacturer = Producer(name=manufacturer_name)

        # Find or create patient
        patient_surname = request.json['patient_surname']
        patient_name = request.json['patient_name']
        patient_birthday = parser.parse(
            request.json['patient_birthday']).date()
        patient_cancer_stage = CancerStage(
            request.json['patient_cancer_stage'])

        # Check if patient is young enough for enrollment
        patient_age = get_age(patient_birthday)
        if patient_age >= 55:
            return Response(
                f"{{'Error':'Patient does not fullfill enrollment criteria: Age {patient_age} >= 55'}}",
                status=400,
                mimetype='application/json')

        match = db.session.query(Patient) \
            .filter(Patient.name == patient_name) \
            .filter(Patient.surname == patient_surname) \
            .filter(Patient.birthday == patient_birthday) \
            .first()

        if match:
            patient = match
        else:
            patient = Patient(name=patient_name,
                              surname=patient_surname,
                              birthday=patient_birthday,
                              cancer_stage=patient_cancer_stage)

        # Find or create product configuration
        product_brand = request.json['product_brand']
        product_name = request.json['product_name']
        product_units = request.json['product_units']
        product_baseprice = request.json['product_baseprice']
        match = db.session.query(Product) \
            .filter(Product.brand == product_brand) \
            .filter(Product.product == product_name) \
            .filter(Product.units == product_units) \
            .filter(Product.baseprice == product_baseprice) \
            .first()

        if match:
            product = match
        else:
            product = Product(brand=product_brand,
                              product=product_name,
                              units=product_units,
                              baseprice=product_baseprice)

        # Find or create payable amounts configuration
        os = request.json['os']
        no_os = request.json['no_os']
        pfs = request.json['pfs']
        no_pfs = request.json['no_pfs']
        match = db.session.query(PayableAmount) \
            .filter(PayableAmount.os_after_12_months == os) \
            .filter(PayableAmount.no_os_after_12_months == no_os) \
            .filter(PayableAmount.pfs_after_9_months == pfs) \
            .filter(PayableAmount.no_pfs_after_9_months == no_pfs) \
            .first()

        if match:
            payable_amounts = match
        else:
            payable_amounts = PayableAmount(os_after_12_months=os,
                                            no_os_after_12_months=no_os,
                                            pfs_after_9_months=pfs,
                                            no_pfs_after_9_months=no_pfs)

        new_contract = Contract(insurer=insurer,
                                producer=manufacturer,
                                product=product,
                                patient=patient,
                                status='ongoing',
                                treatment_start=treatment_start,
                                payable_amounts=payable_amounts)

        # Check if contract is already finished -> simulation purposes
        payable = check_contract_status(new_contract.to_dict(), [])

        if not payable == -1:
            # No events could have been generated at contract creation -> bill for 9 months PFS
            new_contract.amount = 9 * (product_baseprice * payable)
            new_contract.status = 'finished'

        db.session.add(new_contract)
        db.session.commit()

        return Response('{"status": "ok"}', status=200)

    except:

        return Response('{"status": "error"}', status=500)