summaryrefslogtreecommitdiff
path: root/server/controllers/users.js
blob: e10df1bd479def4171d04fcb81ea6104e77121af (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import UserDetails from '../models/users';
import sendSMS from '../services/twilio';
import generateAadhaar from '../utils/aadhaar';

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, dob});
    if (existingUser) {
      return res.status(400).json({message: 'User already exists.'});
    }

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

    return res.status(200).json({result});
  } catch (error) {
    res.status(500).json({message: error});
  }
};

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 getUserByAadhaarNumber = async (req, res) => {
  const {aadhaar} = req.params;
  const aadhaarNumber = Number(aadhaar);
  try {
    const user = await UserDetails.findOne({aadhaarNumber});
    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;
  try {
    const user = await UserDetails.findByIdAndUpdate(
      id,
      {$set: req.body},
      {new: true}
    );

    if (!user.aadhaarNumber) {
      await UserDetails.findByIdAndUpdate(id, {
        $set: {aadhaarNumber: generateAadhaar()},
      });

      sendSMS(user,mobile, `Dear ${user.name}, your Aadhaar Number is ${user.aadhaarNumber} for the EID ${user._id}.`);
    }

    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});
  }
};

const getUpdatingUsers = async (req, res) => {
  try {
    const updatingUsers = await UserDetails.find({
      isUpdating: true,
      verified: true,
    });
    return res.status(200).json(updatingUsers);
  } catch (error) {
    res.status(404).json({message: error.message});
  }
};

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