Skip to content

Commit

Permalink
Replace hardcoded currency strings with a regex parsing [ENG-1303]
Browse files Browse the repository at this point in the history
This will fix a TypeError when we get a string like "50 dollars"
  • Loading branch information
maksim-m committed Aug 29, 2024
1 parent 9d37bd8 commit 60bb99e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion actions/execute_transfer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from typing import Any, Dict
from datetime import datetime
from rasa_sdk import Action, Tracker
Expand All @@ -6,6 +7,9 @@
from actions.db import get_account, write_account, add_transaction, Transaction


AMOUNT_OF_MONEY_REGEX = re.compile(r"\d*[.,]*\d+")


class ExecuteTransfer(Action):

def name(self) -> str:
Expand All @@ -25,7 +29,7 @@ def run(self, dispatcher: CollectingDispatcher,
if recipient == "Jack":
return [SlotSet("transfer_money_transfer_successful", False)]

amount_of_money_value = float(amount_of_money.replace("$", "").replace("USD", "").strip())
amount_of_money_value = float(re.findall(AMOUNT_OF_MONEY_REGEX, amount_of_money)[0])
account.funds -= amount_of_money_value
new_transaction = \
Transaction(datetime=datetime.now().isoformat(), recipient=recipient,
Expand Down

0 comments on commit 60bb99e

Please sign in to comment.