generated from bastian/boilerplate
Initial commit
This commit is contained in:
15
apps/backend/src/dashboard/dashboard.controller.ts
Normal file
15
apps/backend/src/dashboard/dashboard.controller.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { RequirePermissions } from '../auth/guards/require-permissions.decorator';
|
||||
import { Permission } from '../roles/permissions';
|
||||
import { DashboardService } from './dashboard.service';
|
||||
|
||||
@Controller('dashboard')
|
||||
export class DashboardController {
|
||||
constructor(private readonly dashboard: DashboardService) {}
|
||||
|
||||
@Get()
|
||||
@RequirePermissions(Permission.ItemsRead)
|
||||
summary() {
|
||||
return this.dashboard.summary();
|
||||
}
|
||||
}
|
||||
9
apps/backend/src/dashboard/dashboard.module.ts
Normal file
9
apps/backend/src/dashboard/dashboard.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DashboardController } from './dashboard.controller';
|
||||
import { DashboardService } from './dashboard.service';
|
||||
|
||||
@Module({
|
||||
controllers: [DashboardController],
|
||||
providers: [DashboardService],
|
||||
})
|
||||
export class DashboardModule {}
|
||||
32
apps/backend/src/dashboard/dashboard.service.ts
Normal file
32
apps/backend/src/dashboard/dashboard.service.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { IsNull } from 'typeorm';
|
||||
import { ItemEntity } from '../items/entities/item.entity';
|
||||
import { RoleEntity } from '../roles/entities/role.entity';
|
||||
import { SessionEntity } from '../sessions/entities/session.entity';
|
||||
import { UserEntity } from '../users/entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class DashboardService {
|
||||
constructor(@InjectDataSource() private readonly dataSource: DataSource) {}
|
||||
|
||||
async summary(): Promise<{
|
||||
userCount: number;
|
||||
activeSessions: number;
|
||||
roleCount: number;
|
||||
itemCount: number;
|
||||
}> {
|
||||
const [userCount, activeSessions, roleCount, itemCount] = await Promise.all(
|
||||
[
|
||||
this.dataSource.getRepository(UserEntity).count(),
|
||||
this.dataSource
|
||||
.getRepository(SessionEntity)
|
||||
.count({ where: { revokedAt: IsNull() } }),
|
||||
this.dataSource.getRepository(RoleEntity).count(),
|
||||
this.dataSource.getRepository(ItemEntity).count(),
|
||||
],
|
||||
);
|
||||
return { userCount, activeSessions, roleCount, itemCount };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user