Skip to content

Commit

Permalink
added another spec for asset_ids scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis committed Dec 5, 2023
1 parent 81f601d commit 2c2a7e4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
23 changes: 23 additions & 0 deletions spec/asset_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,28 @@ module PlaceOS::Model
booking.asset_ids.size.should eq(1)
booking.asset_id.should eq(asset2.id)
end

it "handles asset_ids enries" do
asset = Generator.asset.save!
asset2 = Generator.asset.save!

tenant = get_tenant
event_start = 5.minutes.from_now
event_end = 10.minutes.from_now
asset_ids = [asset.id.as(String), asset2.id.as(String)]
booking = Generator.booking(tenant.id, asset_ids, event_start, event_end)
booking.save!

booking.asset_ids.size.should eq(2)
booking.asset_ids.first.should eq(booking.asset_id)
booking.asset_id.should eq(asset.id)

asset_ids.reverse!
booking.asset_ids = asset_ids
booking.save!
booking.asset_ids.size.should eq(2)
booking.asset_ids.first.should eq(booking.asset_id)
booking.asset_id.should eq(asset2.id)
end
end
end
20 changes: 20 additions & 0 deletions spec/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ module PlaceOS::Model
)
end

def self.booking(tenant_id, asset_ids : Array(String), start : Time, ending : Time, booking_type = "booking", parent_id = nil, event_id = nil)
user_name = Faker::Hacker.noun
user_email = Faker::Internet.email
Booking.new(
booking_type: booking_type,
asset_ids: asset_ids,
booking_start: start.to_unix,
booking_end: ending.to_unix,
user_email: PlaceOS::Model::Email.new(user_email),
user_name: user_name,
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: user_name,
tenant_id: tenant_id,
parent_id: parent_id,
event_id: event_id,
booked_by_id: "user-1234",
history: [] of Booking::History
)
end

def self.driver(role : Driver::Role? = nil, module_name : String? = nil, repo : Repository? = nil)
role = self.role unless role
repo = self.repository(type: Repository::Type::Driver).save! unless repo
Expand Down
10 changes: 8 additions & 2 deletions src/placeos-models/booking.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ module PlaceOS::Model
end

def update_assets
asset_ids[0] = asset_id unless asset_ids.empty?
asset_ids[0] = asset_id if asset_ids.size == 1
asset_ids.insert(0, asset_id) if asset_ids.empty?
asset_id = asset_ids.first
@asset_id = asset_ids.first
end

def asset_ids=(vals : Array(String))
@asset_ids = vals
@asset_ids_changed = true
end

def survey_trigger
Expand Down Expand Up @@ -156,6 +161,7 @@ module PlaceOS::Model

def set_created
self.last_changed = self.created = Time.utc.to_unix
self.asset_id = self.asset_ids.first unless self.asset_ids.empty?
end

def change_extension_data(data : JSON::Any)
Expand Down

0 comments on commit 2c2a7e4

Please sign in to comment.