@@ -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,54 @@ 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+ transaction_id = transaction ['transactionId' ],
160+ # attempt to grab the licensee id from the authorize.net data, which may be invalid if it was masked
161+ licensee_id = transaction ['licenseeId' ],
162+ )
163+ # We mark the data as UNKNOWN so it still shows up in the history,
164+ # and move onto the next transaction
165+ for jurisdiction in jurisdictions_to_process :
166+ item_id_prefix = f'priv:{ compact } -{ jurisdiction } '
167+ # we set the privilege id to UNKNOWN, so that it will be visible in the report
168+ self ._set_privilege_id_in_line_item (
169+ line_items = line_items , item_id_prefix = item_id_prefix , privilege_id = 'UNKNOWN'
170+ )
171+ continue
172+
173+ # ensure we only map to one provider for this transaction id
174+ provider_ids = {
175+ item ['providerId' ]
176+ for item in records_for_transaction_id
177+ if item ['type' ] == 'privilege' or item ['type' ] == 'privilegeUpdate'
178+ }
179+ # there should only be one provider id in the set
180+ if len (provider_ids ) > 1 :
181+ logger .error (
182+ 'More than one matching provider id found for a transaction id.' ,
183+ transaction_id = transaction ['transactionId' ],
184+ # attempt to grab the licensee id from the authorize.net data, which may be invalid if it was masked
185+ provider_ids = transaction ['licenseeId' ],
186+ )
187+
188+ # The licensee id recorded in Authorize.net cannot be trusted, as Authorize.net masks any values that look
189+ # like a credit card number (consecutive digits separated by dashes). We need to grab the provider id from
190+ # the privileges associated with this transaction and set the licensee id on the transaction to that value
191+ # to ensure it is valid.
192+ transaction ['licenseeId' ] = provider_ids .pop ()
193+
155194 # Process each privilege record
156195 for jurisdiction in jurisdictions_to_process :
157196 # Currently, we only support one license type per transaction when purchasing privileges
158197 # so we can just use this prefix to find the matching privilege record
159198 item_id_prefix = f'priv:{ compact } -{ jurisdiction } '
160199 # find the first privilege record for the jurisdiction that matches the provider ID
161200 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- ),
201+ (item for item in records_for_transaction_id if item ['jurisdiction' ].lower () == jurisdiction ),
167202 None ,
168203 )
169204 if matching_privilege :
@@ -185,7 +220,7 @@ def add_privilege_ids_to_transactions(self, compact: str, transactions: list[dic
185220 compact = compact ,
186221 transactionId = transaction ['transactionId' ],
187222 jurisdiction = jurisdiction ,
188- provider_id = licensee_id ,
223+ provider_id = transaction [ 'licenseeId' ] ,
189224 matching_privilege_records = response .get ('Items' , []),
190225 )
191226 # we set the privilege id to UNKNOWN, so that it will be visible in the report
0 commit comments