Prompt Details
Model
(claude-5-opus)
Token size
1,357
Example input
[What you want to build]: Build a REST API for an e-commerce application that allows customers to create orders, view their orders, and cancel an order before it is shipped.
[Existing code / repository / files, if any]: Existing repository: ecommerce-api/ βββ src/ β βββ controllers/ β βββ routes/ β βββ models/ β βββ app.js βββ tests/ βββ package.json βββ README.md
[Language / framework / database / APIs / deployment]: Language: JavaScript Runtime: Node.js Framework: Express.js Database: PostgreSQL ORM: Prisma Authentication: JWT Testing: Jest + Supertest
[Features and functionality]: Create an order Add multiple products to an order Validate product availability Calculate the total order amount Return the created order Allow customers to view their own orders Allow cancellation only when order status is PENDING
[Code / architecture / debugging / tests / full implementation]: Requirement analysis Confirmed / Assumed / Missing information Root-cause investigation Architecture impact Implementation plan Required file changes Corrected code Unit tests Integration tests Edge-case tests Security review Final validation report
Example output
π PHASE 1 β REQUIREMENT UNDERSTANDING
Goal
Fix incorrect order-total calculations while preserving the existing e-commerce architecture.
Users
Authenticated customers
Existing product-service users
Core Features
Create order
Calculate line-item totals
Calculate final order total
Retrieve orders
Cancel eligible orders
Inputs
Example request:
{
"items": [
{
"productId": "prod_101",
"quantity": 2
},
{
"productId": "prod_205",
"quantity": 1
}
]
}
Expected Output
{
"id": "order_123",
"status": "PENDING",
"total": 1300,
"items": [
{
"productId": "prod_101",
"quantity": 2,
"unitPrice": 500,
"lineTotal": 1000
},
{
"productId": "prod_205",
"quantity": 1,
"unitPrice": 300,
"lineTotal": 300
}
]
}
Business Rules
lineTotal = unitPrice Γ quantity
orderTotal = sum(all lineTotal values)
Quantity must be greater than 0.
Product must exist.
Product must be available.
Only authenticated users can create or access orders.
PENDING orders can be cancelled.
Shipped orders cannot be cancelled.
CONFIRMED
The application uses Node.js/Express.
PostgreSQL and Prisma are used.
JWT authentication exists.
Product APIs already exist.
Multiple products can belong to one order.
The reported bug concerns quantity-based calculation.
ASSUMED
Product price is retrieved from the database during order creation.
The order stores the price used at purchase time.
Prices are represented in the smallest currency unit or a database numeric type.
Existing order endpoints should remain backward compatible.
MISSING
Actual controller implementation
Prisma schema
Existing order model
Actual failing stack trace
Existing order tests
Exact database price type
Current order API response format
Conclusion: The exact root cause cannot yet be confirmed without inspecting the implementation.
---
ποΈ PHASE 2 β ARCHITECTURE
A minimal architecture would be:
HTTP Request
β
Authentication Middleware
β
Order Route
β
Order Controller
β
Order Service
β
Product Repository / Prisma
β
PostgreSQL
Why this structure?
Authentication remains separate from business logic.
Order calculation belongs in the order service.
Database access remains isolated.
Existing product functionality does not need to be rewritten.
---
π§© PHASE 3 β IMPLEMENTATION PLAN
Milestone 1: Investigate
Inspect order controller.
Inspect order service.
Inspect Prisma schema.
Locate total calculation.
Reproduce the reported failure.
Expected Result
Identify whether quantity is being ignored during line-total calculation.
Milestone 2: Fix Calculation
Task: Correct line-item calculation.
Purpose: Ensure every product price is multiplied by its requested quantity.
Expected logic:
lineTotal = unitPrice Γ quantity
Milestone 3: Add Tests
Test:
One product, quantity 1
One product, quantity > 1
Multiple products
Quantity 0
Negative quantity
Missing product
Unavailable product
Decimal prices
Very large quantity
Milestone 4: Regression Validation
Verify existing:
Product APIs
Authentication
Order retrieval
Order cancellation
remain unaffected.
---
π» PHASE 4 β CODE IMPLEMENTATION
Suppose investigation discovers this existing logic:
const total = items.reduce((sum, item) => {
return sum + item.product.price;
}, 0);
Confirmed Root Cause
The calculation adds only the product price:
price
instead of:
price Γ quantity
For:
βΉ500 Γ 2
the current implementation contributes:
βΉ500
instead of:
βΉ1000
Corrected Logic
const total = items.reduce((sum, item) => {
return sum + item.product.price * item.quantity;
}, 0);
For:
βΉ500 Γ 2
βΉ300 Γ 1
the calculation becomes:
500 Γ 2 = 1000
300 Γ 1 = 300
Total = 1300
Important: This code should only be applied after confirming that item.quantity is actually the intended quantity field in the existing codebase.
---
π PHASE 5 β DEBUGGING TRACE
SYMPTOM
Order total is lower than expected when products have quantities greater than 1.
β
FAILURE POINT
Order-total calculation.
β
ROOT CAUSE
The implementation sums product prices but ignores item quantity.
β
FIX
Multiply each product's unit price by its quantity before adding it to the order total.
β
REGRESSION RISK
Potential risk if:
Existing code already calculates line totals elsewhere.
Prices use floating-point numbers.
Quantity validation is missing.
Database values are strings/Decimals rather than JavaScript numbers.
These areas must be checked before finalizing the fix.
---
π§ͺ PHASE 6 β TESTING
Test 1: Single Product
Input
{
"items": [
{
"productId": "prod_1",
"quantity": 1
}
]
}
Product price:
βΉ500
Expected
Total = βΉ500
Pass Criteria
API returns HTTP 201 and total 500.
---
Test 2: Quantity Greater Than One
Input
{
"items": [
{
"productId": "prod_1",
"quantity": 3
}
]
}
Product price:
βΉ500
Expected
500 Γ 3 = βΉ1500
Pass Criteria
Returned total equals 1500.
---
Test 3: Multiple Products
Input
{
"items": [
{
"productId": "prod_1",
"quantity": 2
},
{
"productId": "prod_2",
"quantity": 1
}
]
}
Prices:
prod_1 = βΉ500
prod_2 = βΉ300
Expected
500 Γ 2 + 300 Γ 1
= 1000 + 300
= βΉ1300
Pass Criteria
Returned total equals 1300.
---
Test 4: Zero Quantity
Input
{
"items": [
{
"productId": "prod_1",
"quantity": 0
}
]
}
Expected
HTTP 400 Bad Request
Pass Criteria
Order is not created.
---
Test 5: Negative Quantity
{
"items": [
{
"productId": "prod_1",
"quantity": -5
}
]
}
Expected
HTTP 400 Bad Request
Pass Criteria
Negative quantities are rejected.
---
Test 6: Invalid Product
{
"items": [
{
"productId": "does_not_exist",
"quantity": 1
}
]
}
Expected
HTTP 404 Not Found
Pass Criteria
No order is created.
---
Test 7: Unauthenticated Request
Send the order request without JWT.
Expected
HTTP 401 Unauthorized
Pass Criteria
Order creation is rejected.
---
π SECURITY REVIEW
Check that:
Product prices are taken from the trusted database, not from client input.
Client cannot submit:
{
"price": 1
}
to purchase a βΉ500 product for βΉ1.
Quantity is validated server-side.
Users cannot access another customer's orders.
JWT validation remains enforced.
Database queries are parameterized through Prisma.
No credentials are hardcoded.
Critical security/business rule:
The client should send:
productId
quantity
but not be trusted for the final product price.
---
π PHASE 7 β CODE REVIEW
Correctness
The calculation should multiply unit price by quantity.
Maintainability
Keep the calculation in the existing order-service/business-logic layer rather than duplicating it across controllers.
Performance
Avoid unnecessary product queries inside a loop if the existing architecture can retrieve all required products in one query.
Error Handling
Invalid quantities and unavailable products should fail before creating the order.
Backward Compatibility
Existing API contracts should remain unchanged unless the bug fix requires a documented response change.
---
π FINAL VALIDATION REPORT
FINAL CHANGES
Correct order line-total calculation.
Validate quantity.
Add regression tests.
Ensure server-side product pricing.
Preserve existing authentication and product APIs.
KNOWN LIMITATIONS
Actual files cannot be safely modified until the real repository/code is provided.
Exact database Decimal/number handling needs verification.
Existing API response structure needs confirmation.
REMAINING RISKS
Floating-point currency calculations.
Concurrent inventory changes.
Database transaction failures.
Race conditions during stock validation.
TEST RESULTS
Not executed.
No claim of passing tests can be made because the actual project has not been run.
NEXT STEPS
1. Inspect the actual order controller/service.
2. Inspect the Prisma schema.
3. Reproduce the reported βΉ800 result.
4. Confirm the exact failure point.
5. Apply the smallest safe fix.
6. Run unit and integration tests.
7. Perform regression testing.
8. Review the final diff.
9. Only then determine whether the implementation is production-ready.
π― Expected transformation
IDEA
β
E-Commerce Order API
β
REQUIREMENTS
β
Multi-product ordering + quantity calculation
β
ARCHITECTURE
β
Route β Controller β Service β Prisma β PostgreSQL
β
BUG
β
Quantity ignored during total calculation
β
FIX
β
unitPrice Γ quantity
β
TEST
β
Unit + Integration + Edge Cases
β
VALIDATION
β
Production readiness only after actual execution evidence
By purchasing this prompt, you agree to our terms of service
CLAUDE-5-OPUS
This prompt helps AI turn ideas into structured coding tasks, generate implementation plans, write and improve code, debug failures, and validate the final project without unnecessary complexity.
π‘ Convert ideas into clear coding requirements
ποΈ Design practical project architecture
π» Generate implementation-ready code
π Debug errors & improve existing code
π§ͺ Create tests & validation checks
π Review code quality & maintainability
π Guide the project from idea to working build
π Turn you
...more
Updated 2 weeks ago
