@@ -344,4 +344,104 @@ export default {
344344 getUpgradeRecommendations,
345345 PLAN_ENFORCEMENT_ERRORS ,
346346 isPlanEnforcementError,
347- } ;
347+ } ;
348+
349+ // Compatibility implementations for legacy test/api usage
350+ // Legacy helpers used by older tests and route handlers
351+ export async function checkProductCreationLimit ( request : NextRequest ) : Promise < null | NextResponse > {
352+ try {
353+ const session = await getSessionFromRequest ( request ) ;
354+ if ( ! session ) {
355+ return NextResponse . json ( { error : { code : 'UNAUTHORIZED' , message : 'Authentication required' } } , { status : 401 } ) ;
356+ }
357+
358+ const storeId = ( session as any ) . storeId ;
359+ if ( ! storeId ) {
360+ return NextResponse . json ( { error : { code : 'MISSING_STORE_ID' , message : 'Store ID is required' } } , { status : 400 } ) ;
361+ }
362+
363+ try {
364+ const result = await SubscriptionService . canCreateProduct ( storeId ) ;
365+ if ( result && ( result as any ) . allowed ) {
366+ return null ;
367+ }
368+
369+ return NextResponse . json ( { error : { code : 'PLAN_LIMIT_EXCEEDED' , message : 'Product creation limit exceeded for your current plan' } } , { status : 403 } ) ;
370+ } catch ( err ) {
371+ console . error ( 'checkProductCreationLimit error' , err ) ;
372+ return NextResponse . json ( { error : { code : 'PLAN_CHECK_FAILED' , message : 'Failed to check plan limits' } } , { status : 500 } ) ;
373+ }
374+ } catch ( err ) {
375+ console . error ( 'checkProductCreationLimit unexpected error' , err ) ;
376+ return NextResponse . json ( { error : { code : 'ENFORCEMENT_ERROR' , message : 'Failed to check plan limits' } } , { status : 500 } ) ;
377+ }
378+ }
379+
380+ export async function checkOrderCreationLimit ( request : NextRequest ) : Promise < null | NextResponse > {
381+ try {
382+ const session = await getSessionFromRequest ( request ) ;
383+ if ( ! session ) {
384+ return NextResponse . json ( { error : { code : 'UNAUTHORIZED' , message : 'Authentication required' } } , { status : 401 } ) ;
385+ }
386+
387+ const storeId = ( session as any ) . storeId ;
388+ if ( ! storeId ) {
389+ return NextResponse . json ( { error : { code : 'MISSING_STORE_ID' , message : 'Store ID is required' } } , { status : 400 } ) ;
390+ }
391+
392+ try {
393+ const result = await SubscriptionService . canCreateOrder ( storeId ) ;
394+ if ( result && ( result as any ) . allowed ) {
395+ return null ;
396+ }
397+
398+ return NextResponse . json ( { error : { code : 'PLAN_LIMIT_EXCEEDED' , message : 'Order creation limit exceeded for your current plan' } } , { status : 403 } ) ;
399+ } catch ( err ) {
400+ console . error ( 'checkOrderCreationLimit error' , err ) ;
401+ return NextResponse . json ( { error : { code : 'PLAN_CHECK_FAILED' , message : 'Failed to check plan limits' } } , { status : 500 } ) ;
402+ }
403+ } catch ( err ) {
404+ console . error ( 'checkOrderCreationLimit unexpected error' , err ) ;
405+ return NextResponse . json ( { error : { code : 'ENFORCEMENT_ERROR' , message : 'Failed to check plan limits' } } , { status : 500 } ) ;
406+ }
407+ }
408+
409+ export async function enforcePlanLimits (
410+ request : NextRequest ,
411+ handler : ( request : NextRequest , context ?: any ) => Promise < NextResponse > ,
412+ resource : 'product' | 'order'
413+ ) : Promise < NextResponse > {
414+ // Validate resource
415+ if ( resource !== 'product' && resource !== 'order' ) {
416+ return NextResponse . json ( { error : { code : 'INVALID_RESOURCE_TYPE' , message : 'Invalid resource type for plan enforcement' } } , { status : 400 } ) ;
417+ }
418+
419+ // Allow safe methods without enforcement
420+ const method = ( request && ( request as any ) . method ) || 'GET' ;
421+ if ( [ 'GET' , 'PUT' , 'PATCH' , 'DELETE' ] . includes ( method ) ) {
422+ return await handler ( request ) ;
423+ }
424+
425+ // Run appropriate check
426+ if ( resource === 'product' ) {
427+ const check = await checkProductCreationLimit ( request ) ;
428+ if ( check ) return check as NextResponse ;
429+ } else {
430+ const check = await checkOrderCreationLimit ( request ) ;
431+ if ( check ) return check as NextResponse ;
432+ }
433+
434+ // Allowed, call handler
435+ return await handler ( request ) ;
436+ }
437+
438+ export function withPlanLimits (
439+ handler : ( request : NextRequest , context ?: any ) => Promise < NextResponse > ,
440+ resource : 'product' | 'order'
441+ ) {
442+ return async ( request : NextRequest , context ?: any ) => {
443+ // Delegate to enforcePlanLimits which will call the handler when allowed
444+ const result = await enforcePlanLimits ( request , ( req ) => handler ( req , context ) , resource ) ;
445+ return result ;
446+ } ;
447+ }
0 commit comments