qube_rails 0.2.0 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +321 -0
- data/README.md +406 -76
- data/app/assets/javascripts/qube_rails/question.js +469 -43
- data/app/assets/stylesheets/qube_rails.css +1349 -912
- data/app/controllers/qube_rails/connections_controller.rb +42 -9
- data/app/controllers/qube_rails/questions_controller.rb +39 -7
- data/app/controllers/qube_rails/runs_controller.rb +4 -3
- data/app/controllers/qube_rails/storyboard_controller.rb +1 -1
- data/app/models/qube_rails/connection.rb +131 -9
- data/app/models/qube_rails/quickbooks_record.rb +22 -0
- data/app/models/qube_rails/run.rb +28 -10
- data/app/views/qube_rails/_error.html.erb +2 -2
- data/app/views/qube_rails/_manage_connection.html.erb +19 -0
- data/app/views/qube_rails/_question.html.erb +4 -1
- data/app/views/qube_rails/question/_choice.html.erb +34 -11
- data/app/views/qube_rails/question/_disclosure.html.erb +7 -0
- data/app/views/qube_rails/question/_exit_button.html.erb +8 -0
- data/app/views/qube_rails/question/_field.html.erb +4 -3
- data/app/views/qube_rails/question/_footer.html.erb +12 -4
- data/app/views/qube_rails/question/_inline_search.html.erb +12 -0
- data/app/views/qube_rails/question/_pick.html.erb +29 -6
- data/app/views/qube_rails/question/_picks.html.erb +2 -1
- data/app/views/qube_rails/question/_question.html.erb +31 -7
- data/app/views/qube_rails/question/_submit.html.erb +9 -0
- data/app/views/qube_rails/question/fields/_boolean.html.erb +2 -2
- data/app/views/qube_rails/question/fields/_number.html.erb +2 -2
- data/app/views/qube_rails/question/fields/_rows.html.erb +17 -4
- data/app/views/qube_rails/question/fields/_search.html.erb +6 -3
- data/app/views/qube_rails/question/fields/_select.html.erb +6 -5
- data/app/views/qube_rails/question/fields/_text.html.erb +2 -2
- data/app/views/qube_rails/question/rows/_segmented.html.erb +3 -1
- data/app/views/qube_rails/run_status/_detail.html.erb +5 -1
- data/app/views/qube_rails/run_status/_status.html.erb +5 -2
- data/app/views/qube_rails/storyboard/show.html.erb +4 -3
- data/config/locales/en.yml +30 -1
- data/config/routes.rb +2 -0
- data/db/migrate/20261031000000_add_qube_connection_type_and_run_headline.rb +12 -0
- data/db/migrate/20261124000000_add_qube_company_file_change.rb +17 -0
- data/lib/generators/qube_rails/templates/connection_events.rb.tt +17 -1
- data/lib/generators/qube_rails/templates/initializer.rb.tt +12 -4
- data/lib/qube_rails/configuration.rb +5 -2
- data/lib/qube_rails/connection_events.rb +70 -7
- data/lib/qube_rails/engine.rb +1 -0
- data/lib/qube_rails/link_check.rb +274 -0
- data/lib/qube_rails/operation.rb +116 -32
- data/lib/qube_rails/question_view.rb +293 -35
- data/lib/qube_rails/request.rb +2 -0
- data/lib/qube_rails/rows_view.rb +18 -5
- data/lib/qube_rails/run_status_view.rb +12 -7
- data/lib/qube_rails/storyboard/ask_about_add.json +9 -11
- data/lib/qube_rails/storyboard/choose.json +10 -7
- data/lib/qube_rails/storyboard/enum_select.json +5 -4
- data/lib/qube_rails/storyboard/missing_record.json +11 -14
- data/lib/qube_rails/storyboard/name_clash.json +51 -21
- data/lib/qube_rails/storyboard/near_match.json +18 -15
- data/lib/qube_rails/storyboard/number_boolean.json +5 -4
- data/lib/qube_rails/storyboard/record_search.json +15 -16
- data/lib/qube_rails/storyboard/refusal.json +15 -18
- data/lib/qube_rails/storyboard/rows.json +45 -31
- data/lib/qube_rails/storyboard.rb +28 -7
- data/lib/qube_rails/sync_status.rb +16 -3
- data/lib/qube_rails/version.rb +1 -1
- data/lib/qube_rails/view_helpers.rb +4 -2
- data/lib/qube_rails.rb +11 -2
- metadata +10 -2
|
@@ -39,12 +39,13 @@ module QubeRails
|
|
|
39
39
|
question message details choice choice_detail choice_description field label input select checkbox
|
|
40
40
|
button button_secondary search search_results search_result status summary detail error
|
|
41
41
|
rows row row_header segmented segment row_fields use_for_all stall
|
|
42
|
-
picks pick pick_detail pick_action reason button_ghost choice_footer choice_divider
|
|
42
|
+
picks pick pick_detail pick_action reason button_ghost choice_footer choice_divider choice_actions
|
|
43
43
|
].freeze
|
|
44
44
|
|
|
45
45
|
# Your CSS framework's classes for the engine's screens, by piece, added beside each element's
|
|
46
46
|
# qube- classes (a String, or an Array of them):
|
|
47
|
-
# config.ui_classes = { button: "
|
|
47
|
+
# config.ui_classes = { button: "rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white",
|
|
48
|
+
# input: "rounded-md border border-gray-300 px-3 py-2 text-sm" }
|
|
48
49
|
# A piece not in UI_PIECES raises, naming them.
|
|
49
50
|
def ui_classes=(map)
|
|
50
51
|
raise ArgumentError, "ui_classes is a Hash of piece => CSS classes, in #{FILE}" unless map.respond_to?(:each_pair)
|
|
@@ -113,6 +114,8 @@ module QubeRails
|
|
|
113
114
|
|
|
114
115
|
# The operation class for a slug, or nil when none of your operations has it.
|
|
115
116
|
def operation_class(slug)
|
|
117
|
+
return LinkCheck.for_slug(slug) if slug.to_s.start_with?(LinkCheck::SLUG_PREFIX)
|
|
118
|
+
|
|
116
119
|
name = operations[slug.to_s]
|
|
117
120
|
name && constantize(name)
|
|
118
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,15 +129,21 @@ module QubeRails
|
|
|
104
129
|
return false
|
|
105
130
|
end
|
|
106
131
|
|
|
107
|
-
|
|
108
|
-
|
|
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
|
|
112
139
|
connection.status = status
|
|
113
140
|
connection.stall_reason = event.stalled? ? event.stall_reason : nil
|
|
114
141
|
end
|
|
142
|
+
# An upgrade or downgrade in the dashboard shows here.
|
|
143
|
+
type = event.connection.is_a?(Hash) ? event.connection["type"].to_s : ""
|
|
144
|
+
connection.connection_type = type if Connection::TYPES.include?(type)
|
|
115
145
|
connection.revision = event.revision
|
|
146
|
+
forget_company_file(event) if event.event == COMPANY_FILE_CHANGED
|
|
116
147
|
|
|
117
148
|
if event.connected?
|
|
118
149
|
connected(event)
|
|
@@ -122,6 +153,8 @@ module QubeRails
|
|
|
122
153
|
stalled(event)
|
|
123
154
|
elsif event.resumed?
|
|
124
155
|
resumed(event)
|
|
156
|
+
elsif event.event == COMPANY_FILE_CHANGED
|
|
157
|
+
company_file_changed(event)
|
|
125
158
|
else
|
|
126
159
|
log_event(event)
|
|
127
160
|
end
|
|
@@ -129,10 +162,40 @@ module QubeRails
|
|
|
129
162
|
true
|
|
130
163
|
end
|
|
131
164
|
|
|
132
|
-
#
|
|
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.
|
|
133
196
|
REFRESH_LIMIT = 200
|
|
134
197
|
|
|
135
|
-
# A stall or a
|
|
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
|
|
136
199
|
# still going on this connection: tell them to refresh, after the change is committed (by the
|
|
137
200
|
# connection's runs index, at most REFRESH_LIMIT records).
|
|
138
201
|
def refresh_pages
|
data/lib/qube_rails/engine.rb
CHANGED
|
@@ -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
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module QubeRails
|
|
4
|
+
# Checks a connection's stale links (the old company file's, after
|
|
5
|
+
# connection.company_file_changed) against the file it opens now, once it's connected: a plain
|
|
6
|
+
# request per kind, querying up to BATCH of its ids (ListID or TxnID). An id found there with the
|
|
7
|
+
# same name is kept (current again, with the answer's EditSequence); one found under another name
|
|
8
|
+
# waits for the app's review (Connection#confirm_link / #reject_link); one not found is kept as
|
|
9
|
+
# missing. A kind that can't be looked up by id is kept as missing when the check starts. All are
|
|
10
|
+
# checked again on the next company file change. README, "A different company file".
|
|
11
|
+
#
|
|
12
|
+
# Its record is the connection, and its slug "qube_rails.check_links:<kind>", so it needs no
|
|
13
|
+
# entry in config.operations. The stale rows are its state: a check that's queued again (a second
|
|
14
|
+
# connected, a retried webhook) asks only about what is still stale.
|
|
15
|
+
class LinkCheck < Request
|
|
16
|
+
SLUG_PREFIX = "qube_rails.check_links:"
|
|
17
|
+
BATCH = 50
|
|
18
|
+
# How long after a check ended (or a queue got no answer) a held record queues it again.
|
|
19
|
+
RETRY_AFTER = 5 * 60
|
|
20
|
+
|
|
21
|
+
# Which id each kind's query takes, when the qube_sync gem's Operations::INDEX can't say (an
|
|
22
|
+
# older gem, or the inline backend). A kind in neither is never queried: it's kept as missing.
|
|
23
|
+
TXN_KINDS = %w[
|
|
24
|
+
ar_refund_credit_card bill bill_payment_check bill_payment_credit_card build_assembly charge check
|
|
25
|
+
credit_card_charge credit_card_credit credit_memo deposit estimate inventory_adjustment invoice
|
|
26
|
+
item_receipt journal_entry paycheck payroll_liability_adjustment payroll_liability_check
|
|
27
|
+
payroll_prior_payment payroll_year_to_date_adjustment purchase_order receive_payment sales_order
|
|
28
|
+
sales_receipt sales_tax_payment_check time_tracking transaction transfer transfer_inventory
|
|
29
|
+
vehicle_mileage vendor_credit
|
|
30
|
+
].freeze
|
|
31
|
+
LIST_KINDS = %w[
|
|
32
|
+
account billing_rate class currency customer customer_msg customer_type date_driven_terms employee
|
|
33
|
+
entity inventory_site item item_discount item_fixed_asset item_group item_inventory
|
|
34
|
+
item_inventory_assembly item_non_inventory item_other_charge item_payment item_sales_tax
|
|
35
|
+
item_sales_tax_group item_service item_sites item_subtotal job_type lead other_name payment_method
|
|
36
|
+
payroll_item payroll_item_non_wage payroll_item_wage price_level sales_rep sales_tax_code
|
|
37
|
+
ship_method standard_terms terms to_do unit_of_measure_set vehicle vendor vendor_type
|
|
38
|
+
workers_comp_code
|
|
39
|
+
].freeze
|
|
40
|
+
|
|
41
|
+
# "list_id" or "txn_id": what kind's query looks records up by, from the gem's INDEX when it
|
|
42
|
+
# says, else the lists above; nil for a kind that can't be looked up by id.
|
|
43
|
+
def self.id_param(kind)
|
|
44
|
+
entry = defined?(QubeSync::Operations::INDEX) ? QubeSync::Operations::INDEX["#{kind}.query"] : nil
|
|
45
|
+
return entry[:id_param] if entry&.key?(:id_param)
|
|
46
|
+
return "txn_id" if TXN_KINDS.include?(kind.to_s)
|
|
47
|
+
|
|
48
|
+
LIST_KINDS.include?(kind.to_s) ? "list_id" : nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Queues a check for each kind with stale links, when the connection is connected; a kind
|
|
52
|
+
# whose check is still going keeps it. A kind with no id to query is settled as missing at once.
|
|
53
|
+
# Returns the requests.
|
|
54
|
+
def self.start(connection)
|
|
55
|
+
return [] unless connection.connected? && !connection.qube_id.to_s.empty?
|
|
56
|
+
|
|
57
|
+
kinds = connection.stale_links.distinct.pluck(:kind).sort
|
|
58
|
+
unqueryable, queryable = kinds.partition { |kind| id_param(kind).nil? }
|
|
59
|
+
unqueryable.each { |kind| settle_unqueryable(connection, kind) }
|
|
60
|
+
queryable.filter_map do |kind|
|
|
61
|
+
self.for(kind).queue(connection, connection)
|
|
62
|
+
rescue Backend::APIError => e
|
|
63
|
+
raise unless e.status.between?(400, 499)
|
|
64
|
+
|
|
65
|
+
QubeRails.logger.warn("QuBe Sync: connection #{connection.id}: couldn't check its #{kind} links (#{e.status})")
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# From Connection#wait_for_link_check!, for a held record's stale kinds: queues a kind's check
|
|
71
|
+
# again when its last one ended (or its queue got no answer) more than RETRY_AFTER ago, or it
|
|
72
|
+
# has none. Errors are logged: the caller still raises LinkBeingChecked.
|
|
73
|
+
def self.revive(connection, kinds)
|
|
74
|
+
return unless connection.connected? && !connection.qube_id.to_s.empty?
|
|
75
|
+
|
|
76
|
+
kinds.each do |kind|
|
|
77
|
+
if id_param(kind).nil?
|
|
78
|
+
settle_unqueryable(connection, kind)
|
|
79
|
+
next
|
|
80
|
+
end
|
|
81
|
+
check = self.for(kind)
|
|
82
|
+
last = QubeRails::QueuedRequest.where(connection: connection).latest_for(connection, check::SLUG)
|
|
83
|
+
next unless last.nil? || dormant?(last)
|
|
84
|
+
|
|
85
|
+
last.touch if last && !last.ended? # the back-off counts from this try
|
|
86
|
+
check.queue(connection, connection)
|
|
87
|
+
rescue Backend::APIError, *Backend::NETWORK_ERRORS => e
|
|
88
|
+
QubeRails.logger.warn("QuBe Sync: connection #{connection.id}: couldn't queue its #{kind} link check " \
|
|
89
|
+
"again (#{e.class})")
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.dormant?(row)
|
|
94
|
+
since = row.ended? ? row.ended_at : (row.qube_id.nil? ? row.updated_at : nil)
|
|
95
|
+
!since.nil? && since < Time.now - RETRY_AFTER
|
|
96
|
+
end
|
|
97
|
+
private_class_method :dormant?
|
|
98
|
+
|
|
99
|
+
# Links of a kind no query can look up: kept as missing, and reported like a checked batch.
|
|
100
|
+
def self.settle_unqueryable(connection, kind)
|
|
101
|
+
links = connection.transaction do
|
|
102
|
+
rows = connection.stale_links.where(kind: kind).lock.to_a
|
|
103
|
+
now = Time.now
|
|
104
|
+
rows.each { |link| link.update!(stale_since: nil, missing_since: now) }
|
|
105
|
+
end
|
|
106
|
+
return if links.empty?
|
|
107
|
+
|
|
108
|
+
report(connection, kind, relinked: [], review: [], missing: links)
|
|
109
|
+
end
|
|
110
|
+
private_class_method :settle_unqueryable
|
|
111
|
+
|
|
112
|
+
def self.report(connection, kind, result)
|
|
113
|
+
found = result[:relinked].size + result[:review].size
|
|
114
|
+
QubeRails.logger.info("QuBe Sync: connection #{connection.id}: #{kind} links checked: " \
|
|
115
|
+
"#{found} found (#{result[:review].size} to review), " \
|
|
116
|
+
"#{result[:missing].size} missing")
|
|
117
|
+
QubeRails.config.connection_events_class.new(connection).links_checked(
|
|
118
|
+
kind: kind, found: found, relinked: result[:relinked], review: result[:review],
|
|
119
|
+
missing: result[:missing], stale_left: connection.stale_links.count
|
|
120
|
+
)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The check for one kind, as its own class (Request keys a request by record and SLUG).
|
|
124
|
+
def self.for(kind)
|
|
125
|
+
kind = kind.to_s
|
|
126
|
+
raise ArgumentError, "QuBe Sync: no kind #{kind.inspect} to check" unless kind.match?(/\A[a-z0-9_]+\z/)
|
|
127
|
+
|
|
128
|
+
@kinds ||= {}
|
|
129
|
+
@kinds[kind] ||= Class.new(self) do
|
|
130
|
+
const_set(:KIND, kind)
|
|
131
|
+
const_set(:SLUG, "#{SLUG_PREFIX}#{kind}")
|
|
132
|
+
const_set(:OPERATION, "#{kind}.query")
|
|
133
|
+
def self.name
|
|
134
|
+
"QubeRails::LinkCheck(#{self::KIND})"
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def self.for_slug(slug)
|
|
140
|
+
self.for(slug.to_s.delete_prefix(SLUG_PREFIX))
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def self.queue(_record, connection)
|
|
144
|
+
return nil unless connection.connected? && kind_links(connection).exists?
|
|
145
|
+
return nil unless id_key
|
|
146
|
+
|
|
147
|
+
super(connection, connection)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def self.kind_links(connection)
|
|
151
|
+
connection.stale_links.where(kind: self::KIND)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def self.id_key
|
|
155
|
+
LinkCheck.id_param(self::KIND)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# The ids asked about: up to BATCH of the kind's stale links.
|
|
159
|
+
def params
|
|
160
|
+
ids = self.class.kind_links(connection).order(:id).limit(BATCH).pluck(:qb_id)
|
|
161
|
+
{ self.class.id_key.to_sym => ids }
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def connection
|
|
165
|
+
queued_request.connection
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# The answer settles each id it asked about that's still stale.
|
|
169
|
+
def answered(event)
|
|
170
|
+
@settled = settle(event)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Every page is in (one: a query by id isn't paged): the app hears, and the next batch goes.
|
|
174
|
+
def done
|
|
175
|
+
result = @settled
|
|
176
|
+
# An answer from the old file: ask the new one.
|
|
177
|
+
@next = true if result.nil? && from_old_file?
|
|
178
|
+
return unless result
|
|
179
|
+
|
|
180
|
+
@next = true
|
|
181
|
+
LinkCheck.report(connection, self.class::KIND, result)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# The links stay stale, unused: a held record queues the check again after RETRY_AFTER (revive).
|
|
185
|
+
def failed(event)
|
|
186
|
+
log_event(event)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# A next batch that can't be queued now is queued again by revive.
|
|
190
|
+
def handle(event)
|
|
191
|
+
super
|
|
192
|
+
return unless @next
|
|
193
|
+
|
|
194
|
+
begin
|
|
195
|
+
self.class.queue(connection, connection)
|
|
196
|
+
rescue Backend::APIError, *Backend::NETWORK_ERRORS => e
|
|
197
|
+
QubeRails.logger.warn("QuBe Sync: connection #{connection.id}: couldn't queue its next " \
|
|
198
|
+
"#{self.class::KIND} link check (#{e.class})")
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
private
|
|
203
|
+
|
|
204
|
+
# nil when the answer isn't one it can trust (an error, rows it didn't ask for, an odd shape, or
|
|
205
|
+
# an answer from a file the connection has switched from): nothing is settled.
|
|
206
|
+
def settle(event)
|
|
207
|
+
if from_old_file?
|
|
208
|
+
QubeRails.logger.warn("QuBe Sync: connection #{connection.id}: #{self.class::KIND} link check " \
|
|
209
|
+
"#{queued_request.id} was queued before its company file changed; nothing settled")
|
|
210
|
+
return nil
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
key = self.class.id_key
|
|
214
|
+
asked = Array((queued_request.params || {})[key]).map(&:to_s)
|
|
215
|
+
found = found_rows(event, key, asked)
|
|
216
|
+
return nil unless found
|
|
217
|
+
|
|
218
|
+
# A missing id makes QuickBooks warn 500, naming only the first one, but every id it has
|
|
219
|
+
# still comes back: one not in the answer isn't in the file.
|
|
220
|
+
result = { relinked: [], review: [], missing: [] }
|
|
221
|
+
now = Time.now
|
|
222
|
+
asked.each do |qb_id|
|
|
223
|
+
link = self.class.kind_links(connection).find_by(qb_id: qb_id)
|
|
224
|
+
next unless link # settled already: a redelivery, or linked again by a run since
|
|
225
|
+
|
|
226
|
+
record = found[qb_id]
|
|
227
|
+
if record
|
|
228
|
+
keep(link, record, result)
|
|
229
|
+
else
|
|
230
|
+
link.update!(stale_since: nil, missing_since: now)
|
|
231
|
+
result[:missing] << link
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
result
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def from_old_file?
|
|
238
|
+
changed_at = connection.company_file_changed_at
|
|
239
|
+
!changed_at.nil? && queued_request.created_at < changed_at
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def found_rows(event, key, asked)
|
|
243
|
+
answer = event.response_json
|
|
244
|
+
data = answer.is_a?(Hash) && answer["status"] == "success" ? answer["data"] : nil
|
|
245
|
+
rows = data.nil? ? [] : Array(data)
|
|
246
|
+
unless data.is_a?(Array) && rows.all? { |row| row.is_a?(Hash) && asked.include?(row[key].to_s) }
|
|
247
|
+
QubeRails.logger.warn("QuBe Sync: connection #{connection.id}: #{self.class::KIND} link check " \
|
|
248
|
+
"answered unexpectedly; nothing settled, its links stay stale")
|
|
249
|
+
return nil
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
rows.to_h { |row| [row[key].to_s, row] }
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# An id is stamped with its record's creation time, so finding it only says the files share an
|
|
256
|
+
# origin. The same name (or no name kept) keeps it; another name may be a repurposed record.
|
|
257
|
+
def keep(link, record, result)
|
|
258
|
+
theirs = self.class.id_key == "txn_id" ? record["ref_number"] : record["full_name"] || record["name"]
|
|
259
|
+
if link.name.nil? || normal(link.name) == normal(theirs)
|
|
260
|
+
link.update!(stale_since: nil, missing_since: nil,
|
|
261
|
+
edit_sequence: record["edit_sequence"] || link.edit_sequence)
|
|
262
|
+
result[:relinked] << link
|
|
263
|
+
else
|
|
264
|
+
link.update!(stale_since: nil, review_since: Time.now, review_name: theirs.to_s,
|
|
265
|
+
edit_sequence: record["edit_sequence"] || link.edit_sequence)
|
|
266
|
+
result[:review] << { link: link, record: link.record, old_name: link.name, new_name: theirs.to_s }
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def normal(name)
|
|
271
|
+
name.to_s.strip.squeeze(" ").downcase
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
end
|