Separate out common functions

This commit is contained in:
Mark Veidemanis 2022-04-20 19:07:50 +01:00
parent ba9f99a23f
commit 4a1c359bf9
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 100 additions and 11 deletions

View File

@ -439,6 +439,94 @@ class Transactions(util.Base):
return True return True
return False return False
def find_trades_by_uid(self, uid):
"""
Find a list of trade IDs and references by a customer UID.
:return: tuple of (platform, trade_id, reference, currency)
"""
platform, username = self.get_uid(uid)
refs = self.get_refs()
matching_trades = []
for reference in refs:
ref_data = self.get_ref(reference)
tx_platform = ref_data["subclass"]
tx_username = ref_data["buyer"]
trade_id = ref_data["id"]
currency = ref_data["currency"]
if tx_platform == platform and tx_username == username:
to_append = (platform, trade_id, reference, currency)
matching_trades.append(to_append)
return matching_trades
def user_verification_successful(self, uid):
"""
A user has successfully completed verification.
"""
self.log.info(f"User has completed verification: {uid}")
trade_list = self.find_trades_by_uid(uid)
for platform, trade_id, reference, currency in trade_list:
self.log.info(f"Sending bank details/reference for {platform}/{reference}")
self.send_bank_details(platform, currency, trade_id)
self.send_reference(platform, trade_id, reference)
def create_uid(self, platform, username):
return f"{platform}|{username}"
def get_uid(self, external_user_id):
"""
Get the platform and username from the external user ID.
"""
spl = external_user_id.split("|")
if not len(spl) == 2:
self.log.error(f"Split invalid, cannot get customer: {spl}")
return False
platform, username = spl
return (platform, username)
def get_send_settings(self, platform):
if platform == "agora":
send_setting = settings.Agora.Send
post_message = self.agora.agora.contact_message_post
elif platform == "lbtc":
send_setting = settings.LocalBitcoins.Send
post_message = self.lbtc.lbtc.contact_message_post
return (send_setting, post_message)
def send_reference(self, platform, trade_id, reference):
"""
Send the reference to a customer.
"""
send_setting, post_message = self.get_send_settings(platform)
if send_setting == "1":
post_message(
trade_id,
f"When sending the payment please use reference code: {reference}",
)
def send_verification_url(self, platform, uid, trade_id):
send_setting, post_message = self.get_send_settings(platform)
if send_setting == "1":
auth_url = self.ux.verify.create_applicant_and_get_link(uid)
post_message(
trade_id,
f"Hi! To continue the trade, please complete the verification form: {auth_url}",
)
def send_bank_details(self, platform, currency, trade_id):
"""
Send the bank details to a trade.
"""
send_setting, post_message = self.get_send_settings(platform)
if send_setting == "1":
account_info = self.markets.get_matching_account_details(platform, currency)
formatted_account_info = self.markets.format_payment_details(currency, account_info)
print("formatted", formatted_account_info)
post_message(
trade_id,
f"Payment details: \n{formatted_account_info}",
)
def new_trade( def new_trade(
self, self,
subclass, subclass,
@ -475,17 +563,18 @@ class Transactions(util.Base):
r.hmset(f"trade.{reference}", to_store) r.hmset(f"trade.{reference}", to_store)
self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}") self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}")
self.ux.notify.notify_new_trade(amount, currency) self.ux.notify.notify_new_trade(amount, currency)
if subclass == "agora": uid = self.create_uid(subclass, buyer)
send_setting = settings.Agora.Send print("UID of new trade", uid)
post_message = self.agora.agora.contact_message_post verified = self.ux.verify.get_external_user_id_status(uid)
elif subclass == "lbtc": if verified != "GREEN":
send_setting = settings.LocalBitcoins.Send self.log.info(f"UID {uid} is not verified, sending link.")
post_message = self.lbtc.lbtc.contact_message_post self.send_verification_url(subclass, uid, trade_id)
if send_setting == "1": return
post_message( else: # User is verified
trade_id, self.log.info(f"UID {uid} is verified.")
f"Hi! When sending the payment please use reference code: {reference}", self.log.info(f"Sending bank details/reference for {subclass}/{reference}")
) self.send_bank_details(subclass, currency, trade_id)
self.send_reference(subclass, trade_id, reference)
if existing_ref: if existing_ref:
return util.convert(existing_ref) return util.convert(existing_ref)
else: else: