-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuborders_helpers.js
147 lines (130 loc) · 5.35 KB
/
suborders_helpers.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
const db = require("./database/db-connector");
function addSuborderData(req, res) {
const orderID = req.body.order_id;
const menuID = JSON.parse(req.body.menu_id)[0];
const flavorID = req.body.flavor_id;
let containerID = null;
if (typeof req.body.container_id !== "undefined") {
containerID = `'${JSON.parse(req.body.container_id)[0]}'`;
}
const quantityOrdered = req.body.quantity_ordered;
const subtotal = req.body.subtotal;
let action = req.body.action;
console.log(`FOR ADD SUBORDER: Action is: ${action} for orderID: ${orderID}`);
// Check for an existing suborder with the same menu_id, container_id, and flavor_id
const check_existing_suborder = `SELECT * FROM Suborders WHERE order_id='${orderID}' AND menu_id='${menuID}' AND flavor_id='${flavorID}' AND (container_id=${containerID} OR (container_id IS NULL AND ${containerID} IS NULL))`;
db.pool.query(check_existing_suborder, function (err, rows, fields) {
if (!err) {
if (rows.length > 0) {
console.log(
"FOUND EXISTING SUBORDER THAT MATCHES INPUTTED ADD SUBORDER"
);
// Update the existing suborder's quantity and subtotal
const existingSuborderID = rows[0].suborder_item_id;
const newQuantity =
parseInt(rows[0].quantity_ordered) + parseInt(quantityOrdered);
const newSubtotal = parseInt(rows[0].subtotal) + parseInt(subtotal);
const update_existing_suborder = `UPDATE Suborders SET quantity_ordered=${newQuantity}, subtotal=${newSubtotal} WHERE suborder_item_id=${existingSuborderID}`;
db.pool.query(update_existing_suborder, function (err, rows, fields) {
if (!err) {
handleRedirect(action, orderID, res);
} else {
console.log(err);
res.status(500).send("Internal server error");
}
});
} else {
console.log(
"DID NOT FIND EXISTING SUBORDER THAT MATCHES INPUTTED ADD SUBORDER"
);
// Insert a new suborder
const insert_suborder = `INSERT INTO Suborders (order_id, menu_id, flavor_id, container_id, quantity_ordered, subtotal) VALUES ('${orderID}', '${menuID}','${flavorID}', ${containerID}, '${quantityOrdered}','${subtotal}')`;
db.pool.query(insert_suborder, function (err, rows, fields) {
if (!err) {
handleRedirect(action, orderID, res);
} else {
console.log(err);
res.status(500).send("Internal server error");
}
});
}
} else {
console.log(err);
res.status(500).send("Internal server error");
}
});
}
function editSuborderData(req, res) {
const orderID = req.body.order_id;
const menuID = JSON.parse(req.body.menu_id)[0];
const flavorID = req.body.flavor_id;
let containerID = null;
if (typeof req.body.container_id !== "undefined") {
containerID = `'${JSON.parse(req.body.container_id)[0]}'`;
}
const quantityOrdered = req.body.quantity_ordered;
const subtotal = req.body.subtotal;
let action = req.body.action;
// Check for an existing suborder with the same menu_id, container_id, and flavor_id
const check_existing_suborder = `SELECT * FROM Suborders WHERE order_id='${orderID}' AND menu_id='${menuID}' AND flavor_id='${flavorID}' AND (container_id=${containerID} OR (container_id IS NULL AND ${containerID} IS NULL))`;
db.pool.query(check_existing_suborder, function (err, rows, fields) {
if (!err) {
if (
rows.length > 0 &&
rows[0].suborder_item_id != req.params.suborderID
) {
// if the existing suborder is not current suborder,
action = -1;
handleRedirect(action, orderID, res);
} else {
// Update the current suborder
const update_suborder = `UPDATE Suborders SET order_id=${orderID}, menu_id='${menuID}', flavor_id='${flavorID}', container_id=${containerID}, quantity_ordered='${quantityOrdered}', subtotal='${subtotal}' WHERE order_id=${orderID} AND suborder_item_id=${req.params.suborderID}`;
db.pool.query(update_suborder, function (err, rows, fields) {
if (!err) {
handleRedirect(action, orderID, res);
} else {
console.log(err);
res.status(500).send("Internal server error");
}
});
}
} else {
console.log(err);
res.status(500).send("Internal server error");
}
});
}
function handleRedirect(action, orderID, res) {
console.log(`Action is: ${action} for orderID: ${orderID}`);
if (action == 1) {
const showMessage = "success";
res.redirect("/orders/" + orderID + "/" + showMessage);
} else if (action == -1) {
const showMessage = "dupe";
res.redirect("/orders/" + orderID + "/" + showMessage);
} else {
res.redirect("/orders");
}
}
function deleteSuborderData(req, res) {
const orderID = req.params.orderID;
const delete_suborder = `DELETE FROM Suborders WHERE order_id=${orderID} AND suborder_item_id=${req.params.suborderID}`;
let action = req.body.action;
db.pool.query(delete_suborder, function (err, rows, fields) {
if (!err) {
if (action == 1) {
res.redirect("/orders/" + orderID + "/" + "success");
} else {
res.redirect("/orders");
}
} else {
console.log(err);
res.status(500).send("Internal server error");
}
});
}
module.exports = {
addSuborderData,
editSuborderData,
deleteSuborderData,
};