Skip to content

Commit

Permalink
initial fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-carlos-sousa committed Feb 8, 2025
1 parent 9de7e7f commit 4f2110f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions website/app/controllers/orders_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,29 @@ export default class OrdersController {
}

const successfulOrdersOfGivenProduct = await OrderProduct.query()
.join('orders', 'order_products.order_id', 'orders.id')
.where('order_products.product_id', productId)
.where('orders.status', 'Success')

const successfulOrdersOfGivenProductPerUser = await OrderProduct.query()
.join('orders', 'order_products.order_id', 'orders.id')
.where('orders.user_id', userId)
.where('order_products.product_id', productId)
.where('orders.status', 'Success')

const stockUsed = successfulOrdersOfGivenProduct.reduce(
(acc, orderProduct) => acc + orderProduct.quantity,
0
)



const totalQuantity = successfulOrdersOfGivenProduct.reduce(
const totalQuantity = successfulOrdersOfGivenProductPerUser.reduce(
(acc, orderProduct) => acc + orderProduct.quantity,
0
)

if (product.stock < quantity) {
if (product.stock < quantity + stockUsed) {
return response
.status(400)
.json({ message: `Não há mais stock do produto ${product.name}` })
Expand Down
2 changes: 1 addition & 1 deletion website/inertia/pages/payments/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default function TicketSalePage(
<OrderConfirmationModal
isOpen={orderConfirmationModalOpen}
onClose={() => {
router.visit('/tickets')
router.visit('/')
}}
/>
</CardContent>
Expand Down

0 comments on commit 4f2110f

Please sign in to comment.