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

P2P Tranfer Feature Included #7

Open
wants to merge 2 commits 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
7 changes: 7 additions & 0 deletions apps/user-app/app/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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" />
<SidebarItem href={"/p2pTransaction"} icon={<P2PTransferIcon />} title="P2P Transfer" />
</div>
</div>
{children}
Expand All @@ -36,4 +37,10 @@ function TransactionsIcon() {
<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" />
</svg>

}

function P2PTransferIcon() {
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="m4.5 19.5 15-15m0 0H8.25m11.25 0v11.25" />
</svg>
}
12 changes: 12 additions & 0 deletions apps/user-app/app/(dashboard)/p2pTransaction/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SendMoney } from "../../../components/SendMoneyCard";

export default async function() {

return <div className="w-screen h-screen flex items-center justify-center">
<div className="w-1/3">
<div>
<SendMoney />
</div>
</div>
</div>
}
29 changes: 29 additions & 0 deletions apps/user-app/app/lib/actions/createOnRampTxn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server"
import { getServerSession } from "next-auth";
import { authOptions } from "../auth";
import prisma from "@repo/db/client";

export async function createOnRampTxn (amount:number, provider: string) {
const session = await getServerSession(authOptions);
const token = Math.random().toString();
const userId = session.user.id;

if(!userId){
return {
messafe : "User not Logged In"
}
}

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

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

export async function SendMoneyTo(number:string, amount:number) {
const session = await getServerSession(authOptions);
const userId = session?.user?.id

if(!userId) return { message : "UnAuthorized User!"};

const toUser = await prisma.user.findFirst({
where:{
number
}
})

if(!toUser) return {message : 'Invalid Number. Please provide a valid one..'};

try{
await prisma.$transaction(async (tx) =>{
const sender = await tx.balance.findUnique({
where : {
userId : Number(userId)
}
})

if(!sender || sender.amount / 100 < amount) throw new Error('InSufficient Balance')

await tx.balance.update({
where:{userId : Number(sender.id)},
data:{amount : {decrement:amount * 100}}
})

await tx.balance.update({
where:{userId : toUser.id},
data:{amount : {increment:amount * 100}}
})
});
}catch(e){
console.log("Error => ", e);
return { message : e}
}

return {message : 'Transaction Successful!'}

}
11 changes: 8 additions & 3 deletions apps/user-app/components/AddMoneyCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Center } from "@repo/ui/center";
import { Select } from "@repo/ui/select";
import { useState } from "react";
import { TextInput } from "@repo/ui/textinput";
import { createOnRampTxn } from "../app/lib/actions/createOnRampTxn";

const SUPPORTED_BANKS = [{
name: "HDFC Bank",
Expand All @@ -16,22 +17,26 @@ const SUPPORTED_BANKS = [{

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

<TextInput label={"Amount"} placeholder={"Amount"} onChange={(value) => {
setAmount(Number(value));
}} />
<div className="py-4 text-left">
Bank
</div>
<Select onSelect={(value) => {
setProvider(value);
setRedirectUrl(SUPPORTED_BANKS.find(x => x.name === value)?.redirectUrl || "")
}} options={SUPPORTED_BANKS.map(x => ({
key: x.name,
value: x.name
}))} />
<div className="flex justify-center pt-4">
<Button onClick={() => {
<Button onClick={async() => {
await createOnRampTxn(amoutn, provider);
window.location.href = redirectUrl || "";
}}>
Add Money
Expand Down
29 changes: 29 additions & 0 deletions apps/user-app/components/SendMoneyCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use client"
import { Button } from "@repo/ui/button";
import { Card } from "@repo/ui/card";
import { TextInput } from "@repo/ui/textinput";
import { useState } from "react";
import { SendMoneyTo } from "../app/lib/actions/sendMoney";

export const SendMoney = () => {
const [amount, setAmount] = useState(0);
const [number, setNumber] = useState('');
return <Card title="Send ">
<div className="w-full">
<TextInput label={"Number"} placeholder={"Number"} onChange={(value) => {
setNumber(value);
}} />
<TextInput label={"Amount"} placeholder={"Amount"} onChange={(value) => {
setAmount(Number(value));
}} />
<div className="flex justify-center pt-4">
<Button onClick={async() => {
const res = await SendMoneyTo(number,amount);
console.log(res.message);
}}>
Send Money
</Button>
</div>
</div>
</Card>
}