Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
12.4 KB
Raw Blame
<%# locals: (duplicate_items:, account_overlap:, public_token:, region:, institution_name:, institution_id:) %>

<%# Rendered by PlaidItemsController#create in place of exchanging the public token:
    the user has finished Link, but nothing has been added. When the dialog offers
    "Confirm this connection", the token waits in that form, and is spent only if the
    user submits it. %>

<% state = account_overlap.state %>

<%# The connections to show. When accounts match, only those holding them: a second
    login's connection at the same institution has nothing to do with this link, and
    listing it would make "this connection" ambiguous and could put an update action
    on the wrong one. When nothing matches, or nothing can be compared, every
    connection at the institution stays listed as context. %>
<% listed_items = state.in?(%i[all_connected some_connected]) ? account_overlap.matching_items : duplicate_items %>
<% masked = ->(mask) { t(".mask", mask: mask) if mask.present? } %>

<%# Connections this user can take into update mode: US ones they may manage, and that
    are theirs. An admin may manage a member's connection, but update mode signs in
    with that connection's bank login, which is the member's -- so only the user's own
    connections count, along with those predating per-user ownership, which have no
    owner and which admins have always managed.

    Updating is offered only where it fits -- when some accounts are new, or when
    there is nothing to compare, often because a connection's first sync never
    landed. With exactly one such connection, its button joins the actions at the
    bottom. With several, each card keeps its own, since one button could not say
    which connection it updates. %>
<% users_own = ->(item) { item.owned_by?(Current.user) || item.owner_id.nil? } %>
<% updatable_items = listed_items.select { |item| item.us? && item.manageable_by?(Current.user) && users_own.call(item) } %>

<%# When every account matches only other members' connections, the user didn't make
    "this connection": their own login reaches the same accounts -- joint accounts,
    say -- so the warning says whose connection already has them instead. %>
<% others_connected = state == :all_connected && listed_items.none?(&users_own) %>
<% offers_update = state.in?(%i[some_connected unknown]) %>
<% bottom_update_item = updatable_items.first if offers_update && updatable_items.one? %>
<% card_update_items = offers_update && updatable_items.many? ? updatable_items : [] %>

<%# Close discards the held token without exchanging it. %>
<% close_button = ->(variant) do
     render DS::Button.new(
       text: t(".close"),
       variant: variant,
       size: :sm,
       full_width: true,
       data: { action: "DS--dialog#close" }
     )
   end %>

<%# Sends the held public token back to create, with confirm_duplicate to let it past
    the check. The token already passed through the browser on its way from Link, is
    single-use, expires after 30 minutes, and is worthless without the server's Plaid
    secret, so a hidden field exposes nothing new. _top because a confirmed create
    redirects to Accounts, which has no modal frame to fill. %>
<% confirm_button = ->(variant) do
     render DS::Button.new(
       text: t(".confirm"),
       href: plaid_items_path,
       method: :post,
       params: {
         confirm_duplicate: "1",
         plaid_item: {
           public_token: public_token,
           region: region,
           metadata: { institution: { name: institution_name, institution_id: institution_id } }
         }
       },
       variant: variant,
       size: :sm,
       full_width: true,
       frame: "_top"
     )
   end %>

<%= turbo_frame_tag "modal" do %>
  <%# disable_click_outside: whether to duplicate a connection or keep the existing one
      is not a decision a stray click should make, and this dialog opens as Link
      closes, wherever Link left the pointer. The header close button and Esc still
      dismiss it.

      turbo_temporary: the dialog carries a live public token, and Turbo would
      otherwise keep it in the page snapshot it caches on navigation -- so Back would
      restore a dialog still able to spend it. %>
  <%= render DS::Dialog.new(disable_click_outside: true, data: { turbo_temporary: true }) do |dialog| %>
    <%# A family can legitimately hold several connections for one institution -- that is
        the state this warning exists to stop growing -- so the title counts them. When
        every account matches, this is the same connection made twice, and the title
        says so -- unless the matches are all other members', in which case it is the
        accounts that are already connected, not this connection. %>
    <% title = if others_connected
                 t(".title_others_connected", count: account_overlap.link_accounts.size)
               elsif state == :all_connected
                 t(".title_all_connected")
               else
                 t(".title", count: listed_items.size)
               end %>
    <% dialog.with_header(title: title) %>

    <% dialog.with_body do %>
      <div class="space-y-4">
        <%# What the new connection would duplicate, from the accounts Link just reported,
            so the consequences are stated only where they hold. A second login with
            different accounts duplicates nothing, and a warning that is false for the
            people the override serves costs more than one that is merely unneeded. %>
        <div class="space-y-2">
          <% if others_connected %>
            <% other_owners = listed_items.filter_map(&:owner).uniq %>
            <% if other_owners.one? %>
              <p class="text-sm text-secondary"><%= t(".overlap.others_connected", count: account_overlap.link_accounts.size, name: other_owners.first.display_name) %></p>
            <% else %>
              <p class="text-sm text-secondary"><%= t(".overlap.others_connected_several", count: account_overlap.link_accounts.size) %></p>
            <% end %>
          <% elsif state == :all_connected %>
            <p class="text-sm text-secondary"><%= t(".overlap.all_connected", count: account_overlap.link_accounts.size) %></p>
            <%# Close is the only action here, so say where the other reasons to re-link
                go: Add accounts on the existing connection, where Plaid supports it, or
                New account again with the other login. Link can report an institution
                id without a name, and the matched connection carries the same
                institution's name. %>
            <% institution = institution_name.presence || listed_items.first.name %>
            <% if updatable_items.any? %>
              <p class="text-sm text-secondary"><%= t(".overlap.more_accounts", institution: institution) %></p>
            <% end %>
            <p class="text-sm text-secondary"><%= t(".overlap.different_login", institution: institution) %></p>
          <% elsif state == :some_connected %>
            <p class="text-sm text-secondary"><%= t(".overlap.some_connected.unconnected", count: account_overlap.unconnected_accounts.size) %></p>
            <dl class="space-y-1">
              <% account_overlap.unconnected_accounts.each do |account| %>
                <%= render DS::DetailRow.new(label: account["name"], value: masked.call(account["mask"]), align: :baseline) %>
              <% end %>
            </dl>
            <p class="text-sm text-secondary"><%= t(".overlap.some_connected.connected", count: account_overlap.connected_count) %></p>
          <% elsif state == :none_connected %>
            <p class="text-sm text-secondary"><%= t(".overlap.none_connected") %></p>
          <% else %>
            <p class="text-sm text-secondary"><%= t(".overlap.unknown", count: listed_items.size) %></p>
          <% end %>
        </div>

        <div class="space-y-3">
          <% listed_items.each do |plaid_item| %>
            <div class="p-3 border border-primary rounded-lg space-y-3">
              <div class="flex items-baseline justify-between gap-2">
                <span class="font-medium text-sm text-primary"><%= plaid_item.name %></span>
                <span class="text-xs text-secondary whitespace-nowrap">
                  <%= t(".linked_on", date: l(plaid_item.created_at.to_date, format: :short_with_year)) %>
                </span>
              </div>

              <%# owner_id is nullable -- connections predating per-user ownership (#3613)
                  carry none, and owned_by? is false for those, so attribute only when
                  there is someone to name. %>
              <% if plaid_item.owner.present? && !plaid_item.owned_by?(Current.user) %>
                <p class="text-xs text-secondary">
                  <%= t(".connected_by", name: plaid_item.owner.display_name) %>
                </p>
              <% end %>

              <%# plaid_accounts, not accounts: PlaidItem#accounts resolves through the
                  linked Account and returns nothing for a connection whose accounts were
                  never linked -- which is exactly the broken connection a user re-links. %>
              <% if plaid_item.plaid_accounts.any? %>
                <dl class="space-y-1">
                  <% plaid_item.plaid_accounts.each do |plaid_account| %>
                    <%= render DS::DetailRow.new(label: plaid_account.name, value: masked.call(plaid_account.mask), align: :baseline) %>
                  <% end %>
                </dl>
              <% else %>
                <p class="text-xs text-secondary"><%= t(".no_accounts") %></p>
              <% end %>

              <% if card_update_items.include?(plaid_item) %>
                <%= render "plaid_items/update_connection_link", plaid_item: plaid_item %>
              <% elsif plaid_item.manageable_by?(Current.user) && !plaid_item.us? %>
                <%# Plaid does not support update-mode account selection for UK/EU
                    institutions and directs callers to remove the Item and create a
                    new one instead, so there is no update path to offer here. Say why,
                    rather than silently dropping the action. %>
                <p class="text-xs text-secondary"><%= t(".eu_no_add_accounts") %></p>
                <%= render DS::Link.new(
                      text: t(".manage_connection"),
                      href: accounts_path,
                      variant: :outline,
                      size: :sm,
                      full_width: true,
                      data: { turbo_frame: "_top" }
                    ) %>
              <% end %>
            </div>
          <% end %>
        </div>

        <%# Each case leads with what its evidence suggests:
            - Every account already connected: Close alone. Adding could only duplicate
              them. When the connection is the user's own, the text above says where
              else to go.
            - Some new: update the existing connection, which adds them without a second
              connection. Confirming stays last, for a different login that shares an
              account, such as a joint account. When the overlap is only with other
              members' connections, it is that different login, so confirming leads.
            - None connected: confirm, since this is likely a different login.
            - Nothing to compare: Close, since neither updating nor confirming is
              clearly right. %>
        <div class="space-y-2">
          <% if state == :all_connected %>
            <%= close_button.call(:primary) %>
          <% elsif state == :some_connected && bottom_update_item.nil? && listed_items.none?(&users_own) %>
            <%= confirm_button.call(:primary) %>
            <%= close_button.call(:outline) %>
          <% elsif state == :some_connected %>
            <% if bottom_update_item %>
              <%= render "plaid_items/update_connection_link", plaid_item: bottom_update_item, variant: :primary %>
            <% end %>
            <%= close_button.call(bottom_update_item ? :outline : :primary) %>
            <%= confirm_button.call(:ghost) %>
          <% elsif state == :none_connected %>
            <%= confirm_button.call(:primary) %>
            <%= close_button.call(:outline) %>
          <% else %>
            <%= close_button.call(:primary) %>
            <% if bottom_update_item %>
              <%= render "plaid_items/update_connection_link", plaid_item: bottom_update_item %>
            <% end %>
            <%= confirm_button.call(:outline) %>
          <% end %>
        </div>
      </div>
    <% end %>
  <% end %>
<% end %>