Skip to content

Commit

Permalink
MAP-1038 rename FlightDetails -> ExtraditionFlight (#2246)
Browse files Browse the repository at this point in the history
* MAP-1038 rename FlightDetails -> ExtradtionFlight

* MAP-1038 Fix db migration

* Update spec/requests/api/extradition_flight_controller_update_spec.rb

Co-authored-by: Adam Sharp <adam.sharp@digital.justice.gov.uk>

* Update app/controllers/api/extradition_flight_controller.rb

Co-authored-by: Adam Sharp <adam.sharp@digital.justice.gov.uk>

* MAP-1038 Fix missed renames

* MAP-1038 Linting

* MAP-1038 Typos

* MAP-1038 revert create! -> new and save

* MAP-1038 datetime -> string column type

* MAP-1038 Fix table name in spec

* MAP-1038 Simplify extraditionFlight create

---------

Co-authored-by: Adam Sharp <adam.sharp@digital.justice.gov.uk>
  • Loading branch information
danbenton-mojdt and Thource committed May 13, 2024
1 parent af38eed commit 92db67c
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 131 deletions.
60 changes: 60 additions & 0 deletions app/controllers/api/extradition_flight_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Api
class ExtraditionFlightController < ApiController
before_action :set_extradition_flight, only: %i[index update]

PERMITTED_NEW_PARAMS = [
:type,
{
attributes: %i[flight_number flight_time],
relationships: [{ move: {} }],
},
].freeze

PERMITTED_UPDATE_PARAMS = [:type, { attributes: %i[flight_number flight_time] }].freeze

def index
render_extradition_flight(@extradition_flight, :ok)
end

def create
@extradition_flight = ExtraditionFlight.create!(new_extradition_flight_attributes)
render_extradition_flight(@extradition_flight, :created)
end

def update
@extradition_flight.update!(update_extradition_flight_attributes)

render_extradition_flight(@extradition_flight, :ok)
end

private

def set_extradition_flight
@extradition_flight = ExtraditionFlight.find_by!(move:)
end

def new_extradition_flight_params
params.require(:data).permit(PERMITTED_NEW_PARAMS)
end

def new_extradition_flight_attributes
@new_extradition_flight_attributes ||= new_extradition_flight_params.to_h[:attributes].merge!(move:)
end

def update_extradition_flight_params
params.require(:data).permit(PERMITTED_UPDATE_PARAMS)
end

def update_extradition_flight_attributes
@update_extradition_flight_attributes ||= update_extradition_flight_params.to_h[:attributes]
end

def move
@move ||= Move.accessible_by(current_ability).find(params.require(:move_id))
end

def render_extradition_flight(extradition_flight, status)
render_json extradition_flight, serializer: ExtraditionFlightSerializer, include: included_relationships, status:
end
end
end
63 changes: 0 additions & 63 deletions app/controllers/api/flight_details_controller.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class FlightDetails < ApplicationRecord
class ExtraditionFlight < ApplicationRecord
belongs_to :move

validates :flight_number, presence: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/move.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Move < VersionedModel
has_one :person, through: :profile
has_one :person_escort_record
has_one :youth_risk_assessment
has_one :flight_details
has_one :extradition_flight

belongs_to :prison_transfer_reason, optional: true
belongs_to :allocation, inverse_of: :moves, optional: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

class FlightDetailsSerializer
class ExtraditionFlightSerializer
include JSONAPI::Serializer

set_type :flight_details
set_type :extradition_flight

attributes :flight_number,
:flight_time
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/v2/move_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MoveSerializer
belongs_to :allocation, serializer: AllocationSerializer
belongs_to :original_move, serializer: V2::MoveSerializer

has_one_if_included :flight_details, serializer: FlightDetailsSerializer
has_one_if_included :extradition_flight, serializer: ExtraditionFlightSerializer

SUPPORTED_RELATIONSHIPS = %w[
profile.documents
Expand Down Expand Up @@ -85,7 +85,7 @@ class MoveSerializer
journeys.to_location
lodgings
lodgings.location
flight_details
extradition_flight
].freeze

INCLUDED_FIELDS = {
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
end
end

resources :flight_details, only: %i[index create update]
resources :extradition_flight, only: %i[index create update]

member do
post 'accept', controller: 'move_events'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateFlightDetails < ActiveRecord::Migration[7.0]
class CreateExtraditionFlights < ActiveRecord::Migration[7.0]
def change
create_table :flight_details, id: :uuid do |t|
create_table :extradition_flights, id: :uuid do |t|
t.string :flight_number, null: false
t.string :flight_time, null: false
t.references :move, null: false, foreign_key: true, type: :uuid
Expand Down
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@
t.datetime "disabled_at", precision: nil
end

create_table "flight_details", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
create_table "extradition_flights", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "flight_number", null: false
t.string "flight_time", null: false
t.uuid "move_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["move_id"], name: "index_flight_details_on_move_id"
t.index ["move_id"], name: "index_extradition_flights_on_move_id"
end

create_table "flipper_features", force: :cascade do |t|
Expand Down Expand Up @@ -691,7 +691,7 @@
add_foreign_key "allocations", "locations", column: "to_location_id", name: "fk_rails_allocations_to_location_id"
add_foreign_key "court_hearings", "moves"
add_foreign_key "documents", "moves"
add_foreign_key "flight_details", "moves"
add_foreign_key "extradition_flights", "moves"
add_foreign_key "framework_flags", "framework_questions"
add_foreign_key "framework_questions", "frameworks"
add_foreign_key "framework_responses", "framework_questions"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :flight_details do
factory :extradition_flight do
association(:move, factory: :move)
flight_number { 'BA0001' }
flight_time { '2024-01-01T12:00:00' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require 'rails_helper'

RSpec.describe FlightDetails do
subject(:flight_details) { build(:flight_details, flight_time:) }
RSpec.describe ExtraditionFlight do
subject(:extradition_flight) { build(:extradition_flight, flight_time:) }

let(:flight_time) { '2024-01-01T12:00:00Z' }

Expand Down
2 changes: 1 addition & 1 deletion spec/models/move_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
it { is_expected.to have_many(:notification_events) }
it { is_expected.to have_one(:person_escort_record) }
it { is_expected.to have_one(:youth_risk_assessment) }
it { is_expected.to have_one(:flight_details) }
it { is_expected.to have_one(:extradition_flight) }

it { is_expected.to validate_presence_of(:from_location) }
it { is_expected.to validate_presence_of(:date) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

require 'rails_helper'

RSpec.describe Api::FlightDetailsController do
describe 'POST /moves/:move_id/flight_details' do
RSpec.describe Api::ExtraditionFlightController do
describe 'POST /moves/:move_id/extradition_flight' do
subject(:do_post) do
post "/api/moves/#{move_id}/flight_details", params:, headers:, as: :json
post "/api/moves/#{move_id}/extradition_flight", params:, headers:, as: :json
end

let(:headers) do
Expand All @@ -31,7 +31,7 @@
let(:params) do
{
data: {
type: 'flight_details',
type: 'extradition_flight',
attributes: {
flight_number:,
flight_time:,
Expand All @@ -51,8 +51,8 @@
context 'when successful' do
let(:data) do
{
id: FlightDetails.last.id,
type: 'flight_details',
id: ExtraditionFlight.last.id,
type: 'extradition_flight',
attributes: {
flight_number:,
flight_time:,
Expand All @@ -68,7 +68,7 @@
}
end

let(:schema) { load_yaml_schema('post_flight_details_responses.yaml') }
let(:schema) { load_yaml_schema('post_extradition_flight_responses.yaml') }

it_behaves_like 'an endpoint that responds with success 201' do
before { do_post }
Expand All @@ -79,9 +79,9 @@
expect(response_json).to include_json(data:)
end

it 'creates the flight details in the DB' do
it 'creates the extradition flight in the DB' do
do_post
expect(move.flight_details.attributes.symbolize_keys.slice(:flight_number, :flight_time, :move_id)).to eq({
expect(move.extradition_flight.attributes.symbolize_keys.slice(:flight_number, :flight_time, :move_id)).to eq({
flight_number:,
flight_time:,
move_id:,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

require 'rails_helper'

RSpec.describe Api::FlightDetailsController do
describe 'GET /moves/:move_id/flight_details' do
RSpec.describe Api::ExtraditionFlightController do
describe 'GET /moves/:move_id/extradition_flight' do
subject(:do_get) do
get "/api/moves/#{move_id}/flight_details", headers:, as: :json
get "/api/moves/#{move_id}/extradition_flight", headers:, as: :json
end

let(:headers) do
Expand All @@ -28,13 +28,13 @@
let(:flight_number) { 'BA0123' }
let(:flight_time) { '2024-01-01' }

context 'when flight details exist' do
let!(:flight_details) { create(:flight_details, flight_number:, flight_time:, move:) }
context 'when an extradition flight exists' do
let!(:extradition_flight) { create(:extradition_flight, flight_number:, flight_time:, move:) }

let(:data) do
{
id: flight_details.id,
type: 'flight_details',
id: extradition_flight.id,
type: 'extradition_flight',
attributes: {
flight_number:,
flight_time:,
Expand All @@ -50,7 +50,7 @@
}
end

let(:schema) { load_yaml_schema('get_flight_details_responses.yaml') }
let(:schema) { load_yaml_schema('get_extradition_flight_responses.yaml') }

it_behaves_like 'an endpoint that responds with success 200' do
before { do_get }
Expand All @@ -63,7 +63,7 @@

it 'updates the flight number in the database' do
do_get
expect(flight_details.reload.flight_number).to eq(flight_number)
expect(extradition_flight.reload.flight_number).to eq(flight_number)
end

describe 'with included move' do
Expand Down Expand Up @@ -101,8 +101,8 @@
end
end

context 'when there are no flight details associated with the move' do
let(:detail_404) { "Couldn't find FlightDetails with [WHERE \"flight_details\".\"move_id\" = $1]" }
context 'when there is no extradition flight associated with the move' do
let(:detail_404) { "Couldn't find ExtraditionFlight with [WHERE \"extradition_flights\".\"move_id\" = $1]" }

it_behaves_like 'an endpoint that responds with error 404' do
before do
Expand Down

0 comments on commit 92db67c

Please sign in to comment.