Skip to content

Commit

Permalink
Merge pull request solidusio#6033 from MadelineCollier/admin-user-sto…
Browse files Browse the repository at this point in the history
…re-credit-refactor

[Admin][Users] Add new admin store credits edit_memo flow
  • Loading branch information
MadelineCollier authored Dec 13, 2024
2 parents dd5bbc4 + 72d357a commit 596adae
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :memo) %>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<%= 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) %>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
title: Edit Store Credit Memo
cancel: Cancel
submit: Update Store Credit
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,29 @@
) %>
<% end %>

<% if @store_credit.editable? || @store_credit.invalidateable? %>
<% panel.with_section do %>
<div class="w-[100%] text-right">
<% 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 %>
</div>
<% end %>
<% panel.with_section do %>
<div class="w-[100%] text-right">
<% 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 %>
</div>
<% end %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def tabs
def turbo_frames
%w[
edit_amount_modal
edit_memo_modal
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 38 additions & 5 deletions admin/app/controllers/solidus_admin/store_credits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: '<turbo-stream action="refresh" />'
end
end
end

Expand All @@ -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|
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions admin/config/locales/store_credits.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
2 changes: 2 additions & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
member do
get :edit_amount
put :update_amount
get :edit_memo
put :update_memo
end
end
end
Expand Down
157 changes: 157 additions & 0 deletions admin/spec/features/store_credits_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# frozen_string_literal: true

require "spec_helper"

describe "StoreCredits", :js, type: :feature do
let(:admin) { create(:admin_user, email: "[email protected]") }

before do
sign_in admin
end

context "when a user has no store credits" do
before do
create(:user, email: "[email protected]")
visit "/admin/users"
find_row("[email protected]").click
click_on "Store Credit"
end

it "shows the store credits page" do
expect(page).to have_content("Users / [email protected] / Store Credit")
expect(page).to have_content("Lifetime Stats")
expect(page).to have_content("Store Credit")
expect(page).to be_axe_clean
end

it "shows the appropriate content" do
expect(page).to have_content("No Store Credits found.")
end
end

context "when a user has store credits" do
let!(:store_credit) { create(:store_credit, amount: 199.00, currency: "USD") }
let!(:store_credit_reason) { create(:store_credit_reason, name: "credit given in error") }

before do
store_credit.user.update(email: "[email protected]")

visit "/admin/users"
find_row("[email protected]").click
click_on "Store Credit"
end

it "shows the store credits page" do
expect(page).to have_content("Users / [email protected] / Store Credit")
expect(page).to have_content("Lifetime Stats")
expect(page).to have_content("Store Credit")
expect(page).to be_axe_clean
end

it "lists the user's store credit" do
expect(page).to have_content("Current balance: $199.00")
expect(page).to have_content("Credited")
expect(page).to have_content("Authorized")
expect(page).to have_content("Used")
expect(page).to have_content("Type")
expect(page).to have_content("Created by")
expect(page).to have_content("Issued on")
expect(page).to have_content("Invalidated")
expect(page).not_to have_content("No Store Credits found.")
end

context "when clicking through to a single store credit" do
let!(:store_credit_reason) { create(:store_credit_reason, name: "credit given in error") }

before do
stub_authorization!(admin)
find_row("$199.00").click
end

it "shows individual store credit details" do
expect(page).to have_content("Users / [email protected] / Store Credit / $199.00")
expect(page).to have_content("Store Credit History")
expect(page).to have_content("Action")
expect(page).to have_content("Added")
end

it "allows invalidating of the store credit" do
click_on "Invalidate"
select "credit given in error", from: "store_credit_reason_id"
click_on "Invalidate"
expect(page).to have_content("Store Credit History")
expect(page).to have_content("Action")
expect(page).to have_content("Added")
expect(page).to have_content("Invalidated")
expect(page).to have_content("Reason for updating")
expect(page).to have_content("credit given in error")
end

context "when editing the store credit amount" do
context "with invalid amount" do
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 Amount")

within("dialog") do
fill_in "Amount", with: ""
click_on "Update Store Credit"
expect(page).to have_content("must be greater than 0")
click_on "Cancel"
end
end
end

context "without a valid reason" do
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 Amount")

within("dialog") do
fill_in "Amount", with: "100"
click_on "Update Store Credit"
expect(page).to have_content("Store Credit reason must be provided")
click_on "Cancel"
end
end
end

context "with valid params" do
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 Amount")

within("dialog") do
fill_in "Amount", with: "666"
select "credit given in error", from: "store_credit[store_credit_reason_id]"
click_on "Update Store Credit"
end

expect(page).to have_content("Users / [email protected] / Store Credit / $666.00")
expect(page).to have_content("Adjustment")
expect(page).to have_content("credit given in error")
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
Loading

0 comments on commit 596adae

Please sign in to comment.