|
1 |
| -import { Injectable } from '@nestjs/common'; |
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + HttpException, |
| 4 | + HttpStatus, |
| 5 | + Injectable, |
| 6 | + NotFoundException, |
| 7 | + Req, |
| 8 | +} from '@nestjs/common'; |
| 9 | +import { PreFollow, UserRequest } from '../types/custom-type'; |
| 10 | +import { Department, Tag, UserTag } from './department.entity'; |
| 11 | +import { FollowDto } from './dto/follow.dto'; |
| 12 | +import { User } from '../user/user.entity'; |
| 13 | +import { In } from 'typeorm'; |
2 | 14 |
|
3 | 15 | @Injectable()
|
4 |
| -export class DepartmentService {} |
| 16 | +export class DepartmentService { |
| 17 | + async getAllDepartments(@Req() req: UserRequest): Promise<Department[]> { |
| 18 | + const departments: Department[] = await Department.find({ |
| 19 | + relations: ['tags'], |
| 20 | + }); |
| 21 | + |
| 22 | + return departments |
| 23 | + ? await Promise.all( |
| 24 | + departments.map(async (department) => { |
| 25 | + department.follow = await this.getFollow(department, req.user); |
| 26 | + return department; |
| 27 | + }), |
| 28 | + ) |
| 29 | + : []; |
| 30 | + } |
| 31 | + |
| 32 | + async getDepartment( |
| 33 | + @Req() req: UserRequest, |
| 34 | + id: number, |
| 35 | + ): Promise<Department> { |
| 36 | + const department: Department = await Department.findOne(id, { |
| 37 | + relations: ['tags'], |
| 38 | + }); |
| 39 | + if (!department) { |
| 40 | + throw new NotFoundException('There is no department with the given id'); |
| 41 | + } |
| 42 | + |
| 43 | + department.follow = await this.getFollow(department, req.user); |
| 44 | + return department; |
| 45 | + } |
| 46 | + |
| 47 | + async getFollow(department: Department, user: User): Promise<string[]> { |
| 48 | + user = await User.findOne(user); |
| 49 | + const tags: Tag[] = await Tag.find({ |
| 50 | + department, |
| 51 | + }); |
| 52 | + const userTags: UserTag[] = await UserTag.find({ |
| 53 | + where: { |
| 54 | + user, |
| 55 | + tag: In(tags.map((tag) => tag.id)), |
| 56 | + }, |
| 57 | + relations: ['tag'], |
| 58 | + }); |
| 59 | + |
| 60 | + return userTags ? userTags.map((userTag) => userTag.tag.name) : []; |
| 61 | + } |
| 62 | + |
| 63 | + async validateIdFollow( |
| 64 | + req: UserRequest, |
| 65 | + id: number, |
| 66 | + followData: FollowDto, |
| 67 | + ): Promise<PreFollow> { |
| 68 | + const department: Department = await Department.findOne(id, { |
| 69 | + relations: ['tags'], |
| 70 | + }); |
| 71 | + if (!department) { |
| 72 | + throw new NotFoundException('There is no department with the id'); |
| 73 | + } |
| 74 | + |
| 75 | + const tag: Tag = department.tags.find( |
| 76 | + (tag) => tag.name === followData.follow, |
| 77 | + ); |
| 78 | + if (!tag) { |
| 79 | + throw new BadRequestException( |
| 80 | + `There is no tag with the given name: ${followData.follow}`, |
| 81 | + ); |
| 82 | + } |
| 83 | + const user: User = await User.findOne(req.user); |
| 84 | + const userTag: UserTag = await UserTag.findOne({ |
| 85 | + user, |
| 86 | + tag, |
| 87 | + }); |
| 88 | + |
| 89 | + return { |
| 90 | + department, |
| 91 | + tag, |
| 92 | + user, |
| 93 | + userTag, |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + async createFollow( |
| 98 | + @Req() req, |
| 99 | + id: number, |
| 100 | + followData: FollowDto, |
| 101 | + ): Promise<Department> { |
| 102 | + const { department, tag, user, userTag } = await this.validateIdFollow( |
| 103 | + req, |
| 104 | + id, |
| 105 | + followData, |
| 106 | + ); |
| 107 | + |
| 108 | + if (userTag) { |
| 109 | + throw new BadRequestException('already followed tag'); |
| 110 | + } |
| 111 | + |
| 112 | + const newUserTag: UserTag = UserTag.create({ |
| 113 | + user, |
| 114 | + tag, |
| 115 | + }); |
| 116 | + await UserTag.save(newUserTag); |
| 117 | + |
| 118 | + department.follow = await this.getFollow(department, user); |
| 119 | + return department; |
| 120 | + } |
| 121 | + |
| 122 | + async deleteFollow( |
| 123 | + @Req() req, |
| 124 | + id: number, |
| 125 | + followData: FollowDto, |
| 126 | + ): Promise<Department> { |
| 127 | + const { department, tag, user, userTag } = await this.validateIdFollow( |
| 128 | + req, |
| 129 | + id, |
| 130 | + followData, |
| 131 | + ); |
| 132 | + |
| 133 | + if (!userTag) { |
| 134 | + throw new HttpException( |
| 135 | + { |
| 136 | + message: 'already deleted follow', |
| 137 | + }, |
| 138 | + HttpStatus.NO_CONTENT, |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + await UserTag.delete(userTag); |
| 143 | + |
| 144 | + department.follow = await this.getFollow(department, user); |
| 145 | + return department; |
| 146 | + } |
| 147 | +} |
0 commit comments