import { Injectable } from "@nestjs/common"; import { getErrorMessage } from "@bff/core/utils/error.util"; import { SimNotificationService } from "./sim-notification.service"; import type { SimNotificationContext } from "../interfaces/sim-base.interface"; interface RunOptions { baseContext: SimNotificationContext; enrichSuccess?: (result: T) => Partial; enrichError?: (error: unknown) => Partial; } @Injectable() export class SimActionRunnerService { constructor(private readonly simNotification: SimNotificationService) {} async run( action: string, options: RunOptions, handler: () => Promise ): Promise { try { const result = await handler(); const successContext = { ...options.baseContext, ...(options.enrichSuccess ? options.enrichSuccess(result) : {}), }; await this.simNotification.notifySimAction(action, "SUCCESS", successContext); return result; } catch (error) { const errorContext = { ...options.baseContext, error: getErrorMessage(error), ...(options.enrichError ? options.enrichError(error) : {}), }; await this.simNotification.notifySimAction(action, "ERROR", errorContext); throw error; } } }