-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
185 lines (157 loc) · 5.68 KB
/
database.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const { User, Cart } = require('./model/cartModel');
const { Navs, Brands, Category, Product, Features, Footer, Store } = require('./model/shopModel');
mongoose.connect(process.env.CONNECTION_STRING)
.then(data => console.log('Database Connected'))
.catch(err => console.log("Database not Connected", err))
const getNavs = async (storeId) => {
const data = await Navs.findOne({ storeId })
return data;
}
const getBrandName = async (storeId) => {
const data = await Brands.findOne({ storeId })
return data;
}
const getFeatures = async (storeId) => {
const data = await Features.findOne({ storeId })
return data;
}
// Checking the store existance
exports.storeCheck = async (storeId) => {
const check = await Store.findOne({ storeId });
return check;
}
// Generating the Store
exports.generateStore = async (data) => {
// New ID for store
const storeId = mongoose.Types.ObjectId();
// Extracting data from the provided JSON
const { email, password, brandName, navs, categories, products, features, footers } = data;
// Adding brand name to DB
const brand = new Brands({ value: brandName, storeId })
await brand.save();
// Adding navs to DB
const newNavs = new Navs({ value: navs.value, storeId });
await newNavs.save();
// Adding all the categories to DB
for (let i = 0; i < categories.length; i++) {
const newCategory = new Category({ ...categories[i], storeId });
await newCategory.save();
}
// Adding all the products to DB
for (let i = 0; i < products.length; i++) {
const newProduct = new Product({ ...products[i], storeId });
await newProduct.save();
}
// Adding all the features to DB
const newFeatures = new Features({ value: features.value, storeId });
await newFeatures.save();
// Adding footer data to DB
const newFooters = new Footer({ value: footers.value, storeId });
await newFooters.save();
// Creating store at DB for checking if it exist
const newStore = new Store({ storeId, email, password });
await newStore.save();
// Returning ID of newly generated store
return storeId;
}
// Getting the data of entire shop
exports.getFullShop = async (storeId) => {
const { quick, newProduct, support } = (await Footer.findOne({ storeId }).lean()).value;
const features = (await getFeatures(storeId)).value;
const navItems = (await getNavs(storeId)).value;
const brandName = (await getBrandName(storeId)).value;
const data = { brandName, navItems, features, quick, newProduct, support };
return data;
}
// Cart Actions
exports.addItemToCart = async (item) => {
try {
const checkItem = await Cart.findOne({ id: item.id, storeId: item.storeId, userId: item.userId });
if (checkItem) {
const data = await Cart.updateOne({ id: item.id, userId: item.userId, storeId: item.storeId }, { $inc: { quantity: 1 } })
return data;
}
const selectedItem = { ...item, quantity: parseInt(item.quantity) };
const data = await Cart.create(selectedItem);
return data;
} catch (err) {
console.log(err);
}
}
exports.findAllData = async (userId, storeId) => {
try {
const data = await Cart.find({ userId, storeId });
if (!data) {
throw new Error(false);
}
return data;
} catch (err) {
return false;
}
}
exports.removeOneItem = async (id, userId, storeId) => {
try {
const data = await Cart.findOne({ id, userId, storeId });
if (data.quantity > 1) {
const value = await Cart.updateOne({ id, userId, storeId }, { $inc: { quantity: -1 } })
return value;
}
const value = await Cart.deleteOne({ id, userId, storeId });
return value;
} catch (err) {
console.log(err);
}
}
exports.removeOneInstance = async (id, userId, storeId) => {
try {
const data = await Cart.deleteOne({ id, userId, storeId });
return data;
} catch (err) {
console.log(err);
}
}
exports.registerUser = async (userData) => {
const hashedPassword = await bcrypt.hash(userData.password, 8);
const query = { name: userData.userName, email: userData.mail, pwd: hashedPassword, storeId: userData.storeId, tokens: [] };
const check = await User.findOne({ email: query.email });
if (check) {
return false;
}
const data = await User.create(query);
return data;
}
exports.loginUser = async (userData) => {
const query = { name: userData.userName, email: userData.mail, pwd: userData.password };
const check = await User.findOne({ email: query.email });
if(!check) {
return false;
}
const isMatched = await bcrypt.compare(userData.password, check.pwd);
if (!isMatched) {
return false;
}
const token = jwt.sign({ _id: check["_id"].toString()}, process.env.SECRET_KEY);
check.tokens = check.tokens.concat({ token });
await check.save();
const response = {token};
return response;
}
exports.getLimitedCategories = async (count, storeId) => {
const data = await Category.find({ storeId }).limit(count);
return data;
}
exports.getLimitedProducts = async (count, storeId) => {
const data = await Product.find({ storeId }).limit(count);
return data;
}
exports.getSpecificItem = async (storeId, id) => {
const product = await Product.findOne({ storeId, id });
return product;
}
exports.getProductsByCategory = async (storeId, category) => {
const productsByCategory = await Product.find({ storeId, category })
return productsByCategory;
}