Skip to content

Commit

Permalink
Allow to delete beneficiaries
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Jan 3, 2025
1 parent fd54aef commit 2df77ca
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 19 deletions.
7 changes: 7 additions & 0 deletions app/controllers/api/v1/beneficiaries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def update
end
end

def destroy
beneficiary = beneficiaries_scope.find(params[:id])
beneficiary.destroy!

head :no_content
end

private

def beneficiaries_scope
Expand Down
1 change: 1 addition & 0 deletions app/models/batch_operation/callout_population.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def create_callout_participations
callout_participations = contacts_scope.find_each.map do |contact|
{
contact_id: contact.id,
beneficiary_phone_number: contact.msisdn,
callout_id: callout.id,
callout_population_id: id,
msisdn: contact.msisdn,
Expand Down
16 changes: 4 additions & 12 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,10 @@ class Contact < ApplicationRecord
belongs_to :account

has_many :addresses, class_name: "BeneficiaryAddress", foreign_key: :beneficiary_id

has_many :callout_participations,
dependent: :restrict_with_error

has_many :callouts,
through: :callout_participations

has_many :phone_calls,
dependent: :restrict_with_error

has_many :remote_phone_call_events,
through: :phone_calls
has_many :callout_participations
has_many :callouts, through: :callout_participations
has_many :phone_calls
has_many :remote_phone_call_events, through: :phone_calls

delegate :call_flow_logic,
to: :account,
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

namespace :v1, module: "api/v1", as: "api_v1", defaults: { format: "json" } do
resources :beneficiaries, only: [ :index, :create, :show, :update ] do
resources :beneficiaries, only: [ :index, :create, :show, :update, :destroy ] do
resources :addresses, only: [ :index, :create, :show, :destroy ]
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class AddBeneficiaryPhoneNumberToCalloutParticipations < ActiveRecord::Migration[8.0]
def up
add_column :callout_participations, :beneficiary_phone_number, :string
execute <<-SQL
UPDATE callout_participations cp
SET beneficiary_phone_number = c.msisdn
FROM contacts c
WHERE cp.contact_id = c.id
SQL
change_column_null :callout_participations, :beneficiary_phone_number, false

remove_foreign_key :callout_participations, :contacts
add_foreign_key :callout_participations, :contacts, on_delete: :nullify
change_column_null :callout_participations, :contact_id, true

remove_foreign_key :phone_calls, :contacts
add_foreign_key :phone_calls, :contacts, on_delete: :nullify
change_column_null :phone_calls, :contact_id, true
end

def down
remove_column :callout_participations, :beneficiary_phone_number
change_column_null :callout_participations, :contact_id, false
change_column_null :phone_calls, :contact_id, false
end
end
13 changes: 7 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2024_12_11_092117) do
ActiveRecord::Schema[8.0].define(version: 2025_01_03_145109) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_catalog.plpgsql"
Expand Down Expand Up @@ -80,7 +80,7 @@

create_table "beneficiary_addresses", force: :cascade do |t|
t.bigint "beneficiary_id", null: false
t.citext "iso_region_code"
t.citext "iso_region_code", null: false
t.string "administrative_division_level_2_code"
t.string "administrative_division_level_2_name"
t.string "administrative_division_level_3_code"
Expand All @@ -96,7 +96,7 @@

create_table "callout_participations", force: :cascade do |t|
t.bigint "callout_id", null: false
t.bigint "contact_id", null: false
t.bigint "contact_id"
t.bigint "callout_population_id"
t.string "msisdn", null: false
t.string "call_flow_logic", null: false
Expand All @@ -105,6 +105,7 @@
t.datetime "updated_at", precision: nil, null: false
t.boolean "answered", default: false, null: false
t.integer "phone_calls_count", default: 0, null: false
t.string "beneficiary_phone_number", null: false
t.index ["callout_id", "contact_id"], name: "index_callout_participations_on_callout_id_and_contact_id", unique: true
t.index ["callout_id", "msisdn"], name: "index_callout_participations_on_callout_id_and_msisdn", unique: true
t.index ["callout_id"], name: "index_callout_participations_on_callout_id"
Expand Down Expand Up @@ -210,7 +211,7 @@

create_table "phone_calls", force: :cascade do |t|
t.bigint "callout_participation_id"
t.bigint "contact_id", null: false
t.bigint "contact_id"
t.string "status", null: false
t.string "msisdn", null: false
t.string "remote_call_id"
Expand Down Expand Up @@ -315,7 +316,7 @@
add_foreign_key "beneficiary_addresses", "contacts", column: "beneficiary_id", on_delete: :cascade
add_foreign_key "callout_participations", "batch_operations", column: "callout_population_id"
add_foreign_key "callout_participations", "callouts"
add_foreign_key "callout_participations", "contacts"
add_foreign_key "callout_participations", "contacts", on_delete: :nullify
add_foreign_key "callouts", "accounts"
add_foreign_key "callouts", "users", column: "created_by_id"
add_foreign_key "contacts", "accounts"
Expand All @@ -328,7 +329,7 @@
add_foreign_key "phone_calls", "accounts"
add_foreign_key "phone_calls", "callout_participations"
add_foreign_key "phone_calls", "callouts"
add_foreign_key "phone_calls", "contacts"
add_foreign_key "phone_calls", "contacts", on_delete: :nullify
add_foreign_key "recordings", "accounts"
add_foreign_key "recordings", "contacts"
add_foreign_key "recordings", "phone_calls"
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
factory :callout_participation do
callout
contact
beneficiary_phone_number { contact.msisdn }
end

factory :phone_call do
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/open_ews_api/v1/beneficiaries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,16 @@
)
end
end

delete "/v1/beneficiaries/:id" do
example "Delete a beneficiary" do
beneficiary = create(:beneficiary)
create(:address, beneficiary:)

set_authorization_header_for(beneficiary.account)
do_request(id: beneficiary.id)

expect(response_status).to eq(204)
end
end
end

0 comments on commit 2df77ca

Please sign in to comment.