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
data/lib/qube_rails/operation.rb
CHANGED
|
@@ -45,6 +45,11 @@ module QubeRails
|
|
|
45
45
|
# row stays "starting" and the error is raised: send it again with run.retry_start!, never a
|
|
46
46
|
# new start, or a run that did start runs twice (two invoices).
|
|
47
47
|
#
|
|
48
|
+
# Workflows run on realtime connections: on a standard one (connection.standard?) it raises
|
|
49
|
+
# RealtimeRequired before writing a run or calling QuBe Sync. A start QuBe Sync refuses for
|
|
50
|
+
# that (422 realtime_required) is kept as "realtime_required", with the error, and raises
|
|
51
|
+
# RealtimeRequired; once the connection is upgraded, run.retry_start! sends it again.
|
|
52
|
+
#
|
|
48
53
|
# While record's last run of this operation on connection hasn't ended (completed, failed or
|
|
49
54
|
# cancelled), it starts nothing new: it returns that run (run.reused? is true), sending its
|
|
50
55
|
# start again first when QuBe Sync hasn't confirmed it, with the same reference and input. The
|
|
@@ -56,6 +61,9 @@ module QubeRails
|
|
|
56
61
|
"qube_id): the customer finishes onboarding first"
|
|
57
62
|
end
|
|
58
63
|
|
|
64
|
+
raise_realtime_required(connection) if connection.standard?
|
|
65
|
+
connection.wait_for_link_check!(record, name)
|
|
66
|
+
|
|
59
67
|
run, input_error = claim_run(record, connection)
|
|
60
68
|
raise input_error if input_error
|
|
61
69
|
return retry_start(run) if run.reused?
|
|
@@ -67,16 +75,29 @@ module QubeRails
|
|
|
67
75
|
# the Idempotency-Key, the same workflow, input and webhook URL. QuBe Sync answers a repeat of
|
|
68
76
|
# a start it took with the run that start made (201, Idempotent-Replayed: true) rather than
|
|
69
77
|
# starting another, for as long as that run is kept (60 days after it ends); a start it never
|
|
70
|
-
# took starts now. Returns the run; a run already answered is returned as it is.
|
|
78
|
+
# took starts now. Returns the run; a run already answered is returned as it is. A start
|
|
79
|
+
# refused as realtime_required is sent again too: QuBe Sync stored nothing for it.
|
|
71
80
|
def self.retry_start(run)
|
|
72
81
|
return run if run.qube_run_id
|
|
73
|
-
unless run.state
|
|
74
|
-
raise ArgumentError, "QuBe Sync: run #{run.id} is #{run.state}: only a start that got no answer
|
|
82
|
+
unless RETRYABLE_STATES.include?(run.state)
|
|
83
|
+
raise ArgumentError, "QuBe Sync: run #{run.id} is #{run.state}: only a start that got no answer, or " \
|
|
84
|
+
"one refused as realtime_required, is sent again"
|
|
75
85
|
end
|
|
86
|
+
raise_realtime_required(run.connection) if run.connection.standard?
|
|
76
87
|
|
|
88
|
+
run.update_columns(state: "starting", error: nil, updated_at: Time.now) if run.realtime_required?
|
|
77
89
|
send_start(run, run.input)
|
|
78
90
|
end
|
|
79
91
|
|
|
92
|
+
RETRYABLE_STATES = %w[starting realtime_required].freeze
|
|
93
|
+
|
|
94
|
+
def self.raise_realtime_required(connection)
|
|
95
|
+
raise RealtimeRequired, "QuBe Sync: workflows run on realtime connections, and connection #{connection.id} " \
|
|
96
|
+
"is standard. Upgrade it to realtime from its page in the QuBe Sync dashboard (its " \
|
|
97
|
+
"type can't be changed through the API), then start the run again."
|
|
98
|
+
end
|
|
99
|
+
private_class_method :raise_realtime_required
|
|
100
|
+
|
|
80
101
|
# The record's run that hasn't ended on connection (reused), or a new row with a new reference
|
|
81
102
|
# and its input, written together under the connection row's lock: two starts of one record at
|
|
82
103
|
# once (a double click) find the same row, so they send the same reference and input. Returns
|
|
@@ -116,9 +137,11 @@ module QubeRails
|
|
|
116
137
|
QubeRails.client.start_run(run.connection.qube_id, self::WORKFLOW, input,
|
|
117
138
|
reference: run.reference, webhook_url: run.webhook_url)
|
|
118
139
|
rescue Backend::APIError => e
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
140
|
+
if e.status == 422 && refusal_code(e) == "realtime_required"
|
|
141
|
+
mark_realtime_required(run, refusal_detail(e))
|
|
142
|
+
raise RealtimeRequired, refusal_detail(e)
|
|
143
|
+
end
|
|
144
|
+
mark_failed(run, e) if refused?(e)
|
|
122
145
|
raise
|
|
123
146
|
end
|
|
124
147
|
record_start(run, started.is_a?(Hash) ? started : {})
|
|
@@ -155,18 +178,54 @@ module QubeRails
|
|
|
155
178
|
end
|
|
156
179
|
private_class_method :refusal_errors
|
|
157
180
|
|
|
158
|
-
|
|
159
|
-
|
|
181
|
+
# Kept, not failed: QuBe Sync stored nothing, so the same start works once it's upgraded.
|
|
182
|
+
def self.mark_realtime_required(run, detail)
|
|
183
|
+
run.error = { "code" => "realtime_required", "detail" => detail }
|
|
184
|
+
run.state = "realtime_required"
|
|
185
|
+
run.save!
|
|
186
|
+
end
|
|
187
|
+
private_class_method :mark_realtime_required
|
|
188
|
+
|
|
189
|
+
# Failed with QuBe Sync's refusal as its error, so the run status says why.
|
|
190
|
+
def self.mark_failed(run, error)
|
|
191
|
+
refusal = { "code" => "refused", "message" => refusal_message(error),
|
|
192
|
+
"detail" => "QuBe Sync refused the start (#{error.status})." }
|
|
193
|
+
run.update_columns(state: "failed", error: refusal, updated_at: Time.now)
|
|
160
194
|
end
|
|
161
195
|
private_class_method :mark_failed
|
|
162
196
|
|
|
197
|
+
# A sentence from a refusal's body: {"errors": a sentence, {code, detail}, {field: [messages]}
|
|
198
|
+
# or [{path, message}]}, or {"error": a sentence}.
|
|
199
|
+
def self.refusal_message(error)
|
|
200
|
+
parsed = JSON.parse(error.body.to_s)
|
|
201
|
+
parsed = {} unless parsed.is_a?(Hash)
|
|
202
|
+
text = refusal_text(parsed.key?("errors") ? parsed["errors"] : parsed["error"])
|
|
203
|
+
text.to_s.strip.empty? ? QubeRails.t("status.not_started") : text.strip
|
|
204
|
+
rescue JSON::ParserError
|
|
205
|
+
QubeRails.t("status.not_started")
|
|
206
|
+
end
|
|
207
|
+
private_class_method :refusal_message
|
|
208
|
+
|
|
209
|
+
def self.refusal_text(errors)
|
|
210
|
+
case errors
|
|
211
|
+
when String then errors
|
|
212
|
+
when Array
|
|
213
|
+
errors.map { |e| e.is_a?(Hash) ? [e["path"], e["message"]].compact.join(": ") : e.to_s }.join("; ")
|
|
214
|
+
when Hash
|
|
215
|
+
return errors["detail"] if errors["detail"].is_a?(String)
|
|
216
|
+
|
|
217
|
+
errors.map { |field, messages| "#{field.to_s.tr("_", " ")} #{Array(messages).join(", ")}" }.join("; ")
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
private_class_method :refusal_text
|
|
221
|
+
|
|
163
222
|
# The run's id, links and state from QuBe Sync's answer. Its webhooks may have come first, so
|
|
164
223
|
# the state is taken only while the row still says "starting".
|
|
165
224
|
def self.record_start(run, started)
|
|
166
225
|
links = started["links"].is_a?(Hash) ? started["links"] : {}
|
|
167
226
|
run.with_lock do
|
|
168
227
|
run.qube_run_id = started["id"]
|
|
169
|
-
run.run_url = links["run"]
|
|
228
|
+
run.run_url = links["self"] || links["run"] # a WorkflowRun's link is self; a webhook's, run
|
|
170
229
|
run.cancel_url = links["cancel"]
|
|
171
230
|
run.state = started["state"] if run.state == "starting" && started["state"]
|
|
172
231
|
run.save!
|
|
@@ -296,46 +355,69 @@ module QubeRails
|
|
|
296
355
|
{ full_name: name }
|
|
297
356
|
end
|
|
298
357
|
|
|
299
|
-
# Links
|
|
300
|
-
#
|
|
301
|
-
#
|
|
302
|
-
#
|
|
303
|
-
# "
|
|
304
|
-
#
|
|
305
|
-
# an
|
|
358
|
+
# Links the QuickBooks records a run settled to the records of yours your block returns for
|
|
359
|
+
# them, on this run's connection, so the next run refers to them by id (and quickbooks_ref
|
|
360
|
+
# sends { list_id: }). Takes either event that carries them (docs/workflow-record-events.md in
|
|
361
|
+
# QuBe Sync):
|
|
362
|
+
# - a "records" notification, sent as each pass settles them: event.data["records"], each
|
|
363
|
+
# with paths, action (picked, created, ...), kind, sent and record;
|
|
364
|
+
# - an ending: event.output["refs"], { path => { list_id, type } } for every reference.
|
|
365
|
+
# The block gets the input's path ("customer_ref", "invoice_line_add.0.item_ref") and the
|
|
366
|
+
# entry (list_id, type, and from a notification also kind, action, full_name, edit_sequence,
|
|
367
|
+
# sent), and returns your record, or nil to skip it. Returns the rows linked.
|
|
306
368
|
# link_references(event) do |path, entry|
|
|
307
369
|
# record.customer if path == "customer_ref"
|
|
308
370
|
# end
|
|
309
371
|
def link_references(event)
|
|
372
|
+
self.class.linkable(event).each_with_object([]) do |(path, entry), linked|
|
|
373
|
+
app_record = yield(path, entry)
|
|
374
|
+
next unless app_record
|
|
375
|
+
|
|
376
|
+
kind = entry["type"].to_s.empty? ? entry["kind"] : entry["type"]
|
|
377
|
+
linked << connection.link(app_record, kind: kind, qb_id: entry["list_id"],
|
|
378
|
+
edit_sequence: entry["edit_sequence"], name: entry["full_name"])
|
|
379
|
+
end
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# The [path, entry] pairs link_references links, from a "records" notification or an ending.
|
|
383
|
+
def self.linkable(event)
|
|
384
|
+
records = event["name"] == "records" && event["data"].is_a?(Hash) ? event["data"]["records"] : nil
|
|
385
|
+
return linkable_records(records) if records.is_a?(Array)
|
|
386
|
+
|
|
310
387
|
output = event["output"]
|
|
311
|
-
|
|
312
|
-
return [] unless
|
|
388
|
+
refs = output.is_a?(Hash) ? output["refs"] : nil
|
|
389
|
+
return [] unless refs.is_a?(Hash)
|
|
313
390
|
|
|
314
|
-
|
|
315
|
-
next unless entry.is_a?(Hash) && entry["
|
|
391
|
+
refs.filter_map do |path, entry|
|
|
392
|
+
next unless entry.is_a?(Hash) && !entry["list_id"].to_s.empty?
|
|
316
393
|
|
|
317
|
-
|
|
318
|
-
|
|
394
|
+
[path, { "list_id" => entry["list_id"], "type" => entry["type"] }]
|
|
395
|
+
end
|
|
396
|
+
end
|
|
319
397
|
|
|
320
|
-
|
|
321
|
-
|
|
398
|
+
def self.linkable_records(records)
|
|
399
|
+
records.flat_map do |entry|
|
|
400
|
+
found = entry.is_a?(Hash) ? entry["record"] : nil
|
|
401
|
+
next [] unless found.is_a?(Hash) && !found["list_id"].to_s.empty?
|
|
322
402
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
403
|
+
shown = found.slice("list_id", "type", "full_name", "edit_sequence")
|
|
404
|
+
.merge(entry.slice("kind", "action", "sent"))
|
|
405
|
+
Array(entry["paths"]).map { |path| [path, shown] }
|
|
326
406
|
end
|
|
327
407
|
end
|
|
328
408
|
|
|
329
409
|
# After your user's choice is in: the question is forgotten, unless the run has asked another
|
|
330
|
-
# since (its token is the question's own). taken:
|
|
331
|
-
#
|
|
332
|
-
|
|
410
|
+
# since (its token is the question's own). taken: the run has moved past the question (QuBe
|
|
411
|
+
# Sync took this answer, or said it was answered or asked again), so it reads as running until
|
|
412
|
+
# its next webhook (which waits on the Web Connector's next poll). cancelling: the answer was
|
|
413
|
+
# its cancel, so it reads "Cancelling…" (state "cancelling", this engine's own) until then.
|
|
414
|
+
def answered(token, taken: false, cancelling: false)
|
|
333
415
|
run.with_lock do
|
|
334
416
|
question = run.open_question
|
|
335
417
|
if question && question["token"] == token
|
|
336
418
|
run.question = nil
|
|
337
419
|
run.links = nil
|
|
338
|
-
run.state = "running" if taken
|
|
420
|
+
run.state = cancelling ? "cancelling" : "running" if taken
|
|
339
421
|
run.save!
|
|
340
422
|
end
|
|
341
423
|
end
|
|
@@ -364,10 +446,12 @@ module QubeRails
|
|
|
364
446
|
# whatever the handler does: state, outcome, and a failure's error or a cancel's reason.
|
|
365
447
|
run.state = event.run_state || run_state_for(event)
|
|
366
448
|
run.outcome = event.outcome
|
|
449
|
+
run.headline = event["headline"] if event["headline"].is_a?(String)
|
|
367
450
|
if ended?(event)
|
|
368
451
|
run.ended_at ||= Time.now
|
|
369
|
-
run.error = event
|
|
452
|
+
run.error = event["error"] if event.failed? # raw: qube_sync 0.6 types event.error
|
|
370
453
|
run.cancel_reason = event.cancel_reason if event.cancelled?
|
|
454
|
+
run.cancel_url = nil # links.cancel works only until the run ends
|
|
371
455
|
end
|
|
372
456
|
|
|
373
457
|
if event.awaiting_input?
|