Dynamic AI Pricing
Relevant source files
The following files were used as context for generating this wiki page:
- backend/src/application/use-cases/ai/DynamicPricingUseCase.ts
- backend/src/infrastructure/services/AtlasAIService.ts
- backend/src/interfaces/controllers/AIController.ts
- backend/src/application/use-cases/booking/CreateBookingUseCase.ts
- backend/src/infrastructure/database/prisma/schema.prisma
- backend/src/app.ts
Dynamic AI Pricing calculates service costs based on real-time factors including service type, duration, location, and demand. The system integrates with the AtlasAIService to provide optimized pricing for bookings, ensuring rates reflect current market conditions and resource availability.
The system primary serves the CreateBookingUseCase by generating accurate price estimates before a booking is finalized. It also provides a public API endpoint through the AIController for real-time pricing queries.
Sources: backend/src/application/use-cases/ai/DynamicPricingUseCase.ts, backend/src/application/use-cases/booking/CreateBookingUseCase.ts:33-40
Architecture and Data Flow
Dynamic pricing logic resides within the DynamicPricingUseCase. It coordinates data between the application layer and the AI infrastructure service.
Execution Flow
- The
AIControllerorCreateBookingUseCaseinvokes the pricing logic. - The
DynamicPricingUseCasecalculates initial base rates and duration-based costs. - The system sends these variables to the
AtlasAIService. - The
AtlasAIServiceattempts to contact an external AI API for optimized pricing. - If the external service is unavailable, the system executes local heuristic fallbacks.
The diagram shows the request path from external interfaces through the use case to the AI service layer.
Sources: backend/src/application/use-cases/ai/DynamicPricingUseCase.ts:16-36, backend/src/infrastructure/services/AtlasAIService.ts:109-138
Core Components
DynamicPricingUseCase
This class calculates the preliminary costs and applies initial multipliers. It maps service types to specific base rates and identifies weekend surges.
| Service Type | Base Rate (USD) |
|---|---|
| standard | 80 |
| deep | 140 |
| moveout | 200 |
| commercial | 250 |
| default | 100 |
Sources: backend/src/application/use-cases/ai/DynamicPricingUseCase.ts:18-24
Pricing Multipliers
The system applies several factors to the base cost:
- Duration: Calculated as
hours = minutes / 60, with a minimum of 1 hour. - Extras: Each extra service item adds 15 USD.
- Weekend: A 1.15x multiplier applies if the start date falls on a weekend.
- Rush Hour: A 1.12x multiplier applies for starts during 07:00-08:59 or 17:00-19:59.
- Commercial Boost: Commercial services receive a 1.25x multiplier in fallback mode.
Sources: backend/src/application/use-cases/ai/DynamicPricingUseCase.ts:25-28, backend/src/infrastructure/services/AtlasAIService.ts:122-123
API Implementation
The pricing system is exposed via the /api/v1/ai/pricing/dynamic endpoint.
Pricing Request Structure
interface CalculateDynamicPriceInput {
baseRate: number;
hours: number;
extrasCost: number;
serviceType: string;
postalCode: string;
scheduledStart: Date;
demandMultiplier: number;
}
Sources: backend/src/infrastructure/services/AtlasAIService.ts:32-40
Controller Integration
The AIController validates required fields before executing the pricing logic.
dynamicPricing = async (request: Request, response: Response, next: NextFunction): Promise<void> => {
try {
const { address, serviceType, scheduledStart, scheduledEnd, extras = [] } = request.body;
if (!address || !serviceType || !scheduledStart || !scheduledEnd) {
throw new ValidationError('address, serviceType, scheduledStart, and scheduledEnd are required');
}
const pricing = await this.dynamicPricingUseCase.execute({
address,
serviceType,
scheduledStart: new Date(scheduledStart),
scheduledEnd: new Date(scheduledEnd),
extras,
});
response.status(200).json({ success: true, data: pricing });
} catch (error) {
next(error);
}
};
Sources: backend/src/interfaces/controllers/AIController.ts:31-50
Data Persistence
Pricing results and factors are stored within the Booking model in the aiMetadata JSON field. This allows for historical auditing of how a specific price was reached.
| Field | Type | Description |
|---|---|---|
| amount | Decimal(10,2) | Final calculated price. |
| currency | String | Currency code (defaults to USD). |
| aiMetadata | Json | Stores pricingFactors such as demandMultiplier and rushHour. |
Sources: backend/src/infrastructure/database/prisma/schema.prisma:103-105, backend/src/infrastructure/services/AtlasAIService.ts:130-137
Summary
Dynamic AI Pricing provides a flexible billing mechanism that adjusts to service complexity and demand. By combining fixed base rates with dynamic AI-driven multipliers and local fallbacks, the system maintains consistent pricing operations even during external service interruptions.