Add fixes to procurement logic and fix rel-time connections
This commit is contained in:
@@ -53,17 +53,21 @@ export const SSEProvider: React.FC<SSEProviderProps> = ({ children }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get tenant ID from store - no fallback
|
||||
const tenantId = currentTenant?.id;
|
||||
|
||||
if (!tenantId) {
|
||||
if (!currentTenant?.id) {
|
||||
console.log('No tenant ID available, skipping SSE connection');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Connect to notification service SSE endpoint with token
|
||||
const eventSource = new EventSource(`http://localhost:8006/api/v1/sse/alerts/stream/${tenantId}?token=${encodeURIComponent(token)}`, {
|
||||
// Connect to gateway SSE endpoint with token and tenant_id
|
||||
// Use same protocol and host as the current page to avoid CORS and mixed content issues
|
||||
const protocol = window.location.protocol;
|
||||
const host = window.location.host;
|
||||
const sseUrl = `${protocol}//${host}/api/events?token=${encodeURIComponent(token)}&tenant_id=${currentTenant.id}`;
|
||||
|
||||
console.log('Connecting to SSE endpoint:', sseUrl);
|
||||
|
||||
const eventSource = new EventSource(sseUrl, {
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
@@ -133,39 +137,23 @@ export const SSEProvider: React.FC<SSEProviderProps> = ({ children }) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Handle connection confirmation from notification service
|
||||
eventSource.addEventListener('connected', (event) => {
|
||||
// Handle connection confirmation from gateway
|
||||
eventSource.addEventListener('connection', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
console.log('SSE connection confirmed:', data);
|
||||
} catch (error) {
|
||||
console.error('Error parsing connected event:', error);
|
||||
console.error('Error parsing connection event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle ping events (keepalive)
|
||||
eventSource.addEventListener('ping', (event) => {
|
||||
// Handle heartbeat events (keepalive)
|
||||
eventSource.addEventListener('heartbeat', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
console.log('SSE ping received:', data.timestamp);
|
||||
console.log('SSE heartbeat received:', data.timestamp);
|
||||
} catch (error) {
|
||||
console.error('Error parsing ping event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle initial items
|
||||
eventSource.addEventListener('initial_items', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
console.log('Initial items received:', data);
|
||||
|
||||
// Trigger listeners for initial data
|
||||
const listeners = eventListenersRef.current.get('initial_items');
|
||||
if (listeners) {
|
||||
listeners.forEach(callback => callback(data));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing initial_items event:', error);
|
||||
console.error('Error parsing heartbeat event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -235,6 +223,80 @@ export const SSEProvider: React.FC<SSEProviderProps> = ({ children }) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Handle inventory_alert events (high/urgent severity alerts from gateway)
|
||||
eventSource.addEventListener('inventory_alert', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
const sseEvent: SSEEvent = {
|
||||
type: 'alert',
|
||||
data,
|
||||
timestamp: data.timestamp || new Date().toISOString(),
|
||||
};
|
||||
|
||||
setLastEvent(sseEvent);
|
||||
|
||||
// Show urgent alert toast
|
||||
const toastType = data.severity === 'urgent' ? 'error' : 'error';
|
||||
|
||||
showToast({
|
||||
type: toastType,
|
||||
title: data.title || 'Alerta de Inventario',
|
||||
message: data.message,
|
||||
duration: data.severity === 'urgent' ? 0 : 5000,
|
||||
});
|
||||
|
||||
// Trigger alert listeners
|
||||
const listeners = eventListenersRef.current.get('alert');
|
||||
if (listeners) {
|
||||
listeners.forEach(callback => callback(data));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing inventory_alert event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle generic notification events from gateway
|
||||
eventSource.addEventListener('notification', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
const sseEvent: SSEEvent = {
|
||||
type: data.item_type || 'notification',
|
||||
data,
|
||||
timestamp: data.timestamp || new Date().toISOString(),
|
||||
};
|
||||
|
||||
setLastEvent(sseEvent);
|
||||
|
||||
// Show notification toast
|
||||
let toastType: 'info' | 'success' | 'warning' | 'error' = 'info';
|
||||
if (data.severity === 'urgent') toastType = 'error';
|
||||
else if (data.severity === 'high') toastType = 'warning';
|
||||
else if (data.severity === 'medium') toastType = 'info';
|
||||
|
||||
showToast({
|
||||
type: toastType,
|
||||
title: data.title || 'Notificación',
|
||||
message: data.message,
|
||||
duration: data.severity === 'urgent' ? 0 : 5000,
|
||||
});
|
||||
|
||||
// Trigger listeners for both notification and specific type
|
||||
const notificationListeners = eventListenersRef.current.get('notification');
|
||||
if (notificationListeners) {
|
||||
notificationListeners.forEach(callback => callback(data));
|
||||
}
|
||||
|
||||
if (data.item_type) {
|
||||
const typeListeners = eventListenersRef.current.get(data.item_type);
|
||||
if (typeListeners) {
|
||||
typeListeners.forEach(callback => callback(data));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing notification event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
eventSource.onerror = (error) => {
|
||||
console.error('SSE connection error:', error);
|
||||
setIsConnected(false);
|
||||
|
||||
Reference in New Issue
Block a user