Untitled
unknown
plain_text
5 days ago
20 kB
2
Indexable
Never
@Component public class RSADocumentUploadHandler { private TravelOffersRepository travelOfferRepository; private HomeOffersRepository homeOffersRepository; private final MaktabaMapper maktabaMapper; private final GetEidImagesQueryHandler getEidImagesQueryHandler; private final UploadDocumentQueryHandler uploadDocumentQueryHandler; private final UploadHomeDocumentQueryHandler uploadHomeDocumentQueryHandler; private final CustomerDetailsAPIHandler customerDetailsAPIHandler; private final DocumentActionCompletedEventCommandHandler documentActionCompletedEventCommandHandler; private final DocumentActionFailedEventCommandHandler documentActionFailedEventCommandHandler; private final DocumentActionCompletedInternalEventCommandHandler documentActionCompletedInternalEventCommandHandler; @Autowired QuoteRepository quoteRepository; RSADocumentUploadHandler(TravelOffersRepository travelOfferRepository, HomeOffersRepository homeOffersRepository, CustomerDetailsAPIHandler customerDetailsAPIHandler, MaktabaMapper maktabaMapper, GetEidImagesQueryHandler getEidImagesQueryHandler, UploadDocumentQueryHandler uploadDocumentQueryHandler, DocumentActionCompletedEventCommandHandler documentActionCompletedEventCommandHandler, DocumentActionFailedEventCommandHandler documentActionFailedEventCommandHandler, DocumentActionCompletedInternalEventCommandHandler documentActionCompletedInternalEventCommandHandler, UploadHomeDocumentQueryHandler uploadHomeDocumentQueryHandler){ this.travelOfferRepository = travelOfferRepository; this.homeOffersRepository = homeOffersRepository; this.customerDetailsAPIHandler = customerDetailsAPIHandler; this.maktabaMapper = maktabaMapper; this.getEidImagesQueryHandler = getEidImagesQueryHandler; this.uploadDocumentQueryHandler = uploadDocumentQueryHandler; this.documentActionCompletedEventCommandHandler = documentActionCompletedEventCommandHandler; this.documentActionFailedEventCommandHandler = documentActionFailedEventCommandHandler; this.documentActionCompletedInternalEventCommandHandler = documentActionCompletedInternalEventCommandHandler; this.uploadHomeDocumentQueryHandler = uploadHomeDocumentQueryHandler; } public UploadDocumentResponse proceesRSADocumentUpload(DocumentUploadRequest documentUploadRequest, RequestContext context){ String applicaionId = documentUploadRequest.getApplicationId(); String channelId = context.getChannelId(); String customerId = ""; UploadDocumentResponse res = null; log.info("EIDA upload to RSA start with applicationId {}", applicaionId); Optional<QuoteEntity> quote = quoteRepository.findByQuotationIdAndApplicationId(documentUploadRequest.getQuotationId(),applicaionId); try { customerId = getCustomerId(documentUploadRequest); if(quote.isPresent() && Boolean.TRUE.equals(quote.get().getIsDocUploaded())) return validateDocUpload(documentUploadRequest,customerId,context); Integer quotationId = documentUploadRequest.getQuotationId().intValue(); QueryResult<CustomerDetailsResponse> customerDetailsResponse = customerDetailsAPIHandler.handleDecorators(CustomerDetailsQuery.builder().id(customerId).build(), context); log.info("Called Finacle to get CustomerDetailsResponse with applicationId {}", applicaionId); List<GovIssuedIdentities> g = customerDetailsResponse.getResult().getGovIssuedIdentities(); Optional<GovIssuedIdentities> eida = g.stream().filter(e -> !e.getId().isEmpty() && e.getType().equalsIgnoreCase("EIDA")).findFirst(); GetMaktabaEIDAImagesQuery getMaktabaEIDAImagesQuery = maktabaMapper.buildGetEidNameByEIDANumber(eida.isPresent() ? eida.get().getNumber() : "", channelId); log.info("get all policies for customer id :: {} and customer id type :: {}", customerId); QueryResult<MaktabaGetResults> maktabaResult = getEidImagesQueryHandler.handleDecorators(getMaktabaEIDAImagesQuery, context); log.info("Called Maktaba search api to get EIDA front/back file name with applicationId {}", applicaionId); Optional<MaktabaResult> eidFront = maktabaResult.getResult().getResults().stream().filter(result -> result.getFilename().startsWith(InsuranceFulfillmentConstants.EIDA_FRONT) || result.getFilename().startsWith(InsuranceFulfillmentConstants.FRONT)) .sorted(Comparator.comparingLong(MaktabaResult::getUpdatedAt)).findFirst(); Optional<MaktabaResult> eidBack = maktabaResult.getResult().getResults().stream().filter(result -> result.getFilename().startsWith(InsuranceFulfillmentConstants.EIDA_BACK) || result.getFilename().startsWith(InsuranceFulfillmentConstants.BACK)) .sorted(Comparator.comparingLong(MaktabaResult::getUpdatedAt)).findFirst(); if (!hasMaktabaBothEidFrontAndBackImages(eidFront, eidBack)) { throw new FrameworkException(InsuranceFulfillmentConstants.MSG.EID_FRONT_BACK_MISSING, 400, customerDetailsResponse.getResult().getId()); } GetMaktabaEIDAImagesQuery downloadEidFrontImageQuery = maktabaMapper.buildDownLoadEidImage(eidFront.isPresent() ? eidFront.get().getFilename() : "", channelId); GetMaktabaEIDAImagesQuery downloadEidBackImageQuery = maktabaMapper.buildDownLoadEidImage(eidBack.isPresent() ? eidBack.get().getFilename(): "", channelId); QueryResult<MaktabaGetResults> eidFrontImage = getEidImagesQueryHandler.handleDecorators(downloadEidFrontImageQuery, context); QueryResult<MaktabaGetResults> eidBackImage = getEidImagesQueryHandler.handleDecorators(downloadEidBackImageQuery, context); log.info("Download successfully EIDA front/back document with applicationId {}", applicaionId); if (isDownloadEndImagesSuccessful(eidFrontImage, eidBackImage)) { throw new FrameworkException(InsuranceFulfillmentConstants.MSG.EID_FRONT_BACK_NOT_FOUND, 400, customerDetailsResponse.getResult().getId()); } DocumentContent documentFrontContent = getFrontDocumentContent(quotationId, eidFront, eidFrontImage); DocumentContent documentBackContent = getBackDocumentContent(quotationId, eidBack, eidBackImage); String frontId = getDocumentId(quotationId, eidFront,"eida-front"); String backId = getDocumentId(quotationId, eidFront,"eida-back"); String frontAndBackId = frontId+","+backId; log.info("Calling RSA to upload EIDA front/back document with applicationId {}", applicaionId); res = separateTravelHomeDocUpload( context,documentUploadRequest,documentFrontContent,documentBackContent, quote,frontAndBackId,customerId); log.info("EIDA upload to RSA completed successfully with applicationId {}", documentUploadRequest.getApplicationId()); }catch(Exception e){ DocumentActionFailedEventCommand documentActionFailedEventCommand = perpareDocumentActionFailedEventCommand(documentUploadRequest,customerId); List<Failure> failureList = new ArrayList<>(); Failure failure = new Failure(); failure.setMessage(e.getMessage()); failureList.add(failure); documentActionFailedEventCommand.setError(failureList); documentActionFailedEventCommandHandler.handleDecorators(documentActionFailedEventCommand,context); log.error("Unable to complete EIDA upload to RSA {} ", e.getMessage()); } return res; } private void travelEventPublish(DocumentUploadRequest documentUploadRequest, RequestContext context, String customerId, UploadDocumentResponse res,HomeUploadDocumentResponse homeRes ,Optional<QuoteEntity> quote, String frontAndBackId) { if((TRAVEL_PRODUCT_CODE.equalsIgnoreCase(documentUploadRequest.getProductCode()) && res !=null && res.getErrorsList()==null && res.getEmIdDocFrontFlag().equalsIgnoreCase("true") && res.getEmIdDocBackFlag().equalsIgnoreCase("true")) || (HOME_PRODUCT_CODE.equalsIgnoreCase(documentUploadRequest.getProductCode()) && res !=null && res.getErrorsList()==null && res.getEmIdDocFrontFlag().equalsIgnoreCase("true") && res.getEmIdDocBackFlag().equalsIgnoreCase("true"))){ DocumentActionCompletedEventCommand actionCompletedEvent = prepareDocumentActionCompletedEvent(documentUploadRequest, customerId,"RSA", frontAndBackId); DocumentActionCompletedInternalEventCommand actionCompletedInternalEvent = prepareDocumentActionCompletedInternatEvent(documentUploadRequest, customerId,"RSA", frontAndBackId); CommandResult internalCommandResult = documentActionCompletedEventCommandHandler.handleDecorators(actionCompletedEvent, context); CommandResult externalCommandResult = documentActionCompletedInternalEventCommandHandler.handleDecorators(actionCompletedInternalEvent, context); if(internalCommandResult.isSuccess() && externalCommandResult.isSuccess() ){ log.info("DocumentActionFailed event triggered with {} ",actionCompletedEvent.getCustomerId()); if(quote.isPresent()) { quote.get().setIsDocUploaded(true); quoteRepository.save(quote.get()); } } }else{ publishRSADocumentUploadFailureEvent(documentUploadRequest, res,homeRes, customerId, context); } } private String getCustomerId(DocumentUploadRequest documentUploadRequest) throws FrameworkException { String customerId = ""; if(TRAVEL_PRODUCT_CODE.equalsIgnoreCase(documentUploadRequest.getProductCode())) { List<TravelOffersEntity> optionalTravelOffersEntity = travelOfferRepository.findByApplicationIdAndOriginationId(documentUploadRequest.getApplicationId(), documentUploadRequest.getOriginationId()); if (optionalTravelOffersEntity.isEmpty()) { throw new FrameworkException(InsuranceFulfillmentConstants.MSG.CUSTOMER_DETAILS_NOT_AVAILABLE, 400); } customerId = optionalTravelOffersEntity.get(0).getCustomerId(); } else if(HOME_PRODUCT_CODE.equalsIgnoreCase(documentUploadRequest.getProductCode())) { List<HomeOffersEntity> optionalHomeOffersEntity = homeOffersRepository.findByApplicationIdAndOriginationId(documentUploadRequest.getApplicationId(), documentUploadRequest.getOriginationId()); if(optionalHomeOffersEntity.isEmpty()) { throw new FrameworkException(InsuranceFulfillmentConstants.MSG.CUSTOMER_DETAILS_NOT_AVAILABLE, 400); } customerId = optionalHomeOffersEntity.get(0).getCustomerId(); } return customerId; } private UploadDocumentResponse separateTravelHomeDocUpload( RequestContext context, DocumentUploadRequest documentUploadRequest, DocumentContent documentFrontContent,DocumentContent documentBackContent, Optional<QuoteEntity> quote,String frontAndBackId,String customerId) { UploadDocumentResponse res = null; HomeUploadDocumentResponse homeResponse = null; if(TRAVEL_PRODUCT_CODE.equalsIgnoreCase(documentUploadRequest.getProductCode())) { UploadDocumentRSARequest uploadFrontDocumentRSARequest = UploadDocumentRSARequest.builder().documentContent(documentFrontContent).build(); UploadDocumentRSARequest uploadBackDocumentRSARequest = UploadDocumentRSARequest.builder().documentContent(documentBackContent).build(); uploadDocumentQueryHandler.handleDecorators(uploadFrontDocumentRSARequest, context); res = uploadDocumentQueryHandler.handleDecorators(uploadBackDocumentRSARequest, context).getResult(); travelEventPublish(documentUploadRequest, context, customerId, res, homeResponse,quote, frontAndBackId); } else if (HOME_PRODUCT_CODE.equalsIgnoreCase(documentUploadRequest.getProductCode())) { HomeUploadDocumentRSARequest homeUploadFrontDocumentRSARequest = HomeUploadDocumentRSARequest.builder().documentContent(documentFrontContent).build(); HomeUploadDocumentRSARequest homeUploadBackDocumentRSARequest = HomeUploadDocumentRSARequest.builder().documentContent(documentBackContent).build(); uploadHomeDocumentQueryHandler.handleDecorators(homeUploadFrontDocumentRSARequest, context); homeResponse = uploadHomeDocumentQueryHandler.handleDecorators(homeUploadBackDocumentRSARequest, context).getResult(); travelEventPublish(documentUploadRequest, context, customerId, res, homeResponse,quote, frontAndBackId); } return res; } private UploadDocumentResponse validateDocUpload(DocumentUploadRequest documentUploadRequest, String customerId,RequestContext context) { UploadDocumentResponse uploadDocumentResponse = null; prepareDocumentActionCompletedEvent(documentUploadRequest,customerId,"RSA","front/back already uploaded"); DocumentActionCompletedInternalEventCommand actionCompletedInternalEvent = prepareDocumentActionCompletedInternatEvent(documentUploadRequest,customerId,"RSA",""); documentActionCompletedInternalEventCommandHandler.handleDecorators(actionCompletedInternalEvent,context); return uploadDocumentResponse; } private static String getfileNameExtension(Optional<MaktabaResult> eidFront) { char[] charArray = new char[1080]; if(eidFront.isPresent()) charArray = eidFront.get().getFilename().toCharArray(); boolean flag = false; StringBuilder v = new StringBuilder(); for(char c:charArray){ if(c=='.'){ flag = true; } if(flag) v.append(c); } return v.toString().substring(1); } private DocumentActionCompletedInternalEventCommand prepareDocumentActionCompletedInternatEvent(DocumentUploadRequest documentUploadRequest, String customerId, String source,String frontAndBackId) { Document document = Document.builder() .id(frontAndBackId) .type("EIDA") .page("front and back") .source(source) .build(); return DocumentActionCompletedInternalEventCommand.builder() .applicationId(documentUploadRequest.getApplicationId()) .originationId(documentUploadRequest.getOriginationId()) .customerId(customerId) .productCode(documentUploadRequest.getProductCode()) .productType(documentUploadRequest.getProductType()) .quotationId(documentUploadRequest.getQuotationId()) .document(document) .status("uploaded") .build(); } private void publishRSADocumentUploadFailureEvent(DocumentUploadRequest documentUploadRequest, UploadDocumentResponse res,HomeUploadDocumentResponse homeres,String customerId,RequestContext context) { List<RSAError> rsaErrorList = null; if(res!=null) rsaErrorList = res.getErrorsList(); else if (homeres!=null) { rsaErrorList = homeres.getErrorsList(); } Failure failure = null; List<Failure> failueList = new ArrayList<>(); for(RSAError failur:rsaErrorList){ failure= new Failure(); failure.setCode(failur.getCode()); failure.setMessage(failur.getMessage()); failueList.add(failure); } DocumentActionFailedEventCommand documentActionFailedEventCommand = perpareDocumentActionFailedEventCommand(documentUploadRequest,customerId); documentActionFailedEventCommand.setError(failueList); documentActionFailedEventCommandHandler.handleDecorators(documentActionFailedEventCommand,context); } private DocumentActionFailedEventCommand perpareDocumentActionFailedEventCommand(DocumentUploadRequest documentUploadRequest,String customerId) { return DocumentActionFailedEventCommand.builder() .applicationId(documentUploadRequest.getApplicationId()) .originationId(documentUploadRequest.getOriginationId()) .customerId(customerId) .productCode(documentUploadRequest.getProductCode()) .productType(documentUploadRequest.getProductType()) .quotationId(documentUploadRequest.getQuotationId()) .build(); } private DocumentActionCompletedEventCommand prepareDocumentActionCompletedEvent(DocumentUploadRequest documentUploadRequest,String customerId,String source,String frontAndBackId) { Document document = Document.builder() .id(frontAndBackId) .type("EIDA") .page("front and back") .source(source) .build(); return DocumentActionCompletedEventCommand.builder() .applicationId(documentUploadRequest.getApplicationId()) .originationId(documentUploadRequest.getOriginationId()) .customerId(customerId) .productCode(documentUploadRequest.getProductCode()) .productType(documentUploadRequest.getProductType()) .quotationId(documentUploadRequest.getQuotationId()) .document(document) .status("uploaded") .build(); } private boolean hasMaktabaBothEidFrontAndBackImages(Optional<MaktabaResult> eidFront, Optional<MaktabaResult> eidBack) { return eidFront.isPresent() && eidBack.isPresent(); } private boolean isDownloadEndImagesSuccessful(QueryResult<MaktabaGetResults> eidFrontImage, QueryResult<MaktabaGetResults> eidBackImage) { return eidFrontImage.getResult().isNotFound() && eidBackImage.getResult().isNotFound(); } private static String getfileExtension(Optional<MaktabaResult> eidFront){ return getfileNameExtension(eidFront); } private String getDocumentId(Integer quotationId,Optional<MaktabaResult> eidFront,String side){ return side+"_"+quotationId+"."+getfileExtension(eidFront); } private static DocumentContent getFrontDocumentContent(Integer quotationId,Optional<MaktabaResult> eidFront, QueryResult<MaktabaGetResults> eidFrontImage) { return DocumentContent.builder() .documentCode(1) .fileName(InsuranceFulfillmentConstants.EIDA_FRONT+"_"+quotationId+"."+getfileExtension(eidFront)) .content(eidFrontImage.getResult().getBinaryImage()) .quotationNo(quotationId) .name("EmirateDocument") .build(); } private static DocumentContent getBackDocumentContent(Integer quotationId, Optional<MaktabaResult> eidFront, QueryResult<MaktabaGetResults> eidFrontImage) { return DocumentContent.builder() .documentCode(2) .fileName(InsuranceFulfillmentConstants.EIDA_BACK+"_"+quotationId+"."+getfileExtension(eidFront)) .content(eidFrontImage.getResult().getBinaryImage()) .quotationNo(quotationId) .name("EmirateDocument") .build(); } }
Leave a Comment