Calendar/src/repositories/MockEventRepository.ts

82 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-11-03 21:30:50 +01:00
import { ICalendarEvent } from '../types/CalendarTypes';
import { CalendarEventType } from '../types/BookingTypes';
import { IEventRepository, UpdateSource } from './IEventRepository';
2025-11-03 14:54:57 +01:00
interface RawEventData {
id: string;
title: string;
start: string | Date;
end: string | Date;
type: string;
color?: string;
allDay?: boolean;
[key: string]: unknown;
}
/**
* MockEventRepository - Loads event data from local JSON file (LEGACY)
2025-11-03 14:54:57 +01:00
*
* This repository implementation fetches mock event data from a static JSON file.
* DEPRECATED: Use IndexedDBEventRepository for offline-first functionality.
2025-11-03 14:54:57 +01:00
*
* Data Source: data/mock-events.json
*
* NOTE: Create/Update/Delete operations are not supported - throws errors.
* This is intentional to encourage migration to IndexedDBEventRepository.
2025-11-03 14:54:57 +01:00
*/
export class MockEventRepository implements IEventRepository {
private readonly dataUrl = 'data/mock-events.json';
2025-11-03 21:30:50 +01:00
public async loadEvents(): Promise<ICalendarEvent[]> {
2025-11-03 14:54:57 +01:00
try {
const response = await fetch(this.dataUrl);
if (!response.ok) {
throw new Error(`Failed to load mock events: ${response.status} ${response.statusText}`);
}
const rawData: RawEventData[] = await response.json();
return this.processCalendarData(rawData);
} catch (error) {
console.error('Failed to load event data:', error);
throw error;
}
}
/**
* NOT SUPPORTED - MockEventRepository is read-only
* Use IndexedDBEventRepository instead
*/
public async createEvent(event: Omit<ICalendarEvent, 'id'>, source?: UpdateSource): Promise<ICalendarEvent> {
throw new Error('MockEventRepository does not support createEvent. Use IndexedDBEventRepository instead.');
}
/**
* NOT SUPPORTED - MockEventRepository is read-only
* Use IndexedDBEventRepository instead
*/
public async updateEvent(id: string, updates: Partial<ICalendarEvent>, source?: UpdateSource): Promise<ICalendarEvent> {
throw new Error('MockEventRepository does not support updateEvent. Use IndexedDBEventRepository instead.');
}
/**
* NOT SUPPORTED - MockEventRepository is read-only
* Use IndexedDBEventRepository instead
*/
public async deleteEvent(id: string, source?: UpdateSource): Promise<void> {
throw new Error('MockEventRepository does not support deleteEvent. Use IndexedDBEventRepository instead.');
}
2025-11-03 21:30:50 +01:00
private processCalendarData(data: RawEventData[]): ICalendarEvent[] {
return data.map((event): ICalendarEvent => ({
2025-11-03 14:54:57 +01:00
...event,
start: new Date(event.start),
end: new Date(event.end),
type: event.type as CalendarEventType,
2025-11-03 14:54:57 +01:00
allDay: event.allDay || false,
syncStatus: 'synced' as const
}));
}
}