11import { createLogger } from '@sim/logger'
22import { type NextRequest , NextResponse } from 'next/server'
3- import { z } from 'zod'
3+ import { gmailEditDraftContract } from '@/lib/api/contracts/google-tools'
4+ import { parseRequest } from '@/lib/api/server'
45import { checkInternalAuth } from '@/lib/auth/hybrid'
56import { generateRequestId } from '@/lib/core/utils/request'
67import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
7- import { RawFileInputArraySchema } from '@/lib/uploads/utils/file-schemas'
88import { processFilesToUserFiles } from '@/lib/uploads/utils/file-utils'
99import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server'
1010import {
@@ -19,20 +19,6 @@ export const dynamic = 'force-dynamic'
1919
2020const logger = createLogger ( 'GmailEditDraftAPI' )
2121
22- const GmailEditDraftSchema = z . object ( {
23- accessToken : z . string ( ) . min ( 1 , 'Access token is required' ) ,
24- draftId : z . string ( ) . min ( 1 , 'Draft ID is required' ) ,
25- to : z . string ( ) . min ( 1 , 'Recipient email is required' ) ,
26- subject : z . string ( ) . optional ( ) . nullable ( ) ,
27- body : z . string ( ) . min ( 1 , 'Email body is required' ) ,
28- contentType : z . enum ( [ 'text' , 'html' ] ) . optional ( ) . nullable ( ) ,
29- threadId : z . string ( ) . optional ( ) . nullable ( ) ,
30- replyToMessageId : z . string ( ) . optional ( ) . nullable ( ) ,
31- cc : z . string ( ) . optional ( ) . nullable ( ) ,
32- bcc : z . string ( ) . optional ( ) . nullable ( ) ,
33- attachments : RawFileInputArraySchema . optional ( ) . nullable ( ) ,
34- } )
35-
3622export const POST = withRouteHandler ( async ( request : NextRequest ) => {
3723 const requestId = generateRequestId ( )
3824
@@ -55,8 +41,9 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
5541 { userId : authResult . userId }
5642 )
5743
58- const body = await request . json ( )
59- const validatedData = GmailEditDraftSchema . parse ( body )
44+ const parsed = await parseRequest ( gmailEditDraftContract , request , { } )
45+ if ( ! parsed . success ) return parsed . response
46+ const validatedData = parsed . data . body
6047
6148 logger . info ( `[${ requestId } ] Updating Gmail draft` , {
6249 draftId : validatedData . draftId ,
@@ -180,18 +167,6 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
180167 } ,
181168 } )
182169 } catch ( error ) {
183- if ( error instanceof z . ZodError ) {
184- logger . warn ( `[${ requestId } ] Invalid request data` , { errors : error . errors } )
185- return NextResponse . json (
186- {
187- success : false ,
188- error : 'Invalid request data' ,
189- details : error . errors ,
190- } ,
191- { status : 400 }
192- )
193- }
194-
195170 logger . error ( `[${ requestId } ] Error updating Gmail draft:` , error )
196171
197172 return NextResponse . json (
0 commit comments