Skip to content
Merged
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
8 changes: 7 additions & 1 deletion OPENAPI_DOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ paths:
schema:
type: string
nullable: true
- name: include_deleted
in: query
description: when true, returns all bookings including deleted ones
example: "true"
schema:
type: boolean
- name: deleted
in: query
description: when true, it returns deleted bookings
description: when true, only returns deleted bookings, unless `include_deleted=true`
example: "true"
schema:
type: boolean
Expand Down
90 changes: 90 additions & 0 deletions spec/controllers/bookings_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,96 @@ describe Bookings do
end
end
end

context "[include_deleted/deleted flags]", tags: "include-deleted" do
it "returns only non-deleted bookings by default" do
tenant = get_tenant

booking1 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2.deleted = true
booking2.deleted_at = Time.utc.to_unix
booking2.save!

starting = 5.minutes.from_now.to_unix
ending = 90.minutes.from_now.to_unix
zones_string = "#{booking1.zones.not_nil!.first},#{booking2.zones.not_nil!.first}"

route = "#{BOOKINGS_BASE}?period_start=#{starting}&period_end=#{ending}&type=desk&zones=#{zones_string}"
response = client.get(route, headers: headers)
response.status_code.should eq(200)
bookings = JSON.parse(response.body).as_a
bookings.size.should eq(1)
bookings.map(&.["id"].as_i64).should contain(booking1.id)
bookings.map(&.["id"].as_i64).should_not contain(booking2.id)
end

it "returns only deleted bookings when deleted=true" do
tenant = get_tenant

booking1 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2.deleted = true
booking2.deleted_at = Time.utc.to_unix
booking2.save!

starting = 5.minutes.from_now.to_unix
ending = 90.minutes.from_now.to_unix
zones_string = "#{booking1.zones.not_nil!.first},#{booking2.zones.not_nil!.first}"

route = "#{BOOKINGS_BASE}?period_start=#{starting}&period_end=#{ending}&type=desk&zones=#{zones_string}&deleted=true"
response = client.get(route, headers: headers)
response.status_code.should eq(200)
bookings = JSON.parse(response.body).as_a
bookings.size.should eq(1)
bookings.map(&.["id"].as_i64).should_not contain(booking1.id)
bookings.map(&.["id"].as_i64).should contain(booking2.id)
end

it "returns all bookings when include_deleted=true" do
tenant = get_tenant

booking1 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2.deleted = true
booking2.deleted_at = Time.utc.to_unix
booking2.save!

starting = 5.minutes.from_now.to_unix
ending = 90.minutes.from_now.to_unix
zones_string = "#{booking1.zones.not_nil!.first},#{booking2.zones.not_nil!.first}"

route = "#{BOOKINGS_BASE}?period_start=#{starting}&period_end=#{ending}&type=desk&zones=#{zones_string}&include_deleted=true"
response = client.get(route, headers: headers)
response.status_code.should eq(200)
bookings = JSON.parse(response.body).as_a
bookings.size.should eq(2)
bookings.map(&.["id"].as_i64).should contain(booking1.id)
bookings.map(&.["id"].as_i64).should contain(booking2.id)
end

it "returns all bookings when include_deleted=true and deleted=true" do
tenant = get_tenant

booking1 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2 = BookingsHelper.create_booking(tenant.id.not_nil!)
booking2.deleted = true
booking2.deleted_at = Time.utc.to_unix
booking2.save!

starting = 5.minutes.from_now.to_unix
ending = 90.minutes.from_now.to_unix
zones_string = "#{booking1.zones.not_nil!.first},#{booking2.zones.not_nil!.first}"

route = "#{BOOKINGS_BASE}?period_start=#{starting}&period_end=#{ending}&type=desk&zones=#{zones_string}&include_deleted=true&deleted=true"
response = client.get(route, headers: headers)
response.status_code.should eq(200)
bookings = JSON.parse(response.body).as_a
bookings.size.should eq(2)
bookings.map(&.["id"].as_i64).should contain(booking1.id)
bookings.map(&.["id"].as_i64).should contain(booking2.id)
end
end
end

describe "#booked" do
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/bookings.cr
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ class Bookings < Application
ending : Int64 = 1.hours.from_now.to_unix,
@[AC::Param::Info(name: "type", description: "the generic name of the asset whose bookings you wish to view", example: "desk")]
booking_type : String? = nil,
@[AC::Param::Info(name: "deleted", description: "when true, it returns deleted bookings", example: "true")]
@[AC::Param::Info(description: "when true, returns all bookings including deleted ones", example: "true")]
include_deleted : Bool = false,
@[AC::Param::Info(name: "deleted", description: "when true, only returns deleted bookings, unless `include_deleted=true`", example: "true")]
deleted_flag : Bool = false,
@[AC::Param::Info(description: "when true, returns all bookings including checked out ones", example: "true")]
include_checked_out : Bool = false,
Expand Down Expand Up @@ -242,7 +244,7 @@ class Bookings < Application
end
{% end %}

query = query.where(deleted: deleted_flag)
query = query.where(deleted: deleted_flag) unless include_deleted

unless include_checked_out
# query = checked_out_flag ? query.where("checked_out_at != ?", nil) : query.where(checked_out_at: nil)
Expand Down
Loading