@@ -6,7 +6,7 @@ import { type NextRequest, NextResponse } from 'next/server'
66import { updateInboxConfigContract } from '@/lib/api/contracts/inbox'
77import { parseRequest } from '@/lib/api/server'
88import { getSession } from '@/lib/auth'
9- import { hasInboxAccess } from '@/lib/billing/core/subscription'
9+ import { hasWorkspaceInboxAccess } from '@/lib/billing/core/subscription'
1010import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1111import { disableInbox , enableInbox , updateInboxAddress } from '@/lib/mothership/inbox/lifecycle'
1212import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
@@ -21,18 +21,12 @@ export const GET = withRouteHandler(
2121 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
2222 }
2323
24- const [ hasAccess , permission ] = await Promise . all ( [
25- hasInboxAccess ( session . user . id ) ,
26- getUserEntityPermissions ( session . user . id , 'workspace' , workspaceId ) ,
27- ] )
28- if ( ! hasAccess ) {
29- return NextResponse . json ( { error : 'Sim Mailer requires a Max plan' } , { status : 403 } )
30- }
24+ const permission = await getUserEntityPermissions ( session . user . id , 'workspace' , workspaceId )
3125 if ( ! permission ) {
3226 return NextResponse . json ( { error : 'Not found' } , { status : 404 } )
3327 }
3428
35- const [ wsResult , statsResult ] = await Promise . all ( [
29+ const [ wsResult , statsResult , entitled ] = await Promise . all ( [
3630 db
3731 . select ( {
3832 inboxEnabled : workspace . inboxEnabled ,
@@ -49,6 +43,7 @@ export const GET = withRouteHandler(
4943 . from ( mothershipInboxTask )
5044 . where ( eq ( mothershipInboxTask . workspaceId , workspaceId ) )
5145 . groupBy ( mothershipInboxTask . status ) ,
46+ hasWorkspaceInboxAccess ( workspaceId ) ,
5247 ] )
5348
5449 const [ ws ] = wsResult
@@ -73,6 +68,7 @@ export const GET = withRouteHandler(
7368 return NextResponse . json ( {
7469 enabled : ws . inboxEnabled ,
7570 address : ws . inboxAddress ,
71+ entitled,
7672 taskStats : stats ,
7773 } )
7874 }
@@ -86,21 +82,24 @@ export const PATCH = withRouteHandler(
8682 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
8783 }
8884
89- const [ hasAccess , permission ] = await Promise . all ( [
90- hasInboxAccess ( session . user . id ) ,
91- getUserEntityPermissions ( session . user . id , 'workspace' , workspaceId ) ,
92- ] )
93- if ( ! hasAccess ) {
94- return NextResponse . json ( { error : 'Sim Mailer requires a Max plan' } , { status : 403 } )
95- }
85+ const permission = await getUserEntityPermissions ( session . user . id , 'workspace' , workspaceId )
9686 if ( permission !== 'admin' ) {
9787 return NextResponse . json ( { error : 'Admin access required' } , { status : 403 } )
9888 }
9989
90+ const parsed = await parseRequest ( updateInboxConfigContract , req , context )
91+ if ( ! parsed . success ) return parsed . response
92+ const body = parsed . data . body
93+
10094 try {
101- const parsed = await parseRequest ( updateInboxConfigContract , req , context )
102- if ( ! parsed . success ) return parsed . response
103- const body = parsed . data . body
95+ if ( body . enabled === false ) {
96+ await disableInbox ( workspaceId )
97+ return NextResponse . json ( { enabled : false , address : null } )
98+ }
99+
100+ if ( ! ( await hasWorkspaceInboxAccess ( workspaceId ) ) ) {
101+ return NextResponse . json ( { error : 'Sim Mailer requires a Max plan' } , { status : 403 } )
102+ }
104103
105104 if ( body . enabled === true ) {
106105 const [ current ] = await db
@@ -115,11 +114,6 @@ export const PATCH = withRouteHandler(
115114 return NextResponse . json ( config )
116115 }
117116
118- if ( body . enabled === false ) {
119- await disableInbox ( workspaceId )
120- return NextResponse . json ( { enabled : false , address : null } )
121- }
122-
123117 if ( body . username ) {
124118 const config = await updateInboxAddress ( workspaceId , body . username )
125119 return NextResponse . json ( config )
0 commit comments