Skip to content

Commit feb199d

Browse files
done
1 parent a98865a commit feb199d

File tree

15 files changed

+130
-44
lines changed

15 files changed

+130
-44
lines changed

controllers/contacts/getContactById.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
const { Contact } = require('../../models');
2-
const { createError } = require('../../utils');
2+
const { getErrorMessage } = require('../../utils');
33

44
const getContactById = async (req, res) => {
55
const { contactId } = req.params;
6-
const result = await Contact.findById(contactId);
7-
if (!result) throw createError(404, `Contact with id=${contactId} not found`);
6+
const { _id } = req.user;
7+
const result = await Contact.findById(contactId, _id);
8+
if (!result) {
9+
return res
10+
.status(404)
11+
.json(getErrorMessage(404, `Contact with id: '${contactId}' not found`));
12+
}
813

914
res.status(200).json({
1015
status: 'success',

controllers/contacts/removeContact.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
const { Contact } = require('../../models');
2-
const { createError } = require('../../utils');
2+
const { getErrorMessage } = require('../../utils');
33

44
const removeContact = async (req, res) => {
55
const { contactId } = req.params;
6-
const result = await Contact.findByIdAndRemove(contactId);
7-
if (!result) throw createError(404, `Contact with id=${contactId} not found`);
6+
const { _id } = req.user;
7+
const result = await Contact.findByIdAndRemove(contactId, _id);
8+
if (!result) {
9+
return res
10+
.status(404)
11+
.json(getErrorMessage(404, `Contact with id: '${contactId}' not found`));
12+
}
813

914
res.status(200).json({
1015
status: 'success',

controllers/contacts/updateContact.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
const { Contact } = require('../../models');
2-
const { createError } = require('../../utils');
2+
const { getErrorMessage } = require('../../utils');
33

44
const updateContact = async (req, res) => {
55
const { contactId } = req.params;
6+
const { _id } = req.user;
67
const result = await Contact.findByIdAndUpdate(contactId, req.body, {
8+
_id,
79
new: true,
810
});
911

10-
if (!result) throw createError(404, `Contact with id=${contactId} not found`);
12+
if (!result) {
13+
return res
14+
.status(404)
15+
.json(getErrorMessage(404, `Contact with id=${contactId} not found`));
16+
}
1117

1218
res.status(200).json({
1319
status: 'success',

controllers/contacts/updateStatusContact.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
const { Contact } = require('../../models');
2-
const { createError } = require('../../utils');
2+
const { getErrorMessage } = require('../../utils');
33

44
const updateStatusContact = async (req, res) => {
55
const { contactId } = req.params;
66
const { favorite } = req.body;
7+
const { _id } = req.user;
8+
9+
if (!favorite) {
10+
return res.status(400).json(getErrorMessage(400, `Missing field favorite`));
11+
}
712

8-
if (!favorite) throw createError(400, `missing field favorite`);
913
const result = await Contact.findByIdAndUpdate(
1014
contactId,
1115
{ favorite },
1216
{
17+
_id,
1318
new: true,
1419
},
1520
);
1621

17-
if (!result) return res.status(404).json(getErrorMessage(404, contactId));
22+
if (!result) {
23+
return res
24+
.status(404)
25+
.json(getErrorMessage(404, `Contact with id=${contactId} not found`));
26+
}
1827

1928
res.status(200).json({
2029
status: 'success',

controllers/users/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ const signup = require('./signup');
22
const login = require('./login');
33
const logout = require('./logout');
44
const getCurrent = require('./getCurrent');
5+
const updateSubscription = require('./updateSubscription');
56

67
module.exports = {
78
signup,
89
login,
910
logout,
1011
getCurrent,
12+
updateSubscription,
1113
};

controllers/users/login.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
const bcrypt = require('bcrypt');
22
const jwt = require('jsonwebtoken');
33
const { User } = require('../../models');
4-
const { createError } = require('../../utils');
4+
const { getErrorMessage } = require('../../utils');
55
const { joiLoginSchema } = require('../../models/user');
66
const { SECRET_KEY } = process.env;
77

88
const login = async (req, res) => {
9-
const { error } = joiLoginSchema.validate(req.body);
10-
if (error) throw createError(400, error.message);
9+
// const { error } = joiLoginSchema.validate(req.body);
10+
// if (error) throw createError(400, error.message);
1111

1212
const { email, password } = req.body;
1313
const user = await User.findOne({ email });
1414

15+
if (!user) {
16+
return res
17+
.status(401)
18+
.json(getErrorMessage(401, 'Email or password is wrong'));
19+
}
20+
1521
const comparePassword = bcrypt.compareSync(password, user.password);
16-
if (!user || !comparePassword) {
17-
throw createError(401, 'Email or password is wrong');
22+
if (!comparePassword) {
23+
return res
24+
.status(401)
25+
.json(getErrorMessage(401, 'Email or password is wrong'));
1826
}
1927

28+
// {
29+
// throw Error(401, 'Email or password is wrong');
30+
// }
31+
2032
const payload = {
2133
id: user._id,
2234
};

controllers/users/signup.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
const bcrypt = require('bcrypt');
22
const { User } = require('../../models');
3-
const { createError } = require('../../utils');
4-
const { joiSignupSchema } = require('../../models/user');
3+
const { getErrorMessage } = require('../../utils');
54

65
const signup = async (req, res) => {
7-
const { error } = joiSignupSchema.validate(req.body);
8-
if (error) throw createError(400, error.message);
9-
106
const { email, password, subscription } = req.body;
117

128
const user = await User.findOne({ email });
13-
if (user) throw createError(409, `Email: '${email}' in use`);
9+
if (user) {
10+
return res
11+
.status(409)
12+
.json(getErrorMessage(409, `Email: '${email}' in use`));
13+
}
1414

1515
const hashPassword = bcrypt.hashSync(password, bcrypt.genSaltSync(10));
1616
const result = await User.create({
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { User } = require('../../models');
2+
const { getErrorMessage } = require('../../utils');
3+
4+
const updateSubscription = async (req, res) => {
5+
const { _id, subscription } = req.user;
6+
7+
if (!subscription) {
8+
return res
9+
.status(400)
10+
.json(getErrorMessage(400, `Missing field subscription`));
11+
}
12+
13+
const result = await User.findByIdAndUpdate(
14+
_id,
15+
{ subscription: req.body.subscription },
16+
{
17+
new: true,
18+
},
19+
);
20+
21+
res.status(200).json({
22+
status: 'success',
23+
code: 200,
24+
data: {
25+
email: result.email,
26+
subscription: result.subscription,
27+
},
28+
});
29+
};
30+
31+
module.exports = updateSubscription;

middlewares/auth.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const jwt = require('jsonwebtoken');
2-
const { createError } = require('../utils');
2+
const { getErrorMessage } = require('../utils');
33
const { User } = require('../models');
44
const { SECRET_KEY } = process.env;
55

@@ -8,11 +8,14 @@ const auth = async (req, res, next) => {
88
const [bearer, token] = authorization.split(' ');
99

1010
try {
11-
if (bearer !== 'Bearer') throw createError(401, 'Not authorized');
11+
if (bearer !== 'Bearer')
12+
return res.status(401).json(getErrorMessage(401, 'Not authorized'));
1213

1314
const { id } = jwt.verify(token, SECRET_KEY);
1415
const user = await User.findById(id);
15-
if (!user || !user.token) throw createError(401, 'Not authorized');
16+
if (!user || !user.token) {
17+
return res.status(401).json(getErrorMessage(401, 'Not authorized'));
18+
}
1619

1720
req.user = user;
1821
next();

models/user.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ const joiLoginSchema = Joi.object({
3838
.required(),
3939
});
4040

41+
const joiSubscriptionSchema = Joi.object({
42+
subscription: Joi.string().valueOf('starter', 'pro', 'business').required(),
43+
});
44+
4145
const User = model('user', userSchema);
4246

4347
module.exports = {
4448
User,
4549
joiSignupSchema,
4650
joiLoginSchema,
51+
joiSubscriptionSchema,
4752
};

0 commit comments

Comments
 (0)