Skip to content

Commit 9faf265

Browse files
committed
created a comprehensive and advanced validation system for order data on the server side
1 parent 714669d commit 9faf265

12 files changed

Lines changed: 1862 additions & 221 deletions

app/checkout/page.tsx

Lines changed: 271 additions & 97 deletions
Large diffs are not rendered by default.

server/controllers/customer_order_product.js

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,57 @@ const prisma = new PrismaClient();
44
async function createOrderProduct(request, response) {
55
try {
66
const { customerOrderId, productId, quantity } = request.body;
7-
const corder = await prisma.customer_order_product.create({
7+
8+
// Validate required fields
9+
if (!customerOrderId) {
10+
return response.status(400).json({ error: "Customer order ID is required" });
11+
}
12+
if (!productId) {
13+
return response.status(400).json({ error: "Product ID is required" });
14+
}
15+
if (!quantity || quantity <= 0) {
16+
return response.status(400).json({ error: "Valid quantity is required" });
17+
}
18+
19+
// Verify that the customer order exists
20+
const existingOrder = await prisma.customer_order.findUnique({
21+
where: { id: customerOrderId }
22+
});
23+
24+
if (!existingOrder) {
25+
return response.status(404).json({ error: "Customer order not found" });
26+
}
27+
28+
// Verify that the product exists
29+
const existingProduct = await prisma.product.findUnique({
30+
where: { id: productId }
31+
});
32+
33+
if (!existingProduct) {
34+
return response.status(404).json({ error: "Product not found" });
35+
}
36+
37+
// Create the order product
38+
const orderProduct = await prisma.customer_order_product.create({
839
data: {
9-
customerOrderId,
10-
productId,
11-
quantity
40+
customerOrderId: customerOrderId,
41+
productId: productId,
42+
quantity: parseInt(quantity)
1243
}
1344
});
14-
return response.status(201).json(corder);
45+
46+
return response.status(201).json(orderProduct);
1547
} catch (error) {
16-
console.error("Error creating prodcut order:", error);
48+
console.error("Error creating product order:", error);
1749
return response.status(500).json({ error: "Error creating product order" });
1850
}
1951
}
2052

21-
2253
async function updateProductOrder(request, response) {
2354
try {
2455
const { id } = request.params;
2556
const { customerOrderId, productId, quantity } = request.body;
2657

27-
2858
const existingOrder = await prisma.customer_order_product.findUnique({
2959
where: {
3060
id: id
@@ -35,7 +65,6 @@ async function updateProductOrder(request, response) {
3565
return response.status(404).json({ error: "Order not found" });
3666
}
3767

38-
3968
const updatedOrder = await prisma.customer_order_product.update({
4069
where: {
4170
id: existingOrder.id
@@ -72,10 +101,10 @@ async function getProductOrder(request, response) {
72101
const { id } = request.params;
73102
const order = await prisma.customer_order_product.findMany({
74103
where: {
75-
customerOrderId: id // Use customerOrderId for searching
104+
customerOrderId: id
76105
},
77106
include: {
78-
product: true // Including information about a product for response
107+
product: true
79108
}
80109
});
81110
if (!order) {
@@ -84,10 +113,8 @@ async function getProductOrder(request, response) {
84113
return response.status(200).json(order);
85114
}
86115

87-
88116
async function getAllProductOrders(request, response) {
89117
try {
90-
// Getting all orders from customer_order_product table
91118
const productOrders = await prisma.customer_order_product.findMany({
92119
select: {
93120
productId: true,
@@ -111,15 +138,12 @@ async function getAllProductOrders(request, response) {
111138
}
112139
});
113140

114-
// Creating map for storing data about orders grouped by customerOrderId
115141
const ordersMap = new Map();
116142

117-
// Iterating through all orders
118143
for (const order of productOrders) {
119144
const { customerOrder, productId, quantity } = order;
120145
const { id, ...orderDetails } = customerOrder;
121146

122-
// Finding a product from the table Product with productId
123147
const product = await prisma.product.findUnique({
124148
where: {
125149
id: productId
@@ -134,10 +158,8 @@ async function getAllProductOrders(request, response) {
134158
});
135159

136160
if (ordersMap.has(id)) {
137-
// If there is input for customerOrderId, add a new product in existing map array
138161
ordersMap.get(id).products.push({ ...product, quantity });
139162
} else {
140-
// Otherwise create new input for customerOrderId and add the product
141163
ordersMap.set(id, {
142164
customerOrderId: id,
143165
customerOrder: orderDetails,
@@ -146,7 +168,6 @@ async function getAllProductOrders(request, response) {
146168
}
147169
}
148170

149-
// Converting map to object array
150171
const groupedOrders = Array.from(ordersMap.values());
151172

152173
return response.json(groupedOrders);
@@ -156,6 +177,10 @@ async function getAllProductOrders(request, response) {
156177
}
157178
}
158179

159-
160-
161-
module.exports = { createOrderProduct, updateProductOrder, deleteProductOrder, getProductOrder,getAllProductOrders};
180+
module.exports = {
181+
createOrderProduct,
182+
updateProductOrder,
183+
deleteProductOrder,
184+
getProductOrder,
185+
getAllProductOrders
186+
};

0 commit comments

Comments
 (0)