d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
123 lines
4.0 KiB
TypeScript
123 lines
4.0 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { getErrorMessage, toError } from '@sim/utils/errors'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { jsmSearchObjectsAqlContract } from '@/lib/api/contracts/selectors/jsm'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import { checkInternalAuth } from '@/lib/auth/hybrid'
|
|
import {
|
|
validateAssetsWorkspaceId,
|
|
validateJiraCloudId,
|
|
} from '@/lib/core/security/input-validation'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { parseAtlassianErrorMessage } from '@/tools/jira/utils'
|
|
import {
|
|
getAssetsApiBaseUrl,
|
|
getJsmHeaders,
|
|
mapAssetObject,
|
|
resolveAssetsContext,
|
|
} from '@/tools/jsm/utils'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
const logger = createLogger('JsmAssetsSearchAPI')
|
|
|
|
/** Coerce a string|number|boolean param into a number, falling back when unset */
|
|
function toNumber(value: string | number | undefined, fallback: number): number {
|
|
if (value === undefined) return fallback
|
|
const parsed = typeof value === 'number' ? value : Number(value)
|
|
return Number.isFinite(parsed) ? parsed : fallback
|
|
}
|
|
|
|
export const POST = withRouteHandler(async (request: NextRequest) => {
|
|
const auth = await checkInternalAuth(request)
|
|
if (!auth.success || !auth.userId) {
|
|
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
try {
|
|
const parsed = await parseRequest(jsmSearchObjectsAqlContract, request, {})
|
|
if (!parsed.success) return parsed.response
|
|
|
|
const {
|
|
domain,
|
|
accessToken,
|
|
cloudId: cloudIdParam,
|
|
workspaceId: workspaceIdParam,
|
|
qlQuery,
|
|
page,
|
|
resultsPerPage,
|
|
includeAttributes,
|
|
objectTypeId,
|
|
objectSchemaId,
|
|
} = parsed.data.body
|
|
|
|
const { cloudId, workspaceId } = await resolveAssetsContext(
|
|
domain,
|
|
accessToken,
|
|
cloudIdParam,
|
|
workspaceIdParam
|
|
)
|
|
|
|
const cloudIdValidation = validateJiraCloudId(cloudId, 'cloudId')
|
|
if (!cloudIdValidation.isValid) {
|
|
return NextResponse.json({ error: cloudIdValidation.error }, { status: 400 })
|
|
}
|
|
|
|
const workspaceIdValidation = validateAssetsWorkspaceId(workspaceId, 'workspaceId')
|
|
if (!workspaceIdValidation.isValid) {
|
|
return NextResponse.json({ error: workspaceIdValidation.error }, { status: 400 })
|
|
}
|
|
|
|
const includeAttrs =
|
|
includeAttributes === undefined ? true : String(includeAttributes) === 'true'
|
|
|
|
const body: Record<string, unknown> = {
|
|
qlQuery,
|
|
page: toNumber(page, 1),
|
|
resultsPerPage: toNumber(resultsPerPage, 25),
|
|
includeAttributes: includeAttrs,
|
|
}
|
|
if (objectTypeId) body.objectTypeId = objectTypeId
|
|
if (objectSchemaId) body.objectSchemaId = objectSchemaId
|
|
|
|
const url = `${getAssetsApiBaseUrl(cloudId, workspaceId)}/object/aql`
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: getJsmHeaders(accessToken),
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text()
|
|
logger.error('Assets API error running AQL search', { status: response.status, errorText })
|
|
return NextResponse.json(
|
|
{
|
|
error: parseAtlassianErrorMessage(response.status, response.statusText, errorText),
|
|
details: errorText,
|
|
},
|
|
{ status: response.status }
|
|
)
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
output: {
|
|
ts: new Date().toISOString(),
|
|
objects: Array.isArray(data.objectEntries) ? data.objectEntries.map(mapAssetObject) : [],
|
|
total: data.totalFilterCount ?? (data.objectEntries?.length || 0),
|
|
pageNumber: data.pageNumber ?? 1,
|
|
pageSize: data.pageSize ?? (data.objectEntries?.length || 0),
|
|
},
|
|
})
|
|
} catch (error) {
|
|
logger.error('Error running Assets AQL search', { error: toError(error).message })
|
|
return NextResponse.json(
|
|
{ error: getErrorMessage(error, 'Internal server error'), success: false },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|