Fixing supporter and supporter note creation

This commit is contained in:
Clarissa Borges 2021-02-15 20:22:19 -03:00 committed by Eric Schultz
parent 3f8f5559ab
commit 885bcee03a
8 changed files with 19 additions and 11 deletions

View file

@ -5,7 +5,7 @@
module Campaigns module Campaigns
class SupportersController < ApplicationController class SupportersController < ApplicationController
include Controllers::Campaign::Current include Controllers::Campaign::Current
include Controllers::Campaign::Authorization include Controllers::Campaign::Authorization
before_action :authenticate_campaign_editor!, only: [:index] before_action :authenticate_campaign_editor!, only: [:index]
@ -21,5 +21,5 @@ module Campaigns
format.html format.html
end end
end end
end end
end end

View file

@ -11,13 +11,13 @@ class Nonprofits::SupporterNotesController < ApplicationController
# post /nonprofits/:nonprofit_id/supporters/:supporter_id/supporter_notes # post /nonprofits/:nonprofit_id/supporters/:supporter_id/supporter_notes
def create def create
render_json { InsertSupporterNotes.create(supporter_params[:supporter_note]) } render_json { InsertSupporterNotes.create(supporter_params) }
end end
# put /nonprofits/:nonprofit_id/supporters/:supporter_id/supporter_notes/:id # put /nonprofits/:nonprofit_id/supporters/:supporter_id/supporter_notes/:id
def update def update
render_json { UpdateSupporterNotes.update(current_supporter_note, render_json { UpdateSupporterNotes.update(current_supporter_note,
supporter_params[:supporter_note].merge({user_id: current_user&.id})) } supporter_note_content.merge({user_id: current_user&.id})) }
end end
# delete /nonprofits/:nonprofit_id/supporters/:supporter_id/supporter_notes/:id # delete /nonprofits/:nonprofit_id/supporters/:supporter_id/supporter_notes/:id
@ -32,6 +32,14 @@ class Nonprofits::SupporterNotesController < ApplicationController
end end
def supporter_params def supporter_params
{
content: supporter_note_content,
supporter: current_supporter,
user: current_user
}
end
def supporter_note_content
params.require(:supporter_note).require(:content) params.require(:supporter_note).require(:content)
end end
end end

View file

@ -76,7 +76,7 @@ module Nonprofits
# post /nonprofits/:nonprofit_id/supporters # post /nonprofits/:nonprofit_id/supporters
def create def create
render_json { InsertSupporter.create_or_update(nonprofit, create_supporter_params.to_h) } render_json { InsertSupporter.create_or_update(current_nonprofit, create_supporter_params.to_h) }
end end
# put /nonprofits/:nonprofit_id/supporters/:id # put /nonprofits/:nonprofit_id/supporters/:id

View file

@ -13,7 +13,7 @@ module InsertSupporterNotes
inserted = nil inserted = nil
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
inserted = note_supporter_users.map do |nsu| inserted = note_supporter_users.map do |nsu|
nsu[:supporter].supporter_notes.create(nsu[:note].merge({user: nsu[:user]})) nsu[:supporter].supporter_notes.create!(content: nsu[:content], user: nsu[:user])
end end
InsertActivities.for_supporter_notes(inserted) InsertActivities.for_supporter_notes(inserted)
end end

View file

@ -93,7 +93,7 @@ module PayRecurringDonation
rd.save! rd.save!
result['recurring_donation'] = rd result['recurring_donation'] = rd
Houdini.event_publisher.announce(:recurring_donation_payment_failed, donation) Houdini.event_publisher.announce(:recurring_donation_payment_failed, donation)
InsertSupporterNotes.create({supporter:Supporter.find(donation['supporter_id']), user: User.find(540), note:{ content: "This supporter had a payment failure for their recurring donation with ID #{rd_id}"}}) InsertSupporterNotes.create({supporter:Supporter.find(donation['supporter_id']), user: User.find(540), content: "This supporter had a payment failure for their recurring donation with ID #{rd_id}"})
end end
result result
end end

View file

@ -96,7 +96,7 @@ module UpdateRecurringDonations
.where('id=$id', id: rd_id.to_i) .where('id=$id', id: rd_id.to_i)
) )
rd = QueryRecurringDonations.fetch_for_edit(rd_id)['recurring_donation'] rd = QueryRecurringDonations.fetch_for_edit(rd_id)['recurring_donation']
InsertSupporterNotes.create({ supporter: Supporter.find(rd['supporter_id']), user: nil, note: {content: "This supporter's recurring donation for $#{Format::Currency.cents_to_dollars(rd['amount'])} was cancelled by #{rd['cancelled_by']} on #{Format::Date.simple(rd['cancelled_at'])}"}}) InsertSupporterNotes.create({ supporter: Supporter.find(rd['supporter_id']), user: nil, content: "This supporter's recurring donation for $#{Format::Currency.cents_to_dollars(rd['amount'])} was cancelled by #{rd['cancelled_by']} on #{Format::Date.simple(rd['cancelled_at'])}"})
unless dont_notify_nonprofit unless dont_notify_nonprofit
RecurringDonationCancelledJob.perform_later(Donation.find(rd['donation_id'])) RecurringDonationCancelledJob.perform_later(Donation.find(rd['donation_id']))
end end

View file

@ -11,8 +11,8 @@ describe InsertSupporterNotes do
let(:sn_first) {SupporterNote.first } let(:sn_first) {SupporterNote.first }
let(:sn_last) {SupporterNote.last } let(:sn_last) {SupporterNote.last }
it '.create' do it '.create' do
InsertSupporterNotes.create({supporter:supporter, user: user, note: {content: content}}, InsertSupporterNotes.create({supporter: supporter, user: user, content: content},
{supporter:supporter, user: user, note: {content: content_2}}) {supporter: supporter, user: user, content: content_2})
expect(SupporterNote.count).to eq 2 expect(SupporterNote.count).to eq 2
expect(sn_first.attributes.except('id')).to eq({ expect(sn_first.attributes.except('id')).to eq({
'content' => content, 'content' => content,

View file

@ -8,7 +8,7 @@ describe UpdateRecurringDonations do
# deactivate a recurring donation # deactivate a recurring donation
describe '.cancel' do describe '.cancel' do
let(:np) { force_create(:nm_justice) } let!(:np) { force_create(:nm_justice) }
let(:s) { force_create(:supporter, nonprofit: np) } let(:s) { force_create(:supporter, nonprofit: np) }
let(:donation) { force_create(:donation, nonprofit_id: np.id, supporter_id: s.id) } let(:donation) { force_create(:donation, nonprofit_id: np.id, supporter_id: s.id) }
let(:email) { 'test@test.com' } let(:email) { 'test@test.com' }