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

fix/simplify-command-bugs #37

Merged
merged 4 commits into from
Oct 2, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cashshare-telegram",
"version": "1.1.1",
"version": "1.1.2",
"description": "Cashshare Telegram Bot",
"main": "index.ts",
"scripts": {
Expand Down
34 changes: 23 additions & 11 deletions src/handlers/balanceHandler/balanceHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("BalanceHandler", () => {
});

it("should return an error message if the user has no balance in the group", async () => {
(findUser_byUsername as vi.Mock).mockResolvedValue({ id: "userId" });
(findUser_byUsername as vi.Mock).mockResolvedValue({id: "userId"});
(findUserGroupBalance_byUserIdGroupId as vi.Mock).mockResolvedValue(null);

await individualBalanceHandler(chatId, messageSender);
Expand All @@ -50,17 +50,17 @@ describe("BalanceHandler", () => {
});

it("should return the user's balance in the group indicating they owe money", async () => {
(findUser_byUsername as vi.Mock).mockResolvedValue({ id: "userId" });
(findUserGroupBalance_byUserIdGroupId as vi.Mock).mockResolvedValue({ balance: 100 });
(findUser_byUsername as vi.Mock).mockResolvedValue({id: "userId"});
(findUserGroupBalance_byUserIdGroupId as vi.Mock).mockResolvedValue({balance: 100});

await individualBalanceHandler(chatId, messageSender);

expect(sendMessage).toHaveBeenCalledWith(chatId, "You owe \$100.00");
});

it("should return the user's balance in the group indicating they are owed money", async () => {
(findUser_byUsername as vi.Mock).mockResolvedValue({ id: "userId" });
(findUserGroupBalance_byUserIdGroupId as vi.Mock).mockResolvedValue({ balance: -50 });
(findUser_byUsername as vi.Mock).mockResolvedValue({id: "userId"});
(findUserGroupBalance_byUserIdGroupId as vi.Mock).mockResolvedValue({balance: -50});

await individualBalanceHandler(chatId, messageSender);

Expand All @@ -80,11 +80,11 @@ describe("BalanceHandler", () => {
it("should return the group balance indicating members owe or are owed money", async () => {
(findGroup_byId as vi.Mock).mockResolvedValue({
id: chatId,
members: [{ id: "user1", username: "user1" }, { id: "user2", username: "user2" }],
members: [{id: "user1", username: "user1"}, {id: "user2", username: "user2"}],
});
(findUserGroupBalances_byGroupId as vi.Mock).mockResolvedValue([
{ user: { username: "user1" }, balance: 50 },
{ user: { username: "user2" }, balance: -75 },
{user: {username: "user1"}, balance: 50},
{user: {username: "user2"}, balance: -75},
]);

await groupBalanceHandler(chatId);
Expand All @@ -95,16 +95,28 @@ describe("BalanceHandler", () => {
it("should handle members with no balance", async () => {
(findGroup_byId as vi.Mock).mockResolvedValue({
id: chatId,
members: [{ id: "user1", username: "user1" }, { id: "user2", username: "user2" }],
members: [{id: "user1", username: "user1"}, {id: "user2", username: "user2"}],
});
(findUserGroupBalances_byGroupId as vi.Mock).mockResolvedValue([
{ user: { username: "user1" }, balance: 0 },
{ user: { username: "user2" }, balance: 75 },
{user: {username: "user1"}, balance: 0},
{user: {username: "user2"}, balance: 75},
]);

await groupBalanceHandler(chatId);

expect(sendMessage).toHaveBeenCalledWith(chatId, "The group balance is \nuser1 is owed \$0.00\nuser2 owes \$75.00");
});

it("should return a message if there are no balances in the group", async () => {
(findGroup_byId as vi.Mock).mockResolvedValue({
id: chatId,
members: [{id: "user1", username: "user1"}, {id: "user2", username: "user2"}],
});
(findUserGroupBalances_byGroupId as vi.Mock).mockResolvedValue([]);

await groupBalanceHandler(chatId);

expect(sendMessage).toHaveBeenCalledWith(chatId, "There are no outstanding balances in the group");
});
});
});
4 changes: 4 additions & 0 deletions src/handlers/balanceHandler/balanceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export async function groupBalanceHandler(chatId: string) {

const groupBalances = await findUserGroupBalances_byGroupId(chatId);

if (groupBalances.length < 1) {
return sendMessage(chatId, "There are no outstanding balances in the group");
}

return sendMessage(chatId, `The group balance is \n${groupBalances.map((userGroupBalance) =>
`${userGroupBalance.user.username} ${userGroupBalance.balance > 0 ? `owes \$${userGroupBalance.balance.toFixed(2)}` : `is owed \$${Math.abs(userGroupBalance.balance).toFixed(2)}`}`
).join("\n")}`);
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/simplifyHandler/simplifyHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ describe('simplifyHandler', () => {

await simplifyHandler('testChatId');

expect(mockedSendMessage).toHaveBeenCalledWith('testChatId', '@user2 pays @user1 $30\n' + '@user3 pays @user1 $20\n' + 'Simplified!\n\nTIP: use this to keep track of payments!<blockquote>/pay [amount] [user]</blockquote>');
expect(mockedSendMessage).toHaveBeenCalledWith('testChatId', '@user2 pays @user1 $30.00\n' + '@user3 pays @user1 $20.00\n' + 'Simplified!\n\nTIP: use this to keep track of payments!<blockquote>/pay [amount] [user]</blockquote>');
});
});
2 changes: 1 addition & 1 deletion src/handlers/simplifyHandler/simplifyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function simplifyHandler(chatId: string) {
j--;
continue;
}
resultString += `${debtor.user.username} pays ${creditor.user.username} \$${amount}\n`;
resultString += `${debtor.user.username} pays ${creditor.user.username} \$${amount.toFixed(2)}\n`;
if (creditor.balance === 0) {
i++;
}
Expand Down
6 changes: 4 additions & 2 deletions src/handlers/transactionsHandler/transactionsHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { transactionsHandler } from './transactionsHandler';
import { sendMessage } from '../../utils/telegramUtils';
import { PrismaClient } from '@prisma/client';

vi.mock('../../utils/utils', () => ({
vi.mock('../../utils/telegramUtils', () => ({
sendMessage: vi.fn(),
}));

Expand Down Expand Up @@ -35,13 +35,15 @@ describe('transactionsHandler', () => {
it('should return a message with all transactions', async () => {
prisma.transaction.findMany.mockResolvedValue([
{
groupTransactionId: 1,
type: 'REPAYMENT',
payers: [{ user: { username: 'user1' } }],
payee: [{ username: 'user2' }],
totalAmount: 10,
description: 'Lunch',
},
{
groupTransactionId: 2,
type: 'EXPENSE',
payers: [{ user: { username: 'user3' } }],
payee: [{ username: 'user4' }],
Expand All @@ -53,7 +55,7 @@ describe('transactionsHandler', () => {
await transactionsHandler(chatId);
expect(sendMessage).toHaveBeenCalledWith(
chatId,
'<b>Transactions:</b>\nType: REPAYMENT \nFrom: user1 To: user2 \nAmount: $10 Description: Lunch\n\nType: EXPENSE \nFrom: user3 To: user4 \nAmount: $20 Description: Dinner\n\n'
'<b>Transactions:</b>\nId: 1 Type: REPAYMENT \nFrom: user1 To: user2 \nAmount: $10 Description: Lunch\n\nId: 2 Type: EXPENSE \nFrom: user3 To: user4 \nAmount: $20 Description: Dinner\n\n'
);
});
});
Loading