147 lines
4.4 KiB
TypeScript
147 lines
4.4 KiB
TypeScript
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,
|
|
KeyRepository,
|
|
KeySystemRepository,
|
|
} from 'src/model/repositories';
|
|
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
|
|
import { ActivityHelperService } from 'src/shared/service/activity.logger.service';
|
|
import { IsNull, Not } from 'typeorm';
|
|
|
|
@Injectable()
|
|
export class KeyService {
|
|
constructor(
|
|
private readonly keyrepository: KeyRepository,
|
|
private readonly cylinderRepository: CylinderRepository,
|
|
private readonly systemRepo: KeySystemRepository,
|
|
private handoverRepo: KeyHandoutRepository,
|
|
private readonly activityService: ActivityHelperService
|
|
) {}
|
|
|
|
async getUsersKeys(user: User): Promise<Key[]> {
|
|
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) {
|
|
if (!user || !user.id) {
|
|
throw new HttpException('forbidden', HttpStatus.FORBIDDEN);
|
|
}
|
|
await this.keyrepository.findOneOrFail({
|
|
where: { cylinder: { system: { managers: { id: user.id } } } },
|
|
});
|
|
|
|
return this.keyrepository.save(this.keyrepository.create(key));
|
|
}
|
|
|
|
|
|
async getUsersCylinders(user: User): Promise<Cylinder[]> {
|
|
if (!user || !user.id) {
|
|
throw new HttpException('forbidden', HttpStatus.FORBIDDEN);
|
|
}
|
|
|
|
return this.cylinderRepository.find({
|
|
where: { system: { managers: { id: user.id } } },
|
|
order: { name: { direction: 'ASC' } },
|
|
relations: ['system'],
|
|
});
|
|
}
|
|
|
|
async handoverKey(user: User, 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);
|
|
|
|
const res = await this.handoverRepo.save(
|
|
this.handoverRepo.create({
|
|
customer: data.customer,
|
|
direction: data.direction,
|
|
timestamp: data.timestamp,
|
|
key: key,
|
|
user: user as any
|
|
}),
|
|
);
|
|
|
|
this.activityService.logKeyHandover(user, key, key.cylinder[0].system, res);
|
|
return res;
|
|
}
|
|
|
|
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'],
|
|
});
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
async createKey(user: User, key: any) {
|
|
const k = await this.keyrepository.save(this.keyrepository.create(key));
|
|
this.activityService.logKeyCreated(user, key, key.cylinder[0].system);
|
|
return k;
|
|
}
|
|
|
|
async deleteKey(user: User, id: string) {
|
|
const key = await this.keyrepository.findOneOrFail({
|
|
where: { id, cylinder: { system: { managers: { id: user.id } } } },
|
|
});
|
|
return this.keyrepository.softRemove(key);
|
|
}
|
|
|
|
getDeletedKeys(user: User) {
|
|
return this.keyrepository.find({
|
|
where: {
|
|
cylinder: { system: { managers: { id: user.id } } },
|
|
deletedAt: Not(IsNull()),
|
|
},
|
|
withDeleted: true,
|
|
order: { deletedAt: { direction: 'DESC' } },
|
|
});
|
|
}
|
|
|
|
async restoreKey(user: User, keyID: string) {
|
|
const key = await this.keyrepository.findOneOrFail({
|
|
where: { cylinder: { system: { managers: { id: user.id } } }, id: keyID },
|
|
withDeleted: true,
|
|
});
|
|
key.deletedAt = null;
|
|
return this.keyrepository.save(key);
|
|
}
|
|
}
|