changes
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user