-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.js
89 lines (80 loc) · 1.57 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
const exportsObj = {}
const Order = require('../models').order
const Product = require('../models').product
const Shipping = require('../models').shipping
const User = require('../models').user
exportsObj.getBoughtOrders = (userId) => {
const options = {
where: {
usrId: userId
},
include: [{
model: Product,
as: 'product'
}]
}
return Order.findAll(options)
}
exportsObj.getSoldOrders = (userId) => {
const productOptions = {
where: {
selId: userId
},
attributes: ['id']
}
return Product.findAll(productOptions)
.then(products => products.map(product => product.id))
.then((productIds) => {
const orderOptions = {
where: {
proId: productIds
},
include: [{
model: Product,
as: 'product'
}]
}
return Order.findAll(orderOptions)
})
}
exportsObj.getOrderById = (orderId) => {
const options = {
include: [{
model: Product,
as: 'product',
include: [{
model: User,
as: 'seller'
}]
}, {
model: User,
as: 'buyer'
}, {
model: Shipping,
as: 'shipping'
}],
where: { id: orderId }
}
return Order.findOne(options)
}
exportsObj.getOrder = (order) => {
const options = {
include: [{
model: Product,
as: 'product'
}],
where: order
}
return Order.findOne(options)
}
exportsObj.insertOrder = (order) => {
return Order.create(order)
}
exportsObj.updateOrder = (order) => {
return Order.update(order, { where: { id: order.id } })
}
exportsObj.deleteOrder = (ordId) => {
return Order.destroy({ where: { id: ordId }})
.then(() => ({ id: ordId }))
}
module.exports = exportsObj