2025-12-17 23:54:25 +01:00
|
|
|
import { ICalendarEvent, EntityType, CalendarEventType } from '../types/CalendarTypes';
|
2025-11-20 15:25:38 +01:00
|
|
|
import { IApiRepository } from './IApiRepository';
|
2025-11-03 14:54:57 +01:00
|
|
|
|
|
|
|
|
interface RawEventData {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
start: string | Date;
|
|
|
|
|
end: string | Date;
|
|
|
|
|
type: string;
|
|
|
|
|
allDay?: boolean;
|
2025-12-17 23:54:25 +01:00
|
|
|
bookingId?: string;
|
|
|
|
|
resourceId?: string;
|
|
|
|
|
customerId?: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
recurringId?: string;
|
|
|
|
|
metadata?: Record<string, unknown>;
|
2025-11-03 14:54:57 +01:00
|
|
|
[key: string]: unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-11-20 15:25:38 +01:00
|
|
|
* MockEventRepository - Loads event data from local JSON file
|
2025-11-03 14:54:57 +01:00
|
|
|
*
|
2025-12-17 23:54:25 +01:00
|
|
|
* Used for development and testing. Only fetchAll() is implemented.
|
2025-11-03 14:54:57 +01:00
|
|
|
*/
|
2025-11-20 15:25:38 +01:00
|
|
|
export class MockEventRepository implements IApiRepository<ICalendarEvent> {
|
|
|
|
|
public readonly entityType: EntityType = 'Event';
|
2025-11-03 14:54:57 +01:00
|
|
|
private readonly dataUrl = 'data/mock-events.json';
|
|
|
|
|
|
2025-11-20 15:25:38 +01:00
|
|
|
/**
|
|
|
|
|
* Fetch all events from mock JSON file
|
|
|
|
|
*/
|
|
|
|
|
public async fetchAll(): 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:54:25 +01:00
|
|
|
public async sendCreate(_event: ICalendarEvent): Promise<ICalendarEvent> {
|
2025-11-20 15:25:38 +01:00
|
|
|
throw new Error('MockEventRepository does not support sendCreate. Mock data is read-only.');
|
2025-11-05 00:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:54:25 +01:00
|
|
|
public async sendUpdate(_id: string, _updates: Partial<ICalendarEvent>): Promise<ICalendarEvent> {
|
2025-11-20 15:25:38 +01:00
|
|
|
throw new Error('MockEventRepository does not support sendUpdate. Mock data is read-only.');
|
2025-11-05 00:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:54:25 +01:00
|
|
|
public async sendDelete(_id: string): Promise<void> {
|
2025-11-20 15:25:38 +01:00
|
|
|
throw new Error('MockEventRepository does not support sendDelete. Mock data is read-only.');
|
2025-11-05 00:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-03 21:30:50 +01:00
|
|
|
private processCalendarData(data: RawEventData[]): ICalendarEvent[] {
|
2025-11-20 15:25:38 +01:00
|
|
|
return data.map((event): ICalendarEvent => {
|
2025-12-17 23:54:25 +01:00
|
|
|
// Validate customer event constraints
|
2025-11-20 15:25:38 +01:00
|
|
|
if (event.type === 'customer') {
|
2025-12-17 23:54:25 +01:00
|
|
|
if (!event.bookingId) console.warn(`Customer event ${event.id} missing bookingId`);
|
|
|
|
|
if (!event.resourceId) console.warn(`Customer event ${event.id} missing resourceId`);
|
|
|
|
|
if (!event.customerId) console.warn(`Customer event ${event.id} missing customerId`);
|
2025-11-20 15:25:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: event.id,
|
|
|
|
|
title: event.title,
|
|
|
|
|
description: event.description,
|
|
|
|
|
start: new Date(event.start),
|
|
|
|
|
end: new Date(event.end),
|
|
|
|
|
type: event.type as CalendarEventType,
|
|
|
|
|
allDay: event.allDay || false,
|
|
|
|
|
bookingId: event.bookingId,
|
|
|
|
|
resourceId: event.resourceId,
|
|
|
|
|
customerId: event.customerId,
|
|
|
|
|
recurringId: event.recurringId,
|
|
|
|
|
metadata: event.metadata,
|
|
|
|
|
syncStatus: 'synced' as const
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-11-03 14:54:57 +01:00
|
|
|
}
|
|
|
|
|
}
|