Skip to content
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
23 changes: 4 additions & 19 deletions src/app/api/subscribe/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextResponse } from "next/server";
import { appendEmailToSheet } from "@/lib/services/google-sheets";
import { verifyEmail } from "@devmehq/email-validator-js";
import { customErrorHandler } from "@/lib/utils/error";
import { subscribeEmail } from "@/lib/services/subscribe";

export async function POST(req: Request) {
try {
Expand All @@ -9,26 +9,11 @@ export async function POST(req: Request) {
if (!email) {
return NextResponse.json({ error: "Email is required" }, { status: 400 });
}
await subscribeEmail(email);

const result = await verifyEmail({
emailAddress: email,
verifyMx: true,
verifySmtp: false,
timeout: 4000
});

if (!result.validFormat) {
return NextResponse.json({ error: "Invalid email format" }, { status: 400 });
}

if (!result.validMx) {
return NextResponse.json({ error: "Email domain is invalid" }, { status: 400 });
}

await appendEmailToSheet(email);
return NextResponse.json({ message: "Email added successfully" });
} catch (error) {
console.error("Error adding email:", error);
return NextResponse.json({ error: "Failed to add email" }, { status: 500 });
return customErrorHandler(error, "Failed to add email");
}
}
4 changes: 2 additions & 2 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import toast from "react-hot-toast";

type SubscribeResponse = {
success?: boolean;
error?: string;
message?: string;
};

export default function Footer() {
Expand All @@ -44,7 +44,7 @@ export default function Footer() {
})
.then(async (res) => {
const data = (await res.json()) as SubscribeResponse;
if (!res.ok) throw new Error(data.error ?? "Something went wrong.");
if (!res.ok) throw new Error(data.message ?? "Something went wrong.");
return data;
}),
{
Expand Down
5 changes: 3 additions & 2 deletions src/lib/services/subject.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { connectToDatabase } from "@/lib/database/mongoose";
import { Course } from "@/db/course";
import { IRelatedSubject } from "@/interface";
import { escapeRegExp } from "@/lib/utils/regex";
import { Course } from "@/db/course";
import RelatedSubject from "@/db/relatedSubjects";

export async function getCourseList(){
await connectToDatabase();
return await Course.find().lean();
}

export async function getRelatedSubjects(subject: string) {
await connectToDatabase();
const escapedSubject = escapeRegExp(subject);
Expand All @@ -16,4 +17,4 @@ export async function getRelatedSubjects(subject: string) {
});

return subjects[0]?.related_subjects ?? [];
}
}
22 changes: 22 additions & 0 deletions src/lib/services/subscribe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { verifyEmail } from "@devmehq/email-validator-js";
import { appendEmailToSheet } from "@/lib/services/google-sheets";
import { CustomError } from "@/lib/utils/error";

export async function subscribeEmail(email: string) {
const result = await verifyEmail({
emailAddress: email,
verifyMx: true,
verifySmtp: false,
timeout: 4000
});

if (!result.validFormat) {
throw new CustomError("Invalid email format", 400);
}

if (!result.validMx) {
throw new CustomError("Email domain is invalid", 400);
}

await appendEmailToSheet(email);
}