summaryrefslogtreecommitdiff
path: root/server/controllers/users.js
blob: 3dfed1400940fe50aec61a7248641604d3c8fb97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import UserDetails from '../models/users';
import sendOTP from '../services/twilio';

const createUser = async (req, res) => {
  const {
    indianResident,
    name,
    gender,
    dob,
    mobile,
    email,
    address,
    photo,
    documents,
    biometrics,
  } = req.body;

  try {
    const existingUser = await UserDetails.findOne({name, mobile, email});
    if (existingUser) {
      // sendOTP('+919696712475', 'Hi, I am Rohan Raj Gupta');
      return res.status(400).json({message: 'User already exists.'});
    }

    const result = await UserDetails.create({
      indianResident,
      name,
      gender,
      dob,
      mobile,
      email,
      address,
      photo,
      documents,
      biometrics,
    });

    const aadhaarNumber = 281943258754;

    return res.status(200).json({result, aadhaarNumber});
  } catch (error) {
    res.status(500).json({message: 'Something went wrong.'});
  }
};

const getUser = async (req, res) => {
  const {id} = req.params;

  try {
    const user = await UserDetails.findById(id);
    res.status(200).json(user);
  } catch (error) {
    res.status(404).json({message: error});
  }
};

const getVerifiedUsers = async (req, res) => {
  try {
    const verifiedUsers = await UserDetails.find({verified: true});

    return res.status(200).json(verifiedUsers);
  } catch (error) {
    res.status(404).json({message: error.message});
  }
};

const getUnverifiedUsers = async (req, res) => {
  try {
    const unverifiedUsers = await UserDetails.find({verified: false});

    console.log(unverifiedUsers);

    return res.status(200).json(unverifiedUsers);
  } catch (error) {
    res.status(404).json({message: error.message});
  }
};

const updateUser = async (req, res) => {
  const {id} = req.params;

  console.log(id, req.body);
  try {
    await UserDetails.findByIdAndUpdate(id, {$set: req.body}, {new: true});

    res.status(200).json({message: 'User Updated Successfully'});
  } catch (error) {
    res.status(404).json({message: error.message});
  }
};

const deleteUser = async (req, res) => {
  const {id} = req.params;
  try {
    if (await UserDetails.findById(id)) {
      const user = await UserDetails.findByIdAndDelete(id);
      res.status(200).json({message: 'User Deleted Successfully'});
    }
  } catch (error) {
    res.status(404).json({message: error.message});
  }
};

export default {
  createUser,
  getUser,
  getVerifiedUsers,
  getUnverifiedUsers,
  updateUser,
  deleteUser,
};