forked from Shared/esg
database migrated to POSTGRESQL
This commit is contained in:
+17
-14
@@ -1,8 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AuditEvent } from '../common/enums/audit-event.enum';
|
||||
import { AuditLog, AuditLogDocument } from './schemas/audit-log.schema';
|
||||
import { isUuid } from '../database/postgres.helpers';
|
||||
import { AuditLog } from './schemas/audit-log.schema';
|
||||
|
||||
export interface AuditContext {
|
||||
userId?: string;
|
||||
@@ -19,19 +20,21 @@ export interface AuditContext {
|
||||
@Injectable()
|
||||
export class AuditService {
|
||||
constructor(
|
||||
@InjectModel(AuditLog.name)
|
||||
private readonly auditModel: Model<AuditLogDocument>,
|
||||
@InjectRepository(AuditLog)
|
||||
private readonly auditLogs: Repository<AuditLog>,
|
||||
) {}
|
||||
|
||||
async log(event: AuditEvent, context: AuditContext = {}): Promise<void> {
|
||||
await this.auditModel.create({
|
||||
event,
|
||||
userId: context.userId ? new Types.ObjectId(context.userId) : undefined,
|
||||
actorId: context.actorId ? new Types.ObjectId(context.actorId) : undefined,
|
||||
username: context.username,
|
||||
ip: context.ip,
|
||||
userAgent: context.userAgent,
|
||||
metadata: context.metadata ?? {},
|
||||
});
|
||||
await this.auditLogs.save(
|
||||
this.auditLogs.create({
|
||||
event,
|
||||
userId: isUuid(context.userId) ? context.userId : null,
|
||||
actorId: isUuid(context.actorId) ? context.actorId : null,
|
||||
username: context.username,
|
||||
ip: context.ip,
|
||||
userAgent: context.userAgent,
|
||||
metadata: context.metadata ?? {},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user