Skip to content

Commit

Permalink
Test: top_5_merchants_by_revenue
Browse files Browse the repository at this point in the history
  • Loading branch information
alepbloyd committed Aug 2, 2022
1 parent a8d0851 commit 7e8a120
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
7 changes: 0 additions & 7 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace :csv_load do
t.id = row['id']
t.invoice_id = row['invoice_id']
t.credit_card_number = row['credit_card_number']
t.credit_card_expitation_date = row['credit_card_expiration_date']
t.credit_card_expiration_date = row['credit_card_expiration_date']

if row['result'] == "failed"
t.result = 0
Expand Down
4 changes: 2 additions & 2 deletions db/migrate/20220726041935_create_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ def change
create_table :transactions do |t|
t.integer :invoice_id
t.bigint :credit_card_number
t.datetime :credit_card_expitation_date
t.string :result
t.datetime :credit_card_expiration_date
t.integer :result
t.datetime :created_at
t.datetime :updated_at
t.index ["invoice_id"], name: "index_transactions_on_invoice_id"
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
create_table "transactions", force: :cascade do |t|
t.integer "invoice_id"
t.bigint "credit_card_number"
t.datetime "credit_card_expitation_date"
t.string "result"
t.datetime "credit_card_expiration_date"
t.integer "result"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["invoice_id"], name: "index_transactions_on_invoice_id"
Expand Down
86 changes: 81 additions & 5 deletions spec/features/admin/merchants/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,87 @@

expect(page).to have_content("Trader Joes")
end
end

# Admin Merchant Show
it 'has names of the top 5 merchants by total revenue generated' do
merchant1 = Merchant.create!(name: "Snake Shop")
merchant2 = Merchant.create!(name: "Fish Foods")
merchant3 = Merchant.create!(name: "Cat Cafe")
merchant4 = Merchant.create!(name: "Dog Diner")
merchant5 = Merchant.create!(name: "Aardvark Accessories")
merchant6 = Merchant.create!(name: "Elephant Earmuffs")

customer = Customer.create!(first_name: "Alep", last_name: "Bloyd")

item1_merchant1 = Item.create!(name: "Snake Pants", description: "It is just a sock.", unit_price: 400, merchant_id: merchant1.id)
item1_merchant2 = Item.create!(name: "Stinky Bits", description: "Nondescript floaty chunks.", unit_price: 200, merchant_id: merchant2.id)
item1_merchant3 = Item.create!(name: "Fur Ball", description: "Ew pretty nasty!", unit_price: 1000, merchant_id: merchant3.id)
item1_merchant4 = Item.create!(name: "Milkbone", description: "Is it dairy or is it meat?", unit_price: 50, merchant_id: merchant4.id)
item1_merchant5 = Item.create!(name: "Library Card", description: "This should be free", unit_price: 5000, merchant_id: merchant5.id)
item1_merchant6 = Item.create!(name: "Big Earmuffs", description: "You could wear one like a hat", unit_price: 1000, merchant_id: merchant6.id)

invoice1 = Invoice.create!(customer_id: customer.id, status: 2)
invoiceitem1_item1_invoice1 = InvoiceItem.create!(item_id: item1_merchant1.id, invoice_id: invoice1.id, quantity: 10, unit_price: 1, status: 0) # 10 revenue for merchant 1
invoiceitem2_item1_invoice1 = InvoiceItem.create!(item_id: item1_merchant1.id, invoice_id: invoice1.id, quantity: 10, unit_price: 1, status: 0) # 10 revenue for merchant 1
invoiceitem3_item1_invoice1 = InvoiceItem.create!(item_id: item1_merchant1.id, invoice_id: invoice1.id, quantity: 10, unit_price: 1, status: 0) # 10 revenue for merchant 1

transaction1 = Transaction.create!(invoice_id: invoice1.id, credit_card_number: 2222_3333_4444_5555, credit_card_expiration_date: "05-19-1992", result: 1) # successful transaction
# 30 revenue for merchant 1

invoice2 = Invoice.create!(customer_id: customer.id, status: 2)
invoiceitem1_item1_invoice2 = InvoiceItem.create!(item_id: item1_merchant1.id, invoice_id: invoice2.id, quantity: 100_000, unit_price: 1, status: 0) # 100_000 revenue for merchant 1 but it should not count

transaction2 = Transaction.create!(invoice_id: invoice2.id, credit_card_number: 2222_3333_4444_5555, credit_card_expiration_date: "05-19-1992", result: 0) #failed transaction

invoice3 = Invoice.create!(customer_id: customer.id, status: 2)
invoiceitem1_item1_invoice3 = InvoiceItem.create!(item_id: item1_merchant2.id, invoice_id: invoice3.id, quantity: 10, unit_price: 1, status: 0) # 10 revenue for merchant 2
invoiceitem2_item1_invoice3 = InvoiceItem.create!(item_id: item1_merchant2.id, invoice_id: invoice3.id, quantity: 10, unit_price: 1, status: 0) # 10 revenue for merchant 2

transaction3 = Transaction.create!(invoice_id: invoice3.id, credit_card_number: 2222_3333_4444_5555, credit_card_expiration_date: "05-19-1992", result: 1) # successful transaction


invoice4 = Invoice.create!(customer_id: customer.id, status: 2)
invoiceitem1_item1_invoice4 = InvoiceItem.create!(item_id: item1_merchant3.id, invoice_id: invoice4.id, quantity: 50, unit_price: 1, status: 0) # 50 revenue for merchant 3

transaction4 = Transaction.create!(invoice_id: invoice4.id, credit_card_number: 2222_3333_4444_5555, credit_card_expiration_date: "05-19-1992", result: 1) # successful transaction

invoice5 = Invoice.create!(customer_id: customer.id, status: 2)
invoiceitem1_item1_invoice5 = InvoiceItem.create!(item_id: item1_merchant4.id, invoice_id: invoice5.id, quantity: 3, unit_price: 1, status: 0) # 3 revenue for merchant 4
invoiceitem2_item1_invoice5 = InvoiceItem.create!(item_id: item1_merchant4.id, invoice_id: invoice5.id, quantity: 2, unit_price: 1, status: 0) # 2 revenue for merchant 4

transaction5 = Transaction.create!(invoice_id: invoice5.id, credit_card_number: 2222_3333_4444_5555, credit_card_expiration_date: "05-19-1992", result: 1) # successful transaction

invoice6 = Invoice.create!(customer_id: customer.id, status: 2)
invoiceitem1_item1_invoice6 = InvoiceItem.create!(item_id: item1_merchant5.id, invoice_id: invoice6.id, quantity: 555_555, unit_price: 1, status: 0) # 555_555 revenue for merchant 5 but should fail

transaction6 = Transaction.create!(invoice_id: invoice6.id, credit_card_number: 2222_3333_4444_5555, credit_card_expiration_date: "05-19-1992", result: 0) # failed transaction

invoice7 = Invoice.create!(customer_id: customer.id, status: 2)
invoiceitem1_item1_invoice7 = InvoiceItem.create!(item_id: item1_merchant6.id, invoice_id: invoice7.id, quantity: 100, unit_price: 100, status: 0) # 100 revenue for merchant 6
invoiceitem2_item1_invoice7 = InvoiceItem.create!(item_id: item1_merchant6.id, invoice_id: invoice7.id, quantity: 100, unit_price: 100, status: 0) # 100 revenue for merchant 6

transaction7 = Transaction.create!(invoice_id: invoice7.id, credit_card_number: 1111_2222_3333_4444, credit_card_expiration_date: "12-12-1930", result: 1) # successful transaction

visit admin_merchants_path

within "#top-merchants" do
expect(merchant6.name).to appear_before(merchant3.name)
expect(merchant3.name).to appear_before(merchant1.name)
expect(merchant1.name).to appear_before(merchant2.name)
expect(merchant2.name).to appear_before(merchant4.name)
expect(page).to have_content(merchant4.name)
expect(page).to_not have_content(merchant5.name)
end
end
end

# As an admin,
# When I click on the name of a merchant from the admin merchants index page,
# Then I am taken to that merchant's admin show page (/admin/merchants/merchant_id)
# And I see the name of that merchant
# When I visit the admin merchants index
# Then I see the names of the top 5 merchants by total revenue generated
# And I see that each merchant name links to the admin merchant show page for that merchant
# And I see the total revenue generated next to each merchant name

# Notes on Revenue Calculation:

# Only invoices with at least one successful transaction should count towards revenue
# Revenue for an invoice should be calculated as the sum of the revenue of all invoice items
# Revenue for an invoice item should be calculated as the invoice item unit price multiplied by the quantity (do not use the item unit price)

0 comments on commit 7e8a120

Please sign in to comment.