58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import type { Logger } from '@core/shared/application';
|
|
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
|
|
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
|
|
import type { EngagementAction, EngagementEntityType } from '../../domain/types/EngagementEvent';
|
|
|
|
export interface RecordEngagementInput {
|
|
action: EngagementAction;
|
|
entityType: EngagementEntityType;
|
|
entityId: string;
|
|
actorId?: string;
|
|
actorType: 'anonymous' | 'driver' | 'sponsor';
|
|
sessionId: string;
|
|
metadata?: Record<string, string | number | boolean>;
|
|
}
|
|
|
|
export interface RecordEngagementOutput {
|
|
eventId: string;
|
|
engagementWeight: number;
|
|
}
|
|
|
|
export class RecordEngagementUseCase {
|
|
constructor(
|
|
private readonly engagementRepository: IEngagementRepository,
|
|
private readonly logger: Logger,
|
|
) {}
|
|
|
|
async execute(input: RecordEngagementInput): Promise<RecordEngagementOutput> {
|
|
try {
|
|
const engagementEvent = EngagementEvent.create({
|
|
id: crypto.randomUUID(),
|
|
action: input.action,
|
|
entityType: input.entityType,
|
|
entityId: input.entityId,
|
|
actorId: input.actorId,
|
|
actorType: input.actorType,
|
|
sessionId: input.sessionId,
|
|
metadata: input.metadata,
|
|
});
|
|
|
|
await this.engagementRepository.save(engagementEvent);
|
|
|
|
this.logger.info('Engagement event recorded', {
|
|
engagementId: engagementEvent.id,
|
|
action: input.action,
|
|
entityId: input.entityId,
|
|
entityType: input.entityType,
|
|
});
|
|
|
|
return {
|
|
eventId: engagementEvent.id,
|
|
engagementWeight: engagementEvent.getEngagementWeight(),
|
|
};
|
|
} catch (error) {
|
|
this.logger.error('Failed to record engagement event', { error: error as Error, input });
|
|
throw error;
|
|
}
|
|
}
|
|
} |