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

Fix #4138 fix delivery address #4540

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4bb8332
REFACTOR group examples, remove rubocop disable lines,
jimmyli97 Jul 18, 2024
77b2a55
RED add rspec to test address output in distribution PDFs
jimmyli97 Jul 18, 2024
dcdc0f8
GREEN Fix #4138 address output changes based on delivery method
jimmyli97 Jul 18, 2024
aa38b4b
BUGFIX pdf now writes when test fails
jimmyli97 Jul 23, 2024
f1d7273
RED add rspec for when partner has no addresses
jimmyli97 Jul 23, 2024
d64acc4
GREEN don't print address if no address or delivery address
jimmyli97 Jul 23, 2024
4557128
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Sep 12, 2024
0d53723
Add comments clarifying gitignore and comparison pdf helper rspec
jimmyli97 Sep 12, 2024
d722bca
Update bundler version
jimmyli97 Sep 12, 2024
4516951
REFACTOR replace helper test with environment variable
jimmyli97 Sep 24, 2024
ee159ab
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Sep 24, 2024
cec74ce
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Oct 17, 2024
c26f94b
update schema date and bundler version
jimmyli97 Oct 17, 2024
df4b975
RED remove env variable code, stub out console helper method
jimmyli97 Oct 17, 2024
26ece6e
GREEN add Rails console method for generating comparison pdfs, update…
jimmyli97 Oct 17, 2024
a21c7f2
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Nov 27, 2024
6da7702
Replace instance vars with structs and let
jimmyli97 Nov 27, 2024
8bc117e
FIX destroy request so db is clean, use destroy! instead so exception…
jimmyli97 Nov 27, 2024
ea7e241
Replace FactoryBot with calling ActiveModel.create, move methods to lib/
jimmyli97 Nov 28, 2024
39e8cb4
Fix formatting when address is incomplete, add rspecs, create and del…
jimmyli97 Dec 3, 2024
29e59df
Merge branch 'main' into 4138-fix-delivery-address
jimmyli97 Dec 3, 2024
4c6cd65
FIX replace with space if partner primary contact name/email/phone is…
jimmyli97 Dec 3, 2024
b00d1b0
FIX spacing between issued to and delivery address
jimmyli97 Dec 4, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ dump.rdb
*.pdf
/spec/example_failures.txt

# Track, don't ignore, RSpec comparison PDFs
!spec/fixtures/files/*.pdf

# Ignore Docker stuff (see issues #503, #603)
Dockerfile
docker-compose.yml
Expand Down
63 changes: 37 additions & 26 deletions app/pdfs/distribution_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,53 @@ def compute_and_render
text @organization.email, align: :right
end

text "Issued to:", style: :bold
font_size 12
text @distribution.partner.name
move_up 24
profile = @distribution.partner.profile

font_size 12
text "Partner Primary Contact:", style: :bold, align: :right
text (profile.primary_contact_name.presence || " "), align: :right
font_size 10
text (profile.primary_contact_email.presence || " "), align: :right
text (profile.primary_contact_phone.presence || " "), align: :right
move_up 40

text "Issued to:", style: :bold
font_size 12
text @distribution.partner.profile.primary_contact_name, align: :right
text @distribution.partner.name
font_size 10
text @distribution.partner.profile.primary_contact_email, align: :right
text @distribution.partner.profile.primary_contact_phone, align: :right
move_down 10

if %w(shipped delivered).include?(@distribution.delivery_method)
move_up 10
if (profile.address1.present? || profile.program_address1.present?) &&
(@distribution.delivery? || @distribution.shipped?)
if profile.program_address1.blank?
address1 = profile.address1
address2 = profile.address2
city = profile.city
state = profile.state
zip_code = profile.zip_code
else
address1 = profile.program_address1
address2 = profile.program_address2
city = profile.program_city
state = profile.program_state
zip_code = profile.program_zip_code.to_s
end

text "Delivery address:", style: :bold
font_size 10
text @distribution.partner.profile.address1
text @distribution.partner.profile.address2
text @distribution.partner.profile.city
text @distribution.partner.profile.state
text @distribution.partner.profile.zip_code
move_up 40

text "Issued on:", style: :bold, align: :right
font_size 12
text @distribution.distributed_at, align: :right
font_size 10
move_down 30
else
text "Issued on:", style: :bold
font_size 12
text @distribution.distributed_at
font_size 10
text address1
text address2
text city
text state
text zip_code
end

move_down 10
text "Issued on:", style: :bold
font_size 12
text @distribution.distributed_at
font_size 10

if @organization.ytd_on_distribution_printout
move_up 22
text "Items Received Year-to-Date:", style: :bold, align: :right
Expand Down
146 changes: 146 additions & 0 deletions lib/test_helpers/pdf_comparison_test_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
module PDFComparisonTestFactory
StorageCreation = Data.define(:organization, :storage_location, :items)
FilePaths = Data.define(:expected_pickup_file_path, :expected_same_address_file_path, :expected_different_address_file_path, :expected_incomplete_address_file_path, :expected_no_contact_file_path)

def create_organization_storage_items
org = Organization.create!(
name: "Essentials Bank 1",
short_name: "db",
street: "1500 Remount Road",
city: "Front Royal",
state: "VA",
zipcode: "22630",
email: "[email protected]",
logo: Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/files/logo.jpg"), "image/jpeg")
)

storage_location = StorageLocation.create!(
name: "Smithsonian Conservation Center",
address: "1500 Remount Road, Front Royal, VA 22630",
organization: org
)

base_item = BaseItem.find_or_create_by!(name: "10T Diapers", partner_key: "10t_diapers")

item1 = Item.create!(name: "Item 1", package_size: 50, value_in_cents: 100, organization: org, partner_key: base_item.partner_key)
item2 = Item.create!(name: "Item 2", value_in_cents: 200, organization: org, partner_key: base_item.partner_key)
item3 = Item.create!(name: "Item 3", value_in_cents: 300, organization: org, partner_key: base_item.partner_key)
item4 = Item.create!(name: "Item 4", package_size: 25, value_in_cents: 400, organization: org, partner_key: base_item.partner_key)

StorageCreation.new(org, storage_location, [item1, item2, item3, item4])
end

def create_partner(organization)
Partner.create!(name: "Leslie Sue", organization: organization, email: "[email protected]")
end

def get_file_paths
expected_pickup_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_pickup.pdf")
expected_same_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_same_address.pdf")
expected_different_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_program_address.pdf")
expected_incomplete_address_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_incomplete_address.pdf")
expected_no_contact_file_path = Rails.root.join("spec", "fixtures", "files", "distribution_no_contact.pdf")
FilePaths.new(expected_pickup_file_path, expected_same_address_file_path, expected_different_address_file_path, expected_incomplete_address_file_path, expected_no_contact_file_path)
end

private def create_profile(partner:, program_address1:, program_address2:, program_city:, program_state:, program_zip:,
address1: "Example Address 1", city: "Example City", state: "Example State", zip: "12345", primary_contact_name: "Jaqueline Kihn DDS", primary_contact_email: "[email protected]")

Partners::Profile.create!(
partner_id: partner.id,
essentials_bank_id: partner.organization.id,
primary_contact_name: primary_contact_name,
primary_contact_email: primary_contact_email,
address1: address1,
address2: "",
city: city,
state: state,
zip_code: zip,
program_address1: program_address1,
program_address2: program_address2,
program_city: program_city,
program_state: program_state,
program_zip_code: program_zip
)
end

def create_profile_no_address(partner)
create_profile(partner: partner, program_address1: "", program_address2: "", program_city: "", program_state: "", program_zip: "", address1: "", city: "", state: "", zip: "")
end

def create_profile_without_program_address(partner)
create_profile(partner: partner, program_address1: "", program_address2: "", program_city: "", program_state: "", program_zip: "")
end

def create_profile_with_program_address(partner)
create_profile(partner: partner, program_address1: "Example Program Address 1", program_address2: "", program_city: "Example Program City", program_state: "Example Program State", program_zip: 54321)
end

def create_profile_with_incomplete_address(partner)
create_profile(partner: partner, program_address1: "Example Program Address 1", program_address2: "", program_city: "", program_state: "", program_zip: "")
end

def create_profile_no_contact_with_program_address(partner)
create_profile(partner: partner, program_address1: "Example Program Address 1", program_address2: "", program_city: "Example Program City", program_state: "Example Program State", program_zip: 54321, primary_contact_name: "", primary_contact_email: "")
end

def create_line_items_request(distribution, partner, storage_creation)
LineItem.create!(itemizable: distribution, item: storage_creation.items[0], quantity: 50)
LineItem.create!(itemizable: distribution, item: storage_creation.items[1], quantity: 100)
storage_creation.organization.request_units.find_or_create_by!(name: "pack")
ItemUnit.find_or_create_by!(item: storage_creation.items[3], name: "pack")
req1 = Partners::ItemRequest.new(item: storage_creation.items[1], quantity: 30, name: storage_creation.items[1].name, partner_key: storage_creation.items[1].partner_key)
req2 = Partners::ItemRequest.new(item: storage_creation.items[2], quantity: 50, name: storage_creation.items[2].name, partner_key: storage_creation.items[2].partner_key)
req3 = Partners::ItemRequest.new(item: storage_creation.items[3], quantity: 120, name: storage_creation.items[3].name, partner_key: storage_creation.items[3].partner_key, request_unit: "pack")
Request.create!(
organization: storage_creation.organization,
partner: partner,
distribution: distribution,
request_items: [
{"item_id" => storage_creation.items[1].id, "quantity" => 30},
{"item_id" => storage_creation.items[2].id, "quantity" => 50},
{"item_id" => storage_creation.items[3].id, "quantity" => 120, "request_unit" => "pack"}
],
item_requests: [req1, req2, req3]
)
end

def create_dist(partner, storage_creation, delivery_method)
Time.zone = "America/Los_Angeles"
dist = Distribution.create!(partner: partner, delivery_method: delivery_method, issued_at: DateTime.new(2024, 7, 4, 0, 0, 0, "-07:00"), organization: storage_creation.organization, storage_location: storage_creation.storage_location)
create_line_items_request(dist, partner, storage_creation)
dist
end

private def create_comparison_pdf(storage_creation, profile_create_method, expected_file_path, delivery_method)
partner = create_partner(storage_creation.organization)
profile = PDFComparisonTestFactory.instance_method(profile_create_method).bind_call(Class.new.extend(PDFComparisonTestFactory), partner)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit over-the-top... can we not just move the possible inputs to this to class methods? Then you can just call PDFComparisonTestFactory.send(profile_create_method).

dist = create_dist(partner, storage_creation, delivery_method)
pdf_file = DistributionPdf.new(storage_creation.organization, dist).compute_and_render
File.binwrite(expected_file_path, pdf_file)
profile.destroy!
dist.request.destroy!
dist.destroy!
partner.destroy!
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably better to just wrap this in a transaction and roll it back (i.e. raise an ActiveRecord::Rollback) rather than manually calling destroy.

end

# helper function that can be called from Rails console to generate comparison PDFs
def create_comparison_pdfs
storage_creation = create_organization_storage_items
file_paths = get_file_paths

create_comparison_pdf(storage_creation, :create_profile_no_address, file_paths.expected_pickup_file_path, :pick_up)
create_comparison_pdf(storage_creation, :create_profile_without_program_address, file_paths.expected_same_address_file_path, :shipped)
create_comparison_pdf(storage_creation, :create_profile_with_program_address, file_paths.expected_different_address_file_path, :delivery)
create_comparison_pdf(storage_creation, :create_profile_with_incomplete_address, file_paths.expected_incomplete_address_file_path, :delivery)
create_comparison_pdf(storage_creation, :create_profile_no_contact_with_program_address, file_paths.expected_no_contact_file_path, :delivery)

storage_creation.storage_location.destroy!
storage_creation.items[0].destroy!
storage_creation.items[1].destroy!
storage_creation.items[2].destroy!
storage_creation.items[3].request_units.destroy_all
storage_creation.items[3].destroy!
storage_creation.organization.destroy!
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess the transaction/rollback can be moved to this method.

end
end
Binary file not shown.
Binary file not shown.
Binary file added spec/fixtures/files/distribution_pickup.pdf
Binary file not shown.
Binary file not shown.
Binary file added spec/fixtures/files/distribution_same_address.pdf
Binary file not shown.
Loading
Loading