Skip to content

Commit e5da1e6

Browse files
authored
Merge pull request #239 from ZeusWPI/quick_order
2 parents 3619031 + 26bf7c9 commit e5da1e6

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ See the `Makefile` for all commands.
4141
1. Install npm packages: `npm i`
4242
2. Initialize and migrate the db: `bundle exec rails db:migrate`
4343
3. Seed the db: `bundle exec rails db:seed`
44-
5. Start Tap: `./bin/dev`
44+
5. Start Tap: `./bin/dev` (or run `npm run build:js`, then `bundle exec rails s`)
45+
46+
#### Tests
47+
48+
Run `bundle exec rake`\
49+
or if that doesn't work try `bundle exec rspec`?
50+
51+
#### Linter (rubocop)
52+
- Run `bundle exec rubocop` to lint
53+
- To autocorrect most offences
54+
- safely : `bundle exec rubocop -a`
55+
- unsafely : `bundle exec rubocop -A`
4556
4657
## Deploy to production
4758

app/controllers/users_controller.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,51 @@ def order_dagschotel
101101
end
102102
end
103103

104+
# quickly order a single product
105+
# POST /users/{username}/quickorder
106+
# takes the product_id as payload
107+
def quick_order
108+
product = Product.find_by(id: params[:product_id])
109+
110+
unless product
111+
flash.now[:error] = "Product not found!"
112+
respond_to do |format|
113+
format.html { redirect_back_or_to(root_path) }
114+
format.json do
115+
render json: { message: "Quick order failed for #{@user.name}. Product not found." }, status: :bad_request
116+
end
117+
end
118+
return
119+
end
120+
121+
order = @user.orders.build
122+
order.order_items.build(count: 1, product: product)
123+
authorize! :create, order
124+
125+
if order.save
126+
flash[:success] = "#{product.name} has been ordered!"
127+
128+
flash[:successful_order_items] = order.order_items.map do |oi|
129+
{ product: oi.product, count: oi.count }
130+
end
131+
132+
respond_to do |format|
133+
format.html { redirect_back_or_to(root_path) }
134+
format.json { render json: { message: "Quick order succeeded for #{@user.name}." }, status: :ok }
135+
end
136+
else
137+
flash[:error] = order.valid? ? "Something went wrong! Please try again." : order.errors.full_messages.join(". ")
138+
respond_to do |format|
139+
format.html { redirect_back_or_to(root_path) }
140+
format.json do
141+
render json: {
142+
message: order.errors.full_messages.join(". ")
143+
}, status: :unprocessable_content
144+
end
145+
end
146+
end
147+
end
148+
104149
def reset_key
105150
@user.generate_key!
106151
redirect_to @user

app/models/user.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ def balance
107107
bal - total_pending
108108
end
109109

110+
def last_ordered_products(amount = 5)
111+
orders.includes(:products)
112+
.order(created_at: :desc)
113+
.limit(amount)
114+
.flat_map(&:products)
115+
.uniq
116+
.first(amount)
117+
end
118+
110119
# Static Users
111120

112121
def self.guest
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div class="columns is-multiline is-justify-content-space-between">
2+
<div class="column">
3+
<div class="card">
4+
<div class="card-content">
5+
<h1 class="title is-size-4">Quick orders</h1>
6+
<div style="display: flex; justify-content: space-evenly">
7+
<% products.each do |product| %>
8+
<%= button_to quick_user_path(@user),
9+
method: :post,
10+
params: { product_id: product.id },
11+
class: "button button-flex is-fullwidth is-primary is-light" do %>
12+
<%= image_tag product.avatar.url, title: product.name %>
13+
<% end %>
14+
<% end %>
15+
</div>
16+
</div>
17+
</div>
18+
</div>
19+
</div>

app/views/users/show.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
<div class="column is-12-mobile is-7-tablet is-8-desktop is-9-widescreen">
99
<!-- Statistics -->
1010
<%= render "stats_cards" %>
11+
12+
<!-- Quick orders -->
13+
<% recent_products = @user.last_ordered_products %>
14+
<% if recent_products.any? %>
15+
<%= render "quick_order", products: recent_products %>
16+
<% end %>
1117

1218
<!-- Orders -->
1319
<%= render "orders" %>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
# Legacy endpoints, required for Tappb
4343
get "quickpay" => "users#order_dagschotel"
4444

45+
post "quick" => "users#quick_order"
46+
4547
# Change the user's API key.
4648
post :reset_key
4749
end

spec/controllers/users_controller_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,47 @@
187187
# end
188188
# end
189189
end
190+
191+
# quick order
192+
describe "POST quick_order" do
193+
let(:product) { create(:product, stock: 20) }
194+
195+
before do
196+
# Mock the balance check (Tab API)
197+
balance = 12_345
198+
stub_request(:get, /.*/).to_return(status: 200, body: JSON.dump({ balance: balance }))
199+
end
200+
201+
context "with a valid product_id" do
202+
it "creates a new order" do
203+
expect do
204+
post :quick_order, params: { id: user.id, product_id: product.id }
205+
end.to change { user.reload.orders_count }.by(1)
206+
end
207+
208+
describe "the created order" do
209+
before do
210+
post :quick_order, params: { id: user.id, product_id: product.id }
211+
end
212+
213+
let(:order) { user.orders.last }
214+
215+
it "contains exactly 1 order item" do
216+
expect(order.order_items.size).to eq 1
217+
end
218+
219+
it "contains the correct product" do
220+
expect(order.order_items.first.product).to eq product
221+
end
222+
end
223+
end
224+
225+
context "with an invalid product_id" do
226+
it "does not create an order" do
227+
expect do
228+
post :quick_order, params: { id: user.id, product_id: 99_999 }
229+
end.not_to change(Order, :count)
230+
end
231+
end
232+
end
190233
end

0 commit comments

Comments
 (0)