Skip to content

Commit a31b8bb

Browse files
committed
Better config for Prettier
1 parent 3edbe49 commit a31b8bb

File tree

10 files changed

+2563
-2788
lines changed

10 files changed

+2563
-2788
lines changed

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"arrowParens": "always"
5+
}

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ This is a simple API server that implements a logic required to correct work of
1212

1313
To run this server localy you need to have these requirements:
1414

15-
* [Node.js](https://nodejs.org)
16-
* [MongoDB](https://www.mongodb.com/download-center#community)
15+
- [Node.js](https://nodejs.org)
16+
- [MongoDB](https://www.mongodb.com/download-center#community)
1717

1818
## Installations
1919

@@ -41,24 +41,24 @@ http://localhost:8000/v1/chats
4141

4242
Here's the map of API's HTTP routes:
4343

44-
* `/` — routes related to authentication.
45-
* `/signup` **POST** — create new user with `username` and `password`.
46-
* `/login` **POST** — log user in with `username` and `password`.
47-
* `/logout` **GET** — log out active user.
48-
* `/users` — routes related to users.
49-
* `/users` **GET** — retrieve data about all users.
50-
* `/users/me` **GET** — retrieve my user's data.
51-
* `/users/me` **POST** — update my user's information (`username`, `firstName`, `lastName` and `city`).
52-
* `/users/:id` **GET** — retrieve information about user with specific `:id`.
53-
* `/chats` — routes related to chats.
54-
* `/chats` **GET** — retrieve information about all chats.
55-
* `/chats` **POST** — create new chat with specified `title`.
56-
* `/chats/my` **GET** — get list of all user's chats.
57-
* `/chats/:id` **GET** — get chat's information with messages by specific chat's `:id`.
58-
* `/chats/:id` **POST** — send new message to chat with specific `:id`.
59-
* `/chast/:id` **DELETE** — delete chat with specific `:id`. Only creator of the chat can delete it.
60-
* `/chats/:id/join` **GET** — join chat with specific `:id`.
61-
* `/chats/:id/leave` **GET** — leave chat with specific `:id`.
44+
- `/` — routes related to authentication.
45+
- `/signup` **POST** — create new user with `username` and `password`.
46+
- `/login` **POST** — log user in with `username` and `password`.
47+
- `/logout` **GET** — log out active user.
48+
- `/users` — routes related to users.
49+
- `/users` **GET** — retrieve data about all users.
50+
- `/users/me` **GET** — retrieve my user's data.
51+
- `/users/me` **POST** — update my user's information (`username`, `firstName`, `lastName` and `city`).
52+
- `/users/:id` **GET** — retrieve information about user with specific `:id`.
53+
- `/chats` — routes related to chats.
54+
- `/chats` **GET** — retrieve information about all chats.
55+
- `/chats` **POST** — create new chat with specified `title`.
56+
- `/chats/my` **GET** — get list of all user's chats.
57+
- `/chats/:id` **GET** — get chat's information with messages by specific chat's `:id`.
58+
- `/chats/:id` **POST** — send new message to chat with specific `:id`.
59+
- `/chast/:id` **DELETE** — delete chat with specific `:id`. Only creator of the chat can delete it.
60+
- `/chats/:id/join` **GET** — join chat with specific `:id`.
61+
- `/chats/:id/leave` **GET** — leave chat with specific `:id`.
6262

6363
If you're using [Insomnia](https://insomnia.rest/) for debugging APIs, you can download a workspace backup:
6464

@@ -75,25 +75,25 @@ import SocketIOClient from 'socket.io-client';
7575

7676
socket = SocketIOClient('path/to/api', {
7777
query: {
78-
token: '...your access-token here...'
79-
}
78+
token: '...your access-token here...',
79+
},
8080
});
8181
```
8282

8383
Here's the list of events:
8484

8585
#### Emmiting
8686

87-
* `new-message` — emmited when someone sends new message to specific chat.
88-
* `new-chat` — emmited when someone creates new chat.
89-
* `deleted-chat` — emmited when someone deletes a chat.
87+
- `new-message` — emmited when someone sends new message to specific chat.
88+
- `new-chat` — emmited when someone creates new chat.
89+
- `deleted-chat` — emmited when someone deletes a chat.
9090

9191
#### Listening
9292

93-
* `connection` — connection of socket.io client.
94-
* `mount-chat` — mount a client to listen for messages in chat with specific `:chatId`.
95-
* `unmount-chat` — unmout a client from listening for messages in chat with specific `:chatId`.
96-
* `send-message` — send message with `content` to chat with
93+
- `connection` — connection of socket.io client.
94+
- `mount-chat` — mount a client to listen for messages in chat with specific `:chatId`.
95+
- `unmount-chat` — unmout a client from listening for messages in chat with specific `:chatId`.
96+
- `send-message` — send message with `content` to chat with
9797

9898
## License
9999

controllers/auth.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ function signUp(username, password) {
2929

3030
return newUser.save();
3131
})
32-
.then(savedUser => userController.getUserById(savedUser._id))
32+
.then((savedUser) => userController.getUserById(savedUser._id))
3333
.then(({ user }) => {
3434
const token = jwt.sign(
3535
{ userId: user._id },
3636
JWT_SECRET,
37-
{ expiresIn: 60 * 60 * 24 * 10 }, // 10 days
37+
{ expiresIn: 60 * 60 * 24 * 10 } // 10 days
3838
);
3939

4040
return Promise.resolve({
@@ -65,7 +65,10 @@ function login(username, password) {
6565
});
6666
}
6767

68-
return Promise.all([Promise.resolve(user), user.comparePassword(password)]);
68+
return Promise.all([
69+
Promise.resolve(user),
70+
user.comparePassword(password),
71+
]);
6972
})
7073
.then(([user, isPasswordMatch]) => {
7174
if (!isPasswordMatch) {
@@ -76,12 +79,12 @@ function login(username, password) {
7679
}
7780
return user;
7881
})
79-
.then(savedUser => userController.getUserById(savedUser._id))
82+
.then((savedUser) => userController.getUserById(savedUser._id))
8083
.then(({ user }) => {
8184
const token = jwt.sign(
8285
{ userId: user._id },
8386
JWT_SECRET,
84-
{ expiresIn: 60 * 60 * 24 * 10 }, // 10 days
87+
{ expiresIn: 60 * 60 * 24 * 10 } // 10 days
8588
);
8689

8790
return Promise.resolve({

controllers/chats.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ function getAllChats() {
88
.populate({ path: 'members', select: 'username firstName lastName' })
99
.lean()
1010
.exec()
11-
.then(chats =>
11+
.then((chats) =>
1212
Promise.resolve({
1313
success: true,
1414
chats,
15-
}));
15+
})
16+
);
1617
}
1718

1819
function getMyChats(userId) {
@@ -23,11 +24,12 @@ function getMyChats(userId) {
2324
.populate({ path: 'members', select: 'username firstName lastName' })
2425
.lean()
2526
.exec()
26-
.then(chats =>
27+
.then((chats) =>
2728
Promise.resolve({
2829
success: true,
2930
chats,
30-
}));
31+
})
32+
);
3133
}
3234

3335
function joinChat(userId, chatId) {
@@ -45,7 +47,9 @@ function joinChat(userId, chatId) {
4547
}
4648

4749
const isCreator = chat.creator._id.toString() === userId;
48-
const isMember = chat.members.some(member => member._id.toString() === userId);
50+
const isMember = chat.members.some(
51+
(member) => member._id.toString() === userId
52+
);
4953

5054
if (isCreator || isMember) {
5155
return Promise.reject({
@@ -63,7 +67,7 @@ function joinChat(userId, chatId) {
6367
},
6468
{
6569
new: true,
66-
},
70+
}
6771
)
6872
.populate({ path: 'creator', select: 'username firstName lastName' })
6973
.populate({ path: 'members', select: 'username firstName lastName' })
@@ -83,7 +87,8 @@ function joinChat(userId, chatId) {
8387
success: statusMessage.success,
8488
message: statusMessage.message,
8589
chat,
86-
}));
90+
})
91+
);
8792
}
8893

8994
function leaveChat(userId, chatId) {
@@ -101,12 +106,15 @@ function leaveChat(userId, chatId) {
101106
}
102107

103108
const isCreator = chat.creator._id.toString() === userId;
104-
const isMember = chat.members.some(member => member._id.toString() === userId);
109+
const isMember = chat.members.some(
110+
(member) => member._id.toString() === userId
111+
);
105112

106113
if (isCreator) {
107114
return Promise.reject({
108115
success: false,
109-
message: 'You cannot delete your own chat! You can only delete you own chats.',
116+
message:
117+
'You cannot delete your own chat! You can only delete you own chats.',
110118
});
111119
}
112120

@@ -124,7 +132,7 @@ function leaveChat(userId, chatId) {
124132
},
125133
{
126134
new: true,
127-
},
135+
}
128136
)
129137
.populate({ path: 'creator', select: 'username firstName lastName' })
130138
.populate({ path: 'members', select: 'username firstName lastName' })
@@ -144,7 +152,8 @@ function leaveChat(userId, chatId) {
144152
success: statusMessage.success,
145153
message: statusMessage.message,
146154
chat,
147-
}));
155+
})
156+
);
148157
}
149158

150159
function getChat(userId, chatId) {
@@ -178,18 +187,20 @@ function newChat(userId, data = {}) {
178187

179188
return chat
180189
.save()
181-
.then(createdChat =>
190+
.then((createdChat) =>
182191
Chat.findById(createdChat._id)
183192
.populate({ path: 'creator', select: 'username firstName lastName' })
184193
.populate({ path: 'members', select: 'username firstName lastName' })
185194
.lean()
186-
.exec())
187-
.then(createdChat =>
195+
.exec()
196+
)
197+
.then((createdChat) =>
188198
Promise.resolve({
189199
success: true,
190200
message: 'Chat has been created',
191201
chat: createdChat,
192-
}));
202+
})
203+
);
193204
}
194205

195206
function deleteChat(userId, chatId) {
@@ -212,7 +223,8 @@ function deleteChat(userId, chatId) {
212223
Promise.resolve({
213224
success: true,
214225
message: 'Chat deleted!',
215-
}));
226+
})
227+
);
216228
}
217229

218230
module.exports = {

controllers/messages.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
const Message = require('../models/Message');
22

33
function sendMessage(userId, chatId, data) {
4-
const message = new Message(Object.assign({}, data, {
5-
chatId,
6-
sender: userId,
7-
statusMessageUserId: userId,
8-
}));
4+
const message = new Message(
5+
Object.assign({}, data, {
6+
chatId,
7+
sender: userId,
8+
statusMessageUserId: userId,
9+
})
10+
);
911

1012
return message
1113
.save()
12-
.then(savedMessage =>
14+
.then((savedMessage) =>
1315
Message.findById(savedMessage._id)
1416
.populate({ path: 'sender', select: 'username firstName lastName' })
15-
.exec())
16-
.then(savedMessage =>
17+
.exec()
18+
)
19+
.then((savedMessage) =>
1720
Promise.resolve({
1821
success: true,
1922
message: savedMessage,
20-
}));
23+
})
24+
);
2125
}
2226

2327
function getAllMessages(chatId) {
2428
return Message.find({ chatId })
2529
.populate({ path: 'sender', select: 'username firstName lastName' })
2630
.sort({ createdAt: 1 })
2731
.exec()
28-
.then(messages =>
32+
.then((messages) =>
2933
Promise.resolve({
3034
success: true,
3135
messages,
32-
}));
36+
})
37+
);
3338
}
3439

3540
module.exports = {

controllers/users.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ function getAllUsers(exceptId) {
77
return User.find({ _id: { $ne: exceptId } })
88
.select('username firstName lastName')
99
.exec()
10-
.then(users =>
10+
.then((users) =>
1111
Promise.resolve({
1212
success: true,
1313
message: 'Users has been found',
1414
users,
15-
}));
15+
})
16+
);
1617
}
1718

1819
// Get profile data for specific user by id
@@ -31,20 +32,24 @@ function getUserChats(userId) {
3132
.sort({ createdAt: -1 })
3233
.lean()
3334
.exec()
34-
.then(chats => chats || []);
35+
.then((chats) => chats || []);
3536
}
3637

3738
// Count the amount of messages by specific user
3839
function countUserMessages(userId) {
3940
return Message.find({ sender: userId, statusMessage: false })
4041
.count()
4142
.exec()
42-
.then(count => count || 0);
43+
.then((count) => count || 0);
4344
}
4445

4546
// Get User data by specific id
4647
function getUserById(userId) {
47-
const userPromises = [getUserData(userId), getUserChats(userId), countUserMessages(userId)];
48+
const userPromises = [
49+
getUserData(userId),
50+
getUserChats(userId),
51+
countUserMessages(userId),
52+
];
4853

4954
return Promise.all(userPromises).then(([user, chats, messagesCount]) => {
5055
if (!user) {
@@ -94,8 +99,9 @@ function editUser(userId, data) {
9499
},
95100
{
96101
new: true,
97-
},
98-
).select('username firstName lastName'))
102+
}
103+
).select('username firstName lastName')
104+
)
99105
.then((user) => {
100106
if (!user) {
101107
return Promise.reject({

models/Chat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const chatSchema = new mongoose.Schema(
1515
},
1616
],
1717
},
18-
{ timestamps: true },
18+
{ timestamps: true }
1919
);
2020

2121
module.exports = mongoose.model('Chat', chatSchema);

0 commit comments

Comments
 (0)