This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c0ade5
commit ec40a98
Showing
6 changed files
with
112 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Controller, Get, Param, ParseIntPipe, Query } from '@nestjs/common'; | ||
import { AuditLog } from '@prisma/client'; | ||
import { CursorPipe } from '../../pipes/cursor.pipe'; | ||
import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; | ||
import { OrderByPipe } from '../../pipes/order-by.pipe'; | ||
import { WherePipe } from '../../pipes/where.pipe'; | ||
import { Expose } from '../../providers/prisma/prisma.interface'; | ||
import { Scopes } from '../auth/scope.decorator'; | ||
import { AuditLogsService } from './audit-logs.service'; | ||
|
||
@Controller('groups/:groupId/audit-logs') | ||
export class AuditLogGroupController { | ||
constructor(private auditLogsService: AuditLogsService) {} | ||
|
||
/** Get audit logs for a group */ | ||
@Get() | ||
@Scopes('group-{groupId}:read-audit-log-*') | ||
async getAll( | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Query('skip', OptionalIntPipe) skip?: number, | ||
@Query('take', OptionalIntPipe) take?: number, | ||
@Query('cursor', CursorPipe) cursor?: Record<string, number | string>, | ||
@Query('where', WherePipe) where?: Record<string, number | string>, | ||
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>, | ||
): Promise<Expose<AuditLog>[]> { | ||
return this.auditLogsService.getAuditLogs({ | ||
skip, | ||
take, | ||
orderBy, | ||
cursor, | ||
where: { ...where, group: { id: groupId } }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Controller, Get, Param, ParseIntPipe, Query } from '@nestjs/common'; | ||
import { AuditLog } from '@prisma/client'; | ||
import { CursorPipe } from '../../pipes/cursor.pipe'; | ||
import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; | ||
import { OrderByPipe } from '../../pipes/order-by.pipe'; | ||
import { WherePipe } from '../../pipes/where.pipe'; | ||
import { Expose } from '../../providers/prisma/prisma.interface'; | ||
import { Scopes } from '../auth/scope.decorator'; | ||
import { AuditLogsService } from './audit-logs.service'; | ||
|
||
@Controller('users/:userId/audit-logs') | ||
export class AuditLogUserController { | ||
constructor(private auditLogsService: AuditLogsService) {} | ||
|
||
/** Get audit logs for a user */ | ||
@Get() | ||
@Scopes('user-{userId}:read-audit-log-*') | ||
async getAll( | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Query('skip', OptionalIntPipe) skip?: number, | ||
@Query('take', OptionalIntPipe) take?: number, | ||
@Query('cursor', CursorPipe) cursor?: Record<string, number | string>, | ||
@Query('where', WherePipe) where?: Record<string, number | string>, | ||
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>, | ||
): Promise<Expose<AuditLog>[]> { | ||
return this.auditLogsService.getAuditLogs({ | ||
skip, | ||
take, | ||
orderBy, | ||
cursor, | ||
where: { ...where, user: { id: userId } }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaModule } from '../../providers/prisma/prisma.module'; | ||
import { AuditLogController } from './audit-logs.controller'; | ||
import { AuditLogGroupController } from './audit-logs-group.controller'; | ||
import { AuditLogUserController } from './audit-logs-user.controller'; | ||
import { AuditLogsService } from './audit-logs.service'; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
controllers: [AuditLogController], | ||
controllers: [ | ||
AuditLogController, | ||
AuditLogGroupController, | ||
AuditLogUserController, | ||
], | ||
providers: [AuditLogsService], | ||
}) | ||
export class AuditLogsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,33 @@ | ||
import { | ||
Injectable, | ||
NotFoundException, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
import type { Prisma } from '@prisma/client'; | ||
import { AuditLog } from '@prisma/client'; | ||
import { UNAUTHORIZED_RESOURCE } from '../../errors/errors.constants'; | ||
import { Expose } from '../../providers/prisma/prisma.interface'; | ||
import { PrismaService } from '../../providers/prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class AuditLogsService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async getAuditLogs( | ||
groupId: number, | ||
params: { | ||
skip?: number; | ||
take?: number; | ||
cursor?: Prisma.AuditLogWhereUniqueInput; | ||
where?: Prisma.AuditLogWhereInput; | ||
orderBy?: Prisma.AuditLogOrderByInput; | ||
}, | ||
): Promise<Expose<AuditLog>[]> { | ||
async getAuditLogs(params: { | ||
skip?: number; | ||
take?: number; | ||
cursor?: Prisma.AuditLogWhereUniqueInput; | ||
where?: Prisma.AuditLogWhereInput; | ||
orderBy?: Prisma.AuditLogOrderByInput; | ||
}): Promise<Expose<AuditLog>[]> { | ||
const { skip, take, cursor, where, orderBy } = params; | ||
const AuditLog = await this.prisma.auditLog.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where: { ...where, group: { id: groupId } }, | ||
orderBy, | ||
}); | ||
return AuditLog.map((group) => this.prisma.expose<AuditLog>(group)); | ||
} | ||
|
||
async getAuditLog(groupId: number, id: number): Promise<Expose<AuditLog>> { | ||
const AuditLog = await this.prisma.auditLog.findUnique({ | ||
where: { id }, | ||
}); | ||
if (!AuditLog) throw new NotFoundException(UNAUTHORIZED_RESOURCE); | ||
if (AuditLog.groupId !== groupId) | ||
throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); | ||
return this.prisma.expose<AuditLog>(AuditLog); | ||
try { | ||
const AuditLog = await this.prisma.auditLog.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where, | ||
orderBy, | ||
include: { group: true, user: true }, | ||
}); | ||
return AuditLog.map((group) => this.prisma.expose<AuditLog>(group)); | ||
} catch (error) { | ||
return []; | ||
} | ||
} | ||
} |