| import { |
| Controller, |
| Get, |
| HttpException, |
| Query, |
| } from '@nestjs/common'; |
| import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.request'; |
| import { User } from '@prisma/client'; |
| import { ApiTags } from '@nestjs/swagger'; |
| import { ErrorsService } from '@gitroom/nestjs-libraries/database/prisma/errors/errors.service'; |
| import { AdminStatsService } from '@gitroom/nestjs-libraries/database/prisma/admin-stats/admin-stats.service'; |
| import dayjs from 'dayjs'; |
|
|
| @ApiTags('Admin') |
| @Controller('/admin') |
| export class AdminController { |
| constructor( |
| private _errorsService: ErrorsService, |
| private _adminStatsService: AdminStatsService |
| ) {} |
|
|
| private assertSuperAdmin(user: User) { |
| if (!user?.isSuperAdmin) { |
| throw new HttpException('Unauthorized', 400); |
| } |
| } |
|
|
| @Get('/errors') |
| async listErrors( |
| @GetUserFromRequest() user: User, |
| @Query('page') page?: string, |
| @Query('limit') limit?: string, |
| @Query('platform') platform?: string, |
| @Query('email') email?: string, |
| @Query('unknownFirst') unknownFirst?: string |
| ) { |
| this.assertSuperAdmin(user); |
| return this._errorsService.listErrors({ |
| page: page ? parseInt(page, 10) : 0, |
| limit: limit ? parseInt(limit, 10) : 20, |
| platform: platform || undefined, |
| email: email || undefined, |
| unknownFirst: unknownFirst === 'true' || unknownFirst === '1', |
| }); |
| } |
|
|
| @Get('/errors/platforms') |
| async listPlatforms(@GetUserFromRequest() user: User) { |
| this.assertSuperAdmin(user); |
| return this._errorsService.listPlatforms(); |
| } |
|
|
| @Get('/stats') |
| async getStats( |
| @GetUserFromRequest() user: User, |
| @Query('from') from?: string, |
| @Query('to') to?: string, |
| @Query('unknownOnly') unknownOnly?: string |
| ) { |
| this.assertSuperAdmin(user); |
|
|
| const fromDate = from ? dayjs(from) : dayjs().subtract(30, 'day'); |
| const toDate = to ? dayjs(to) : dayjs(); |
|
|
| return this._adminStatsService.getStats({ |
| from: fromDate.startOf('day').toDate(), |
| to: toDate.endOf('day').toDate(), |
| unknownOnly: unknownOnly === 'true' || unknownOnly === '1', |
| }); |
| } |
| } |
|
|