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

Adding transaction display for customer #9

Merged
merged 11 commits into from
Mar 2, 2024
33 changes: 32 additions & 1 deletion backend/src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HttpCodes } from "../types/HttpCodes";
import { CustomResponse } from "../types/CustomResponse";
import { newUser } from "../service/user.service";
import { updateUser } from "../service/user.service";
import { getUserByEmail } from "../service/user.service";
import { getUserByEmail, getTransactionsByUserEmail } from "../service/user.service";
import { User } from "@prisma/client";

export const userRouter = Router();
Expand Down Expand Up @@ -68,6 +68,37 @@ userRouter.get("/get", async (req, res) => {
}
});

userRouter.get("/transactions", async (req, res) => {
try {
const userEmail = req.session.email as string;
const transactionsResponse = await getTransactionsByUserEmail(userEmail);

if (transactionsResponse.error) {
throw new Error("Failed to fetch transactions");
}

const transactions = transactionsResponse.data;

const response: CustomResponse = {
error: false,
message: "Transactions fetched successfully",
data: transactions
};

return res.status(HttpCodes.OK).json(response);
} catch (error) {
console.error("Error fetching transactions:", error);

const response: CustomResponse = {
error: true,
message: "Failed to fetch transactions",
data: null
};

return res.status(HttpCodes.INTERNAL_SERVER_ERROR).json(response);
}
});


userRouter.post("/update", async (req, res) => {
try {
Expand Down
33 changes: 33 additions & 0 deletions backend/src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,36 @@ export const getUserByEmail = async (email: string): Promise<CustomReturn<User>>
}
}
}

export const getTransactionsByUserEmail = async (email: string): Promise<CustomReturn<any>> => {
try {
let user = await prisma.user.findUnique({
where: {
email: email
},
include: {
posts: {
include: {
requests: true
}
},
requests: true
}
});

if (!user) return {
error: true,
data: "User does not exist."
}

return {
error: false,
data: user
}
} catch (error) {
return {
error: true,
data: null
}
}
}
128 changes: 128 additions & 0 deletions frontend/app/user/transactions/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"use client"
import { title } from "@/components/primitives";
import { Table, TableHeader, TableBody, TableColumn, TableRow, TableCell } from "@nextui-org/table";
import { Chip } from "@nextui-org/chip";
import { useState, useEffect, Fragment } from "react";
import axios from "axios";
import { siteConfig } from "@/config/site";
import { set } from "react-hook-form";

interface UserData {
id: string;
name: string;
email: string;
phoneNumber: string;
karmaPoints: number;
posts: Post[];
requests: Request[];
}

interface Post {
id: string;
authorId: string;
costInPoints: number;
source: string;
destination: string;
service: string;
requests: Request[];
}

interface Request {
id: string;
status: string;
senderId: string;
postId: string;
Saphereye marked this conversation as resolved.
Show resolved Hide resolved
}

export default function ShowTransactions() {
const transactions = [
{
authorName: "Divyateja",
source: "Mess 1",
destination: "V335",
service: "Pick up: Food",
costInPoints: 2
},
];

const [error, setError] = useState<string | null>(null);
const [userData, setUserData] = useState<UserData | null>(null);

const fetchUserData = async () => {
try {
const response = await axios.get(
siteConfig.server_url + "/user/transactions",
{
params: {},
withCredentials: true,
}
);

setUserData(response.data.data);
setError(null);
} catch (err) {
console.error("Error fetching user data:", err);
setError("Error fetching user data.");
}
};

useEffect(() => {
fetchUserData();
}, []);

return (
<section className="flex flex-col items-center justify-center gap-4 py-8 md:py-10">
<div className="inline-block max-w-lg text-center justify-center">
<h1 className={title({ color: "green", size: "md" })}>Geddit.&nbsp;</h1>
{userData && (
<>
<p>ID: {userData.id}</p>
<p>Name: {userData.name}</p>
<p>Email: {userData.email}</p>
<p>Phone Number: {userData.phoneNumber}</p>
<p>Karma Points: {userData.karmaPoints}</p>
<p>Posts: {userData.posts.length}</p>
<p>Requests: {userData.requests.length}</p>
</>
)}
<p>{error}</p>
<h1 className={title({
size: "md"
})} >
Transactions
</h1>
</div>

<div>
<Table aria-label="Example static collection table">
<TableHeader>
<TableColumn>Post content</TableColumn>
<TableColumn>Transaction Status</TableColumn>
<TableColumn>The guy who accepted it</TableColumn>
<TableColumn>Points</TableColumn>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>Sus stuff</TableCell>
<TableCell><Chip color="success">Completed</Chip></TableCell>
<TableCell>Divyateja</TableCell>
<TableCell>2</TableCell>
</TableRow>
<TableRow>
<TableCell>Useful stuff</TableCell>
<TableCell><Chip color="warning">In Progess</Chip></TableCell>
<TableCell>Divyateja</TableCell>
<TableCell>2</TableCell>
</TableRow>
<TableRow>
<TableCell>Life stuff</TableCell>
<TableCell><Chip color="danger">Incomplete</Chip></TableCell>
<TableCell><Chip color="default">No Takers</Chip></TableCell>
<TableCell>2</TableCell>
</TableRow>
</TableBody>
</Table>
</div>
</section>
);
}
Loading
Loading