Skip to content

Commit

Permalink
Display shipment items on shipment page
Browse files Browse the repository at this point in the history
  • Loading branch information
marlena-b committed Oct 21, 2024
1 parent 07321e4 commit 6af968f
Show file tree
Hide file tree
Showing 19 changed files with 443 additions and 11 deletions.
9 changes: 8 additions & 1 deletion rails_application/app/controllers/shipments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ class ShipmentsController < ApplicationController
def index
@shipments =
Shipments::Shipment
.joins(:order)
.includes(:order)
.order("id DESC")
.with_full_address
.order(id: :desc)
.page(params[:page])
.per(10)
end

def show
@shipment = Shipments::Shipment.find(params[:id])
@shipment_items = @shipment.shipment_items.page(params[:page]).per(25)
end
end
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
12 changes: 12 additions & 0 deletions rails_application/app/read_models/shipments/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Shipment < ApplicationRecord
foreign_key: :uid,
primary_key: :order_uid

has_many :shipment_items

scope :with_full_address, -> { where.not(address_line_1: nil, address_line_2: nil, address_line_3: nil, address_line_4: nil) }

def full_address
[self.address_line_1, self.address_line_2, self.address_line_3, self.address_line_4].join(" ")
end
Expand All @@ -16,10 +20,18 @@ class Order < ApplicationRecord
self.table_name = "shipments_orders"
end

class ShipmentItem < ApplicationRecord
self.table_name = "shipment_items"

belongs_to :shipment
end

class Configuration
def call(event_store)
event_store.subscribe(SetShippingAddress, to: [Shipping::ShippingAddressAddedToShipment])
event_store.subscribe(MarkOrderPlaced, to: [Ordering::OrderPlaced])
event_store.subscribe(AddItemToShipment, to: [Shipping::ItemAddedToShipmentPickingList])
event_store.subscribe(RemoveItemFromShipment, to: [Shipping::ItemRemovedFromShipmentPickingList])
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
4 changes: 2 additions & 2 deletions rails_application/app/views/orders/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
<dd class="mb-2" id="<%= "orders_order_#{@order.uid}_state" %>"><%= @order.state %></dd>
<dt class="font-bold">Shipping Details</dt>
<dd class="mb-2">
<% unless @shipment %>
<% unless @shipment&.full_address.present? %>
Shipping address is missing.
<% end %>
</dd>
<dd class="mb-2">
<% unless @shipment %>
<% unless @shipment&.full_address.present? %>
<%= link_to "Add shipment address",
edit_order_shipping_address_path(@order.uid),
class: 'px-2 py-1 border rounded-md shadow-sm text-xs font-medium focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 border-transparent text-white bg-blue-600 hover:bg-blue-700'
Expand Down
11 changes: 6 additions & 5 deletions rails_application/app/views/shipments/index.html.erb
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>
30 changes: 30 additions & 0 deletions rails_application/app/views/shipments/show.html.erb
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>
2 changes: 1 addition & 1 deletion rails_application/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
resource :invoice, only: [:create]
end

resources :shipments, only: [:index]
resources :shipments, only: [:index, :show]

resources :events_catalog, only: [:index]

Expand Down
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
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
14 changes: 12 additions & 2 deletions rails_application/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_08_27_090619) do
ActiveRecord::Schema[7.2].define(version: 2024_10_18_113912) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -194,8 +194,18 @@
t.decimal "lowest_recent_price", precision: 8, scale: 2
end

create_table "shipment_items", force: :cascade do |t|
t.bigint "shipment_id", null: false
t.string "product_name", null: false
t.integer "quantity", null: false
t.uuid "product_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["shipment_id"], name: "index_shipment_items_on_shipment_id"
end

create_table "shipments", force: :cascade do |t|
t.string "order_uid", null: false
t.uuid "order_uid", null: false
t.string "address_line_1"
t.string "address_line_2"
t.string "address_line_3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def test_remove_item_when_quantity_eq_1
product_id: product_id
)
)
run_command(
ProductCatalog::NameProduct.new(
product_id: product_id,
name: "Async Remote"
)
)
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
customer_id = SecureRandom.uuid
run_command(
Expand Down
63 changes: 63 additions & 0 deletions rails_application/test/integration/shipments_test.rb
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
24 changes: 24 additions & 0 deletions rails_application/test/orders/broadcast_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def test_broadcast_add_item_to_basket
product_id: product_id
)
)
run_command(
ProductCatalog::NameProduct.new(
product_id: product_id,
name: "Async Remote"
)
)
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))

in_memory_broadcast.result.clear
Expand Down Expand Up @@ -63,6 +69,12 @@ def test_broadcast_remove_item_from_basket
product_id: product_id
)
)
run_command(
ProductCatalog::NameProduct.new(
product_id: product_id,
name: "Async Remote"
)
)
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
order_id = SecureRandom.uuid

Expand Down Expand Up @@ -104,6 +116,12 @@ def test_broadcast_update_order_value
product_id: product_id
)
)
run_command(
ProductCatalog::NameProduct.new(
product_id: product_id,
name: "Async Remote"
)
)
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
event_store.publish(
Pricing::PriceItemAdded.new(
Expand Down Expand Up @@ -164,6 +182,12 @@ def test_broadcast_update_discount
product_id: product_id
)
)
run_command(
ProductCatalog::NameProduct.new(
product_id: product_id,
name: "Async Remote"
)
)
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
event_store.publish(
Pricing::PriceItemAdded.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def test_remove_item_when_quantity_eq_1
product_id: product_id
)
)
run_command(
ProductCatalog::NameProduct.new(
product_id: product_id,
name: "Async Remote"
)
)
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
customer_id = SecureRandom.uuid
run_command(
Expand Down
6 changes: 6 additions & 0 deletions rails_application/test/orders/order_expired_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def test_expire_created_order
product_id: product_id
)
)
run_command(
ProductCatalog::NameProduct.new(
product_id: product_id,
name: "Async Remote"
)
)
run_command(Pricing::SetPrice.new(product_id: product_id, price: 39))

order_id = SecureRandom.uuid
Expand Down
Loading

0 comments on commit 6af968f

Please sign in to comment.