diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 9449f6f91e..90f52194da 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -382,4 +382,13 @@ html, body{ padding: 10px; padding-bottom: 20px; margin-bottom: 10px; +} + +.holiday-discount { + background-color: #bbedd0; + border-radius: 10px; + border: 1px solid black; + padding: 10px; + padding-bottom: 20px; + margin-bottom: 10px; } \ No newline at end of file diff --git a/app/controllers/holiday_discounts_controller.rb b/app/controllers/holiday_discounts_controller.rb new file mode 100644 index 0000000000..d02a7815eb --- /dev/null +++ b/app/controllers/holiday_discounts_controller.rb @@ -0,0 +1,25 @@ +class HolidayDiscountsController < ApplicationController + + def new + @holiday_name = params[:holiday_name] + @merchant = Merchant.find(params[:merchant_id]) + end + + def create + if holiday_discount_params[:percent_discount] == "" || holiday_discount_params[:quantity_threshold] == "" + flash[:notice] = "Error - please complete all fields" + + redirect_to new_merchant_holiday_discount_path(holiday_discount_params[:merchant_id]) + else + HolidayDiscount.create(percent_discount: holiday_discount_params[:percent_discount], quantity_threshold: holiday_discount_params[:quantity_threshold], holiday_name: holiday_discount_params[:holiday_name],merchant_id: holiday_discount_params[:merchant_id]) + + redirect_to merchant_bulk_discounts_path(holiday_discount_params[:merchant_id]) + end + end + + private + def holiday_discount_params + params.permit(:merchant_id, :percent_discount, :quantity_threshold, :holiday_name) + end + +end \ No newline at end of file diff --git a/app/models/holiday_discount.rb b/app/models/holiday_discount.rb new file mode 100644 index 0000000000..14923757ea --- /dev/null +++ b/app/models/holiday_discount.rb @@ -0,0 +1,7 @@ +class HolidayDiscount < ApplicationRecord + + belongs_to :merchant + has_many :items, through: :merchant + has_many :invoice_items, through: :items + +end \ No newline at end of file diff --git a/app/models/merchant.rb b/app/models/merchant.rb index fdd482b101..938cddf2a6 100644 --- a/app/models/merchant.rb +++ b/app/models/merchant.rb @@ -2,6 +2,7 @@ class Merchant < ApplicationRecord enum status: { Disabled: 0, Enabled: 1 } has_many :bulk_discounts + has_many :holiday_discounts has_many :items has_many :invoice_items, through: :items has_many :invoices, through: :invoice_items diff --git a/app/views/bulk_discounts/index.html.erb b/app/views/bulk_discounts/index.html.erb index c67a64e040..56157cb2fe 100644 --- a/app/views/bulk_discounts/index.html.erb +++ b/app/views/bulk_discounts/index.html.erb @@ -25,6 +25,9 @@ <% @merchant.bulk_discounts.each do |discount| %>
+

+ Bulk Discount +

Percent Discount: <%=discount.percent_discount%>%

@@ -38,6 +41,27 @@
<% end %>
+ +
+ <% @merchant.holiday_discounts.each do |discount|%> +
+
+

+ Holiday Discount +

+

+ <%=discount.holiday_name.titleize%> Discount +

+

+ Percent Discount: <%=discount.percent_discount%>% +

+

+ Quantity Threshold: <%=discount.quantity_threshold%> +

+
+
+ <% end %> +
@@ -50,12 +74,14 @@ Order Name Date + Discount <%@holiday_search.holiday_information.each_with_index do |holiday,index|%> <%=index+1%> <%=holiday.name%> <%=holiday.date%> + <%=link_to "New Discount",new_merchant_holiday_discount_path(@merchant, :holiday_name => holiday.name)%> <% end %> diff --git a/app/views/holiday_discounts/new.html.erb b/app/views/holiday_discounts/new.html.erb new file mode 100644 index 0000000000..4841c03fe9 --- /dev/null +++ b/app/views/holiday_discounts/new.html.erb @@ -0,0 +1,12 @@ +

New Holiday Discount Page

+ +Discount Name: <%=@holiday_name%> Discount + +<%= form_with url: "/merchants/#{@merchant.id}/holiday_discounts",method: :post, class: "holiday_discount_form" do |form|%> + <%=form.label :percent_discount, "Percent Discount" %> + <%=form.number_field :percent_discount, value: 30%> + <%=form.label :quantity_threshold, "Quantity Threshold" %> + <%=form.number_field :quantity_threshold, value: 2%> + <%=form.hidden_field :holiday_name, value: @holiday_name%> + <%=form.submit "Submit" %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 1739256b52..384ea210de 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,6 +22,7 @@ resources :invoices, controller: 'merchant_invoices', only: %i[index show update] resources :items, controller: 'merchant_items', only: %i[index edit show update new create] resources :bulk_discounts, controller: 'bulk_discounts', only: %i[index show new create edit update destroy] + resources :holiday_discounts, controller: 'holiday_discounts' end resources :admin, only: [:index] diff --git a/db/migrate/20220810015919_create_holiday_discounts.rb b/db/migrate/20220810015919_create_holiday_discounts.rb new file mode 100644 index 0000000000..d5430b4d3f --- /dev/null +++ b/db/migrate/20220810015919_create_holiday_discounts.rb @@ -0,0 +1,12 @@ +class CreateHolidayDiscounts < ActiveRecord::Migration[5.2] + def change + create_table :holiday_discounts do |t| + t.string :holiday_name + t.integer :percent_discount, default: 30 + t.integer :quantity_threshold, default: 2 + t.references :merchant, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index fb4d67d38b..23ff19f9a1 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.define(version: 2022_08_05_012510) do +ActiveRecord::Schema.define(version: 2022_08_10_015919) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,6 +29,16 @@ t.datetime "updated_at" end + create_table "holiday_discounts", force: :cascade do |t| + t.string "holiday_name" + t.integer "percent_discount", default: 30 + t.integer "quantity_threshold", default: 2 + t.bigint "merchant_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["merchant_id"], name: "index_holiday_discounts_on_merchant_id" + end + create_table "invoice_items", force: :cascade do |t| t.integer "item_id" t.integer "invoice_id" @@ -78,5 +88,6 @@ end add_foreign_key "bulk_discounts", "merchants" + add_foreign_key "holiday_discounts", "merchants" add_foreign_key "items", "merchants" end diff --git a/spec/features/admin/merchants/index_spec.rb b/spec/features/admin/merchants/index_spec.rb index d487c0198e..1e28e874a6 100644 --- a/spec/features/admin/merchants/index_spec.rb +++ b/spec/features/admin/merchants/index_spec.rb @@ -149,7 +149,6 @@ expect(page).to have_content('Trader Joes') expect(page).to have_content('Disabled') expect(page).to have_button('Enable') - click_on('Enable') end expect(current_path).to eq(admin_merchants_path) diff --git a/spec/features/merchants/bulk_discounts/index_spec.rb b/spec/features/merchants/bulk_discounts/index_spec.rb index c3cdfc10e8..eee4a353f2 100644 --- a/spec/features/merchants/bulk_discounts/index_spec.rb +++ b/spec/features/merchants/bulk_discounts/index_spec.rb @@ -112,4 +112,20 @@ expect(page).to have_content("2022-11-11") end end + + it "has a 'create discount' button next to each upcoming holiday" do + merchant1 = Merchant.create!(name: "Snake Shop") + + bulk_discount1 = BulkDiscount.create!(percent_discount: 10, quantity_threshold: 5, merchant_id: merchant1.id) + bulk_discount2 = BulkDiscount.create!(percent_discount: 20, quantity_threshold: 10, merchant_id: merchant1.id) + bulk_discount3 = BulkDiscount.create!(percent_discount: 30, quantity_threshold: 15, merchant_id: merchant1.id) + + visit merchant_bulk_discounts_path(merchant1.id) + + within "#upcoming-holiday-1" do + click_on "New Discount" + end + + expect(current_path).to eq(new_merchant_holiday_discount_path(merchant1)) + end end diff --git a/spec/features/merchants/holiday_discounts/new_spec.rb b/spec/features/merchants/holiday_discounts/new_spec.rb new file mode 100644 index 0000000000..dd41070eef --- /dev/null +++ b/spec/features/merchants/holiday_discounts/new_spec.rb @@ -0,0 +1,46 @@ +require 'rails_helper' + +RSpec.describe 'merchant new holiday discount page' do + + it 'has pre-filled percent discount, quantity threshold, and holiday name fields, and reroutes to the discount index page where the new discount is shown' do + merchant1 = Merchant.create!(name: "Snake Shop") + + bulk_discount1 = BulkDiscount.create!(percent_discount: 10, quantity_threshold: 5, merchant_id: merchant1.id) + + visit merchant_bulk_discounts_path(merchant1.id) + + within "#upcoming-holiday-1" do + click_on "New Discount" + end + + expect(current_path).to eq(new_merchant_holiday_discount_path(merchant1)) + + expect(page).to have_content("Discount Name: Labor Day Discount") + expect(page).to have_field(:percent_discount, :with => 30) + expect(page).to have_field(:quantity_threshold, :with => 2) + + click_on "Submit" + + expect(current_path).to eq(merchant_bulk_discounts_path(merchant1.id)) + + within "#holiday-discount-list" do + expect(page).to have_content("Labor Day Discount") + expect(page).to have_content("Percent Discount: 30") + expect(page).to have_content("Quantity Threshold: 2") + end + end +end + +# Create a Holiday Discount + +# As a merchant, +# when I visit the discounts index page, +# In the Holiday Discounts section, I see a `create discount` button next to each of the 3 upcoming holidays. +# When I click on the button I am taken to a new discount form that has the form fields auto populated with the following: + +# Discount name: discount +# Percentage Discount: 30 +# Quantity Threshold: 2 + +# I can leave the information as is, or modify it before saving. +# I should be redirected to the discounts index page where I see the newly created discount added to the list of discounts. \ No newline at end of file diff --git a/spec/models/holiday_discount_spec.rb b/spec/models/holiday_discount_spec.rb new file mode 100644 index 0000000000..d031780b13 --- /dev/null +++ b/spec/models/holiday_discount_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +RSpec.describe HolidayDiscount do + describe 'relationships' do + it { should belong_to :merchant} + it { should have_many(:items).through(:merchant)} + it { should have_many(:invoice_items).through(:items)} + end +end \ No newline at end of file diff --git a/spec/models/merchant_spec.rb b/spec/models/merchant_spec.rb index 5603c7bfc5..351388ec5e 100644 --- a/spec/models/merchant_spec.rb +++ b/spec/models/merchant_spec.rb @@ -6,6 +6,7 @@ it { should have_many(:invoice_items).through(:items)} it { should have_many :bulk_discounts} + it { should have_many :holiday_discounts} end describe 'instance methods' do @@ -124,21 +125,6 @@ expect(actual).to eq(expected) end - it 'disabled_merchants returns all merchants with status 0 (Disabled)' do - merchant1 = Merchant.create!(name: 'Potions and Things', status: 'Enabled') - merchant2 = Merchant.create!(name: 'Wands and Wigs', status: 'Disabled') - merchant3 = Merchant.create!(name: 'Berties Beans', status: 'Disabled') - merchant4 = Merchant.create!(name: 'Butterbeer Superstore', status: 'Disabled') - - actual = Merchant.disabled_merchants.map do |merchant| - merchant.name - end - expected = [merchant2, merchant3, merchant4].map do |merchant| - merchant.name - end - - expect(actual).to eq(expected) - end it 'returns all associated enabled items' do merchant1 = Merchant.create!(name: "Snake Shop", status: 'Enabled')