SHARE
TWEET

Untitled

a guest Jun 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable, HttpException, HttpStatus, BadRequestException } from '@nestjs/common';
  2. import { UserRegisterDTO } from '../models/dto/users/user-register.dto';
  3. import { UserLoginDTO } from '../models/dto/users/user-login.dto';
  4. import { JwtPayload } from '../core/interfaces/jwt-payload';
  5. import { User } from '../data/entities/user';
  6. import { InjectRepository } from '@nestjs/typeorm';
  7. import { Repository } from 'typeorm';
  8. import * as bcrypt from 'bcrypt';
  9. import { ShowUserDTO } from '../models/dto/users/show-users.dto';
  10. import { BanUserDTO } from '../models/dto/ban-user/ban-user.dto';
  11. import { BanStatus } from '../data/entities/ban-status';
  12. import { UserRole } from '../models/user-role';
  13. import { UserInfoDTO } from 'src/models/dto/users/user-info.dto';
  14.  
  15. @Injectable()
  16. export class UsersService {
  17.     public constructor(@InjectRepository(User) private readonly userRepository: Repository<User>) { }
  18.  
  19.     async returnUser(user: User) {
  20.         return await{
  21.             id: user.id,
  22.             name: user.name,
  23.             email: user.email,
  24.             createdOn: user.createdOn,
  25.             posts: await user.posts,
  26.             comments: await user.comments,
  27.         };
  28.     }
  29.  
  30.     async signIn(user: UserLoginDTO): Promise<User | undefined> {
  31.         return await this.userRepository.findOne({
  32.             where: {
  33.                 ...user,
  34.             },
  35.         });
  36.     }
  37.  
  38.     async register(user: UserRegisterDTO): Promise<ShowUserDTO | undefined> {
  39.         const usernameExists = await this.userRepository.findOne({
  40.             where: {
  41.                 name: user.name,
  42.             },
  43.         });
  44.         if (usernameExists) {
  45.             throw new HttpException('Username already exists!', HttpStatus.BAD_REQUEST);
  46.         }
  47.         const emailExists = await this.userRepository.findOne({
  48.             where: {
  49.                 email: user.email,
  50.             },
  51.         });
  52.         if (emailExists) {
  53.             throw new HttpException('Email already exists', HttpStatus.BAD_REQUEST);
  54.         }
  55.  
  56.         const userForRegistration = await this.userRepository.create(user);
  57.         const passwordHash = await bcrypt.hash(userForRegistration.password, 10);
  58.         userForRegistration.password = passwordHash;
  59.  
  60.         await this.userRepository.save({ ...userForRegistration });
  61.  
  62.         const returnUser = {
  63.             id: userForRegistration.id,
  64.             name: userForRegistration.name,
  65.             email: userForRegistration.email,
  66.         };
  67.  
  68.         return returnUser;
  69.     }
  70.  
  71.     async validate(payload: JwtPayload): Promise<User | undefined> {
  72.         return await this.userRepository.findOne({
  73.             where: {
  74.                 ...payload,
  75.             },
  76.         });
  77.     }
  78.  
  79.     public async banUser(id: string, reason: BanUserDTO): Promise<User | undefined> {
  80.         const userToBeBanned = await this.userRepository.findOne({
  81.             where: {
  82.                 id,
  83.             },
  84.         });
  85.  
  86.         const ban = new BanStatus();
  87.         ban.reason = reason.reason;
  88.         ban.isBanned = true;
  89.         userToBeBanned.isBanned = Promise.resolve(ban);
  90.         userToBeBanned.preveleges = UserRole.BANNED;
  91.  
  92.         return await this.userRepository.save(userToBeBanned);
  93.     }
  94.  
  95.     public async getAllUsers(): Promise<User[]> {
  96.         return await this.userRepository.find({
  97.             where: {
  98.                 isDeleted: false,
  99.             },
  100.         });
  101.     }
  102.  
  103.     public async getUserById(id: string): Promise<User> {
  104.         const getById = await this.userRepository.findOne({
  105.             where: {
  106.                 id,
  107.                 isDeleted: false,
  108.             },
  109.         });
  110.  
  111.         return getById;
  112.     }
  113.  
  114.     public async deleteUser(id: string): Promise<string> {
  115.         const userToDelete = await this.userRepository.findOne({
  116.             where: {
  117.                 id,
  118.             },
  119.         });
  120.  
  121.         if (userToDelete.isDeleted === true) {
  122.             return `User with id:${id} is already deleted`;
  123.         }
  124.  
  125.         await this.userRepository.update(userToDelete, { isDeleted: true, preveleges: UserRole.DELETED });
  126.  
  127.         return `User with id: ${id} is deleted`;
  128.     }
  129.  
  130.     public async getUserInfo(username: string): Promise<UserInfoDTO> {
  131.         const user = await this.userRepository.findOne({
  132.             where: {
  133.                 name: username,
  134.                 isDeleted: false,
  135.             },
  136.         });
  137.  
  138.         const getUser = await this.returnUser(user);
  139.  
  140.         return getUser;
  141.     }
  142. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top