This commit is contained in:
Bastian Wagner
2024-12-27 20:57:37 +01:00
parent 5194298fa6
commit 5363d0880f
33 changed files with 640 additions and 50 deletions

View File

@@ -25,6 +25,9 @@ export class Key implements IKey {
@Column({ name: 'handed_out', default: false })
handedOut: boolean;
@Column({ name: 'lost', default: false })
keyLost: boolean;
@ManyToOne(() => Cylinder, (cylinder) => cylinder.keys)
cylinder: Cylinder;

View File

@@ -16,4 +16,7 @@ export class Role {
@Column({ nullable: true })
name: string;
@Column({ name: 'is_default', default: false})
defaultRole: boolean;
}

View File

@@ -20,7 +20,7 @@ export class KeySystem implements IKeySystem {
@CreateDateColumn({ name: 'created_at' })
createdAt: Date;
@Column({ nullable: false, unique: true })
@Column({ nullable: false, unique: false })
name: string;
@ManyToMany(() => User, (manager) => manager.systems)

View File

@@ -9,6 +9,6 @@ export class RoleRepository extends Repository<Role> {
}
getStandardRole(): Promise<Role> {
return this.findOne({ where: { name: 'user' } });
return this.findOne({ where: { defaultRole: true } });
}
}

View File

@@ -1,5 +1,5 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Cylinder, Key, User } from 'src/model/entitites';
import { Customer, Cylinder, Key, User } from 'src/model/entitites';
import { IUser } from 'src/model/interface';
import {
CylinderRepository,
@@ -21,10 +21,14 @@ export class KeyService {
) {}
async getUsersKeys(user: User): Promise<Key[]> {
return this.keyrepository.find({
const keys = await this.keyrepository.find({
where: { cylinder: { system: { managers: { id: user.id } } } },
relations: ['cylinder', 'cylinder.system', 'customer'],
});
for (let k of keys) {
k.customer = await this.getCustomerOfLastHandout(user, k.id);
}
return keys;
}
async updateKey(user: User, key: Key) {
@@ -98,6 +102,25 @@ export class KeyService {
});
}
async getCustomerOfLastHandout (user: User, keyID: string): Promise<Customer> {
const handover = await this.handoverRepo.find({
where: {
key: { cylinder: { system: { managers: { id: user.id } } }, id: keyID },
},
order: {
timestamp: { direction: 'DESC' },
created: { direction: 'DESC' },
},
relations: ['customer'],
take: 1
});
if (handover.length == 1 && handover[0].direction == 'out') {
return handover[0].customer;
} else {
return null;
}
}
createKey(user: User, key: any) {
return this.keyrepository.save(this.keyrepository.create(key));
}

View File

@@ -38,6 +38,11 @@ export class SystemController {
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);

View File

@@ -43,4 +43,13 @@ export class SystemService {
const system = await this.systemRepo.findOne({ where: { id } });
return this.systemRepo.softRemove(system);
}
async getManagers(id: string) {
const system = await this.systemRepo.findOne({
where: { id: id },
relations: ['managers']
});
return system.managers;
}
}