-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display shipment items on shipment page
- Loading branch information
Showing
19 changed files
with
442 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
rails_application/app/read_models/shipments/add_item_to_shipment.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Shipments | ||
class AddItemToShipment | ||
def call(event) | ||
product_id = event.data.fetch(:product_id) | ||
order_id = event.data.fetch(:order_id) | ||
product = Orders::Product.find_by_uid!(product_id) | ||
|
||
item = find_or_create_item(order_id, product) | ||
item.quantity += 1 | ||
item.save! | ||
end | ||
|
||
private | ||
|
||
def find_or_create_item(order_id, product) | ||
Shipment | ||
.find_or_create_by!(order_uid: order_id) | ||
.shipment_items | ||
.create_with(product_name: product.name, quantity: 0) | ||
.find_or_create_by!(product_id: product.uid) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
rails_application/app/read_models/shipments/remove_item_from_shipment.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Shipments | ||
class RemoveItemFromShipment | ||
def call(event) | ||
product_id = event.data.fetch(:product_id) | ||
order_id = event.data.fetch(:order_id) | ||
|
||
item = find(order_id, product_id) | ||
item.quantity -= 1 | ||
item.quantity > 0 ? item.save! : item.destroy! | ||
end | ||
|
||
private | ||
|
||
def find(order_uid, product_id) | ||
Shipment | ||
.find_by!(order_uid: order_uid) | ||
.shipment_items | ||
.find_by!(product_id: product_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
<% content_for(:header) do %> | ||
Shipments | ||
<% end %> | ||
|
||
<table class="w-full"> | ||
<thead> | ||
<tr> | ||
<th class="text-left py-2">Order Number</th> | ||
<th class="text-left py-2">Address</th> | ||
<th class="text-left py-2"></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @shipments.each do |shipment| %> | ||
<tr class="border-t"> | ||
<td class="py-2"><%= link_to shipment.order.number, order_path(shipment.order.uid), class: "text-blue-500 hover:underline" %></td> | ||
<td class="py-2"><%= shipment.full_address %></td> | ||
<td class="py-2"><%= link_to "Show Shipment", shipment_path(shipment), class: "text-blue-500 hover:underline" %> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
|
||
|
||
<div class="mt-8 hidden sm:flex-1 sm:flex sm:items-center sm:justify-between"> | ||
<div> | ||
<%= page_entries_info @shipments %> | ||
</div> | ||
<div> | ||
<%= paginate @shipments %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<% content_for(:header) do %> | ||
Shipment | ||
<% end %> | ||
|
||
<dl class="mb-8"> | ||
<dt class="font-bold">Order Number</dt> | ||
<dd class="mb-2"><%= link_to @shipment.order.number, order_path(@shipment.order.uid), class: "text-blue-500 hover:underline" %></dd> | ||
<dt class="font-bold">Address</dt> | ||
<dd class="mb-2"> | ||
<%= @shipment&.full_address %> | ||
</dd> | ||
</dl> | ||
|
||
<table class="w-full"> | ||
<thead> | ||
<tr> | ||
<th class="text-left py-2">Product Name</th> | ||
<th class="text-left py-2">Quantity</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @shipment_items.each do |item| %> | ||
<tr class="border-t"> | ||
<td class="py-2"><%= item.product_name %></td> | ||
<td class="py-2"><%= item.quantity %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
rails_application/db/migrate/20241017115056_create_shipment_items.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateShipmentItems < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :shipment_items do |t| | ||
t.references :shipment, null: false | ||
t.string :product_name, null: false | ||
t.integer :quantity, null: false | ||
t.uuid :product_id, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
rails_application/db/migrate/20241018113912_change_order_uid_to_uuid_in_shipments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class ChangeOrderUidToUuidInShipments < ActiveRecord::Migration[7.2] | ||
def up | ||
change_column :shipments, :order_uid, 'uuid USING order_uid::uuid', null: false | ||
end | ||
|
||
def down | ||
change_column :shipments, :order_uid, 'varchar USING order_uid::varchar', null: false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
require "test_helper" | ||
|
||
class ShipmentsTest < InMemoryRESIntegrationTestCase | ||
def setup | ||
super | ||
add_available_vat_rate(10) | ||
end | ||
|
||
def test_list_shipments | ||
shopify_id = register_customer("Shopify") | ||
order_id = SecureRandom.uuid | ||
async_remote_id = register_product("Async Remote", 39, 10) | ||
|
||
add_product_to_basket(order_id, async_remote_id) | ||
put "/orders/#{order_id}/shipping_address", | ||
params: { | ||
"shipments_shipment" => { | ||
address_line_1: "123 Main Street", | ||
address_line_2: "Apt 1", | ||
address_line_3: "San Francisco", | ||
address_line_4: "US" | ||
} | ||
} | ||
submit_order(shopify_id, order_id) | ||
|
||
order = Orders::Order.find_by(uid: order_id) | ||
|
||
get "/shipments" | ||
|
||
assert_response :success | ||
assert_select("td", order.number) | ||
assert_select("td", "123 Main Street Apt 1 San Francisco US") | ||
end | ||
|
||
def test_shipment_page | ||
shopify_id = register_customer("Shopify") | ||
order_id = SecureRandom.uuid | ||
async_remote_id = register_product("Async Remote", 39, 10) | ||
|
||
add_product_to_basket(order_id, async_remote_id) | ||
put "/orders/#{order_id}/shipping_address", | ||
params: { | ||
"shipments_shipment" => { | ||
address_line_1: "123 Main Street", | ||
address_line_2: "Apt 1", | ||
address_line_3: "San Francisco", | ||
address_line_4: "US" | ||
} | ||
} | ||
submit_order(shopify_id, order_id) | ||
|
||
shipment = Shipments::Shipment.find_by(order_uid: order_id) | ||
order = Orders::Order.find_by(uid: order_id) | ||
|
||
get "/shipments/#{shipment.id}" | ||
assert_response :success | ||
assert_select("dd", order.number) | ||
assert_select("dd", "123 Main Street Apt 1 San Francisco US") | ||
assert_select("td", "Async Remote") | ||
assert_select("td", "1") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.