ceo-api/src/segments/segments.service.ts
valentinbvro 0f8390f31b feat: Faza A2 -- intelligence proxy, saved segments, research briefs, briefing
- IntelligenceModule: proxy tenant-scoped catre intelligence-api
  (/companies/search, /companies/{id}); ceo-web nu vorbeste niciodata
  direct cu intelligence-api (blueprint 10.3/16).
- SavedSegments: salveaza o cautare Apollo si o ruleaza din nou oricand.
- ResearchBriefs: dovezi curatate manual per companie, cu sursa Apollo
  implicita plus surse suplimentare -- explicit fara rezumat generat de
  AI (AI Gateway inca neconstruit, blueprint 14).
- BriefingService: /v1/briefing/today, agregare deterministica de
  taskuri restante/viitoare + activitate saptamanala; campul
  aiExplanation ramane null si vizibil in raspuns, nu simulat.
2026-07-28 22:01:17 +02:00

72 lines
2.4 KiB
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common';
import { and, desc, eq } from 'drizzle-orm';
import { db } from '../db/client';
import { savedSegments } from '../db/schema';
import { AuditService } from '../audit/audit.service';
import { IntelligenceService } from '../intelligence/intelligence.service';
import type { AuthenticatedSession } from '../auth/tenant.guard';
import type { CreateSegmentDto } from './dto';
@Injectable()
export class SegmentsService {
constructor(
private readonly audit: AuditService,
private readonly intelligence: IntelligenceService,
) {}
async list(session: AuthenticatedSession) {
return db.query.savedSegments.findMany({
where: eq(savedSegments.tenantId, session.tenantId),
orderBy: desc(savedSegments.createdAt),
});
}
async create(session: AuthenticatedSession, dto: CreateSegmentDto) {
const [row] = await db
.insert(savedSegments)
.values({
tenantId: session.tenantId,
createdByUserId: session.userId,
name: dto.name,
query: dto.query,
resultLimit: dto.resultLimit,
})
.returning();
await this.audit.record(null, {
tenantId: session.tenantId,
actorId: session.userId,
action: 'segment.created',
resource: `segment:${row.id}`,
});
return row;
}
/** Re-ruleaza cautarea salvata contra intelligence-api si intoarce rezultate proaspete. */
async run(session: AuthenticatedSession, id: string) {
const segment = await this.getById(session, id);
const results = await this.intelligence.searchCompanies(segment.query, segment.resultLimit);
return { segment, ...results };
}
async remove(session: AuthenticatedSession, id: string) {
const segment = await this.getById(session, id);
await db.delete(savedSegments).where(eq(savedSegments.id, segment.id));
await this.audit.record(null, {
tenantId: session.tenantId,
actorId: session.userId,
action: 'segment.deleted',
resource: `segment:${id}`,
});
return { deleted: true };
}
private async getById(session: AuthenticatedSession, id: string) {
const segment = await db.query.savedSegments.findFirst({
where: and(eq(savedSegments.id, id), eq(savedSegments.tenantId, session.tenantId)),
});
if (!segment) {
throw new NotFoundException('Segment not found');
}
return segment;
}
}