generated from lighthouse-labs/node-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 4
/
database.js
136 lines (127 loc) · 4.02 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
const { Pool } = require("pg");
const dbParams = require("./lib/db.js");
const pool = new Pool(dbParams);
/// Orders
/**
* Get all orders in the database.
* @param {Number} limit Limits the number of results.
* @return {Promise<{}>} A promise to the order.
*/
const getAllOrders = (limit = 8) => {
return pool
.query(`
SELECT
orders.id,
order_date,
customers.name,
customers.phone_number,
confirmed,
completed,
json_agg(json_build_object('food_name', food_items.food_name, 'quantity', quantity)) AS food_items
FROM orders
JOIN customers ON customers.id = customer_id
JOIN order_food_items ON order_id = orders.id
JOIN food_items ON food_items.id = food_item_id
GROUP BY orders.id, customers.name, customers.phone_number
ORDER BY order_date DESC
LIMIT $1;
`, [limit])
.then((result) => result.rows)
.catch((err) => err.message)
};
exports.getAllOrders = getAllOrders;
/**
* Get a single order from the database given its id.
* @param {string} id The id of the order.
* @return {Promise<{}>} A promise to the order.
*/
const getOrderWithId = (id) => {
return pool
.query(`
SELECT
orders.*,
customers.name AS customer_name,
customers.phone_number AS customer_phone_number,
restaurants.name AS restaurant_name,
restaurants.phone_number AS restaurant_phone_number,
json_agg(json_build_object('food_name', food_items.food_name, 'quantity', quantity, 'picture_url', picture_url, 'price', (price * quantity))) AS food_items,
sum(price * quantity) AS total
FROM orders
JOIN restaurants ON restaurants.id = restaurant_id
JOIN customers ON customers.id = customer_id
JOIN order_food_items ON order_id = orders.id
JOIN food_items ON food_items.id = food_item_id
WHERE orders.id = $1
GROUP BY orders.id, customers.name, customers.phone_number, restaurants.name, restaurants.phone_number;
`, [id])
.then((result) => result.rows[0])
.catch((err) => err.message)
};
exports.getOrderWithId = getOrderWithId;
/**
* Set order's status to confirmed.
* @param {array} queryParams Estimated pickup time and order id
* @return {Promise<{}>} A promise to the order.
*/
const confirmOrder = (queryParams) => {
return pool
.query(`
UPDATE orders
SET estimated_pickup = $1,
confirmed = TRUE
WHERE id = $2;
`, queryParams)
.then((result) => result.rows)
.catch((err) => err.message)
};
exports.confirmOrder = confirmOrder;
/**
* Set order's status to confirmed.
* @param {array} id order id
* @return {Promise<{}>} A promise to the order.
*/
const completeOrder = (id) => {
return pool
.query(`
UPDATE orders
SET completed = TRUE
WHERE id = $1;
`, [id])
.then((result) => result.rows)
.catch((err) => err.message)
};
exports.completeOrder = completeOrder;
const createCustomer = (name, phoneNumber) => {
return pool
.query(`
INSERT INTO customers (name, phone_number)
VALUES ($1, $2)
RETURNING id;
`,[name, phoneNumber])
.then((result) => result.rows[0])
.catch((err) => err.message)
}
exports.createCustomer = createCustomer;
const createOrder = (restaurantId, customerId, preferredPickup) => {
return pool
.query(`
INSERT INTO orders (restaurant_id, customer_id, preferred_pickup)
VALUES ($1, $2, $3)
RETURNING id;
`,[restaurantId, customerId, preferredPickup])
.then((result) => result.rows[0])
.catch((err) => err.message)
}
exports.createOrder = createOrder;
const bridgeOrderFoodItems = (orderId, cart) => {
let queries = [];
cart.forEach((item) => {
queries.push(
pool.query('INSERT INTO order_food_items (food_item_id, order_id, quantity) VALUES ($1, $2, $3) RETURNING order_id', [item.food_items_id, orderId, item.quantity])
)
})
return Promise.all(queries)
.then((result) => result[0].rows[0])
.catch((err) => err.message)
}
exports.bridgeOrderFoodItems = bridgeOrderFoodItems;