user.controller.js 2.81 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import User from './user.model.js'
import formidable from 'formidable'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
3
import extend from 'lodash/extend.js'
Yoon, Daeki's avatar
Yoon, Daeki committed
4
import fs from 'fs'
Yoon, Daeki's avatar
Yoon, Daeki committed
5
import dbErrorHandler from '../helpers/dbErrorHandler.js'
Yoon, Daeki's avatar
Yoon, Daeki committed
6
7
8

const create = async (req, res) => {
  const user = new User(req.body)
Yoon, Daeki's avatar
Yoon, Daeki committed
9
  // console.log('user in user.controll:', req.body);
Yoon, Daeki's avatar
Yoon, Daeki committed
10
11
12
  try {
    await user.save()
    return res.json({
Yoon, Daeki's avatar
Yoon, Daeki committed
13
      message: 'Successfully signed up!'
Yoon, Daeki's avatar
Yoon, Daeki committed
14
15
16
    })
  } catch (error) {
    return res.status(400).json({
Yoon, Daeki's avatar
Yoon, Daeki committed
17
      error: dbErrorHandler.getErrorMessage(error)
Yoon, Daeki's avatar
Yoon, Daeki committed
18
19
20
21
22
23
24
25
26
27
    })
  }
}

const list = async (req, res) => {
  try {
    let users = await User.find().select('name email updated created').exec()
    return res.json(users)
  } catch (error) {
    return res.status(400).json({
Yoon, Daeki's avatar
Yoon, Daeki committed
28
29
30
31
32
33
34
35
36
37
38
      error: 'Users not found'
    })
  }
}

const read = (req, res) => {
  req.profile.hashedPassword = undefined
  req.profile.salt = undefined
  return res.json(req.profile)
}

Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
39
const update = (req, res) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
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
  let form = new formidable.IncomingForm()
  form.keepExtensions = true
  form.parse(req, async (err, fields, files) => {
    if (err) {
      return res.status(400).json({
        error: 'Photo could not be uploaded'
      })
    }
    let user = req.profile
    user = extend(user, fields)
    user.updated = new Date()
    if (files.photo) {
      user.photo.data = fs.readFileSync(files,photo.path)
      user.photo.contentType = files.photo.type
    }

    try {
      await user.save()
      user.hashedPassword = undefined
      user.salt = undefined
      res.json(user)
    } catch (error) {
      return res.status(400).json({
        error: 'User save error'
      })
    }
  })
}

const remove = async (req, res) => {
  try {
    let user = req.profile
    let deletedUser = await user.remove()
    deletedUser.hashedPassword = undefined
    deletedUser.salt = undefined
    res.json(deletedUser)
  } catch (error) {
    return res.status(400).json({
      error: 'User delete error'
    })
  }
}

Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
83
84
85
86
87
88
89
90
91
92
const isInstructor = (req, res, next) => {
  const instructor = req.profile && req.profile.instructor
  if (!instructor) {
    return res.status(403).json({
      error: 'User is not an instructor'
    })
  }
  next()
}

Yoon, Daeki's avatar
Yoon, Daeki committed
93
94
95
96
97
98
99
100
101
102
const isAdmin = (req, res, next) => {
  const admin = req.profile && req.profile.admin
  if (!admin) {
    return res.status(403).json({
      error: 'User is not an admin'
    })
  }
  next()
}

Yoon, Daeki's avatar
Yoon, Daeki committed
103
const userById = async (req, res, next, id) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
104
  // console.log('req.body in userById', req.body);
Yoon, Daeki's avatar
Yoon, Daeki committed
105
106
107
108
109
110
111
112
113
114
115
116
117
  try {
    let user = await User.findById(id)
      .exec()
    if (!user) {
      return res.status(400).json({
        error: 'User not found'
      })
    }
    req.profile = user
    next()
  } catch (error) {
    return res.status(400).json({
      error: 'Could not retrieve user'
Yoon, Daeki's avatar
Yoon, Daeki committed
118
119
120
121
122
123
124
    })
  }
}

export default {
  create,
  list,
Yoon, Daeki's avatar
Yoon, Daeki committed
125
126
127
  read,
  update,
  remove,
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
128
  isInstructor,
Yoon, Daeki's avatar
Yoon, Daeki committed
129
  isAdmin,
Yoon, Daeki's avatar
Yoon, Daeki committed
130
  userById,
Yoon, Daeki's avatar
Yoon, Daeki committed
131
}