-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaiter.js
155 lines (139 loc) · 4.98 KB
/
waiter.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
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
145
146
147
148
149
150
151
152
153
154
155
module.exports = Waiter = (pool) => {
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const week_days = async () => {
for (const day of days) {
await pool.query('INSERT INTO weekdays (dayName) VALUES ($1)', [day]);
}
}
const findUser = async (username,job_Type) => {
if (username != undefined || username != ""
&& job_Type !==undefined || job_Type !=='') {
let found = await pool.query('SELECT * FROM waiterDB WHERE username=$1 And position=$2 ', [username,job_Type]);
if (found.rowCount === 1) {
return found.rows[0].position;
}
else {
return false;
}
}
}
const findusername = async (username) => {
let found = await pool.query('SELECT full_name FROM waiterDB WHERE username=$1', [username]);
return found.rows[0].full_name;
}
const getWeekdays = async (username) => {
let storedDays = await pool.query('SELECT dayName FROM weekdays');
let selectedShift = await findSelectedDays(username);
for (let i = 0; i < storedDays.rowCount; i++) {
let current = storedDays.rows[i];
selectedShift.forEach(days => {
let found = days.dayname;
if (current.dayname === found) {
current.checked = true;
}
})
}
return storedDays.rows;
}
const addWaiter = async (username, full_name, position) => {
if (username !== "" && full_name !== "" && position !== "") {
await pool.query('INSERT INTO waiterDB (username,full_name,position) VALUES ($1,$2,$3)', [username, full_name, position]);
return true;
} else {
false;
}
}
const selectShift = async (shift) => {
const weekdays = shift.days;
const findUsernameID = await pool.query('SELECT id From waiterDB WHERE username=$1', [shift.username]);
if (findUsernameID.rowCount > 0) {
let userID = findUsernameID.rows[0].id;
await pool.query('DELETE FROM dayShifts WHERE waiter_id =$1', [userID]);
for (let day of weekdays) {
let findDayID = await pool.query('SELECT id From weekdays WHERE dayName=$1', [day]);
await pool.query('INSERT INTO dayShifts (waiter_id,weekday_id) VALUES($1,$2)', [userID, findDayID.rows[0].id]);
}
return true;
} else {
return false;
}
}
const allShifts = async () => {
let storedShifts = await pool.query(
`SELECT full_name, dayName FROM dayShifts
JOIN waiterDB ON waiterDB.id = dayShifts.waiter_id
JOIN weekdays ON weekdays.id = dayShifts.weekday_id`
)
return storedShifts.rows;
}
const groupByDay = async () => {
let storedShifts = await allShifts();
const shiftArray = [{
id: 0,
day: 'Sunday',
Waiters: []
}, {
id: 1,
day: 'Monday',
Waiters: []
}, {
id: 2,
day: 'Tuesday',
Waiters: []
}, {
id: 3,
day: 'Wednesday',
Waiters: []
}, {
id: 4,
day: 'Thursday',
Waiters: []
}, {
id: 5,
day: 'Friday',
Waiters: []
}, {
id: 7,
day: 'Saturday',
Waiters: []
}]
if (storedShifts.length > 0) {
for (let i = 0; i < storedShifts.length; i++) {
shiftArray.forEach(current => {
if (current.day === storedShifts[i].dayname) {
current.Waiters.push(storedShifts[i].full_name);
}
if (current.Waiters.length === 2) {
current.color = "orange";
}
if (current.Waiters.length > 3) {
current.color = "blue";
}
if (current.Waiters.length === 3) {
current.color = "green";
}
})
}
}
return shiftArray;
}
const deleteShifts = async () => {
let clear = await pool.query('DELETE FROM dayShifts');
return clear.rows;
}
const findSelectedDays = async (username) => {
let foundDays = await pool.query('SELECT dayName FROM dayShifts JOIN waiterDB ON waiterDB.id = dayShifts.waiter_id JOIN weekdays ON weekdays.id = dayShifts.weekday_id WHERE username=$1', [username]);
return foundDays.rows;
}
return {
add_waiter: addWaiter,
foundUser: findUser,
getUsername: findusername,
weekDays: week_days,
getdays: getWeekdays,
dayShift: selectShift,
clearShifts: deleteShifts,
allShifts,
groupByDay,
}
}