diff --git a/src/interceptors/audit-log.interceptor.ts b/src/interceptors/audit-log.interceptor.ts index c5d35e579..e9997cd74 100644 --- a/src/interceptors/audit-log.interceptor.ts +++ b/src/interceptors/audit-log.interceptor.ts @@ -6,13 +6,14 @@ import { NestInterceptor, } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; +import { getClientIp } from 'request-ip'; import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; import { STAART_AUDIT_LOG_DATA } from 'src/modules/audit-logs/audit-log.constants'; import { UserRequest } from 'src/modules/auth/auth.interface'; -import { PrismaService } from 'src/modules/prisma/prisma.service'; -import { getClientIp } from 'request-ip'; import { GeolocationService } from 'src/modules/geolocation/geolocation.service'; +import { PrismaService } from 'src/modules/prisma/prisma.service'; +import { UAParser } from 'ua-parser-js'; @Injectable() export class AuditLogger implements NestInterceptor { @@ -25,7 +26,6 @@ export class AuditLogger implements NestInterceptor { ) {} intercept(context: ExecutionContext, next: CallHandler): Observable { - console.log('Before...', context.getClass()); let auditLog = this.reflector.get( STAART_AUDIT_LOG_DATA, context.getHandler(), @@ -34,7 +34,6 @@ export class AuditLogger implements NestInterceptor { tap(() => { (async () => { if (auditLog) { - console.log('Doing audit log', auditLog); if (typeof auditLog === 'string') auditLog = [auditLog]; const request = context.switchToHttp().getRequest() as UserRequest; const groupId = parseInt(request.params.id); @@ -43,29 +42,28 @@ export class AuditLogger implements NestInterceptor { const ip = getClientIp(request); const location = await this.geolocationService.getLocation(ip); const userAgent = request.get('user-agent'); + const ua = new UAParser(userAgent); for await (const event of auditLog) { - console.log('saving', { - user: { connect: { id: request.user.id } }, - group: { connect: { id: groupId } }, - event, - city: location?.city?.names?.en, - region: location?.subdivisions?.pop()?.names?.en, - timezone: location?.location?.time_zone, - countryCode: location?.country?.iso_code, - userAgent, + await this.prisma.auditLogs.create({ + data: { + user: { connect: { id: request.user.id } }, + group: { connect: { id: groupId } }, + event, + city: location?.city?.names?.en, + region: location?.subdivisions?.pop()?.names?.en, + timezone: location?.location?.time_zone, + countryCode: location?.country?.iso_code, + userAgent, + browser: + `${ua.getBrowser().name ?? ''} ${ + ua.getBrowser().version ?? '' + }`.trim() || undefined, + operatingSystem: + `${ua.getOS().name ?? ''} ${ + ua.getOS().version ?? '' + }`.trim() || undefined, + }, }); - // await this.prisma.auditLogs.create({ - // data: { - // user: { connect: { id: request.user.id } }, - // group: { connect: { id: groupId } }, - // event, - // city: location?.city?.names?.en, - // region: location?.subdivisions?.pop()?.names?.en, - // timezone: location?.location?.time_zone, - // countryCode: location?.country?.iso_code, - // userAgent - // }, - // }); } } })()