solved duplicate key problem on getBillerData

This commit is contained in:
Lincpay Tech
2025-11-05 13:58:53 +05:30
parent 1e7b3c8a6b
commit d18caaeed9
6 changed files with 71 additions and 38 deletions
@@ -72,7 +72,6 @@ public class MasterDataController {
return ResponseEntity.ok(masterDataService.getCustomerParamByBillerId(customerParamRequest));
}
/**
* Get All Master Data .The parameter is different from the data we are getting
* in the MasterRequest
@@ -28,8 +28,7 @@ public class BillerDetails {
@NotBlank(message = "Biller name cannot be blank")
private String billerName;
@Column(name = "biller_alias_name", nullable = false)
@NotBlank(message = "Biller alias name cannot be blank")
@Column(name = "biller_alias_name")
private String billerAliasName;
@Column(name = "biller_category_name", nullable = false)
@@ -43,39 +42,36 @@ public class BillerDetails {
@Column(name = "biller_accepts_adhoc", nullable = false)
private boolean billerAcceptsAdhoc;
@Column(name = "parent_biller", nullable = false)
@Column(name = "parent_biller")
private boolean parentBiller;
@Column(name = "parent_biller_id", nullable = false)
@NotBlank(message = "Parent biller ID cannot be blank")
@Column(name = "parent_biller_id")
private String parentBillerId;
@Column(name = "biller_ownership", nullable = false)
@Column(name = "biller_ownership")
@NotBlank(message = "Biller ownership is required (PSU or Private)")
private String billerOwnerShp;
@Column(name = "biller_coverage", nullable = false)
@NotBlank(message = "Biller coverage cannot be blank")
@Column(name = "biller_coverage")
private String billerCoverage;
@Column(name = "fetch_requirement", nullable = false)
@Column(name = "fetch_requirement")
@NotBlank(message = "Fetch requirement is mandatory")
private String fetchRequirement;
@Column(name = "payment_amount_exactness", nullable = false)
@Column(name = "payment_amount_exactness")
@NotBlank(message = "Payment amount exactness is required")
private String paymentAmountExactness;
@Column(name = "support_bill_validation", nullable = false)
@Column(name = "support_bill_validation")
@NotBlank(message = "Support bill validation is required")
private String supportBillValidation;
@Column(name = "biller_effctv_from", nullable = false)
@Column(name = "biller_effctv_from")
@NotBlank(message = "Effective from date is required")
private String billerEffctvFrom;
@Column(name = "biller_effctv_to", nullable = false)
@NotBlank(message = "Effective to date is required")
@Column(name = "biller_effctv_to")
private String billerEffctvTo;
@Column(name = "biller_temp_deactivation_start")
@@ -84,32 +80,25 @@ public class BillerDetails {
@Column(name = "biller_temp_deactivation_end")
private String billerTempDeactivationEnd;
@Column(name = "biller_description", nullable = false, length = 500)
@NotBlank(message = "Biller description cannot be blank")
@Column(name = "biller_description", length = 500)
private String billerDescription;
@Column(name = "biller_response_type", nullable = false)
@NotBlank(message = "Biller response type is required (SINGLE or SELECTIVE)")
@Column(name = "biller_response_type")
private String billerResponseType;
@Column(name = "plan_mdm_requirement", nullable = false)
@NotBlank(message = "Plan MDM requirement cannot be blank")
@Column(name = "plan_mdm_requirement")
private String planMDMRequirement;
@Column(name = "status", nullable = false)
@NotBlank(message = "Status must be ACTIVE or INACTIVE")
@Column(name = "status")
private String status;
@Column(name = "support_deemed", nullable = false)
@NotBlank(message = "Support deemed value is required (Yes/No)")
@Column(name = "support_deemed")
private String supportDeemed;
@Column(name = "support_pending_status", nullable = false)
@NotBlank(message = "Support pending status is required (Yes/No)")
@Column(name = "support_pending_status")
private String supportPendingStatus;
@Column(name = "biller_time_out", nullable = false)
@NotBlank(message = "Biller timeout is required")
@Column(name = "biller_time_out")
private String billerTimeOut;
@Column(name = "route_url")
@@ -151,7 +140,6 @@ public class BillerDetails {
* @OneToMany(mappedBy = "biller", cascade = CascadeType.ALL, orphanRemoval =
* true) private List<AdditionalInfo> billerAdditionalInfoPayments;
*/
@OneToMany(mappedBy = "biller", cascade = CascadeType.ALL, orphanRemoval = true)
private List<InterchangeFeeConf> interchangeFeeConves;
@@ -24,11 +24,9 @@ import lombok.ToString;
@Table(name = "biller_payment_modes")
@ToString
public class PaymentMode {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id //
@Column(name = "payment_model_limit_id", unique = true, length = 50)
@NotBlank(message = "Payment model limit ID cannot be blank")
private String paymentModelLimitId;
@@ -7,9 +7,11 @@ import org.springframework.stereotype.Repository;
import com.billpaymentservice.bbps.entites.BillerDetails;
import java.util.List;
import java.util.Optional;
@Repository
public interface BillerDataRepository extends JpaRepository<BillerDetails, String> {
Optional<BillerDetails> findByBillerId(String billerId);
}
@@ -4,6 +4,7 @@ package com.billpaymentservice.bbps.serviceImpl;
import java.time.LocalDateTime;
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;
@@ -202,6 +203,7 @@ public class MasterDataServiceImpl implements MasterDataService {
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
BillerDetails entity = mapper.map(responseWrapper.getBillerDAO(), BillerDetails.class);
EntityRelationshipUtil.linkBillerRelationships(entity);
helper.saveOrUpdateBillerData(entity);
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
@@ -2,6 +2,7 @@ package com.billpaymentservice.bbps.util;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.function.Function;
import org.slf4j.Logger;
@@ -12,12 +13,16 @@ import com.billpaymentservice.bbps.dto.ResponseModel;
import com.billpaymentservice.bbps.entites.BBPSMastersData;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.repository.BBPSMasterDataRepo;
import com.billpaymentservice.bbps.repository.BillerDataRepository;
import com.billpaymentservice.bbps.repository.InboundTraceLogRepository;
import com.billpaymentservice.bbps.dto.request.BillerDetailRequest;
import com.billpaymentservice.bbps.dto.request.MerchantRequestModel;
import com.billpaymentservice.bbps.entites.BBPSMerchantValidation;
import com.billpaymentservice.bbps.entites.BillerDetails;
import com.billpaymentservice.bbps.entites.InboundTraceLog;
import com.billpaymentservice.bbps.entites.OutboundTraceLog;
import com.billpaymentservice.bbps.entites.PaymentChannel;
import com.billpaymentservice.bbps.entites.PaymentMode;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.repository.MerchantRepository;
import com.billpaymentservice.bbps.repository.OutboundTraceLogRepository;
@@ -36,13 +41,15 @@ public class Helper {
private final ObjectMapper objectMapper = new ObjectMapper();
private final BBPSMasterDataRepo bbpsMasterDataRepo;
private final BillerDataRepository billerDataRespository;
// Constructor Injection
public Helper(BBPSMasterDataRepo bbpsMasterDataRepo, InboundTraceLogRepository inboundTraceLogRepository,
OutboundTraceLogRepository outboundTraceLogRepository) {
OutboundTraceLogRepository outboundTraceLogRepository,BillerDataRepository billerDataRepository) {
this.bbpsMasterDataRepo = bbpsMasterDataRepo;
this.inboundTraceLogRepository = inboundTraceLogRepository;
this.outboundTraceLogRepository = outboundTraceLogRepository;
this.billerDataRespository=billerDataRepository;
}
/**
@@ -182,7 +189,44 @@ public class Helper {
outboundLog.setResponseReceiveTime(LocalDateTime.now());
outboundTraceLogRepository.save(outboundLog);
}
/* ---------- End helpers ---------- */
public void saveOrUpdateBillerData(BillerDetails newEntity) {
Optional<BillerDetails> existingOpt = billerDataRespository.findByBillerId(newEntity.getBillerId());
if (existingOpt.isPresent()) {
BillerDetails existing = existingOpt.get();
// Update base biller fields
existing.setBillerName(newEntity.getBillerName());
existing.setBillerAliasName(newEntity.getBillerAliasName());
existing.setBillerDescription(newEntity.getBillerDescription());
existing.setStatus(newEntity.getStatus());
existing.setBillerCoverage(newEntity.getBillerCoverage());
// ... (update other simple fields as needed)
// ✅ Clear and replace nested lists to avoid duplicate inserts
existing.getBillerPaymentModes().clear();
if (newEntity.getBillerPaymentModes() != null) {
for (PaymentMode mode : newEntity.getBillerPaymentModes()) {
mode.setBiller(existing);
existing.getBillerPaymentModes().add(mode);
}
}
existing.getBillerPaymentChannels().clear();
if (newEntity.getBillerPaymentChannels() != null) {
for (PaymentChannel channel : newEntity.getBillerPaymentChannels()) {
channel.setBiller(existing);
existing.getBillerPaymentChannels().add(channel);
}
}
billerDataRespository.save(existing);
}
else {
// brand new biller
billerDataRespository.save(newEntity);
}
}
}