Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to delete beneficiaries #1680

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,6 +37,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 @@ -12,18 +12,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
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@
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
get "stats" => "beneficiaries/stats#index", on: :collection

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this to a separate rake task for migrating the data. There are probably millions of callout participations so this might take a long time and the app will be down while this migration is running

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
9 changes: 5 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -212,7 +213,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 @@ -317,7 +318,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 @@ -330,7 +331,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 @@ -102,6 +102,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 @@ -434,4 +434,16 @@
expect(response_status).to eq(400)
end
end

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

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

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