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

360 lines
13 KiB
TypeScript
Raw Normal View History

2025-09-23 12:49:35 +02:00
/**
* Production API Service - Handles all production-related API calls
*/
import { apiClient } from '../client/apiClient';
import {
// Types
ProductionBatchResponse,
2025-09-10 08:00:50 +02:00
ProductionBatchCreate,
2025-09-23 12:49:35 +02:00
ProductionBatchUpdate,
2025-09-10 08:00:50 +02:00
ProductionBatchStatusUpdate,
ProductionBatchListResponse,
2025-09-23 12:49:35 +02:00
ProductionBatchFilters,
ProductionScheduleResponse,
ProductionScheduleCreate,
ProductionScheduleUpdate,
ProductionScheduleFilters,
ProductionCapacityResponse,
ProductionCapacityFilters,
QualityCheckResponse,
QualityCheckCreate,
QualityCheckFilters,
ProductionPerformanceAnalytics,
YieldTrendsAnalytics,
TopDefectsAnalytics,
EquipmentEfficiencyAnalytics,
CapacityBottlenecks,
2025-09-10 08:00:50 +02:00
ProductionDashboardSummary,
2025-09-23 12:49:35 +02:00
BatchStatistics,
2025-09-10 08:00:50 +02:00
} from '../types/production';
export class ProductionService {
2025-09-23 12:49:35 +02:00
private baseUrl = '/production';
// ================================================================
// PRODUCTION BATCH ENDPOINTS
// ================================================================
async getBatches(
tenantId: string,
filters?: ProductionBatchFilters
): Promise<ProductionBatchListResponse> {
const params = new URLSearchParams();
if (filters?.status) params.append('status', filters.status);
if (filters?.product_id) params.append('product_id', filters.product_id);
if (filters?.order_id) params.append('order_id', filters.order_id);
if (filters?.start_date) params.append('start_date', filters.start_date);
if (filters?.end_date) params.append('end_date', filters.end_date);
if (filters?.page) params.append('page', filters.page.toString());
if (filters?.page_size) params.append('page_size', filters.page_size.toString());
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/batches${queryString ? `?${queryString}` : ''}`;
2025-09-10 08:00:50 +02:00
2025-09-23 12:49:35 +02:00
return apiClient.get<ProductionBatchListResponse>(url);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async getBatch(tenantId: string, batchId: string): Promise<ProductionBatchResponse> {
return apiClient.get<ProductionBatchResponse>(`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}`);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async createBatch(
tenantId: string,
batchData: ProductionBatchCreate
): Promise<ProductionBatchResponse> {
return apiClient.post<ProductionBatchResponse>(
`/tenants/${tenantId}${this.baseUrl}/batches`,
batchData
);
}
async updateBatch(
tenantId: string,
batchId: string,
batchData: ProductionBatchUpdate
): Promise<ProductionBatchResponse> {
return apiClient.put<ProductionBatchResponse>(
`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}`,
batchData
);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async deleteBatch(tenantId: string, batchId: string): Promise<void> {
return apiClient.delete<void>(`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}`);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async updateBatchStatus(
tenantId: string,
batchId: string,
statusData: ProductionBatchStatusUpdate
): Promise<ProductionBatchResponse> {
return apiClient.patch<ProductionBatchResponse>(
`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}/status`,
statusData
);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async startBatch(tenantId: string, batchId: string): Promise<ProductionBatchResponse> {
return apiClient.post<ProductionBatchResponse>(
`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}/start`
);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async completeBatch(
2025-09-10 08:00:50 +02:00
tenantId: string,
batchId: string,
2025-09-23 12:49:35 +02:00
completionData?: { actual_quantity?: number; notes?: string }
2025-09-10 08:00:50 +02:00
): Promise<ProductionBatchResponse> {
2025-09-23 12:49:35 +02:00
return apiClient.post<ProductionBatchResponse>(
`/tenants/${tenantId}${this.baseUrl}/batches/${batchId}/complete`,
completionData || {}
);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async getBatchStatistics(
2025-09-10 08:00:50 +02:00
tenantId: string,
startDate?: string,
endDate?: string
2025-09-23 12:49:35 +02:00
): Promise<BatchStatistics> {
const params = new URLSearchParams();
if (startDate) params.append('start_date', startDate);
if (endDate) params.append('end_date', endDate);
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/batches/stats${queryString ? `?${queryString}` : ''}`;
return apiClient.get<BatchStatistics>(url);
}
// ================================================================
// PRODUCTION SCHEDULE ENDPOINTS
// ================================================================
async getSchedules(
tenantId: string,
filters?: ProductionScheduleFilters
): Promise<{ schedules: ProductionScheduleResponse[]; total_count: number; page: number; page_size: number }> {
const params = new URLSearchParams();
if (filters?.start_date) params.append('start_date', filters.start_date);
if (filters?.end_date) params.append('end_date', filters.end_date);
if (filters?.is_finalized !== undefined) params.append('is_finalized', filters.is_finalized.toString());
if (filters?.page) params.append('page', filters.page.toString());
if (filters?.page_size) params.append('page_size', filters.page_size.toString());
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/schedules${queryString ? `?${queryString}` : ''}`;
return apiClient.get(url);
}
async getSchedule(tenantId: string, scheduleId: string): Promise<ProductionScheduleResponse> {
return apiClient.get<ProductionScheduleResponse>(`/tenants/${tenantId}${this.baseUrl}/schedules/${scheduleId}`);
}
async createSchedule(
tenantId: string,
scheduleData: ProductionScheduleCreate
): Promise<ProductionScheduleResponse> {
return apiClient.post<ProductionScheduleResponse>(
`/tenants/${tenantId}${this.baseUrl}/schedules`,
scheduleData
);
}
async updateSchedule(
tenantId: string,
scheduleId: string,
scheduleData: ProductionScheduleUpdate
): Promise<ProductionScheduleResponse> {
return apiClient.put<ProductionScheduleResponse>(
`/tenants/${tenantId}${this.baseUrl}/schedules/${scheduleId}`,
scheduleData
);
}
async deleteSchedule(tenantId: string, scheduleId: string): Promise<void> {
return apiClient.delete<void>(`/tenants/${tenantId}${this.baseUrl}/schedules/${scheduleId}`);
}
async finalizeSchedule(tenantId: string, scheduleId: string): Promise<ProductionScheduleResponse> {
return apiClient.post<ProductionScheduleResponse>(
`/tenants/${tenantId}${this.baseUrl}/schedules/${scheduleId}/finalize`
);
}
async getTodaysSchedule(tenantId: string): Promise<ProductionScheduleResponse | null> {
return apiClient.get<ProductionScheduleResponse | null>(`/tenants/${tenantId}${this.baseUrl}/schedules/today`);
}
// ================================================================
// PRODUCTION CAPACITY ENDPOINTS
// ================================================================
async getCapacity(
tenantId: string,
filters?: ProductionCapacityFilters
): Promise<{ capacity: ProductionCapacityResponse[]; total_count: number; page: number; page_size: number }> {
const params = new URLSearchParams();
if (filters?.resource_type) params.append('resource_type', filters.resource_type);
if (filters?.date) params.append('date', filters.date);
if (filters?.availability !== undefined) params.append('availability', filters.availability.toString());
if (filters?.page) params.append('page', filters.page.toString());
if (filters?.page_size) params.append('page_size', filters.page_size.toString());
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/capacity${queryString ? `?${queryString}` : ''}`;
return apiClient.get(url);
}
async getCapacityByDate(tenantId: string, date: string): Promise<ProductionCapacityResponse[]> {
return apiClient.get<ProductionCapacityResponse[]>(`/tenants/${tenantId}${this.baseUrl}/capacity/date/${date}`);
}
async getCapacityByResource(tenantId: string, resourceId: string): Promise<ProductionCapacityResponse[]> {
return apiClient.get<ProductionCapacityResponse[]>(`/tenants/${tenantId}${this.baseUrl}/capacity/resource/${resourceId}`);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
// ================================================================
// QUALITY CHECK ENDPOINTS
// ================================================================
async getQualityChecks(
tenantId: string,
filters?: QualityCheckFilters
): Promise<{ quality_checks: QualityCheckResponse[]; total_count: number; page: number; page_size: number }> {
const params = new URLSearchParams();
if (filters?.batch_id) params.append('batch_id', filters.batch_id);
if (filters?.product_id) params.append('product_id', filters.product_id);
if (filters?.start_date) params.append('start_date', filters.start_date);
if (filters?.end_date) params.append('end_date', filters.end_date);
if (filters?.pass_fail !== undefined) params.append('pass_fail', filters.pass_fail.toString());
if (filters?.page) params.append('page', filters.page.toString());
if (filters?.page_size) params.append('page_size', filters.page_size.toString());
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/quality-checks${queryString ? `?${queryString}` : ''}`;
return apiClient.get(url);
}
async getQualityCheck(tenantId: string, checkId: string): Promise<QualityCheckResponse> {
return apiClient.get<QualityCheckResponse>(`/tenants/${tenantId}${this.baseUrl}/quality-checks/${checkId}`);
2025-09-10 08:00:50 +02:00
}
2025-09-23 12:49:35 +02:00
async createQualityCheck(
tenantId: string,
checkData: QualityCheckCreate
): Promise<QualityCheckResponse> {
return apiClient.post<QualityCheckResponse>(
`/tenants/${tenantId}${this.baseUrl}/quality-checks`,
checkData
);
}
async getQualityChecksByBatch(tenantId: string, batchId: string): Promise<QualityCheckResponse[]> {
return apiClient.get<QualityCheckResponse[]>(`/tenants/${tenantId}${this.baseUrl}/quality-checks/batch/${batchId}`);
}
// ================================================================
// ANALYTICS ENDPOINTS
// ================================================================
async getPerformanceAnalytics(
2025-09-10 08:00:50 +02:00
tenantId: string,
startDate: string,
endDate: string
2025-09-23 12:49:35 +02:00
): Promise<ProductionPerformanceAnalytics> {
return apiClient.get<ProductionPerformanceAnalytics>(
`/tenants/${tenantId}${this.baseUrl}/analytics/performance?start_date=${startDate}&end_date=${endDate}`
);
}
async getYieldTrends(
tenantId: string,
period: 'week' | 'month' = 'week'
): Promise<YieldTrendsAnalytics> {
return apiClient.get<YieldTrendsAnalytics>(
`/tenants/${tenantId}${this.baseUrl}/analytics/yield-trends?period=${period}`
);
}
async getTopDefects(
tenantId: string,
startDate?: string,
endDate?: string
): Promise<TopDefectsAnalytics> {
const params = new URLSearchParams();
if (startDate) params.append('start_date', startDate);
if (endDate) params.append('end_date', endDate);
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/analytics/defects${queryString ? `?${queryString}` : ''}`;
return apiClient.get<TopDefectsAnalytics>(url);
}
async getEquipmentEfficiency(
tenantId: string,
startDate?: string,
endDate?: string
): Promise<EquipmentEfficiencyAnalytics> {
const params = new URLSearchParams();
if (startDate) params.append('start_date', startDate);
if (endDate) params.append('end_date', endDate);
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/analytics/equipment-efficiency${queryString ? `?${queryString}` : ''}`;
return apiClient.get<EquipmentEfficiencyAnalytics>(url);
}
async getCapacityBottlenecks(
tenantId: string,
days: number = 7
): Promise<CapacityBottlenecks> {
return apiClient.get<CapacityBottlenecks>(
`/tenants/${tenantId}${this.baseUrl}/analytics/capacity-bottlenecks?days=${days}`
);
}
// ================================================================
// DASHBOARD ENDPOINTS
// ================================================================
async getDashboardSummary(tenantId: string): Promise<ProductionDashboardSummary> {
return apiClient.get<ProductionDashboardSummary>(`/tenants/${tenantId}${this.baseUrl}/dashboard/summary`);
}
async getDailyProductionPlan(tenantId: string, date?: string): Promise<any> {
const queryString = date ? `?date=${date}` : '';
return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/dashboard/daily-plan${queryString}`);
}
async getProductionRequirements(tenantId: string, date: string): Promise<any> {
return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/dashboard/requirements/${date}`);
}
async getCapacityOverview(tenantId: string, date?: string): Promise<any> {
const queryString = date ? `?date=${date}` : '';
return apiClient.get(`/tenants/${tenantId}${this.baseUrl}/dashboard/capacity-overview${queryString}`);
}
async getQualityOverview(
tenantId: string,
startDate?: string,
endDate?: string
): Promise<any> {
const params = new URLSearchParams();
if (startDate) params.append('start_date', startDate);
if (endDate) params.append('end_date', endDate);
const queryString = params.toString();
const url = `/tenants/${tenantId}${this.baseUrl}/dashboard/quality-overview${queryString ? `?${queryString}` : ''}`;
return apiClient.get(url);
2025-09-10 08:00:50 +02:00
}
}
2025-09-23 12:49:35 +02:00
export const productionService = new ProductionService();
export default productionService;