File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { NextResponse } from "next/server" ;
2- import { appendEmailToSheet } from "@/lib/services/google-sheets " ;
3- import { verifyEmail } from "@devmehq/email-validator-js " ;
2+ import { customErrorHandler } from "@/lib/utils/error " ;
3+ import { subscribeEmail } from "@/lib/services/subscribe " ;
44
55export async function POST ( req : Request ) {
66 try {
@@ -9,26 +9,11 @@ export async function POST(req: Request) {
99 if ( ! email ) {
1010 return NextResponse . json ( { error : "Email is required" } , { status : 400 } ) ;
1111 }
12+ await subscribeEmail ( email ) ;
1213
13- const result = await verifyEmail ( {
14- emailAddress : email ,
15- verifyMx : true ,
16- verifySmtp : false ,
17- timeout : 4000
18- } ) ;
19-
20- if ( ! result . validFormat ) {
21- return NextResponse . json ( { error : "Invalid email format" } , { status : 400 } ) ;
22- }
23-
24- if ( ! result . validMx ) {
25- return NextResponse . json ( { error : "Email domain is invalid" } , { status : 400 } ) ;
26- }
27-
28- await appendEmailToSheet ( email ) ;
2914 return NextResponse . json ( { message : "Email added successfully" } ) ;
3015 } catch ( error ) {
3116 console . error ( "Error adding email:" , error ) ;
32- return NextResponse . json ( { error : "Failed to add email" } , { status : 500 } ) ;
17+ return customErrorHandler ( error , "Failed to add email" ) ;
3318 }
3419}
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ import toast from "react-hot-toast";
1818
1919type SubscribeResponse = {
2020 success ?: boolean ;
21- error ?: string ;
21+ message ?: string ;
2222} ;
2323
2424export default function Footer ( ) {
@@ -44,7 +44,7 @@ export default function Footer() {
4444 } )
4545 . then ( async ( res ) => {
4646 const data = ( await res . json ( ) ) as SubscribeResponse ;
47- if ( ! res . ok ) throw new Error ( data . error ?? "Something went wrong." ) ;
47+ if ( ! res . ok ) throw new Error ( data . message ?? "Something went wrong." ) ;
4848 return data ;
4949 } ) ,
5050 {
Original file line number Diff line number Diff line change 11import { connectToDatabase } from "@/lib/database/mongoose" ;
2- import { Course } from "@/db/course" ;
32import { IRelatedSubject } from "@/interface" ;
43import { escapeRegExp } from "@/lib/utils/regex" ;
4+ import { Course } from "@/db/course" ;
55import RelatedSubject from "@/db/relatedSubjects" ;
66
77export async function getCourseList ( ) {
88 await connectToDatabase ( ) ;
99 return await Course . find ( ) . lean ( ) ;
1010}
11+
1112export async function getRelatedSubjects ( subject : string ) {
1213 await connectToDatabase ( ) ;
1314 const escapedSubject = escapeRegExp ( subject ) ;
@@ -16,4 +17,4 @@ export async function getRelatedSubjects(subject: string) {
1617 } ) ;
1718
1819 return subjects [ 0 ] ?. related_subjects ?? [ ] ;
19- }
20+ }
Original file line number Diff line number Diff line change 1+ import { verifyEmail } from "@devmehq/email-validator-js" ;
2+ import { appendEmailToSheet } from "@/lib/services/google-sheets" ;
3+ import { CustomError } from "@/lib/utils/error" ;
4+
5+ export async function subscribeEmail ( email : string ) {
6+ const result = await verifyEmail ( {
7+ emailAddress : email ,
8+ verifyMx : true ,
9+ verifySmtp : false ,
10+ timeout : 4000
11+ } ) ;
12+
13+ if ( ! result . validFormat ) {
14+ throw new CustomError ( "Invalid email format" , 400 ) ;
15+ }
16+
17+ if ( ! result . validMx ) {
18+ throw new CustomError ( "Email domain is invalid" , 400 ) ;
19+ }
20+
21+ await appendEmailToSheet ( email ) ;
22+ }
You can’t perform that action at this time.
0 commit comments