import { resolveNotificationService } from "../../notifications/lib/notificationService.js";
import { buildNotificationFromType } from "../../notifications/lib/notificationBuilder.js";
import { notificationTypes } from "../notifications.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("workflows");
const metadata = {
  event: "workflows.task.assigned",
  persistent: true,
  id: "workflows:task-assigned-notification"
};
async function handle(payload, ctx) {
  if (!payload.assignedUserId) return;
  try {
    const notificationService = resolveNotificationService(ctx);
    const typeDef = notificationTypes.find((type) => type.type === "workflows.task.assigned");
    if (!typeDef) return;
    const notificationInput = buildNotificationFromType(typeDef, {
      recipientUserId: payload.assignedUserId,
      bodyVariables: {
        taskName: payload.taskName,
        workflowName: payload.workflowName,
        dueDate: payload.dueDate ?? ""
      },
      sourceEntityType: "workflows:user_task",
      sourceEntityId: payload.taskId,
      linkHref: `/backend/workflows/tasks/${payload.taskId}`
    });
    await notificationService.create(notificationInput, {
      tenantId: payload.tenantId,
      organizationId: payload.organizationId ?? null
    });
  } catch (err) {
    logger.error("Failed to create notification", { component: "task-assigned-notification", err });
  }
}
export {
  handle as default,
  metadata
};
//# sourceMappingURL=task-assigned-notification.js.map
