56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { User } from '../user/user.entity';
|
|
import { Group } from './group.entity';
|
|
|
|
@Injectable()
|
|
export class GroupService {
|
|
constructor(
|
|
@InjectRepository(Group)
|
|
private readonly groupRepository: Repository<Group>,
|
|
) {}
|
|
|
|
async create({ name, color }: Partial<Group>, user: User) {
|
|
const group = new Group();
|
|
group.name = name;
|
|
group.color = color;
|
|
group.members = [user];
|
|
|
|
return this.groupRepository.save(group);
|
|
}
|
|
|
|
async getById(id: number, relations = []) {
|
|
return this.groupRepository.findOne({ where: { id }, relations });
|
|
}
|
|
|
|
async getByIdAndMemberSub(id: number, sub: string, relations = []) {
|
|
return this.groupRepository.findOne({
|
|
where: { id, members: { sub } },
|
|
relations,
|
|
});
|
|
}
|
|
|
|
async getGroupsByMemberSub(sub: string, relations = []) {
|
|
return this.groupRepository.find({
|
|
where: {
|
|
members: {
|
|
sub,
|
|
},
|
|
},
|
|
relations: ['members', ...relations],
|
|
});
|
|
}
|
|
|
|
async getGroupsForBuilding(buildingId: number, relations = []) {
|
|
return this.groupRepository.find({
|
|
where: {
|
|
buildings: {
|
|
id: buildingId,
|
|
},
|
|
},
|
|
relations: ['buildings', ...relations],
|
|
});
|
|
}
|
|
}
|