Calendar/src/managers/EventManager.ts

198 lines
6.1 KiB
TypeScript
Raw Normal View History

2025-11-03 21:30:50 +01:00
import { IEventBus, ICalendarEvent } from '../types/CalendarTypes';
import { CoreEvents } from '../constants/CoreEvents';
2025-11-03 22:04:37 +01:00
import { Configuration } from '../configurations/CalendarConfig';
import { DateService } from '../utils/DateService';
2025-11-03 14:54:57 +01:00
import { IEventRepository } from '../repositories/IEventRepository';
/**
* 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 {
private dateService: DateService;
2025-11-03 21:30:50 +01:00
private config: Configuration;
2025-11-03 14:54:57 +01:00
private repository: IEventRepository;
constructor(
private eventBus: IEventBus,
dateService: DateService,
2025-11-03 21:30:50 +01:00
config: Configuration,
2025-11-03 14:54:57 +01:00
repository: IEventRepository
) {
this.dateService = dateService;
this.config = config;
2025-11-03 14:54:57 +01:00
this.repository = repository;
2025-08-09 01:16:04 +02:00
}
2025-08-09 01:16:04 +02:00
/**
2025-11-03 14:54:57 +01:00
* Load event data from repository
* No longer caches - delegates to repository
2025-08-09 01:16:04 +02:00
*/
public async loadData(): Promise<void> {
try {
// Just ensure repository is ready - no caching
await this.repository.loadEvents();
} catch (error) {
console.error('Failed to load event data:', error);
2025-11-03 14:54:57 +01:00
throw error;
2025-08-07 00:15:44 +02:00
}
}
/**
* Get all events from repository
*/
public async getEvents(copy: boolean = false): Promise<ICalendarEvent[]> {
const events = await this.repository.loadEvents();
return copy ? [...events] : events;
}
/**
* Get event by ID from repository
*/
public async getEventById(id: string): Promise<ICalendarEvent | undefined> {
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
*/
public async getEventForNavigation(id: string): Promise<{ event: ICalendarEvent; eventDate: Date } | null> {
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
*/
public async navigateToEvent(eventId: string): Promise<boolean> {
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
*/
public async getEventsForPeriod(startDate: Date, endDate: Date): Promise<ICalendarEvent[]> {
const events = await this.repository.loadEvents();
// Event overlaps period if it starts before period ends AND ends after period starts
return events.filter(event => {
2025-09-09 14:35:21 +02:00
return event.start <= endDate && event.end >= startDate;
});
}
/**
* Create a new event and add it to the calendar
* Delegates to repository with source='local'
*/
public async addEvent(event: Omit<ICalendarEvent, 'id'>): Promise<ICalendarEvent> {
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'
*/
public async updateEvent(id: string, updates: Partial<ICalendarEvent>): Promise<ICalendarEvent | null> {
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'
*/
public async deleteEvent(id: string): Promise<boolean> {
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'
*/
public async handleRemoteUpdate(event: ICalendarEvent): Promise<void> {
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);
}
}
}