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: created the getCurrentExpense route #55

Merged
merged 21 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3f55f46
feat: created the getCurrentExpense route
raizo07 Apr 13, 2024
3fbdbe9
fix: fix format and lint errors
raizo07 Apr 15, 2024
0d1da66
Merge branch 'main' into feat/getCurrentExpense
raizo07 Apr 15, 2024
28162ac
feat: wrote a test for getCurrentExpense.ts
raizo07 Apr 15, 2024
6558416
fix: Updated getExpense query and test
raizo07 Apr 16, 2024
93a8d2d
Merge branch 'feat/getCurrentExpense' of https://github.com/raizo07/v…
raizo07 Apr 16, 2024
f1aecf0
Merge branch 'main' into feat/getCurrentExpense
raizo07 Apr 16, 2024
ff735af
fix: fix format and lint errors
raizo07 Apr 16, 2024
18893c5
Merge branch 'main' of https://github.com/raizo07/vault into feat/get…
raizo07 Apr 16, 2024
6c8b11b
updated query
raizo07 Apr 17, 2024
f30489e
updated the test to use transaction hash and createdAt
raizo07 Apr 17, 2024
8d02628
Merge branch 'main' into feat/getCurrentExpense
raizo07 Apr 17, 2024
a6040da
added the suggested changes
raizo07 Apr 17, 2024
21c250b
Merge branch 'feat/getCurrentExpense' of https://github.com/raizo07/v…
raizo07 Apr 17, 2024
63297f5
applied requested changes
raizo07 Apr 17, 2024
5f11c18
changed amount format from Hexadecimal
raizo07 Apr 17, 2024
c66dfdb
updated the amount in test file
raizo07 Apr 18, 2024
9c077b3
used viem to format number values
raizo07 Apr 18, 2024
41a9554
Merge branch 'main' into feat/getCurrentExpense
raizo07 Apr 18, 2024
39fe650
test(backend): fix last test getCurrentExpense
0xLucqs Apr 18, 2024
01d4dd9
Merge branch 'main' into feat/getCurrentExpense
0xLucqs Apr 19, 2024
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
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"fastify-plugin": "^4.5.1",
"otp-generator": "^4.0.1",
"postgres": "^3.4.4",
"twilio": "^3.56.0"
"twilio": "^3.56.0",
"viem": "^2.9.21"
}
}
146 changes: 119 additions & 27 deletions backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions backend/src/routes/getCurrentExpense.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { sql } from 'drizzle-orm';
import { and, between, eq, gte } from 'drizzle-orm/pg-core/expressions';
import type { FastifyInstance } from 'fastify';
import { formatUnits, parseUnits } from 'viem';

import { usdcBalance, usdcTransfer } from '@/db/schema';
import { addressRegex } from '.';

export function getCurrentExpenseRoute(fastify: FastifyInstance) {
fastify.get('/get_current_expense', async (request, reply) => {
const { address } = request.query as { address?: string };
const decimal = 6;

if (!address) {
return reply.status(400).send({ error: 'Address is required.' });
}
// Validate address format
if (!addressRegex.test(address)) {
return reply.status(400).send({ error: 'Invalid address format.' });
}

try {
const currentDate = new Date();
// Calculate the date 7 days ago
const sevenDaysAgo = new Date(currentDate);
sevenDaysAgo.setDate(currentDate.getDate() - 7);
// Use Drizzle ORM to find expense by address
const expenses = await fastify.db
.select()
.from(usdcTransfer)
.where(
and(eq(usdcTransfer.fromAddress, address), gte(usdcTransfer.createdAt, sevenDaysAgo)),
);

// Calculate the sum of amounts
const totalAmount = expenses.reduce(
(acc, curr) => acc + parseUnits(curr.amount || '0', decimal),
parseUnits('0', decimal),
);

return reply.send({
cumulated_expense: formatUnits(totalAmount, decimal),
});
} catch (error) {
console.error(error);
return reply.status(500).send({ error: 'Internal server error' });
}
});
}
2 changes: 2 additions & 0 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { sql } from 'drizzle-orm';
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';

import { getBalanceRoute } from './getBalance';
import { getCurrentExpenseRoute } from './getCurrentExpense';
import { getHistoricalBalanceRoute } from './getHistoricalBalance';
import { getOtp } from './getOtp';
import { getTransactionHistory } from './getTransactionHistory';
Expand All @@ -14,6 +15,7 @@ export const addressRegex = /^0x0[0-9a-fA-F]{63}$/;
export function declareRoutes(fastify: FastifyInstance) {
getStatusRoute(fastify);
getBalanceRoute(fastify);
getCurrentExpenseRoute(fastify);
getTransactionHistory(fastify);
getRegisterRoute(fastify);
getOtp(fastify);
Expand Down
Loading
Loading