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)
def _persist_default_statement_frequency(payment_account_id): statement_settings_model = StatementSettingsModel( frequency=StatementFrequency.default_frequency().value, payment_account_id=payment_account_id) statement_settings_model.save()