@@ -6,9 +6,16 @@ import { validateJiraCloudId } from '@/lib/core/security/input-validation'
66import { generateRequestId } from '@/lib/core/utils/request'
77import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
88import { resolveAtlassianSelectorCredential } from '@/lib/selectors/application/atlassian-credential'
9- import { resolveAuthorizedSelectorContext } from '@/lib/selectors/server/resolve-authorized-context'
9+ import {
10+ resolveSelectorProviderValue ,
11+ SELECTOR_ATLASSIAN_DISCOVERY_OPTIONS ,
12+ selectorProviderFailure ,
13+ } from '@/lib/selectors/server/provider-errors'
14+ import {
15+ authenticateSelectorRequest ,
16+ resolveAuthorizedSelectorContext ,
17+ } from '@/lib/selectors/server/resolve-authorized-context'
1018import { getConfluenceCloudId } from '@/tools/confluence/utils'
11- import { parseAtlassianErrorMessage } from '@/tools/jira/utils'
1219
1320const logger = createLogger ( 'ConfluenceSelectorSpacesAPI' )
1421
@@ -58,11 +65,15 @@ function parseCursor(raw: string | undefined): { status: SpaceStatus; inner?: st
5865export const POST = withRouteHandler ( async ( request : NextRequest ) => {
5966 const requestId = generateRequestId ( )
6067 try {
68+ const authentication = await authenticateSelectorRequest ( request )
69+ if ( ! authentication . ok ) {
70+ return NextResponse . json ( { error : authentication . error } , { status : authentication . status } )
71+ }
6172 const parsed = await parseRequest ( confluenceSpacesSelectorContract , request , { } )
6273 if ( ! parsed . success ) return parsed . response
6374
6475 const { credential, workflowId, domain : domainReference , cursor, spaceKey } = parsed . data . body
65- const resolution = await resolveAuthorizedSelectorContext ( request , {
76+ const resolution = await resolveAuthorizedSelectorContext ( authentication . principal , {
6677 workflowId,
6778 credentialId : credential ,
6879 context : { domain : domainReference } ,
@@ -78,6 +89,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
7889 credentialId : credential ,
7990 credentialOwnerUserId,
8091 requestId,
92+ serviceId : 'confluence' ,
8193 } )
8294 if ( ! bundle ) {
8395 return NextResponse . json (
@@ -87,7 +99,20 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
8799 }
88100 const domain = resolution . context . domain as string
89101 const accessToken = bundle . accessToken
90- const cloudId = bundle . cloudId || ( await getConfluenceCloudId ( domain , accessToken ) )
102+ const cloudIdResolution = await resolveSelectorProviderValue ( 'Confluence' , async ( ) =>
103+ bundle . cloudId
104+ ? bundle . cloudId
105+ : getConfluenceCloudId ( domain , accessToken , SELECTOR_ATLASSIAN_DISCOVERY_OPTIONS )
106+ )
107+ if ( ! cloudIdResolution . ok ) {
108+ logger . warn ( 'Confluence selector discovery failed' , {
109+ status : cloudIdResolution . upstreamStatus ?? 'unknown' ,
110+ } )
111+ return NextResponse . json ( cloudIdResolution . failure , {
112+ status : cloudIdResolution . failure . status ,
113+ } )
114+ }
115+ const cloudId = cloudIdResolution . value
91116
92117 const cloudIdValidation = validateJiraCloudId ( cloudId , 'cloudId' )
93118 if ( ! cloudIdValidation . isValid ) {
@@ -106,10 +131,12 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
106131 } )
107132
108133 if ( ! response . ok ) {
109- const errorText = await response . text ( )
110- const message = parseAtlassianErrorMessage ( response . status , response . statusText , errorText )
111- logger . error ( 'Confluence API error response' , { error : message , status : response . status } )
112- return { ok : false , response : NextResponse . json ( { error : message } , { status : 502 } ) }
134+ logger . warn ( 'Confluence selector spaces request failed' , { status : response . status } )
135+ const failure = selectorProviderFailure ( 'Confluence' , response . status )
136+ return {
137+ ok : false ,
138+ response : NextResponse . json ( failure , { status : failure . status } ) ,
139+ }
113140 }
114141
115142 return { ok : true , data : await response . json ( ) }
0 commit comments