mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-07 22:24:06 -05:00
145 lines
3.9 KiB
TypeScript
145 lines
3.9 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { NextResponse } from 'next/server'
|
|
import { getJiraCloudId, getJsmApiBaseUrl, getJsmHeaders } from '@/tools/jsm/utils'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
const logger = createLogger('JsmRequestAPI')
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.json()
|
|
const {
|
|
domain,
|
|
accessToken,
|
|
cloudId: cloudIdParam,
|
|
issueIdOrKey,
|
|
serviceDeskId,
|
|
requestTypeId,
|
|
summary,
|
|
description,
|
|
raiseOnBehalfOf,
|
|
requestFieldValues,
|
|
} = body
|
|
|
|
if (!domain) {
|
|
logger.error('Missing domain in request')
|
|
return NextResponse.json({ error: 'Domain is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!accessToken) {
|
|
logger.error('Missing access token in request')
|
|
return NextResponse.json({ error: 'Access token is required' }, { status: 400 })
|
|
}
|
|
|
|
const cloudId = cloudIdParam || (await getJiraCloudId(domain, accessToken))
|
|
const baseUrl = getJsmApiBaseUrl(cloudId)
|
|
|
|
const isCreateOperation = serviceDeskId && requestTypeId && summary
|
|
|
|
if (isCreateOperation) {
|
|
const url = `${baseUrl}/request`
|
|
|
|
logger.info('Creating request at:', url)
|
|
|
|
const requestBody: Record<string, unknown> = {
|
|
serviceDeskId,
|
|
requestTypeId,
|
|
requestFieldValues: requestFieldValues || {
|
|
summary,
|
|
...(description && { description }),
|
|
},
|
|
}
|
|
|
|
if (raiseOnBehalfOf) {
|
|
requestBody.raiseOnBehalfOf = raiseOnBehalfOf
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: getJsmHeaders(accessToken),
|
|
body: JSON.stringify(requestBody),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text()
|
|
logger.error('JSM API error:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: errorText,
|
|
})
|
|
|
|
return NextResponse.json(
|
|
{ error: `JSM API error: ${response.status} ${response.statusText}`, details: errorText },
|
|
{ status: response.status }
|
|
)
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
output: {
|
|
ts: new Date().toISOString(),
|
|
issueId: data.issueId,
|
|
issueKey: data.issueKey,
|
|
requestTypeId: data.requestTypeId,
|
|
serviceDeskId: data.serviceDeskId,
|
|
success: true,
|
|
url: `https://${domain}/browse/${data.issueKey}`,
|
|
},
|
|
})
|
|
}
|
|
if (!issueIdOrKey) {
|
|
logger.error('Missing issueIdOrKey in request')
|
|
return NextResponse.json({ error: 'Issue ID or key is required' }, { status: 400 })
|
|
}
|
|
|
|
const url = `${baseUrl}/request/${issueIdOrKey}`
|
|
|
|
logger.info('Fetching request from:', url)
|
|
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: getJsmHeaders(accessToken),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text()
|
|
logger.error('JSM API error:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: errorText,
|
|
})
|
|
|
|
return NextResponse.json(
|
|
{ error: `JSM API error: ${response.status} ${response.statusText}`, details: errorText },
|
|
{ status: response.status }
|
|
)
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
output: {
|
|
ts: new Date().toISOString(),
|
|
request: data,
|
|
},
|
|
})
|
|
} catch (error) {
|
|
logger.error('Error with request operation:', {
|
|
error: error instanceof Error ? error.message : String(error),
|
|
stack: error instanceof Error ? error.stack : undefined,
|
|
})
|
|
|
|
return NextResponse.json(
|
|
{
|
|
error: error instanceof Error ? error.message : 'Internal server error',
|
|
success: false,
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|