/* * 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 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); } }