Handout management

This commit is contained in:
Bastian Wagner
2024-10-20 14:49:42 +02:00
parent 5e2b900b18
commit aa9abdd512
37 changed files with 754 additions and 67 deletions

View File

@@ -1,12 +1,14 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateKeySystemDto } from 'src/model/dto';
import { Cylinder, Key, User } from 'src/model/entitites';
import { IUser } from 'src/model/interface';
import {
CylinderRepository,
KeyActivityRepository,
KeyRepository,
KeySystemRepository,
} from 'src/model/repositories';
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
@Injectable()
export class KeyService {
@@ -15,12 +17,13 @@ export class KeyService {
private readonly cylinderRepository: CylinderRepository,
private readonly systemRepo: KeySystemRepository,
private activityRepo: KeyActivityRepository,
private handoverRepo: KeyHandoutRepository,
) {}
async getUsersKeys(user: User): Promise<Key[]> {
return this.keyrepository.find({
where: { cylinder: { system: { managers: { id: user.id } } } },
relations: ['cylinder', 'cylinder.system'],
relations: ['cylinder', 'cylinder.system', 'customer'],
});
}
@@ -74,4 +77,35 @@ export class KeyService {
throw new HttpException(e.code, HttpStatus.UNPROCESSABLE_ENTITY);
}
}
async handoverKey(user: IUser, data: any, keyID: string) {
const key: Key = await this.keyrepository.findOneOrFail({
where: { id: keyID, cylinder: { system: { managers: { id: user.id } } } },
});
key.handedOut = data.direction == 'out';
this.keyrepository.save(key);
return this.handoverRepo.save(
this.handoverRepo.create({
customer: data.customer,
direction: data.direction,
timestamp: data.timestamp,
key: key,
}),
);
}
getKeyHandovers(user: User, keyID: string) {
return this.handoverRepo.find({
where: {
key: { cylinder: { system: { managers: { id: user.id } } }, id: keyID },
},
order: {
timestamp: { direction: 'DESC' },
created: { direction: 'DESC' },
},
relations: ['customer'],
});
}
}