-
Notifications
You must be signed in to change notification settings - Fork 1
/
offers.js
101 lines (95 loc) · 2.54 KB
/
offers.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
// Fetch offers which this user should see
app.get('/api/offers-for-me', function(req, res) {
var user_id = req.param('user_id');
con.query(
'SELECT id,store_id,name,info,img ' +
'FROM offers WHERE status=1 ORDER BY id DESC LIMIT 1',
function(err, data) {
if (!err) {
if (data.length == 0) return res.json({ response: 0 });
else {
// no offers to show
// Fetched latest offer, but gotta check if this user has already seen it
var offer_id = data[0].id;
con.query(
'SELECT id FROM seen_offers WHERE offer_id=? AND user_id=? LIMIT 1',
[offer_id, user_id],
function(err, seen) {
if (!err) {
if (seen.length) return res.json({ response: 0 });
else {
// Seen
// Hasn't seen yet, record that they see it now
con.query(
'INSERT INTO seen_offers(offer_id,user_id) VALUES(?,?)',
[offer_id, user_id],
function(err, insert) {
if (err) return res.json({ response: 0 });
res.json({
response: data[0]
});
}
);
}
} else {
res.json({ response: 0, err });
}
}
);
}
} else {
res.json({ response: 0, err });
}
}
);
});
// Fetch all available offers (for Offers screen maybe)
app.get('/api/all-offers', function(req, res) {
con.query(
'SELECT id,store_id,name,info,img ' +
'FROM offers WHERE status=1 ORDER BY id DESC',
function(err, data) {
if (!err) {
if (data.length == 0) return res.json({ response: 0 });
else {
data.forEach((offer,i)=>{
con.query('SELECT id AS `key`, display_name AS name, img AS image, info AS `desc`,'+
'delivery_cost AS deliver_price, delivery_time AS time, min_delivery_cost, status '+
'FROM stores where id = ?',[offer.store_id],function(err,resturant){
offer.resturant = resturant[0];
console.log('i'+i);
if(i == data.length-1){
res.json({
response: data
});
}
})
})
}
} else {
res.json({ response: 0, err });
}
}
);
});
// fecth single offer details
app.get('/api/offer', function(req, res) {
var offer_id = req.param('offer_id');
con.query(
'SELECT id,store_id,name,info,img ' +
'FROM offers WHERE status=1 AND id = ? ORDER BY id DESC',
[offer_id],
function(err, data) {
if (!err) {
if (data.length == 0) return res.json({ response: 0 });
else {
res.json({
response: data
});
}
} else {
res.json({ response: 0, err });
}
}
);
});