-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from TEDx-SJEC/email
Added emailing feature with code and bug imporvements
- Loading branch information
Showing
7 changed files
with
130 additions
and
74 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,69 @@ | ||
import { MailUsingResend } from "@/lib/resend-mailer"; | ||
import prisma from "@/server/db"; | ||
import getErrorMessage from "@/utils/getErrorMessage"; | ||
import { emailSchema } from "@/utils/zod-schemas"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import sendEmail from "@/lib/sendMail"; | ||
import otpGenerator from "otp-generator"; | ||
import prisma from "@/server/db"; | ||
import { addToQueue } from "@/jobs"; | ||
import { MailUsingResend } from "@/lib/resend-mailer"; | ||
import { z } from "zod"; | ||
|
||
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, | ||
}, | ||
}); | ||
if (!body.email) { | ||
try { | ||
const body = await req.json(); | ||
const parsedBody = emailSchema.parse(body); | ||
|
||
const otp = otpGenerator.generate(6, { | ||
upperCaseAlphabets: false, | ||
lowerCaseAlphabets: false, | ||
specialChars: false, | ||
}); | ||
|
||
const expiresIn = 10; // OTP valid for 10 minutes | ||
const expiresAt = new Date(Date.now() + expiresIn * 60 * 1000); | ||
|
||
await prisma.verificationRequest.create({ | ||
data: { | ||
identifier: parsedBody.email, | ||
otp, | ||
expires: expiresAt, | ||
}, | ||
}); | ||
|
||
const mailResponse = await MailUsingResend({ | ||
email: parsedBody.email, | ||
name: parsedBody.name, | ||
OTP: otp, | ||
}); | ||
|
||
// const mailResponse1 = await addToQueue({ | ||
// email: parsedBody.email, | ||
// name: parsedBody.name, | ||
// OTP: otp, | ||
// }) | ||
|
||
return NextResponse.json({ | ||
message: "Email sent successfully!", | ||
mailResponse, | ||
}); | ||
|
||
} catch (error:unknown) { | ||
const errorMessage = getErrorMessage(error); | ||
|
||
if (error instanceof z.ZodError) { | ||
return NextResponse.json( | ||
{ message: "Validation error", errors: errorMessage }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
// Handle general server errors | ||
return NextResponse.json( | ||
{ message: "No recipients defined", status: 400 }, | ||
{ status: 400 } | ||
{ message: "Internal Server Error", errorMessage }, | ||
{ status: 500 } | ||
); | ||
} | ||
console.log(body); | ||
// const mailResponse1 = await addToQueue({ | ||
// email: body.email, | ||
// name: body.name, | ||
// OTP: otp, | ||
// }); | ||
const mailResponse2 = await MailUsingResend({ | ||
email: body.email, | ||
name: body.name, | ||
OTP: otp, | ||
}); | ||
|
||
return NextResponse.json({ | ||
message: "Email sent successfully!", | ||
// mailResponse1, | ||
mailResponse2, | ||
}); | ||
} | ||
|
||
// Test endpoint | ||
export async function GET() { | ||
return NextResponse.json({ message: "Hello from the Send mail!" }); | ||
} |
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 |
---|---|---|
@@ -1,20 +1,47 @@ | ||
import prisma from "@/server/db"; | ||
|
||
import { NextResponse } from "next/server"; | ||
|
||
export async function GET(req: Request) { | ||
const { searchParams } = new URL(req.url); | ||
const page = parseInt(searchParams.get("page") || "1"); | ||
const search = searchParams.get("search") || ""; | ||
const limit = 10; | ||
try { | ||
const { searchParams } = new URL(req.url); | ||
|
||
const page = Math.max(1, parseInt(searchParams.get("page") || "1", 10)); | ||
const search = searchParams.get("search") || ""; | ||
const limit = 10; | ||
|
||
const [users, totalCount] = await Promise.all([ | ||
prisma.user.findMany({ | ||
skip: (page - 1) * limit, | ||
take: limit, | ||
where: { | ||
name: { | ||
contains: search, | ||
}, | ||
}, | ||
}), | ||
prisma.user.count({ // Get the total number of users for pagination | ||
where: { | ||
name: { | ||
contains: search, | ||
}, | ||
}, | ||
}) | ||
]); | ||
|
||
const totalPages = Math.ceil(totalCount / limit); | ||
|
||
return NextResponse.json({ | ||
users, | ||
pagination: { | ||
currentPage: page, | ||
totalPages, | ||
totalCount, | ||
limit | ||
} | ||
}); | ||
|
||
const users = await prisma.user.findMany({ | ||
skip: (page - 1) * limit, | ||
take: limit, | ||
where: { | ||
name: { | ||
contains: search, | ||
}, | ||
}, | ||
}); | ||
return NextResponse.json({ users }); | ||
} catch (error) { | ||
console.error("Failed to fetch users:", error); | ||
return NextResponse.json({ message: "Failed to fetch users", status: 500 }, { status: 500 }); | ||
} | ||
} |
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
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