fix: email validation, conditional order update, improved tests for payment routes
This commit is contained in:
@@ -9,6 +9,11 @@ export async function registerUserPaymentRoutes(fastify) {
|
||||
async (request, reply) => {
|
||||
const userId = request.user.sub
|
||||
const userEmail = request.user.email
|
||||
|
||||
if (!userEmail) {
|
||||
return reply.code(422).send({ error: 'Для онлайн-оплаты необходим email в профиле' })
|
||||
}
|
||||
|
||||
const { id } = request.params
|
||||
|
||||
const order = await prisma.order.findFirst({
|
||||
@@ -42,7 +47,7 @@ export async function registerUserPaymentRoutes(fastify) {
|
||||
return { confirmationUrl: existingPayment.confirmationUrl }
|
||||
}
|
||||
|
||||
const idempotencyKey = `${id}-v1`
|
||||
const idempotencyKey = `${id}-${Date.now()}`
|
||||
const returnUrl = `${process.env.CLIENT_PUBLIC_URL || 'http://127.0.0.1:5173'}/me/orders/${id}?paid=1`
|
||||
const clientIp = request.ip
|
||||
|
||||
@@ -123,15 +128,17 @@ export async function registerUserPaymentRoutes(fastify) {
|
||||
})
|
||||
|
||||
if (ykPayment.status === 'succeeded' && order.status === 'PENDING_PAYMENT') {
|
||||
await prisma.order.update({
|
||||
where: { id: orderId },
|
||||
const updated = await prisma.order.updateMany({
|
||||
where: { id: orderId, status: 'PENDING_PAYMENT' },
|
||||
data: { status: 'PAID' },
|
||||
})
|
||||
request.server.eventBus.emit(NOTIFICATION_EVENTS.PAYMENT_STATUS_CHANGED, {
|
||||
orderId,
|
||||
userId: order.userId,
|
||||
paymentStatus: 'paid',
|
||||
})
|
||||
if (updated.count > 0) {
|
||||
request.server.eventBus.emit(NOTIFICATION_EVENTS.PAYMENT_STATUS_CHANGED, {
|
||||
orderId,
|
||||
userId: order.userId,
|
||||
paymentStatus: 'paid',
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user