Security, permission and search fixes

Security
- Connecting to QuickBooks (authenticate/oauth_callback) and all syncs
  are admin-only; the OAuth state is stored and verified on callback
- New "Bill issue time to QuickBooks" project permission; billing is now
  a POST and checks the permission and issue visibility
- Estimate/invoice PDFs require a logged-in user, or a customer share
  link limited to the estimate and invoices attached to that issue
  (the old require_user check never ran)
- Attachments patch keeps the share token per request instead of in
  Thread.current, so it can't leak into other requests

Permissions and share links
- customers#autocomplete and the filter_* actions are part of View
  customers, so non-admins can pick customers on forms
- customers#view no longer goes through authorize_global; share needs
  the new "Share issues with customers" permission and a visible issue
- Removed a check in customers#view that compared a value to itself

Bugs
- Subjects and customer names capitalize each word without lowercasing
  the rest (LED, JL, 4x4 stay as typed); subjects only when changed
- Settings page shows Invoice/Estimate sync times (Item/Account models
  no longer exist)
- customers#update shows validation errors instead of reporting success
- Billing marks each activity billed once sent to QBO, so retries and
  token refreshes don't create duplicate TimeActivities

Search
- New CustomerSearchService for autocomplete and the customer search
  page: nicknames (Bill/William), typos, word order, partial words,
  accents, and phone digits in any format; ranked, with the candidate
  list cached until a customer changes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-28 22:08:36 -04:00
1 parent aacc2d645d
commit 0523e8dd24
18 files changed
+412 -111

No files matched your search

+35 -12
View File
@@ -17,23 +17,36 @@ class BillIssueTimeJob < ActiveJob::Base
issue = Issue.find(issue_id)
log "Starting billing for issue ##{issue.id}"
failure = nil
issue.with_lock do
unbilled_entries = issue.time_entries.where(billed: [false, nil]).lock
unbilled_entries = issue.time_entries.where(billed: [false, nil]).lock.includes(:activity).to_a
return if unbilled_entries.blank?
totals = aggregate_hours(unbilled_entries)
return if totals.blank?
log "Aggregated hours for billing: #{totals.inspect}"
qbo = QboConnectionService.current!
qbo.perform_authenticated_request do |access_token|
create_time_activities(issue, totals, access_token, qbo)
# Activities already sent to QBO (or deliberately skipped) in this run. If QBO fails part way, or the
# token refresh re-runs the block, finished activities are not sent again.
done = []
begin
qbo = QboConnectionService.current!
qbo.perform_authenticated_request do |access_token|
create_time_activities(issue, totals, access_token, qbo, done)
end
rescue => e
failure = e
end
# Only mark billed AFTER successful QBO creation
unbilled_entries.update_all(billed: true)
# Mark billed only the entries whose activity was finished, and commit that even if a later
# activity failed, so a retry only sends what's left
finished = unbilled_entries.select { |entry| done.include?(entry.activity&.name) }
TimeEntry.where(id: finished.map(&:id)).update_all(billed: true) if finished.any?
end
raise failure if failure
log "Completed billing for issue ##{issue.id}"
Qbo.update_time_stamp
rescue => e
@@ -45,25 +58,34 @@ class BillIssueTimeJob < ActiveJob::Base
# Aggregate time entries by activity name and sum their hours
def aggregate_hours(entries)
entries.includes(:activity)
.group_by { |e| e.activity&.name }
entries.group_by { |e| e.activity&.name }
.transform_values { |rows| rows.sum(&:hours) }
.compact
end
# Create TimeActivity records in QBO for each activity type with the appropriate hours and link them to the issue's assigned employee and customer
def create_time_activities(issue, totals, access_token, qbo)
# Adds each activity name to done once it has been created in QBO (or skipped because it can't be billed).
def create_time_activities(issue, totals, access_token, qbo, done)
log "Creating TimeActivity records in QBO for issue ##{issue.id}"
time_service = Quickbooks::Service::TimeActivity.new( company_id: qbo.realm_id, access_token: access_token)
item_service = Quickbooks::Service::Item.new( company_id: qbo.realm_id, access_token: access_token )
totals.each do |activity_name, hours_float|
next if activity_name.blank?
next if hours_float.to_f <= 0
next if done.include?(activity_name)
# Not billable: skipped, and marked billed as before
if activity_name.blank? || hours_float.to_f <= 0
done << activity_name
next
end
item = find_item(item_service, activity_name)
next unless item
unless item
log "No QBO item named #{activity_name.inspect}; skipping"
done << activity_name
next
end
hours, minutes = convert_hours(hours_float)
@@ -82,6 +104,7 @@ class BillIssueTimeJob < ActiveJob::Base
log "Creating TimeActivity for #{activity_name} (#{hours}h #{minutes}m)"
time_service.create(time_entry)
done << activity_name
end
end