/* * 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.notification; import com.example.dto.dialogflow.notification.NotificationDTO; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.cloud.firestore.DocumentSnapshot; import java.time.Instant; import java.util.Map; import java.util.Objects; /** * Spring component for mapping notification data between application DTOs and Firestore documents. * This class handles the transformation of Dialogflow event details and notification metadata * into a `NotificationDTO` for persistence and provides methods to serialize and deserialize * this DTO to and from Firestore-compatible data structures. */ @Component public class FirestoreNotificationMapper { private static final String DEFAULT_LANGUAGE_CODE = "es"; private static final String FIXED_EVENT_NAME = "notificacion"; private final ObjectMapper objectMapper; private static final String DEFAULT_NOTIFICATION_STATUS="ACTIVE"; public FirestoreNotificationMapper(ObjectMapper objectMapper) { this.objectMapper = objectMapper; } public NotificationDTO mapToFirestoreNotification( String notificationId, String telephone, String notificationText, Map parameters) { Objects.requireNonNull(notificationId, "Notification ID cannot be null for mapping."); Objects.requireNonNull(notificationText, "Notification text cannot be null for mapping."); Objects.requireNonNull(parameters, "Dialogflow parameters map cannot be null."); return new NotificationDTO( notificationId, telephone, Instant.now(), notificationText, FIXED_EVENT_NAME, DEFAULT_LANGUAGE_CODE, parameters, DEFAULT_NOTIFICATION_STATUS ); } public NotificationDTO mapFirestoreDocumentToNotificationDTO(DocumentSnapshot documentSnapshot) { Objects.requireNonNull(documentSnapshot, "DocumentSnapshot cannot be null for mapping."); if (!documentSnapshot.exists()) { throw new IllegalArgumentException("DocumentSnapshot does not exist."); } try { return objectMapper.convertValue(documentSnapshot.getData(), NotificationDTO.class); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Failed to convert Firestore document data to NotificationDTO for ID " + documentSnapshot.getId(), e); } } public Map mapNotificationDTOToMap(NotificationDTO notificationDTO) { Objects.requireNonNull(notificationDTO, "NotificationDTO cannot be null for mapping to map."); return objectMapper.convertValue(notificationDTO, new com.fasterxml.jackson.core.type.TypeReference>() {}); } }