Skip to content

Commit

Permalink
Merge pull request #6 from TEDx-SJEC/newform
Browse files Browse the repository at this point in the history
Successfuly added mail features
  • Loading branch information
joywin2003 authored Sep 12, 2024
2 parents 8a4528a + 04f94fb commit 44a6fbf
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 11 deletions.
139 changes: 139 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
"@next-auth/prisma-adapter": "^1.0.7",
"@prisma/adapter-libsql": "^5.19.1",
"@prisma/client": "^5.19.1",
"axios": "^1.7.7",
"jest": "^29.7.0",
"next": "14.2.6",
"next-auth": "^4.24.7",
"nodemailer": "^6.9.15",
"otp-generator": "^4.0.1",
"react": "^18",
"react-dom": "^18",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20",
"@types/nodemailer": "^6.4.15",
"@types/otp-generator": "^4.0.2",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
Expand Down
Binary file modified prisma/dev.db
Binary file not shown.
Binary file modified prisma/dev.db-journal
Binary file not shown.
24 changes: 24 additions & 0 deletions prisma/migrations/20240912175524_email/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Warnings:
- You are about to drop the column `token` on the `VerificationRequest` table. All the data in the column will be lost.
- Added the required column `otp` to the `VerificationRequest` table without a default value. This is not possible if the table is not empty.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_VerificationRequest" (
"id" TEXT NOT NULL PRIMARY KEY,
"identifier" TEXT NOT NULL,
"otp" TEXT NOT NULL,
"expires" DATETIME NOT NULL,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" DATETIME NOT NULL
);
INSERT INTO "new_VerificationRequest" ("created_at", "expires", "id", "identifier", "updated_at") SELECT "created_at", "expires", "id", "identifier", "updated_at" FROM "VerificationRequest";
DROP TABLE "VerificationRequest";
ALTER TABLE "new_VerificationRequest" RENAME TO "VerificationRequest";
CREATE UNIQUE INDEX "VerificationRequest_identifier_otp_key" ON "VerificationRequest"("identifier", "otp");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
8 changes: 4 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ model Form {

model VerificationRequest {
id String @id @default(cuid())
identifier String
token String @unique
identifier String // Like email or phone number
otp String
expires DateTime
created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@unique([identifier, token])
}
@@unique([identifier, otp]) // Ensure OTP is unique for a given identifier
}
5 changes: 4 additions & 1 deletion src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ TURSO_AUTH_TOKEN = ""
GOOGLE_ID = ""
GOOGLE_SECRET = ""
NEXT_PUBLIC_SITE_URL = ""
NEXTAUTH_SECRET = ""
NEXTAUTH_SECRET = ""
SJEC_DISCOUNT_PRICE = 0
REFERAL_DISCOUNT_PRICE = 0
REGULAR_PRICE = 0
40 changes: 40 additions & 0 deletions src/app/api/send-mail/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NextRequest, NextResponse } from "next/server";
import sendEmail from "@/utils/sendMail";
import otpGenerator from "otp-generator";
import prisma from "@/server/db";

export async function POST(req: NextRequest) {
const body = await req.json();
const otp = otpGenerator.generate(6, {
upperCaseAlphabets: false,
lowerCaseAlphabets: false,
specialChars: false,
});

const expiresIn = 10; // 10 minutes
const expiresAt = new Date(Date.now() + expiresIn * 60 * 1000);

await prisma.verificationRequest.create({
data: {
identifier: body.email,
otp,
expires: expiresAt,
},
});

console.log(body);
const mailResponse = await sendEmail({
email: body.email,
name: body.name,
OTP: otp,
});

return NextResponse.json({
message: "Email sent successfully!",
mailResponse,
});
}

export async function GET() {
return NextResponse.json({ message: "Hello from the Send mail!" });
}
6 changes: 6 additions & 0 deletions src/app/api/submit-form/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { NextRequest, NextResponse } from "next/server";
import { RegistrationFormSchema, TRegistrationForm } from "@/utils/zod-schemas";
import prisma from "@/server/db";
import determinePrice from "@/utils/determinePrice";

export async function GET() {
return NextResponse.json({ message: "Hello from the API!" });
Expand Down Expand Up @@ -34,6 +35,8 @@ export async function POST(req: NextRequest, res: NextResponse) {
createdById,
} = body;

let price = await determinePrice(email, referralId);

const newFormEntry = await prisma.form.create({
data: {
name,
Expand All @@ -58,3 +61,6 @@ export async function POST(req: NextRequest, res: NextResponse) {
);
}
}



Loading

0 comments on commit 44a6fbf

Please sign in to comment.