Refactor event payload types and event handling
Extracts common event payload interfaces for entity saved, deleted, and audit logged events Improves type safety and reduces code duplication by centralizing event payload definitions
This commit is contained in:
parent
9ea98e3a04
commit
185330402e
3 changed files with 41 additions and 22 deletions
|
|
@ -1,10 +1,10 @@
|
|||
import { ISync, EntityType, SyncStatus } from '../types/CalendarTypes';
|
||||
import { ISync, EntityType, SyncStatus, IEventBus } from '../types/CalendarTypes';
|
||||
import { IEntityService } from './IEntityService';
|
||||
import { SyncPlugin } from './SyncPlugin';
|
||||
import { IndexedDBContext } from './IndexedDBContext';
|
||||
import { IEventBus } from '../types/CalendarTypes';
|
||||
import { CoreEvents } from '../constants/CoreEvents';
|
||||
import { diff } from 'json-diff-ts';
|
||||
import { IEntitySavedPayload, IEntityDeletedPayload } from '../types/EventTypes';
|
||||
|
||||
/**
|
||||
* BaseEntityService<T extends ISync> - Abstract base class for all entity services
|
||||
|
|
@ -171,13 +171,14 @@ export abstract class BaseEntityService<T extends ISync> implements IEntityServi
|
|||
|
||||
request.onsuccess = () => {
|
||||
// Emit ENTITY_SAVED event
|
||||
this.eventBus.emit(CoreEvents.ENTITY_SAVED, {
|
||||
const payload: IEntitySavedPayload = {
|
||||
entityType: this.entityType,
|
||||
entityId,
|
||||
operation: isCreate ? 'create' : 'update',
|
||||
changes,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
};
|
||||
this.eventBus.emit(CoreEvents.ENTITY_SAVED, payload);
|
||||
resolve();
|
||||
};
|
||||
|
||||
|
|
@ -201,12 +202,13 @@ export abstract class BaseEntityService<T extends ISync> implements IEntityServi
|
|||
|
||||
request.onsuccess = () => {
|
||||
// Emit ENTITY_DELETED event
|
||||
this.eventBus.emit(CoreEvents.ENTITY_DELETED, {
|
||||
const payload: IEntityDeletedPayload = {
|
||||
entityType: this.entityType,
|
||||
entityId: id,
|
||||
operation: 'delete',
|
||||
timestamp: Date.now()
|
||||
});
|
||||
};
|
||||
this.eventBus.emit(CoreEvents.ENTITY_DELETED, payload);
|
||||
resolve();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { IndexedDBContext } from '../IndexedDBContext';
|
|||
import { IAuditEntry } from '../../types/AuditTypes';
|
||||
import { EntityType, IEventBus } from '../../types/CalendarTypes';
|
||||
import { CoreEvents } from '../../constants/CoreEvents';
|
||||
import { IEntitySavedPayload, IEntityDeletedPayload, IAuditLoggedPayload } from '../../types/EventTypes';
|
||||
|
||||
/**
|
||||
* AuditService - Entity service for audit entries
|
||||
|
|
@ -52,13 +53,7 @@ export class AuditService extends BaseEntityService<IAuditEntry> {
|
|||
/**
|
||||
* Handle ENTITY_SAVED event - create audit entry
|
||||
*/
|
||||
private async handleEntitySaved(payload: {
|
||||
entityType: EntityType;
|
||||
entityId: string;
|
||||
operation: 'create' | 'update';
|
||||
changes: any;
|
||||
timestamp: number;
|
||||
}): Promise<void> {
|
||||
private async handleEntitySaved(payload: IEntitySavedPayload): Promise<void> {
|
||||
// Don't audit audit entries (prevent infinite loops)
|
||||
if (payload.entityType === 'Audit') return;
|
||||
|
||||
|
|
@ -80,12 +75,7 @@ export class AuditService extends BaseEntityService<IAuditEntry> {
|
|||
/**
|
||||
* Handle ENTITY_DELETED event - create audit entry
|
||||
*/
|
||||
private async handleEntityDeleted(payload: {
|
||||
entityType: EntityType;
|
||||
entityId: string;
|
||||
operation: 'delete';
|
||||
timestamp: number;
|
||||
}): Promise<void> {
|
||||
private async handleEntityDeleted(payload: IEntityDeletedPayload): Promise<void> {
|
||||
// Don't audit audit entries (prevent infinite loops)
|
||||
if (payload.entityType === 'Audit') return;
|
||||
|
||||
|
|
@ -123,13 +113,14 @@ export class AuditService extends BaseEntityService<IAuditEntry> {
|
|||
|
||||
request.onsuccess = () => {
|
||||
// Emit AUDIT_LOGGED instead of ENTITY_SAVED
|
||||
this.eventBus.emit(CoreEvents.AUDIT_LOGGED, {
|
||||
const payload: IAuditLoggedPayload = {
|
||||
auditId: entity.id,
|
||||
entityType: entity.entityType,
|
||||
entityId: entity.entityId,
|
||||
operation: entity.operation,
|
||||
timestamp: entity.timestamp
|
||||
});
|
||||
};
|
||||
this.eventBus.emit(CoreEvents.AUDIT_LOGGED, payload);
|
||||
resolve();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
*/
|
||||
|
||||
import { IColumnBounds } from "../utils/ColumnDetectionUtils";
|
||||
import { ICalendarEvent } from "./CalendarTypes";
|
||||
import { ICalendarEvent, EntityType } from "./CalendarTypes";
|
||||
|
||||
/**
|
||||
* Drag Event Payload Interfaces
|
||||
|
|
@ -104,3 +104,29 @@ export interface INavButtonClickedEventPayload {
|
|||
direction: 'next' | 'previous' | 'today';
|
||||
newDate: Date;
|
||||
}
|
||||
|
||||
// Entity saved event payload
|
||||
export interface IEntitySavedPayload {
|
||||
entityType: EntityType;
|
||||
entityId: string;
|
||||
operation: 'create' | 'update';
|
||||
changes: any;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
// Entity deleted event payload
|
||||
export interface IEntityDeletedPayload {
|
||||
entityType: EntityType;
|
||||
entityId: string;
|
||||
operation: 'delete';
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
// Audit logged event payload
|
||||
export interface IAuditLoggedPayload {
|
||||
auditId: string;
|
||||
entityType: EntityType;
|
||||
entityId: string;
|
||||
operation: 'create' | 'update' | 'delete';
|
||||
timestamp: number;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue