diff --git a/server/routers/dry-wash/order.js b/server/routers/dry-wash/order.js index d258fa0..f93edc3 100644 --- a/server/routers/dry-wash/order.js +++ b/server/routers/dry-wash/order.js @@ -74,7 +74,7 @@ const VALIDATION_MESSAGES = { }, } -router.post('/create', async (req, res, next) => { +router.post('/create', async (req, res) => { const bodyErrors = [] const { customer } = req.body @@ -120,46 +120,37 @@ router.post('/create', async (req, res, next) => { throw new Error(bodyErrors.join(', ')) } - try { - const order = await OrderModel.create({ - phone: customer.phone, - carNumber: car.number, - carBody: car.body, - carColor: car.color, - startWashTime: washing.begin, - endWashTime: washing.end, - location: washing.location, - status: orderStatus.PROGRESS, - notes: '', - created: new Date().toISOString(), - }) + const order = await OrderModel.create({ + phone: customer.phone, + carNumber: car.number, + carBody: car.body, + carColor: car.color, + startWashTime: washing.begin, + endWashTime: washing.end, + location: washing.location, + status: orderStatus.PROGRESS, + notes: '', + created: new Date().toISOString(), + }) - res.status(200).send({ success: true, body: order }) - - } catch (error) { - next(error) - } + res.status(200).send({ success: true, body: order }) }) -router.get('/:id', async (req, res, next) => { +router.get('/:id', async (req, res) => { const { id } = req.params if (!mongoose.Types.ObjectId.isValid(id)) { throw new Error(VALIDATION_MESSAGES.orderId.invalid) } - try { - const order = await OrderModel.findById(id) - if (!order) { - throw new Error(VALIDATION_MESSAGES.order.notFound) - } - - res.status(200).send({ success: true, body: order }) - } catch (error) { - next(error) + const order = await OrderModel.findById(id) + if (!order) { + throw new Error(VALIDATION_MESSAGES.order.notFound) } + + res.status(200).send({ success: true, body: order }) }) -router.patch('/:id', async (req, res, next) => { +router.patch('/:id', async (req, res) => { const { id } = req.params if (!mongoose.Types.ObjectId.isValid(id)) { throw new Error(VALIDATION_MESSAGES.orderId.invalid) @@ -179,13 +170,9 @@ router.patch('/:id', async (req, res, next) => { if (!mongoose.Types.ObjectId.isValid(masterId)) { bodyErrors.push(VALIDATION_MESSAGES.masterId.invalid) } else { - try { - const master = await MasterModel.findById(masterId) - if (!master) { - bodyErrors.push(VALIDATION_MESSAGES.master.notFound) - } - } catch (error) { - next(error) + const master = await MasterModel.findById(masterId) + if (!master) { + bodyErrors.push(VALIDATION_MESSAGES.master.notFound) } } } @@ -201,51 +188,43 @@ router.patch('/:id', async (req, res, next) => { throw new Error(bodyErrors.join(', ')) } - try { - const updateData = {} - if (status) { - updateData.status = status - } - if (masterId) { - updateData.master = masterId - } - if (notes) { - updateData.notes = notes - } - updateData.updated = new Date().toISOString() - - const order = await OrderModel.findByIdAndUpdate( - id, - updateData, - { new: true } - ) - if (!order) { - throw new Error(VALIDATION_MESSAGES.order.notFound) - } - - res.status(200).send({ success: true, body: order }) - } catch (error) { - next(error) + const updateData = {} + if (status) { + updateData.status = status } + if (masterId) { + updateData.master = masterId + } + if (notes) { + updateData.notes = notes + } + updateData.updated = new Date().toISOString() + + const order = await OrderModel.findByIdAndUpdate( + id, + updateData, + { new: true } + ) + if (!order) { + throw new Error(VALIDATION_MESSAGES.order.notFound) + } + + res.status(200).send({ success: true, body: order }) }) -router.delete('/:id', async (req, res, next) => { +router.delete('/:id', async (req, res) => { const { id } = req.params if (!mongoose.Types.ObjectId.isValid(id)) { throw new Error(VALIDATION_MESSAGES.orderId.invalid) } - try { - const order = await OrderModel.findByIdAndDelete(id, { - new: true, - }) - if (!order) { - throw new Error(VALIDATION_MESSAGES.order.notFound) - } - res.status(200).send({ success: true, body: order }) - } catch (error) { - next(error) + const order = await OrderModel.findByIdAndDelete(id, { + new: true, + }) + if (!order) { + throw new Error(VALIDATION_MESSAGES.order.notFound) } + res.status(200).send({ success: true, body: order }) }) module.exports = router \ No newline at end of file