52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import { GroupService } from 'src/objects/group/group.service';
|
|
import { User } from 'src/objects/user/user.entity';
|
|
import { UserService } from 'src/objects/user/user.service';
|
|
import { CreateGroupRequestDto } from './dto/groups-create-request.dto';
|
|
import { GroupsResponseDto, PartialUser } from './dto/groups-response.dto';
|
|
import pick from 'lodash.pick';
|
|
import { Group } from 'src/objects/group/group.entity';
|
|
|
|
@Injectable()
|
|
export class AppGroupService {
|
|
constructor(
|
|
private readonly userService: UserService,
|
|
private readonly groupService: GroupService,
|
|
) {}
|
|
|
|
async getUserGroups(user: User) {
|
|
const { groups } = await this.userService.getUserById(user.id, [
|
|
'groups',
|
|
'groups.members',
|
|
]);
|
|
|
|
(groups as GroupsResponseDto[]).forEach((group) => {
|
|
this.mapGroup(group);
|
|
});
|
|
|
|
return groups;
|
|
}
|
|
|
|
async createNewGroup(user: User, body: CreateGroupRequestDto) {
|
|
const { color } = body;
|
|
if (
|
|
!color.startsWith('#') &&
|
|
!color.startsWith('rgb') &&
|
|
!color.startsWith('hsl') &&
|
|
!color.startsWith('hsv')
|
|
) {
|
|
throw new BadRequestException('Invalid color value');
|
|
}
|
|
|
|
const result = await this.groupService.create(body, user);
|
|
return this.mapGroup(result);
|
|
}
|
|
|
|
private mapGroup(input: Group | GroupsResponseDto): GroupsResponseDto {
|
|
input.members = input.members.map((member: PartialUser) =>
|
|
pick(member, ['sub', 'name', 'color', 'picture']),
|
|
);
|
|
return input;
|
|
}
|
|
}
|