la-flowerita 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +70 -0
- package/package.json +78 -0
- package/public/favicon.ico +0 -0
- package/public/images/1.jpg +0 -0
- package/public/images/2.jpg +0 -0
- package/public/images/3.jpg +0 -0
- package/public/images/Logo.png +0 -0
- package/public/images/add-user.png +0 -0
- package/public/images/contact-us.png +0 -0
- package/public/images/flower.png +0 -0
- package/public/images/flower_shop.png +0 -0
- package/public/images/flower_shop1.png +0 -0
- package/public/images/flowers/buttercup.png +0 -0
- package/public/images/flowers/daffodil.png +0 -0
- package/public/images/flowers/daisy.png +0 -0
- package/public/images/flowers/jasmine.png +0 -0
- package/public/images/flowers/lily.png +0 -0
- package/public/images/flowers/marigold.png +0 -0
- package/public/images/flowers/orchid.png +0 -0
- package/public/images/flowers/rose.png +0 -0
- package/public/images/flowers/sunflower.png +0 -0
- package/public/images/flowers/tulip.png +0 -0
- package/public/images/garbage.png +0 -0
- package/public/images/icon1.jpg +0 -0
- package/public/images/login.png +0 -0
- package/public/images/refresh.png +0 -0
- package/public/images/transaction.png +0 -0
- package/public/index.html +41 -0
- package/public/logo192.png +0 -0
- package/public/logo512.png +0 -0
- package/public/manifest.json +26 -0
- package/public/offline.html +44 -0
- package/public/robots.txt +3 -0
- package/public/serviceworker.js +44 -0
- package/server.js +97 -0
- package/src/App.css +38 -0
- package/src/App.js +25 -0
- package/src/App.test.js +8 -0
- package/src/Config/db.js +5 -0
- package/src/Config/mail.js +7 -0
- package/src/Config/passport.js +34 -0
- package/src/Controllers/catalog.controller.js +78 -0
- package/src/Controllers/shoppinglist.controller.js +186 -0
- package/src/Controllers/user.controller.js +214 -0
- package/src/Controllers/wishlist.controller.js +150 -0
- package/src/Middleware/sendMail.js +29 -0
- package/src/Middleware/uploadImage.js +26 -0
- package/src/Models/orderProducts.js +10 -0
- package/src/Models/products.js +17 -0
- package/src/Models/shoppinglists.js +12 -0
- package/src/Models/userShoppinglists.js +9 -0
- package/src/Models/users.js +57 -0
- package/src/Models/wishlists.js +9 -0
- package/src/Routes/auth.js +21 -0
- package/src/Routes/indexRouter.js +40 -0
- package/src/Services/ProductService.js +76 -0
- package/src/Services/ShoppinglistService.js +135 -0
- package/src/Services/UserService.js +63 -0
- package/src/Services/WishlistService.js +105 -0
- package/src/components/About.js +45 -0
- package/src/components/Auth.js +15 -0
- package/src/components/Catalog.js +118 -0
- package/src/components/Chat.js +77 -0
- package/src/components/Contact.js +48 -0
- package/src/components/Dashboard.js +13 -0
- package/src/components/DetailsProductModal.js +362 -0
- package/src/components/LoginModal.js +173 -0
- package/src/components/NewProductModal.js +271 -0
- package/src/components/NoPermission.js +10 -0
- package/src/components/OrderedProduct.js +104 -0
- package/src/components/PreChat.js +51 -0
- package/src/components/Product.js +158 -0
- package/src/components/ResetPassword.js +211 -0
- package/src/components/ShoppingCart.js +198 -0
- package/src/components/SideNav.js +76 -0
- package/src/components/SignupModal.js +306 -0
- package/src/components/Spinner.js +22 -0
- package/src/components/Wishlist.js +195 -0
- package/src/components/social-config.js +17 -0
- package/src/css/about.css +12 -0
- package/src/css/catalog.css +218 -0
- package/src/css/chat.css +191 -0
- package/src/css/contact.css +404 -0
- package/src/css/index.css +129 -0
- package/src/css/newProductModal.css +69 -0
- package/src/css/noPermission.css +89 -0
- package/src/css/orderedProduct.css +0 -0
- package/src/css/resetPassword.css +44 -0
- package/src/css/shoppingCart.css +10 -0
- package/src/css/sideNav.css +68 -0
- package/src/css/spinner.css +23 -0
- package/src/images/1.jpg +0 -0
- package/src/images/2.jpg +0 -0
- package/src/images/3.jpg +0 -0
- package/src/images/Logo.png +0 -0
- package/src/images/add-user.png +0 -0
- package/src/images/buttercup.png +0 -0
- package/src/images/contact-us.png +0 -0
- package/src/images/daffodil.png +0 -0
- package/src/images/daisy.png +0 -0
- package/src/images/flower.png +0 -0
- package/src/images/flower_shop.png +0 -0
- package/src/images/flower_shop1.png +0 -0
- package/src/images/flowers/buttercup.png +0 -0
- package/src/images/flowers/daffodil.png +0 -0
- package/src/images/flowers/daisy.png +0 -0
- package/src/images/flowers/jasmine.png +0 -0
- package/src/images/flowers/lily.png +0 -0
- package/src/images/flowers/marigold.png +0 -0
- package/src/images/flowers/orchid.png +0 -0
- package/src/images/flowers/rose.png +0 -0
- package/src/images/flowers/sunflower.png +0 -0
- package/src/images/flowers/tulip.png +0 -0
- package/src/images/garbage.png +0 -0
- package/src/images/icon1.jpg +0 -0
- package/src/images/jasmine.png +0 -0
- package/src/images/lily.png +0 -0
- package/src/images/login.png +0 -0
- package/src/images/marigold.png +0 -0
- package/src/images/orchid.png +0 -0
- package/src/images/refresh.png +0 -0
- package/src/images/rose.png +0 -0
- package/src/images/sunflower.png +0 -0
- package/src/images/transaction.png +0 -0
- package/src/images/tulip.png +0 -0
- package/src/index.js +371 -0
- package/src/logo.svg +1 -0
- package/src/reportWebVitals.js +13 -0
- package/src/setupTests.js +5 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
const ProductService = require('../Services/ProductService');
|
2
|
+
const WishlistService = require('../Services/WishlistService');
|
3
|
+
const mongoose = require('mongoose');
|
4
|
+
const Products = mongoose.model('Products');
|
5
|
+
const Users = mongoose.model('Users');
|
6
|
+
var fs = require("fs");
|
7
|
+
const imagesMiddleware = require("../Middleware/uploadImage");
|
8
|
+
|
9
|
+
module.exports = class Catalog {
|
10
|
+
|
11
|
+
static async getCatalog(req, res, next){
|
12
|
+
var type = req.body.type;
|
13
|
+
console.log(type);
|
14
|
+
var products = [];
|
15
|
+
if(type == "All" || !type){
|
16
|
+
products = await ProductService.GetALL();
|
17
|
+
} else{
|
18
|
+
console.log(type);
|
19
|
+
products = await ProductService.GetCatalogByType(type);
|
20
|
+
}
|
21
|
+
for (let i = 0; i < products.length; i++) {
|
22
|
+
console.log("sellerid")
|
23
|
+
console.log(products[i].sellerId)
|
24
|
+
products[i].sellerId = (await Users.findById(products[i].sellerId)).name;
|
25
|
+
}
|
26
|
+
return res.json({products: products});
|
27
|
+
}
|
28
|
+
static async addNewProduct(req, res, next){
|
29
|
+
if(!req.user){
|
30
|
+
return res.json({status: 403});
|
31
|
+
}
|
32
|
+
var product = req.body.product;
|
33
|
+
product.isActivate = true;
|
34
|
+
product.sellerId = req.user._id;
|
35
|
+
const newProduct = new Products(product);
|
36
|
+
await newProduct.save();
|
37
|
+
console.log('Product created:' + newProduct);
|
38
|
+
var id = newProduct._id;;
|
39
|
+
console.log(id);
|
40
|
+
return res.json({status: 200, id: id});
|
41
|
+
}
|
42
|
+
|
43
|
+
static async addProductPicture(req, res, next){
|
44
|
+
await imagesMiddleware.uploadFile(req, res);
|
45
|
+
var id = req.body.id;
|
46
|
+
console.log(id);
|
47
|
+
var product = await ProductService.FIND(id);
|
48
|
+
if(product){
|
49
|
+
product.image = {data: req.file.buffer, contentType:req.file.mimetype};
|
50
|
+
console.log("good");
|
51
|
+
await ProductService.UpdatePicture(id,product)
|
52
|
+
}
|
53
|
+
return res.sendStatus(200);
|
54
|
+
}
|
55
|
+
|
56
|
+
static async updateProduct(req, res, next){
|
57
|
+
var oldProduct = await ProductService.FIND(req.body.product._id);
|
58
|
+
if(oldProduct){
|
59
|
+
var newProduct = req.body.product;
|
60
|
+
newProduct = new Products(newProduct);
|
61
|
+
await ProductService.UPDATE(newProduct._id, newProduct)
|
62
|
+
console.log('Product updated:' + newProduct);
|
63
|
+
} else{
|
64
|
+
return res.sendStatus(404);
|
65
|
+
}
|
66
|
+
return res.sendStatus(200);
|
67
|
+
}
|
68
|
+
static async deleteProduct(req, res, next){
|
69
|
+
var oldProduct = await ProductService.FIND(req.body.id);
|
70
|
+
if(oldProduct){
|
71
|
+
await ProductService.DELETE(req.body.id)
|
72
|
+
console.log('Product deleted:' + req.body.id);
|
73
|
+
} else{
|
74
|
+
return res.sendStatus(404);
|
75
|
+
}
|
76
|
+
return res.sendStatus(200);
|
77
|
+
}
|
78
|
+
}
|
@@ -0,0 +1,186 @@
|
|
1
|
+
const ShoppinglistsService = require('../Services/ShoppinglistService');
|
2
|
+
const mongoose = require('mongoose');
|
3
|
+
const Shoppinglists = mongoose.model('Shoppinglists');
|
4
|
+
const Users = mongoose.model('Users');
|
5
|
+
const Products = mongoose.model('Products');
|
6
|
+
|
7
|
+
module.exports = class Shoppinglist {
|
8
|
+
static async addNewProductToCart(req, res, next) {
|
9
|
+
try {
|
10
|
+
var cart = { products: [] };
|
11
|
+
var productId = req.body.productId;
|
12
|
+
var product = await Products.findById(productId);
|
13
|
+
if (req.user) {
|
14
|
+
await ShoppinglistsService.AddProduct(req.user._id, product);
|
15
|
+
cart = await ShoppinglistsService.GetCurrentCart(req.user._id);
|
16
|
+
console.log(cart);
|
17
|
+
cart.products = await ShoppinglistsService.GetProductsDetails(
|
18
|
+
cart.products
|
19
|
+
);
|
20
|
+
} else {
|
21
|
+
console.log("add product to cart in session");
|
22
|
+
if (req.session.cart == undefined) {
|
23
|
+
req.session.cart = { products: [] };
|
24
|
+
req.session.save((err) => {
|
25
|
+
console.log(err);
|
26
|
+
});
|
27
|
+
}
|
28
|
+
console.log(req.session);
|
29
|
+
let ssn = req.session;
|
30
|
+
console.log(ssn.cart);
|
31
|
+
|
32
|
+
if (
|
33
|
+
!ssn.cart.products.find((p) => {
|
34
|
+
if (p.id === product._id.toString()) {
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
return false;
|
38
|
+
})
|
39
|
+
) {
|
40
|
+
console.log("the only one in the list");
|
41
|
+
ssn.cart.products.push({
|
42
|
+
id: product._id,
|
43
|
+
quantity: 1,
|
44
|
+
price: product.price,
|
45
|
+
image: product.image,
|
46
|
+
name: product.name,
|
47
|
+
});
|
48
|
+
} else {
|
49
|
+
console.log("duplicates");
|
50
|
+
ssn.cart.products.map((p) =>
|
51
|
+
p.id == product._id
|
52
|
+
? (p.quantity = p.quantity + 1)
|
53
|
+
: (p.quantity = p.quantity)
|
54
|
+
);
|
55
|
+
}
|
56
|
+
console.log(ssn.cart);
|
57
|
+
req.session.cart = ssn.cart;
|
58
|
+
// req.session.cart = ssn.cart;
|
59
|
+
req.session.save((err) => {
|
60
|
+
console.log(err);
|
61
|
+
});
|
62
|
+
return res.json({ status: 200, cart: ssn.cart });
|
63
|
+
}
|
64
|
+
console.log(cart);
|
65
|
+
return res.json({ status: 200, cart: cart });
|
66
|
+
} catch (e) {
|
67
|
+
console.log(e);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
static async updateProductInCart(req, res, next) {
|
71
|
+
try {
|
72
|
+
var cart = { id: 0, products: [] };
|
73
|
+
var product = req.body.product;
|
74
|
+
var productId = product.productId;
|
75
|
+
console.log(productId);
|
76
|
+
if (req.user) {
|
77
|
+
var id = req.user._id;
|
78
|
+
console.log(product);
|
79
|
+
await ShoppinglistsService.UpdateProduct(id, product);
|
80
|
+
var cart = await ShoppinglistsService.GetCurrentCart(id);
|
81
|
+
cart.products = await ShoppinglistsService.GetProductsDetails(
|
82
|
+
cart.products
|
83
|
+
);
|
84
|
+
} else {
|
85
|
+
if (req.session.cart) {
|
86
|
+
console.log(req.session);
|
87
|
+
let ssn = req.session;
|
88
|
+
ssn.cart.products.map((p) =>
|
89
|
+
p.id == product.productId
|
90
|
+
? (p.quantity = product.quantity)
|
91
|
+
: (p.quantity = product.quantity)
|
92
|
+
);
|
93
|
+
req.session.cart = ssn.cart;
|
94
|
+
req.session.save((err) => {
|
95
|
+
console.log(err);
|
96
|
+
});
|
97
|
+
return res.json({ status: 200, cart: ssn.cart });
|
98
|
+
} else {
|
99
|
+
return res.json({ status: 404, cart: cart });
|
100
|
+
}
|
101
|
+
}
|
102
|
+
return res.json({ status: 200, cart: cart });
|
103
|
+
} catch (e) {
|
104
|
+
console.log(e);
|
105
|
+
return res.json({ status: 500 });
|
106
|
+
}
|
107
|
+
}
|
108
|
+
static async deleteProductFromCart(req, res, next) {
|
109
|
+
try {
|
110
|
+
var cart = { id: 0, products: [] };
|
111
|
+
var productId = req.body.productId;
|
112
|
+
if (req.user) {
|
113
|
+
var id = req.user._id;
|
114
|
+
// console.log("product id " + productId);
|
115
|
+
// console.log("cart id " + cartId);
|
116
|
+
await ShoppinglistsService.DeleteProduct(id, productId);
|
117
|
+
var cart = await ShoppinglistsService.GetCurrentCart(id);
|
118
|
+
cart.products = await ShoppinglistsService.GetProductsDetails(
|
119
|
+
cart.products
|
120
|
+
);
|
121
|
+
} else {
|
122
|
+
if (req.session.cart) {
|
123
|
+
console.log("product id: " + productId);
|
124
|
+
var ssn = req.session;
|
125
|
+
ssn.cart.products = ssn.cart.products.filter(
|
126
|
+
(p) => p.id != productId
|
127
|
+
);
|
128
|
+
req.session.cart = ssn.cart;
|
129
|
+
req.session.save((err) => {
|
130
|
+
console.log(err);
|
131
|
+
});
|
132
|
+
return res.json({ status: 200, cart: ssn.cart });
|
133
|
+
} else {
|
134
|
+
return res.json({ status: 404, cart: cart });
|
135
|
+
}
|
136
|
+
}
|
137
|
+
return res.json({ status: 200, cart: cart });
|
138
|
+
} catch (e) {
|
139
|
+
console.log(e);
|
140
|
+
return res.sendStatus(500);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
static async getCurrentCart(req, res, next) {
|
144
|
+
// console.log("getCurrentCart");
|
145
|
+
console.log("hi");
|
146
|
+
var cart = { products: [] };
|
147
|
+
if (req.user) {
|
148
|
+
// console.log("logged user");
|
149
|
+
var id = req.user._id;
|
150
|
+
if (id) {
|
151
|
+
// console.log("searching for cart");
|
152
|
+
cart = await ShoppinglistsService.GetCurrentCart(id);
|
153
|
+
// console.log("found cart");
|
154
|
+
// console.log(cart);
|
155
|
+
if (cart && cart.products.length >= 1) {
|
156
|
+
cart.products = await ShoppinglistsService.GetProductsDetails(
|
157
|
+
cart.products
|
158
|
+
);
|
159
|
+
} else {
|
160
|
+
// console.log("cart not found. new cart opened");
|
161
|
+
cart = new Shoppinglists({
|
162
|
+
userId: id,
|
163
|
+
products: [],
|
164
|
+
isPaid: false,
|
165
|
+
});
|
166
|
+
await cart.save();
|
167
|
+
}
|
168
|
+
console.log("cart of user successful");
|
169
|
+
return res.json({ status: 200, cart: cart });
|
170
|
+
}
|
171
|
+
} else {
|
172
|
+
if (req.session.cart) {
|
173
|
+
cart = req.session.cart;
|
174
|
+
console.log("get cart from session");
|
175
|
+
return res.json({ status: 200, cart: cart });
|
176
|
+
}
|
177
|
+
}
|
178
|
+
}
|
179
|
+
static async payNow(req, res, next) {
|
180
|
+
if (req.user) {
|
181
|
+
await ShoppinglistsService.CartPaid(req.user._id);
|
182
|
+
return res.sendStatus(200);
|
183
|
+
}
|
184
|
+
return res.sendStatus(404);
|
185
|
+
}
|
186
|
+
};
|
@@ -0,0 +1,214 @@
|
|
1
|
+
const UserService = require("../Services/UserService");
|
2
|
+
const passport = require("passport");
|
3
|
+
const LocalStrategy = require("passport-local");
|
4
|
+
const sendEmail = require("../Middleware/sendMail")
|
5
|
+
const mongoose = require('mongoose');
|
6
|
+
const Users = mongoose.model('Users');
|
7
|
+
const crypto = require('crypto');
|
8
|
+
var fs = require('fs');
|
9
|
+
const imagesMiddleware = require("../Middleware/uploadImage");
|
10
|
+
const ShoppinglistsService = require('../Services/ShoppinglistService');
|
11
|
+
const WishlistService = require('../Services/WishlistService');
|
12
|
+
|
13
|
+
module.exports = class User {
|
14
|
+
static async auth(req, res, next) {
|
15
|
+
console.log("inside auth function");
|
16
|
+
let exist = await UserService.FindByEmail(req.body.email);
|
17
|
+
// console.log("/auth" + exist);
|
18
|
+
if (exist) {
|
19
|
+
passport.authenticate("local", function (err, user, info) {
|
20
|
+
if (err || !user) {
|
21
|
+
if(info && info.status == 404){
|
22
|
+
return res.sendStatus(404);
|
23
|
+
}
|
24
|
+
else{
|
25
|
+
console.log("Error", err);
|
26
|
+
return res.sendStatus(400);
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
req.logIn(user, function (err) {
|
31
|
+
if (err) {
|
32
|
+
return next(err);
|
33
|
+
// return res.status(404).send("Username or password incorrect");
|
34
|
+
}
|
35
|
+
});
|
36
|
+
console.log(req.session.cart);
|
37
|
+
if(req.session.cart && req.session.cart.products != []){
|
38
|
+
ShoppinglistsService.AddProductsFromSession(req.user._id, req.session.cart.products)
|
39
|
+
}
|
40
|
+
if(req.session.wishlist && req.session.wishlist.products != []){
|
41
|
+
WishlistService.AddProductsFromSession(req.user._id, req.session.wishlist.products)
|
42
|
+
}
|
43
|
+
req.session.cart = {products:[]}
|
44
|
+
req.session.wishlist = {products:[]}
|
45
|
+
req.session.save((err) => {
|
46
|
+
console.log(err);
|
47
|
+
});
|
48
|
+
return res.sendStatus(200);
|
49
|
+
})(req, res, next);
|
50
|
+
} else {
|
51
|
+
return res.sendStatus(404);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
static async getCurrentUser(req, res, next) {
|
56
|
+
console.log(req.session);
|
57
|
+
var profileImage = "";
|
58
|
+
if(req.user){
|
59
|
+
var user = await UserService.FindByEmail(req.user.email);
|
60
|
+
profileImage = user.profileImage;
|
61
|
+
}
|
62
|
+
console.log(req.user);
|
63
|
+
return res.json({user: req.user, profileImage:profileImage});
|
64
|
+
}
|
65
|
+
|
66
|
+
static async logout(req, res, next) {
|
67
|
+
console.log("logout");
|
68
|
+
req.logout();
|
69
|
+
return res.sendStatus(200);
|
70
|
+
}
|
71
|
+
|
72
|
+
static async emailForResetPassword(req, res, next) {
|
73
|
+
console.log("emailForResetPassword");
|
74
|
+
var email = req.body.email;
|
75
|
+
const user = await Users.findOne({ email: email });
|
76
|
+
if(!user){
|
77
|
+
return res.sendStatus(404);
|
78
|
+
}
|
79
|
+
const resetToken = user.getResetPasswordToken();
|
80
|
+
await user.save({ validateBeforeSave: false})
|
81
|
+
|
82
|
+
// const resetUrl = `${req.protocol}://${req.get('host')}/resetPassword/${resetToken}`;
|
83
|
+
const message = `You are receiving this email because you (or someone else) has requested the reset of a password.`
|
84
|
+
try {
|
85
|
+
await sendEmail({
|
86
|
+
email: user.email,
|
87
|
+
subject: 'Password reset',
|
88
|
+
message: message,
|
89
|
+
html: '<strong>To set new Password: </strong>'
|
90
|
+
+ ' <h1">' + resetToken + '</h1>'
|
91
|
+
})
|
92
|
+
console.log("email sent " + resetToken);
|
93
|
+
return res.sendStatus(200);
|
94
|
+
} catch (error) {
|
95
|
+
console.log(error);
|
96
|
+
user.getResetPasswordToken = undefined;
|
97
|
+
user.resetPasswordExpire = undefined;
|
98
|
+
await user.save({ validateBeforeSave: false })
|
99
|
+
return res.sendStatus(500);
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
static async checkToken(req,res,next){
|
104
|
+
|
105
|
+
const resetPasswordToken = crypto
|
106
|
+
.createHash('sha512')
|
107
|
+
.update(req.body.token)
|
108
|
+
.digest('hex');
|
109
|
+
|
110
|
+
|
111
|
+
const user = await Users.findOne({
|
112
|
+
resetPasswordToken,
|
113
|
+
resetPasswordExpire: { $gt: Date.now() }
|
114
|
+
});
|
115
|
+
|
116
|
+
console.log("I am in resetPassword function");
|
117
|
+
if(!user){
|
118
|
+
// console.log(user);
|
119
|
+
return res.json({status: 404});
|
120
|
+
}
|
121
|
+
console.log("finished checking resetPassword function");
|
122
|
+
// console.log(user);
|
123
|
+
return res.json({id: user._id, status: 200});
|
124
|
+
}
|
125
|
+
|
126
|
+
static async updatePassword(req,res,next){
|
127
|
+
const user = await Users.findOne({ _id: req.body.id});
|
128
|
+
if(!user){
|
129
|
+
return res.sendStatus(404);
|
130
|
+
}
|
131
|
+
console.log(req.body);
|
132
|
+
user.password = req.body.password;
|
133
|
+
user.setPassword(user.password);
|
134
|
+
user.resetPasswordToken = undefined;
|
135
|
+
user.resetPasswordExpire = undefined;
|
136
|
+
await UserService.UPDATE(user.email, user);
|
137
|
+
console.log("finished resetUserPass function");
|
138
|
+
return res.sendStatus(200);
|
139
|
+
}
|
140
|
+
|
141
|
+
static async signup(req, res, next){
|
142
|
+
var user = req.body.user;
|
143
|
+
|
144
|
+
if (user.email == "" || user.password == "" || user.phone == "" || user.degree == "" || user.address == "" || user.name == ""){
|
145
|
+
return res.json({status:422});
|
146
|
+
}
|
147
|
+
|
148
|
+
let email = user.email;
|
149
|
+
const oldUser = await Users.findOne({ email });
|
150
|
+
if (oldUser){
|
151
|
+
return res.json({status:409});
|
152
|
+
}
|
153
|
+
const newUser = new Users(user);
|
154
|
+
newUser.setPassword(user.password);
|
155
|
+
newUser.isApproved = user.degree != "Customer" ? false: true;
|
156
|
+
await newUser.save();
|
157
|
+
console.log("user added successfullly");
|
158
|
+
// console.log(newUser);
|
159
|
+
return res.json({status:200, id:newUser._id});
|
160
|
+
}
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
static async addUserProfile(req, res, next){
|
165
|
+
await imagesMiddleware.uploadFile(req, res);
|
166
|
+
var id = req.body.id;
|
167
|
+
var user = await UserService.FindById(id);
|
168
|
+
if(user){
|
169
|
+
user.profileImage = {data:req.file.buffer, contentType: req.file.mimetype};
|
170
|
+
// console.log(user);
|
171
|
+
await UserService.UpdateById(id,user)
|
172
|
+
}
|
173
|
+
return res.sendStatus(200);
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
static async getProfileImage(req, res, next){
|
179
|
+
var user = await UserService.FindByEmail(req.user.email);
|
180
|
+
return res.json({profileImage:user.profileImage});
|
181
|
+
}
|
182
|
+
|
183
|
+
static async isLogged(req, res, next){
|
184
|
+
if(req.user)
|
185
|
+
return res.sendStatus(200);
|
186
|
+
return res.sendStatus(404);
|
187
|
+
}
|
188
|
+
|
189
|
+
static async getSession(req, res, next){
|
190
|
+
var profileImage = {data:"", contentType:""};
|
191
|
+
var cart = {products: []}
|
192
|
+
var wishlist = {products: []}
|
193
|
+
var isLogged = false;
|
194
|
+
console.log("getsession");
|
195
|
+
if(req.user){
|
196
|
+
profileImage = await Users.findById(req.user._id).profileImage;
|
197
|
+
cart = await ShoppinglistsService.GetCurrentCart(req.user._id);
|
198
|
+
wishlist = await WishlistService.GetCurrentWishlist(req.user._id);
|
199
|
+
isLogged = true;
|
200
|
+
} else{
|
201
|
+
if(req.session.cart)
|
202
|
+
{
|
203
|
+
cart = req.session.cart;
|
204
|
+
}
|
205
|
+
if(req.session.wishlist){
|
206
|
+
console.log("hi")
|
207
|
+
wishlist = req.session.wishlist;
|
208
|
+
}
|
209
|
+
}
|
210
|
+
var result = {isLogged: isLogged, profileImage:profileImage, cart: cart, wishlist: wishlist}
|
211
|
+
console.log(req.session);
|
212
|
+
return res.json(result)
|
213
|
+
}
|
214
|
+
}
|
@@ -0,0 +1,150 @@
|
|
1
|
+
const WishlistService = require('../Services/WishlistService');
|
2
|
+
const mongoose = require('mongoose');
|
3
|
+
const Wishlists = mongoose.model('Wishlists');
|
4
|
+
const Users = mongoose.model('Users');
|
5
|
+
const Products = mongoose.model('Products');
|
6
|
+
|
7
|
+
module.exports = class Wishlist {
|
8
|
+
|
9
|
+
static async addNewProductToWishlist(req, res, next){
|
10
|
+
try{
|
11
|
+
var wishlist = { products: [] };
|
12
|
+
var productId = req.body.productId;
|
13
|
+
var product = await Products.findById(productId);
|
14
|
+
if(req.user){
|
15
|
+
await WishlistService.AddProduct(req.user._id, productId);
|
16
|
+
wishlist = await WishlistService.GetCurrentWishlist(req.user._id);
|
17
|
+
console.log(wishlist);
|
18
|
+
wishlist.products = await WishlistService.GetProductsDetails(wishlist.products);
|
19
|
+
} else {
|
20
|
+
console.log("add product to wishlist in session")
|
21
|
+
if(req.session.wishlist == undefined){
|
22
|
+
req.session.wishlist = {products:[]}
|
23
|
+
req.session.save((err) => {
|
24
|
+
console.log(err);
|
25
|
+
});
|
26
|
+
}
|
27
|
+
console.log(req.session)
|
28
|
+
let ssn = req.session;
|
29
|
+
console.log(ssn.wishlist)
|
30
|
+
if(!ssn.wishlist.products.find(p => {if (p.id === product._id.toString()) {
|
31
|
+
return true;
|
32
|
+
}
|
33
|
+
return false;
|
34
|
+
}))
|
35
|
+
{
|
36
|
+
console.log("the only one in the list")
|
37
|
+
ssn.wishlist.products.push({id:product._id, price: product.price, image: product.image, name:product.name});
|
38
|
+
} else{
|
39
|
+
console.log("exists")
|
40
|
+
}
|
41
|
+
console.log(ssn.wishlist)
|
42
|
+
req.session.wishlist = ssn.wishlist;
|
43
|
+
// req.session.wishlist = ssn.wishlist;
|
44
|
+
req.session.save((err) => {
|
45
|
+
console.log(err);
|
46
|
+
});
|
47
|
+
return res.json({status:200,wishlist: ssn.wishlist});
|
48
|
+
}
|
49
|
+
console.log(wishlist);
|
50
|
+
return res.json({status:200,wishlist: wishlist});
|
51
|
+
} catch(e){
|
52
|
+
console.log(e);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
static async updateProductInWishlist(req, res, next){
|
56
|
+
try{
|
57
|
+
var wishlist = { id: 0, products: [] };
|
58
|
+
var product = req.body.product;
|
59
|
+
var productId = product.productId;
|
60
|
+
console.log(productId);
|
61
|
+
if (req.user) {
|
62
|
+
var id = req.user._id;
|
63
|
+
console.log(product);
|
64
|
+
await WishlistService.UpdateProduct(product);
|
65
|
+
var wishlist = await WishlistService.GetCurrentWishlist(id);
|
66
|
+
wishlist.products = await WishlistService.GetProductsDetails(wishlist.products);
|
67
|
+
} else {
|
68
|
+
if(req.session.wishlist){
|
69
|
+
console.log(req.session)
|
70
|
+
let ssn = req.session;
|
71
|
+
ssn.wishlist.products.map(p => p.id == product.productId ? p.quantity = product.quantity : p.quantity = product.quantity);
|
72
|
+
req.session.wishlist = ssn.wishlist;
|
73
|
+
req.session.save((err) => {
|
74
|
+
console.log(err);
|
75
|
+
});
|
76
|
+
return res.json({status:200,wishlist: ssn.wishlist});
|
77
|
+
} else{
|
78
|
+
return res.json({status:404,wishlist: wishlist});
|
79
|
+
}
|
80
|
+
}
|
81
|
+
return res.json({status:200,wishlist: wishlist});
|
82
|
+
} catch(e){
|
83
|
+
console.log(e);
|
84
|
+
return res.json({status:500});
|
85
|
+
}
|
86
|
+
}
|
87
|
+
static async deleteProductFromWishlist(req, res, next){
|
88
|
+
try{
|
89
|
+
var wishlist = {products: [] };
|
90
|
+
var loggedUser = req.user ? true : false;
|
91
|
+
var productId = req.body.productId;
|
92
|
+
if (loggedUser) {
|
93
|
+
var id = req.user._id;
|
94
|
+
await WishlistService.DeleteProduct(id, productId);
|
95
|
+
var wishlist = await WishlistService.GetCurrentWishlist(id);
|
96
|
+
wishlist.products = await WishlistService.GetProductsDetails(wishlist.products);
|
97
|
+
} else {
|
98
|
+
if(req.session.wishlist){
|
99
|
+
console.log("product id: " + productId)
|
100
|
+
var ssn = req.session;
|
101
|
+
ssn.wishlist.products = ssn.wishlist.products.filter(p => p.id != productId);
|
102
|
+
req.session.wishlist = ssn.wishlist;
|
103
|
+
req.session.save((err) => {
|
104
|
+
console.log(err);
|
105
|
+
});
|
106
|
+
return res.json({status:200,wishlist: ssn.wishlist});
|
107
|
+
} else{
|
108
|
+
return res.json({status:404,wishlist: wishlist});
|
109
|
+
}
|
110
|
+
}
|
111
|
+
return res.json({status:200,wishlist: wishlist});
|
112
|
+
} catch(e){
|
113
|
+
console.log(e)
|
114
|
+
return res.sendStatus(500);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
static async getCurrentWishlist(req, res, next){
|
118
|
+
console.log("hi")
|
119
|
+
var wishlist = {products:[]};
|
120
|
+
var loggedUser = req.user ? true : false;
|
121
|
+
if (loggedUser) {
|
122
|
+
var id = req.user._id;
|
123
|
+
wishlist = await WishlistService.GetCurrentWishlist(id);
|
124
|
+
if (wishlist) {
|
125
|
+
wishlist.products = await WishlistService.GetProductsDetails(
|
126
|
+
wishlist.products
|
127
|
+
);
|
128
|
+
} else {
|
129
|
+
wishlist = new Wishlists({
|
130
|
+
userId: id,
|
131
|
+
products: [],
|
132
|
+
});
|
133
|
+
await wishlist.save();
|
134
|
+
var wishlist = { products: [] };
|
135
|
+
}
|
136
|
+
console.log("wishlist of user successful");
|
137
|
+
} else {
|
138
|
+
if(req.session.wishlist){
|
139
|
+
wishlist = req.session.wishlist;
|
140
|
+
wishlist.products = await WishlistService.GetProductsDetails(
|
141
|
+
wishlist.products.map(p => p.id)
|
142
|
+
);
|
143
|
+
console.log("get wishlist from session")
|
144
|
+
}
|
145
|
+
}
|
146
|
+
console.log(wishlist)
|
147
|
+
return res.json({status: 200, wishlist: wishlist});
|
148
|
+
}
|
149
|
+
|
150
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
const nodemailer = require("nodemailer");
|
2
|
+
const procees = require("../Config/mail")
|
3
|
+
// async..await is not allowed in global scope, must use a wrapper
|
4
|
+
async function sendEmail(options) {
|
5
|
+
|
6
|
+
let transporter = nodemailer.createTransport({
|
7
|
+
service: procees.SERVICE,
|
8
|
+
auth: {
|
9
|
+
user: procees.SMTP_EMAIL, // generated ethereal user
|
10
|
+
pass: procees.SMTP_PASSWORD, // generated ethereal password
|
11
|
+
},
|
12
|
+
});
|
13
|
+
|
14
|
+
// send mail with defined transport object
|
15
|
+
let message = await transporter.sendMail({
|
16
|
+
from: `${procees.FROM_NAME}<${procees.FROM_EMAIL}>`, // sender address
|
17
|
+
to: options.email, // list of receivers
|
18
|
+
subject: options.subject, // Subject line
|
19
|
+
text: options.message, // plain text body
|
20
|
+
html: options.html
|
21
|
+
});
|
22
|
+
|
23
|
+
|
24
|
+
const info = await transporter.sendMail(message);
|
25
|
+
console.log("Message sent: %s", info.messageId);
|
26
|
+
}
|
27
|
+
|
28
|
+
module.exports = sendEmail;
|
29
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
const util = require("util");
|
2
|
+
const multer = require("multer");
|
3
|
+
const { GridFsStorage } = require("multer-gridfs-storage");
|
4
|
+
const dbConfig = require("../Config/db");
|
5
|
+
const MongoClient = require('mongodb').MongoClient;
|
6
|
+
var Server = require('mongodb').Server;
|
7
|
+
// const uri = "mongodb+srv://tratzon:tratzon1@cluster0.l39y0.mongodb.net/?retryWrites=true&w=majority";
|
8
|
+
// const client = new MongoClient(uri);
|
9
|
+
|
10
|
+
var storage = new GridFsStorage({
|
11
|
+
url: dbConfig.url + dbConfig.database,
|
12
|
+
options: { useNewUrlParser: true, useUnifiedTopology: true },
|
13
|
+
file: (req, file) => {
|
14
|
+
const match = ["image/png"];
|
15
|
+
// console.log(file);
|
16
|
+
return {
|
17
|
+
bucketName: dbConfig.collectionUsers,
|
18
|
+
filename: file.originalname + Date.now(),
|
19
|
+
};
|
20
|
+
},
|
21
|
+
});
|
22
|
+
var uploadFiles = multer({storage: multer.memoryStorage()}).single("file")
|
23
|
+
var uploadFilesMiddleware = util.promisify(uploadFiles);
|
24
|
+
module.exports.uploadFile = uploadFilesMiddleware;
|
25
|
+
|
26
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
const mongo = require("mongoose");
|
2
|
+
const { Schema } = mongo;
|
3
|
+
|
4
|
+
const OrderProductSchema = new Schema({
|
5
|
+
productId: String,
|
6
|
+
quantity: Number,
|
7
|
+
priceForEach: Number,
|
8
|
+
}, { autoIndex: true });
|
9
|
+
|
10
|
+
mongo.model('OrderProducts', OrderProductSchema, 'OrderProducts'); // if model name as lowercase with suffix "s" === collection name: User => users
|