Skip to content

Commit b9b8b2e

Browse files
committed
Move AML-ready invoice recheck into its own task1
1 parent 65e8e0c commit b9b8b2e

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

shkeeper/amlchecker.py

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,11 @@ def withdraw_to_external_wallet(crypto_name, source, destination):
6464

6565

6666
def check_all_paid_invoices():
67-
# with app.app_context():
6867
app.logger.info(f"Check all invoices")
6968
paid_and_overpaid_invoices = (
7069
Invoice.query
7170
.filter(Invoice.status.in_([InvoiceStatus.PAID,
72-
InvoiceStatus.OVERPAID,
73-
InvoiceStatus.AML_CHECK_PAID,
74-
InvoiceStatus.AML_CHECK_OVERPAID,]))
75-
.all()
76-
)
77-
78-
71+
InvoiceStatus.OVERPAID,])).all())
7972

8073
for invoice in paid_and_overpaid_invoices:
8174
skip_invoice = False
@@ -142,8 +135,6 @@ def check_all_paid_invoices():
142135
db.session.commit()
143136
else:
144137
pass
145-
# app.logger.info(f'Incorrect invoice {invoice.external_id} status in this context, should be paid or overpaid')
146-
# continue
147138

148139
#if we here - all aml_scores are higher than AML_MIN_ACCEPT_SCORE
149140
for tx in invoice_transactions:
@@ -155,3 +146,59 @@ def check_all_paid_invoices():
155146
app.config.get("AML_EXTERNAL_ADDRESSES")[tx.crypto])
156147

157148

149+
def recheck_all_aml_invoices():
150+
# with app.app_context():
151+
app.logger.info(f"Reheck all AML invoices")
152+
paid_and_overpaid_invoices = (
153+
Invoice.query
154+
.filter(Invoice.status.in_([InvoiceStatus.AML_CHECK_PAID,
155+
InvoiceStatus.AML_CHECK_OVERPAID,])).all())
156+
157+
for invoice in paid_and_overpaid_invoices:
158+
skip_invoice = False
159+
invoice_transactions = Transaction.query.filter_by(invoice_id = invoice.id)
160+
invoice_tx_aml_scores = []
161+
invoice_tx_cryptos = []
162+
for tx in invoice_transactions:
163+
164+
tx_address = InvoiceAddress.query.filter(InvoiceAddress.invoice_id == invoice.id,
165+
InvoiceAddress.crypto == tx.crypto).first()
166+
aml_result = get_tx_aml_score(tx.crypto,
167+
tx.txid,
168+
tx.amount_crypto,
169+
tx_address.addr)
170+
if not aml_result:
171+
app.logger.info(f"Cannot get info for {tx.txid} tx, skip invoice")
172+
skip_invoice = True
173+
else:
174+
aml_score = aml_result['aml_score']
175+
invoice_tx_aml_scores.append(aml_score)
176+
invoice_tx_cryptos.append(tx.crypto)
177+
178+
if skip_invoice:
179+
app.logger.info(f"Skip invoce {invoice.external_id}")
180+
continue
181+
182+
all_aml_scores_above_limit = True
183+
184+
for aml_score in invoice_tx_aml_scores:
185+
if -1 < aml_score < app.config.get("AML_MIN_ACCEPT_SCORE"):
186+
# one invoice transaction is below the AML_MIN_ACCEPT_SCORE
187+
# invoice should be DECLINED
188+
app.logger.info(f"One of {invoice.external_id,} transactions below the AML_MIN_ACCEPT_SCORE, should refunded manually")
189+
invoice.status = InvoiceStatus.AML_CHECK_DECLINED
190+
db.session.commit()
191+
all_aml_scores_above_limit = False
192+
193+
if not all_aml_scores_above_limit:
194+
continue
195+
196+
for tx in invoice_transactions:
197+
# with app.app_context():
198+
tx_address = InvoiceAddress.query.filter(InvoiceAddress.invoice_id == invoice.id,
199+
InvoiceAddress.crypto == tx.crypto).first()
200+
withdraw_to_external_wallet(tx.crypto,
201+
tx_address.addr,
202+
app.config.get("AML_EXTERNAL_ADDRESSES")[tx.crypto])
203+
204+

shkeeper/tasks.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,13 @@ def task_check_aml_results():
136136
scheduler.app.logger.info(scheduler.app.config.get("AML_MODE").lower())
137137
if scheduler.app.config.get("AML_MODE").lower() != 'true':
138138
return
139-
check_all_paid_invoices()
139+
check_all_paid_invoices()
140+
141+
142+
@scheduler.task("interval", id="check_aml_results2", seconds=150)
143+
def task_check_aml_results2():
144+
with scheduler.app.app_context():
145+
scheduler.app.logger.info(scheduler.app.config.get("AML_MODE").lower())
146+
if scheduler.app.config.get("AML_MODE").lower() != 'true':
147+
return
148+
recheck_all_aml_invoices()

0 commit comments

Comments
 (0)