2025-10-22 11:33:23 +09:00
|
|
|
import { Body, Controller, Post, Request, UsePipes, Inject } from "@nestjs/common";
|
|
|
|
|
import { Logger } from "nestjs-pino";
|
|
|
|
|
import { ZodValidationPipe } from "@bff/core/validation";
|
|
|
|
|
import { CheckoutService } from "../services/checkout.service";
|
|
|
|
|
import {
|
|
|
|
|
CheckoutCart,
|
|
|
|
|
checkoutCartSchema,
|
2025-10-27 15:47:50 +09:00
|
|
|
checkoutBuildCartRequestSchema,
|
|
|
|
|
checkoutBuildCartResponseSchema,
|
|
|
|
|
type CheckoutBuildCartRequest,
|
2025-10-22 11:33:23 +09:00
|
|
|
} from "@customer-portal/domain/orders";
|
|
|
|
|
import { apiSuccessResponseSchema } from "@customer-portal/domain/common";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import type { RequestWithUser } from "@bff/modules/auth/auth.types";
|
|
|
|
|
|
|
|
|
|
const validateCartResponseSchema = apiSuccessResponseSchema(z.object({ valid: z.boolean() }));
|
|
|
|
|
|
|
|
|
|
@Controller("checkout")
|
|
|
|
|
export class CheckoutController {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly checkoutService: CheckoutService,
|
|
|
|
|
@Inject(Logger) private readonly logger: Logger
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
@Post("cart")
|
2025-10-27 15:47:50 +09:00
|
|
|
@UsePipes(new ZodValidationPipe(checkoutBuildCartRequestSchema))
|
2025-10-29 13:29:28 +09:00
|
|
|
async buildCart(@Request() req: RequestWithUser, @Body() body: CheckoutBuildCartRequest) {
|
2025-10-22 11:33:23 +09:00
|
|
|
this.logger.log("Building checkout cart", {
|
|
|
|
|
userId: req.user?.id,
|
|
|
|
|
orderType: body.orderType,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const cart = await this.checkoutService.buildCart(
|
|
|
|
|
body.orderType,
|
|
|
|
|
body.selections,
|
2025-10-27 15:47:50 +09:00
|
|
|
body.configuration
|
2025-10-22 11:33:23 +09:00
|
|
|
);
|
|
|
|
|
|
2025-10-27 15:47:50 +09:00
|
|
|
return checkoutBuildCartResponseSchema.parse({
|
2025-10-22 11:33:23 +09:00
|
|
|
success: true,
|
|
|
|
|
data: cart,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error("Failed to build checkout cart", {
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
userId: req.user?.id,
|
|
|
|
|
orderType: body.orderType,
|
|
|
|
|
});
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("validate")
|
|
|
|
|
@UsePipes(new ZodValidationPipe(checkoutCartSchema))
|
|
|
|
|
validateCart(@Body() cart: CheckoutCart) {
|
|
|
|
|
this.logger.log("Validating checkout cart", {
|
|
|
|
|
itemCount: cart.items.length,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
this.checkoutService.validateCart(cart);
|
|
|
|
|
|
|
|
|
|
return validateCartResponseSchema.parse({
|
|
|
|
|
success: true,
|
|
|
|
|
data: { valid: true },
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error("Checkout cart validation failed", {
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
itemCount: cart.items.length,
|
|
|
|
|
});
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|