Merge pull request 'feat: add get master with orders' (#68) from feat/get-master into master
Reviewed-on: #68
This commit is contained in:
commit
3c89d8b9a8
@ -1,16 +1,40 @@
|
|||||||
const router = require('express').Router()
|
const router = require('express').Router()
|
||||||
const {MasterModel} = require('./model/master')
|
const {MasterModel} = require('./model/master')
|
||||||
const mongoose = require("mongoose")
|
const mongoose = require("mongoose")
|
||||||
|
const {OrderModel} = require("./model/order")
|
||||||
|
|
||||||
|
|
||||||
router.get('/masters', async (req, res,next) => {
|
router.get("/masters", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const master = await MasterModel.find({})
|
const masters = await MasterModel.find({});
|
||||||
res.status(200).send({success: true, body: master})
|
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) {
|
} catch (error) {
|
||||||
next(error)
|
next(error);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
router.delete('/masters/:id', async (req, res,next) => {
|
router.delete('/masters/:id', async (req, res,next) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
Loading…
Reference in New Issue
Block a user