Skip to content

Commit

Permalink
Merge pull request #25 from ivanleekk/feat/add-error-handling-for-lambda
Browse files Browse the repository at this point in the history
feat: add try catch for all handlers
  • Loading branch information
ivanleekk authored Sep 28, 2024
2 parents f7e20c0 + 87daa99 commit 255ccb4
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 349 deletions.
102 changes: 55 additions & 47 deletions src/handlers/BalanceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,69 @@ import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient({});

export async function individualBalanceHandler(chatId: string, messageSender: string) {
const user = await findUser_byUsername(`@${messageSender}`);
if (!user) {
return sendMessage(chatId, "You are not part of this group!");
}
const userBalance = await prisma.userGroupBalance.findUnique({
where: {
userId_groupId: {
userId: user.id,
groupId: chatId.toString()
}
},
});
try {
const user = await findUser_byUsername(`@${messageSender}`);
if (!user) {
return sendMessage(chatId, "You are not part of this group!");
}
const userBalance = await prisma.userGroupBalance.findUnique({
where: {
userId_groupId: {
userId: user.id,
groupId: chatId.toString()
}
},
});

if (!userBalance) {
return sendMessage(chatId, "You have no balance in this group!");
}
if (!userBalance) {
return sendMessage(chatId, "You have no balance in this group!");
}

const balance = userBalance.balance;
const balanceMessage = balance > 0
? `You owe \$${balance.toFixed(2)}`
: `You are owed \$${Math.abs(balance).toFixed(2)}`;
return sendMessage(chatId, `${balanceMessage}`);
const balance = userBalance.balance;
const balanceMessage = balance > 0
? `You owe \$${balance.toFixed(2)}`
: `You are owed \$${Math.abs(balance).toFixed(2)}`;
return sendMessage(chatId, `${balanceMessage}`);
} catch (error: any) {
return sendMessage(chatId, `An error occurred: ${error.message}`);
}
}

export async function groupBalanceHandler(chatId: string) {
const group = await prisma.group.findUnique({
where: {
id: chatId.toString()
},
include: {
members: true
}
});
try {
const group = await prisma.group.findUnique({
where: {
id: chatId.toString()
},
include: {
members: true
}
});

if (!group) {
return sendMessage(chatId, "Group not found!");
}
if (!group) {
return sendMessage(chatId, "Group not found!");
}

const groupMembers = group.members;
const groupMembers = group.members;

const groupBalance = await Promise.all(groupMembers.map(async (member) => {
const userBalance = await prisma.userGroupBalance.findUnique({
where: {
userId_groupId: {
userId: member.id,
groupId: chatId.toString()
const groupBalance = await Promise.all(groupMembers.map(async (member) => {
const userBalance = await prisma.userGroupBalance.findUnique({
where: {
userId_groupId: {
userId: member.id,
groupId: chatId.toString()
}
}
}
});
const balance = userBalance ? userBalance.balance : 0;
const balanceMessage = balance > 0
? `owes \$${balance.toFixed(2)}`
: `is owed \$${Math.abs(balance).toFixed(2)}`;
return { username: member.username, balanceMessage };
}));
});
const balance = userBalance ? userBalance.balance : 0;
const balanceMessage = balance > 0
? `owes \$${balance.toFixed(2)}`
: `is owed \$${Math.abs(balance).toFixed(2)}`;
return { username: member.username, balanceMessage };
}));

return sendMessage(chatId, `The group balance is \n${groupBalance.map((user) => `${user.username} ${user.balanceMessage}`).join("\n")}`);
return sendMessage(chatId, `The group balance is \n${groupBalance.map((user) => `${user.username} ${user.balanceMessage}`).join("\n")}`);
} catch (error: any) {
return sendMessage(chatId, `An error occurred: ${error.message}`);
}
}
Loading

0 comments on commit 255ccb4

Please sign in to comment.