Merge pull request 'feature/master-date' (#108) from feature/master-date into master
Reviewed-on: #108
This commit is contained in:
commit
2f1e1dc040
@ -1,111 +1,116 @@
|
|||||||
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")
|
const { OrderModel } = require("./model/order");
|
||||||
|
|
||||||
|
|
||||||
router.get("/masters", async (req, res, next) => {
|
router.get("/masters", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const masters = await MasterModel.find({});
|
const masters = await MasterModel.find({});
|
||||||
const orders = await OrderModel.find({});
|
|
||||||
|
|
||||||
const mastersWithOrders = masters.map((master) => {
|
// Создаем объекты для начала и конца текущего дня
|
||||||
const masterOrders = orders.filter((order) => {
|
const today = new Date();
|
||||||
return (
|
today.setHours(0, 0, 0, 0);
|
||||||
order?.master && order.master.toString() === master._id.toString()
|
const tomorrow = new Date(today);
|
||||||
);
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||||
});
|
|
||||||
|
|
||||||
const schedule = masterOrders.map((order) => ({
|
const orders = await OrderModel.find({
|
||||||
id: order._id,
|
startWashTime: {
|
||||||
startWashTime: order.startWashTime,
|
$gte: today,
|
||||||
endWashTime: order.endWashTime,
|
$lt: tomorrow,
|
||||||
}));
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
const mastersWithOrders = masters.map((master) => {
|
||||||
id: master._id,
|
const masterOrders = orders.filter((order) => {
|
||||||
name: master.name,
|
return (
|
||||||
schedule: schedule,
|
order?.master && order.master.toString() === master._id.toString()
|
||||||
phone: master.phone,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).send({ success: true, body: mastersWithOrders });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete('/masters/:id', async (req, res,next) => {
|
|
||||||
const { id } = req.params;
|
|
||||||
|
|
||||||
if (!mongoose.Types.ObjectId.isValid(id)){
|
|
||||||
throw new Error('ID is required')
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const master = await MasterModel.findByIdAndDelete(id, {
|
|
||||||
new: true,
|
|
||||||
});
|
|
||||||
if (!master) {
|
|
||||||
throw new Error('master not found')
|
|
||||||
}
|
|
||||||
res.status(200).send({success: true, body: master})
|
|
||||||
} catch (error) {
|
|
||||||
next(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
router.post('/masters', async (req, res,next) => {
|
|
||||||
|
|
||||||
const {name, phone} = req.body
|
|
||||||
|
|
||||||
if (!name || !phone ){
|
|
||||||
throw new Error('Enter name and phone')
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const master = await MasterModel.create({name, phone})
|
|
||||||
res.status(200).send({success: true, body: master})
|
|
||||||
} catch (error) {
|
|
||||||
next(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
router.patch('/masters/:id', async (req, res, next) => {
|
|
||||||
const { id } = req.params;
|
|
||||||
|
|
||||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
||||||
throw new Error('ID is required')
|
|
||||||
}
|
|
||||||
|
|
||||||
const { name, phone } = req.body;
|
|
||||||
|
|
||||||
if (!name && !phone) {
|
|
||||||
throw new Error('Enter name and phone')
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
const updateData = {};
|
|
||||||
if (name) updateData.name = name;
|
|
||||||
if (phone) updateData.phone = phone;
|
|
||||||
|
|
||||||
const master = await MasterModel.findByIdAndUpdate(
|
|
||||||
id,
|
|
||||||
updateData,
|
|
||||||
{ new: true }
|
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
if (!master) {
|
const schedule = masterOrders.map((order) => ({
|
||||||
throw new Error('master not found')
|
id: order._id,
|
||||||
}
|
startWashTime: order.startWashTime,
|
||||||
|
endWashTime: order.endWashTime,
|
||||||
|
}));
|
||||||
|
|
||||||
res.status(200).send({ success: true, body: master });
|
return {
|
||||||
} catch (error) {
|
id: master._id,
|
||||||
next(error);
|
name: master.name,
|
||||||
}
|
schedule: schedule,
|
||||||
|
phone: master.phone,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).send({ success: true, body: mastersWithOrders });
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router
|
router.delete("/masters/:id", async (req, res, next) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||||
|
throw new Error("ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const master = await MasterModel.findByIdAndDelete(id, {
|
||||||
|
new: true,
|
||||||
|
});
|
||||||
|
if (!master) {
|
||||||
|
throw new Error("master not found");
|
||||||
|
}
|
||||||
|
res.status(200).send({ success: true, body: master });
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/masters", async (req, res, next) => {
|
||||||
|
const { name, phone } = req.body;
|
||||||
|
|
||||||
|
if (!name || !phone) {
|
||||||
|
throw new Error("Enter name and phone");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const master = await MasterModel.create({ name, phone });
|
||||||
|
res.status(200).send({ success: true, body: master });
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.patch("/masters/:id", async (req, res, next) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||||
|
throw new Error("ID is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { name, phone } = req.body;
|
||||||
|
|
||||||
|
if (!name && !phone) {
|
||||||
|
throw new Error("Enter name and phone");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const updateData = {};
|
||||||
|
if (name) updateData.name = name;
|
||||||
|
if (phone) updateData.phone = phone;
|
||||||
|
|
||||||
|
const master = await MasterModel.findByIdAndUpdate(id, updateData, {
|
||||||
|
new: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!master) {
|
||||||
|
throw new Error("master not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).send({ success: true, body: master });
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
Loading…
Reference in New Issue
Block a user