qube_rails 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +105 -0
  3. data/README.md +213 -56
  4. data/app/assets/javascripts/qube_rails/question.js +80 -41
  5. data/app/assets/stylesheets/qube_rails.css +143 -0
  6. data/app/controllers/qube_rails/connections_controller.rb +19 -3
  7. data/app/controllers/qube_rails/storyboard_controller.rb +1 -1
  8. data/app/models/qube_rails/connection.rb +110 -8
  9. data/app/models/qube_rails/quickbooks_record.rb +22 -0
  10. data/app/views/qube_rails/_manage_connection.html.erb +19 -0
  11. data/app/views/qube_rails/question/_choice.html.erb +2 -4
  12. data/app/views/qube_rails/question/_disclosure.html.erb +7 -0
  13. data/app/views/qube_rails/question/_footer.html.erb +2 -8
  14. data/app/views/qube_rails/question/_inline_search.html.erb +12 -0
  15. data/app/views/qube_rails/question/_pick.html.erb +25 -7
  16. data/app/views/qube_rails/question/_picks.html.erb +2 -3
  17. data/app/views/qube_rails/question/_question.html.erb +22 -13
  18. data/app/views/qube_rails/question/fields/_search.html.erb +3 -2
  19. data/app/views/qube_rails/storyboard/show.html.erb +4 -3
  20. data/config/locales/en.yml +7 -0
  21. data/config/routes.rb +2 -0
  22. data/db/migrate/20261124000000_add_qube_company_file_change.rb +17 -0
  23. data/lib/generators/qube_rails/templates/connection_events.rb.tt +17 -1
  24. data/lib/qube_rails/configuration.rb +2 -0
  25. data/lib/qube_rails/connection_events.rb +67 -7
  26. data/lib/qube_rails/engine.rb +1 -0
  27. data/lib/qube_rails/link_check.rb +274 -0
  28. data/lib/qube_rails/operation.rb +1 -0
  29. data/lib/qube_rails/question_view.rb +170 -118
  30. data/lib/qube_rails/request.rb +2 -0
  31. data/lib/qube_rails/storyboard/ask_about_add.json +1 -1
  32. data/lib/qube_rails/storyboard/choose.json +2 -2
  33. data/lib/qube_rails/storyboard/enum_select.json +1 -1
  34. data/lib/qube_rails/storyboard/missing_record.json +1 -1
  35. data/lib/qube_rails/storyboard/name_clash.json +41 -3
  36. data/lib/qube_rails/storyboard/near_match.json +8 -3
  37. data/lib/qube_rails/storyboard/number_boolean.json +1 -1
  38. data/lib/qube_rails/storyboard/record_search.json +2 -2
  39. data/lib/qube_rails/storyboard/refusal.json +7 -2
  40. data/lib/qube_rails/storyboard/rows.json +35 -18
  41. data/lib/qube_rails/storyboard.rb +21 -5
  42. data/lib/qube_rails/sync_status.rb +12 -1
  43. data/lib/qube_rails/version.rb +1 -1
  44. data/lib/qube_rails.rb +9 -0
  45. metadata +6 -1
@@ -1,10 +1,7 @@
1
1
  <%# The exits (question.exits) in one row: cancel on the left, the rest (decline and the like)
2
2
  on the right. In a plain question (no picks, no forms) every choice, the first primary. With
3
- descriptions they stack, cancel last. In a chain it ends in "Not this one" / "None of these"
4
- (data-qube-branch="search", shown by the script, which hides the decline: that's on the search
5
- branch); when the search's row holds the exits, this row is the offered branch's. %>
6
- <% part = question.branch_part(:footer) %>
7
- <div class="<%= qube_classes(:choice_footer, "qube-choice-footer") %>"<%= " data-qube-plain" if question.plain? %><% if part %> data-qube-branch-part="<%= part %>"<% end %><%= " hidden" unless question.shown_part?(part) %>>
3
+ descriptions they stack, cancel last. %>
4
+ <div class="<%= qube_classes(:choice_footer, "qube-choice-footer") %>"<%= " data-qube-plain" if question.plain? %>>
8
5
  <div class="qube-choice-footer-start">
9
6
  <% question.exits.select(&:cancel?).each do |choice| %>
10
7
  <%= render "qube_rails/question/choice", question: question, choice: choice %>
@@ -13,9 +10,6 @@
13
10
  <div class="qube-choice-footer-end">
14
11
  <% question.exits.reject(&:cancel?).each do |choice| %>
15
12
  <%= render "qube_rails/question/choice", question: question, choice: choice %>
16
- <% end %>
17
- <% if question.chain? %>
18
- <button type="button" class="<%= qube_classes(:button_secondary, "qube-choice-button") %>" data-qube-button="secondary" data-qube-branch="search" hidden><%= question.ui_text(:none_of_these, count: question.offered_count) %></button>
19
13
  <% end %>
20
14
  </div>
21
15
  </div>
@@ -0,0 +1,12 @@
1
+ <%# A search beside offered records (question.searches): its box alone under them, "Search
2
+ QuickBooks…", its label for screen readers. It has no submit: the script puts the match
3
+ picked in it as a row with the offered records, whose "Use this one" answers. Hidden until the
4
+ script runs: without it the box can't search. %>
5
+ <form class="<%= qube_classes(:choice, "qube-choice", "qube-search-inline") %>" method="post" action="<%= choice.action_url %>"
6
+ id="<%= choice.form_id %>" data-qube-choice="<%= choice.id %>" data-qube-choice-kind="form" data-qube-choice-role="<%= choice.role %>" data-qube-needs-script hidden>
7
+ <%= hidden_field_tag request_forgery_protection_token, form_authenticity_token, id: nil %>
8
+ <%= hidden_field_tag :choice, choice.id, id: nil %>
9
+ <% choice.fields.each do |field| %>
10
+ <%= render field.partial, field: field %>
11
+ <% end %>
12
+ </form>
@@ -1,14 +1,31 @@
1
1
  <%# One pick (a QubeRails::ChoiceView of kind :pick): an option row, a form posting choice=<id>
2
2
  to choice.action_url with your CSRF token. An offered record (choice.style set) ends in a
3
- "Use this one" button: the first primary, the rest secondary. %>
3
+ "Use this one" button: the first primary, the rest secondary. A choice about one record shows
4
+ its title, lines and facts, and its action as the button. An offered record's button is its
5
+ only target, described by the record's name; a row without one is a button whole. A search box's picked match
6
+ (choice.slot?) is hidden and empty until the script fills it. %>
4
7
  <form class="qube-choice qube-pick-form" method="post" action="<%= choice.action_url %>" role="listitem"
5
- data-qube-choice="<%= choice.id %>" data-qube-choice-kind="pick"<% if choice.role %> data-qube-choice-role="<%= choice.role %>"<% end %>>
8
+ data-qube-choice="<%= choice.id %>" data-qube-choice-kind="pick"<% if choice.role %> data-qube-choice-role="<%= choice.role %>"<% end %><%= " hidden" if choice.slot? %>>
6
9
  <%= hidden_field_tag request_forgery_protection_token, form_authenticity_token, id: nil %>
7
10
  <%= hidden_field_tag :choice, choice.id, id: nil %>
11
+ <% choice.data.each do |name, value| %>
12
+ <%= hidden_field_tag name, value, id: nil %>
13
+ <% end %>
14
+ <% if choice.style %>
15
+ <div class="<%= qube_classes(:pick, "qube-pick") %>">
16
+ <% else %>
8
17
  <button type="submit" class="<%= qube_classes(:pick, "qube-pick") %>">
18
+ <% end %>
9
19
  <span class="qube-pick-text">
10
- <span class="qube-pick-label"><%= choice.label %></span>
11
- <% if choice.detail %>
20
+ <span class="qube-pick-label"<% if choice.style %> id="<%= choice.label_id %>"<% end %>><%= choice.title || choice.label %></span>
21
+ <% if choice.lines.any? || choice.facts.any? %>
22
+ <% choice.lines.each do |line| %>
23
+ <span class="<%= qube_classes(:pick_detail, "qube-pick-detail", "qube-pick-line") %>"><%= safe_join(choice.detail_parts(line).map { |part| tag.span(part, class: "qube-detail-part") }, " ") %></span>
24
+ <% end %>
25
+ <% if choice.facts.any? %>
26
+ <span class="qube-pick-facts"><% choice.facts.each do |fact| %><span class="qube-pick-fact"><span class="qube-pick-fact-label"><%= fact.label %></span> <%= fact.value %></span><% end %></span>
27
+ <% end %>
28
+ <% elsif choice.detail || choice.slot? %>
12
29
  <span class="<%= qube_classes(:pick_detail, "qube-pick-detail") %>"><%= safe_join(choice.detail_parts.map { |part| tag.span(part, class: "qube-detail-part") }, " ") %></span>
13
30
  <% end %>
14
31
  <% if choice.description %>
@@ -16,9 +33,10 @@
16
33
  <% end %>
17
34
  </span>
18
35
  <% if choice.style %>
19
- <span class="<%= qube_classes(choice.button_piece, "qube-pick-action", "qube-choice-button") %>" data-qube-button="<%= choice.style %>" data-qube-offered="<%= choice.style %>"><%= QubeRails.t("question.use_pick") %></span>
36
+ <button type="submit" class="<%= qube_classes(choice.button_piece, "qube-pick-action", "qube-choice-button") %>" data-qube-button="<%= choice.style %>" data-qube-offered="<%= choice.style %>" aria-describedby="<%= choice.label_id %>"><%= choice.action || QubeRails.t("question.use_pick") %></button>
37
+ </div>
20
38
  <% else %>
21
- <span class="<%= qube_classes(:pick_action, "qube-pick-action") %>"><%= QubeRails.t("question.use_pick") %></span>
22
- <% end %>
39
+ <span class="<%= qube_classes(:pick_action, "qube-pick-action") %>"><%= choice.action || QubeRails.t("question.use_pick") %></span>
23
40
  </button>
41
+ <% end %>
24
42
  </form>
@@ -1,7 +1,6 @@
1
1
  <%# The question's picks (question.picks): option rows, one form each. The question's message says
2
- what they are, so they have no heading. In a chain they're its offered branch. %>
3
- <% part = question.branch_part(:picks) %>
4
- <div class="<%= qube_classes(:picks, "qube-picks") %>" role="list"<% if part %> data-qube-branch-part="<%= part %>"<% end %><%= " hidden" unless question.shown_part?(part) %>>
2
+ what they are, so they have no heading. %>
3
+ <div class="<%= qube_classes(:picks, "qube-picks") %>" role="list">
5
4
  <% question.picks.each do |choice| %>
6
5
  <%= render "qube_rails/question/pick", question: question, choice: choice %>
7
6
  <% end %>
@@ -1,9 +1,9 @@
1
1
  <%# A question (a QubeRails::QuestionView): a refusal, its message, what it's about, then its
2
- choices by kind: the picks (option rows), each form choice ("or" between), and the exits in a
3
- footer. With one form and no picks, the exits share that form's button row instead (question.
4
- joined_form). A question with neither picks nor forms shows its choices in one row.
5
- Offered records with a search (question.chain?) show a branch at a time: each part carries
6
- data-qube-branch-part, and the one not on show is hidden; the script moves between them.
2
+ choices by kind: the picks (option rows), a search box under offered records (question.
3
+ searches), a decline beside them (question.alongside), each form choice ("or" between), the
4
+ forms behind "or <its label>…" (question.disclosed), and the exits in a footer. With one form
5
+ and no picks, the exits share that form's button row instead (question.joined_form). A
6
+ question with neither picks nor forms shows its choices in one row.
7
7
  data-qube-button-*-class are the script's, for swapping a button's weight. %>
8
8
  <section class="<%= qube_classes(:question, "qube-question") %>" data-qube-question="<%= question.token %>"
9
9
  data-qube-button-class="<%= qube_classes(:button) %>" data-qube-button-secondary-class="<%= qube_classes(:button_secondary) %>">
@@ -13,26 +13,35 @@
13
13
  <%= render "qube_rails/question/details", question: question %>
14
14
  <% end %>
15
15
  <div class="qube-choices">
16
- <% if question.chain? %>
17
- <button type="button" class="<%= qube_classes(:button_ghost, "qube-choice-button", "qube-branch-back") %>" data-qube-button="ghost" data-qube-branch="offered" data-qube-branch-part="search" hidden><%= question.ui_text(:back_to_offered, count: question.offered_count) %></button>
18
- <% end %>
19
16
  <% if question.picks.any? %>
20
17
  <%= render "qube_rails/question/picks", question: question %>
21
18
  <% end %>
19
+ <% question.searches.each do |choice| %>
20
+ <%= render "qube_rails/question/inline_search", question: question, choice: choice %>
21
+ <% end %>
22
+ <% if question.alongside.any? %>
23
+ <div class="qube-choice-alongside">
24
+ <% question.alongside.each do |choice| %>
25
+ <%= render "qube_rails/question/choice", question: question, choice: choice %>
26
+ <% end %>
27
+ </div>
28
+ <% end %>
22
29
  <% joined = question.joined_form %>
23
30
  <% question.forms.each_with_index do |choice, n| %>
24
31
  <% if question.divided?(choice, n) %>
25
- <% part = question.branch_part(choice) %>
26
- <div class="<%= qube_classes(:choice_divider, "qube-choice-divider") %>" role="separator"<% if part %> data-qube-branch-part="<%= part %>"<% end %><%= " hidden" unless question.shown_part?(part) %>><span><%= QubeRails.t("question.or") %></span></div>
32
+ <div class="<%= qube_classes(:choice_divider, "qube-choice-divider") %>" role="separator"><span><%= QubeRails.t("question.or") %></span></div>
27
33
  <% end %>
28
34
  <%= render "qube_rails/question/choice", question: question, choice: choice,
29
- exits: (choice.equal?(joined) ? question.joined_exits : []) %>
35
+ exits: (choice.equal?(joined) ? question.exits : []) %>
36
+ <% end %>
37
+ <% question.disclosed.each do |choice| %>
38
+ <%= render "qube_rails/question/disclosure", question: question, choice: choice %>
30
39
  <% end %>
31
- <% if joined && !question.chain? %>
40
+ <% if joined %>
32
41
  <% question.exits.each do |choice| %>
33
42
  <%= render "qube_rails/question/choice", question: question, choice: choice, detached: true %>
34
43
  <% end %>
35
- <% elsif question.exits.any? || question.chain? %>
44
+ <% elsif question.exits.any? %>
36
45
  <%= render "qube_rails/question/footer", question: question %>
37
46
  <% end %>
38
47
  </div>
@@ -1,10 +1,11 @@
1
1
  <%# A record field: what the user types is looked up at field.search_url (GET ?field=&q=&row=,
2
2
  answering [{value, label, detail}]), and the match they pick fills the hidden input posted as
3
3
  field.input_name. The behaviour script keys on the data-qube-* attributes; the box's
4
- data-qube-text-* attributes are its words (I18n's qube_rails.question.*). %>
4
+ data-qube-text-* attributes are its words (I18n's qube_rails.question.*). Under offered records
5
+ (field.inline?) it says "Search QuickBooks…" and isn't required. %>
5
6
  <% inputs = capture do %>
6
7
  <%= tag.input(type: "search", id: field.dom_id, class: qube_classes(:input, "qube-search-input"), maxlength: 100,
7
- autocomplete: "off", required: field.required, disabled: field.disabled,
8
+ placeholder: field.placeholder, autocomplete: "off", required: field.required, disabled: field.disabled,
8
9
  role: "combobox", "aria-autocomplete": "list", "aria-expanded": "false",
9
10
  "aria-label": (field.labelled? ? nil : field.label), data: { "qube-search-input" => "" },
10
11
  **field.aria_invalid) %>
@@ -23,9 +23,6 @@
23
23
  <% end %>
24
24
  </fieldset>
25
25
  </header>
26
- <% if flash[:qube_rails_storyboard] %>
27
- <p class="qube-storyboard-notice" role="status"><%= flash[:qube_rails_storyboard] %></p>
28
- <% end %>
29
26
  <div class="qube-storyboard-body">
30
27
  <nav class="qube-storyboard-nav" aria-label="Screens">
31
28
  <% QubeRails::Storyboard.screens.group_by(&:group).each do |group, screens| %>
@@ -68,6 +65,10 @@
68
65
  question: QubeRails::Storyboard.question_view(screen, answer, qube_rails.storyboard_search_path(screen.key)) %>
69
66
  <% end %>
70
67
  </div>
68
+ <% if single && flash[:qube_rails_storyboard] %>
69
+ <%# Under the question, where the answer was given, so it's seen. %>
70
+ <p class="qube-storyboard-notice" role="status"><%= flash[:qube_rails_storyboard] %></p>
71
+ <% end %>
71
72
  <% if screen.partials.any? %>
72
73
  <details class="qube-storyboard-files">
73
74
  <summary>Override files (<%= screen.partials.size %>)</summary>
@@ -27,6 +27,9 @@ en:
27
27
  not_checking_in: "QuickBooks hasn't checked in for a while, so this is waiting. Make sure the computer with QuickBooks is on and the Web Connector is running."
28
28
  quickbooks_unavailable: "The Web Connector is checking in but can't open QuickBooks. Open the company file in QuickBooks on that computer."
29
29
  other: "Syncing with QuickBooks has stopped for now. Check the computer with QuickBooks and its Web Connector."
30
+ connection:
31
+ set_up_again: "Set up QuickBooks again"
32
+ use_different_company_file: "Use a different company file"
30
33
  errors:
31
34
  not_starting: "This was already sent, so there's nothing to try again."
32
35
  not_quiet: "This was updated recently. Check again in a few minutes."
@@ -42,6 +45,8 @@ en:
42
45
  question:
43
46
  set_all: "Set all to:"
44
47
  use_pick: "Use this one"
48
+ # none_of_these and back_to_offered are unused since offered records and their search show
49
+ # at once; they go when qube_sync's Phoenix package drops its copies.
45
50
  none_of_these:
46
51
  one: "Not this one"
47
52
  other: "None of these"
@@ -66,3 +71,5 @@ en:
66
71
  no_matches: "No matches"
67
72
  searching: "Searching…"
68
73
  search_failed: "Search isn't available right now. Try again in a moment."
74
+ disclose_choice: "or %{label}…"
75
+ search_quickbooks: "Search QuickBooks…"
data/config/routes.rb CHANGED
@@ -6,6 +6,8 @@ QubeRails::Engine.routes.draw do
6
6
  # come back to your app: it tells the connection by webhook.
7
7
  post "quickbooks/connections", to: "connections#create", as: :connections
8
8
  post "quickbooks/connections/:id/onboarding", to: "connections#onboarding", as: :connection_onboarding
9
+ # Its "Use a different company file": QuBe Sync's page for switching it to another file.
10
+ post "quickbooks/connections/:id/company_file", to: "connections#company_file", as: :connection_company_file
9
11
  delete "quickbooks/connections/:id", to: "connections#destroy", as: :connection
10
12
 
11
13
  # Each run's webhook URL is QUBE_WEBHOOK_URL/runs/<run id> (QubeRails::Run#webhook_url), and a
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # For a connection switched to another company file (connection.company_file_changed): when it
4
+ # was, so runs from before it stop counting, and the revision of the change applied last; which
5
+ # QuickBooks links are the old file's until they're checked against the new one (stale_since),
6
+ # which the new file doesn't have (missing_since: kept, unused, checked again on the next change),
7
+ # and which it has under another name (review_since, review_name: unused until the app decides).
8
+ class AddQubeCompanyFileChange < ActiveRecord::Migration[5.2]
9
+ def change
10
+ add_column :qube_connections, :company_file_changed_at, :datetime
11
+ add_column :qube_connections, :company_file_revision, :integer
12
+ add_column :qube_quickbooks_records, :stale_since, :datetime
13
+ add_column :qube_quickbooks_records, :missing_since, :datetime
14
+ add_column :qube_quickbooks_records, :review_since, :datetime
15
+ add_column :qube_quickbooks_records, :review_name, :string
16
+ end
17
+ end
@@ -1,7 +1,8 @@
1
1
  # What your app does when one of its customers' QuickBooks Desktop connections changes. The
2
2
  # qube_rails gem does the rest: it checks each event is this connection's and newer than the
3
3
  # last one applied, under the connection row's lock, and updates the row's status
4
- # ("connected", "setup_cancelled", "stalled") and stall_reason before your handler runs.
4
+ # ("connected", "setup_cancelled", "stalled", or "pending" while it's set up again) and
5
+ # stall_reason before your handler runs.
5
6
  #
6
7
  # connection is the QubeRails::Connection (one company file; an account can have several) and
7
8
  # owner the account that connected it. Start a run on it with
@@ -34,5 +35,20 @@ module QuickbooksDesktop
34
35
  def resumed(event)
35
36
  log_event(event)
36
37
  end
38
+
39
+ # They chose "Use a different company file": the gem keeps this connection's QuickBooks links
40
+ # stale (unused) and checks them once it's connected. Set aside ids you keep yourself, and ask
41
+ # them to finish the setup.
42
+ def company_file_changed(event)
43
+ log_event(event)
44
+ end
45
+
46
+ # A batch of stale links of kind was looked up on the new file: relinked found with the same name
47
+ # (kept); review found under another name (ask the customer, then connection.confirm_link or
48
+ # reject_link each); missing not found (review a missing transaction before sending it again).
49
+ # stale_left is 0 once every check is done.
50
+ def links_checked(kind:, found:, relinked:, review:, missing:, stale_left:)
51
+ super
52
+ end
37
53
  end
38
54
  end
@@ -114,6 +114,8 @@ module QubeRails
114
114
 
115
115
  # The operation class for a slug, or nil when none of your operations has it.
116
116
  def operation_class(slug)
117
+ return LinkCheck.for_slug(slug) if slug.to_s.start_with?(LinkCheck::SLUG_PREFIX)
118
+
117
119
  name = operations[slug.to_s]
118
120
  name && constantize(name)
119
121
  end
@@ -10,6 +10,7 @@ module QubeRails
10
10
  # connection.setup_cancelled they chose "Cancel setup" on the setup page
11
11
  # connection.stalled requests are stuck; see stall_reason
12
12
  # connection.resumed after a stall, they're being taken again
13
+ # connection.company_file_changed they chose "Use a different company file": set up again
13
14
  #
14
15
  # The setup ends on the QuickBooks computer, which usually isn't signed in to your app, so
15
16
  # nothing comes back to it there: connection.connected, signed by QuBe Sync, is how your app
@@ -20,9 +21,13 @@ module QubeRails
20
21
  "connection.connected" => "connected",
21
22
  "connection.setup_cancelled" => "setup_cancelled",
22
23
  "connection.stalled" => "stalled",
23
- "connection.resumed" => "connected"
24
+ "connection.resumed" => "connected",
25
+ # Set up again, like a new connection, until connection.connected comes.
26
+ "connection.company_file_changed" => "pending"
24
27
  }.freeze
25
28
 
29
+ COMPANY_FILE_CHANGED = "connection.company_file_changed"
30
+
26
31
  attr_reader :connection
27
32
 
28
33
  def initialize(connection)
@@ -40,7 +45,9 @@ module QubeRails
40
45
  # - an event whose connection isn't this row's qube_id (a signed body posted to another
41
46
  # connection's URL) is logged and skipped;
42
47
  # - any other is applied only when its revision is newer than the last one applied, so a
43
- # retried connection.stalled that comes after its connection.resumed is skipped.
48
+ # retried connection.stalled that comes after its connection.resumed is skipped;
49
+ # - but a connection.company_file_changed newer than the last one applied (company_file_revision)
50
+ # still forgets the old file when it comes late, leaving status and revision as they are.
44
51
  # It holds the connection row's lock while it does, so two workers never apply its events at
45
52
  # once, and what your handler writes is saved with the revision or not at all.
46
53
  #
@@ -56,7 +63,9 @@ module QubeRails
56
63
  end
57
64
 
58
65
  applied = connection.with_lock { apply(event) }
59
- refresh_pages if applied && (event.stalled? || event.resumed?)
66
+ refresh_pages if applied && (event.stalled? || event.resumed? || event.event == COMPANY_FILE_CHANGED)
67
+ # Connected with links from an earlier company file: check them (again, after a retry).
68
+ LinkCheck.start(connection) if connection.connected? && connection.stale_links.exists?
60
69
  end
61
70
 
62
71
  # What your app does with each: until you fill them in, each logs.
@@ -88,6 +97,22 @@ module QubeRails
88
97
  log_event(event)
89
98
  end
90
99
 
100
+ # Its QuickBooks links are stale (unused) until they're checked on connected. Set aside ids you
101
+ # keep yourself, and ask the customer to finish the setup.
102
+ def company_file_changed(event)
103
+ log_event(event)
104
+ end
105
+
106
+ # A batch of its stale links of kind was looked up on the new file. relinked: found with the same
107
+ # name (kept); review: found under another name ({link:, record:, old_name:, new_name:}), unused
108
+ # until you ask the customer and call confirm_link or reject_link; missing: not found, kept
109
+ # unused (review a missing transaction before it's sent again). stale_left: 0 once all are done.
110
+ def links_checked(kind:, found:, relinked:, review:, missing:, stale_left:)
111
+ QubeRails.logger.info("QuBe Sync: connection #{connection.id}: #{kind} links checked on its company file: " \
112
+ "#{found} found (#{review.size} to review), #{missing.size} missing, " \
113
+ "#{stale_left} still to check")
114
+ end
115
+
91
116
  # The account disconnected it (DELETE quickbooks/connections/:id): delete your rows that
92
117
  # point at it. Runs in the transaction that deletes it, before its runs and links go.
93
118
  def disconnected; end
@@ -104,8 +129,10 @@ module QubeRails
104
129
  return false
105
130
  end
106
131
 
107
- # Applied already, or older than what was.
108
- return false unless event.newer_than?(connection.revision)
132
+ unless event.newer_than?(connection.revision)
133
+ # Applied already, or older than what was.
134
+ return event.event == COMPANY_FILE_CHANGED ? late_company_file_change(event) : false
135
+ end
109
136
 
110
137
  status = STATUS[event.event]
111
138
  if status
@@ -116,6 +143,7 @@ module QubeRails
116
143
  type = event.connection.is_a?(Hash) ? event.connection["type"].to_s : ""
117
144
  connection.connection_type = type if Connection::TYPES.include?(type)
118
145
  connection.revision = event.revision
146
+ forget_company_file(event) if event.event == COMPANY_FILE_CHANGED
119
147
 
120
148
  if event.connected?
121
149
  connected(event)
@@ -125,6 +153,8 @@ module QubeRails
125
153
  stalled(event)
126
154
  elsif event.resumed?
127
155
  resumed(event)
156
+ elsif event.event == COMPANY_FILE_CHANGED
157
+ company_file_changed(event)
128
158
  else
129
159
  log_event(event)
130
160
  end
@@ -132,10 +162,40 @@ module QubeRails
132
162
  true
133
163
  end
134
164
 
135
- # At most this many records' pages are told to refresh when the connection stalls or resumes.
165
+ # A company file change that came after a later event (connection.connected): the links are
166
+ # still the old file's. Status and revision stay; your company_file_changed handler runs.
167
+ def late_company_file_change(event)
168
+ return false unless company_file_change_newer?(event)
169
+
170
+ QubeRails.logger.info("QuBe Sync: connection #{connection.id}: #{event.event} (revision #{event.revision}) " \
171
+ "came late; its links are checked again")
172
+ forget_company_file(event)
173
+ company_file_changed(event)
174
+ connection.save!
175
+ true
176
+ end
177
+
178
+ def company_file_change_newer?(event)
179
+ connection.company_file_revision.nil? || event.newer_than?(connection.company_file_revision)
180
+ end
181
+
182
+ # Every id kept for the connection is the old file's until checked on the new one (LinkCheck),
183
+ # each sync's since goes, and runs from before now stop counting (SyncStatus).
184
+ def forget_company_file(event)
185
+ now = Time.now
186
+ # Missing ones too: a switch back to the file that has them gets them back.
187
+ QuickbooksRecord.where(connection_id: connection.id)
188
+ .update_all(stale_since: now, missing_since: nil, review_since: nil, review_name: nil)
189
+ SyncMark.where(connection_id: connection.id).delete_all
190
+ connection.company_file_changed_at = now
191
+ connection.company_file_revision = event.revision
192
+ end
193
+
194
+ # At most this many records' pages are told to refresh when the connection stalls, resumes or
195
+ # changes company file.
136
196
  REFRESH_LIMIT = 200
137
197
 
138
- # A stall or a resume changes what the run status says on the pages of the records with a run
198
+ # A stall, a resume or a company file change changes what the run status says on the pages of the records with a run
139
199
  # still going on this connection: tell them to refresh, after the change is committed (by the
140
200
  # connection's runs index, at most REFRESH_LIMIT records).
141
201
  def refresh_pages
@@ -7,6 +7,7 @@ module QubeRails
7
7
  # to where it's mounted:
8
8
  # POST quickbooks/connections a connection for the signed-in account
9
9
  # POST quickbooks/connections/:id/onboarding one of its connections, through setup again
10
+ # POST quickbooks/connections/:id/company_file one of its connections, to another company file
10
11
  # POST webhooks/qube/runs/:id a run's webhooks: no sign-in, no CSRF
11
12
  # POST webhooks/qube/connections/:id a connection's webhooks: no sign-in, no CSRF
12
13
  # POST qube/questions/runs/:id an answer, behind your sign-in and CSRF