From 67021c9971b6fcbe1073b309c41b563b40329cff Mon Sep 17 00:00:00 2001 From: Pablo Alonso Date: Sun, 1 May 2022 19:43:26 +0200 Subject: [PATCH] Cambio de estado de pedidos en metodos find --- restapi/controllers/order.ts | 76 ++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/restapi/controllers/order.ts b/restapi/controllers/order.ts index 6b26ac6..f01296c 100644 --- a/restapi/controllers/order.ts +++ b/restapi/controllers/order.ts @@ -7,7 +7,7 @@ const nodeMailer = require("nodemailer"); export const findAll = async (req: Request, res: Response): Promise => { const orders = await Order.find(); - const today = new Date() + const today = new Date(); orders.forEach(order => { if (order.status != "RECEIVED") { @@ -19,23 +19,49 @@ export const findAll = async (req: Request, res: Response): Promise => order.save(); } }); + return res.status(200).json({ orders }); }; export const findById = async (req: Request, res: Response): Promise => { - if(!req.params.id){ + if (!req.params.id) { return res.status(400).json({ msg: "Please. Send an ID" }); } const order = await Order.findById(req.params.id); + if (order) { + const today = new Date(); + + if (order.status != "RECEIVED") { + if (order.receptionDate <= today) { + order.status = "RECEIVED" + } else if (order.orderDate < today) { + order.status = "ON DELIVERY" + } + order.save(); + } + } + return res.status(200).json({ order }); }; export const findByUsername = async (req: Request, res: Response): Promise => { - if(!req.params.email){ + if (!req.params.email) { return res.status(400).json({ msg: "Please. Send an ID" }); } const orders = await Order.find({ user: req.params.email }); + const today = new Date(); + + orders.forEach(order => { + if (order.status != "RECEIVED") { + if (order.receptionDate <= today) { + order.status = "RECEIVED" + } else if (order.orderDate < today) { + order.status = "ON DELIVERY" + } + order.save(); + } + }); return res.status(200).json({ orders }); }; @@ -50,19 +76,19 @@ export const createOrder = async (req: Request, res: Response): Promise => {