import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { products, orders, generateOrderId, Order } from "../shared/data.js";
interface CreateOrderRequest {
productId: string;
quantity: number;
}
/**
* POST /api/orders - Create a new order
* Request body:
* - productId: ID of the product to order (required)
* - quantity: Number of items to order (required)
*/
export async function createOrder(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`createOrder function processed a request`);
let body: CreateOrderRequest;
try {
body = await request.json() as CreateOrderRequest;
} catch {
return {
status: 400,
jsonBody: {
success: false,
error: "Invalid JSON body"
}
};
}
const { productId, quantity } = body;
// Validate required fields
if (!productId || typeof productId !== "string") {
return {
status: 400,
jsonBody: {
success: false,
error: "productId is required"
}
};
}
if (!quantity || typeof quantity !== "number" || quantity < 1) {
return {
status: 400,
jsonBody: {
success: false,
error: "quantity must be a positive number"
}
};
}
// Find the product
const product = products.find(p => p.id === productId);
if (!product) {
return {
status: 404,
jsonBody: {
success: false,
error: `Product with ID '${productId}' not found`
}
};
}
// Check stock availability
if (product.stock < quantity) {
return {
status: 400,
jsonBody: {
success: false,
error: `Insufficient stock. Available: ${product.stock}, Requested: ${quantity}`
}
};
}
// Create the order
const order: Order = {
orderId: generateOrderId(),
productId: product.id,
productName: product.name,
quantity,
unitPrice: product.price,
totalPrice: Math.round(product.price * quantity * 100) / 100,
status: "created",
createdAt: new Date().toISOString()
};
// Update stock (in-memory)
product.stock -= quantity;
// Store the order
orders.push(order);
context.log(`Order created: ${order.orderId}`);
return {
status: 201,
jsonBody: {
success: true,
message: "Order created successfully",
order
}
};
}
/**
* GET /api/orders - List all orders
*/
export async function getOrders(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`getOrders function processed a request`);
return {
status: 200,
jsonBody: {
success: true,
count: orders.length,
orders
}
};
}
/**
* GET /api/orders/{orderId} - Get a specific order
*/
export async function getOrder(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
const orderId = request.params.orderId;
context.log(`getOrder function processed a request for order: ${orderId}`);
const order = orders.find(o => o.orderId === orderId);
if (!order) {
return {
status: 404,
jsonBody: {
success: false,
error: `Order with ID '${orderId}' not found`
}
};
}
return {
status: 200,
jsonBody: {
success: true,
order
}
};
}
app.http("createOrder", {
methods: ["POST"],
authLevel: "function",
route: "orders",
handler: createOrder
});
app.http("getOrders", {
methods: ["GET"],
authLevel: "function",
route: "orders",
handler: getOrders
});
app.http("getOrder", {
methods: ["GET"],
authLevel: "function",
route: "orders/{orderId}",
handler: getOrder
});