Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2d583cf
Refactors create action.
raptorf1 Jan 19, 2023
217ce51
Fixes failing contact_us controller spec
raptorf1 Jan 19, 2023
f3a8720
Refactors update action.
raptorf1 Jan 20, 2023
8786dd6
Adds equality in date check
raptorf1 Jan 20, 2023
97fdc2c
Merge branch 'conversations-controller-refactoring' into 'bookings-co…
raptorf1 Jan 20, 2023
bc3eb07
Merge branch 'reviews-controller-refactoring' into 'bookings-controll…
raptorf1 Jan 20, 2023
a4f54f1
Creates new namespace and endpoint for incoming booking stats.
raptorf1 Jan 24, 2023
74cf41f
Creates new endpoint for outgoing booking stats.
raptorf1 Jan 24, 2023
9c49266
Creates new endpoint to return host unavailable dates.
raptorf1 Jan 24, 2023
32be7b1
Creates new endpoint for fetching incoming requests.
raptorf1 Jan 24, 2023
6b43101
Creates new endpoint for incoming upcoming bookings.
raptorf1 Jan 24, 2023
188c3bb
Creates new endpoint for incoming history bookings.
raptorf1 Jan 24, 2023
226d8ad
Creates new endpoint and specs for outgoing requests
raptorf1 Feb 1, 2023
f40ddc6
Creates new endpoint and specs for outgoing upcoming
raptorf1 Feb 1, 2023
1aabd2a
Creates new endpoint and specs for outgoing history
raptorf1 Feb 1, 2023
ca63f5e
Bookings controller, routes and specs cleanup.
raptorf1 Feb 1, 2023
119db60
Merge branch 'development' into 'bookings-controller-refactoring'
raptorf1 Feb 1, 2023
32562cb
Returns no content when hosto profile does not exist in host_unavaila…
raptorf1 Feb 3, 2023
cf0174b
Returns no content when hosto profile does not exist in incoming_hist…
raptorf1 Feb 3, 2023
51d21be
Returns no content when host profile does not exist in incoming_reque…
raptorf1 Feb 3, 2023
7f5b03f
Returns no content when host profile does not exist in incoming_stats…
raptorf1 Feb 3, 2023
8115490
Returns no content when host profile does not exist in incoming_upcom…
raptorf1 Feb 3, 2023
2646689
Sorts requests.
raptorf1 Feb 4, 2023
fd7fbee
Sorts upcoming.
raptorf1 Feb 4, 2023
9343c85
Sorts history.
raptorf1 Feb 4, 2023
87fbd6c
Extracts logic of history in model method.
raptorf1 Feb 4, 2023
6fdfc3a
Refactors logic of history in model method.
raptorf1 Feb 4, 2023
62e5819
Extracts logic of requests in model method.
raptorf1 Feb 4, 2023
448b192
Extracts logic of upcoming in model method.
raptorf1 Feb 4, 2023
41a4999
Extracts logic of stats in model method.
raptorf1 Feb 4, 2023
706d775
Merge branch 'development' into 'bookings-controller-refactoring'
raptorf1 Feb 6, 2023
cce0c27
Add more test scenarios to make sure begin works correctly
raptorf1 Feb 6, 2023
a393cf5
Typo fix in method name
raptorf1 Feb 6, 2023
698c33d
Changes render order in create action
raptorf1 Feb 6, 2023
a0e1c44
Further begin-rescue block updates
raptorf1 Feb 6, 2023
39523b6
Task code rearrangement.
raptorf1 Feb 6, 2023
3e06076
Merge branch 'development' into 'bookings-controller-refactoring'
raptorf1 Feb 7, 2023
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
225 changes: 62 additions & 163 deletions app/controllers/api/v1/bookings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,179 +1,78 @@
class Api::V1::BookingsController < ApplicationController
include BookingsConcern
before_action :authenticate_api_v1_user!, only: %i[create update]

before_action :authenticate_api_v1_user!, only: %i[index create update]
def create
host = User.find_by(nickname: params[:host_nickname])
if host.nil?
StripeService.cancel_payment_intent(params[:payment_intent_id])
render json: { errors: [I18n.t("controllers.bookings.create_error_2")] }, status: 400 and return
end

def index
case
when params[:stats] == "yes" && params[:host_nickname] == current_api_v1_user.nickname &&
params[:user_id].to_i == current_api_v1_user.id
now = DateTime.new(Time.now.year, Time.now.month, Time.now.day, 0, 0, 0, 0)
now_epoch_javascript = (now.to_f * 1000).to_i
incoming_bookings = Booking.where(host_nickname: params[:host_nickname])
incoming_requests = []
incoming_upcoming = []
incoming_history = []
incoming_unpaid = []
incoming_bookings.each do |booking|
case
when booking.status == "pending"
incoming_requests.push(booking)
when booking.status == "accepted" && booking.dates.last > now_epoch_javascript
incoming_upcoming.push(booking)
else
incoming_history.push(booking)
end
incoming_unpaid.push(booking) if booking.status == "accepted" && booking.paid == false
end
outgoing_bookings = Booking.where(user_id: params[:user_id])
outgoing_requests = []
outgoing_upcoming = []
outgoing_history = []
outgoing_unpaid = []
outgoing_bookings.each do |booking|
case
when booking.status == "pending"
outgoing_requests.push(booking)
when booking.status == "accepted" && booking.dates.last > now_epoch_javascript
outgoing_upcoming.push(booking)
else
outgoing_history.push(booking)
end
outgoing_unpaid.push(booking) if booking.status == "accepted" && booking.paid == false
end
render json: {
stats: {
in_requests: "#{incoming_requests.length}",
in_upcoming: "#{incoming_upcoming.length}",
in_history: "#{incoming_history.length}",
in_unpaid: "#{incoming_unpaid.length}",
out_requests: "#{outgoing_requests.length}",
out_upcoming: "#{outgoing_upcoming.length}",
out_history: "#{outgoing_history.length}",
out_unpaid: "#{outgoing_unpaid.length}"
}
},
status: 200
when params[:stats] == "no" && params[:host_nickname] == current_api_v1_user.nickname
if params.has_key?("dates")
bookings = []
host = User.where(nickname: params[:host_nickname])
profile = HostProfile.where(user_id: host[0].id)
if profile.length == 1
render json: find_host_bookings(profile[0].id, 0)
else
render json: bookings
end
else
bookings = Booking.where(host_nickname: params[:host_nickname])
render json: bookings, each_serializer: Bookings::IndexSerializer
end
when params[:stats] == "no" && params[:user_id].to_i == current_api_v1_user.id
bookings = Booking.where(user_id: params[:user_id])
render json: bookings, each_serializer: Bookings::IndexSerializer
else
bookings = []
render json: bookings, each_serializer: Bookings::IndexSerializer
if !BookingService.validate_dates(params[:host_nickname], params[:dates].map(&:to_i))
StripeService.cancel_payment_intent(params[:payment_intent_id])
render json: { errors: [I18n.t("controllers.bookings.create_error_1")] }, status: 400 and return
end
end

def create
booking = Booking.create(booking_params)
if booking.persisted?
host = User.where(nickname: booking.host_nickname)
if host.length == 1
profile = HostProfile.where(user_id: host[0].id)
user = User.where(id: booking.user_id)
if (booking.dates - find_host_bookings(profile[0].id, booking.id)) == booking.dates
render json: { message: I18n.t("controllers.reusable.create_success") }, status: 200
BookingsMailer.delay(queue: "bookings_email_notifications").notify_host_create_booking(
host[0],
booking,
user[0]
)
else
cancel_payment_intent(booking.payment_intent_id)
booking.destroy
render json: { error: [I18n.t("controllers.bookings.create_error_1")] }, status: 422
end
else
cancel_payment_intent(booking.payment_intent_id)
booking.destroy
render json: { error: [I18n.t("controllers.bookings.create_error_2")] }, status: 422
end
else
cancel_payment_intent(booking.payment_intent_id)
render json: { error: booking.errors.full_messages }, status: 422
booking_to_create = Booking.create(booking_params)
if !booking_to_create.persisted?
StripeService.cancel_payment_intent(params[:payment_intent_id])
render json: { errors: booking_to_create.errors.full_messages }, status: 400 and return
end

user = User.find(booking_to_create.user_id)
BookingsMailer.delay(queue: "bookings_email_notifications").notify_host_create_booking(
host,
booking_to_create,
user
)
render json: { message: I18n.t("controllers.reusable.create_success") }, status: 200
end

def update
Stripe.api_key =
if ENV["OFFICIAL"] == "yes"
Rails.application.credentials.STRIPE_API_KEY_PROD
else
Rails.application.credentials.STRIPE_API_KEY_DEV
end
Stripe.api_key = StripeService.get_api_key
booking = Booking.find(params[:id])
if current_api_v1_user.nickname == booking.host_nickname
user = User.where(id: booking.user_id)
host = User.where(nickname: booking.host_nickname)
profile = HostProfile.where(user_id: host[0].id)
booking.update(status: params[:status], host_message: params[:host_message])
case
when booking.persisted? == true && booking.host_message.length < 201 && booking.status == "accepted"
if (booking.dates - find_host_bookings(profile[0].id, booking.id)) == booking.dates
begin
!Rails.env.test? && Stripe::PaymentIntent.capture(booking.payment_intent_id)
render json: { message: I18n.t("controllers.bookings.update_success") }, status: 200
booking.update(
host_description: profile[0].description,
host_full_address: profile[0].full_address,
host_real_lat: profile[0].latitude,
host_real_long: profile[0].longitude,
host_profile_id: profile[0].id
)
new_availability = profile[0].availability - booking.dates
profile.update(availability: new_availability)
BookingsMailer.delay(queue: "bookings_email_notifications").notify_user_accepted_booking(
host[0],
booking,
user[0]
)
rescue Stripe::StripeError
booking.update(status: "pending", host_message: nil)
render json: { error: I18n.t("controllers.reusable.stripe_error") }, status: 555
end
else
render json: { error: I18n.t("controllers.bookings.update_error_same_dates") }, status: 427
booking.update(
status: "canceled",
host_message:
"This booking got canceled by KattBNB. The host has accepted another booking in that date range."
)
BookingsMailer.delay(queue: "bookings_email_notifications").notify_user_declined_booking(
host[0],
booking,
user[0]
)
cancel_payment_intent(booking.payment_intent_id)
end
when booking.persisted? == true && booking.host_message.length < 201 && booking.status == "declined"
render json: { message: I18n.t("controllers.bookings.update_success") }, status: 200
BookingsMailer.delay(queue: "bookings_email_notifications").notify_user_declined_booking(
host[0],
booking,
user[0]
)
cancel_payment_intent(booking.payment_intent_id)
else
render json: { error: booking.errors.full_messages }, status: 422
end

if current_api_v1_user.nickname != booking.host_nickname
render json: { errors: [I18n.t("controllers.reusable.update_error")] }, status: 400 and return
end

booking.update(status: params[:status], host_message: params[:host_message])

if booking.errors.full_messages.length > 0
render json: { errors: booking.errors.full_messages }, status: 400 and return
end

user = User.find(booking.user_id)
host = User.find_by(nickname: booking.host_nickname)
profile = HostProfile.find_by(user_id: host.id)

if booking.status == "declined"
StripeService.cancel_payment_intent(booking.payment_intent_id)
BookingsMailer.delay(queue: "bookings_email_notifications").notify_user_declined_booking(host, booking, user)
render json: { message: I18n.t("controllers.bookings.update_success") }, status: 200 and return
end

begin
!Rails.env.test? && Stripe::PaymentIntent.capture(booking.payment_intent_id)
rescue Stripe::StripeError
booking.update(status: "pending", host_message: nil)
render json: { errors: [I18n.t("controllers.reusable.stripe_error")] }, status: 400
else
render json: { error: I18n.t("controllers.reusable.update_error") }, status: 422
BookingService.cancel_same_date_pending_bookings_on_update(host, booking.dates, booking.id)
booking.update(
host_description: profile.description,
host_full_address: profile.full_address,
host_real_lat: profile.latitude,
host_real_long: profile.longitude,
host_profile_id: profile.id
)
new_availability = profile.availability - booking.dates
profile.update(availability: new_availability)
BookingsMailer.delay(queue: "bookings_email_notifications").notify_user_accepted_booking(host, booking, user)
render json: { message: I18n.t("controllers.bookings.update_success") }, status: 200
end
rescue ActiveRecord::RecordNotFound
render json: { error: [I18n.t("controllers.bookings.update_error")] }, status: :not_found
render json: { errors: [I18n.t("controllers.bookings.update_error")] }, status: 400
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Api::V1::FetchBookingActions::HostUnavailableDatesController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
head :no_content, status: 204 and return if current_api_v1_user.host_profile.nil?

render json: BookingService.get_host_unavailable_dates(current_api_v1_user.host_profile.id), status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::V1::FetchBookingActions::IncomingHistoryController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
head :no_content, status: 204 and return if current_api_v1_user.host_profile.nil?

render json: Booking.get_history_bookings_sorted(current_api_v1_user.nickname, nil),
each_serializer: Bookings::IndexSerializer,
status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::V1::FetchBookingActions::IncomingRequestsController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
head :no_content, status: 204 and return if current_api_v1_user.host_profile.nil?

render json: Booking.get_request_bookings_sorted(current_api_v1_user.nickname, nil),
each_serializer: Bookings::IndexSerializer,
status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Api::V1::FetchBookingActions::IncomingStatsController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
head :no_content, status: 204 and return if current_api_v1_user.host_profile.nil?

render json: Booking.get_booking_stats(current_api_v1_user.nickname, nil), status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::V1::FetchBookingActions::IncomingUpcomingController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
head :no_content, status: 204 and return if current_api_v1_user.host_profile.nil?

render json: Booking.get_upcoming_bookings_sorted(current_api_v1_user.nickname, nil),
each_serializer: Bookings::IndexSerializer,
status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Api::V1::FetchBookingActions::OutgoingHistoryController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
render json: Booking.get_history_bookings_sorted(nil, current_api_v1_user.id),
each_serializer: Bookings::IndexSerializer,
status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Api::V1::FetchBookingActions::OutgoingRequestsController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
render json: Booking.get_request_bookings_sorted(nil, current_api_v1_user.id),
each_serializer: Bookings::IndexSerializer,
status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Api::V1::FetchBookingActions::OutgoingStatsController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
render json: Booking.get_booking_stats(nil, current_api_v1_user.id), status: 200
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Api::V1::FetchBookingActions::OutgoingUpcomingController < ApplicationController
before_action :authenticate_api_v1_user!, only: %i[index]

def index
render json: Booking.get_upcoming_bookings_sorted(nil, current_api_v1_user.id),
each_serializer: Bookings::IndexSerializer,
status: 200
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/reviews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def create
host = User.find_by(nickname: booking.host_nickname)
user = User.find(booking.user_id)

render json: { message: I18n.t("controllers.reusable.create_success") }, status: 200
ReviewsMailer.delay(queue: "reviews_email_notifications").notify_host_create_review(host, booking, user, review)
render json: { message: I18n.t("controllers.reusable.create_success") }, status: 200
end

def update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def perform

host = User.find_by(nickname: host_nickname)
if host.nil?
StripeService.webhook_cancel_payment_intent(payment_intent)
StripeService.cancel_payment_intent(payment_intent)
return
end

if !BookingService.validate_dates(host_nickname, dates)
StripeService.webhook_cancel_payment_intent(payment_intent)
StripeService.cancel_payment_intent(payment_intent)
return
end

Expand All @@ -95,7 +95,7 @@ def perform
user_id: user_id
)
if !booking_to_create.persisted?
StripeService.webhook_cancel_payment_intent(payment_intent)
StripeService.cancel_payment_intent(payment_intent)
return
end

Expand Down
Loading