Skip to content

Commit c439729

Browse files
committed
Update cloud function
1 parent a6ed691 commit c439729

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Splito/UI/Home/Groups/Group/GroupExpenseListView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ private struct GroupExpenseHeaderOverallView: View {
344344
Text("Your \(Date().nameOfMonth.lowercased()) spending")
345345
.font(.body3())
346346
.foregroundStyle(disableText)
347-
.multilineTextAlignment(.trailing)
348347

349348
let spendingText = viewModel.currentMonthSpending.map { (currency, amount) in
350349
abs(amount).formattedCurrency(currency)
@@ -355,6 +354,7 @@ private struct GroupExpenseHeaderOverallView: View {
355354

356355
Spacer()
357356
}
357+
.multilineTextAlignment(.trailing)
358358
.frame(maxWidth: .infinity, alignment: .trailing)
359359
.padding(16)
360360
}

functions/src/users/users_service.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,20 @@ export const onGroupWrite = onDocumentWritten(
3737
{ document: 'groups/{groupId}' },
3838
async (event) => {
3939
try {
40+
const DEFAULT_CURRENCY = 'INR';
41+
4042
// Extract 'before' and 'after' data from the event
4143
const beforeData = event.data?.before?.data() as { balances: GroupMemberBalance[], is_active: boolean } | undefined;
4244
const afterData = event.data?.after?.data() as { balances: GroupMemberBalance[], is_active: boolean } | undefined;
4345

4446
// Initialize a Firestore batch to group all write operations
4547
const batch = db.batch();
4648

49+
// Helper function to round currency to 2 decimal places
50+
const roundCurrency = (amount: number): number => {
51+
return Number(amount.toFixed(2));
52+
};
53+
4754
// Helper function to process balances and update user totals
4855
const processBalances = async (balances: GroupMemberBalance[], multiplier: number) => {
4956
if (balances.length === 0) return; // Skip if balances are empty
@@ -72,8 +79,14 @@ export const onGroupWrite = onDocumentWritten(
7279
// Iterate over each currency in the balance_by_currency
7380
const { balance_by_currency } = balances[index];
7481
for (const [currency, currencyBalance] of Object.entries(balance_by_currency)) {
75-
const currentBalance = updatedTotal[currency] || 0;
76-
updatedTotal[currency] = currentBalance + multiplier * currencyBalance.balance;
82+
const currencyKey = currency || DEFAULT_CURRENCY;
83+
const currentBalance = updatedTotal[currencyKey] || 0;
84+
updatedTotal[currencyKey] = roundCurrency(currentBalance + multiplier * currencyBalance.balance);
85+
}
86+
87+
// Ensure at least the default currency is present
88+
if (!updatedTotal[DEFAULT_CURRENCY]) {
89+
updatedTotal[DEFAULT_CURRENCY] = 0;
7790
}
7891

7992
// Add the update to the batch
@@ -115,7 +128,7 @@ export const onGroupWrite = onDocumentWritten(
115128
const afterCurrencyBalance = balance_by_currency[currency]?.balance || 0;
116129
const beforeCurrencyBalance = oldBalances[currency]?.balance || 0;
117130
const diff = afterCurrencyBalance - beforeCurrencyBalance;
118-
diffs[currency] = { balance: diff };
131+
diffs[currency] = { balance: roundCurrency(diff) };
119132
}
120133

121134
return { id, balance_by_currency: diffs };

0 commit comments

Comments
 (0)