feat(CC-054): add @Public ingest endpoint with X-Ingest-Key auth
This commit is contained in:
parent
f52fe6d709
commit
925ce11a06
1 changed files with 10 additions and 6 deletions
|
|
@ -1,14 +1,14 @@
|
||||||
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
|
import { Body, Controller, Get, Headers, Post, Query, UnauthorizedException } from '@nestjs/common';
|
||||||
import { desc, isNull, or, eq, and, gte } from 'drizzle-orm';
|
import { desc, isNull, or, eq, and, gte } from 'drizzle-orm';
|
||||||
import { CurrentSession } from '../auth/session.decorator';
|
import { CurrentSession } from '../auth/session.decorator';
|
||||||
import type { AuthenticatedSession } from '../auth/tenant.guard';
|
import type { AuthenticatedSession } from '../auth/tenant.guard';
|
||||||
|
import { Public } from '../auth/public.decorator';
|
||||||
import { db } from '../db/client';
|
import { db } from '../db/client';
|
||||||
import { financialSignals } from '../db/schema';
|
import { financialSignals } from '../db/schema';
|
||||||
import { IngestSignalsDto } from './dto';
|
import { IngestSignalsDto } from './dto';
|
||||||
|
|
||||||
@Controller('financial-intelligence')
|
@Controller('financial-intelligence')
|
||||||
export class FinancialIntelligenceController {
|
export class FinancialIntelligenceController {
|
||||||
/** GET /v1/financial-intelligence/signals — semnale active pentru tenant */
|
|
||||||
@Get('signals')
|
@Get('signals')
|
||||||
async signals(
|
async signals(
|
||||||
@CurrentSession() session: AuthenticatedSession,
|
@CurrentSession() session: AuthenticatedSession,
|
||||||
|
|
@ -33,16 +33,20 @@ export class FinancialIntelligenceController {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** POST /v1/financial-intelligence/ingest — webhook pentru n8n */
|
/** POST /v1/financial-intelligence/ingest — n8n webhook, semnat cu X-Ingest-Key */
|
||||||
|
@Public()
|
||||||
@Post('ingest')
|
@Post('ingest')
|
||||||
async ingest(
|
async ingest(
|
||||||
@CurrentSession() session: AuthenticatedSession,
|
@Headers('x-ingest-key') ingestKey: string | undefined,
|
||||||
@Body() dto: IngestSignalsDto,
|
@Body() dto: IngestSignalsDto,
|
||||||
) {
|
) {
|
||||||
if (!dto.signals || dto.signals.length === 0) {
|
const expected = process.env.INGEST_SECRET;
|
||||||
return { ingested: 0 };
|
if (expected && ingestKey !== expected) {
|
||||||
|
throw new UnauthorizedException('Invalid ingest key');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!dto.signals || dto.signals.length === 0) return { ingested: 0 };
|
||||||
|
|
||||||
const rows = dto.signals.map((s) => ({
|
const rows = dto.signals.map((s) => ({
|
||||||
tenantId: dto.tenantId ?? null,
|
tenantId: dto.tenantId ?? null,
|
||||||
category: s.category ?? 'macro',
|
category: s.category ?? 'macro',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue