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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +321 -0
  3. data/README.md +406 -76
  4. data/app/assets/javascripts/qube_rails/question.js +469 -43
  5. data/app/assets/stylesheets/qube_rails.css +1349 -912
  6. data/app/controllers/qube_rails/connections_controller.rb +42 -9
  7. data/app/controllers/qube_rails/questions_controller.rb +39 -7
  8. data/app/controllers/qube_rails/runs_controller.rb +4 -3
  9. data/app/controllers/qube_rails/storyboard_controller.rb +1 -1
  10. data/app/models/qube_rails/connection.rb +131 -9
  11. data/app/models/qube_rails/quickbooks_record.rb +22 -0
  12. data/app/models/qube_rails/run.rb +28 -10
  13. data/app/views/qube_rails/_error.html.erb +2 -2
  14. data/app/views/qube_rails/_manage_connection.html.erb +19 -0
  15. data/app/views/qube_rails/_question.html.erb +4 -1
  16. data/app/views/qube_rails/question/_choice.html.erb +34 -11
  17. data/app/views/qube_rails/question/_disclosure.html.erb +7 -0
  18. data/app/views/qube_rails/question/_exit_button.html.erb +8 -0
  19. data/app/views/qube_rails/question/_field.html.erb +4 -3
  20. data/app/views/qube_rails/question/_footer.html.erb +12 -4
  21. data/app/views/qube_rails/question/_inline_search.html.erb +12 -0
  22. data/app/views/qube_rails/question/_pick.html.erb +29 -6
  23. data/app/views/qube_rails/question/_picks.html.erb +2 -1
  24. data/app/views/qube_rails/question/_question.html.erb +31 -7
  25. data/app/views/qube_rails/question/_submit.html.erb +9 -0
  26. data/app/views/qube_rails/question/fields/_boolean.html.erb +2 -2
  27. data/app/views/qube_rails/question/fields/_number.html.erb +2 -2
  28. data/app/views/qube_rails/question/fields/_rows.html.erb +17 -4
  29. data/app/views/qube_rails/question/fields/_search.html.erb +6 -3
  30. data/app/views/qube_rails/question/fields/_select.html.erb +6 -5
  31. data/app/views/qube_rails/question/fields/_text.html.erb +2 -2
  32. data/app/views/qube_rails/question/rows/_segmented.html.erb +3 -1
  33. data/app/views/qube_rails/run_status/_detail.html.erb +5 -1
  34. data/app/views/qube_rails/run_status/_status.html.erb +5 -2
  35. data/app/views/qube_rails/storyboard/show.html.erb +4 -3
  36. data/config/locales/en.yml +30 -1
  37. data/config/routes.rb +2 -0
  38. data/db/migrate/20261031000000_add_qube_connection_type_and_run_headline.rb +12 -0
  39. data/db/migrate/20261124000000_add_qube_company_file_change.rb +17 -0
  40. data/lib/generators/qube_rails/templates/connection_events.rb.tt +17 -1
  41. data/lib/generators/qube_rails/templates/initializer.rb.tt +12 -4
  42. data/lib/qube_rails/configuration.rb +5 -2
  43. data/lib/qube_rails/connection_events.rb +70 -7
  44. data/lib/qube_rails/engine.rb +1 -0
  45. data/lib/qube_rails/link_check.rb +274 -0
  46. data/lib/qube_rails/operation.rb +116 -32
  47. data/lib/qube_rails/question_view.rb +293 -35
  48. data/lib/qube_rails/request.rb +2 -0
  49. data/lib/qube_rails/rows_view.rb +18 -5
  50. data/lib/qube_rails/run_status_view.rb +12 -7
  51. data/lib/qube_rails/storyboard/ask_about_add.json +9 -11
  52. data/lib/qube_rails/storyboard/choose.json +10 -7
  53. data/lib/qube_rails/storyboard/enum_select.json +5 -4
  54. data/lib/qube_rails/storyboard/missing_record.json +11 -14
  55. data/lib/qube_rails/storyboard/name_clash.json +51 -21
  56. data/lib/qube_rails/storyboard/near_match.json +18 -15
  57. data/lib/qube_rails/storyboard/number_boolean.json +5 -4
  58. data/lib/qube_rails/storyboard/record_search.json +15 -16
  59. data/lib/qube_rails/storyboard/refusal.json +15 -18
  60. data/lib/qube_rails/storyboard/rows.json +45 -31
  61. data/lib/qube_rails/storyboard.rb +28 -7
  62. data/lib/qube_rails/sync_status.rb +16 -3
  63. data/lib/qube_rails/version.rb +1 -1
  64. data/lib/qube_rails/view_helpers.rb +4 -2
  65. data/lib/qube_rails.rb +11 -2
  66. metadata +10 -2
@@ -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 == "starting"
74
- raise ArgumentError, "QuBe Sync: run #{run.id} is #{run.state}: only a start that got no answer is sent again"
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
- mark_failed(run) if refused?(e)
120
- raise RealtimeRequired, refusal_detail(e) if refusal_code(e) == "realtime_required"
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
- def self.mark_failed(run)
159
- run.update_columns(state: "failed", updated_at: Time.now)
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 each QuickBooks record an ending carries (event.output["references"], keyed by the
300
- # input's path: "customer_ref", "invoice_line_add.0.item_ref") to the record of yours your
301
- # block returns for it, on this run's connection, so the next run refers to it by id. The
302
- # block gets the path and the entry (kind, outcome and record: the record's own list as
303
- # "type", with list_id, edit_sequence and full_name) and returns your record, or nil to skip
304
- # it. An entry with no record (not found, or not reached before a cancel) is skipped. Works on
305
- # an added ending and on a cancelled one, which can carry some. Returns the rows linked.
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
- references = output.is_a?(Hash) ? output["references"] : nil
312
- return [] unless references.is_a?(Hash)
388
+ refs = output.is_a?(Hash) ? output["refs"] : nil
389
+ return [] unless refs.is_a?(Hash)
313
390
 
314
- references.each_with_object([]) do |(path, entry), linked|
315
- next unless entry.is_a?(Hash) && entry["record"].is_a?(Hash)
391
+ refs.filter_map do |path, entry|
392
+ next unless entry.is_a?(Hash) && !entry["list_id"].to_s.empty?
316
393
 
317
- found = entry["record"]
318
- next if found["list_id"].to_s.empty?
394
+ [path, { "list_id" => entry["list_id"], "type" => entry["type"] }]
395
+ end
396
+ end
319
397
 
320
- app_record = yield(path, entry)
321
- next unless app_record
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
- kind = found["type"].to_s.empty? ? entry["kind"] : found["type"]
324
- linked << connection.link(app_record, kind: kind, qb_id: found["list_id"],
325
- edit_sequence: found["edit_sequence"], name: found["full_name"])
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: QuBe Sync took the answer, so the run is
331
- # running again until its next webhook (which waits on the Web Connector's next poll).
332
- def answered(token, taken: false)
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.error if event.failed?
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?