Files
bbps-backend/src/main/java/com/billpaymentservice/bbps/util/BillerMapperHelper.java
T
2025-11-26 18:01:36 +05:30

1061 lines
44 KiB
Java

package com.billpaymentservice.bbps.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.billpaymentservice.bbps.dto.AdditionalInfoBillDTO;
import com.billpaymentservice.bbps.dto.AmountOptionDTO;
import com.billpaymentservice.bbps.dto.BillAdditionalInfoDTO;
import com.billpaymentservice.bbps.dto.BillCustomerParamsDTO;
import com.billpaymentservice.bbps.dto.BillerIconDTO;
import com.billpaymentservice.bbps.dto.CheckClientBillerResponseDTO;
import com.billpaymentservice.bbps.dto.ComplaintHistoryDTO;
import com.billpaymentservice.bbps.dto.ContactMaster;
import com.billpaymentservice.bbps.dto.CustomerParamDAO;
import com.billpaymentservice.bbps.dto.CustomerParamGroupsDTO;
import com.billpaymentservice.bbps.dto.Detail;
import com.billpaymentservice.bbps.dto.GroupDTO;
import com.billpaymentservice.bbps.dto.MasterCategoryBillerResponse;
import com.billpaymentservice.bbps.dto.PlanDTO;
import com.billpaymentservice.bbps.dto.TicketDetailsDTO;
import com.billpaymentservice.bbps.dto.TicketDispositionsDTO;
import com.billpaymentservice.bbps.dto.request.BillFetchByBillIdRequest;
import com.billpaymentservice.bbps.dto.request.BillPaymentRequest;
import com.billpaymentservice.bbps.dto.request.ComplaintRequestDTO;
import com.billpaymentservice.bbps.dto.request.FetchBillRequest;
import com.billpaymentservice.bbps.dto.request.FetchBillValidatestRequest;
import com.billpaymentservice.bbps.dto.request.GetTicketLatestStausRequest;
import com.billpaymentservice.bbps.dto.response.AmountOptionsResponse;
import com.billpaymentservice.bbps.dto.response.BillDataResponse;
import com.billpaymentservice.bbps.dto.response.ClientReqIdResponse;
import com.billpaymentservice.bbps.dto.response.ComplaintHistoryStatusResponse;
import com.billpaymentservice.bbps.dto.response.FetchBillValidateResponse;
import com.billpaymentservice.bbps.dto.response.PlanResponse;
import com.billpaymentservice.bbps.dto.response.TicketCheckStatusResponse;
import com.billpaymentservice.bbps.dto.response.ViewAllComplaintsResponse;
import com.billpaymentservice.bbps.dto.response.ViewAllComplaintsTicketsResponse;
import com.billpaymentservice.bbps.dto.response.ViewComplainByIdResponse;
import com.billpaymentservice.bbps.dto.sentRequest.BillPaymentForwardRequest;
import com.billpaymentservice.bbps.dto.sentRequest.FetchBillForwardRequest;
import com.billpaymentservice.bbps.dto.sentRequest.FetchBillValidatestForwardRequest;
import com.billpaymentservice.bbps.dto.sentRequest.FetchComplaintForwardRequest;
import com.billpaymentservice.bbps.dto.sentRequest.FetchTicketLatestStatusRequest;
import com.billpaymentservice.bbps.entites.AdditionalInfoBill;
import com.billpaymentservice.bbps.entites.AdditionalInfoBillValidate;
import com.billpaymentservice.bbps.entites.AdditionalInfoPlans;
import com.billpaymentservice.bbps.entites.AmountOptionByBillerId;
import com.billpaymentservice.bbps.entites.AmountOptionDetails;
import com.billpaymentservice.bbps.entites.BillAdditionalInfo;
import com.billpaymentservice.bbps.entites.BillCustomerParams;
import com.billpaymentservice.bbps.entites.BillMaster;
import com.billpaymentservice.bbps.entites.BillTag;
import com.billpaymentservice.bbps.entites.BillValidation;
import com.billpaymentservice.bbps.entites.BillerCategoryIcon;
import com.billpaymentservice.bbps.entites.BillerCategoryWise;
import com.billpaymentservice.bbps.entites.BillerDetails;
import com.billpaymentservice.bbps.entites.BillerResponse;
import com.billpaymentservice.bbps.entites.CategoryResponseList;
import com.billpaymentservice.bbps.entites.CheckClientBillerData;
import com.billpaymentservice.bbps.entites.CheckClientStatusReqId;
import com.billpaymentservice.bbps.entites.ComplainStatus;
import com.billpaymentservice.bbps.entites.ComplaintHistory;
import com.billpaymentservice.bbps.entites.ComplaintStatusDetails;
import com.billpaymentservice.bbps.entites.ContactMasterData;
import com.billpaymentservice.bbps.entites.CustomerParamBill;
import com.billpaymentservice.bbps.entites.CustomerParamBillValidation;
import com.billpaymentservice.bbps.entites.CustomerParamGroupById;
import com.billpaymentservice.bbps.entites.GroupParam;
import com.billpaymentservice.bbps.entites.Plan;
import com.billpaymentservice.bbps.entites.PlanCategorySubType;
import com.billpaymentservice.bbps.entites.PlanResponses;
import com.billpaymentservice.bbps.entites.PlansList;
import com.billpaymentservice.bbps.entites.TicketDispositionDetails;
import com.billpaymentservice.bbps.entites.ViewAllComplaintTicketStatusDetails;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.repository.AmountOptionByBillerIdRepository;
import com.billpaymentservice.bbps.repository.BBPSMasterDataRepo;
import com.billpaymentservice.bbps.repository.BillMasterRepository;
import com.billpaymentservice.bbps.repository.BillValidationRepository;
import com.billpaymentservice.bbps.repository.BillerCategoryWiseRepository;
import com.billpaymentservice.bbps.repository.BillerDataRepository;
import com.billpaymentservice.bbps.repository.CategoryResponseListRepository;
import com.billpaymentservice.bbps.repository.CheckClientBillerDataRepository;
import com.billpaymentservice.bbps.repository.ClientReqIdRepository;
import com.billpaymentservice.bbps.repository.ComplainDetailRepository;
import com.billpaymentservice.bbps.repository.ComplaintHistoryRepository;
import com.billpaymentservice.bbps.repository.ComplaintStatusRepository;
import com.billpaymentservice.bbps.repository.ContactMasterDataRepository;
import com.billpaymentservice.bbps.repository.CustomerParamGroupByBillerIdRepository;
import com.billpaymentservice.bbps.repository.FetchBillValidateRepository;
import com.billpaymentservice.bbps.repository.PlanResponseRepository;
import com.billpaymentservice.bbps.repository.ReOpenComplaintRepository;
import com.billpaymentservice.bbps.repository.TicketDispositionRepository;
import com.billpaymentservice.bbps.repository.ViewAllComplainTicketDetailRepository;
@Component
public class BillerMapperHelper {
private static final Logger LOGGER = LoggerFactory.getLogger(BillerMapperHelper.class);
private final BillerCategoryWiseRepository billerRepo;
private final CustomerParamGroupByBillerIdRepository customerParamGroupByBillerIdRepo;
private final AmountOptionByBillerIdRepository amountOptionByBillerIdRepository;
private final PlanResponseRepository planResponseRepository;
private final TicketDispositionRepository ticketDispositionRepository;
private final CategoryResponseListRepository categoryResponseListRepository;
private final FetchBillValidateRepository fetchBillValidateRepository;
private final ComplaintStatusRepository complaintStatusRepository;
private final ReOpenComplaintRepository reOpenComplaintRepository;
private final ComplaintHistoryRepository complaintHistoryRepository;
private final ClientReqIdRepository clientReqIdRepository;
private final ComplainDetailRepository complainDetailRepository;
private final CheckClientBillerDataRepository checkClientBillerDataRepository;
private final ViewAllComplainTicketDetailRepository viewAllComplainTicketDetailRepository;
private final ContactMasterDataRepository contactMasterDataRepository;
private final BBPSMasterDataRepo bbpsMasterDataRepo;
private final BillerDataRepository billerDataRespository;
private final BillMasterRepository billMasterRepository;
private final BillValidationRepository billValidationRepository;
public BillerMapperHelper(BillerCategoryWiseRepository billerRepo,
CustomerParamGroupByBillerIdRepository customerParamGroupByBillerIdRepo,
AmountOptionByBillerIdRepository amountOptionByBillerIdRepository,
PlanResponseRepository planResponseRepository,
CategoryResponseListRepository categoryResponseListRepository,
FetchBillValidateRepository fetchBillValidateRepository,
ComplaintStatusRepository complaintStatusRepository, ReOpenComplaintRepository reOpenComplaintRepository,
ComplaintHistoryRepository complaintHistoryRepository, ComplainDetailRepository complainDetailRepository,
ClientReqIdRepository clientReqIdRepository,
CheckClientBillerDataRepository checkClientBillerDataRepository,
TicketDispositionRepository ticketDispositionRepository,
ViewAllComplainTicketDetailRepository viewAllComplainTicketDetailRepository,
ContactMasterDataRepository contactMasterDataRepository, BBPSMasterDataRepo bbpsMasterDataRepo,
BillerDataRepository billerDataRespository, BillMasterRepository billMasterRepository,
BillValidationRepository billValidationRepository) {
this.billerRepo = billerRepo;
this.customerParamGroupByBillerIdRepo = customerParamGroupByBillerIdRepo;
this.amountOptionByBillerIdRepository = amountOptionByBillerIdRepository;
this.planResponseRepository = planResponseRepository;
this.categoryResponseListRepository = categoryResponseListRepository;
this.fetchBillValidateRepository = fetchBillValidateRepository;
this.complaintStatusRepository = complaintStatusRepository;
this.reOpenComplaintRepository = reOpenComplaintRepository;
this.complaintHistoryRepository = complaintHistoryRepository;
this.complainDetailRepository = complainDetailRepository;
this.clientReqIdRepository = clientReqIdRepository;
this.checkClientBillerDataRepository = checkClientBillerDataRepository;
this.ticketDispositionRepository = ticketDispositionRepository;
this.viewAllComplainTicketDetailRepository = viewAllComplainTicketDetailRepository;
this.contactMasterDataRepository = contactMasterDataRepository;
this.bbpsMasterDataRepo = bbpsMasterDataRepo;
this.billerDataRespository = billerDataRespository;
this.billMasterRepository = billMasterRepository;
this.billValidationRepository = billValidationRepository;
}
/**
* Maps a list of MasterCategoryBillerResponse DTOs to entities, saves them, and
* returns the saved list
*/
public List<BillerCategoryWise> mapAndSaveAll(List<MasterCategoryBillerResponse> dtos, String requestId,
String txnId) {
if (dtos == null || dtos.isEmpty()) {
return Collections.emptyList();
}
List<BillerCategoryWise> entities = new ArrayList<>();
for (MasterCategoryBillerResponse dto : dtos) {
// Check if biller already exists
BillerCategoryWise biller = billerRepo.findById(dto.getBillerId()).orElse(new BillerCategoryWise()); // create
// Set/update fields
biller.setBillerId(dto.getBillerId());
biller.setBillerName(dto.getBillerName());
biller.setBillerType(dto.getBillerType());
biller.setBillerCategory(dto.getBillerCategory());
biller.setBillerCoverage(dto.getBillerCoverage());
biller.setBillerResponseType(dto.getBillerResponseType());
biller.setBillerDescription(dto.getBillerDescription());
biller.setPlanMDMRequirement(dto.getPlanMDMRequirement());
biller.setAdhocBiller(dto.getAdhocBiller());
biller.setPaymentAmountExactness(dto.getPaymentAmountExactness());
biller.setCreateDateTime(new Date());
biller.setRequestId(requestId);
biller.setTxnId(txnId);
// Handle icon
BillerIconDTO iconDTO = dto.getBiller_icon();
if (iconDTO != null) {
BillerCategoryIcon icon;
if (biller.getIcon() != null) {
icon = biller.getIcon(); // update existing icon
} else {
icon = new BillerCategoryIcon();
icon.setBiller(biller);
}
icon.setIconData(iconDTO.getIcon_data());
icon.setIconType(iconDTO.getIcon_type());
biller.setIcon(icon); // set to biller
}
entities.add(biller);
}
// Save all at once
return billerRepo.saveAll(entities);
}
public CustomerParamGroupsDTO saveCustomerParamGroups(CustomerParamGroupsDTO dtos, String requestId, String txnId) {
if (dtos == null || dtos.getGroups() == null || dtos.getGroups().isEmpty()) {
return new CustomerParamGroupsDTO(); // Return empty DTO if no groups exist
}
List<CustomerParamGroupById> entities = new ArrayList<>();
// Loop through the top-level groups and save them (including nested groups)
for (GroupDTO groupDTO : dtos.getGroups()) {
CustomerParamGroupById entity = saveGroupWithChildren(groupDTO, null, requestId, txnId); // null is for root
// (no
// parent group)
entities.add(entity); // Add the saved entity to the list
}
// Save all entities at once using the repository
List<CustomerParamGroupById> savedEntities = customerParamGroupByBillerIdRepo.saveAll(entities);
// Convert saved entities back into DTOs to return
return convertEntitiesToDTO(savedEntities);
}
private CustomerParamGroupById saveGroupWithChildren(GroupDTO groupDTO, CustomerParamGroupById parentGroup,
String requestId, String txnId) {
// Create a new entity for the group
CustomerParamGroupById groupEntity = new CustomerParamGroupById();
groupEntity.setGroupId(groupDTO.getGroupId());
groupEntity.setName(groupDTO.getName());
groupEntity.setInput(groupDTO.getInput());
groupEntity.setParentGroup(parentGroup); // Link to the parent group (if exists)
groupEntity.setRequestId(requestId);
groupEntity.setTxnId(txnId);
groupEntity.setCreateDateTime(new Date());
// Save group parameters (if any)
if (groupDTO.getParams() != null && !groupDTO.getParams().isEmpty()) {
List<GroupParam> groupParams = new ArrayList<>();
for (String paramName : groupDTO.getParams()) {
GroupParam param = new GroupParam();
param.setParamName(paramName); // Set the parameter name (e.g., "Admission No")
param.setGroup(groupEntity); // Link the parameter to the current group
groupParams.add(param);
}
groupEntity.setParams(groupParams); // Link parameters to the group
}
// Recursively save child groups if they exist
if (groupDTO.getGroups() != null && !groupDTO.getGroups().isEmpty()) {
List<CustomerParamGroupById> childEntities = new ArrayList<>();
for (GroupDTO childGroupDTO : groupDTO.getGroups()) {
CustomerParamGroupById childEntity = saveGroupWithChildren(childGroupDTO, groupEntity, requestId,
txnId); // Pass
// current
// group
// as
// parent
childEntities.add(childEntity);
}
groupEntity.setChildGroups(childEntities); // Set the child groups
}
// Save the current group entity and return it
return groupEntity;
}
private CustomerParamGroupsDTO convertEntitiesToDTO(List<CustomerParamGroupById> savedEntities) {
if (savedEntities == null || savedEntities.isEmpty()) {
// Return an empty or default DTO if no entities are found
return new CustomerParamGroupsDTO(); // return an empty DTO object
}
CustomerParamGroupsDTO responseDTO = new CustomerParamGroupsDTO();
List<GroupDTO> savedGroups = new ArrayList<>();
// Convert each saved entity to DTO
for (CustomerParamGroupById savedEntity : savedEntities) {
GroupDTO groupDTO = new GroupDTO();
groupDTO.setGroupId(savedEntity.getGroupId());
groupDTO.setName(savedEntity.getName());
groupDTO.setInput(savedEntity.getInput());
// Recursively map child groups if they exist
if (savedEntity.getChildGroups() != null) {
List<GroupDTO> childGroupDTOs = new ArrayList<>();
for (CustomerParamGroupById childEntity : savedEntity.getChildGroups()) {
GroupDTO childGroupDTO = new GroupDTO();
childGroupDTO.setGroupId(childEntity.getGroupId());
childGroupDTO.setName(childEntity.getName());
childGroupDTO.setInput(childEntity.getInput());
// childGroupDTO.setParams(childEntity.getParams()); // Set parameters of child
// group
childGroupDTOs.add(childGroupDTO);
}
groupDTO.setGroups(childGroupDTOs);
}
// Add the DTO to the response
savedGroups.add(groupDTO);
}
responseDTO.setGroups(savedGroups);
return responseDTO;
}
// Method to save AmountOptionsResponse and AmountOption entities
public String saveAmountOptionsResponse(AmountOptionsResponse responseWrapper, String requestId, String txnId) {
// Convert AmountOptionsResponseDTO to AmountOptionsResponse entity
if (responseWrapper == null) {
// Return an empty or default DTO if no entities are found
return "Data Is Empty"; // return an empty DTO object
}
AmountOptionByBillerId entity = new AmountOptionByBillerId();
entity.setAmountOptions(responseWrapper.getAmountOptions());
// Map AmountOptionDTO list to AmountOptionDetails entities
List<AmountOptionDetails> amountOptionDetailsList = mapAmountOptionDTOToEntity(
responseWrapper.getAmountOptionDAOS(), requestId, txnId);
// Set the mapped AmountOptionDetails list to AmountOptionByBillerId entity
entity.setAmountOptionDetails(amountOptionDetailsList);
entity.setCreateDateTime(new Date());
// Save AmountOptionByBillerId entity (this will also save related
// AmountOptionDetails entities)
amountOptionByBillerIdRepository.save(entity); // Saves the entity and cascades to AmountOptionDetails
// Return success message
return "Data saved successfully!";
}
private List<AmountOptionDetails> mapAmountOptionDTOToEntity(List<AmountOptionDTO> amountOptionDTOs,
String requestId, String txnId) {
// If the list is null or empty, return an empty list of entities
if (amountOptionDTOs == null || amountOptionDTOs.isEmpty()) {
return new ArrayList<>(); // Return an empty list of AmountOptionDetails
}
// AmountOptionDetails entities
return amountOptionDTOs.stream().flatMap(dto ->
// For each DTO, convert its breakup sets to AmountOptionDetails entities
dto.getAmountBreakupSets().stream().map(breakup -> {
// Create a new AmountOptionDetails entity for each breakup set
AmountOptionDetails amountOptionDetails = new AmountOptionDetails();
// Set the breakup set as a separate entity (each breakup set becomes a new
// record)
amountOptionDetails.setAmountBreakupSet(breakup); // Store each breakup set as an individual record
// You can optionally set the amountOptionId here, if you want to link this
// detail to the parent.
// Assuming you want to set it directly here or pass it via the entity later:
amountOptionDetails.setAmountOptionId(dto.getAmountOptionId());
amountOptionDetails.setRequestId(requestId);
amountOptionDetails.setTxnId(txnId);
// Return the mapped AmountOptionDetails entity
return amountOptionDetails;
})).collect(Collectors.toList()); // Collect them into a list and return
}
public PlanResponses savePlanResponse(PlanResponse planResponse, String reqId, String txnId) {
PlanResponses response = new PlanResponses();
response.setRequestId(reqId);
response.setPageNo(planResponse.getPageNo());
response.setPageSize(planResponse.getPageSize());
response.setTotalElements(planResponse.getTotalElements());
response.setTotalPages(planResponse.getTotalPages());
response.setTxnId(txnId);
response.setCreateDateTime(new Date());
PlansList plansList = new PlansList();
List<Plan> planEntities = new ArrayList<>();
for (PlanDTO dto : planResponse.getPlansList().getPlans()) {
Plan plan = new Plan();
plan.setPlanId(dto.getPlanId());
plan.setDescription(dto.getDescription());
plan.setBillerId(dto.getBillerId());
plan.setCategoryType(dto.getCategoryType());
plan.setAmountInRupees(dto.getAmountInRupees());
plan.setEffctvFrom(dto.getEffctvFrom());
plan.setEffctvTo(dto.getEffctvTo());
plan.setStatus(dto.getStatus());
plan.setPlansList(plansList);
// categorySubType
List<PlanCategorySubType> subs = new ArrayList<>();
if (dto.getCategorySubType() != null) {
for (String s : dto.getCategorySubType()) {
PlanCategorySubType sub = new PlanCategorySubType();
sub.setCategorySubType(s);
sub.setPlan(plan);
subs.add(sub);
}
}
plan.setCategorySubTypes(subs);
// additionalInfo + tags
// ✅ additionalInfo + tags
if (dto.getAdditionalInfo() != null) {
AdditionalInfoPlans info = new AdditionalInfoPlans();
info.setPlanAdditionalInfoId(dto.getAdditionalInfo().getPlanAdditionalInfoId().toString());
List<com.billpaymentservice.bbps.entites.PlanTag> tagEntities = new ArrayList<>();
if (dto.getAdditionalInfo().getTags() != null && !dto.getAdditionalInfo().getTags().isEmpty()) {
for (com.billpaymentservice.bbps.dto.PlanTag tagDto : dto.getAdditionalInfo().getTags()) {
com.billpaymentservice.bbps.entites.PlanTag tagEntity = new com.billpaymentservice.bbps.entites.PlanTag();
tagEntity.setTagId(tagDto.getTagId().toString());
tagEntity.setName(tagDto.getName());
tagEntity.setValue(tagDto.getValue());
tagEntity.setAdditionalInfo(info); // ✅ maintain relationship
tagEntities.add(tagEntity);
}
}
// ✅ Now set the entity list
info.setTags(tagEntities);
plan.setAdditionalInfo(info);
}
planEntities.add(plan);
}
plansList.setPlans(planEntities);
response.setPlansList(plansList);
return planResponseRepository.save(response);
}
public String saveCategoryItems(List<String> categoryRespList, String requestId, String txnId) {
int savedCount = 0; // To track how many items were saved
if (categoryRespList == null || categoryRespList.isEmpty()) {
return "No items to save.";
}
for (String item : categoryRespList) {
CategoryResponseList category = new CategoryResponseList();
category.setCategoryRespList(item);
category.setRequestId(txnId);
category.setRequestId(requestId);
category.setCreateDateTime(new Date());
categoryResponseListRepository.save(category);
savedCount++;
}
return savedCount + " items were successfully saved.";
}
public String saveComplaintStatus(TicketCheckStatusResponse response, String reqId, String txnId) {
if (response == null || response.getTicketDetails() == null) {
throw new CustomException("Invalid response received!", "FAILURE", 500, "Response Null");
}
// ✔ Validate HTTP style statusCode
if (!"200".equals(response.getStatusCode())) {
throw new CustomException(response.getMessage(), response.getStatus(),
Integer.parseInt(response.getStatusCode()), response.getRemarks());
}
TicketDetailsDTO dto = response.getTicketDetails();
if (dto == null) {
throw new CustomException("Ticket details missing!", "FAILURE", 500, "Invalid Data");
}
// ✔ Check if already exists
// if (complaintStatusRepository.existsByTicketId(dto.getTicketId())) {
// return "Complaint already exists in DB. TicketId: " + dto.getTicketId();
// }
// ✔ Create entity
ComplainStatus entity = new ComplainStatus();
entity.setRefId(response.getRefId());
entity.setReqId(reqId);
entity.setTxnId(txnId);
entity.setTicketId(dto.getTicketId());
entity.setTicketNpciId(dto.getTicketNpciId());
entity.setTxnNpciId(dto.getTxnNpciId());
entity.setTicketType(dto.getTicketType());
entity.setTicketDespo(dto.getTicketDespo());
entity.setTicketDescr(dto.getTicketDescr());
entity.setStatus(dto.getStatus());
entity.setNpciTicketType(dto.getNpciTicketType());
entity.setNpciTicketStatus(dto.getNpciTicketStatus());
entity.setCreatedTime(dto.getCreatedTime());
entity.setCreateDateTime(new Date());
try {
ComplainStatus saved = complaintStatusRepository.save(entity);
return "Complaint saved successfully with ID: " + saved.getId();
} catch (Exception e) {
e.printStackTrace();
return "Error saving complaint: " + e.getMessage();
}
}
public String saveComplaintHistory(ComplaintHistoryStatusResponse responseWrapper, String reqId, String txnId) {
if (responseWrapper == null || responseWrapper.getTicketHistories() == null
|| responseWrapper.getTicketHistories().isEmpty()) {
return "No complaint history data found to save.";
}
try {
int savedCount = 0;
for (ComplaintHistoryDTO dto : responseWrapper.getTicketHistories()) {
ComplaintHistory entity = new ComplaintHistory();
entity.setHistoryId(dto.getHistoryId());
entity.setTicketId(dto.getTicketId());
entity.setTime(dto.getTime());
entity.setHistoryStatus(dto.getHistoryStatus());
entity.setReqId(reqId);
entity.setTxnId(txnId);
entity.setCreateDateTime(new Date());
complaintHistoryRepository.save(entity);
savedCount++;
}
return "✅ " + savedCount + " Complaint history record(s) saved successfully.";
} catch (Exception e) {
// Log the error for debugging
LOGGER.error("❌ Error saving complaint history data: {}", e.getMessage(), e);
return "❌ Failed to save complaint history data due to: " + e.getMessage();
}
}
public Map<String, Object> getComplaintStatusJson() {
Map<String, Object> complaints = new HashMap<>();
complaints.put("assignedTo", "XYZ Bank");
complaints.put("complaintDescr", "Testing complaint");
complaints.put("complaintDispos", "Transaction Successful, account not updated");
complaints.put("complaintId", "2dfecc3c-9114-4094-9aa8-ff26f03a9bfd");
complaints.put("complaintNpciId", "FE4123109489834");
complaints.put("complaintRaisedFrom", "agent");
complaints.put("complaintType", "npciComplaint");
complaints.put("createdEpochDate", 1681905485);
complaints.put("creationDate", "2023-04-19T17:28:05.508679Z");
complaints.put("modifiedDate", "2023-04-19T17:28:10.001432Z");
complaints.put("modifiedEpochDate", 1681905490);
complaints.put("refId", "mqcXfZs2XMkRKQfpIwoSYQDnIr231091728");
complaints.put("status", "COMPLAINT_OPEN_SUCCESS");
complaints.put("tenantId", "FE41");
complaints.put("txnId", "24fe190c-42c9-4cb5-9d36-965839b0cb5f");
complaints.put("txnNpciId", "FE4131090DAI26Y0mAxl");
complaints.put("userId", "FE41FE42BNK519012345");
complaints.put("agentId", "FE41FE42BNK519012345");
Map<String, Object> finalJson = new HashMap<>();
finalJson.put("complaints", complaints);
return finalJson;
}
public Map<String, Object> getDummyReopenComplaintJson() {
Map<String, Object> complaints = new HashMap<>();
complaints.put("assignedTo", "Federal Bank");
complaints.put("complaintDescr", "Issue not yet solved - reopening");
complaints.put("complaintDispos", "Transaction Successful, account not updated");
complaints.put("complaintId", "809ec4c7-d81b-4d2b-9c66-3011a6fae714");
complaints.put("complaintNpciId", "FE4123109776644");
complaints.put("complaintRaisedFrom", "agent");
complaints.put("complaintType", "npciComplaint");
complaints.put("createdEpochDate", 1681893776);
complaints.put("creationDate", "2023-04-19T14:12:56.329935Z");
complaints.put("modifiedDate", "2023-04-19T14:19:44.891423Z");
complaints.put("modifiedEpochDate", 1681894184);
complaints.put("refId", "7sQwBamvWFyLxhK84dYgGmV24r831091419");
complaints.put("status", "COMPLAINT_REOPEN_SUCCESS");
complaints.put("tenantId", "FE41");
complaints.put("txnId", "155a4a71-141f-40fd-b772-9d0e020963e8");
complaints.put("txnNpciId", "FE413045Hl3L90ZWg59o");
complaints.put("userId", "FE41FE42BNK519012345");
complaints.put("agentId", "FE41FE42BNK519012345");
Map<String, Object> finalJson = new HashMap<>();
finalJson.put("complaints", complaints);
return finalJson;
}
public Map<String, Object> getDummyClosureComplaintJson() {
Map<String, Object> complaints = new HashMap<>();
complaints.put("complaintDescr", "Closing complaint Issue resolved");
complaints.put("complaintDispos", "Transaction Successful, account not updated");
complaints.put("complaintId", "809ec4c7-d81b-4d2b-9c66-3011a6fae714");
complaints.put("complaintNpciId", "FE4123109776644");
complaints.put("complaintRaisedFrom", "agent");
complaints.put("complaintType", "npciComplaint");
complaints.put("createdEpochDate", 1681893776);
complaints.put("creationDate", "2023-04-19T14:12:56.329935Z");
complaints.put("modifiedDate", "2023-04-19T14:13:32.25255Z");
complaints.put("modifiedEpochDate", 1681893812);
complaints.put("refId", "hHfEKYZExIpxNEV65HMQZ2Lh0DA31091413");
complaints.put("status", "COMPLAINT_CLOSURE_REQUEST_SUCCESS"); // removed spaces
complaints.put("tenantId", "FE41");
complaints.put("txnId", "155a4a71-141f-40fd-b772-9d0e020963e8");
complaints.put("txnNpciId", "FE413045Hl3L90ZWg59o");
complaints.put("userId", "FE41FE42BNK519012345");
complaints.put("agentId", "FE41FE42BNK519012345");
Map<String, Object> finalJson = new HashMap<>();
finalJson.put("complaints", complaints);
return finalJson;
}
public String saveComplainStatusDetail(ViewComplainByIdResponse responseWrapper, String reqId, String txnId) {
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
// Map list → ComplaintStatusDetails list
List<ComplaintStatusDetails> entityList = responseWrapper.getTickets().stream()
.map(c -> mapper.map(c, ComplaintStatusDetails.class)).collect(Collectors.toList());
// Set extra fields
for (ComplaintStatusDetails entity : entityList) {
entity.setReqId(reqId);
entity.setSystemTxnId(txnId);
entity.setTotalPages(String.valueOf(responseWrapper.getTotalPages()));
entity.setTotalElements(String.valueOf(responseWrapper.getTotalElements()));
entity.setPage(String.valueOf(responseWrapper.getPage()));
entity.setPageSize(String.valueOf(responseWrapper.getPageSize()));
entity.setCreateDateTime(new Date());
}
// Save to repository
complainDetailRepository.saveAll(entityList);
return "SUCCESS";
}
public String saveAllComplaints(ViewAllComplaintsResponse responseWrapper, String reqId, String txnId) {
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
// Map list → ComplaintStatusDetails list
List<ComplaintStatusDetails> entityList = responseWrapper.getComplaintsList().stream()
.map(c -> mapper.map(c, ComplaintStatusDetails.class)).collect(Collectors.toList());
// Set extra fields
for (ComplaintStatusDetails entity : entityList) {
entity.setReqId(reqId);
entity.setTxnId(txnId);
}
// Save to repository
complainDetailRepository.saveAll(entityList);
return "SUCCESS";
}
// Payment Data Service Impl
public ClientReqIdResponse storeClientReqIdResponse(ClientReqIdResponse responseWrapper, String reqId,
String txnId) {
// 1. Save Parent (bill_id is PK)
CheckClientStatusReqId parent = new CheckClientStatusReqId();
parent.setBillId(responseWrapper.getBillId());
parent.setRefId(responseWrapper.getRefId());
parent.setBillerId(responseWrapper.getBillerId());
parent.setStatus(responseWrapper.getStatus());
parent.setActionType(responseWrapper.getActionType());
parent.setUserId(responseWrapper.getUserId());
parent.setCreationDate(responseWrapper.getCreationDate());
parent.setModifiedDate(responseWrapper.getModifiedDate());
parent.setCreatedEpochDate(responseWrapper.getCreatedEpochDate());
parent.setModifiedEpochDate(responseWrapper.getModifiedEpochDate());
parent.setReqId(reqId);
parent.setTxnId(txnId);
parent = clientReqIdRepository.save(parent);
// If no biller data, return
if (responseWrapper.getBillerResponse() == null) {
return responseWrapper;
}
CheckClientBillerResponseDTO dto = responseWrapper.getBillerResponse();
// 2. Save Biller Data
CheckClientBillerData billerData = new CheckClientBillerData();
billerData.setBillMaster(parent);
billerData.setAmount(dto.getAmount());
billerData.setBillDate(dto.getBillDate());
billerData.setBillNumber(dto.getBillNumber());
billerData.setBillPeriod(dto.getBillPeriod());
billerData.setCustConvFee(dto.getCustConvFee());
billerData.setCouCustConvFee(dto.getCouCustConvFee());
billerData.setDueDate(dto.getDueDate());
billerData.setCustomerName(dto.getCustomerName());
billerData.setAmountOption(dto.getAmountOption());
billerData = checkClientBillerDataRepository.save(billerData);
// 3. Save Tag List
List<BillTag> tagList = new ArrayList<>();
if (dto.getTagList() != null) {
for (Detail tagDto : dto.getTagList()) {
BillTag tag = new BillTag();
tag.setName(tagDto.getName());
tag.setValue(tagDto.getValue());
tag.setBillerResponse(billerData);
tagList.add(tag);
}
}
billerData.setTagList(tagList);
// 4. Save Additional Info
List<BillAdditionalInfo> addList = new ArrayList<>();
if (dto.getAdditionalInfo() != null) {
for (BillAdditionalInfoDTO ai : dto.getAdditionalInfo()) {
BillAdditionalInfo info = new BillAdditionalInfo();
info.setAdditionalInfoId(ai.getAdditionalInfoId());
info.setName(ai.getName());
info.setValue(ai.getValue());
info.setBillerResponse(billerData);
addList.add(info);
}
}
billerData.setAdditionalInfo(addList);
// 5. Save Customer Params
List<BillCustomerParams> cpList = new ArrayList<>();
if (dto.getCustomerParams() != null) {
for (BillCustomerParamsDTO cp : dto.getCustomerParams()) {
BillCustomerParams param = new BillCustomerParams();
param.setName(cp.getName());
param.setValue(cp.getValue());
param.setType(cp.getType());
param.setBillerResponse(billerData);
cpList.add(param);
}
}
billerData.setCustomerParams(cpList);
// Final Save for children cascade
checkClientBillerDataRepository.save(billerData);
return responseWrapper;
}
public String saveTicketDisoptionDetails(List<TicketDispositionsDTO> dispositionsList, String requestId,
String txnId) {
if (dispositionsList == null || dispositionsList.isEmpty()) {
return "No disposition data received.";
}
int savedCount = 0;
for (TicketDispositionsDTO dto : dispositionsList) {
// Check if already exists
boolean exists = ticketDispositionRepository.existsByDispositionId(dto.getDispositionId());
if (!exists) {
TicketDispositionDetails entity = new TicketDispositionDetails();
// Manual mapping (to avoid ModelMapper conflict)
entity.setDispositionId(dto.getDispositionId());
entity.setDispositionDescr(dto.getDispositionDescr());
entity.setTenantId(dto.getTenantId());
entity.setDispositionCode(dto.getDispositionCode());
// Set current datetime
entity.setCreateDateTime(new Date());
entity.setTxnId(txnId);
entity.setReqId(requestId);
ticketDispositionRepository.save(entity);
savedCount++;
}
}
if (savedCount > 0) {
return savedCount + " disposition(s) saved successfully.";
} else {
return "All dispositions already exist. No new records saved.";
}
}
public String saveAllComplaints(ViewAllComplaintsTicketsResponse responseWrapper, String reqId, String txnId) {
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
// Map list → ComplaintStatusDetails list
List<ViewAllComplaintTicketStatusDetails> entityList = responseWrapper.getTickets().stream()
.map(c -> mapper.map(c, ViewAllComplaintTicketStatusDetails.class)).collect(Collectors.toList());
// Set extra fields
for (ViewAllComplaintTicketStatusDetails entity : entityList) {
entity.setReqId(reqId);
entity.setSystemTxnId(txnId);
entity.setPage(String.valueOf(responseWrapper.getPage()));
entity.setPageSize(String.valueOf(responseWrapper.getPageSize()));
entity.setTotalPages(String.valueOf(responseWrapper.getTotalPages()));
entity.setTotalElements(String.valueOf(responseWrapper.getTotalElements()));
entity.setCreateDateTime(new Date());
}
// Save to repository
viewAllComplainTicketDetailRepository.saveAll(entityList);
return "SUCCESS";
}
public String saveContactMasterData(List<ContactMaster> contactMasterList, String requestId, String txnID) {
if (contactMasterList == null || contactMasterList.isEmpty()) {
return "No contact master data found";
}
// Convert DTO → Entity
List<ContactMasterData> entities = contactMasterList.stream().map(dto -> {
ContactMasterData entity = new ContactMasterData();
entity.setMobNo(dto.getMobNo());
entity.setReqId(requestId);
entity.setTxnId(txnID);
entity.setCreateDateTime(new Date());
return entity;
}).collect(Collectors.toList());
// Save list in DB
contactMasterDataRepository.saveAll(entities);
return "Contact Master Saved Successfully";
}
public void saveOrUpdateBillerData(BillerDetails newEntity) {
Optional<BillerDetails> existingOpt = billerDataRespository.findByBillerId(newEntity.getBillerId());
if (existingOpt.isPresent()) {
// logic to be written
} else {
// brand new biller
billerDataRespository.save(newEntity);
}
}
public void saveOrUpdateBillData(BillDataResponse responseWrapper,
BillFetchByBillIdRequest billFetchByBillIdRequest, String txnId) {
BillMaster billMaster = new BillMaster();
billMaster.setBillId(responseWrapper.getBillId());
billMaster.setRefId(responseWrapper.getRefId());
billMaster.setBillerId(responseWrapper.getBillerId());
billMaster.setStatus(responseWrapper.getStatus());
billMaster.setActionType(responseWrapper.getActionType());
billMaster.setUserId(responseWrapper.getUserId());
billMaster.setReqId(billFetchByBillIdRequest.getReqId());
billMaster.setTxnId(txnId);
// BILLER RESPONSE
BillerResponse billerResponse = new BillerResponse();
if (responseWrapper.getBillerResponse() != null) {
billerResponse.setBillId(responseWrapper.getBillerResponse().getBillId());
billerResponse.setAmount(responseWrapper.getBillerResponse().getAmount());
billerResponse.setBillDate(responseWrapper.getBillerResponse().getBillDate());
billerResponse.setBillNumber(responseWrapper.getBillerResponse().getBillNumber());
billerResponse.setBillPeriod(responseWrapper.getBillerResponse().getBillPeriod());
billerResponse.setCustConvFee(responseWrapper.getBillerResponse().getCustConvFee());
billerResponse.setCouCustConvFee(responseWrapper.getBillerResponse().getCouCustConvFee());
billerResponse.setDueDate(responseWrapper.getBillerResponse().getDueDate());
billerResponse.setCustomerName(responseWrapper.getBillerResponse().getCustomerName());
billerResponse.setAmountOption(responseWrapper.getBillerResponse().isAmountOption());
billerResponse.setBillMaster(billMaster);
}
billMaster.setBillerResponse(billerResponse);
// ADDITIONAL INFO
List<AdditionalInfoBill> additionalInfos = new ArrayList<>();
if (responseWrapper.getAdditionalInfo() != null) {
for (AdditionalInfoBillDTO info : responseWrapper.getAdditionalInfo()) {
AdditionalInfoBill entity = new AdditionalInfoBill();
entity.setName(info.getName());
entity.setValue(info.getValue());
entity.setBillMaster(billMaster);
additionalInfos.add(entity);
}
}
billMaster.setAdditionalInfo(additionalInfos);
// CUSTOMER PARAMS
List<CustomerParamBill> customerParams = new ArrayList<>();
if (responseWrapper.getCustomerParams() != null) {
for (CustomerParamDAO param : responseWrapper.getCustomerParams()) {
CustomerParamBill entity = new CustomerParamBill();
entity.setName(param.getName());
entity.setValue(param.getValue() == null ? "" : param.getValue());
entity.setType(param.getType() == null ? "" : param.getType());
entity.setBillMaster(billMaster);
customerParams.add(entity);
}
}
billMaster.setCustomerParams(customerParams);
// DATES
billMaster.setCreationDate(responseWrapper.getCreationDate());
billMaster.setModifiedDate(responseWrapper.getModifiedDate());
billMaster.setCreatedEpochDate(responseWrapper.getCreatedEpochDate());
billMaster.setModifiedEpochDate(responseWrapper.getModifiedEpochDate());
billMasterRepository.save(billMaster);
}
public void saveBillValidation(FetchBillValidateResponse responseWrapper, BillFetchByBillIdRequest billRequest) {
System.out.println("responseWrapper");
ModelMapper modelMapper = new ModelMapper();
try {
LOGGER.info("Saving bill validation data for billId={}", responseWrapper.getBillId());
// ✅ Map the main object
BillValidation billEntity = modelMapper.map(responseWrapper, BillValidation.class);
// ✅ Manually handle only the nested relationship part
if (responseWrapper.getCustomerParams() != null) {
List<CustomerParamBillValidation> customerParams = responseWrapper.getCustomerParams().stream()
.map(param -> {
CustomerParamBillValidation cp = modelMapper.map(param, CustomerParamBillValidation.class);
cp.setBill(billEntity); // maintain relation
return cp;
}).collect(Collectors.toList());
billEntity.setCustomerParams(customerParams);
}
if (responseWrapper.getAdditionalInfo() != null) {
List<AdditionalInfoBillValidate> additionalInfos = responseWrapper.getAdditionalInfo().stream()
.map(info -> {
AdditionalInfoBillValidate ai = modelMapper.map(info, AdditionalInfoBillValidate.class);
ai.setBill(billEntity); // maintain relation
return ai;
}).collect(Collectors.toList());
billEntity.setAdditionalInfo(additionalInfos);
}
// ✅ Save everything (cascade handles child)
billValidationRepository.save(billEntity);
LOGGER.info("Bill validation saved successfully for billId={}", billEntity.getBillId());
} catch (Exception ex) {
LOGGER.error("Error saving BillValidation data: {}", ex.getMessage(), ex);
throw new RuntimeException("Failed to save bill validation data", ex);
}
}
public FetchBillForwardRequest fetchBillForwardRequestMap(FetchBillRequest req) {
FetchBillForwardRequest forward = new FetchBillForwardRequest();
forward.setBillerId(req.getBillerId());
forward.setBillerName(req.getBillerName());
forward.setBillerCategory(req.getBillerCategory());
forward.setClientReqId(req.getClientReqId());
forward.setMacAdress(req.getMacAdress());
forward.setCustomerMobNo(req.getCustomerMobNo());
forward.setRemitterName(req.getRemitterName());
forward.setPaymentChannel(req.getPaymentChannel());
forward.setDeviceBlockTags(req.getDeviceBlockTags());
forward.setCustomerParamsRequest(req.getCustomerParamsRequest());
forward.setRemitterIdentity(req.getRemitterIdentity());
return forward;
}
public FetchBillValidatestForwardRequest fetchBillValidatestForwardRequest(FetchBillValidatestRequest req) {
FetchBillValidatestForwardRequest forward = new FetchBillValidatestForwardRequest();
forward.setBillerId(req.getBillerId());
forward.setBillerName(req.getBillerName());
forward.setBillerCategory(req.getBillerCategory());
forward.setCustomerMobNo(req.getCustomerMobNo());
forward.setClientReqId(req.getClientReqId());
forward.setMacAddress(req.getMacAddress());
forward.setCustomerParamsRequest(req.getCustomerParamsRequest());
return forward;
}
public BillPaymentForwardRequest billPaymentForwardRequest(BillPaymentRequest req) {
BillPaymentForwardRequest forward = new BillPaymentForwardRequest();
forward.setTenantId(req.getTenantId());
forward.setRefId(req.getRefId());
forward.setBillId(req.getBillId());
forward.setAmountOptions(req.isAmountOptions());
forward.setPaymentMode(req.getPaymentMode());
forward.setPaymentInformation(req.getPaymentInformation());
forward.setBillerId(req.getBillerId());
forward.setBillerName(req.getBillerName());
forward.setBillerCategory(req.getBillerCategory());
forward.setMacAdress(req.getMacAdress());
forward.setCustomerMobNo(req.getCustomerMobNo());
forward.setTxnAmount(req.getTxnAmount());
forward.setPaymentRefId(req.getPaymentRefId());
forward.setClientReqId(req.getClientReqId());
forward.setPaymentChannel(req.getPaymentChannel());
forward.setDeviceBlockTags(req.getDeviceBlockTags());
forward.setCustomerParamsRequest(req.getCustomerParamsRequest());
forward.setRemitterIdentity(req.getRemitterIdentity());
forward.setAmountBreakSet(req.getAmountBreakSet());
return forward;
}
public FetchComplaintForwardRequest fetchComplaintForwardRequestMap(ComplaintRequestDTO complainRequestDTO) {
FetchComplaintForwardRequest forward = new FetchComplaintForwardRequest();
forward.setTicketDescription(complainRequestDTO.getTicketDescription());
forward.setTicketDisposition(complainRequestDTO.getTicketDisposition());
forward.setTicketDispositionCode(complainRequestDTO.getTicketDispositionCode());
forward.setTxnId(complainRequestDTO.getTxnId());
return forward;
}
public FetchTicketLatestStatusRequest fetchTicketLatestStatusRequestMap(GetTicketLatestStausRequest request) {
FetchTicketLatestStatusRequest forward = new FetchTicketLatestStatusRequest();
forward.setTicketId(request.getTicketId());
forward.setTxnNpciId(request.getTxnNpciId());
return forward;
}
}