feat(observations): add observations.service

This commit is contained in:
admin-valentin 2026-07-31 10:45:01 +00:00
parent 3cd2bdc82b
commit f2733c6e72

View file

@ -0,0 +1,47 @@
import { Injectable } from '@nestjs/common';
import { and, desc, eq } from 'drizzle-orm';
import { db } from '../db/client';
import { observations } from '../db/schema';
import { AuditService } from '../audit/audit.service';
import type { AuthenticatedSession } from '../auth/tenant.guard';
import type { CreateObservationDto } from './dto';
@Injectable()
export class ObservationsService {
constructor(private readonly audit: AuditService) {}
async list(session: AuthenticatedSession, subjectType?: string, subjectId?: string) {
return db.query.observations.findMany({
where: and(
eq(observations.tenantId, session.tenantId),
...(subjectType ? [eq(observations.subjectType, subjectType)] : []),
...(subjectId ? [eq(observations.subjectId, subjectId)] : []),
),
orderBy: desc(observations.observedAt),
});
}
async create(session: AuthenticatedSession, dto: CreateObservationDto) {
const [row] = await db
.insert(observations)
.values({
tenantId: session.tenantId,
subjectType: dto.subjectType,
subjectId: dto.subjectId,
metric: dto.metric,
value: dto.value,
unit: dto.unit,
source: dto.source,
observedAt: dto.observedAt ? new Date(dto.observedAt) : new Date(),
confidence: dto.confidence !== undefined ? String(dto.confidence) : undefined,
})
.returning();
await this.audit.record(null, {
tenantId: session.tenantId,
actorId: session.userId,
action: 'observation.created',
resource: `observation:${row.id}:${dto.subjectType}/${dto.subjectId}`,
});
return row;
}
}