Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions apps/app/src/app/api/cloud-tests/findings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));

Expand All @@ -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,
Expand All @@ -93,6 +109,7 @@ export async function GET(request: NextRequest) {
severity: true,
collectedAt: true,
checkRunId: true,
passed: true,
},
orderBy: {
collectedAt: 'desc',
Expand All @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down