-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.js
86 lines (79 loc) · 1.88 KB
/
accounts.js
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
/* eslint-disable no-console */
const db = require('../models');
const util = require('../utilities');
const returnNew = { new: true };
async function create(req, res) {
try {
if (!req.body.email) {
util.Error.throwError(400);
}
const data = {
email: req.body.email.toLowerCase().trim(),
};
const duplicate = await db.Account.findOne(data);
if (duplicate) {
util.Error.throwError(409);
}
const newAccount = await db.Account.create(data);
res.status(201).json(newAccount);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function search(req, res) {
try {
const query = req.query.query || '';
const options = req.query;
delete options.query;
// options.state = { $nin: ['new', 'inactive'] };
const foundAccounts = await db.Account
.fuzzySearch(query, options)
.select('email name picUrl model');
res.json(foundAccounts);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function show(req, res) {
try {
const accountId = req.params.id;
util.Error.validateUUID(accountId);
const showAccount = await db.Account
.findById(accountId)
.select('email name picUrl model');
res.json(showAccount);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function update(req, res) {
try {
const updateAccount = await db.Account.findByIdAndUpdate(
req.params.id,
req.body,
returnNew,
);
res.json(updateAccount);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function deactivate(req, res) {
try {
const deactivateAccount = await db.Account.findByIdAndUpdate(
req.params.id,
{ active: false },
returnNew,
);
res.json(deactivateAccount);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
module.exports = {
create,
search,
show,
update,
deactivate,
};