Compare commits

..

No commits in common. "979bf24767e3378fc5be9baf1a25c5d1fac452cd" and "949416d2a3ffa40786c7745ed04419cb51b6a434" have entirely different histories.

View File

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