-
Notifications
You must be signed in to change notification settings - Fork 10
/
users-socket.js
55 lines (35 loc) · 1.48 KB
/
users-socket.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
const users = [];
const shops = [];
const addUser = ({ id, shopId, tableNo }) => {
/*shopId = shopId.trim().toLowerCase();
tableNo = tableNo.trim().toLowerCase();*/
const existingUserInTable = users.find((user) => user.shopId === shopId && user.tableNo === tableNo);
//if(!name || !room) return { error: 'Username and room are required.' };
if(existingUserInTable) return { error: 'Table is taken.' };
const user = { id, shopId, tableNo };
users.push(user);
return { user };
}
// method for creating new shop socket
/*const addShop = ({ id, shopId}) => {
const existingShop = shops.find((shop) => shop.shopId === shopId);
//if(!name || !room) return { error: 'Username and room are required.' };
if(existingShop) return { error: 'Shop Already joined to a socket.' };
const shop = { id, shopId };
shops.push(shop);
return { shop };
}*/
const removeUser = (id) => {
const index = users.findIndex((user) => user.id === id);
if(index !== -1) return users.splice(index, 1)[0];
}
// method to remote shop from shops Array
/*const removeShop = (id) => {
const index = shops.findIndex((shop) => shop.id === id);
if(index !== -1) return shops.splice(index, 1)[0];
}*/
const getUser = (id) => users.find((user) => user.id === id);
//get shop from shops array
//const getShop = (id) => shops.find((shop) => shop.id === id);
//const getUsersInRoom = (room) => users.filter((user) => user.room === room);
module.exports = { addUser, removeUser, getUser, getUsersInRoom };