Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add try catch for all handlers #25

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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