Files
Open_Ultra_SAST/benchmarks/pairs/agent-vfc/supplieradvisor-mvp-route-a69e8f/fixed.ts
T
Marius CiepluchandClaude Opus 5 c2d9c4ad01 feat(learning-harness): absence rows travel with their registration
Eight of the ten vendored access-control rows had no named handler at all,
so every one of them would have scored `missing_context` forever rather
than as a detector miss. After the re-harvest nine are named and the tenth
states its reason.

The handler-context mode now carries the class its framework registers,
multi-line decorators, and commented-out guards -- `#@token_required`
above a ThreatByte handler is the entire bug. Where the registration lives
in another document (connexion's `operationId`, an express router), a
recipe names `context = [...]`, the harvester writes that stanza per side,
and the pair materializes both files at their repository-relative paths so
the entry-point mapper sees the layout the project has. `harvest_recipe`
fetches every blob before the first write and the driver takes `--report`,
so a failed fetch leaves a reviewed excerpt exactly as it was.

Two defects the re-harvest exposed: `.mjs` was not a known suffix, so the
one pair vendored as an ES module had no file target at all; and
`_python_route_access` matched auth words inside decorator prose, reading
`@ns.doc(description='... without proper authorization checks')` as a
guard. Recorded in the hygiene artifact: nine of the ten duplicate groups
sit on both sides of the split, which the repository-level split cannot
see.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01T7XD7x5KonMdmGRkp63EQG
2026-09-06 12:39:55 +02:00

159 lines
5.3 KiB
TypeScript

// Provenance: bigfivefoods/supplieradvisor-mvp GET (fixed).
// repo: bigfivefoods/supplieradvisor-mvp
// commit: a69e8f27c0f4d402573d16f0612b51155bbc46e1
// parent: 8558538471e4fb10856f8d9a17d2be5ab5c4fb9c
// commit_url: https://github.com/bigfivefoods/supplieradvisor-mvp/commit/a69e8f27c0f4d402573d16f0612b51155bbc46e1
// cve:
// license: MIT
// function: GET
// relpath: app/api/customers/opportunities/route.ts
// provenance: agent
// mechanism: identity_from_request_body
// upstream_start: 89
export async function GET(request: NextRequest) {
try {
const companyId = Number(request.nextUrl.searchParams.get('companyId'));
const stage = request.nextUrl.searchParams.get('stage');
const q = (request.nextUrl.searchParams.get('q') || '').trim().toLowerCase();
if (!Number.isFinite(companyId) || companyId <= 0) {
return NextResponse.json({ error: 'companyId required' }, { status: 400 });
}
const _gate = await requireCompanyAccess(request, companyId, { legacyPrivyUserId: legacyPrivyFrom(request) });
if (!_gate.ok) return _gate.response;
const supabase = getSupabaseServer();
const { loadHoldingSubtree, annotateGroupOpportunity } = await import(
'@/lib/business/holding-pipeline'
);
const tree = await loadHoldingSubtree(companyId);
let query = supabase
.from('opportunities')
.select('*')
.in('profile_id', tree.ids)
.order('updated_at', { ascending: false })
.limit(2000);
if (stage && stage !== 'all') {
query = query.or(`stage.eq.${stage},status.eq.${stage}`);
}
const { data, error } = await query;
if (error) {
return NextResponse.json({
success: true,
opportunities: [],
warning: error.message,
hint: 'Run 20260709_crm_leads_opportunities.sql',
});
}
let opportunities: OpportunityListItem[] = (data || []).map((raw) => {
const o = raw as Record<string, unknown>;
const st = normalizeStage(
o.stage as string | null,
o.status as string | null
);
const amount = Number(o.amount ?? o.opportunity_size ?? 0);
const probability =
o.probability != null ? Number(o.probability) : stageProbability(st);
const openDate =
toDateOnly(o.open_date as string | null) ||
toDateOnly(o.created_at as string | null) ||
null;
const expectedClose =
toDateOnly(o.expected_close_date as string | null) ||
toDateOnly(o.estimated_date as string | null) ||
null;
const actualClose = toDateOnly(o.actual_close_date as string | null);
const annotated = annotateGroupOpportunity(
{ ...o, stage: st, amount, probability },
companyId,
tree.names
);
return {
...o,
...annotated,
name: o.name ?? null,
contact_name: o.contact_name ?? null,
company_name: o.company_name ?? null,
contact_email: o.contact_email ?? null,
product_interest: o.product_interest ?? null,
stage: st,
amount,
probability,
open_date: openDate,
expected_close_date: expectedClose,
actual_close_date: actualClose,
contact_phone: o.contact_phone || o.contact_number || null,
location: o.location || o.opportunity_location || null,
description: o.description || o.notes || null,
weighted_amount: Math.round((amount * probability) / 100),
days_to_expected:
openDate && expectedClose
? Math.round(
(new Date(expectedClose).getTime() -
new Date(openDate).getTime()) /
(24 * 60 * 60 * 1000)
)
: null,
days_to_close:
openDate && actualClose
? Math.round(
(new Date(actualClose).getTime() -
new Date(openDate).getTime()) /
(24 * 60 * 60 * 1000)
)
: null,
days_open:
openDate && !actualClose
? Math.round(
(Date.now() - new Date(openDate).getTime()) /
(24 * 60 * 60 * 1000)
)
: openDate && actualClose
? Math.round(
(new Date(actualClose).getTime() -
new Date(openDate).getTime()) /
(24 * 60 * 60 * 1000)
)
: null,
};
});
if (q) {
opportunities = opportunities.filter((o) => {
const hay = [
searchHay(o.name),
searchHay(o.contact_name),
searchHay(o.company_name),
searchHay(o.contact_email),
searchHay(o.contact_phone),
searchHay(o.stage),
searchHay(o.product_interest),
]
.join(' ')
.toLowerCase();
return hay.includes(q);
});
}
const { summarizeGroupPipeline } = await import(
'@/lib/business/group-pipeline-view'
);
const group = summarizeGroupPipeline({
viewerCompanyId: companyId,
names: tree.names,
companyIds: tree.ids,
isSubsidiary: tree.isSubsidiary,
opportunities,
});
return NextResponse.json({
success: true,
opportunities,
group,
});
} catch (e: unknown) {
return NextResponse.json({ error: e instanceof Error ? e.message : 'Error' }, { status: 500 });
}
}