Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Create audit log
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 7, 2020
1 parent 7221d2c commit 36bda48
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/interceptors/audit-log.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,7 +26,6 @@ export class AuditLogger implements NestInterceptor {
) {}

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
console.log('Before...', context.getClass());
let auditLog = this.reflector.get<string | string[]>(
STAART_AUDIT_LOG_DATA,
context.getHandler(),
Expand All @@ -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);
Expand All @@ -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
// },
// });
}
}
})()
Expand Down

0 comments on commit 36bda48

Please sign in to comment.