Skip to content

Commit

Permalink
htttp endpoint -> server action
Browse files Browse the repository at this point in the history
  • Loading branch information
ducheharsh committed Jul 18, 2024
1 parent 1fe1713 commit 8836269
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
31 changes: 0 additions & 31 deletions apps/user-app/app/api/transactions/route.ts

This file was deleted.

27 changes: 27 additions & 0 deletions apps/user-app/app/lib/actions/Transactions.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 async function createOnRampTransactions({amount, provider}:{amount:number, provider:string}) {
const session = await getServerSession(authOptions);
const userId = session.user?.id;
const token = Math.random().toString(36).substring(7);
if (!userId) {
throw new Error("User not logged in");
}
const transaction = await prisma.onRampTransaction.create({
data: {
userId: Number(userId),
startTime: new Date(),
token:token,
status:"Processing",
amount: amount * 100,
provider:provider
}
})
return {
message: "Transaction initiated",
transaction
};
}
1 change: 0 additions & 1 deletion apps/user-app/app/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const authOptions = {
// TODO: can u fix the type here? Using any is bad
async session({ token, session }: any) {
session.user.id = token.sub

return session
}
}
Expand Down
16 changes: 8 additions & 8 deletions apps/user-app/components/AddMoney.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Input } from "./ui/input"
import { Select } from "./ui/select"
import { useState } from "react"
import axios from "axios"
import { createOnRampTransactions } from "../app/lib/actions/Transactions"

const SUPPORTED_BANKS = [{
name: "HDFC Bank",
Expand All @@ -18,6 +19,7 @@ const SUPPORTED_BANKS = [{
export default function AddMoney() {
const [redirectUrl, setRedirectUrl] = useState(SUPPORTED_BANKS[0]?.redirectUrl);
const [amount, setAmount] = useState(0);
const [provider, setProvider] = useState(SUPPORTED_BANKS[0]?.name)
return(
<Card className='mt-6 w-[35vw] h-fit'>
<CardHeader>
Expand All @@ -35,19 +37,17 @@ export default function AddMoney() {
<div className='flex flex-col mt-6' >
<Select onSelect={(value:any) => {
setRedirectUrl(SUPPORTED_BANKS.find(x => x.name === value)?.redirectUrl || "")
setProvider(SUPPORTED_BANKS.find(x => x.name === value)?.name || "")
}} options={SUPPORTED_BANKS.map(x => ({
key: x.name,
value: x.name
}))} />
<div className="mt-6">
<Button onClick={() => {
axios.post("/api/transactions", {
amount: amount,
token:"token__13"
}).then((res) => {
console.log(res.data)
})
}}>
<Button onClick={async()=>{
window.location.href = redirectUrl || "";
const res = await createOnRampTransactions({amount, provider: provider || ""})
alert(res.message);
}}>
Add Money
</Button>
</div>
Expand Down

0 comments on commit 8836269

Please sign in to comment.