@@ -125,17 +125,16 @@ def _set_privilege_id_in_line_item(self, line_items: list[dict], item_id_prefix:
125125 if line_item .get ('itemId' ).lower ().startswith (item_id_prefix .lower ()):
126126 line_item ['privilegeId' ] = privilege_id
127127
128- def add_privilege_ids_to_transactions (self , compact : str , transactions : list [dict ]) -> list [dict ]:
128+ def add_privilege_information_to_transactions (self , compact : str , transactions : list [dict ]) -> list [dict ]:
129129 """
130- Add privilege IDs to transaction line items based on the jurisdiction they were purchased for.
130+ Add privilege and licensee IDs to transaction line items based on the jurisdiction they were purchased for.
131131
132132 :param compact: The compact name
133133 :param transactions: List of transaction records to process
134- :return: Modified list of transactions with privilege IDs added to line items
134+ :return: Modified list of transactions with privilege and licensee IDs added to line items
135135 """
136136 for transaction in transactions :
137137 line_items = transaction ['lineItems' ]
138- licensee_id = transaction ['licenseeId' ]
139138 # Extract jurisdictions from line items with format priv:{compact}-{jurisdiction}-{license type abbr}
140139 jurisdictions_to_process = set ()
141140 for line_item in line_items :
@@ -152,18 +151,56 @@ def add_privilege_ids_to_transactions(self, compact: str, transactions: list[dic
152151 KeyConditionExpression = Key ('compactTransactionIdGSIPK' ).eq (gsi_pk ),
153152 )
154153
154+ # Verify that the query returned at least one record
155+ records_for_transaction_id = response .get ('Items' , [])
156+ if not records_for_transaction_id :
157+ logger .error (
158+ 'No privilege records found for this transaction id.' ,
159+ compact = compact ,
160+ transaction_id = transaction ['transactionId' ],
161+ # attempt to grab the licensee id from the authorize.net data, which may be invalid if it was masked
162+ licensee_id = transaction ['licenseeId' ],
163+ )
164+ # We mark the data as UNKNOWN so it still shows up in the history,
165+ # and move onto the next transaction
166+ for jurisdiction in jurisdictions_to_process :
167+ item_id_prefix = f'priv:{ compact } -{ jurisdiction } '
168+ # we set the privilege id to UNKNOWN, so that it will be visible in the report
169+ self ._set_privilege_id_in_line_item (
170+ line_items = line_items , item_id_prefix = item_id_prefix , privilege_id = 'UNKNOWN'
171+ )
172+ continue
173+
174+ # ensure we only map to one provider for this transaction id
175+ provider_ids = {
176+ item ['providerId' ]
177+ for item in records_for_transaction_id
178+ if item ['type' ] == 'privilege' or item ['type' ] == 'privilegeUpdate'
179+ }
180+ # there should only be one provider id in the set
181+ if len (provider_ids ) > 1 :
182+ logger .error (
183+ 'More than one matching provider id found for a transaction id.' ,
184+ compact = compact ,
185+ transaction_id = transaction ['transactionId' ],
186+ # attempt to grab the licensee id from the authorize.net data, which may be invalid if it was masked
187+ provider_ids = transaction ['licenseeId' ],
188+ )
189+
190+ # The licensee id recorded in Authorize.net cannot be trusted, as Authorize.net masks any values that look
191+ # like a credit card number (consecutive digits separated by dashes). We need to grab the provider id from
192+ # the privileges associated with this transaction and set the licensee id on the transaction to that value
193+ # to ensure it is valid.
194+ transaction ['licenseeId' ] = provider_ids .pop ()
195+
155196 # Process each privilege record
156197 for jurisdiction in jurisdictions_to_process :
157198 # Currently, we only support one license type per transaction when purchasing privileges
158199 # so we can just use this prefix to find the matching privilege record
159200 item_id_prefix = f'priv:{ compact } -{ jurisdiction } '
160201 # find the first privilege record for the jurisdiction that matches the provider ID
161202 matching_privilege = next (
162- (
163- item
164- for item in response .get ('Items' , [])
165- if item ['jurisdiction' ].lower () == jurisdiction and item ['providerId' ] == licensee_id
166- ),
203+ (item for item in records_for_transaction_id if item ['jurisdiction' ].lower () == jurisdiction ),
167204 None ,
168205 )
169206 if matching_privilege :
@@ -185,7 +222,7 @@ def add_privilege_ids_to_transactions(self, compact: str, transactions: list[dic
185222 compact = compact ,
186223 transactionId = transaction ['transactionId' ],
187224 jurisdiction = jurisdiction ,
188- provider_id = licensee_id ,
225+ provider_id = transaction [ 'licenseeId' ] ,
189226 matching_privilege_records = response .get ('Items' , []),
190227 )
191228 # we set the privilege id to UNKNOWN, so that it will be visible in the report
0 commit comments