Skip to content

Conversation

@rizan-ibrahim
Copy link

No description provided.

@yunchen4 yunchen4 self-assigned this Jun 13, 2025
Copy link

@yunchen4 yunchen4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Rizan,
There are two points you need to rework, and I have made them explicit. Please let me know on Slack after you finish the rework!

Comment on lines +36 to +45
const result = await collection
.aggregate([
{ $match: { Year: year, Age: age } },
{
$addFields: {
TotalPopulation: { $add: ["$M", "$F"] },
},
},
])
.toArray();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs rework: In the CSV file you can see continents are mixed with countries, for example there are documents for AFRICA. In this case you cannot just add all documents.

But I have to say it is not a good dataset 😅

Comment on lines 9 to 65
export async function transfer(fromAcc, toAcc, amount, remark) {
try {
await client.connect();
const db = client.db(dbName);
const accounts = db.collection("accounts");

// Fetch both accounts
const sender = await accounts.findOne({ account_number: fromAcc });
const receiver = await accounts.findOne({ account_number: toAcc });

if (!sender || !receiver) {
throw new Error("One or both accounts not found.");
}

if (sender.balance < amount) {
throw new Error("Insufficient funds.");
}

const nextSenderChange = sender.account_changes.length + 1;
const nextReceiverChange = receiver.account_changes.length + 1;

await accounts.updateOne(
{ account_number: fromAcc },
{
$inc: { balance: -amount },
$push: {
account_changes: {
change_number: nextSenderChange,
amount: -amount,
changed_date: new Date(),
remark: `Sent to ${toAcc}: ${remark}`,
},
},
}
);

await accounts.updateOne(
{ account_number: toAcc },
{
$inc: { balance: amount },
$push: {
account_changes: {
change_number: nextReceiverChange,
amount: amount,
changed_date: new Date(),
remark: `Received from ${fromAcc}: ${remark}`,
},
},
}
);

console.log(`Transfer of ${amount} from ${fromAcc} to ${toAcc} completed.`);
} catch (err) {
console.error("Transfer failed:", err.message);
} finally {
await client.close();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs rework: I didn't see you're using transactions here?

Comment on lines +12 to +19
const continents = [
"AFRICA",
"ASIA",
"EUROPE",
"LATIN AMERICA AND THE CARIBBEAN",
"NORTHERN AMERICA",
"OCEANIA",
];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to filter them out 😄 It solves the problem from the origin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants