Example #1
0
def test_update_statement_daily_to_daily(session):
    """Assert that going from daily to daily creates a new record."""
    bcol_account = factory_premium_payment_account()
    bcol_account.save()

    payment = factory_payment()
    payment.save()
    i = factory_invoice(payment_account=bcol_account)
    i.save()
    factory_invoice_reference(i.id).save()
    factory_statement_settings(payment_account_id=bcol_account.id,
                               frequency=StatementFrequency.DAILY.value)

    # update to weekly
    payment_account = PaymentAccount.find_by_id(bcol_account.id)
    statement_settings = StatementSettingsService.update_statement_settings(payment_account.auth_account_id,
                                                                            StatementFrequency.DAILY.value)
    assert statement_settings is not None
    assert statement_settings.get('frequency') == StatementFrequency.DAILY.value
    assert statement_settings.get('to_date') is None

    # daily to daily - assert daily should start by tomorow
    assert statement_settings.get('from_date') == (datetime.today() + timedelta(days=1)).strftime('%Y-%m-%d')

    # daily to daily - assert current active one is stil daily ending today
    current_statement_settings = StatementSettingsModel.find_active_settings(payment_account.auth_account_id,
                                                                             datetime.today())
    assert current_statement_settings is not None
    assert current_statement_settings.frequency == StatementFrequency.DAILY.value
    assert current_statement_settings.to_date == datetime.today().date()
def test_update_statement_monthly(session):
    """Assert that the statement settings by id works."""
    bcol_account = factory_premium_payment_account()
    bcol_account.save()

    payment = factory_payment()
    payment.save()
    i = factory_invoice(payment_account=bcol_account)
    i.save()
    factory_invoice_reference(i.id).save()
    factory_statement_settings(payment_account_id=bcol_account.id,
                               frequency=StatementFrequency.MONTHLY.value)

    # update to weekly
    payment_account = PaymentAccount.find_by_id(bcol_account.id)
    statement_settings = StatementSettingsService.update_statement_settings(
        payment_account.auth_account_id, StatementFrequency.WEEKLY.value)
    assert statement_settings is not None
    assert statement_settings.get(
        'frequency') == StatementFrequency.WEEKLY.value
    assert statement_settings.get('to_date') is None

    # monthly to weekly - assert weekly should start by next week first day
    end_of_month_date = get_first_and_last_dates_of_month(
        datetime.today().month,
        datetime.today().year)[1]
    assert statement_settings.get('from_date') == (
        end_of_month_date + timedelta(days=1)).strftime(DT_SHORT_FORMAT)

    # monthly to weekly - assert current active one is stil monthly ending end of the week
    current_statement_settings = StatementSettingsModel.find_active_settings(
        payment_account.auth_account_id, datetime.today())
    assert current_statement_settings is not None
    assert current_statement_settings.frequency == StatementFrequency.MONTHLY.value
    assert current_statement_settings.to_date == end_of_month_date.date()

    # travel to next week and see whats active
    with freeze_time(end_of_month_date + timedelta(days=2)):
        next_week_statement_settings = StatementSettingsModel.find_active_settings(
            payment_account.auth_account_id, datetime.today())
        assert next_week_statement_settings is not None
        assert next_week_statement_settings.frequency == StatementFrequency.WEEKLY.value
        assert next_week_statement_settings.to_date is None
Example #3
0
    def update_statement_settings(auth_account_id: str, frequency: str):
        """Update statements by account id.

        rather than checking frequency changes by individual if , it just applies the following logic.
        find the maximum frequency of current one and new one ;and calculate the date which it will keep on going.

        """
        statements_settings_schema = StatementSettingsModelSchema()
        today = datetime.today()
        current_statements_settings = StatementSettingsModel.find_active_settings(
            auth_account_id, today)
        payment_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id(
            auth_account_id)
        if current_statements_settings is None:
            # no frequency yet.first time accessing the statement settings.so create a new record
            statements_settings = StatementSettingsModel(
                frequency=frequency, payment_account_id=payment_account.id)
            statements_settings.save()
            return statements_settings_schema.dump(statements_settings)

        # check if the latest one is the active one.. if not , inactivate the latest one.
        # this handles the case of quickly changing of frequencies..
        # changed from daily to monthly but then changed back to weekly..
        # the monthly didn't get applied ,but even before that its being changed to weekly
        future_statements_settings = StatementSettingsModel.find_latest_settings(
            auth_account_id)
        if future_statements_settings is not None and current_statements_settings.id != future_statements_settings.id:
            future_statements_settings.to_date = today
            future_statements_settings.save()

        max_frequency = StatementSettings._find_longest_frequency(
            current_statements_settings.frequency, frequency)
        last_date = StatementSettings._get_end_of(max_frequency)
        current_statements_settings.to_date = last_date
        current_statements_settings.save()

        new_statements_settings = StatementSettingsModel(
            frequency=frequency,
            payment_account_id=payment_account.id,
            from_date=last_date + timedelta(days=1))

        new_statements_settings.save()
        return statements_settings_schema.dump(new_statements_settings)