This commit is contained in:
2026-02-19 17:50:14 +00:00
committed by Anibal Angulo
parent b63a1ae4a7
commit 41ba38495b
171 changed files with 7281 additions and 1144 deletions

View File

@@ -0,0 +1,68 @@
/*
* 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.ExternalNotRequestDTO;
import com.example.dto.dialogflow.base.DetectIntentRequestDTO;
import com.example.dto.dialogflow.conversation.QueryInputDTO;
import com.example.dto.dialogflow.conversation.QueryParamsDTO;
import com.example.dto.dialogflow.conversation.TextInputDTO;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Spring component for mapping an external notification request to a Dialogflow `DetectIntentRequestDTO`.
* This class takes a simplified `ExternalNotRequestDTO` and converts it into the structured
* DTO required for a Dialogflow API call, specifically for triggering a notification event.
* It ensures required parameters like the phone number are present and populates the
* request with event-specific details.
*/
@Component
public class ExternalNotRequestMapper {
private static final String LANGUAGE_CODE = "es";
private static final String TELEPHONE_PARAM_NAME = "telefono";
private static final String NOTIFICATION_TEXT_PARAM = "notification_text";
private static final String NOTIFICATION_LABEL = "NOTIFICACION";
private static final String PREFIX_PO_PARAM = "notification_po_";
public DetectIntentRequestDTO map(ExternalNotRequestDTO request) {
Objects.requireNonNull(request, "NotificationRequestDTO cannot be null for mapping.");
if (request.phoneNumber() == null || request.phoneNumber().isEmpty()) {
throw new IllegalArgumentException("Phone numbers is required and cannot be empty in NotificationRequestDTO.");
}
String phoneNumber = request.phoneNumber();
Map<String, Object> parameters = new HashMap<>();
parameters.put(TELEPHONE_PARAM_NAME, phoneNumber);
parameters.put(NOTIFICATION_TEXT_PARAM, request.text());
if (request.hiddenParameters() != null && !request.hiddenParameters().isEmpty()) {
StringBuilder poBuilder = new StringBuilder();
request.hiddenParameters().forEach((key, value) -> {
parameters.put(PREFIX_PO_PARAM + key, value);
poBuilder.append(key).append(": ").append(value).append("\n");
});
parameters.put("po", poBuilder.toString());
}
TextInputDTO textInput = new TextInputDTO(NOTIFICATION_LABEL);
QueryInputDTO queryInput = new QueryInputDTO(textInput, null, LANGUAGE_CODE);
QueryParamsDTO queryParams = new QueryParamsDTO(parameters);
return new DetectIntentRequestDTO(queryInput, queryParams);
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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<String, Object> 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<String, Object> 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<Map<String, Object>>() {});
}
}