2025-08-28 16:57:57 +09:00
|
|
|
import { Body, Controller, Get, Param, Post, Request } from "@nestjs/common";
|
2025-08-27 20:01:46 +09:00
|
|
|
import { OrderOrchestrator } from "./services/order-orchestrator.service";
|
2025-08-23 17:24:37 +09:00
|
|
|
import { ApiBearerAuth, ApiOperation, ApiParam, ApiResponse, ApiTags } from "@nestjs/swagger";
|
|
|
|
|
import { RequestWithUser } from "../auth/auth.types";
|
2025-08-27 10:54:05 +09:00
|
|
|
import { Logger } from "nestjs-pino";
|
2025-08-29 13:26:57 +09:00
|
|
|
import { CreateOrderDto } from "./dto/order.dto";
|
2025-08-20 18:02:50 +09:00
|
|
|
|
2025-08-21 15:24:40 +09:00
|
|
|
@ApiTags("orders")
|
|
|
|
|
@Controller("orders")
|
2025-08-20 18:02:50 +09:00
|
|
|
export class OrdersController {
|
2025-08-27 10:54:05 +09:00
|
|
|
constructor(
|
2025-08-27 20:01:46 +09:00
|
|
|
private orderOrchestrator: OrderOrchestrator,
|
2025-08-27 10:54:05 +09:00
|
|
|
private readonly logger: Logger
|
|
|
|
|
) {}
|
2025-08-20 18:02:50 +09:00
|
|
|
|
2025-08-23 17:24:37 +09:00
|
|
|
@ApiBearerAuth()
|
|
|
|
|
@Post()
|
2025-08-28 16:57:57 +09:00
|
|
|
@ApiOperation({ summary: "Create Salesforce Order" })
|
|
|
|
|
@ApiResponse({ status: 201, description: "Order created successfully" })
|
|
|
|
|
@ApiResponse({ status: 400, description: "Invalid request data" })
|
2025-08-29 13:26:57 +09:00
|
|
|
async create(@Request() req: RequestWithUser, @Body() body: CreateOrderDto) {
|
2025-08-28 16:57:57 +09:00
|
|
|
this.logger.log(
|
|
|
|
|
{
|
|
|
|
|
userId: req.user?.id,
|
2025-08-29 13:26:57 +09:00
|
|
|
orderType: body.orderType,
|
|
|
|
|
skuCount: body.skus?.length || 0,
|
2025-08-28 16:57:57 +09:00
|
|
|
},
|
|
|
|
|
"Order creation request received"
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-27 20:01:46 +09:00
|
|
|
try {
|
|
|
|
|
return await this.orderOrchestrator.createOrder(req.user.id, body);
|
|
|
|
|
} catch (error) {
|
2025-08-28 16:57:57 +09:00
|
|
|
this.logger.error(
|
|
|
|
|
{
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
userId: req.user?.id,
|
2025-08-29 13:26:57 +09:00
|
|
|
orderType: body.orderType,
|
2025-08-28 16:57:57 +09:00
|
|
|
},
|
|
|
|
|
"Order creation failed"
|
|
|
|
|
);
|
2025-08-27 20:01:46 +09:00
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiBearerAuth()
|
|
|
|
|
@Get("user")
|
|
|
|
|
@ApiOperation({ summary: "Get user's orders" })
|
|
|
|
|
async getUserOrders(@Request() req: RequestWithUser) {
|
|
|
|
|
return this.orderOrchestrator.getOrdersForUser(req.user.id);
|
2025-08-23 17:24:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiBearerAuth()
|
|
|
|
|
@Get(":sfOrderId")
|
|
|
|
|
@ApiOperation({ summary: "Get order summary/status" })
|
|
|
|
|
@ApiParam({ name: "sfOrderId", type: String })
|
|
|
|
|
async get(@Request() req: RequestWithUser, @Param("sfOrderId") sfOrderId: string) {
|
2025-08-27 20:01:46 +09:00
|
|
|
return this.orderOrchestrator.getOrder(sfOrderId);
|
2025-08-23 17:24:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiBearerAuth()
|
|
|
|
|
@Post(":sfOrderId/provision")
|
|
|
|
|
@ApiOperation({ summary: "Trigger provisioning for an approved order" })
|
|
|
|
|
@ApiParam({ name: "sfOrderId", type: String })
|
|
|
|
|
async provision(@Request() req: RequestWithUser, @Param("sfOrderId") sfOrderId: string) {
|
2025-08-27 20:01:46 +09:00
|
|
|
return this.orderOrchestrator.provisionOrder(req.user.id, sfOrderId);
|
2025-08-23 17:24:37 +09:00
|
|
|
}
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|