Skip to content

Commit 36d67b3

Browse files
committed
push to heroku
1 parent f615c55 commit 36d67b3

File tree

7 files changed

+438
-0
lines changed

7 files changed

+438
-0
lines changed

models/orderModel.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const mongoose = require('mongoose');
2+
3+
const orderSchema = new mongoose.Schema(
4+
{
5+
user: {
6+
type: mongoose.Schema.ObjectId,
7+
ref: 'User',
8+
required: [true, 'Order must be belong to user'],
9+
},
10+
cartItems: [
11+
{
12+
product: {
13+
type: mongoose.Schema.ObjectId,
14+
ref: 'Product',
15+
},
16+
quantity: Number,
17+
color: String,
18+
price: Number,
19+
},
20+
],
21+
22+
taxPrice: {
23+
type: Number,
24+
default: 0,
25+
},
26+
shippingAddress: {
27+
details: String,
28+
phone: String,
29+
city: String,
30+
postalCode: String,
31+
},
32+
shippingPrice: {
33+
type: Number,
34+
default: 0,
35+
},
36+
totalOrderPrice: {
37+
type: Number,
38+
},
39+
paymentMethodType: {
40+
type: String,
41+
enum: ['card', 'cash'],
42+
default: 'cash',
43+
},
44+
isPaid: {
45+
type: Boolean,
46+
default: false,
47+
},
48+
paidAt: Date,
49+
isDelivered: {
50+
type: Boolean,
51+
default: false,
52+
},
53+
deliveredAt: Date,
54+
},
55+
{ timestamps: true }
56+
);
57+
58+
orderSchema.pre(/^find/, function (next) {
59+
this.populate({
60+
path: 'user',
61+
select: 'name profileImg email phone',
62+
}).populate({
63+
path: 'cartItems.product',
64+
select: 'title imageCover ',
65+
});
66+
67+
next();
68+
});
69+
70+
module.exports = mongoose.model('Order', orderSchema);
71+
72+
// In@in2016
73+
//progahmedelsayed@gmail.com

package-lock.json

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"dependencies": {
1414
"bcryptjs": "^2.4.3",
1515
"colors": "^1.4.0",
16+
"compression": "^1.7.4",
17+
"cors": "^2.8.5",
1618
"dotenv": "^10.0.0",
1719
"express": "^4.17.1",
1820
"express-async-handler": "^1.2.0",
@@ -24,6 +26,7 @@
2426
"nodemailer": "^6.7.2",
2527
"sharp": "^0.29.3",
2628
"slugify": "^1.6.3",
29+
"stripe": "^8.212.0",
2730
"uuid": "^8.3.2"
2831
},
2932
"devDependencies": {

routes/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const wishlistRoute = require('./wishlistRoute');
99
const addressRoute = require('./addressRoute');
1010
const couponRoute = require('./couponRoute');
1111
const cartRoute = require('./cartRoute');
12+
const orderRoute = require('./orderRoute');
1213

1314
const mountRoutes = (app) => {
1415
app.use('/api/v1/categories', categoryRoute);
@@ -22,6 +23,7 @@ const mountRoutes = (app) => {
2223
app.use('/api/v1/addresses', addressRoute);
2324
app.use('/api/v1/coupons', couponRoute);
2425
app.use('/api/v1/cart', cartRoute);
26+
app.use('/api/v1/orders', orderRoute);
2527
};
2628

2729
module.exports = mountRoutes;

routes/orderRoute.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const express = require('express');
2+
const {
3+
createCashOrder,
4+
findAllOrders,
5+
findSpecificOrder,
6+
filterOrderForLoggedUser,
7+
updateOrderToPaid,
8+
updateOrderToDelivered,
9+
checkoutSession,
10+
} = require('../services/orderService');
11+
12+
const authService = require('../services/authService');
13+
14+
const router = express.Router();
15+
16+
router.use(authService.protect);
17+
18+
router.get(
19+
'/checkout-session/:cartId',
20+
authService.allowedTo('user'),
21+
checkoutSession
22+
);
23+
24+
router.route('/:cartId').post(authService.allowedTo('user'), createCashOrder);
25+
router.get(
26+
'/',
27+
authService.allowedTo('user', 'admin', 'manager'),
28+
filterOrderForLoggedUser,
29+
findAllOrders
30+
);
31+
router.get('/:id', findSpecificOrder);
32+
33+
router.put(
34+
'/:id/pay',
35+
authService.allowedTo('admin', 'manager'),
36+
updateOrderToPaid
37+
);
38+
router.put(
39+
'/:id/deliver',
40+
authService.allowedTo('admin', 'manager'),
41+
updateOrderToDelivered
42+
);
43+
44+
module.exports = router;

server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const path = require('path');
33
const express = require('express');
44
const dotenv = require('dotenv');
55
const morgan = require('morgan');
6+
const cors = require('cors');
7+
const compression = require('compression');
68

79
dotenv.config({ path: 'config.env' });
810
const ApiError = require('./utils/apiError');
@@ -17,6 +19,13 @@ dbConnection();
1719
// express app
1820
const app = express();
1921

22+
// Enable other domains to access your application
23+
app.use(cors());
24+
app.options('*', cors());
25+
26+
// compress all responses
27+
app.use(compression());
28+
2029
// Middlewares
2130
app.use(express.json());
2231
app.use(express.static(path.join(__dirname, 'uploads')));

0 commit comments

Comments
 (0)