Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable, HttpException, HttpStatus, BadRequestException } from '@nestjs/common';
- import { UserRegisterDTO } from '../models/dto/users/user-register.dto';
- import { UserLoginDTO } from '../models/dto/users/user-login.dto';
- import { JwtPayload } from '../core/interfaces/jwt-payload';
- import { User } from '../data/entities/user';
- import { InjectRepository } from '@nestjs/typeorm';
- import { Repository } from 'typeorm';
- import * as bcrypt from 'bcrypt';
- import { ShowUserDTO } from '../models/dto/users/show-users.dto';
- import { BanUserDTO } from '../models/dto/ban-user/ban-user.dto';
- import { BanStatus } from '../data/entities/ban-status';
- import { UserRole } from '../models/user-role';
- import { UserInfoDTO } from 'src/models/dto/users/user-info.dto';
- @Injectable()
- export class UsersService {
- public constructor(@InjectRepository(User) private readonly userRepository: Repository<User>) { }
- async returnUser(user: User) {
- return await{
- id: user.id,
- name: user.name,
- email: user.email,
- createdOn: user.createdOn,
- posts: await user.posts,
- comments: await user.comments,
- };
- }
- async signIn(user: UserLoginDTO): Promise<User | undefined> {
- return await this.userRepository.findOne({
- where: {
- ...user,
- },
- });
- }
- async register(user: UserRegisterDTO): Promise<ShowUserDTO | undefined> {
- const usernameExists = await this.userRepository.findOne({
- where: {
- name: user.name,
- },
- });
- if (usernameExists) {
- throw new HttpException('Username already exists!', HttpStatus.BAD_REQUEST);
- }
- const emailExists = await this.userRepository.findOne({
- where: {
- email: user.email,
- },
- });
- if (emailExists) {
- throw new HttpException('Email already exists', HttpStatus.BAD_REQUEST);
- }
- const userForRegistration = await this.userRepository.create(user);
- const passwordHash = await bcrypt.hash(userForRegistration.password, 10);
- userForRegistration.password = passwordHash;
- await this.userRepository.save({ ...userForRegistration });
- const returnUser = {
- id: userForRegistration.id,
- name: userForRegistration.name,
- email: userForRegistration.email,
- };
- return returnUser;
- }
- async validate(payload: JwtPayload): Promise<User | undefined> {
- return await this.userRepository.findOne({
- where: {
- ...payload,
- },
- });
- }
- public async banUser(id: string, reason: BanUserDTO): Promise<User | undefined> {
- const userToBeBanned = await this.userRepository.findOne({
- where: {
- id,
- },
- });
- const ban = new BanStatus();
- ban.reason = reason.reason;
- ban.isBanned = true;
- userToBeBanned.isBanned = Promise.resolve(ban);
- userToBeBanned.preveleges = UserRole.BANNED;
- return await this.userRepository.save(userToBeBanned);
- }
- public async getAllUsers(): Promise<User[]> {
- return await this.userRepository.find({
- where: {
- isDeleted: false,
- },
- });
- }
- public async getUserById(id: string): Promise<User> {
- const getById = await this.userRepository.findOne({
- where: {
- id,
- isDeleted: false,
- },
- });
- return getById;
- }
- public async deleteUser(id: string): Promise<string> {
- const userToDelete = await this.userRepository.findOne({
- where: {
- id,
- },
- });
- if (userToDelete.isDeleted === true) {
- return `User with id:${id} is already deleted`;
- }
- await this.userRepository.update(userToDelete, { isDeleted: true, preveleges: UserRole.DELETED });
- return `User with id: ${id} is deleted`;
- }
- public async getUserInfo(username: string): Promise<UserInfoDTO> {
- const user = await this.userRepository.findOne({
- where: {
- name: username,
- isDeleted: false,
- },
- });
- const getUser = await this.returnUser(user);
- return getUser;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement