import { HttpException , HttpStatus , Injectable } from '@nestjs/common' ; import { InjectModel } from '@nestjs/mongoose' ; import * as bcrypt from 'bcrypt' ; import { Model } from 'mongoose' ; import { LoginDTO , RegisterDTO } from '../auth/auth.dto' ; import { Payload } from '../types/payload' ; import { User } from '../types/user' ; @ Injectable ( ) export class UserService { constructor ( @ InjectModel ( 'User' ) private userModel ) { } async create ( userDTO ) { const { username } = userDTO ; const user = await this . userModel . findOne ( { username } ) ; if ( user ) { throw new HttpException ( 'User already exists' , HttpStatus . BAD_REQUEST ) ; } const createdUser = new this . userModel ( userDTO ) ; await createdUser . save ( ) ; return this . sanitizeUser ( createdUser ) ; } async find ( ) { return await this . userModel . find ( ) ; } async findByLogin ( userDTO ) { const { username , password } = userDTO ; const user = await this . userModel . findOne ( { username } ) . select ( 'username password seller created address' ) ; if ( ! user ) { throw new HttpException ( 'Invalid credentials' , HttpStatus . UNAUTHORIZED ) ; } if ( await bcrypt . compare ( password , user . password ) ) { return this . sanitizeUser ( user ) ; } else { throw new HttpException ( 'Invalid credentials' , HttpStatus . UNAUTHORIZED ) ; } } async findByPayload ( payload ) { const { username } = payload ; return await this . userModel . findOne ( { username } ) ; } sanitizeUser ( user ) { const sanitized = user . toObject ( ) ; delete sanitized [ 'password' ] ; return sanitized ; } }