Merge pull request 'feat: add get master with orders' (#68) from feat/get-master into master

Reviewed-on: #68
This commit is contained in:
Primakov Alexandr Alexandrovich 2025-01-26 11:02:24 +03:00
commit 3c89d8b9a8

View File

@ -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;