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 202
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
5dfc1ca
commit 049e3eb
Showing
5 changed files
with
106 additions
and
0 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
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,42 @@ | ||
import { Controller, Get, Param, ParseIntPipe, Query } from '@nestjs/common'; | ||
import { auditLogs } 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 { Scopes } from '../auth/scope.decorator'; | ||
import { Expose } from '../prisma/prisma.interface'; | ||
import { AuditLogsService } from './audit-logs.service'; | ||
|
||
@Controller('groups/:groupId/audit-logs') | ||
export class AuditLogController { | ||
constructor(private auditLogsService: AuditLogsService) {} | ||
|
||
@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<auditLogs>[]> { | ||
return this.auditLogsService.getAuditLogs(groupId, { | ||
skip, | ||
take, | ||
orderBy, | ||
cursor, | ||
where, | ||
}); | ||
} | ||
|
||
@Get(':id') | ||
@Scopes('group-{groupId}:read-audit-log-{id}') | ||
async get( | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<auditLogs>> { | ||
return this.auditLogsService.getAuditLog(groupId, Number(id)); | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaModule } from '../prisma/prisma.module'; | ||
import { AuditLogController } from './audit-logs.controller'; | ||
import { AuditLogsService } from './audit-logs.service'; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
controllers: [AuditLogController], | ||
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
HttpException, | ||
HttpStatus, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { | ||
auditLogs, | ||
auditLogsOrderByInput, | ||
auditLogsWhereInput, | ||
auditLogsWhereUniqueInput, | ||
} from '@prisma/client'; | ||
import { Expose } from '../prisma/prisma.interface'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class AuditLogsService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async getAuditLogs( | ||
groupId: number, | ||
params: { | ||
skip?: number; | ||
take?: number; | ||
cursor?: auditLogsWhereUniqueInput; | ||
where?: auditLogsWhereInput; | ||
orderBy?: auditLogsOrderByInput; | ||
}, | ||
): Promise<Expose<auditLogs>[]> { | ||
const { skip, take, cursor, where, orderBy } = params; | ||
const auditLogs = await this.prisma.auditLogs.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where: { ...where, group: { id: groupId } }, | ||
orderBy, | ||
}); | ||
return auditLogs.map((group) => this.prisma.expose<auditLogs>(group)); | ||
} | ||
|
||
async getAuditLog(groupId: number, id: number): Promise<Expose<auditLogs>> { | ||
const auditLog = await this.prisma.auditLogs.findOne({ | ||
where: { id }, | ||
}); | ||
if (!auditLog) | ||
throw new HttpException('AuditLog not found', HttpStatus.NOT_FOUND); | ||
if (auditLog.groupId !== groupId) throw new UnauthorizedException(); | ||
return this.prisma.expose<auditLogs>(auditLog); | ||
} | ||
} |