2025-11-18 16:37:33 +01:00
|
|
|
import { ISync, EntityType, SyncStatus } from '../types/CalendarTypes';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* IEntityService<T> - Generic interface for entity services with sync capabilities
|
|
|
|
|
*
|
|
|
|
|
* All entity services (Event, Booking, Customer, Resource) implement this interface
|
2025-11-20 15:25:38 +01:00
|
|
|
* to enable polymorphic operations across different entity types.
|
2025-11-18 16:37:33 +01:00
|
|
|
*
|
|
|
|
|
* ENCAPSULATION: Services encapsulate sync status manipulation.
|
|
|
|
|
* SyncManager does NOT directly manipulate entity.syncStatus - it delegates to the service.
|
|
|
|
|
*
|
2025-11-20 15:25:38 +01:00
|
|
|
* POLYMORPHISM: Both SyncManager and DataSeeder work with Array<IEntityService<any>>
|
|
|
|
|
* and use entityType property for runtime routing, avoiding switch statements.
|
2025-11-18 16:37:33 +01:00
|
|
|
*/
|
|
|
|
|
export interface IEntityService<T extends ISync> {
|
|
|
|
|
/**
|
|
|
|
|
* Entity type discriminator for runtime routing
|
|
|
|
|
* Must match EntityType values: 'Event', 'Booking', 'Customer', 'Resource'
|
|
|
|
|
*/
|
|
|
|
|
readonly entityType: EntityType;
|
|
|
|
|
|
2025-11-20 15:25:38 +01:00
|
|
|
// ============================================================================
|
|
|
|
|
// CRUD Operations (used by DataSeeder and other consumers)
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all entities from IndexedDB
|
|
|
|
|
* Used by DataSeeder to check if store is empty before seeding
|
|
|
|
|
*
|
|
|
|
|
* @returns Promise<T[]> - Array of all entities
|
|
|
|
|
*/
|
|
|
|
|
getAll(): Promise<T[]>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save an entity (create or update) to IndexedDB
|
|
|
|
|
* Used by DataSeeder to persist fetched data
|
|
|
|
|
*
|
|
|
|
|
* @param entity - Entity to save
|
|
|
|
|
*/
|
|
|
|
|
save(entity: T): Promise<void>;
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// SYNC Methods (used by SyncManager)
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
2025-11-18 16:37:33 +01:00
|
|
|
/**
|
|
|
|
|
* Mark entity as successfully synced with backend
|
|
|
|
|
* Sets syncStatus = 'synced' and persists to IndexedDB
|
|
|
|
|
*
|
|
|
|
|
* @param id - Entity ID
|
|
|
|
|
*/
|
|
|
|
|
markAsSynced(id: string): Promise<void>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mark entity as sync error (max retries exceeded)
|
|
|
|
|
* Sets syncStatus = 'error' and persists to IndexedDB
|
|
|
|
|
*
|
|
|
|
|
* @param id - Entity ID
|
|
|
|
|
*/
|
|
|
|
|
markAsError(id: string): Promise<void>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current sync status for an entity
|
|
|
|
|
* Used by SyncManager to check entity state
|
|
|
|
|
*
|
|
|
|
|
* @param id - Entity ID
|
|
|
|
|
* @returns SyncStatus or null if entity not found
|
|
|
|
|
*/
|
|
|
|
|
getSyncStatus(id: string): Promise<SyncStatus | null>;
|
|
|
|
|
}
|