-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.js
219 lines (182 loc) · 5.98 KB
/
orders.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict'
const epilogue = require('./epilogue');
const db = require('APP/db');
const { OrderProduct, Order, Product, User } = require('APP/db/models');
const customOrderRoutes = require('express').Router();
// --------------------------------------------------------------------------
// Orders GET
customOrderRoutes.get('/', function (request, response, next) {
Order.findAll({
include: [ Product, User ]
})
.then((orderArray) => {
response.json(orderArray);
})
.catch(next);
});
// --------------------------------------------------------------------------
// Orders GET :orderNumber
customOrderRoutes.get('/:orderNumber', function (request, response, next) {
Order.findOne({
include: [ Product, User ],
where: {
orderNumber: request.params.orderNumber
}
})
.then((order) => {
response.json(order);
})
.catch(next);
});
// --------------------------------------------------------------------------
// Orders GET :orderNumber/user
customOrderRoutes.get('/:orderNumber/user', function (request, response, next) {
Order.findOne({
include: [ Product, User ],
where: {
orderNumber: request.params.orderNumber
}
})
.then((order) => {
response.json(order.user);
})
.catch(next);
});
// --------------------------------------------------------------------------
// Orders GET :orderNumber/products
customOrderRoutes.get('/:orderNumber/products', function (request, response, next) {
const products = [];
Order.findOne({
include: [ Product, User ],
where: {
orderNumber: request.params.orderNumber
}
})
.then((order) => {
order.products.map((prod, index) => {
return products[index] = {
artistName: prod.artistName,
description: prod.description,
price: prod.order_products.price,
quantity: prod.order_products.quantity,
totalAmount: prod.order_products.totalAmount,
id: prod.id,
image: prod.image,
genre: prod.genre,
location: prod.location,
email: prod.email,
audioSample: prod.audioSample,
rating: prod.rating
}
});
response.json(products);
})
.catch(next);
});
// --------------------------------------------------------------------------
// Orders POST
// Request.body must have
// - user_id: is an id (id is a string)
// - productIds: is an ARRAY of product ids (ids are strings)
// - quantity should be on the body (quantity must be an integer because the model schema demands it!)
// it doesn't make sense to handle quantity on the post route now that I think about it because we don't want the user to be able to 'submit' the order if we are out of stock. The whole point of having the quantity column is to validate whether we can approve the purchase. So we have to validate quantity on 'state' and throw a front end warning/block as well as disable the submit button. Check the product table for the instance method I wrote - that instance method should be called before post route is hit. This being said we have to somehow handle the post route for security purposes --> if someone hacks the api and submits an order for stock that isn't available how do we handle?
// - products: array of objects. Each object has
// - pId (strings)
// - quantity (strings)
// - price (strings)
// Example Input Object (i.e., request.body):
// {
// "deliveryAddress": "5 Hanover Square, New York, NY 10001",
// "products": [
// { "pId": "1", "quantity": "10", "price": "100" },
// { "pId": "2", "quantity": "20", "price": "200" }
// ],
// "user_id": "2",
// "quantity": ["10", "20"]
// }
customOrderRoutes.post('/', function (request, response, next) {
console.log(request.body)
// const temp = request.body;
// if temp
const cartProducts = request.body.products;
const cartProductIds = [];
cartProducts.forEach((product) => {
// cartProductIds.push(product.pId); changing from pid to id
cartProductIds.push(product.id);
});
let grandTotal = 0;
Order.create({
deliveryAddress: request.body.deliveryAddress
})
.then((order) => {
return User.findOne({
where: {
id: request.body.user_id
}
})
.then((user) => {
return order.setUser(user);
});
})
.then((order) => {
return Product.findAll({
where: {
id: cartProductIds
}
})
.then((productsArray) => {
productsArray.forEach((prod) => {
cartProducts.forEach((cartPr) => {
// if(prod.id.toString()===cartPr.pId.toString()) { changing from pid
if(prod.id.toString()===cartPr.id.toString()) {
grandTotal += cartPr.quantity*cartPr.price;
order.addProduct(prod, {
quantity: cartPr.quantity,
price: cartPr.price,
totalAmount: cartPr.quantity*cartPr.price
});
}
});
});
return order;
});
})
.then((order) => {
order.grandTotal = grandTotal;
return order.save();
})
.then((order) => {
// response.sendStatus(200);
cartProducts.forEach((product) => {
Product.findOne({
where: {id: product.id}
})
.then(data => data.updateQuantity(product.id))
});
response.json(order)
})
.catch(next);
});
// --------------------------------------------------------------------------
// Orders PUT
// For MVP, we will not provide the ability to update an order after it is submitted
// --------------------------------------------------------------------------
// Orders DELETE
// Request.body must have
// - orderNumber: is a string
// Example Input Object (i.e., request.body):
// {
// "orderNumber": "2F29EEF8D4"
// }
customOrderRoutes.delete('/', function (request, response, next) {
Order.destroy({
where: {
orderNumber: request.body.orderNumber
}
})
.then((deleted) => {
response.sendStatus(200);
})
.catch(next);
});
module.exports = customOrderRoutes;