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
107 lines
3.6 KiB
TypeScript
107 lines
3.6 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { toError } from '@sim/utils/errors'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { agiloftAttachmentInfoContract } from '@/lib/api/contracts/tools/agiloft'
|
|
import { getValidationErrorMessage, parseRequest } from '@/lib/api/server'
|
|
import { checkInternalAuth } from '@/lib/auth/hybrid'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import type { AgiloftAttachmentInfoResponse } from '@/tools/agiloft/types'
|
|
import { buildAttachmentInfoUrl } from '@/tools/agiloft/utils'
|
|
import { executeAgiloftRequest } from '@/tools/agiloft/utils.server'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
const logger = createLogger('AgiloftAttachmentInfoAPI')
|
|
|
|
export const POST = withRouteHandler(async (request: NextRequest) => {
|
|
const requestId = generateRequestId()
|
|
|
|
try {
|
|
const authResult = await checkInternalAuth(request, { requireWorkflowId: false })
|
|
|
|
if (!authResult.success || !authResult.userId) {
|
|
logger.warn(
|
|
`[${requestId}] Unauthorized Agiloft attachment_info attempt: ${authResult.error}`
|
|
)
|
|
return NextResponse.json(
|
|
{ success: false, error: authResult.error || 'Authentication required' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const parsed = await parseRequest(
|
|
agiloftAttachmentInfoContract,
|
|
request,
|
|
{},
|
|
{
|
|
validationErrorResponse: (error) => {
|
|
logger.warn(`[${requestId}] Invalid request data`, { errors: error.issues })
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: getValidationErrorMessage(error, 'Invalid request data'),
|
|
details: error.issues,
|
|
},
|
|
{ status: 400 }
|
|
)
|
|
},
|
|
}
|
|
)
|
|
if (!parsed.success) return parsed.response
|
|
const params = parsed.data.body
|
|
|
|
const result = await executeAgiloftRequest<AgiloftAttachmentInfoResponse>(
|
|
params,
|
|
(base) => ({
|
|
url: buildAttachmentInfoUrl(base, params),
|
|
method: 'GET',
|
|
}),
|
|
async (response) => {
|
|
if (!response.ok) {
|
|
const errorText = await response.text()
|
|
return {
|
|
success: false,
|
|
output: { attachments: [], totalCount: 0 },
|
|
error: `Agiloft error: ${response.status} - ${errorText}`,
|
|
}
|
|
}
|
|
|
|
const data = (await response.json()) as Record<string, unknown>
|
|
const result = (data.result ?? data) as Record<string, unknown>
|
|
|
|
const attachments: Array<{ position: number; name: string; size: number }> = []
|
|
|
|
if (Array.isArray(result)) {
|
|
for (let i = 0; i < result.length; i++) {
|
|
const item = result[i] as Record<string, unknown>
|
|
attachments.push({
|
|
position: (item.filePosition as number) ?? (item.position as number) ?? i,
|
|
name:
|
|
(item.fileName as string) ??
|
|
(item.name as string) ??
|
|
(item.filename as string) ??
|
|
'',
|
|
size: (item.size as number) ?? (item.fileSize as number) ?? 0,
|
|
})
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: data.success !== false,
|
|
output: {
|
|
attachments,
|
|
totalCount: attachments.length,
|
|
},
|
|
}
|
|
}
|
|
)
|
|
|
|
return NextResponse.json(result)
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Error getting Agiloft attachment info:`, error)
|
|
|
|
return NextResponse.json({ success: false, error: toError(error).message }, { status: 500 })
|
|
}
|
|
})
|