mirror of
https://github.com/we-promise/sure.git
synced 2026-05-24 13:04:56 +00:00
* feat(sync): add Brex provider schema Adds Brex item and account tables with per-family credentials, scoped upstream account uniqueness, encrypted token storage, and sanitized provider payload columns. * feat(sync): add Brex provider core Adds Brex item/account models, provider client and adapter support, family connection helpers, and provider enum registration for read-only Brex cash and card data. * feat(sync): add Brex import pipeline Adds Brex account discovery, linked-account sync, cash/card balance processors, transaction import, sanitized metadata handling, and idempotent provider entry processing. * feat(sync): add Brex connection flows Adds Mercury-style Brex connection management, explicit item-scoped account selection and linking, settings provider UI, account index visibility, localized copy, and per-item cache handling. * test(sync): cover Brex provider workflows Adds targeted coverage for Brex provider requests, adapter config, item/account guards, importer behavior, entry processing, and Mercury-style controller flows. * fix(sync): align Brex API edge cases Tightens Brex account fetching against the official card-account response shape, sends transaction start filters as RFC3339 date-times, and keeps provider error bodies out of user-facing messages while expanding provider client guard coverage. * fix(sync): harden Brex provider integration Restrict Brex API base URLs to official hosts, tighten account-selection UI behavior, and add tests for invalid credentials, cache scoping, and provider setup edge cases. * test(sync): avoid Brex secret-shaped fixtures * refactor(sync): extract Brex account flows * fix(sync): address Brex provider review feedback * fix(sync): address Brex review follow-ups Move remaining Brex review cleanup into focused model behavior, tighten link/setup edge cases, localize summaries, and add regression coverage from CodeRabbit feedback. Also records the security-review pass as no-findings after diff-scoped inspection and Brakeman validation. * refactor(sync): split Brex account flow controllers Route Brex account selection and setup actions through small namespaced controllers while keeping existing URLs and helpers stable. Business flow remains in BrexItem::AccountFlow; the main Brex item controller now only handles connection CRUD, provider-panel rendering, destroy, and sync. * fix(sync): address Brex CodeRabbit review * fix(sync): address Brex follow-up review * fix(sync): address Brex review follow-ups * fix(sync): address Brex sync review findings * fix(sync): polish Brex review copy and errors * fix(sync): register Brex provider health * fix(sync): polish Brex bank sync presentation * fix(sync): address Brex review follow-ups * fix(sync): tighten Brex setup params * test(api): stabilize usage rate-limit window * fix(sync): polish Brex setup flow nits * fix(sync): harden Brex setup params * fix(sync): finalize Brex review cleanup --------- Signed-off-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
133 lines
4.6 KiB
Ruby
133 lines
4.6 KiB
Ruby
class BrexItems::AccountFlowsController < ApplicationController
|
|
before_action :require_admin!
|
|
|
|
def preload_accounts
|
|
render json: brex_account_flow.preload_payload
|
|
end
|
|
|
|
def select_accounts
|
|
@accountable_type = params[:accountable_type] || "Depository"
|
|
@return_to = safe_return_to_path
|
|
result = brex_account_flow.select_accounts_result(accountable_type: @accountable_type)
|
|
|
|
return handle_brex_selection_result(result, empty_path: new_account_path, api_return_path: @return_to) unless result.success?
|
|
|
|
@brex_item = result.brex_item
|
|
@available_accounts = result.available_accounts
|
|
|
|
render "brex_items/select_accounts", layout: false
|
|
end
|
|
|
|
def link_accounts
|
|
result = brex_account_flow.link_new_accounts_result(
|
|
account_ids: params[:account_ids] || [],
|
|
accountable_type: params[:accountable_type] || "Depository"
|
|
)
|
|
|
|
redirect_with_navigation(result, return_to: safe_return_to_path)
|
|
end
|
|
|
|
def select_existing_account
|
|
return redirect_to accounts_path, alert: t("brex_items.select_existing_account.no_account_specified") if params[:account_id].blank?
|
|
|
|
@account = Current.family.accounts.find_by(id: params[:account_id])
|
|
return redirect_to accounts_path, alert: t("brex_items.select_existing_account.no_account_specified") unless @account
|
|
|
|
result = brex_account_flow.select_existing_account_result(account: @account)
|
|
|
|
return handle_brex_selection_result(result, empty_path: accounts_path, api_return_path: accounts_path) unless result.success?
|
|
|
|
@brex_item = result.brex_item
|
|
@available_accounts = result.available_accounts
|
|
@return_to = safe_return_to_path
|
|
|
|
render "brex_items/select_existing_account", layout: false
|
|
end
|
|
|
|
def link_existing_account
|
|
return redirect_to accounts_path, alert: t("brex_items.link_existing_account.no_account_specified") if params[:account_id].blank?
|
|
|
|
account = Current.family.accounts.find_by(id: params[:account_id])
|
|
return redirect_to accounts_path, alert: t("brex_items.link_existing_account.no_account_specified") unless account
|
|
|
|
result = brex_account_flow.link_existing_account_result(
|
|
account: account,
|
|
brex_account_id: params[:brex_account_id]
|
|
)
|
|
|
|
redirect_with_navigation(result, return_to: safe_return_to_path)
|
|
end
|
|
|
|
private
|
|
|
|
def brex_account_flow
|
|
@brex_account_flow ||= BrexItem::AccountFlow.new(family: Current.family, brex_item_id: params[:brex_item_id])
|
|
end
|
|
|
|
def handle_brex_selection_result(result, empty_path:, api_return_path:)
|
|
case result.status
|
|
when :empty, :account_already_linked
|
|
redirect_to empty_path, alert: result.message
|
|
when :no_api_token, :select_connection
|
|
redirect_to settings_providers_path, alert: result.message
|
|
when :setup_required
|
|
if turbo_frame_request?
|
|
render partial: "brex_items/setup_required", layout: false
|
|
else
|
|
redirect_to settings_providers_path, alert: result.message
|
|
end
|
|
when :api_error, :unexpected_error
|
|
render_api_error_partial(result.message, api_return_path)
|
|
else
|
|
redirect_to settings_providers_path, alert: result.message
|
|
end
|
|
end
|
|
|
|
def redirect_with_navigation(result, return_to:)
|
|
redirect_to navigation_path_for(result.target, return_to: return_to), result.flash_type => result.message
|
|
end
|
|
|
|
def navigation_path_for(target, return_to:)
|
|
{
|
|
new_account: new_account_path,
|
|
settings_providers: settings_providers_path,
|
|
return_to_or_accounts: return_to || accounts_path
|
|
}.fetch(target, accounts_path)
|
|
end
|
|
|
|
def render_api_error_partial(error_message, return_path)
|
|
render partial: "brex_items/api_error", locals: { error_message: error_message, return_path: return_path }, layout: false
|
|
end
|
|
|
|
def safe_return_to_path
|
|
return nil if params[:return_to].blank?
|
|
|
|
return_to = params[:return_to].to_s.strip
|
|
return nil unless return_to.start_with?("/")
|
|
|
|
second_character = return_to[1]
|
|
return nil if second_character.blank?
|
|
return nil if second_character == "/" || second_character == "\\"
|
|
return nil if second_character.match?(/[[:space:][:cntrl:]]/)
|
|
return nil if encoded_path_separator?(return_to)
|
|
|
|
uri = URI.parse(return_to)
|
|
|
|
return nil if uri.scheme.present? || uri.host.present?
|
|
|
|
return_to
|
|
rescue URI::InvalidURIError
|
|
nil
|
|
end
|
|
|
|
def encoded_path_separator?(return_to)
|
|
encoded_second_character = return_to[1, 3]
|
|
return false unless encoded_second_character&.start_with?("%")
|
|
|
|
decoded = URI.decode_www_form_component(encoded_second_character)
|
|
decoded == "/" || decoded == "\\"
|
|
rescue ArgumentError
|
|
false
|
|
end
|
|
end
|