-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.controller.js
executable file
·162 lines (153 loc) · 5.59 KB
/
products.controller.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
156
157
158
159
160
161
162
const db = require("../config/database.config");
const catModel = require("../middlewares/cat");
const crypto = require("../middlewares/crypto");
exports.products = async (req, res) => {
var isLogged = crypto.decrypt(req.cookies.login_status || "");
if (isLogged) {
try {
const userId = crypto.decrypt(req.cookies.userId);
const currencyCode = crypto.decrypt(req.cookies.currencyCode);
console.log("currencyCode: ", currencyCode);
const [currRate, notification] = await Promise.all([
catModel.fetchCurrencyRate(currencyCode),
catModel.fetchAllNotifications(userId),
]);
var uID = crypto.decrypt(req.cookies.userId);
var userName = crypto.decrypt(req.cookies.userName);
var userImage = crypto.decrypt(req.cookies.userImage || "");
// First Query
db.query(
"SELECT *, `shop`.`shop_custom_url` FROM `products` INNER JOIN `shop` ON `shop`.`id` = `products`.`seller_id` WHERE `shop`.`seller_user_id` = ? ORDER BY `products`.`product_name` ASC",
[uID],
(err1, res1) => {
console.log(uID);
if (!err1) {
// Check if products are returned
if (res1.length > 0) {
db.query(
"SELECT * FROM `products` INNER JOIN `shop` ON `shop`.`id` = `products`.`seller_id` INNER JOIN `product_image` ON `products`.`product_id` = `product_image`.`product_id` WHERE `shop`.`seller_user_id` = ? AND `product_image`.`featured_image` = 1",
[uID],
(err2, res2) => {
if (!err2) {
var images = res2.map((image) => {
image.product_id = crypto.smallEncrypt(image.product_id);
return image;
});
var products = res1.map((product) => {
product.product_id = crypto.smallEncrypt(
product.product_id
);
return product;
});
res.render("products", {
ogImage: "https://admin.save71.com/images/logo-og.webp",
ogTitle:
"Save71 Connects You and the World through Business.",
ogUrl: "https://admin-save71.lens-ecom.store",
currRate,
currencyCode,
userName: userName,
userImage: userImage,
menuId: "shop-owner-products",
products: products,
images: images,
name: "Products",
notification: notification,
shop_custom_url: res1[0].shop_custom_url, // Access safely
});
} else {
res.status(500).send(err2);
}
}
);
} else {
// Handle no products case
res.render("products", {
ogImage: "https://admin.save71.com/images/logo-og.webp",
ogTitle: "Save71 Connects You and the World through Business.",
ogUrl: "https://admin-save71.lens-ecom.store",
currRate,
currencyCode,
userName: userName,
userImage: userImage,
menuId: "shop-owner-products",
products: [], // Empty products array
images: [], // Empty images array
name: "Products",
notification: notification,
shop_custom_url: "", // Provide fallback if no product found
});
}
} else {
res.status(500).send(err1);
}
}
);
} catch (err) {
console.error(err);
res.status(500).send("Internal Server Error");
}
} else {
res.redirect("/login");
}
};
exports.del_product = (req, res) => {
var pID = crypto.smallDecrypt(req.params.id);
var query = "DELETE FROM `products` WHERE `products`.`product_id` = ?";
// console.log("Delete product id : " + pID);
db.query(query, [pID], (err1, res1) => {
if (!err1) {
db.query(
"DELETE FROM `product_image` WHERE `product_image`.`product_id` = ?",
[pID],
(err2, res2) => {
if (!err2) {
db.query(
"DELETE FROM `product_video` WHERE `product_id` = ?",
[pID],
(err3, res3) => {
if (!err3) {
res.redirect("/products");
} else {
res.send(err3);
}
}
);
} else {
res.send(err2);
}
}
);
} else {
res.send(err1);
}
});
};
exports.unpublish_product = (req, res) => {
var product_id = crypto.smallDecrypt(req.params.product_id);
db.query(
"UPDATE `products` SET `quantity` = '-1' WHERE `products`.`product_id` = ?",
[product_id],
(err1, res1) => {
if (!err1) {
res.redirect("/products");
} else {
res.send(err1);
}
}
);
};
exports.publish_product = (req, res) => {
var product_id = crypto.smallDecrypt(req.params.product_id);
db.query(
"UPDATE `products` SET `quantity` = '0' WHERE `products`.`product_id` = ?",
[product_id],
(err1, res1) => {
if (!err1) {
res.redirect("/products");
} else {
res.send(err1);
}
}
);
};