mirror of
https://git.auk.su/Dinect/bonus-import-tools.git
synced 2026-04-01 08:00:00 +00:00
Add error handling and retry logic to API calls
- Add retry decorator for transient errors in dinect_api.py - Add error handling decorator for consistent error responses - Update bonuses_update to handle numeric bonus amounts properly - Fix parameter name typo in bonuses_update - Skip invalid phone numbers and rows in app.py - Add duplicate doc_id detection in transaction processing - Improve logging of successful transactions - Remove redundant doc_id generation code - Fix file closing logic in app.py
This commit is contained in:
37
app.py
37
app.py
@@ -104,6 +104,7 @@ def run_import():
|
||||
if phone == '':
|
||||
print(f'error in line: [{line_count}]- Invalid phone number: {phone}')
|
||||
log_file.write(f'error in line: [{line_count}]- Invalid phone number: {phone}\n')
|
||||
continue # Skip invalid phone
|
||||
|
||||
# validate email
|
||||
email = email.strip()
|
||||
@@ -131,6 +132,7 @@ def run_import():
|
||||
except ValueError as e:
|
||||
ret = f'Unexpected error in line: [{line_count}] {repr(e)}'
|
||||
log_file.write(f'{ret}\n')
|
||||
continue # Skip invalid rows
|
||||
|
||||
# Find user via the API
|
||||
# by card
|
||||
@@ -176,6 +178,7 @@ def run_import():
|
||||
print(f'Processing "transaction" file: {f}')
|
||||
csv_reader = csv.reader(csv_file, delimiter=',')
|
||||
line_count = 0
|
||||
processed_doc_ids = set() # Track processed doc_ids to detect duplicates
|
||||
for row in csv_reader:
|
||||
if line_count == 0:
|
||||
line_count += 1
|
||||
@@ -185,6 +188,30 @@ def run_import():
|
||||
print(f'Processing line {line_count}: {row}')
|
||||
user_id, card, phone, summ_total, summ_discount, sum_with_discount, bonus_amount, transaction_date, transaction_time, doc_id = row
|
||||
|
||||
# Validate numeric fields
|
||||
try:
|
||||
float(summ_total) if summ_total.strip() else 0.0
|
||||
float(summ_discount) if summ_discount.strip() else 0.0
|
||||
float(sum_with_discount) if sum_with_discount.strip() else 0.0
|
||||
float(bonus_amount) if bonus_amount.strip() else 0.0
|
||||
except ValueError as ve:
|
||||
print(f'error in line: [{line_count}]- Invalid numeric value: {ve}')
|
||||
log_file.write(f'error in line: [{line_count}]- Invalid numeric value: {ve}\n')
|
||||
continue
|
||||
|
||||
# Check for duplicate doc_id
|
||||
if doc_id.strip() == '':
|
||||
doc_id = f'{file_name[0:20]}-{line_count}-{card}'
|
||||
else:
|
||||
doc_id = doc_id.strip()
|
||||
|
||||
# Check for duplicate doc_id in current run
|
||||
if doc_id in processed_doc_ids:
|
||||
print(f'error in line: [{line_count}]- Duplicate doc_id: {doc_id}')
|
||||
log_file.write(f'error in line: [{line_count}]- Duplicate doc_id: {doc_id}\n')
|
||||
continue
|
||||
processed_doc_ids.add(doc_id)
|
||||
|
||||
if card.strip() != '':
|
||||
user_found_by_card, din_id, _, _, _ = get_user(card, get_type='auto')
|
||||
if not user_found_by_card:
|
||||
@@ -210,11 +237,6 @@ def run_import():
|
||||
|
||||
user_found = user_found_by_card or user_found_by_phone
|
||||
if user_found:
|
||||
if doc_id.strip() == '':
|
||||
doc_id = f'{file_name[0:20]}-{line_count}-{card}'
|
||||
else:
|
||||
doc_id = doc_id.strip()
|
||||
|
||||
result, data = bonuses_update(
|
||||
user_id=din_id,
|
||||
summ_total=summ_total,
|
||||
@@ -230,6 +252,7 @@ def run_import():
|
||||
log_file.write(f'error in line: [{line_count}]- bonuses_update: {data}\n')
|
||||
else:
|
||||
print(f'RESULT=OK, Bonuses updated, user_id={din_id}')
|
||||
log_file.write(f'Line: [{line_count}]- Success: doc_id={doc_id}, bonus={bonus_amount}\n')
|
||||
|
||||
else:
|
||||
print(f'error in line: [{line_count}]- Invalid user: {user_id}')
|
||||
@@ -238,13 +261,11 @@ def run_import():
|
||||
except ValueError as e:
|
||||
ret = f'error in line: [{line_count}] {repr(e)}'
|
||||
log_file.write(f'{ret}\n')
|
||||
continue
|
||||
|
||||
end_time = time.time()
|
||||
print(f'Elapsed time of TRANSACTIONS file processing : {end_time - start_time} seconds')
|
||||
|
||||
csv_file.close()
|
||||
log_file.close()
|
||||
|
||||
os.rename(f, f + '.' + time.strftime("%Y%m%d-%H%M%S") + '.old')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user