Assist_Design/apps/bff/src/orders/services/order-fulfillment.service.ts
T. Narantuya 98f998db51 Refactor code for improved readability and maintainability
- Simplified import statements in auth.controller.ts and consolidated DTO imports.
- Streamlined accountStatus method in AuthController for better clarity.
- Refactored error handling in AuthService for existing mapping checks and password validation.
- Cleaned up whitespace and formatting across various files for consistency.
- Enhanced logging configuration in logging.module.ts to reduce noise and improve clarity.
- Updated frontend components for better formatting and readability in ProfilePage and SignupPage.
2025-09-02 16:09:17 +09:00

97 lines
3.1 KiB
TypeScript

import { Injectable, Inject } from "@nestjs/common";
import { Logger } from "nestjs-pino";
import { OrderFulfillmentOrchestrator } from "./order-fulfillment-orchestrator.service";
import { OrderFulfillmentValidator } from "./order-fulfillment-validator.service";
import { OrderFulfillmentErrorService } from "./order-fulfillment-error.service";
export interface OrderFulfillmentRequest {
orderId: string;
timestamp: string;
nonce: string;
}
export interface OrderFulfillmentResult {
success: boolean;
status: "Already Fulfilled" | "Fulfilled" | "Failed";
whmcsOrderId?: string;
whmcsServiceIds?: number[];
message: string;
errorCode?: string;
}
/**
* Main order fulfillment service - coordinates modular fulfillment components
* Uses clean architecture similar to order creation workflow
*/
@Injectable()
export class OrderFulfillmentService {
constructor(
private readonly orderFulfillmentOrchestrator: OrderFulfillmentOrchestrator,
private readonly orderFulfillmentValidator: OrderFulfillmentValidator,
private readonly orderFulfillmentErrorService: OrderFulfillmentErrorService,
@Inject(Logger) private readonly logger: Logger
) {}
/**
* Main fulfillment method called by Salesforce webhook
* Uses modular architecture for clean separation of concerns
*/
async fulfillOrder(
sfOrderId: string,
request: OrderFulfillmentRequest,
idempotencyKey: string
): Promise<OrderFulfillmentResult> {
this.logger.log("Starting order fulfillment workflow", {
sfOrderId,
idempotencyKey,
timestamp: request.timestamp,
});
try {
// 1. Validate request payload format
const validatedPayload = this.orderFulfillmentValidator.validateRequestPayload(request);
// 2. Execute complete fulfillment workflow using orchestrator
const context = await this.orderFulfillmentOrchestrator.executeFulfillment(
sfOrderId,
validatedPayload,
idempotencyKey
);
// 3. Generate result summary from context
const summary = this.orderFulfillmentOrchestrator.getFulfillmentSummary(context);
this.logger.log("Order fulfillment workflow completed", {
sfOrderId,
success: summary.success,
status: summary.status,
whmcsOrderId: summary.whmcsOrderId,
serviceCount: summary.whmcsServiceIds?.length || 0,
completedSteps: summary.steps.filter(s => s.status === "completed").length,
totalSteps: summary.steps.length,
});
return {
success: summary.success,
status: summary.status,
whmcsOrderId: summary.whmcsOrderId,
whmcsServiceIds: summary.whmcsServiceIds,
message: summary.message,
errorCode: summary.success ? undefined : this.orderFulfillmentErrorService.determineErrorCode(summary.message),
};
} catch (error) {
this.logger.error("Order fulfillment workflow failed", {
sfOrderId,
idempotencyKey,
error: error instanceof Error ? error.message : String(error),
});
// Use centralized error handling service
return this.orderFulfillmentErrorService.createErrorResponse(error);
}
}
}