added orders_spec
This commit is contained in:
parent
bd61cb5646
commit
6d0ffcd1bc
@ -7,6 +7,7 @@ const orderRoutes = (app: express.Application) => {
|
|||||||
app.get('/orders/:id', read)
|
app.get('/orders/:id', read)
|
||||||
app.post('/orders', verifyAuthToken, create)
|
app.post('/orders', verifyAuthToken, create)
|
||||||
app.post('/orders/:id/products', verifyAuthToken, addProduct)
|
app.post('/orders/:id/products', verifyAuthToken, addProduct)
|
||||||
|
app.delete('/orders/:id/products', verifyAuthToken, deleteProduct)
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = new OrderStore()
|
const store = new OrderStore()
|
||||||
@ -36,10 +37,9 @@ const create = async (req: Request, res: Response) => {
|
|||||||
try {
|
try {
|
||||||
const orderInfo: Order = {
|
const orderInfo: Order = {
|
||||||
status: req.body.status,
|
status: req.body.status,
|
||||||
user_id: parseInt(req.body.userId)
|
user_id: parseInt(req.body.user_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('orderInfo', orderInfo)
|
|
||||||
|
|
||||||
const newOrder = await store.create(orderInfo)
|
const newOrder = await store.create(orderInfo)
|
||||||
res.json(newOrder)
|
res.json(newOrder)
|
||||||
@ -56,7 +56,6 @@ const addProduct = async (req: Request, res: Response) => {
|
|||||||
quantity: parseInt(req.body.quantity),
|
quantity: parseInt(req.body.quantity),
|
||||||
product_id: parseInt(req.body.product_id)
|
product_id: parseInt(req.body.product_id)
|
||||||
}
|
}
|
||||||
console.log(orderProductInfo)
|
|
||||||
const addedProduct = await store.addProduct(orderProductInfo)
|
const addedProduct = await store.addProduct(orderProductInfo)
|
||||||
res.json(addedProduct)
|
res.json(addedProduct)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -65,5 +64,15 @@ const addProduct = async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deleteProduct = async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const deletedProduct = await store.deleteProduct(parseInt(req.params.id))
|
||||||
|
res.json(deletedProduct)
|
||||||
|
} catch (err) {
|
||||||
|
res.status(400)
|
||||||
|
res.json(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export default orderRoutes
|
export default orderRoutes
|
@ -72,20 +72,64 @@ export class OrderStore {
|
|||||||
async addProduct(o: OrderProduct): Promise<OrderProduct> {
|
async addProduct(o: OrderProduct): Promise<OrderProduct> {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
const ordersql = 'SELECT * FROM orders WHERE id=($1)'
|
||||||
const conn = await client.connect()
|
const conn = await client.connect()
|
||||||
const sql = 'INSERT INTO order_products (quantity, order_id, product_id) VALUES ($1, $2, $3) RETURNING *'
|
const result = await conn.query(ordersql, [o.order_id])
|
||||||
|
const order = result.rows[0]
|
||||||
|
|
||||||
|
if(order.status !== "active") {
|
||||||
|
throw new Error(`Could not add product ${o.product_id} to order ${o.order_id} because order status is ${order.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.release()
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
const sql = 'INSERT INTO order_products (quantity, order_id, product_id) VALUES ($1, $2, $3) RETURNING *'
|
||||||
|
const conn = await client.connect()
|
||||||
const result = await conn.query(sql, [o.quantity, o.order_id, o.product_id])
|
const result = await conn.query(sql, [o.quantity, o.order_id, o.product_id])
|
||||||
const order = result.rows[0]
|
const order = result.rows[0]
|
||||||
|
|
||||||
conn.release()
|
conn.release()
|
||||||
|
|
||||||
return order
|
return order
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Could not add a new orderProducts ${o.order_id}. Error: ${err}`)
|
throw new Error(`Could not add product ${o.product_id} to order ${o.order_id}: Error: ${err}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteProduct(id: number): Promise<Order> {
|
||||||
|
try {
|
||||||
|
|
||||||
|
const sql = 'DELETE FROM order_products WHERE order_id=($1)'
|
||||||
|
const conn = await client.connect()
|
||||||
|
const result = await conn.query(sql, [id])
|
||||||
|
|
||||||
|
conn.release()
|
||||||
|
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Could not delete order ${id}: Error: ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
const ordersql = 'DELETE FROM orders WHERE id=($1)'
|
||||||
|
const conn = await client.connect()
|
||||||
|
const ordersResult = await conn.query(ordersql, [id])
|
||||||
|
const order = ordersResult.rows[0]
|
||||||
|
|
||||||
|
conn.release()
|
||||||
|
|
||||||
|
return order
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`${err}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,89 @@
|
|||||||
|
import supertest from "supertest"
|
||||||
|
import app from "../../server"
|
||||||
|
import { Product } from '../../models/product'
|
||||||
|
import { User } from '../../models/user'
|
||||||
|
import { Order, OrderProduct } from '../../models/order'
|
||||||
|
|
||||||
|
const token = process.env.TOKEN_SECRET_TEST as string
|
||||||
|
const request = supertest(app);
|
||||||
|
|
||||||
|
const testProduct: Product = {
|
||||||
|
id: 3,
|
||||||
|
name: "metro",
|
||||||
|
price: 10
|
||||||
|
}
|
||||||
|
|
||||||
|
const testUser: User = {
|
||||||
|
id: 3,
|
||||||
|
firstname: 'John',
|
||||||
|
lastname: 'Doe',
|
||||||
|
username: 'Jd',
|
||||||
|
password: 'password'
|
||||||
|
}
|
||||||
|
|
||||||
|
const testOrder: Order = {
|
||||||
|
status: "active",
|
||||||
|
user_id: 3
|
||||||
|
}
|
||||||
|
|
||||||
|
const testOrderProduct: OrderProduct = {
|
||||||
|
id: 2,
|
||||||
|
quantity: 5,
|
||||||
|
order_id: 2,
|
||||||
|
product_id: 3
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Order handler", () => {
|
||||||
|
|
||||||
|
beforeAll( async () => {
|
||||||
|
const product = await request
|
||||||
|
.post('/products')
|
||||||
|
.auth(token, {type: "bearer"})
|
||||||
|
.send(testProduct)
|
||||||
|
|
||||||
|
const user = await request
|
||||||
|
.post('/users')
|
||||||
|
.auth(token, {type: "bearer"})
|
||||||
|
.send(testUser)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should create a new order', async () => {
|
||||||
|
const response = await request
|
||||||
|
.post("/orders")
|
||||||
|
.auth(token, {type: "bearer"})
|
||||||
|
.send(testOrder)
|
||||||
|
|
||||||
|
expect(response.status).toBe(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should index orders', async () => {
|
||||||
|
const response = await request
|
||||||
|
.get("/orders")
|
||||||
|
|
||||||
|
expect(response.status).toBe(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should get order by id', async () => {
|
||||||
|
const response = await request
|
||||||
|
.get("/orders/2")
|
||||||
|
|
||||||
|
expect(response.status).toBe(200)
|
||||||
|
})
|
||||||
|
it('Should add a new product to order', async () => {
|
||||||
|
const response = await request
|
||||||
|
.post('/orders/2/products')
|
||||||
|
.auth(token, {type: 'bearer'})
|
||||||
|
.send(testOrderProduct)
|
||||||
|
|
||||||
|
expect(response.status).toBe(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should delete order', async () => {
|
||||||
|
const response = await request
|
||||||
|
.delete('/orders/2/products')
|
||||||
|
.auth(token, {type: 'bearer'})
|
||||||
|
|
||||||
|
expect(response.status).toBe(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
@ -1,6 +1,6 @@
|
|||||||
import { User, UserStore } from '../../models/user'
|
import { User, UserStore } from '../../models/user'
|
||||||
import { Product, ProductStore } from '../../models/product';
|
import { Product, ProductStore } from '../../models/product';
|
||||||
import { Order, OrderProduct, OrderStore } from '../../models/order'
|
import { Order, OrderProduct, OrderStore } from '../../models/order'
|
||||||
|
|
||||||
const orderStore = new OrderStore()
|
const orderStore = new OrderStore()
|
||||||
const userStore = new UserStore()
|
const userStore = new UserStore()
|
||||||
@ -74,11 +74,19 @@ describe("Order model", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
it('addProduct should add a new order_products', async () => {
|
it('addProduct should add a new product to order', async () => {
|
||||||
const result = await orderStore.addProduct(testOrderProduct)
|
const result = await orderStore.addProduct(testOrderProduct)
|
||||||
expect(result).toEqual(testOrderProduct)
|
expect(result).toEqual(testOrderProduct)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('deleteProduct should delete product from order', async () => {
|
||||||
|
const result = await orderStore.deleteProduct(1)
|
||||||
|
|
||||||
|
const checkorder = await orderStore.index()
|
||||||
|
expect(checkorder).toEqual([])
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user