@@ -7,27 +7,26 @@ class ApiKeyGuard {
77 const request = context . switchToHttp ( ) . getRequest ( ) ;
88 const apiKey = request . headers [ 'authorization' ] ?. replace ( 'Bearer ' , '' ) ;
99 const expectedKey = process . env . API_KEY_SECRET ;
10-
10+
1111 if ( ! expectedKey ) {
1212 throw new Error ( 'API_KEY_SECRET not configured' ) ;
1313 }
14-
14+
1515 return apiKey === expectedKey ;
1616 }
1717}
1818
1919@UseGuards ( ApiKeyGuard )
2020@Controller ( 'email' )
2121export class EmailController {
22- constructor ( private readonly emailService : EmailService ) { }
22+ constructor ( private readonly emailService : EmailService ) { }
2323
2424 @Post ( 'send-prompt' )
2525 @HttpCode ( HttpStatus . OK )
2626 async sendPrompt (
2727 @Body ( ) body : { email : string ; prompt ?: string ; userId ?: string } ,
28- @Headers ( 'authorization' ) auth : string ,
2928 ) {
30- const { email, prompt, userId } = body ;
29+ const { email, prompt } = body ;
3130
3231 // Validate required fields
3332 if ( ! email ) {
@@ -40,9 +39,14 @@ export class EmailController {
4039 throw new UnauthorizedException ( 'Valid email is required' ) ;
4140 }
4241
42+ const user = await this . emailService [ 'prisma' ] . user . findUnique ( { where : { email } } ) ;
43+ if ( ! user ) {
44+ throw new UnauthorizedException ( 'User not found' ) ;
45+ } ;
46+
4347 try {
4448 const promptContent = prompt || this . getDefaultPrompt ( ) ;
45-
49+
4650 const result = await this . emailService . sendPrompt ( email , promptContent ) ;
4751
4852 return {
@@ -72,7 +76,7 @@ export class EmailController {
7276 'What are you looking forward to tomorrow?' ,
7377 'What was the most meaningful part of your day?' ,
7478 ] ;
75-
79+
7680 return prompts [ Math . floor ( Math . random ( ) * prompts . length ) ] ;
7781 }
7882}
0 commit comments