mirror of
https://github.com/rickbarrette/redmine_qbo_vehicles.git
synced 2026-04-02 07:01:59 -04:00
Compare commits
3 Commits
89dc3cd5f4
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| c38bced329 | |||
| 8503c48944 | |||
| 44bfb69e22 |
@@ -76,10 +76,20 @@ class VehiclesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
# used by the polling JS in vehicle#show
|
||||
def status
|
||||
vehicle = Vehicle.find(params[:id])
|
||||
|
||||
# decode the vin if it hasn't been done for some reason
|
||||
unless vehicle.vin_decoded
|
||||
if vehicle.error.nil?
|
||||
VehicleVinDecodeJob.perform_later(vehicle)
|
||||
end
|
||||
end
|
||||
|
||||
render json: {
|
||||
decoded: vehicle.vin_decoded
|
||||
decoded: vehicle.vin_decoded,
|
||||
error: vehicle.error
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class VehicleVinDecodeJob < ApplicationJob
|
||||
|
||||
unless result.success?
|
||||
log "Failed to decode vin"
|
||||
vehicle.update(vin_decoded: false)
|
||||
vehicle.update(vin_decoded: false, error: result.error)
|
||||
return
|
||||
end
|
||||
|
||||
@@ -33,7 +33,8 @@ class VehicleVinDecodeJob < ApplicationJob
|
||||
doors: details.doors.presence || vehicle.doors,
|
||||
trim: details.trim.presence || vehicle.trim,
|
||||
name: vehicle.to_s,
|
||||
vin_decoded: true
|
||||
vin_decoded: true,
|
||||
error: nil
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class Vehicle < ApplicationRecord
|
||||
validates :customer, presence: true
|
||||
validates :vin, presence: true, uniqueness: true
|
||||
before_validation :normalize_vin
|
||||
after_commit :enqueue_vin_decode
|
||||
after_commit :enqueue_vin_decode, if: :saved_change_to_vin?
|
||||
acts_as_searchable columns: %w[vin make model year], scope: ->(_context) { all }, date_column: :updated_at
|
||||
acts_as_event :title => Proc.new {|o| "#{o.to_s}"},
|
||||
:url => Proc.new {|o| { :controller => 'vehicles', :action => 'show', :id => o.id} },
|
||||
@@ -47,7 +47,6 @@ class Vehicle < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.search(query)
|
||||
return none if query.blank?
|
||||
q = "%#{sanitize_sql_like(query)}%"
|
||||
where( "vin LIKE :q OR make LIKE :q OR model LIKE :q OR year LIKE :q", q: q)
|
||||
end
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
<h2><%=t(:field_vehicle)%> #<%=@vehicle.id%></h2>
|
||||
|
||||
<% unless @vehicle.vin_decoded? %>
|
||||
<div id="vin-status" class="flash notice">
|
||||
<div id="vin-status" class="flash <%= @vehicle.error ? "error" : "notice" %>">
|
||||
<% if @vehicle.error%>
|
||||
<%= @vehicle.error %>
|
||||
<% else %>
|
||||
<%= t :notice_decoding_vin %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
@@ -35,9 +39,11 @@
|
||||
|
||||
if (alreadyDecoded) return;
|
||||
|
||||
const interval = 3000; // 3 seconds
|
||||
const interval = 3000;
|
||||
let attempts = 0;
|
||||
const maxAttempts = 40; // ~2 minutes
|
||||
const maxAttempts = 40;
|
||||
|
||||
const statusEl = document.getElementById("vin-status");
|
||||
|
||||
const checkStatus = () => {
|
||||
fetch(`/vehicles/${vehicleId}/status`, {
|
||||
@@ -45,19 +51,51 @@
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
|
||||
// SUCCESS → reload
|
||||
if (data.decoded) {
|
||||
window.location.reload();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
// ERROR → stop + show message
|
||||
if (data.error) {
|
||||
clearInterval(timer);
|
||||
|
||||
if (statusEl) {
|
||||
statusEl.classList.remove("notice");
|
||||
statusEl.classList.add("error");
|
||||
statusEl.textContent = data.error;
|
||||
}
|
||||
|
||||
console.warn("VIN decode failed:", data.error);
|
||||
return;
|
||||
}
|
||||
|
||||
// continue polling
|
||||
attempts++;
|
||||
if (attempts >= maxAttempts) {
|
||||
clearInterval(timer);
|
||||
console.warn("VIN decode polling timed out");
|
||||
|
||||
if (statusEl) {
|
||||
statusEl.classList.remove("notice");
|
||||
statusEl.classList.add("warning");
|
||||
statusEl.textContent = "VIN decode timed out. Please refresh or try again.";
|
||||
}
|
||||
|
||||
console.warn("VIN decode polling timed out");
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Polling error:", err);
|
||||
clearInterval(timer);
|
||||
|
||||
if (statusEl) {
|
||||
statusEl.classList.remove("notice");
|
||||
statusEl.classList.add("error");
|
||||
statusEl.textContent = "Error checking VIN status.";
|
||||
}
|
||||
|
||||
console.error("Polling error:", err);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -8,15 +8,20 @@
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
class AddIndexes < ActiveRecord::Migration[7.0]
|
||||
class AddPollingAndIndexes < ActiveRecord::Migration[7.0]
|
||||
|
||||
def change
|
||||
add_column :vehicles, :vin_decoded, :boolean, default: false, null: false
|
||||
add_column :vehicles, :error, :string
|
||||
add_index :vehicles, :vin_decoded
|
||||
add_index :vehicles, :vin, unique: true
|
||||
add_index :vehicles, :make
|
||||
add_index :vehicles, :model
|
||||
add_index :vehicles, :year
|
||||
|
||||
Vehicle.all.each do |v|
|
||||
VehicleVinDecodeJob.perform_later(v.id)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user