Calendar/wwwroot/js/managers/EventManager.js

164 lines
5.2 KiB
JavaScript
Raw Normal View History

2026-02-03 00:02:25 +01:00
import { CoreEvents } from '../constants/CoreEvents';
/**
* EventManager - Event lifecycle and CRUD operations
* Delegates all data operations to IEventRepository
* No longer maintains in-memory cache - repository is single source of truth
*/
export class EventManager {
constructor(eventBus, dateService, config, repository) {
this.eventBus = eventBus;
this.dateService = dateService;
this.config = config;
this.repository = repository;
}
/**
* Load event data from repository
* No longer caches - delegates to repository
*/
async loadData() {
try {
// Just ensure repository is ready - no caching
await this.repository.loadEvents();
}
catch (error) {
console.error('Failed to load event data:', error);
throw error;
}
}
/**
* Get all events from repository
*/
async getEvents(copy = false) {
const events = await this.repository.loadEvents();
return copy ? [...events] : events;
}
/**
* Get event by ID from repository
*/
async getEventById(id) {
const events = await this.repository.loadEvents();
return events.find(event => event.id === id);
}
/**
* Get event by ID and return event info for navigation
* @param id Event ID to find
* @returns Event with navigation info or null if not found
*/
async getEventForNavigation(id) {
const event = await this.getEventById(id);
if (!event) {
return null;
}
// Validate event dates
const validation = this.dateService.validateDate(event.start);
if (!validation.valid) {
console.warn(`EventManager: Invalid event start date for event ${id}:`, validation.error);
return null;
}
// Validate date range
if (!this.dateService.isValidRange(event.start, event.end)) {
console.warn(`EventManager: Invalid date range for event ${id}: start must be before end`);
return null;
}
return {
event,
eventDate: event.start
};
}
/**
* Navigate to specific event by ID
* Emits navigation events for other managers to handle
* @param eventId Event ID to navigate to
* @returns true if event found and navigation initiated, false otherwise
*/
async navigateToEvent(eventId) {
const eventInfo = await this.getEventForNavigation(eventId);
if (!eventInfo) {
console.warn(`EventManager: Event with ID ${eventId} not found`);
return false;
}
const { event, eventDate } = eventInfo;
// Emit navigation request event
this.eventBus.emit(CoreEvents.NAVIGATE_TO_EVENT, {
eventId,
event,
eventDate,
eventStartTime: event.start
});
return true;
}
/**
* Get events that overlap with a given time period
*/
async getEventsForPeriod(startDate, endDate) {
const events = await this.repository.loadEvents();
// Event overlaps period if it starts before period ends AND ends after period starts
return events.filter(event => {
return event.start <= endDate && event.end >= startDate;
});
}
/**
* Create a new event and add it to the calendar
* Delegates to repository with source='local'
*/
async addEvent(event) {
const newEvent = await this.repository.createEvent(event, 'local');
this.eventBus.emit(CoreEvents.EVENT_CREATED, {
event: newEvent
});
return newEvent;
}
/**
* Update an existing event
* Delegates to repository with source='local'
*/
async updateEvent(id, updates) {
try {
const updatedEvent = await this.repository.updateEvent(id, updates, 'local');
this.eventBus.emit(CoreEvents.EVENT_UPDATED, {
event: updatedEvent
});
return updatedEvent;
}
catch (error) {
console.error(`Failed to update event ${id}:`, error);
return null;
}
}
/**
* Delete an event
* Delegates to repository with source='local'
*/
async deleteEvent(id) {
try {
await this.repository.deleteEvent(id, 'local');
this.eventBus.emit(CoreEvents.EVENT_DELETED, {
eventId: id
});
return true;
}
catch (error) {
console.error(`Failed to delete event ${id}:`, error);
return false;
}
}
/**
* Handle remote update from SignalR
* Delegates to repository with source='remote'
*/
async handleRemoteUpdate(event) {
try {
await this.repository.updateEvent(event.id, event, 'remote');
this.eventBus.emit(CoreEvents.REMOTE_UPDATE_RECEIVED, {
event
});
this.eventBus.emit(CoreEvents.EVENT_UPDATED, {
event
});
}
catch (error) {
console.error(`Failed to handle remote update for event ${event.id}:`, error);
}
}
}
//# sourceMappingURL=EventManager.js.map