Skip to content

Commit 2c72658

Browse files
authored
Fix race conditions caused by missing await in calls, clean up code slightly (#147)
* Fix race conditions due to missing `await` in calls * Format code with Prettier * Refactor user switching component to use `useRef` instead of `createRef` for better performance * Use `const` instead of `var` * Format code with Prettier --------- Co-authored-by: IsaacCheng9 <[email protected]>
1 parent 1caa22b commit 2c72658

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

client/src/components/user_switching.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import React from "react";
1+
import React, { useRef } from "react";
22
import "../styles/user_switching.css";
3-
import { createRef } from "react";
43

54
function UserSwitching(props) {
65
let group = props.group;
7-
let selectRef = createRef();
6+
let selectRef = useRef();
87

98
function handleChange() {
109
// Get the selected user

server/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ app.use(debtsRouter);
2020
// !IMPORTANT: Create .env file with password
2121
const password = process.env.PASSWORD || "changePasswordHere";
2222
let devUrl = `mongodb+srv://admin:${password}@fairsplit.fjvgxmg.mongodb.net/?retryWrites=true&w=majority`;
23-
var mongoDB = process.env.MONGODB_URI || devUrl;
23+
const mongoDB = process.env.MONGODB_URI || devUrl;
2424
// Set up the Mongoose connection.
2525
mongoose
2626
.connect(mongoDB)

server/controllers/debt_controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ exports.settleDebt = async (request, response) => {
6868
);
6969
// Recalculate debts to minimise the number of transactions, as this
7070
// settlement may have changed the optimal strategy.
71-
helpers.simplifyDebts();
71+
await helpers.simplifyDebts();
7272
response.send(
7373
`Debt from '${request.body.from}' to '${request.body.to}' partially\
7474
settled and reduced successfully.`,
@@ -91,7 +91,7 @@ exports.settleDebt = async (request, response) => {
9191
);
9292
// Recalculate debts to minimise the number of transactions, as this
9393
// settlement may have changed the optimal strategy.
94-
helpers.simplifyDebts();
94+
await helpers.simplifyDebts();
9595
response.send(
9696
`Debt from '${request.body.from}' to '${request.body.to}' fully\
9797
settled and deleted successfully.`,
@@ -111,7 +111,7 @@ exports.deleteDebtBetweenUsers = async (request, response) => {
111111
});
112112
// Now that we've deleted the debt, there may be a better way of allocating
113113
// the debt, so recalculate debts to minimise the number of transactions.
114-
helpers.simplifyDebts();
114+
await helpers.simplifyDebts();
115115
response.send(
116116
`Debt from '${request.params.from}' to '${request.params.to}' deleted\
117117
successfully.`,

server/controllers/expense_controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const expenseModel = require("../models/expense");
55
exports.addExpense = async (request, response) => {
66
// Check that the sum of amounts owed matches the total expense amount.
77
let currentSum = 0;
8-
for (borrower of request.body.borrowers) {
8+
for (const borrower of request.body.borrowers) {
99
if (borrower[1] <= 0) {
1010
return response.status(400).json({
1111
error: "The amount owed must be a positive number.",
@@ -30,7 +30,7 @@ exports.addExpense = async (request, response) => {
3030
});
3131

3232
// Loop through each borrower and create/update debts accordingly.
33-
for (borrowerInfo of request.body.borrowers) {
33+
for (const borrowerInfo of request.body.borrowers) {
3434
const borrower = borrowerInfo[0];
3535
// The amount they owe must be handled before this to account for different
3636
// methods of splitting the expense (e.g. specific amounts, by percentage,
@@ -41,7 +41,7 @@ exports.addExpense = async (request, response) => {
4141

4242
// Recalculate debts to minimise the number of transactions, as this
4343
// settlement may have changed the optimal strategy.
44-
helpers.simplifyDebts();
44+
await helpers.simplifyDebts();
4545
response.status(201).json(expense);
4646
};
4747

server/controllers/helpers/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,17 @@ exports.simplifyDebts = async function () {
112112
}
113113

114114
// Create a new set of optimised debts to store the simplified debts.
115-
optimisedDebtModel.deleteMany({});
115+
await optimisedDebtModel.deleteMany({});
116116
// Create transactions until the min-heaps are empty to reach a zero-state.
117117
while (!minHeapDebt.empty() && !minHeapCredit.empty()) {
118118
const smallestDebt = minHeapDebt.pop();
119119
const smallestCredit = minHeapCredit.pop();
120120
// Create a new optimised debt.
121-
transactionAmount = Math.min(smallestDebt.amount, smallestCredit.amount);
122-
optimisedDebtModel.create({
121+
const transactionAmount = Math.min(
122+
smallestDebt.amount,
123+
smallestCredit.amount,
124+
);
125+
await optimisedDebtModel.create({
123126
from: smallestDebt.username,
124127
to: smallestCredit.username,
125128
amount: transactionAmount,

0 commit comments

Comments
 (0)