/* * Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose. * Your use of it is subject to your agreement with Google. */ package com.example.mapper.conversation; import com.google.cloud.Timestamp; import com.example.dto.dialogflow.conversation.ConversationMessageDTO; import com.example.dto.dialogflow.conversation.MessageType; import org.springframework.stereotype.Component; import java.time.Instant; import java.util.HashMap; import java.util.Map; @Component public class ConversationMessageMapper { public Map toMap(ConversationMessageDTO message) { Map map = new HashMap<>(); map.put("entidad", message.type().name()); map.put("tiempo", message.timestamp()); map.put("mensaje", message.text()); if (message.parameters() != null) { map.put("parametros", message.parameters()); } if (message.canal() != null) { map.put("canal", message.canal()); } return map; } public ConversationMessageDTO fromMap(Map map) { Object timeObject = map.get("tiempo"); Instant timestamp = null; if (timeObject instanceof Timestamp) { timestamp = ((Timestamp) timeObject).toDate().toInstant(); } else if (timeObject instanceof Instant) { timestamp = (Instant) timeObject; } return new ConversationMessageDTO( MessageType.valueOf((String) map.get("entidad")), timestamp, (String) map.get("mensaje"), (Map) map.get("parametros"), (String) map.get("canal") ); } }