From 2df77cafc9cfe5234bb71631552b98b1c5e1872d Mon Sep 17 00:00:00 2001 From: Samnang Chhun Date: Fri, 3 Jan 2025 22:57:16 +0700 Subject: [PATCH] Allow to delete beneficiaries --- .../api/v1/beneficiaries_controller.rb | 7 +++++ .../batch_operation/callout_population.rb | 1 + app/models/contact.rb | 16 +++--------- config/routes.rb | 2 +- ..._phone_number_to_callout_participations.rb | 26 +++++++++++++++++++ db/schema.rb | 13 +++++----- spec/factories.rb | 1 + .../open_ews_api/v1/beneficiaries_spec.rb | 12 +++++++++ 8 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20250103145109_add_beneficiary_phone_number_to_callout_participations.rb diff --git a/app/controllers/api/v1/beneficiaries_controller.rb b/app/controllers/api/v1/beneficiaries_controller.rb index eb0e950d7..1cb85bc88 100644 --- a/app/controllers/api/v1/beneficiaries_controller.rb +++ b/app/controllers/api/v1/beneficiaries_controller.rb @@ -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 diff --git a/app/models/batch_operation/callout_population.rb b/app/models/batch_operation/callout_population.rb index fa8c166ba..f9d5a7e21 100644 --- a/app/models/batch_operation/callout_population.rb +++ b/app/models/batch_operation/callout_population.rb @@ -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, diff --git a/app/models/contact.rb b/app/models/contact.rb index c7ab55e3d..b79e5dce4 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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, diff --git a/config/routes.rb b/config/routes.rb index 7ba5a03f3..23f673417 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20250103145109_add_beneficiary_phone_number_to_callout_participations.rb b/db/migrate/20250103145109_add_beneficiary_phone_number_to_callout_participations.rb new file mode 100644 index 000000000..ed05e7275 --- /dev/null +++ b/db/migrate/20250103145109_add_beneficiary_phone_number_to_callout_participations.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 2c24cf1d0..e4cfe9d6d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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" @@ -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 @@ -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" @@ -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" @@ -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" @@ -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" diff --git a/spec/factories.rb b/spec/factories.rb index a9983e079..d39796f7f 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -101,6 +101,7 @@ factory :callout_participation do callout contact + beneficiary_phone_number { contact.msisdn } end factory :phone_call do diff --git a/spec/requests/open_ews_api/v1/beneficiaries_spec.rb b/spec/requests/open_ews_api/v1/beneficiaries_spec.rb index 70c69f1e4..8a6e52929 100644 --- a/spec/requests/open_ews_api/v1/beneficiaries_spec.rb +++ b/spec/requests/open_ews_api/v1/beneficiaries_spec.rb @@ -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