text: import models from '../../database/models'
  import { Authentication } from '../../middlewares/authentication'
  import { UserDTO } from '../../dto/users'
  
  export const addUser = async (req, res) => {
      try {
          const { body } = req
  
          const duplicateUsername = await models.users.findOne({
              where: {
                  username: body.username,
              },
          })
  
          if (duplicateUsername) {
              res.status(409).send({ error: 'Username already taken' })
              return
          }
  
          const duplicateEmail = await models.users.findOne({
              where: {
                  email: body.email,
              },
          })
  
          if (duplicateEmail) {
              res.status(409).send({ error: 'Email has already been taken' })
              return
          }
  
          const createUser = await models.users.create({
              username: body.username,
              nickname: body.nickname,
              email: body.email,
              password: body.password,
          })
          const authToken = Authentication.generateAuthToken(createUser)
          res.status(201).send({
              message: 'User successfully created',
              authToken,
          })
      } catch (error) {
          res.status(500).send(error)
      }
  }
  
  export const getUsersByUsername = async (req, res) => {
      try {
          const { params } = req
          const user = await models.users.findOne({
              where: {
                  username: params.username,
              },
          })
          const authToken = req.get('Authorization')
  
          if (!authToken) {
              res.status(400).send({
                  'Error message': 'Auth token not provided',
              })
          }
  
          const decodedUser = Authentication.extractUser(authToken)
  
          if (!decodedUser.id) {
              res.status(401).send({
                  'Error message': 'Auth token invalid',
              })
          } else {
              const userDTO = await UserDTO.convertToDto(user)
              res.status(200).send(userDTO)
          }
      } catch (error) {
          res.status(500).send({ 'Error message': error.toString() })
      }
  }
  
  export const authenticateUser = async (req, res) => {
      try {
          const { body } = req
          const user = await models.users.findOne({
              where: {
                  email: body.email,
              },
          })
  
          if (!user || !user.validatePassword(body.password)) {
              res.status(401).send({
                  error: 'Incorrect email or password',
              })
          } else {
              const authToken = Authentication.generateAuthToken(user)
              res.status(200).send({
                  message: 'Authentication successful',
                  authToken,
              })
          }
      } catch (error) {
          res.status(500).send({ 'Error message': error.toString() })
      }
  }
  