Compare commits

..

2 Commits

Author SHA1 Message Date
RustamRu
979bf24767 feat: extract order handlers 2025-01-19 11:49:29 +03:00
RustamRu
98daf19853 remove try-catch 2025-01-19 11:45:42 +03:00
295 changed files with 105 additions and 162 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "multi-stub",
"version": "1.2.0",
"version": "1.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "multi-stub",
"version": "1.2.0",
"version": "1.1.0",
"license": "MIT",
"dependencies": {
"axios": "^1.7.9",

View File

@ -1,6 +1,6 @@
{
"name": "multi-stub",
"version": "1.2.0",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {

View File

@ -1,21 +1,13 @@
const router = require('express').Router()
const { OrderModel } = require('./model/order')
router.post('/orders', async (req, res, next) => {
const {startDate, endDate} = req.body
if (!startDate || !endDate) {
throw new Error('startDate and endDate are required')
}
const orders = await OrderModel.find({
$or: [
{startWashTime: { $gte: new Date(startDate), $lte: new Date(endDate) }},
{endWashTime: { $gte: new Date(startDate), $lte: new Date(endDate) }},
]
})
router.get('/orders', async (req, res, next) => {
try {
const orders = await OrderModel.find({})
res.status(200).send({ success: true, body: orders })
} catch (error) {
next(error)
}
})
module.exports = router
module.exports = router

View File

@ -74,7 +74,7 @@ const VALIDATION_MESSAGES = {
},
}
router.post('/create', async (req, res, next) => {
const createOrder = 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 })
res.status(200).send({ success: true, body: order })
}
} catch (error) {
next(error)
}
})
router.get('/:id', async (req, res, next) => {
const getOrder = 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)
}
})
router.patch('/:id', async (req, res, next) => {
res.status(200).send({ success: true, body: order })
}
const updateOrder = 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,48 @@ 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()
router.delete('/:id', async (req, res, next) => {
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 })
}
const deleteOrder = 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 })
}
router.post('/create', createOrder)
router.get('/:id', getOrder)
router.patch('/:id', updateOrder)
router.delete('/:id', deleteOrder)
module.exports = router

Some files were not shown because too many files have changed in this diff Show More