@@ -40,7 +40,7 @@ const CreateTemplateSchema = z.object({
4040 about : z . string ( ) . optional ( ) , // Markdown long description
4141 } )
4242 . optional ( ) ,
43- creatorId : z . string ( ) . optional ( ) , // Creator profile ID
43+ creatorId : z . string ( ) . min ( 1 , ' Creator profile is required' ) ,
4444 tags : z . array ( z . string ( ) ) . max ( 10 , 'Maximum 10 tags allowed' ) . optional ( ) . default ( [ ] ) ,
4545} )
4646
@@ -204,50 +204,47 @@ export async function POST(request: NextRequest) {
204204 return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
205205 }
206206
207- // Validate creator profile if provided
208- if ( data . creatorId ) {
209- // Verify the creator profile exists and user has access
210- const creatorProfile = await db
211- . select ( )
212- . from ( templateCreators )
213- . where ( eq ( templateCreators . id , data . creatorId ) )
214- . limit ( 1 )
207+ // Validate creator profile - required for all templates
208+ const creatorProfile = await db
209+ . select ( )
210+ . from ( templateCreators )
211+ . where ( eq ( templateCreators . id , data . creatorId ) )
212+ . limit ( 1 )
213+
214+ if ( creatorProfile . length === 0 ) {
215+ logger . warn ( `[${ requestId } ] Creator profile not found: ${ data . creatorId } ` )
216+ return NextResponse . json ( { error : 'Creator profile not found' } , { status : 404 } )
217+ }
218+
219+ const creator = creatorProfile [ 0 ]
215220
216- if ( creatorProfile . length === 0 ) {
217- logger . warn ( `[${ requestId } ] Creator profile not found: ${ data . creatorId } ` )
218- return NextResponse . json ( { error : 'Creator profile not found' } , { status : 404 } )
221+ // Verify user has permission to use this creator profile
222+ if ( creator . referenceType === 'user' ) {
223+ if ( creator . referenceId !== session . user . id ) {
224+ logger . warn ( `[${ requestId } ] User cannot use creator profile: ${ data . creatorId } ` )
225+ return NextResponse . json (
226+ { error : 'You do not have permission to use this creator profile' } ,
227+ { status : 403 }
228+ )
219229 }
230+ } else if ( creator . referenceType === 'organization' ) {
231+ // Verify user is a member of the organization
232+ const membership = await db
233+ . select ( )
234+ . from ( member )
235+ . where (
236+ and ( eq ( member . userId , session . user . id ) , eq ( member . organizationId , creator . referenceId ) )
237+ )
238+ . limit ( 1 )
220239
221- const creator = creatorProfile [ 0 ]
222-
223- // Verify user has permission to use this creator profile
224- if ( creator . referenceType === 'user' ) {
225- if ( creator . referenceId !== session . user . id ) {
226- logger . warn ( `[${ requestId } ] User cannot use creator profile: ${ data . creatorId } ` )
227- return NextResponse . json (
228- { error : 'You do not have permission to use this creator profile' } ,
229- { status : 403 }
230- )
231- }
232- } else if ( creator . referenceType === 'organization' ) {
233- // Verify user is a member of the organization
234- const membership = await db
235- . select ( )
236- . from ( member )
237- . where (
238- and ( eq ( member . userId , session . user . id ) , eq ( member . organizationId , creator . referenceId ) )
239- )
240- . limit ( 1 )
241-
242- if ( membership . length === 0 ) {
243- logger . warn (
244- `[${ requestId } ] User not a member of organization for creator: ${ data . creatorId } `
245- )
246- return NextResponse . json (
247- { error : 'You must be a member of the organization to use its creator profile' } ,
248- { status : 403 }
249- )
250- }
240+ if ( membership . length === 0 ) {
241+ logger . warn (
242+ `[${ requestId } ] User not a member of organization for creator: ${ data . creatorId } `
243+ )
244+ return NextResponse . json (
245+ { error : 'You must be a member of the organization to use its creator profile' } ,
246+ { status : 403 }
247+ )
251248 }
252249 }
253250
@@ -307,7 +304,7 @@ export async function POST(request: NextRequest) {
307304 workflowId : data . workflowId ,
308305 name : data . name ,
309306 details : data . details || null ,
310- creatorId : data . creatorId || null ,
307+ creatorId : data . creatorId ,
311308 views : 0 ,
312309 stars : 0 ,
313310 status : 'pending' as const , // All new templates start as pending
0 commit comments