diff --git a/src/handlers/orders.ts b/src/handlers/orders.ts index 3435f83..6be44d7 100644 --- a/src/handlers/orders.ts +++ b/src/handlers/orders.ts @@ -7,6 +7,7 @@ const orderRoutes = (app: express.Application) => { app.get('/orders/:id', read) app.post('/orders', verifyAuthToken, create) app.post('/orders/:id/products', verifyAuthToken, addProduct) + app.delete('/orders/:id/products', verifyAuthToken, deleteProduct) } const store = new OrderStore() @@ -36,10 +37,9 @@ const create = async (req: Request, res: Response) => { try { const orderInfo: Order = { 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) res.json(newOrder) @@ -56,7 +56,6 @@ const addProduct = async (req: Request, res: Response) => { quantity: parseInt(req.body.quantity), product_id: parseInt(req.body.product_id) } - console.log(orderProductInfo) const addedProduct = await store.addProduct(orderProductInfo) res.json(addedProduct) } 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 \ No newline at end of file diff --git a/src/models/order.ts b/src/models/order.ts index 971cff7..b72c438 100644 --- a/src/models/order.ts +++ b/src/models/order.ts @@ -72,20 +72,64 @@ export class OrderStore { async addProduct(o: OrderProduct): Promise { try { + const ordersql = 'SELECT * FROM orders WHERE id=($1)' 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 order = result.rows[0] conn.release() return order - } 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 { + 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}`) + } + } } \ No newline at end of file diff --git a/src/tests/handlers/orders_spec.ts b/src/tests/handlers/orders_spec.ts index e69de29..cd557f3 100644 --- a/src/tests/handlers/orders_spec.ts +++ b/src/tests/handlers/orders_spec.ts @@ -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) +}) + +}) \ No newline at end of file diff --git a/src/tests/models/order_spec.ts b/src/tests/models/order_spec.ts index 70d4683..bc52af3 100644 --- a/src/tests/models/order_spec.ts +++ b/src/tests/models/order_spec.ts @@ -1,6 +1,6 @@ import { User, UserStore } from '../../models/user' 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 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) 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 () => {