Files
CIAgent/apps/web/hooks/use-companies.ts
sakshamandClaude Sonnet 5 18305b545c Backfill sparse report sections, add enrichment section refresh, and bootstrap first-admin
Reports: the LLM reliably used company_enrichment for prose fields but
inconsistently populated the parallel Finding-list/string-list fields from
the same evidence, even with progressively more explicit prompting. Add a
code-level backfill (products, recent developments, financial signals,
strategic initiatives, regulatory signals, risks/opportunities mirrored
from SWOT, unknowns, monitoring recommendations) that only ever fills in
what the model left empty, never overwrites what it produced.

Enrichment tab: reorder sections (Products/Recent updates before
Customers/Competitors) and add a per-section "Refresh" button that
re-fetches just one of NinjaPear's six independent per-company endpoints
when it came back empty - confirmed live that a data-coverage gap (e.g.
Amazon returning no products) is real provider behavior, not a bug.

Auth: the first account registered on a deployment with zero existing
admins is now auto-promoted to admin, closing the chicken-and-egg gap
where the only path to admin access was direct DB access. Self-heals if
the last admin ever deletes their account.

Also bumps nginx's proxy_read_timeout for api.ciagent.org to cover the
enrichment refresh's synchronous funding-endpoint call (up to 5 minutes
per NinjaPear's docs).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-06 17:41:02 -04:00

93 lines
3.1 KiB
TypeScript

"use client";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "@/lib/api-client";
import type {
CompanyCreatePayload,
CompanyResponse,
CompanyUpdatePayload,
EnrichmentSection,
MonitorConfigurationUpdatePayload,
} from "@/lib/types";
export function useCompanies() {
return useQuery({ queryKey: ["companies"], queryFn: api.listCompanies });
}
export function useCompany(companyId: string | undefined) {
return useQuery({
queryKey: ["companies", companyId],
queryFn: () => api.getCompany(companyId as string),
enabled: Boolean(companyId),
// Enrichment (NinjaPear) runs in the background after company
// creation - poll while it's still pending so the Enrichment tab
// updates itself once the task finishes, without a manual refresh.
refetchInterval: (query) => {
const company = query.state.data as CompanyResponse | undefined;
return company?.enrichment?.status === "pending" ? 3000 : false;
},
});
}
export function useCreateCompany() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (payload: CompanyCreatePayload) => api.createCompany(payload),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ["companies"] }),
});
}
export function useUpdateCompany(companyId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (payload: CompanyUpdatePayload) => api.updateCompany(companyId, payload),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["companies"] });
queryClient.invalidateQueries({ queryKey: ["companies", companyId] });
},
});
}
export function useDeleteCompany() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (companyId: string) => api.deleteCompany(companyId),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ["companies"] }),
});
}
export function useSetCompanyPaused() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ companyId, paused }: { companyId: string; paused: boolean }) =>
paused ? api.pauseCompany(companyId) : api.resumeCompany(companyId),
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ["companies"] });
queryClient.invalidateQueries({ queryKey: ["companies", variables.companyId] });
},
});
}
export function useRefreshEnrichmentSection(companyId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (section: EnrichmentSection) =>
api.refreshEnrichmentSection(companyId, section),
onSuccess: (company) => {
queryClient.setQueryData(["companies", companyId], company);
},
});
}
export function useUpdateMonitorConfiguration(companyId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (payload: MonitorConfigurationUpdatePayload) =>
api.updateMonitorConfiguration(companyId, payload),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["companies"] });
queryClient.invalidateQueries({ queryKey: ["companies", companyId] });
},
});
}