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

Completed paytm project #11

Open
wants to merge 1 commit into
base: migration
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions apps/merchant-app/public/circles.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 14 additions & 7 deletions apps/user-app/app/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function Layout({
<SidebarItem href={"/dashboard"} icon={<HomeIcon />} title="Home" />
<SidebarItem href={"/transfer"} icon={<TransferIcon />} title="Transfer" />
<SidebarItem href={"/transactions"} icon={<TransactionsIcon />} title="Transactions" />
</div>
<SidebarItem href={"/p2ptransfer"} icon={<P2PTransferIcon/>} title="P2P Transfer"/>
</div>
</div>
{children}
</div>
Expand All @@ -21,19 +22,25 @@ export default function Layout({

// Icons Fetched from https://heroicons.com/
function HomeIcon() {
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" className="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="m2.25 12 8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25" />
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
<path strokeLinecap="round" strokeLinejoin="round" d="m2.25 12 8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25" />
</svg>
}
function TransferIcon() {
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" className="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M7.5 21 3 16.5m0 0L7.5 12M3 16.5h13.5m0-13.5L21 7.5m0 0L16.5 12M21 7.5H7.5" />
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
<path strokeLinecap="round" strokeLinejoin="round" d="M7.5 21 3 16.5m0 0L7.5 12M3 16.5h13.5m0-13.5L21 7.5m0 0L16.5 12M21 7.5H7.5" />
</svg>
}

function TransactionsIcon() {
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" className="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
<path strokeLinecap="round" strokeLinejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>

}

function P2PTransferIcon() {
return <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
<path strokeLinecap="round" strokeLinejoin="round" d="m4.5 19.5 15-15m0 0H8.25m11.25 0v11.25" />
</svg>
}
35 changes: 35 additions & 0 deletions apps/user-app/app/(dashboard)/p2ptransfer/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import prisma from "@repo/db/client";
import P2PTransfer from "../../../components/P2PTransfer";
import { getServerSession } from "next-auth";
import { authOptions } from "../../lib/auth";
import { P2pTransactions } from "../../../components/P2pTransactions";

const getp2ptransaction = async () => {
const session = await getServerSession(authOptions);
const transactions = await prisma.p2PTransfer.findMany({
where: {
OR: [
{ fromUserId: Number(session?.user?.id) },
{ toUserId: Number(session?.user?.id) },
],
},
orderBy: {
timestamp: "desc",
},
});
return transactions;
};

export default async function () {
const transactions = await getp2ptransaction();
return (
<div className="flex justify-center items-center w-[80vw] gap-20">
<div className="w-[30%]">
<P2PTransfer />
</div>
<div className="w-[60%]">
<P2pTransactions transactions={transactions} />
</div>
</div>
);
}
2 changes: 1 addition & 1 deletion apps/user-app/app/(dashboard)/transfer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default async function() {
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 p-4">
<div>
<AddMoney />
</div>
</div>
<div>
<BalanceCard amount={balance.amount} locked={balance.locked} />
<div className="pt-4">
Expand Down
27 changes: 27 additions & 0 deletions apps/user-app/app/lib/actions/createOnRamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use server"

import { getServerSession } from "next-auth"
import { authOptions } from "../auth"
import prisma from "@repo/db/client";

export const createOnRamp = async (provider:string , amount:number)=>{
const session = await getServerSession(authOptions);
if(!session?.user || !session?.user?.id) return {message:"Unauthenticated request"}

const token = (Math.random()*1000).toString();

const onRamp = await prisma.onRampTransaction.create({
data:{
token,
provider,
amount:amount*100,
userId:Number(session.user.id),
status:"Processing",
startTime: new Date()
}
})

return{
message:"Done"
}
}
62 changes: 62 additions & 0 deletions apps/user-app/app/lib/actions/p2ptransfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use server"
import { getServerSession } from "next-auth"
import { authOptions } from "../auth"
import prisma from "@repo/db/client";

export const p2ptransfer = async (to:string,amount:number)=>{
const session = await getServerSession(authOptions);
if(!session?.user || !session?.user?.id) return {message:"Unauthenticated request"}

const from = session?.user?.id;

const toUser = await prisma.user.findFirst({
where:{
number:to
}
})
if(!toUser) return {message:"User not found"}

await prisma.$transaction(async(tx)=>{
await tx.$queryRaw`SELECT*FROM "Balance" WHERE "userId" = ${Number(from)} FOR UPDATE` //locks the Balance table
const fromBalance = await tx.balance.findFirst({
where:{
userId:Number(from)
}
})
if(!fromBalance || fromBalance?.amount < amount ){
return {message:"Insufficient funds"}
}
await tx.balance.update({
where:{
userId:Number(from)
},
data:{
amount:{
decrement:Number(amount)*100
}
}
})
await tx.balance.update({
where:{
userId:Number(toUser.id)
},
data:{
amount:{
increment:Number(amount)*100
}
}
})

await tx.p2PTransfer.create({
data:{
fromUserId:Number(from),
toUserId:Number(toUser.id),
timestamp:new Date(),
amount:Number(amount)*100
}
})

return {message:"Transaction completed"}
})

}
12 changes: 8 additions & 4 deletions apps/user-app/components/AddMoneyCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use client"
import { Button } from "@repo/ui/button";
import { Card } from "@repo/ui/card";
import { Center } from "@repo/ui/center";
import { Select } from "@repo/ui/select";
import { useState } from "react";
import { TextInput } from "@repo/ui/textinput";
import { createOnRamp } from "../app/lib/actions/createOnRamp";

const SUPPORTED_BANKS = [{
name: "HDFC Bank",
Expand All @@ -15,23 +15,27 @@ const SUPPORTED_BANKS = [{
}];

export const AddMoney = () => {
const [amount,setAmount] = useState(0);
const [bank,setBank] = useState(SUPPORTED_BANKS[0]?.name || "");
const [redirectUrl, setRedirectUrl] = useState(SUPPORTED_BANKS[0]?.redirectUrl);
return <Card title="Add Money">
<div className="w-full">
<TextInput label={"Amount"} placeholder={"Amount"} onChange={() => {

<TextInput label={"Amount"} placeholder={"Amount"} onChange={(e:any) => {
setAmount(e)
}} />
<div className="py-4 text-left">
Bank
</div>
<Select onSelect={(value) => {
setRedirectUrl(SUPPORTED_BANKS.find(x => x.name === value)?.redirectUrl || "")
setBank(value);
}} options={SUPPORTED_BANKS.map(x => ({
key: x.name,
value: x.name
}))} />
<div className="flex justify-center pt-4">
<Button onClick={() => {
<Button onClick={async ()=>{
await createOnRamp(bank, amount)
window.location.href = redirectUrl || "";
}}>
Add Money
Expand Down
2 changes: 1 addition & 1 deletion apps/user-app/components/OnRampTransactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const OnRampTransactions = ({
}
return <Card title="Recent Transactions">
<div className="pt-2">
{transactions.map(t => <div className="flex justify-between">
{transactions.map(t => <div className="flex justify-between" key={t.amount}>
<div>
<div className="text-sm">
Received INR
Expand Down
23 changes: 23 additions & 0 deletions apps/user-app/components/P2PTransfer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client"
import { useState } from 'react'
import { Button } from '../../../packages/ui/src/button'
import { Card } from '../../../packages/ui/src/card'
import { TextInput } from '../../../packages/ui/src/TextInput'
import {p2ptransfer} from '../app/lib/actions/p2ptransfer'
export default function P2PTransfer() {
const [number,setNumber] = useState("");
const [amount,setAmount] = useState(0);
return (
<div className="w-full">
<Card title='P2P Transfer'>
<div className='flex flex-col gap-4 w-full'>
<TextInput placeholder='Number' label='Number' onChange={(e:any)=>{setNumber(e)}}/>
<TextInput placeholder='Amount' label='Amount' onChange={(e:any)=>{setAmount(e)}}/>
<Button onClick={async()=>{
await p2ptransfer(number,amount)
}}>Transfer</Button>
</div>
</Card>
</div>
)
}
62 changes: 62 additions & 0 deletions apps/user-app/components/P2pTransactions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Card } from "@repo/ui/card";
import { getServerSession } from "next-auth";
import React from "react";
import { authOptions } from "../app/lib/auth";

export const P2pTransactions = async ({
transactions,
}: {
transactions: {
amount: number;
fromUserId: number;
toUserId: number;
timestamp: Date;
}[];
}) => {
const session = await getServerSession(authOptions);
const id = Number(session?.user?.id);

if (!transactions.length) {
return (
<Card title="Recent Transactions">
<div className="text-center pb-8 pt-8">No Recent transactions</div>
</Card>
);
}

return (
<Card title="Recent Transactions">
<div>
{transactions.map((t) => (
<div className="flex w-full justify-between my-2">
{t.toUserId === id ? (
<div className="flex w-full justify-between my-2">
<div>
<div className="text-sm">Received INR</div>
<div className="text-slate-600 text-xs">
{t.timestamp.toDateString()}
</div>
</div>
<div className="flex flex-col justify-center">
+ Rs {t.amount / 100}
</div>
</div>
) : (
<div className="flex w-full justify-between my-2">
<div>
<div className="text-sm">Sent INR</div>
<div className="text-slate-600 text-xs">
{t.timestamp.toDateString()}
</div>
</div>
<div className="flex flex-col justify-center">
- Rs {t.amount / 100}
</div>
</div>
)}
</div>
))}
</div>
</Card>
);
};
6 changes: 3 additions & 3 deletions apps/user-app/public/circles.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ model User {
password String
OnRampTransaction OnRampTransaction[]
Balance Balance[]
sentTransfer P2PTransfer[] @relation(name: "FromUserRelation")
receivedTransfer P2PTransfer[] @relation(name: "ToUserRelation")
}

model P2PTransfer {
id Int @id @default(autoincrement())
amount Int
timestamp DateTime
fromUserId Int
fromUser User @relation(name: "FromUserRelation", fields: [fromUserId], references: [id])
toUserId Int
toUser User @relation(name: "ToUserRelation", fields: [toUserId], references: [id])
}

model Merchant {
Expand Down