import { Body, Controller, Get, Param, Post, Request } from "@nestjs/common"; import { OrderOrchestrator } from "./services/order-orchestrator.service"; import { ApiBearerAuth, ApiOperation, ApiParam, ApiResponse, ApiTags } from "@nestjs/swagger"; import type { RequestWithUser } from "../auth/auth.types"; import { Logger } from "nestjs-pino"; import * as OrderDto from "./dto/order.dto"; @ApiTags("orders") @Controller("orders") export class OrdersController { constructor( private orderOrchestrator: OrderOrchestrator, private readonly logger: Logger ) {} @ApiBearerAuth() @Post() @ApiOperation({ summary: "Create Salesforce Order" }) @ApiResponse({ status: 201, description: "Order created successfully" }) @ApiResponse({ status: 400, description: "Invalid request data" }) async create(@Request() req: RequestWithUser, @Body() body: OrderDto.CreateOrderDto) { this.logger.log( { userId: req.user?.id, orderType: body.orderType, skuCount: body.skus?.length || 0, }, "Order creation request received" ); try { return await this.orderOrchestrator.createOrder(req.user.id, body); } catch (error) { this.logger.error( { error: error instanceof Error ? error.message : String(error), userId: req.user?.id, orderType: body.orderType, }, "Order creation failed" ); throw error; } } @ApiBearerAuth() @Get("user") @ApiOperation({ summary: "Get user's orders" }) async getUserOrders(@Request() req: RequestWithUser) { return this.orderOrchestrator.getOrdersForUser(req.user.id); } @ApiBearerAuth() @Get(":sfOrderId") @ApiOperation({ summary: "Get order summary/status" }) @ApiParam({ name: "sfOrderId", type: String }) async get(@Request() req: RequestWithUser, @Param("sfOrderId") sfOrderId: string) { return this.orderOrchestrator.getOrder(sfOrderId); } @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) { return this.orderOrchestrator.provisionOrder(req.user.id, sfOrderId); } }