refactor: extract findUserOrder helper

This commit is contained in:
Kirill
2026-05-27 21:47:16 +05:00
parent 24b3b4063d
commit 45627206c0
5 changed files with 246 additions and 196 deletions
+8 -13
View File
@@ -1,6 +1,7 @@
import { NOTIFICATION_EVENTS } from '../../../shared/constants/notification-events.js'
import { asyncHandler } from '../lib/async-handler.js'
import { isDeliveryCarrier } from '../lib/delivery-carrier.js'
import { findUserOrder } from '../lib/find-user-order.js'
import { prisma } from '../lib/prisma.js'
export async function registerUserOrderRoutes(fastify) {
@@ -207,11 +208,10 @@ export async function registerUserOrderRoutes(fastify) {
asyncHandler(async (request, reply) => {
const userId = request.user.sub
const { id } = request.params
const order = await prisma.order.findFirst({
where: { id, userId },
include: { items: true, messages: { orderBy: { createdAt: 'asc' } } },
const order = await findUserOrder(prisma, id, userId, {
items: true,
messages: { orderBy: { createdAt: 'asc' } },
})
if (!order) return reply.code(404).send({ error: 'Заказ не найден' })
return { item: order }
}),
)
@@ -219,14 +219,10 @@ export async function registerUserOrderRoutes(fastify) {
fastify.get(
'/api/me/orders/:id/review-eligibility',
{ preHandler: [fastify.authenticate] },
async (request, reply) => {
asyncHandler(async (request, reply) => {
const userId = request.user.sub
const { id } = request.params
const order = await prisma.order.findFirst({
where: { id, userId },
include: { items: true },
})
if (!order) return reply.code(404).send({ error: 'Заказ не найден' })
const order = await findUserOrder(prisma, id, userId, { items: true })
if (order.status !== 'DONE') {
return { canReview: false, items: [] }
}
@@ -253,7 +249,7 @@ export async function registerUserOrderRoutes(fastify) {
hasReview: reviewed.has(x.productId),
})),
}
},
}),
)
fastify.post(
@@ -262,8 +258,7 @@ export async function registerUserOrderRoutes(fastify) {
asyncHandler(async (request, reply) => {
const userId = request.user.sub
const { id } = request.params
const order = await prisma.order.findFirst({ where: { id, userId } })
if (!order) return reply.code(404).send({ error: 'Заказ не найден' })
const order = await findUserOrder(prisma, id, userId)
const okDelivery = order.deliveryType === 'delivery' && order.status === 'SHIPPED'
const okPickup = order.deliveryType === 'pickup' && order.status === 'READY_FOR_PICKUP'