diff --git a/apps/app/src/app/api/cloud-tests/findings/route.ts b/apps/app/src/app/api/cloud-tests/findings/route.ts index b0f3ebe2b..d790583f3 100644 --- a/apps/app/src/app/api/cloud-tests/findings/route.ts +++ b/apps/app/src/app/api/cloud-tests/findings/route.ts @@ -41,22 +41,39 @@ export async function GET(request: NextRequest) { const newConnections = await db.integrationConnection.findMany({ where: { organizationId: orgId, + status: 'active', provider: { slug: { in: CLOUD_PROVIDER_SLUGS, }, }, }, - select: { - id: true, - provider: { - select: { - slug: true, - }, + include: { + provider: true, + }, + }); + + // ==================================================================== + // Fetch from OLD integration table (Integration) - for backward compat + // ==================================================================== + const legacyIntegrations = await db.integration.findMany({ + where: { + organizationId: orgId, + integrationId: { + in: CLOUD_PROVIDER_SLUGS, }, }, }); + // Filter out legacy integrations that have been migrated to new platform + const newConnectionSlugs = new Set(newConnections.map((c) => c.provider.slug)); + const activeLegacyIntegrations = legacyIntegrations.filter( + (i) => !newConnectionSlugs.has(i.integrationId), + ); + + // ==================================================================== + // Fetch findings from NEW platform (IntegrationCheckResult) + // ==================================================================== const newConnectionIds = newConnections.map((c) => c.id); const connectionToSlug = Object.fromEntries(newConnections.map((c) => [c.id, c.provider.slug])); @@ -77,13 +94,12 @@ export async function GET(request: NextRequest) { const latestRunIds = latestRuns.map((r) => r.id); const checkRunMap = Object.fromEntries(latestRuns.map((cr) => [cr.id, cr])); - // Fetch only failed results from the latest runs (findings only, no passing results) + // Fetch results only from the latest runs (both passed and failed) const newResults = latestRunIds.length > 0 ? await db.integrationCheckResult.findMany({ where: { checkRunId: { in: latestRunIds }, - passed: false, }, select: { id: true, @@ -93,6 +109,7 @@ export async function GET(request: NextRequest) { severity: true, collectedAt: true, checkRunId: true, + passed: true, }, orderBy: { collectedAt: 'desc', @@ -107,33 +124,26 @@ export async function GET(request: NextRequest) { title: result.title, description: result.description, remediation: result.remediation, - status: 'failed', + status: result.passed ? 'passed' : 'failed', severity: result.severity, completedAt: result.collectedAt, integration: { - integrationId: checkRun - ? connectionToSlug[checkRun.connectionId] || 'unknown' - : 'unknown', + integrationId: checkRun ? connectionToSlug[checkRun.connectionId] || 'unknown' : 'unknown', }, }; }); // ==================================================================== - // Fetch from OLD integration platform + // Fetch findings from OLD platform (IntegrationResult) // ==================================================================== - // Filter out cloud providers that have migrated to new platform - const newConnectionSlugs = new Set(newConnections.map((c) => c.provider.slug)); - const legacySlugs = CLOUD_PROVIDER_SLUGS.filter((s) => !newConnectionSlugs.has(s)); + const legacyIntegrationIds = activeLegacyIntegrations.map((i) => i.id); const legacyResults = - legacySlugs.length > 0 + legacyIntegrationIds.length > 0 ? await db.integrationResult.findMany({ where: { - organizationId: orgId, - integration: { - integrationId: { - in: legacySlugs, - }, + integrationId: { + in: legacyIntegrationIds, }, }, select: { @@ -171,7 +181,7 @@ export async function GET(request: NextRequest) { })); // ==================================================================== - // Merge and sort by date + // Merge all findings and sort by date // ==================================================================== const findings = [...newFindings, ...legacyFindings].sort((a, b) => { const dateA = a.completedAt ? new Date(a.completedAt).getTime() : 0;