Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return assets #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/sejfguru/assets/asset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ defmodule Sejfguru.Assets.Asset do
:name, :product_name, :state_name, :tag, :type_name, :used_by,
:values, :vendor_name])
end

def is_borrowed(asset) do
last_booking = asset.bookings |> Enum.sort_by(fn(x) -> x.id end) |> List.last()
last_booking != nil && last_booking.returned_at == nil
end

def is_borrowed_by_user(asset, user) do
last_booking = asset.bookings |> Enum.sort_by(fn(x) -> x.id end) |> List.last()
last_booking != nil && last_booking.returned_at == nil && last_booking.user.id == user.id
end

def is_not_borrowed(asset) do
last_booking = asset.bookings |> Enum.sort_by(fn(x) -> x.id end) |> List.last()
last_booking == nil || (last_booking != nil && last_booking.returned_at != nil)
end
end
2 changes: 1 addition & 1 deletion lib/sejfguru/bookings/booking.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Sejfguru.Bookings.Booking do
@doc false
def changeset(%Booking{} = booking, attrs) do
booking
|> cast(attrs, [:asset_id, :user_id])
|> cast(attrs, [:asset_id, :user_id, :returned_at])
|> validate_required([:asset_id, :user_id])
end
end
17 changes: 17 additions & 0 deletions lib/sejfguru/bookings/bookings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ defmodule Sejfguru.Bookings do
|> Repo.preload(:asset)
end

@doc """
Returns the list of bookings for given asset.

## Examples

iex> list_bookings_for_asset(asset)
[%Booking{}, ...]

"""
def list_bookings_for_asset(asset_id) do
Booking
|> order_by(:inserted_at)
|> where(asset_id: ^asset_id)
|> Repo.all
|> Repo.preload([:user, :asset])
end

@doc """
Gets a single booking.

Expand Down
40 changes: 34 additions & 6 deletions lib/sejfguru_web/controllers/booking_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@ defmodule SejfguruWeb.BookingController do
use SejfguruWeb, :controller

alias Sejfguru.Repo
alias Sejfguru.Assets
alias Sejfguru.Assets.Asset
alias Sejfguru.Bookings
alias Sejfguru.Bookings.Booking

def index(conn, %{ "asset_id" => asset_id }) do
asset = Sejfguru.Assets.get_asset!(String.to_integer(asset_id))
|> Repo.preload([bookings: :user])
render conn, "index.html", bookings: asset.bookings
bookings = Sejfguru.Bookings.list_bookings_for_asset(asset_id)
conn
|> assign(:bookings, bookings)
|> render("index.html")
end

def create(conn, %{ "asset_id" => asset_id }) do
booking = %Booking{ asset_id: String.to_integer(asset_id), user_id: conn.assigns[:current_user].id }
Repo.insert!(booking)
redirect conn, to: booking_path(conn, :index, asset_id)
asset = Assets.get_asset!(asset_id) |> Repo.preload(:bookings)
with true <- Asset.is_not_borrowed(asset),
booking <- %Booking{asset_id: String.to_integer(asset_id), user_id: conn.assigns[:current_user].id},
{:ok, booking} <- Repo.insert(booking) do
conn
|> put_flash(:info, "Asset borrowed")
|> redirect(to: booking_path(conn, :index, asset_id))
else
false ->
conn
|> put_flash(:error, "Asset already borrowed")
|> redirect(to: asset_path(conn, :index))
end
end

def return(conn, %{"id" => booking_id}) do
booking = Bookings.get_booking!(booking_id) |> Repo.preload(asset: [bookings: :user])
with true <- Asset.is_borrowed_by_user(booking.asset, conn.assigns[:current_user]),
{:ok, booking} <- Bookings.update_booking(booking, %{returned_at: DateTime.utc_now()}) do
conn
|> put_flash(:info, "Asset returned")
|> redirect(to: booking_path(conn, :index, booking.asset_id))
else
false ->
conn
|> put_flash(:error, "Asset cannot be returned by you")
|> redirect(to: asset_path(conn, :index))
end
end

def my_booking(conn, %{}) do
Expand Down
1 change: 1 addition & 0 deletions lib/sejfguru_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ defmodule SejfguruWeb.Router do
get "/bookings/my_booking", BookingController, :my_booking
get "/bookings/:asset_id", BookingController, :index
post "/bookings", BookingController, :create
post "/bookings/:id/return", BookingController, :return
end
end
12 changes: 11 additions & 1 deletion lib/sejfguru_web/templates/asset/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<th scope="col">Location</th>
<th scope="col">Last User</th>
<th scope="col">Last Borrowed</th>
<th scope="col">Status</th>
<th scope="col" colspan="2">Actions</th>
</tr>
</thead>
Expand All @@ -22,8 +23,17 @@
<td><%= asset.location_name %></td>
<td><%= SejfguruWeb.AssetView.last_user(asset) %></td>
<td><%= SejfguruWeb.AssetView.last_borrowed_at(asset) %></td>
<td><%= SejfguruWeb.AssetView.asset_status(asset) %></td>
<td><%= link "Show history", to: booking_path(@conn, :index, asset.id) %></td>
<td><%= link "Borrow", to: booking_path(@conn, :create, asset_id: asset.id), method: :post %></td>
<td>
<%= if SejfguruWeb.AssetView.can_return(asset, @current_user) do %>
<%= link "Return", to: booking_path(@conn, :return, SejfguruWeb.AssetView.last_booking(asset).id), method: :post %>
<% else %>
<%= if SejfguruWeb.AssetView.can_borrow(asset) do %>
<%= link "Borrow", to: booking_path(@conn, :create, asset_id: asset.id), method: :post %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 2 additions & 0 deletions lib/sejfguru_web/templates/booking/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">Asset name</th>
<th scope="col">User</th>
<th scope="col">Borrowed at</th>
<th scope="col">Returned at</th>
Expand All @@ -10,6 +11,7 @@
<tbody>
<%= for booking <- @bookings do %>
<tr>
<td><%= booking.asset.name %></td>
<td><%= "#{booking.user.first_name} #{booking.user.last_name}" %></td>
<td><%= booking.inserted_at %></td>
<td><%= booking.returned_at %></td>
Expand Down
22 changes: 20 additions & 2 deletions lib/sejfguru_web/views/asset_view.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
defmodule SejfguruWeb.AssetView do
use SejfguruWeb, :view
alias Sejfguru.Assets.Asset

def last_booking(asset) do
asset.bookings |> Enum.sort_by(fn(x) -> x.id end) |> List.last()
end

def last_user(asset) do
booking = List.last(asset.bookings)
booking = last_booking(asset)
if booking do
"#{booking.user.first_name} #{booking.user.last_name}"
else
Expand All @@ -11,12 +16,25 @@ defmodule SejfguruWeb.AssetView do
end

def last_borrowed_at(asset) do
booking = List.last(asset.bookings)
booking = last_booking(asset)
if booking do
datetime = booking.inserted_at
"#{datetime.day}/#{datetime.month}/#{datetime.year} #{datetime.hour}:#{datetime.minute}"
else
"N/A"
end
end

def asset_status(asset) do
booking = last_booking(asset)
if booking != nil && booking.returned_at == nil, do: "borrowed", else: "available"
end

def can_return(asset, user) do
Asset.is_borrowed_by_user(asset, user)
end

def can_borrow(asset) do
Asset.is_not_borrowed(asset)
end
end