From bdc8d9a8e01f2be6270bf33f3000578f3c19412e Mon Sep 17 00:00:00 2001 From: ilnaz <237x237@gmail.com> Date: Sun, 26 Jan 2025 00:41:48 +0300 Subject: [PATCH] feat: add get master with orders --- server/routers/dry-wash/arm-master.js | 34 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/server/routers/dry-wash/arm-master.js b/server/routers/dry-wash/arm-master.js index 7288d47..ce1af19 100644 --- a/server/routers/dry-wash/arm-master.js +++ b/server/routers/dry-wash/arm-master.js @@ -1,16 +1,40 @@ const router = require('express').Router() const {MasterModel} = require('./model/master') const mongoose = require("mongoose") +const {OrderModel} = require("./model/order") -router.get('/masters', async (req, res,next) => { +router.get("/masters", async (req, res, next) => { try { - const master = await MasterModel.find({}) - res.status(200).send({success: true, body: master}) + const masters = await MasterModel.find({}); + const orders = await OrderModel.find({}); + + const mastersWithOrders = masters.map((master) => { + const masterOrders = orders.filter((order) => { + return ( + order?.master && order.master.toString() === master._id.toString() + ); + }); + + const schedule = masterOrders.map((order) => ({ + id: order._id, + startWashTime: order.startWashTime, + endWashTime: order.endWashTime, + })); + + return { + id: master._id, + name: master.name, + schedule: schedule, + phone: master.phone, + }; + }); + + res.status(200).send({ success: true, body: mastersWithOrders }); } catch (error) { - next(error) + next(error); } -}) +}); router.delete('/masters/:id', async (req, res,next) => { const { id } = req.params;