From 9639966e4a834eb38a2148c85ede6f4283fc5c7f Mon Sep 17 00:00:00 2001 From: Madeline Collier Date: Thu, 12 Dec 2024 10:29:15 +0100 Subject: [PATCH] Add new admin store credits edit_memo flow --- .../store_credits/edit_amount/component.yml | 2 +- .../edit_memo/component.html.erb | 16 ++++++ .../store_credits/edit_memo/component.rb | 13 +++++ .../store_credits/edit_memo/component.yml | 4 ++ .../store_credits/show/component.html.erb | 43 +++++++------- .../users/store_credits/show/component.rb | 1 + .../users/store_credits/show/component.yml | 3 +- .../solidus_admin/store_credits_controller.rb | 43 ++++++++++++-- admin/config/locales/store_credits.en.yml | 3 + admin/config/routes.rb | 2 + admin/spec/features/store_credits_spec.rb | 23 ++++++-- .../solidus_admin/store_credits_spec.rb | 56 +++++++++++++++++++ 12 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 admin/app/components/solidus_admin/users/store_credits/edit_memo/component.html.erb create mode 100644 admin/app/components/solidus_admin/users/store_credits/edit_memo/component.rb create mode 100644 admin/app/components/solidus_admin/users/store_credits/edit_memo/component.yml diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.yml b/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.yml index 3bfc972f77..c3e1d5ed25 100644 --- a/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.yml +++ b/admin/app/components/solidus_admin/users/store_credits/edit_amount/component.yml @@ -1,5 +1,5 @@ en: - title: Edit Store Credit + title: Edit Store Credit Amount cancel: Cancel submit: Update Store Credit choose_reason: Choose Reason For Changing Amount diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.html.erb new file mode 100644 index 0000000000..24a10a89c8 --- /dev/null +++ b/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.html.erb @@ -0,0 +1,16 @@ +<%= turbo_frame_tag :edit_memo_modal do %> + <%= render component("ui/modal").new(title: t(".title")) do |modal| %> + <%= form_for @store_credit, url: solidus_admin.update_memo_user_store_credit_path(@user, @store_credit), method: :put, html: { id: form_id } do |f| %> +
+ <%= render component("ui/forms/field").text_field(f, :memo) %> +
+ <% modal.with_actions do %> +
+ <%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> +
+ <%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %> + <% end %> + <% end %> + <% end %> +<% end %> +<%= render component("users/store_credits/show").new(user: @user, store_credit: @store_credit, events: @store_credit_events) %> diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.rb b/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.rb new file mode 100644 index 0000000000..6f37180002 --- /dev/null +++ b/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class SolidusAdmin::Users::StoreCredits::EditMemo::Component < SolidusAdmin::BaseComponent + def initialize(user:, store_credit:, events:) + @user = user + @store_credit = store_credit + @store_credit_events = events + end + + def form_id + dom_id(@store_credit, "#{stimulus_id}_edit_memo_form") + end +end diff --git a/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.yml b/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.yml new file mode 100644 index 0000000000..8aa036bb73 --- /dev/null +++ b/admin/app/components/solidus_admin/users/store_credits/edit_memo/component.yml @@ -0,0 +1,4 @@ +en: + title: Edit Store Credit Memo + cancel: Cancel + submit: Update Store Credit diff --git a/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb b/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb index 508b80ea31..7592c642dd 100644 --- a/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb +++ b/admin/app/components/solidus_admin/users/store_credits/show/component.html.erb @@ -24,26 +24,29 @@ ) %> <% end %> - <% if @store_credit.editable? || @store_credit.invalidateable? %> - <% panel.with_section do %> -
- <% if @store_credit.invalidateable? %> - <%= render component("ui/button").new( - scheme: :danger, - tag: :a, - text: t(".invalidate"), - href: spree.edit_validity_admin_user_store_credit_path(@user, @store_credit) - )%> - <% end %> - <% if @store_credit.editable? %> - <%= render component("ui/button").new( - "data-action": "click->#{stimulus_id}#actionButtonClicked", - "data-#{stimulus_id}-url-param": solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_amount_modal), - text: t(".edit"), - )%> - <% end %> -
- <% end %> + <% panel.with_section do %> +
+ <% if @store_credit.invalidateable? %> + <%= render component("ui/button").new( + scheme: :danger, + tag: :a, + text: t(".invalidate"), + href: spree.edit_validity_admin_user_store_credit_path(@user, @store_credit) + )%> + <% end %> + <%= render component("ui/button").new( + "data-action": "click->#{stimulus_id}#actionButtonClicked", + "data-#{stimulus_id}-url-param": solidus_admin.edit_memo_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_memo_modal), + text: t(".edit_memo"), + )%> + <% if @store_credit.editable? %> + <%= render component("ui/button").new( + "data-action": "click->#{stimulus_id}#actionButtonClicked", + "data-#{stimulus_id}-url-param": solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_amount_modal), + text: t(".edit_amount"), + )%> + <% end %> +
<% end %> <% end %> diff --git a/admin/app/components/solidus_admin/users/store_credits/show/component.rb b/admin/app/components/solidus_admin/users/store_credits/show/component.rb index bf1eca2c20..945979acb9 100644 --- a/admin/app/components/solidus_admin/users/store_credits/show/component.rb +++ b/admin/app/components/solidus_admin/users/store_credits/show/component.rb @@ -51,6 +51,7 @@ def tabs def turbo_frames %w[ edit_amount_modal + edit_memo_modal ] end diff --git a/admin/app/components/solidus_admin/users/store_credits/show/component.yml b/admin/app/components/solidus_admin/users/store_credits/show/component.yml index a94e5bb8c3..0d74d12f8e 100644 --- a/admin/app/components/solidus_admin/users/store_credits/show/component.yml +++ b/admin/app/components/solidus_admin/users/store_credits/show/component.yml @@ -7,7 +7,8 @@ en: store_credit: Store Credit last_active: Last Active add_store_credit: Add Store Credit - edit: Edit Amount + edit_amount: Edit Amount + edit_memo: Edit Memo invalidate: Invalidate store_credit_history: Store Credit History credited: Credited diff --git a/admin/app/controllers/solidus_admin/store_credits_controller.rb b/admin/app/controllers/solidus_admin/store_credits_controller.rb index 7d0297778c..44f6ccd0cf 100644 --- a/admin/app/controllers/solidus_admin/store_credits_controller.rb +++ b/admin/app/controllers/solidus_admin/store_credits_controller.rb @@ -3,7 +3,7 @@ module SolidusAdmin class StoreCreditsController < SolidusAdmin::BaseController before_action :set_user - before_action :set_store_credit, only: [:show, :edit_amount, :update_amount] + before_action :set_store_credit, only: [:show, :edit_amount, :update_amount, :edit_memo, :update_memo] before_action :set_store_credit_reasons, only: [:edit_amount, :update_amount] def index @@ -54,7 +54,40 @@ def update_amount end end else - render_edit_page_with_errors and return + render_edit_amount_with_errors and return + end + end + + def edit_memo + @store_credit_events = @store_credit.store_credit_events.chronological + + respond_to do |format| + format.html { + render component("users/store_credits/edit_memo").new( + user: @user, + store_credit: @store_credit, + events: @store_credit_events, + ) + } + end + end + + def update_memo + if @store_credit.update(memo: permitted_store_credit_params[:memo]) + flash[:notice] = t('.success') + else + # Memo update failures are nearly impossible to trigger due to lack of validation. + flash[:error] = t('.failure') + end + + respond_to do |format| + format.html do + redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other + end + + format.turbo_stream do + render turbo_stream: '' + end end end @@ -79,7 +112,7 @@ def permitted_store_credit_params params.require(:store_credit).permit(permitted_params).merge(created_by: spree_current_user) end - def render_edit_page_with_errors + def render_edit_amount_with_errors @store_credit_events = @store_credit.store_credit_events.chronological respond_to do |format| @@ -98,7 +131,7 @@ def render_edit_page_with_errors def ensure_amount if permitted_store_credit_params[:amount].blank? @store_credit.errors.add(:amount, :greater_than, count: 0, value: permitted_store_credit_params[:amount]) - render_edit_page_with_errors + render_edit_amount_with_errors return false end true @@ -109,7 +142,7 @@ def ensure_store_credit_reason if @store_credit_reason.blank? @store_credit.errors.add(:store_credit_reason_id, "Store Credit reason must be provided") - render_edit_page_with_errors + render_edit_amount_with_errors return false end true diff --git a/admin/config/locales/store_credits.en.yml b/admin/config/locales/store_credits.en.yml index e24c5cda30..694c81bf1b 100644 --- a/admin/config/locales/store_credits.en.yml +++ b/admin/config/locales/store_credits.en.yml @@ -8,3 +8,6 @@ en: success: "Store credit was successfully created." update_amount: success: "Store credit was successfully updated." + update_memo: + success: "Store credit was successfully updated." + failure: "Something went wrong. Store credit could not be updated." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 2116087386..6f6708c14a 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -57,6 +57,8 @@ member do get :edit_amount put :update_amount + get :edit_memo + put :update_memo end end end diff --git a/admin/spec/features/store_credits_spec.rb b/admin/spec/features/store_credits_spec.rb index 89515ff7be..3b67f02d25 100644 --- a/admin/spec/features/store_credits_spec.rb +++ b/admin/spec/features/store_credits_spec.rb @@ -92,7 +92,7 @@ it "shows the appropriate error message" do click_on "Edit Amount" expect(page).to have_selector("dialog", wait: 5) - expect(page).to have_content("Edit Store Credit") + expect(page).to have_content("Edit Store Credit Amount") within("dialog") do fill_in "Amount", with: "" @@ -107,7 +107,7 @@ it "shows the appropriate error message" do click_on "Edit Amount" expect(page).to have_selector("dialog", wait: 5) - expect(page).to have_content("Edit Store Credit") + expect(page).to have_content("Edit Store Credit Amount") within("dialog") do fill_in "Amount", with: "100" @@ -122,9 +122,8 @@ it "allows editing of the store credit amount" do click_on "Edit Amount" expect(page).to have_selector("dialog", wait: 5) - expect(page).to have_content("Edit Store Credit") + expect(page).to have_content("Edit Store Credit Amount") - # Invalid amount within("dialog") do fill_in "Amount", with: "666" select "credit given in error", from: "store_credit[store_credit_reason_id]" @@ -137,6 +136,22 @@ end end end + + context "when editing the store credit memo" do + it "allows editing of the store credit memo" do + click_on "Edit Memo" + expect(page).to have_selector("dialog", wait: 5) + expect(page).to have_content("Edit Store Credit Memo") + + within("dialog") do + fill_in "Memo", with: "dogtown" + click_on "Update Store Credit" + end + + expect(page).to have_content("Store credit was successfully updated.") + expect(page).to have_content("dogtown") + end + end end end end diff --git a/admin/spec/requests/solidus_admin/store_credits_spec.rb b/admin/spec/requests/solidus_admin/store_credits_spec.rb index e278be5db1..e5fbab8c5e 100644 --- a/admin/spec/requests/solidus_admin/store_credits_spec.rb +++ b/admin/spec/requests/solidus_admin/store_credits_spec.rb @@ -9,6 +9,7 @@ let!(:store_credit_event) { create(:store_credit_adjustment_event, store_credit:, amount_remaining: 50) } let(:valid_params) { { amount: 100, store_credit_reason_id: create(:store_credit_reason).id } } let(:invalid_params) { { amount: nil } } + let(:valid_memo_params) { { memo: "Updated memo text" } } before do allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user) @@ -78,6 +79,61 @@ end end + describe "GET /edit_memo" do + it "renders the edit_memo template with a 200 OK status" do + get solidus_admin.edit_memo_user_store_credit_path(user, store_credit) + expect(response).to have_http_status(:ok) + expect(response.body).to include(store_credit.memo.to_s) + end + end + + describe "PUT /update_memo" do + context "with valid parameters" do + it "updates the store credit memo" do + expect { + put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params } + }.to change { store_credit.reload.memo }.to("Updated memo text") + end + + it "redirects to the store credit show page with a 303 See Other status" do + put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params } + expect(response).to redirect_to(solidus_admin.user_store_credit_path(user, store_credit)) + expect(response).to have_http_status(:see_other) + end + + it "displays a success flash message" do + put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params } + follow_redirect! + expect(response.body).to include("Store credit was successfully updated.") + end + end + + context "when the database update fails" do + before do + # Memo update failures are nearly impossible to trigger due to lack of validation. + allow_any_instance_of(Spree::StoreCredit).to receive(:update).and_return(false) + end + + it "does not update the store credit memo" do + expect { + put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params } + }.not_to change { store_credit.reload.memo } + end + + it "redirects to the store credit show page with a 303 See Other status" do + put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params } + expect(response).to redirect_to(solidus_admin.user_store_credit_path(user, store_credit)) + expect(response).to have_http_status(:see_other) + end + + it "displays a failure flash message" do + put solidus_admin.update_memo_user_store_credit_path(user, store_credit), params: { store_credit: valid_memo_params } + follow_redirect! + expect(response.body).to include("Something went wrong. Store credit could not be updated.") + end + end + end + describe "private methods" do describe "#ensure_amount" do it "adds an error when the amount is blank" do