Skip to content

Instantly share code, notes, and snippets.

@AndrewDryga
Last active December 1, 2018 17:47
Show Gist options
  • Save AndrewDryga/1744cf70289a0a58c41cdb26d67422fd to your computer and use it in GitHub Desktop.
Save AndrewDryga/1744cf70289a0a58c41cdb26d67422fd to your computer and use it in GitHub Desktop.
Sage: Make multiple bookings
def book_trip(attrs) do
with {:ok, exchange_rates} <- BillingAPI.fetch_currency_exchange_rates(attrs),
{:ok, card_authorization} <- BillingAPI.authorize_card(exchange_rates, attrs),
{:booking, {:ok, hotel_booking}, _} <- {:booking, HotelsBookingAPI.book_hotel(attrs), []},
{:booking, {:ok, car_booking}, _} <- {:booking, CarsBookingAPI.book_hotel(attrs), [hotel_booking]},
{:booking, {:ok, flight_booking}, _} <- {:booking, FlightsBookingAPI.book_hotel(attrs), [hotel_booking, car_booking]},
:ok <- Mailer.send_email_confirmation(card_authorization, hotel_booking, attrs),
{:charge, {:ok, charge}, _} <- {:charge, BillingAPI.charge_card(card_authorization), [...]} do
{:ok, %{charge: charge, bookings: [hotel_booking, car_booking, flight_booking]}}
else
{:charge, {:error, reason], bookings} ->
:ok = send_excuse_email(authorization)
:ok = cancel_bookings(bookings)
{:error, reason}
{:booking, {:error, reason}, bookings} ->
:ok = cancel_bookings(bookings)
{:error, reason}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment