101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { CreateSystemDto } from './dto/create-system.dto';
|
|
import { UpdateSystemDto } from './dto/update-system.dto';
|
|
import { ActivityRepository, KeySystemRepository, UserRepository } from 'src/model/repositories';
|
|
import { User } from 'src/model/entitites';
|
|
import { IUser } from 'src/model/interface';
|
|
|
|
@Injectable()
|
|
export class SystemService {
|
|
constructor(
|
|
private systemRepo: KeySystemRepository,
|
|
private userRepo: UserRepository,
|
|
private systemActivityRepo: ActivityRepository,
|
|
) {}
|
|
|
|
async create(user: User, createSystemDto: CreateSystemDto) {
|
|
const sys = this.systemRepo.create(createSystemDto);
|
|
sys.managers = [user];
|
|
try {
|
|
const res = await this.systemRepo.save(sys);
|
|
|
|
this.systemActivityRepo.save({
|
|
message: `Schließanlage ${(res as any).name} angelegt`,
|
|
user: user,
|
|
system: res
|
|
});
|
|
|
|
|
|
return res;
|
|
} catch (e) {
|
|
throw new HttpException(e.code, HttpStatus.UNPROCESSABLE_ENTITY);
|
|
}
|
|
}
|
|
|
|
findAll(user: User) {
|
|
return this.systemRepo.find({
|
|
where: { managers: { id: user.id } },
|
|
order: { name: { direction: 'ASC' } },
|
|
});
|
|
}
|
|
|
|
findOne(id: string) {
|
|
return this.systemRepo.findOne({ where: { id: id } });
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
update(id: string, updateSystemDto: UpdateSystemDto) {
|
|
throw new HttpException(
|
|
`This action updates a #${id} system but is not implemented`,
|
|
HttpStatus.NOT_IMPLEMENTED,
|
|
);
|
|
return `This action updates a #${id} system`;
|
|
}
|
|
|
|
async remove(id: string) {
|
|
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;
|
|
}
|
|
|
|
async manageManagers(systemID: string, manageObject: { email: string, action: 'add' | 'remove'}) {
|
|
const sys = await this.systemRepo.findOne({
|
|
where: { id: systemID },
|
|
relations: ['managers']
|
|
});
|
|
|
|
if (!sys) {
|
|
throw new HttpException('Das System wurde nicht im System gefunden', HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
if (manageObject.action == 'remove') {
|
|
sys.managers = sys.managers.filter( m => m.username != manageObject.email);
|
|
|
|
await this.systemRepo.save(sys);
|
|
return sys.managers;
|
|
}
|
|
|
|
if (sys.managers.some(m => m.username == manageObject.email)) {
|
|
return sys.managers;
|
|
}
|
|
|
|
const user = await this.userRepo.findOneBy({ username: manageObject.email.trim() });
|
|
if (!user) {
|
|
throw new HttpException('Es wurde kein User mit dieser Emailadresse gefunden. Bitte prüfe die Emailadresse und versuche es erneut.', HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
sys.managers.push(user);
|
|
await this.systemRepo.save(sys);
|
|
return sys.managers;
|
|
|
|
}
|
|
}
|