Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 1x 1x 6x 1x 6x 1x 5x 1x 4x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 1x 1x 3x 1x | import { Inject, Injectable, forwardRef } from "@nestjs/common"; import { CreateUserDto } from "./dto/create-user.dto"; import { UpdateUserDto } from "./dto/update-user.dto"; import { InjectRepository } from "@nestjs/typeorm"; import { User } from "./entities/user.entity"; import { LessThanOrEqual, Repository } from "typeorm"; import { AuthService } from "src/auth/auth.service"; import { Expertise } from "src/expertise/entities/expertise.entity"; import { ExpertiseException } from "src/expertise/expertise.service"; import { instanceToPlain } from "class-transformer"; import { Badge } from "src/badge/entities/badge.entity"; import * as bcrypt from "bcrypt"; type UserError = | "DUPLICATE EMAIL" | "DUPLICATE USERNAME" | "USER DOESNT EXIST" | "USER NEED AT LEAST ONE EXPERTISE" | "USER CAN HAVE UNTIL 3 EXPERTISES"; export class UserException extends Error { name: UserError; constructor(name: UserError) { super(); this.name = name; } } @Injectable() export class UserService { constructor( @InjectRepository(User) private userRepository: Repository<User>, @InjectRepository(Badge) private badgeRepository: Repository<Badge>, @Inject(forwardRef(() => AuthService)) private authService: AuthService, ) { } async create({ username, password, email, expertises, name }: CreateUserDto) { if (await this.userRepository.findOneBy({ email: email })) { throw new UserException("DUPLICATE EMAIL"); } else if (await this.userRepository.findOneBy({ username: username })) { throw new UserException("DUPLICATE USERNAME"); } else if (expertises.length < 1) { throw new UserException("USER NEED AT LEAST ONE EXPERTISE"); } else if (expertises. length > 3) { throw new UserException("USER CAN HAVE UNTIL 3 EXPERTISES"); } const { hash, jwt } = await this.authService.signUp(username, password); try { await this.userRepository.save({ name, username, email, hashPassword: hash, expertises: expertises.map(e => { const expertise = new Expertise(); expertise.title = e; return expertise; }), currentBadge: { id: 1, }, badges: [ { id: 1 } ], } as User); const user = await this.findOneByUsername(username); return { token: jwt, user: instanceToPlain(user), }; } catch (e) { const error = e as Error; if (error.message.includes("expertise")) { throw new ExpertiseException("EXPERTISE DOESNT EXIST"); } throw error; } } async findAll() { return this.userRepository.find({ relations: { expertises: true, badges: true, currentBadge: true, } }); } async findOneByUsername(username: string) { return this.findOne({ username }); } async findOneByEmail(email: string) { return this.findOne({ email }); } async update(username: string, updateUserDto: UpdateUserDto) { if (await this.userRepository.countBy({ username }) === 0) { throw new UserException("USER DOESNT EXIST"); } const user = (await this .userRepository .findOneBy({ username }))!; const parsedDto = { username: username, name: updateUserDto.name, email: updateUserDto.email, hashPassword: user.hashPassword, xp: user.xp, currentBadge: updateUserDto.badge, expertises: updateUserDto.expertises }; if (updateUserDto.password !== "") { const hash = await bcrypt.hash(updateUserDto.password!, 10); parsedDto["hashPassword"] = hash; } await this.userRepository.save(parsedDto); return true; } async remove(username: string) { return (await this.userRepository.delete(username))!.affected! > 0; } async addXp(username: string, quantity: number) { const user = await this.findOneByUsername(username); const newXp = Math.max(0, user.xp + quantity); const badges = await this.badgeRepository.find({ where: { xp: LessThanOrEqual(newXp), }, }); await this.userRepository.save({ username, xp: newXp, badges, }); } private async findOne(where: Partial<User>) { const user = await this.userRepository.findOne({ where, relations: { expertises: true, currentBadge: true, badges: true, } }); if (user) { return user; } else { throw new UserException("USER DOESNT EXIST"); } } } |