Dashboard

This commit is contained in:
Bastian Wagner
2025-01-02 15:55:56 +01:00
parent efbfc2eb01
commit 5c6516095b
24 changed files with 490 additions and 36 deletions

View File

@@ -2,6 +2,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Customer, Cylinder, Key, User } from 'src/model/entitites';
import { IUser } from 'src/model/interface';
import {
ActivityRepository,
CylinderRepository,
KeyActivityRepository,
KeyRepository,
@@ -18,6 +19,7 @@ export class KeyService {
private readonly systemRepo: KeySystemRepository,
private activityRepo: KeyActivityRepository,
private handoverRepo: KeyHandoutRepository,
private systemActivityRepo: ActivityRepository,
) {}
async getUsersKeys(user: User): Promise<Key[]> {
@@ -74,19 +76,30 @@ export class KeyService {
async handoverKey(user: IUser, data: any, keyID: string) {
const key: Key = await this.keyrepository.findOneOrFail({
where: { id: keyID, cylinder: { system: { managers: { id: user.id } } } },
relations: [ 'cylinder', 'cylinder.system' ]
});
key.handedOut = data.direction == 'out';
this.keyrepository.save(key);
return this.handoverRepo.save(
const res = await this.handoverRepo.save(
this.handoverRepo.create({
customer: data.customer,
direction: data.direction,
timestamp: data.timestamp,
key: key,
user: user as any
}),
);
const msg = `Schlüssel ${key.nr} ${res.direction == 'out' ? 'ausgegeben an ' : 'zurückgegeben von '}${res.customer.name}`
this.systemActivityRepo.save(
this.systemActivityRepo.create({
system: key.cylinder[0].system,
user: user as any,
message: msg,
}))
return res;
}
getKeyHandovers(user: User, keyID: string) {
@@ -121,8 +134,14 @@ export class KeyService {
}
}
createKey(user: User, key: any) {
return this.keyrepository.save(this.keyrepository.create(key));
async createKey(user: User, key: any) {
const k = await this.keyrepository.save(this.keyrepository.create(key));
this.systemActivityRepo.save({
message: `Schlüssel ${(k as any).nr} angelegt`,
user: user,
system: (k as any).cylinder[0].system
});
return k;
}
async deleteKey(user: User, id: string) {

View File

@@ -32,17 +32,21 @@ export class SystemController {
findAll(@Req() req: AuthenticatedRequest) {
return this.systemService.findAll(req.user);
}
@Get(':id/manager')
getManagers(@Param('id') id: string) {
return this.systemService.getManagers(id);
}
@Post(':id/manager')
manaManager(@Param('id') id: string, @Body() body: any){
return this.systemService.manageManagers(id, body);
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.systemService.findOne(id);
}
@Get(':id/manager')
getManagers(@Param('id') id: string) {
return this.systemService.getManagers(id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateSystemDto: UpdateSystemDto) {
return this.systemService.update(id, updateSystemDto);
@@ -52,9 +56,4 @@ export class SystemController {
remove(@Param('id') id: string) {
return this.systemService.remove(id);
}
@Post(':id/manager')
manaManager(@Param('id') id: string, @Body() body: any){
return this.systemService.manageManagers(id, body);
}
}

View File

@@ -3,6 +3,7 @@ import { CreateSystemDto } from './dto/create-system.dto';
import { UpdateSystemDto } from './dto/update-system.dto';
import { KeySystemRepository, UserRepository } from 'src/model/repositories';
import { User } from 'src/model/entitites';
import { IUser } from 'src/model/interface';
@Injectable()
export class SystemService {

View File

@@ -22,6 +22,16 @@ export class UserController {
return this.userService.saveUser(user);
}
@Get('stats')
getStats(@Req() req: AuthenticatedRequest) {
return this.userService.getUserStats(req.user);
}
@Get('activities')
getUseractivities(@Req() req: AuthenticatedRequest) {
return this.userService.getActivitiesforUser(req.user)
}
@Delete(':id')
deleteUserWithId(@Req() req: AuthenticatedRequest, @Param('id') id: string) {
if (req.user.role.name != "admin") {

View File

@@ -1,13 +1,16 @@
import { Injectable } from '@nestjs/common';
import { User } from 'src/model/entitites';
import { IUser } from 'src/model/interface';
import { RoleRepository, UserRepository } from 'src/model/repositories';
import { ActivityRepository, KeyActivityRepository, KeySystemRepository, RoleRepository, UserRepository } from 'src/model/repositories';
import { FindOperator, FindOperators } from 'typeorm';
@Injectable()
export class UserService {
constructor(
private readonly userRepo: UserRepository,
private readonly roleRepo: RoleRepository,
private readonly systemRepo: KeySystemRepository,
private readonly systemActivityRepo: ActivityRepository
) {}
getAllUsers(): Promise<User[]> {
@@ -28,4 +31,30 @@ export class UserService {
return this.userRepo.deleteUserById(id);
}
async getUserStats(user: IUser) {
const systems = await this.systemRepo.find({
where: { managers: { id: user.id }},
relations: ['cylinders', 'cylinders.keys'],
});
const cylinders = systems.map(s => s.cylinders).flat()
const keys = cylinders.map(c => c.keys).flat().map(k => k.id);
const keycount = [...new Set(keys)]
return {
keys: keycount.length,
cylinders: cylinders.length,
systems: systems.length
}
}
async getActivitiesforUser(user: IUser) {
const activities = await this.systemActivityRepo.find({
where: { system: { managers: { id: user.id } }},
order: { createdAt: 'DESC' },
relations: ['user']
});
return activities;
}
}