Skip to content

Instantly share code, notes, and snippets.

@gtan66
Last active August 29, 2015 14:27
Show Gist options
  • Save gtan66/355ba01e8101ed1db085 to your computer and use it in GitHub Desktop.
Save gtan66/355ba01e8101ed1db085 to your computer and use it in GitHub Desktop.
keycard query
#in spaceman
loc = Location.find_by(name: location_name)
floors = Floor.where(location_id: loc.id).where(name: floors)
os = Space.for_location(loc).where(floor_id: floors)
accounts = PrimaryReservation.where(reservable_uuid: os.pluck(:uuid)).where(started_on:Date.new(2015, 9, 1)).map(&:account).uniq
OR
accounts = PrimaryReservation.where(location_id: loc.id).where(started_on:Date.new(2015, 9, 1)).map(&:account).uniq
transfers = accounts.select{|a| a.primary_reservations.any?{ |p| p.started_on < Date.new(2015, 9, 1) } }
new_accounts = accounts - transfers
t_data = transfers.map do |t|
ps = t.primary_reservations.where(location_id: loc.id).where(started_on: Date.new(2015, 9, 1))
account = ps.first.account
offices = ps.map(&:office_num).join(" ")
transfer_from = t.primary_reservations.select{|p| p.ended_on && p.ended_on < Date.new(2015, 9, 1)}.last
other_locs = t.primary_reservations.select{ |p| p.location_id != loc.id && (p.ended_on.blank? || p.ended_on > Date.new(2015, 9, 1)) }.map{|p| p.location}.uniq.join(" ")
{account_name: account.name,
account_uuid: account.uuid,
offices: offices,
location: loc.uuid,
location_code: loc.code,
location_name: loc.name,
partition_name: loc.custom_fields["s2_partition_name"],
type: "transfer",
transfer_from: transfer_from.present? ? transfer_from.location.name : nil,
other_locations: other_locs ,
num_total_active_res: t.primary_reservations.select{ |p| p.ended_on.blank? || p.ended_on > Date.new(2015, 9, 1)}.count }
end
n_data = new_accounts.map do |t|
ps = t.primary_reservations.where(location_id: loc.id).where(started_on: Date.new(2015, 9, 1))
account = ps.first.account
offices = ps.map(&:office_num).join(" ")
{account_name: account.name,
account_uuid: account.uuid,
offices: offices,
location: loc.uuid,
partition_name: loc.custom_fields["s2_partition_name"],
location_code: loc.code,
location_name: loc.name,
type: "new" }
end
t_data + n_data
#in id
CSV.open("tmp/member_data.csv", "wb") do |csv|
data.each do |d|
c= Company.find_by(uuid: d[:account_uuid])
if c.blank?
puts "#{d[:account_name]} (#{d[:account_name]}) was not found"
next
end
users = c.seats.map(&:user)
users.each do |u|
name = u.name.split(" ")
keycard = u.keycard
hotstamp = keycard.present? ? keycard.hotstamp : " "
card_format = keycard.present? ? keycard.card_format : " "
access_at_this_location = u.keycard.present? ? u.keycard.accesses.any?{|k| k.partition_name == "LON06 - Moor Place"} : false
csv << [u.id, name.first, name.last, c.name, d[:offices], d[:type], d[:location_name], d[:transfer_from], d[:other_locations], hotstamp, card_format, access_at_this_location]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment