-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea-shop.controller.js
executable file
·161 lines (147 loc) · 5.5 KB
/
area-shop.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
const db = require("../config/database.config");
const catModel = require("../middlewares/cat");
const Shop = require("../middlewares/shop");
const locationOptimizedDistance = require("../middlewares/locationOptimizedDistance");
const crypto = require("../middlewares/crypto");
exports.areaShop = async (req, res) => {
try {
var isLogged = crypto.decrypt(req.cookies.login_status || "");
if (isLogged) {
const userId = crypto.decrypt(req.cookies.userId);
const currencyCode = crypto.decrypt(req.cookies.currencyCode);
const [
mainCat,
subCat,
extraCat,
allCat,
images,
currRate,
notification,
] = await Promise.all([
catModel.fetchMainCat(),
catModel.fetchSubCat(),
catModel.fetchExtraCat(),
catModel.fetchAllCat(),
catModel.fetchFeaturedImages(),
catModel.fetchCurrencyRate(currencyCode),
catModel.fetchAllNotifications(userId),
]);
const Shops = await Shop.getShops();
// Get user's current location from request query parameters
const location = JSON.parse(JSON.stringify(req.cookies.location) || "{}");
const userLat = crypto.decrypt(location.latitude);
const userLng = crypto.decrypt(location.longitude);
if (!userLat || !userLng) {
return res
.status(400)
.json({ error: "User location is missing or invalid." });
}
// Find nearest shops to the user's current location
const sortedShops =
await locationOptimizedDistance.getSortedShopsByDistance(
userLat,
userLng
);
// Get user's order details
const isLoggedIn = crypto.decrypt(req.cookies.login_status);
const userImageURL = crypto.decrypt(req.cookies.userImage || "");
const userName = crypto.decrypt(req.cookies.userName);
const userID = crypto.decrypt(req.cookies.userId);
// Fetch user's cart items
const cart = await new Promise((resolve, reject) => {
db.query(
"SELECT * FROM `orders` INNER JOIN `order_details` ON `orders`.`order_id` = `order_details`.`order_id` INNER JOIN `products` ON `products`.`product_id` = `order_details`.`product_id` WHERE `orders`.`user_id` = ? AND `orders`.`in_cart` = 1 ORDER BY `orders`.`order_id` DESC",
[userID],
(err, cartItems) => {
if (err) {
reject(err);
} else {
resolve(cartItems);
}
}
);
});
// Fetch products
const products = await new Promise((resolve, reject) => {
db.query(
"SELECT * FROM `products` \
INNER JOIN `shop` ON `shop`.`id` = `products`.`seller_id` \
INNER JOIN `extra_cat` ON `extra_cat`.`extra_cat_id` = `products`.`product_cat_id` \
INNER JOIN `product_image` ON `product_image`.`product_id` = `products`.`product_id` \
WHERE `products`.`quantity` = 0 \
AND `products`.`status` = 1 \
AND `products`.`admin_published` = 1 \
AND `shop`.`shop_type` = 2 \
AND `product_image`.`featured_image` = 1;",
(err, productsData) => {
if (err) {
reject(err);
} else {
resolve(productsData);
}
}
);
});
const updatedProducts = products.map((product) => {
const shop = sortedShops.find((shop) => shop.id == product.seller_id);
return {
...product,
shop,
};
});
var encImages = images.map((image) => {
image.product_id = crypto.smallEncrypt(image.product_id);
return image;
});
var encProduct = updatedProducts.map((product) => {
product.product_id = crypto.smallEncrypt(product.product_id);
var image = encImages.find(
(image) => image.product_id == product.product_id
);
product.product_image_url = image ? image.product_image_url : "";
return product;
});
// console.log('details: ', encProduct[0].shop.distanceKm)
var encSortedShops = sortedShops.map((shop) => {
shop.id = crypto.smallEncrypt(shop.id);
var image = encImages.find(
(image) => image.product_id == shop.product_id
);
shop.product_image_url = image ? image.product_image_url : "";
return shop;
});
var encCart = cart.map((item) => {
item.product_id = crypto.smallEncrypt(item.product_id);
return item;
});
console.log("Products: ", encProduct[0]);
// Render the template securely
res.render("area-shop", {
ogImage: "https://save71.com/images/logo-og.webp",
ogTitle: "Save71 Connects You and the World through Business.",
ogUrl: "http://localhost:3000",
navId: "area-shop",
currRate,
currencyCode,
images: encImages,
userName: userName,
userImage: userImageURL,
login_status: isLoggedIn,
products: encProduct,
cart: encCart,
subCat: subCat,
mainCat: mainCat,
extraCat: extraCat,
allCat: allCat,
shops: encSortedShops,
notification: notification,
});
} else {
res.redirect("/login");
return;
}
} catch (err) {
console.error(err);
res.status(500).send("Internal Server Error");
}
};