Use this file to discover all available pages before exploring further.
The Sales API powers the checkout flow and order history for AutoPart Pro. Both endpoints require authentication. GET /api/sales is role-aware: admin users receive every order ever placed across all customers, while users with the client role see only their own orders. POST /api/sales executes a full checkout inside a MySQL transaction — it inserts the sale record, creates individual line items, and decrements inventory stock atomically. If any step fails, the entire transaction is rolled back so inventory counts never fall out of sync with recorded sales.
Retrieves order history. The response is scoped automatically by the authenticated user’s role: admins receive all sales with customer identity fields (user_name, user_email), while clients receive only their own orders without those fields.
Each sale includes a nested items array built server-side using JSON_ARRAYAGG. The array always contains at least one entry because a sale cannot be created with an empty cart.
Processes a checkout for the authenticated user. The server computes the order total by summing quantity × unit_price for every item in the request — the client-supplied unit_price values are the source of truth and should match the current product prices fetched from GET /api/products. The entire operation runs inside a MySQL transaction: if inserting any line item or decrementing any product’s stock fails, the whole transaction is rolled back and no data is persisted.
Stock is decremented atomically during the transaction using UPDATE products SET stock = stock - ? WHERE id = ?. There is no inventory reservation step — concurrent purchases of the same product can result in negative stock if you do not enforce a check constraint at the database level.