Skip to content
Open
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
6 changes: 5 additions & 1 deletion app/api/ica/endpoints/v1/transactions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'rfid_tid'

module ICA::Endpoints::V1
# Receives information about new or updated parking transactions
# rubocop:disable Metrics/ClassLength
Expand Down Expand Up @@ -94,7 +96,8 @@ def carpark
# To avoid problems with duplicate card numbers, we need to look up the concrete RFID tag belonging to the
# media key specified in the request.
def rfid_tag_information
rfid_tag_id = garage_system.card_account_mappings.find_by(card_key: params[:Media][:MediaKey]).rfid_tag_id
rfid_tag_id = garage_system.card_account_mappings.find_by(card_key: params[:Media][:MediaKey])&.rfid_tag_id ||
RfidTag.find_by(uid: RfidTid.new(params[:DriveIn][:Media][:MediaId]).to_s).id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this approach is that the specification doesn't guarantee that this will be present or that the media ID will be an RFID TID. Ideally, when running the sync, the card and customer account mappings are only soft-deleted (via acts_as_paranoid) and when finding them here, we then use the with_deleted scope. That way we don't need to make any assumptions about the request.

{
rfid_tag: { id: rfid_tag_id }
}
Expand Down Expand Up @@ -212,4 +215,5 @@ def finish_or_cancel_parking_transaction(facade_arguments)
end
end
end
# rubocop:enable Metrics/ClassLength
end
58 changes: 58 additions & 0 deletions lib/rfid_tid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should either be moved into ruby-utils (so that it's available in all repositories) or reside in spec/dummy. Otherwise we have a very unfortunate code duplication here.


# Utility to work with RFID tag TIDs
class RfidTid
def initialize(tid)
@tid = sanitize(tid)
end

def xtid?
e2? && bit_set?(8)
end

def security?
e2? && bit_set?(9)
end

def file?
e2? && bit_set?(10)
end

def designer
bits[11, 9].to_i
end

def model_number
bits[20, 12].to_i
end

def to_s
@tid
end

private

def e2?
@tid.starts_with?('E2')
end

def sanitize(tid)
tid = tid.upcase
return tid if plausible?(tid)
tid.scan(/(\w{2})(\w{2})/).map(&:reverse).join.tap do |reversed|
raise ArgumentError, 'Implausible TID' unless plausible?(reversed)
end
end

def plausible?(tid)
tid =~ /\AE2[0-9A-F]{22}\z/
end

def bit_set?(idx)
bits[idx] == '1'
end

def bits
@bits ||= @tid.to_i(16).to_s(2)
end
end
10 changes: 10 additions & 0 deletions spec/lib/rfid_tid_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true
require 'rfid_tid'

RSpec.describe RfidTid do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above: the spec shouldn't be duplicated either.


it 'ensures big endianness' do
expect(RfidTid.new('80E2051100208A267E868E08').to_s).to eq('E28011052000268A867E088E')
expect(RfidTid.new('E28011052000268A867E088E').to_s).to eq('E28011052000268A867E088E')
end
end
31 changes: 31 additions & 0 deletions spec/requests/api/v1/transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,37 @@ def fake_facade_response(success = true, result = {})
api_request(garage_system, :put, api_path, params)
expect(last_response.status).to eq(204)
end

context 'CardAccountMapping mismatch on started transaction' do
let(:params) do
{
CarParkId: carpark.carpark_id,
AccountKey: customer_account_mapping.account_key,
Media: {
MediaId: rfid_tag.tag_number,
MediaKey: 'some unknown MediaKey',
MediaType: 255
},
DriveIn: {
DateTime: entered_at.iso8601,
DeviceNumber: device_id,
InfoId: nil,
Media: {
MediaId: rfid_tag.uid,
MediaKey: nil,
MediaType: 1
},
Status: 1
},
Status: 0
}
end

it 'returns 204' do
api_request(garage_system, :put, api_path, params)
expect(last_response.status).to eq(204)
end
end
end

context 'with invalid params' do
Expand Down