Skip to content

Commit

Permalink
fix missing strip
Browse files Browse the repository at this point in the history
  • Loading branch information
madanalogy committed Nov 13, 2023
1 parent 9bff9b9 commit 0ece0ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
42 changes: 25 additions & 17 deletions actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,45 @@ async def run_add(chat_id, text):
return constants.ERROR_ADD_FORMAT
amount = core[1].strip()
if not is_valid_amount(amount):
print("Detected not valid amount: ", amount)
return constants.ERROR_PRECONDITION
details = {
"name": core[0].strip().lower(),
"amount": float(amount),
"payer": core[2].strip().lower(),
}

owed_amount = {}
owed_amounts = {}
split_count = 0
running_sum = float(0)
for line in lines[1:]:
parsed = line.split(",")
if not parsed or len(parsed) > 2:
return constants.ERROR_ADD_FORMAT
if len(parsed) == 2 and not is_valid_amount(parsed[1]):
print("Detected not valid amount: ", parsed[1])
return constants.ERROR_PRECONDITION
if len(parsed) == 2:
owed_amount[parsed[0].strip()] = float(parsed[1])
owed = parsed[1].strip()
if not is_valid_amount(owed):
return constants.ERROR_PRECONDITION
owed_amounts[parsed[0].strip()] = float(owed)
running_sum += owed
else:
owed_amount[parsed[0].strip()] = -1
owed_amounts[parsed[0].strip()] = 0
split_count += 1
if running_sum > amount:
return constants.ERROR_SUM_MISMATCH
if running_sum == amount and split_count != 0:
return constants.ERROR_SUM_MISMATCH
if split_count != 0:
debt_each = (amount-running_sum)/split_count
for debtor in owed_amounts:
if owed_amounts[debtor] == 0:
owed_amounts[debtor] = debt_each


transactions = get_transactions(chat_id)
update_time, trans_ref = await transactions.add(details)
debt_ref = trans_ref.collection("debtors")
for debtor in owed_amount:
await debt_ref.document(debtor).add({"name": debtor, "amount": owed_amount[debtor]})
for debtor in owed_amounts:
await debt_ref.document(debtor).add({"name": debtor, "amount": owed_amounts[debtor]})

return "Added successfully! Use /list if you want to see all pending transactions"

Expand Down Expand Up @@ -74,9 +87,7 @@ async def run_detail(chat_id, text):
output = f"{to_get.name}, {to_get.amount}, {to_get.payer}"
debtors = transactions.document(to_get.id).collection("debtors")
for debtor in debtors:
output += f"\n{debtor.name}"
if debtor.amount:
output += f", {debtor.amount}"
output += f"\n{debtor.name}, {debtor.amount}"

return output

Expand All @@ -94,7 +105,6 @@ async def run_delete(chat_id, text):

async def run_settle(chat_id, text):
transactions = get_transactions(chat_id)

return "TODO"


Expand All @@ -110,16 +120,14 @@ def is_valid_amount(value):


def get_at(transactions, index):
if not index or not index.isnumeric():
if not index or not index.isnumeric() or int(index) < 1:
return None
sn = int(index)
if sn < 1:
return None
docs = transactions.stream()
parsed_transactions = []
for doc in docs:
parsed_transactions.append(doc)
if len(parsed_transactions) > sn:
return None
parsed_transactions.sort(key=lambda x: x.update_time)
return parsed_transactions[sn]
return parsed_transactions[sn-1]
2 changes: 2 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@
{ASSUMPTIONS}'''.format(ASSUMPTIONS=ASSUMPTIONS)

ERROR_EMPTY_LIST = "Nothing here yet! Use the /add command to add transactions."

ERROR_SUM_MISMATCH = "The math is not mathing. Please re-check the values you added."

0 comments on commit 0ece0ac

Please sign in to comment.