Skip to content

Commit 30c2cf4

Browse files
committed
Fix race conditions due to missing await in calls
1 parent 31aeca4 commit 30c2cf4

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ 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(smallestDebt.amount, smallestCredit.amount);
122+
await optimisedDebtModel.create({
123123
from: smallestDebt.username,
124124
to: smallestCredit.username,
125125
amount: transactionAmount,

0 commit comments

Comments
 (0)