· 4 years ago · Jan 26, 2021, 02:48 PM
1const Session = require('../models/Session');
2const bcrypt = require('bcryptjs');
3const Table = require('../models/Table');
4const pusher = require('../config/pusher');
5const Restaurant = require('../models/Restaurant');
6const moment = require('moment');
7const today = moment().startOf('day');
8
9
10
11// @desc Start a session
12// @route POST /api/session/Start
13// @access Public
14// CHANGES: from frontend we need allowMultiple value to be passed here!
15exports.Start_Session = async (req, res, next) => {
16 try {
17 console.log(req.body);
18 const { restaurant, table, allowMultiple} = req.body;
19 const table_1 = await Table.findById(table);
20 const r = await Restaurant.findById(restaurant);
21 if (!r.Open_Now || !r.Authenticated) {
22 return res.status(404).json({
23 success: false,
24 msg: 'Restaurant Closed',
25 });
26 }
27 if (table_1.status === 'disabled') {
28 return res.status(400).json({
29 success: false,
30 msg: 'This QR code is disabled.',
31 });
32 }
33 const sess = await Session.findOne({
34 table: table,
35 active: true,
36 });
37 console.log(`allowMultiple: ${allowMultiple} ` )
38 // for Single Session ONLY, ie if allowMultiple=false (for multiple, table.status always 'active')
39 if (!allowMultiple && sess && table_1.status === 'active') {
40 console.log(1);
41 return res.status(401).json({
42 success: false,
43 msg: 'The table is Engaged',
44 flag: -1,
45 join: sess._id, //Prompt User to Join Session by providing the Session Password
46 tableNo: table_1.tableNo,
47 Restaurant_Name: r.Name, // Prompt to set Session Password
48 verify: sess.verify,
49 Short_Address: r.Short_Address,
50 });
51 }
52
53 console.log(2);
54
55 const session = await Session.create(req.body);
56 const t = await Table.findById(table);
57 return res.status(200).json({
58 success: true,
59 data: session,
60 tableNo: t.tableNo,
61 Restaurant_Name: r.Name,
62 Short_Address: r.Short_Address, // Prompt to set Session Password
63 });
64 } catch (error) {
65 console.log(error);
66 return res.status(500).json({
67 success: false,
68 error: error,
69 });
70 }
71};
72
73// NOTE: Only called for multiple session!
74// @desc For multiple sessions, user wants to join his missed session via credentials
75// @route POST /api/session/JoinMultipleSession
76// @access Public
77exports.Join_Multiple_Session = async (req, res, next) => {
78 try {
79 // verify ~ nickname
80 const { verify, password} = req.body;
81 const session = await Session.findOne({verify: verify, allowMultiple: true});
82
83 // No session found with given nickname
84 if (!session) {
85 return res.status(404).json({
86 success: false,
87 msg: 'There seems to be some error',
88 });
89 }
90
91 // Session found via nickname, but vendor closed the session
92 // User needs to create a new session
93 if (!session.active) {
94 return res.status(404).json({
95 success: false,
96 msg: 'Session Already Closed!. You need to create a new session',
97 });
98 }
99
100 // validating user entered pwd with pwd stored in session
101 const isMatch = await bcrypt.compare(password, session.password);
102
103 // incorrect password entered by user
104 if (!isMatch) {
105 return res
106 .status(400)
107 .json({ success: false, msg: 'Incorrect Credentials' });
108 }
109
110 // Success!
111 // Session is active
112 // Credentials matched
113 // Let user enter his session
114 return res.status(200).json({
115 success: true,
116 data: session,
117 });
118 } catch (error) {
119 return res.status(500).json({
120 success: false,
121 error: error,
122 });
123 }
124}
125
126// @desc Set nickname and password for a newly created session
127// @route POST /api/session/set_password/:session_id
128// @access Public
129// NOTE: as in v2, we want nickname to be unique!
130// CHANGES: from frontend we need allowMultiple value to be passed here!
131// DONE
132exports.Set_Session_Password = async (req, res, next) => {
133 try {
134 const { password, verify, allowMultiple } = req.body;
135 const { session_id } = req.params;
136 const salt = await bcrypt.genSalt(10);
137
138 // in case of multiple sessions, we want all nicknames to be unique!
139 if(allowMultiple){
140 console.log('HERERERERARA');
141 const _session = await Session.findOne({verify, allowMultiple: true});
142 // User with same nickname exists for an active session
143 // Current user should change his nickname
144 if(_session){
145 return res.status(404).json({
146 success: false,
147 msg: `${verify} nickname is already in use! Please enter your ${verify} <lastname>`,
148 });
149 }
150 }
151
152 const encrypted_password = await bcrypt.hash(password, salt);
153
154 const session = await Session.findByIdAndUpdate(
155 session_id,
156 {
157 password: encrypted_password,
158 authentication_requested: true,
159 verify: verify,
160 Date_Ref: Date.now(),
161 },
162 {
163 new: true,
164 runValidators: true,
165 }
166 );
167 console.log(1);
168 if (!session) {
169 return res.status(404).json({
170 success: false,
171 msg: 'There seems to be some error',
172 });
173 }
174 const s = await Session.findById(session_id).populate('table');
175 pusher.trigger('session_request', s.restaurant, s);
176 return res.status(200).json({
177 success: true,
178 data: session,
179 });
180 } catch (error) {
181 return res.status(500).json({
182 success: false,
183 error: error,
184 });
185 }
186};
187
188
189// @desc For single sessions, user wants to join his missed session via session_id and password
190// @route POST /api/session/Join_session/:session_id
191// @access Public
192// NOTE: Only called for single session!
193exports.Join_Session = async (req, res, next) => {
194 try {
195 const { password } = req.body;
196 const { session_id } = req.params;
197 const session = await Session.findById(session_id);
198 if (!session) {
199 return res.status(404).json({
200 success: false,
201 msg: 'There seems to be some error',
202 });
203 }
204 if (!session.active) {
205 return res.status(404).json({
206 success: false,
207 msg: 'Session Already Closed!',
208 });
209 }
210 const compare = await bcrypt.compare(password, session.password);
211 if (!compare) {
212 return res
213 .status(400)
214 .json({ success: false, msg: 'Incorrect Credentials' });
215 }
216 return res.status(200).json({
217 success: true,
218 data: session,
219 });
220 } catch (error) {
221 return res.status(500).json({
222 success: false,
223 error: error,
224 });
225 }
226}
227
228
229// @desc For single/multiple sessions, vendor approves the session
230// @route PUT /api/session/Authenticate_Session/:session_id/:table_id
231// @access Private
232// NOTE: Pass allowMultiple field in req.body for single and multiple sessions
233exports.Authenticate_Session = async (req, res, next) => {
234 try {
235 const table_1 = await Table.findById(req.params.table_id);
236 if (!table_1.allowMultiple && table_1.status === 'active') {
237 console.log(2);
238 return res.status(200).json({
239 success: false,
240 msg: 'Dine-In has already begun on this Table',
241 });
242 }
243 var IndiaDate = new Date().toLocaleString('en-us', {
244 timeZone: 'Asia/Kolkata',
245 });
246 const table = await Table.findByIdAndUpdate(
247 req.params.table_id,
248 {
249 status: 'active',
250 },
251 {
252 new: true,
253 runValidators: true,
254 }
255 );
256
257 if (!table) {
258 console.log(1);
259 return res.status(404).json({
260 success: false,
261 msg: 'There seems to be some error --table',
262 });
263 }
264 console.log(new Date(IndiaDate).toLocaleString());
265 const session = await Session.findByIdAndUpdate(
266 req.params.session_id,
267 {
268 active: true,
269 authenticatedBy: req.user._id,
270 Start: new Date(IndiaDate).toLocaleString(),
271 verified: true,
272 },
273 {
274 new: true,
275 runValidators: true,
276 }
277 );
278 console.log(2);
279 if (!session) {
280 return res.status(404).json({
281 success: false,
282 msg: 'There seems to be some error',
283 });
284 }
285 pusher.trigger('confirm_session', `${session.restaurant}`, session);
286 return res.status(200).json({
287 success: true,
288 data: session,
289 });
290 } catch (error) {
291 console.log(error)
292 return res.status(500).json({
293 success: false,
294 error: error,
295 });
296 }
297};
298
299//Get if the Session has been Authenticated to Order Items
300
301exports.Check_Status = async (req, res, next) => {
302 try {
303 const session = await Session.findById(req.params.session_id);
304 if (!session) {
305 return res.status(404).json({
306 success: false,
307 msg: 'There seems to be some error',
308 });
309 }
310
311 return res.status(200).json({
312 success: true,
313 data: session,
314 });
315 } catch (error) {
316 return res.status(500).json({
317 success: false,
318 error: error,
319 });
320 }
321};
322
323//Waiter and Admin Route
324
325exports.Session_List = async (req, res, next) => {
326 try {
327 const sessions = await Session.find({
328 restaurant: req.user.restaurant,
329 authentication_requested: true,
330 active: false,
331 verified: false,
332 }).populate('table');
333 console.log(sessions);
334 return res.status(200).json({
335 success: true,
336 data: sessions,
337 });
338 } catch (error) {
339 return res.status(500).json({
340 success: false,
341 error: error,
342 });
343 }
344};
345
346//Admin/Waiter
347exports.Delete_Session = async (req, res, next) => {
348 try {
349 const session = await Session.findByIdAndDelete(req.params.session_id);
350 if (!session) {
351 return res.status(400).json({
352 success: false,
353 msg: 'No such session exists',
354 });
355 }
356 return res.status(200).json({
357 success: true,
358 msg: 'Deleted Session',
359 });
360 } catch (error) {
361 return res.status(500).json({
362 success: false,
363 error: error,
364 });
365 }
366};
367
368//Admin Route
369
370exports.Remove_Spam_Sessions = async (req, res, next) => {
371 try {
372 const session = await Session.deleteMany({
373 authentication_requested: false,
374 restaurant: req.body.restaurant,
375 });
376 return res.status(200).json({
377 success: true,
378 msg: 'Deleted Session',
379 });
380 } catch (error) {
381 return res.status(500).json({
382 success: false,
383 error: error,
384 });
385 }
386};
387
388//To be Executed at the End of the Day to Remove any spam unverified Session Requests
389
390exports.Remove_unverified = async (req, res, next) => {
391 try {
392 const session = await Session.deleteMany({
393 verified: false,
394 restaurant: req.body.restaurant,
395 });
396 } catch (error) {
397 return res.status(500).json({
398 success: false,
399 error: error,
400 });
401 }
402};
403
404//Note:Close Session when generating Final Bill
405
406exports.Get_Daily_Sessions = async (req, res, next) => {
407 try {
408 const s_list = await Session.find({
409 Date_Ref: {
410 $gte: today.toDate(),
411 $lte: moment(today).endOf('day').toDate(),
412 },
413 verified: true,
414 active: true,
415 });
416 if (!s_list) {
417 return res.status(404).json({
418 success: false,
419 msg: 'Sessions Yet not started for the Day',
420 });
421 }
422 return res.status(200).json({
423 success: true,
424 data: s_list,
425 });
426 } catch (error) {
427 return res.status(500).json({
428 success: false,
429 error: error,
430 });
431 }
432};
433
434