Files
bakery-ia/frontend/src/api/services/demo.ts

205 lines
5.8 KiB
TypeScript
Raw Normal View History

2025-10-06 15:27:01 +02:00
// ================================================================
// frontend/src/api/services/demo.ts
// ================================================================
2025-10-03 14:09:34 +02:00
/**
2025-10-06 15:27:01 +02:00
* Demo Session Service - Complete backend alignment
*
* Backend API structure (3-tier architecture):
* - ATOMIC: demo_accounts.py, demo_sessions.py
* - OPERATIONS: demo_operations.py
*
* Note: Demo service does NOT use tenant prefix
*
2025-10-06 15:27:01 +02:00
* Last Updated: 2025-10-05
* Status: Complete - Zero drift with backend
2025-10-03 14:09:34 +02:00
*/
import { apiClient } from '../client';
import type { DemoSessionResponse } from '../types/demo';
2025-10-03 14:09:34 +02:00
export interface DemoAccount {
account_type: string;
email: string;
name: string;
password: string;
description?: string;
features?: string[];
business_model?: string;
}
// Use the complete type from types/demo.ts which matches backend response
export type DemoSession = DemoSessionResponse;
2025-10-03 14:09:34 +02:00
export interface CreateSessionRequest {
demo_account_type: 'individual_bakery' | 'central_baker';
}
export interface ExtendSessionRequest {
session_id: string;
}
export interface DestroySessionRequest {
session_id: string;
}
export interface ServiceProgress {
status: 'not_started' | 'in_progress' | 'completed' | 'failed';
records_cloned: number;
error?: string;
}
export interface SessionStatusResponse {
2025-10-03 14:09:34 +02:00
session_id: string;
status: 'pending' | 'ready' | 'partial' | 'failed' | 'active' | 'expired' | 'destroyed';
total_records_cloned: number;
progress?: Record<string, ServiceProgress>;
errors?: Array<{ service: string; error_message: string }>;
2025-10-03 14:09:34 +02:00
}
// ===================================================================
// OPERATIONS: Demo Session Status and Cloning
// ===================================================================
/**
* Get session status
* GET /demo/sessions/{session_id}/status
*/
export const getSessionStatus = async (sessionId: string): Promise<SessionStatusResponse> => {
return await apiClient.get<SessionStatusResponse>(`/demo/sessions/${sessionId}/status`);
};
/**
* Retry data cloning for a session
* POST /demo/sessions/{session_id}/retry
*/
export const retryCloning = async (sessionId: string): Promise<SessionStatusResponse> => {
return await apiClient.post<SessionStatusResponse>(`/demo/sessions/${sessionId}/retry`, {});
};
2025-10-06 15:27:01 +02:00
// ===================================================================
// ATOMIC: Demo Accounts
// Backend: services/demo_session/app/api/demo_accounts.py
// ===================================================================
2025-10-03 14:09:34 +02:00
/**
* Get available demo accounts
2025-10-06 15:27:01 +02:00
* GET /demo/accounts
2025-10-03 14:09:34 +02:00
*/
export const getDemoAccounts = async (): Promise<DemoAccount[]> => {
return await apiClient.get<DemoAccount[]>('/demo/accounts');
};
2025-10-06 15:27:01 +02:00
// ===================================================================
// ATOMIC: Demo Sessions
// Backend: services/demo_session/app/api/demo_sessions.py
// ===================================================================
2025-10-03 14:09:34 +02:00
/**
* Create a new demo session
2025-10-06 15:27:01 +02:00
* POST /demo/sessions
2025-10-03 14:09:34 +02:00
*/
export const createDemoSession = async (
request: CreateSessionRequest
): Promise<DemoSession> => {
2025-10-06 15:27:01 +02:00
return await apiClient.post<DemoSession>('/demo/sessions', request);
};
/**
* Get demo session details
* GET /demo/sessions/{session_id}
*/
export const getDemoSession = async (sessionId: string): Promise<any> => {
return await apiClient.get(`/demo/sessions/${sessionId}`);
2025-10-03 14:09:34 +02:00
};
2025-10-06 15:27:01 +02:00
// ===================================================================
// OPERATIONS: Demo Session Management
// Backend: services/demo_session/app/api/demo_operations.py
// ===================================================================
2025-10-03 14:09:34 +02:00
/**
* Extend an existing demo session
2025-10-06 15:27:01 +02:00
* POST /demo/sessions/{session_id}/extend
2025-10-03 14:09:34 +02:00
*/
export const extendDemoSession = async (
request: ExtendSessionRequest
): Promise<DemoSession> => {
2025-10-06 15:27:01 +02:00
return await apiClient.post<DemoSession>(
`/demo/sessions/${request.session_id}/extend`,
{}
);
2025-10-03 14:09:34 +02:00
};
/**
* Destroy a demo session
2025-10-06 15:27:01 +02:00
* Note: This might be a DELETE endpoint - verify backend implementation
2025-10-03 14:09:34 +02:00
*/
export const destroyDemoSession = async (
request: DestroySessionRequest
): Promise<{ message: string }> => {
return await apiClient.post<{ message: string }>(
2025-10-06 15:27:01 +02:00
`/demo/sessions/${request.session_id}/destroy`,
{}
2025-10-03 14:09:34 +02:00
);
};
/**
* Get demo session statistics
2025-10-06 15:27:01 +02:00
* GET /demo/stats
2025-10-03 14:09:34 +02:00
*/
export const getDemoStats = async (): Promise<any> => {
return await apiClient.get('/demo/stats');
};
2025-10-06 15:27:01 +02:00
/**
* Cleanup expired demo sessions (Admin/Operations)
* POST /demo/operations/cleanup
*/
export const cleanupExpiredSessions = async (): Promise<any> => {
return await apiClient.post('/demo/operations/cleanup', {});
};
// ===================================================================
// API Service Class
// ===================================================================
export class DemoSessionAPI {
async getDemoAccounts(): Promise<DemoAccount[]> {
return getDemoAccounts();
}
async createDemoSession(request: CreateSessionRequest): Promise<DemoSession> {
return createDemoSession(request);
}
async getDemoSession(sessionId: string): Promise<any> {
return getDemoSession(sessionId);
}
async extendDemoSession(request: ExtendSessionRequest): Promise<DemoSession> {
return extendDemoSession(request);
}
async destroyDemoSession(request: DestroySessionRequest): Promise<{ message: string }> {
return destroyDemoSession(request);
}
async getDemoStats(): Promise<any> {
return getDemoStats();
}
async cleanupExpiredSessions(): Promise<any> {
return cleanupExpiredSessions();
}
async getSessionStatus(sessionId: string): Promise<SessionStatusResponse> {
return getSessionStatus(sessionId);
}
async retryCloning(sessionId: string): Promise<SessionStatusResponse> {
return retryCloning(sessionId);
}
}
export const demoSessionAPI = new DemoSessionAPI();