-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '100xdevs-cohort-2:master' into migration
- Loading branch information
Showing
18 changed files
with
265 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Build on PR | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Use Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '20' | ||
|
||
- name: Install Dependencies | ||
run: npm install | ||
|
||
- name: Generate prisma client | ||
run: npm run db:generate | ||
|
||
- name: Run Build | ||
run: npm run build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Build and Deploy to Docker Hub | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build-and-push: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check Out Repo | ||
uses: actions/checkout@v2 | ||
|
||
- name: Log in to Docker Hub | ||
uses: docker/login-action@v1 | ||
with: | ||
username: ${{ secrets.DOCKER_USERNAME }} | ||
password: ${{ secrets.DOCKER_PASSWORD }} | ||
|
||
- name: Build and Push Docker image | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
file: ./docker/Dockerfile.user | ||
push: true | ||
tags: 100xdevs/week-18-class:latest # Replace with your Docker Hub username and repository | ||
|
||
- name: Verify Pushed Image | ||
run: docker pull 100xdevs/week-18-class:latest # Replace with your Docker Hub username and repository | ||
|
||
- name: Deploy to EC2 | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: ${{ secrets.SSH_HOST }} | ||
username: ${{ secrets.SSH_USERNAME }} | ||
key: ${{ secrets.SSH_KEY }} | ||
script: | | ||
sudo docker pull 100xdevs/week-18-class:latest | ||
sudo docker stop web-app || true | ||
sudo docker rm web-app || true | ||
sudo docker run -d --name web-app -p 3005:3000 100xdevs/week-18-class:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import NextAuth from "next-auth" | ||
import { authOptions } from "../../../../lib/auth" | ||
|
||
//@ts-ignore | ||
const handler = NextAuth(authOptions) | ||
|
||
export { handler as GET, handler as POST } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SendCard } from "../../../components/SendCard"; | ||
|
||
export default function() { | ||
return <div className="w-full"> | ||
<SendCard /> | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use server"; | ||
|
||
import { getServerSession } from "next-auth"; | ||
import { authOptions } from "../auth"; | ||
import prisma from "@repo/db/client"; | ||
|
||
export async function createOnRampTransaction(amount: number, provider: string) { | ||
const session = await getServerSession(authOptions); | ||
const token = Math.random().toString(); | ||
const userId = session.user.id; | ||
if (!userId) { | ||
return { | ||
message: "User not logged in" | ||
} | ||
} | ||
await prisma.onRampTransaction.create({ | ||
data: { | ||
userId: Number(userId), // 1 | ||
amount: amount, | ||
status: "Processing", | ||
startTime: new Date(), | ||
provider, | ||
token: token | ||
} | ||
}) | ||
|
||
return { | ||
message: "On ramp transaction added" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"use server" | ||
import { getServerSession } from "next-auth"; | ||
import { authOptions } from "../auth"; | ||
import prisma from "@repo/db/client"; | ||
|
||
export async function p2pTransfer(to: string, amount: number) { | ||
const session = await getServerSession(authOptions); | ||
const from = session?.user?.id; | ||
if (!from) { | ||
return { | ||
message: "Error while sending" | ||
} | ||
} | ||
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`; | ||
|
||
const fromBalance = await tx.balance.findUnique({ | ||
where: { userId: Number(from) }, | ||
}); | ||
if (!fromBalance || fromBalance.amount < amount) { | ||
throw new Error('Insufficient funds'); | ||
} | ||
|
||
await tx.balance.update({ | ||
where: { userId: Number(from) }, | ||
data: { amount: { decrement: amount } }, | ||
}); | ||
|
||
await tx.balance.update({ | ||
where: { userId: toUser.id }, | ||
data: { amount: { increment: amount } }, | ||
}); | ||
|
||
await tx.p2pTransfer.create({ | ||
data: { | ||
fromUserId: Number(from), | ||
toUserId: toUser.id, | ||
amount, | ||
timestamp: new Date() | ||
} | ||
}) | ||
// locking | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"use client" | ||
import { Button } from "@repo/ui/button"; | ||
import { Card } from "@repo/ui/card"; | ||
import { Center } from "@repo/ui/center"; | ||
import { TextInput } from "@repo/ui/textinput"; | ||
import { useState } from "react"; | ||
import { p2pTransfer } from "../app/lib/actions/p2pTransfer"; | ||
|
||
export function SendCard() { | ||
const [number, setNumber] = useState(""); | ||
const [amount, setAmount] = useState(""); | ||
|
||
return <div className="h-[90vh]"> | ||
<Center> | ||
<Card title="Send"> | ||
<div className="min-w-72 pt-2"> | ||
<TextInput placeholder={"Number"} label="Number" onChange={(value) => { | ||
setNumber(value) | ||
}} /> | ||
<TextInput placeholder={"Amount"} label="Amount" onChange={(value) => { | ||
setAmount(value) | ||
}} /> | ||
<div className="pt-4 flex justify-center"> | ||
<Button onClick={async () => { | ||
await p2pTransfer(number, Number(amount) * 100) | ||
}}>Send</Button> | ||
</div> | ||
</div> | ||
</Card> | ||
</Center> | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM node:20.12.0-alpine3.19 | ||
|
||
WORKDIR /usr/src/app | ||
|
||
COPY package.json package-lock.json turbo.json tsconfig.json ./ | ||
|
||
COPY apps ./apps | ||
COPY packages ./packages | ||
|
||
# Install dependencies | ||
RUN npm install | ||
# Can you add a script to the global package.json that does this? | ||
RUN npm run db:generate | ||
|
||
# Can you filter the build down to just one app? | ||
RUN npm run build | ||
|
||
CMD ["npm", "run", "start-user-app"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/db/prisma/migrations/20240330154923_adds_p2p_transfer/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- CreateTable | ||
CREATE TABLE "p2pTransfer" ( | ||
"id" SERIAL NOT NULL, | ||
"amount" INTEGER NOT NULL, | ||
"timestamp" TIMESTAMP(3) NOT NULL, | ||
"fromUserId" INTEGER NOT NULL, | ||
"toUserId" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "p2pTransfer_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "p2pTransfer" ADD CONSTRAINT "p2pTransfer_fromUserId_fkey" FOREIGN KEY ("fromUserId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "p2pTransfer" ADD CONSTRAINT "p2pTransfer_toUserId_fkey" FOREIGN KEY ("toUserId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.