Skip to content

Commit

Permalink
feat: Add vehicle_depot to Journey and JourneyStart & JourneyChangeVe…
Browse files Browse the repository at this point in the history
…hicle events (#2273)

[MAP-972]
  • Loading branch information
jimbali authored Jun 5, 2024
1 parent 0a333ad commit f14e7d2
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 10 deletions.
19 changes: 12 additions & 7 deletions app/controllers/api/journey_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class JourneyEventsController < ApiController
around_action :idempotent_action

COMMON_PARAMS = [:type, { attributes: %i[timestamp notes] }].freeze
START_COMPLETE_PARAMS = [:type, { attributes: %i[timestamp notes vehicle_reg] }].freeze
START_PARAMS = [:type, { attributes: %i[timestamp notes vehicle_reg vehicle_depot] }].freeze
COMPLETE_PARAMS = [:type, { attributes: %i[timestamp notes vehicle_reg] }].freeze
LOCKOUT_PARAMS = [:type, { attributes: %i[timestamp notes], relationships: { from_location: {} } }].freeze
LODGING_PARAMS = [:type, { attributes: %i[timestamp notes], relationships: { to_location: {} } }].freeze

Expand All @@ -20,8 +21,8 @@ def cancel
end

def complete
JourneyEvents::ParamsValidator.new(start_complete_params).validate!
process_event(journey, GenericEvent::JourneyComplete, start_complete_params)
JourneyEvents::ParamsValidator.new(complete_params).validate!
process_event(journey, GenericEvent::JourneyComplete, complete_params)
render status: :no_content
end

Expand All @@ -44,8 +45,8 @@ def reject
end

def start
JourneyEvents::ParamsValidator.new(start_complete_params).validate!
process_event(journey, GenericEvent::JourneyStart, start_complete_params)
JourneyEvents::ParamsValidator.new(start_params).validate!
process_event(journey, GenericEvent::JourneyStart, start_params)
render status: :no_content
end

Expand Down Expand Up @@ -75,8 +76,12 @@ def common_params
@common_params ||= params.require(:data).permit(COMMON_PARAMS).to_h
end

def start_complete_params
@start_complete_params ||= params.require(:data).permit(START_COMPLETE_PARAMS).to_h
def start_params
@start_params ||= params.require(:data).permit(START_PARAMS).to_h
end

def complete_params
@complete_params ||= params.require(:data).permit(COMPLETE_PARAMS).to_h
end

def journey
Expand Down
6 changes: 5 additions & 1 deletion app/models/generic_event/journey_change_vehicle.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class GenericEvent
class JourneyChangeVehicle < GenericEvent
details_attributes :vehicle_reg, :previous_vehicle_reg
details_attributes :vehicle_reg, :previous_vehicle_reg, :vehicle_depot
eventable_types 'Journey'

include VehicleRegValidations
Expand All @@ -9,6 +9,10 @@ class JourneyChangeVehicle < GenericEvent

def trigger(*)
eventable.vehicle_registration = vehicle_reg

if vehicle_depot.present?
eventable.vehicle_depot = vehicle_depot
end
end
end
end
6 changes: 5 additions & 1 deletion app/models/generic_event/journey_start.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class GenericEvent
class JourneyStart < GenericEvent
details_attributes :vehicle_reg
details_attributes :vehicle_reg, :vehicle_depot
eventable_types 'Journey'
validate_occurs_before 'GenericEvent::JourneyComplete'
validate_occurs_after 'GenericEvent::MoveStart'
Expand All @@ -12,6 +12,10 @@ def trigger(*)
Sentry.capture_message("#{self.class} created without vehicle_reg", level: 'warning', extra: { supplier: supplier&.key })
end

if vehicle_depot.present?
eventable.vehicle_depot = vehicle_depot
end

eventable.start
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/journey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def vehicle_registration=(reg)
(self.vehicle ||= {})['registration'] = reg
end

def vehicle_depot
vehicle['depot'] if vehicle
end

def vehicle_depot=(reg)
(self.vehicle ||= {})['depot'] = reg
end

def handle_event_run(dry_run: false)
save! if changed? && valid? && !dry_run
end
Expand Down
25 changes: 24 additions & 1 deletion spec/models/generic_event/journey_change_vehicle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe GenericEvent::JourneyChangeVehicle do
subject(:generic_event) { build(:event_journey_change_vehicle) }

it_behaves_like 'an event with details', :vehicle_reg, :previous_vehicle_reg
it_behaves_like 'an event with details', :vehicle_reg, :previous_vehicle_reg, :vehicle_depot
it_behaves_like 'an event with eventable types', 'Journey'
it_behaves_like 'an event that specifies a vehicle registration'

Expand All @@ -20,5 +20,28 @@
.from(generic_event.eventable.vehicle_registration)
.to(generic_event.vehicle_reg)
end

context 'when supplied a vehicle_depot' do
before do
generic_event.vehicle_depot = 'a vehicle depot'
end

it 'sets the vehicle_depot on the journey' do
generic_event.trigger
expect(generic_event.eventable.vehicle_depot).to eq(generic_event.vehicle_depot)
end
end

context 'when not supplied a vehicle_depot' do
before do
generic_event.eventable.vehicle_depot = 'Home Depot'
generic_event.vehicle_depot = nil
end

it 'does not set the vehicle_depot on the journey' do
generic_event.trigger
expect(generic_event.eventable.vehicle_depot).to eq('Home Depot')
end
end
end
end
23 changes: 23 additions & 0 deletions spec/models/generic_event/journey_start_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@
end
end

context 'when supplied a vehicle_depot' do
before do
generic_event.vehicle_depot = 'a vehicle depot'
end

it 'sets the vehicle_depot on the journey' do
generic_event.trigger
expect(generic_event.eventable.vehicle_depot).to eq(generic_event.vehicle_depot)
end
end

context 'when not supplied a vehicle_depot' do
before do
generic_event.eventable.vehicle_depot = 'Home Depot'
generic_event.vehicle_depot = nil
end

it 'does not set the vehicle_depot on the journey' do
generic_event.trigger
expect(generic_event.eventable.vehicle_depot).to eq('Home Depot')
end
end

it_behaves_like 'an event that must not occur after', 'GenericEvent::JourneyComplete'
it_behaves_like 'an event that must not occur before', 'GenericEvent::MoveStart'
end
26 changes: 26 additions & 0 deletions spec/models/journey_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,32 @@
end
end

describe '#vehicle_depot=' do
subject(:journey) { build(:journey) }

let(:new_depot) { 'Depot 2' }

context 'when vehicle was already present' do
before do
journey.vehicle = { id: '12345678ABC', registration: 'AB12 CDE', depot: 'Depot 1' }
end

it 'assigns the new vehicle depot value' do
expect { journey.vehicle_depot = new_depot }.to change(journey, :vehicle_depot).from('Depot 1').to('Depot 2')
end
end

context 'when vehicle was not already present' do
before do
journey.vehicle = nil
end

it 'assigns the new depot' do
expect { journey.vehicle_depot = new_depot }.to change(journey, :vehicle_depot).from(nil).to('Depot 2')
end
end
end

describe '#number' do
let(:move) { create(:move) }
let(:journey_1) { create(:journey, move:, client_timestamp: Date.new(2020, 1, 1)) }
Expand Down
14 changes: 14 additions & 0 deletions spec/requests/api/journey_events_controller_start_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
attributes: {
timestamp: '2020-04-23T18:25:43.511Z',
notes: 'something noteworthy',
vehicle_reg: 'B0RT',
vehicle_depot: 'Shelbyville',
},
},
}
Expand All @@ -48,6 +50,18 @@
event = GenericEvent.last
expect(event.created_by).to eq('TEST_USER')
end

it 'sets the correct vehicle_reg' do
do_post
event = GenericEvent.last
expect(event.vehicle_reg).to eq('B0RT')
end

it 'sets the correct vehicle_depot' do
do_post
event = GenericEvent.last
expect(event.vehicle_depot).to eq('Shelbyville')
end
end

context 'with unhappy params' do
Expand Down
4 changes: 4 additions & 0 deletions swagger/v1/post_journey_start.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ PostJourneyStart:
$ref: notes_attribute.yaml#/Notes
vehicle_reg:
$ref: vehicle_reg_attribute.yaml#/Notes
vehicle_depot:
type: string
example: Leeds
description: The depot where the vehicle is based

0 comments on commit f14e7d2

Please sign in to comment.