Resolved conflicts
This commit is contained in:
@@ -654,12 +654,19 @@ public class ApplicationAmendmentRequestDao {
|
||||
log.info(" Application amendment deleted with ID: {}", id);
|
||||
}
|
||||
|
||||
public ApplicationAmendmentRequestResponse getApplicationAmendmentRequestById(Long id) {
|
||||
public ApplicationAmendmentRequestResponseBean getApplicationAmendmentRequestById(Long id) {
|
||||
log.info("Fetching application amendment with ID: {}", id);
|
||||
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity = validateApplicationAmendmentRequest(id);
|
||||
ApplicationAmendmentRequestResponse response = convertEntityToResponse(applicationAmendmentRequestEntity,true);
|
||||
log.info("Application Amendment fetched successfully by ID: {}", response);
|
||||
return response;
|
||||
ApplicationAmendmentRequestResponse sourceResponse = convertEntityToResponse(applicationAmendmentRequestEntity,true);
|
||||
ApplicationAmendmentRequestResponseBean targetResponse = Utils.convertSourceObjectToDestinationObject(
|
||||
sourceResponse, ApplicationAmendmentRequestResponseBean.class
|
||||
);
|
||||
|
||||
if (targetResponse != null) {
|
||||
targetResponse.setEmailSendResponse(applicationAmendmentRequestEntity.getEmailSendResponse());
|
||||
}
|
||||
log.info("Application Amendment fetched successfully by ID: {}", targetResponse);
|
||||
return targetResponse;
|
||||
}
|
||||
|
||||
public List<GetAllAmendmentResponseBean> getAllApplicationAmendmentRequest(HttpServletRequest request, Long userId) {
|
||||
|
||||
@@ -176,6 +176,12 @@ public class AppointmentDao {
|
||||
}
|
||||
|
||||
private HubEntity loginToOdessa(HubEntity hub, ApplicationEntity application) {
|
||||
|
||||
int maxRetries = 3;
|
||||
int attempt = 0;
|
||||
boolean success = false;
|
||||
while (attempt < maxRetries && !success) {
|
||||
attempt++;
|
||||
try {
|
||||
//code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call
|
||||
String authJwtToken = Utils.generateAuthTokenForLoginToOdessa();
|
||||
@@ -195,15 +201,15 @@ public class AppointmentDao {
|
||||
hub.setAreaCode(parsedResponse.getAreaCode());
|
||||
hubRepository.save(hub);
|
||||
log.info("Saved new authToken and areaCode for Hub.");
|
||||
success = true;
|
||||
return hub;
|
||||
} else {
|
||||
throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again.");
|
||||
}
|
||||
}
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN));
|
||||
}
|
||||
catch (FeignException.Forbidden forbiddenException) {
|
||||
logForbiddenError();
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
log.error("Failed to login to odessa due to some error");
|
||||
|
||||
// Extract raw response body
|
||||
String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response
|
||||
@@ -239,15 +245,12 @@ public class AppointmentDao {
|
||||
} catch (IOException e) {
|
||||
log.error("Error parsing JSON response: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// Regenerate the token and retry
|
||||
loginToOdessa(hub, application);
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e);
|
||||
throw new RuntimeException("Authentication failed on Odessa. try again", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void startAsyncNdgProcessing(Long applicationId) {
|
||||
@@ -423,76 +426,80 @@ public class AppointmentDao {
|
||||
|
||||
private HubEntity authenticateAndSaveToken(HubEntity hub) {
|
||||
|
||||
try {
|
||||
//code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call
|
||||
String authJwtToken = Utils.generateAuthTokenForLoginToOdessa();
|
||||
log.info("Got the auth for login to odessa {}", authJwtToken);
|
||||
hub.setAuthToken(authJwtToken);
|
||||
hubRepository.save(hub);
|
||||
// Prepare the request body (adjust if necessary for login API)
|
||||
Map<String, Object> body = Collections.emptyMap();
|
||||
// Perform login API call
|
||||
ResponseEntity<Object> responseLogin = appointmentApiService.loginWithOdessa(authJwtToken, source, context, user, password, body);
|
||||
|
||||
// Handle successful login
|
||||
if (responseLogin.getStatusCode() == HttpStatus.OK) {
|
||||
log.info("Login successful to odessa. Parsing response.");
|
||||
String loginResponseJson = Utils.convertObjectToJson(responseLogin.getBody());
|
||||
AppointmentLoginResponse parsedResponse = parseLoginResponse(loginResponseJson);
|
||||
|
||||
// Validate and save token
|
||||
if (parsedResponse.getTokenId() != null) {
|
||||
hub.setAppointmentAuthTokenId(parsedResponse.getTokenId());
|
||||
hub.setAreaCode(parsedResponse.getAreaCode());
|
||||
hubRepository.save(hub);
|
||||
|
||||
log.info("Saved new authToken and areaCode for Hub.");
|
||||
return hub;
|
||||
} else {
|
||||
throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again.");
|
||||
}
|
||||
}
|
||||
// Handle non-OK response
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN));
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
logForbiddenError();
|
||||
|
||||
// Extract raw response body
|
||||
String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response
|
||||
|
||||
// Parse JSON to check for "PasswordExpired"
|
||||
int maxRetries = 3;
|
||||
int attempt = 0;
|
||||
boolean success = false;
|
||||
while (attempt < maxRetries && !success) {
|
||||
attempt++;
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode rootNode = objectMapper.readTree(responseBody);
|
||||
JsonNode errorsNode = rootNode.path("errors");
|
||||
//code to generate token with payload having "iat" epoch timestamp and secret key with no expiry and send in below method call
|
||||
String authJwtToken = Utils.generateAuthTokenForLoginToOdessa();
|
||||
log.info("Got the auth for login to odessa {}", authJwtToken);
|
||||
hub.setAuthToken(authJwtToken);
|
||||
hubRepository.save(hub);
|
||||
// Prepare the request body (adjust if necessary for login API)
|
||||
Map<String, Object> body = Collections.emptyMap();
|
||||
// Perform login API call
|
||||
ResponseEntity<Object> responseLogin = appointmentApiService.loginWithOdessa(authJwtToken, source, context, user, password, body);
|
||||
|
||||
if (errorsNode.isArray()) {
|
||||
for (JsonNode error : errorsNode) {
|
||||
// Check the main errorCode
|
||||
if (GepafinConstant.PASSWORD_EXPIRED.equals(error.path("errorCode").asText())) {
|
||||
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
||||
}
|
||||
// Handle successful login
|
||||
if (responseLogin.getStatusCode() == HttpStatus.OK) {
|
||||
log.info("Login successful to odessa. Parsing response.");
|
||||
String loginResponseJson = Utils.convertObjectToJson(responseLogin.getBody());
|
||||
AppointmentLoginResponse parsedResponse = parseLoginResponse(loginResponseJson);
|
||||
|
||||
// Check inside "subErrors"
|
||||
JsonNode subErrorsNode = error.path("subErrors");
|
||||
if (subErrorsNode.isArray()) {
|
||||
for (JsonNode subError : subErrorsNode) {
|
||||
if (GepafinConstant.PASSWORD_EXPIRED.equals(subError.path("errorCode").asText())) {
|
||||
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
||||
// Validate and save token
|
||||
if (parsedResponse.getTokenId() != null) {
|
||||
hub.setAppointmentAuthTokenId(parsedResponse.getTokenId());
|
||||
hub.setAreaCode(parsedResponse.getAreaCode());
|
||||
hubRepository.save(hub);
|
||||
|
||||
log.info("Saved new authToken and areaCode for Hub.");
|
||||
success = true;
|
||||
return hub;
|
||||
} else {
|
||||
throw new RuntimeException("Login response is missing a valid tokenId for login to odessa system, please try again.");
|
||||
}
|
||||
}
|
||||
// Handle non-OK response
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.ERROR_IN_GENERATING_NDG_TRY_AGAIN));
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
log.error("Failed to login to odessa due to some error occurred.");
|
||||
|
||||
// Extract raw response body
|
||||
String responseBody = forbiddenException.contentUTF8(); // Extract raw JSON response
|
||||
|
||||
// Parse JSON to check for "PasswordExpired"
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode rootNode = objectMapper.readTree(responseBody);
|
||||
JsonNode errorsNode = rootNode.path("errors");
|
||||
|
||||
if (errorsNode.isArray()) {
|
||||
for (JsonNode error : errorsNode) {
|
||||
// Check the main errorCode
|
||||
if (GepafinConstant.PASSWORD_EXPIRED.equals(error.path("errorCode").asText())) {
|
||||
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
||||
}
|
||||
|
||||
// Check inside "subErrors"
|
||||
JsonNode subErrorsNode = error.path("subErrors");
|
||||
if (subErrorsNode.isArray()) {
|
||||
for (JsonNode subError : subErrorsNode) {
|
||||
if (GepafinConstant.PASSWORD_EXPIRED.equals(subError.path("errorCode").asText())) {
|
||||
throw new CustomValidationException(Status.FORBIDDEN, Translator.toLocale(GepafinConstant.PASSWORD_EXPIRED_LOGIN_TO_ODESSA));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error parsing JSON response: {}", e.getMessage());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error parsing JSON response: {}", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e);
|
||||
throw new RuntimeException("Authentication failed on Odessa. try again", e);
|
||||
}
|
||||
|
||||
// Regenerate the token and retry
|
||||
regenerateTokenAndSave(hub);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to authenticate user on Odessa : {}", e.getMessage(), e);
|
||||
throw new RuntimeException("Authentication failed on Odessa. try again", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -758,12 +765,31 @@ public class AppointmentDao {
|
||||
|
||||
if (appointmentResponse.getBody() != null) {
|
||||
log.info("Appointment API Response : {}", appointmentResponse.getBody());
|
||||
Map<String, Object> responseBody = (Map<String, Object>) appointmentResponse.getBody();
|
||||
if (responseBody.containsKey(GepafinConstant.DATA_STRING)) {
|
||||
Map<String, Object> data = (Map<String, Object>) responseBody.get(GepafinConstant.DATA_STRING);
|
||||
if (data != null && data.containsKey(GepafinConstant.ID_STRING)) {
|
||||
return data.get(GepafinConstant.ID_STRING).toString();
|
||||
try {
|
||||
Map<String, Object> responseBody = (Map<String, Object>) appointmentResponse.getBody();
|
||||
// 1. Try to get appointment ID from data.id
|
||||
if (responseBody.containsKey(GepafinConstant.DATA_STRING)) {
|
||||
Map<String, Object> data = (Map<String, Object>) responseBody.get(GepafinConstant.DATA_STRING);
|
||||
if (data != null && data.containsKey(GepafinConstant.ID_STRING)) {
|
||||
return data.get(GepafinConstant.ID_STRING).toString();
|
||||
}
|
||||
}
|
||||
// 2. If ID not present, check errors[0].cause.errorDescription
|
||||
if (responseBody.containsKey(GepafinConstant.ERROR_STRING)) {
|
||||
List<Map<String, Object>> errors = (List<Map<String, Object>>) responseBody.get(GepafinConstant.ERROR_STRING);
|
||||
if (errors != null && !errors.isEmpty()) {
|
||||
Map<String, Object> firstError = errors.get(0);
|
||||
if (firstError.containsKey(GepafinConstant.CAUSE_STRING)) {
|
||||
Map<String, Object> cause = (Map<String, Object>) firstError.get(GepafinConstant.CAUSE_STRING);
|
||||
if (cause != null && cause.containsKey(GepafinConstant.ERROR_DESCRIPTION_STRING)) {
|
||||
String errorDescription = cause.get(GepafinConstant.ERROR_DESCRIPTION_STRING).toString();
|
||||
log.warn("Appointment creation failed: {}", errorDescription);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error while extracting appointment ID or parsing error message", e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -3,9 +3,9 @@ package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.CallEntity;
|
||||
import net.gepafin.tendermanagement.entities.EmailLogEntity;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.enums.EmailScenarioTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.EmailServiceTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.RecipientTypeEnum;
|
||||
@@ -13,9 +13,12 @@ import net.gepafin.tendermanagement.enums.StatusTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.request.EmailLogRequest;
|
||||
import net.gepafin.tendermanagement.model.response.EmailResendResponseBean;
|
||||
import net.gepafin.tendermanagement.model.response.EmailSendResponse;
|
||||
import net.gepafin.tendermanagement.repositories.EmailLogRepository;
|
||||
import net.gepafin.tendermanagement.repositories.*;
|
||||
import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -38,10 +41,28 @@ public class EmailDao {
|
||||
@Autowired
|
||||
private EmailLogDao emailLogDao;
|
||||
|
||||
@Autowired
|
||||
private UserActionsRepository userActionsRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEvaluationRepository applicationEvaluationRepository;
|
||||
|
||||
public EmailResendResponseBean resendEmail(HttpServletRequest request , Long userActionId){
|
||||
UserActionEntity userActionEntity = userActionsRepository.findUserActionByIdAndIsDeletedFalse(userActionId);
|
||||
if(userActionEntity == null){
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_ACTION_ID_NOT_FOUND));
|
||||
}
|
||||
List<EmailLogEntity> emailLogs = emailLogRepository.findByUserActionIdAndEmailServiceTypeAndSendStatus(userActionId,EmailServiceTypeEnum.PEC_SERVICE.getValue(),StatusTypeEnum.FAILED.getValue());
|
||||
|
||||
if (emailLogs.isEmpty()) {
|
||||
log.info("No emails found for given userActionId.");
|
||||
log.info("No emails found for given userActionId: {}",userActionId);
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.NO_EMAIL_LOG_FOUND));
|
||||
}
|
||||
EmailResendResponseBean emailResendResponseBean = new EmailResendResponseBean();
|
||||
for (EmailLogEntity log : emailLogs){
|
||||
@@ -67,23 +88,104 @@ public class EmailDao {
|
||||
}
|
||||
EmailSendResponse emailSendResponse = buildEmailSendResponseFromRequest(request);
|
||||
emailResendResponseBean.setEmailSendResponse(emailSendResponse);
|
||||
|
||||
if (Boolean.TRUE.equals(emailSendResponse.getIsEmailSend())){
|
||||
updateEmailSendStatusIfSuccessful(emailSendResponse);
|
||||
}
|
||||
return emailResendResponseBean;
|
||||
}
|
||||
|
||||
private void updateEmailSendStatusIfSuccessful(EmailSendResponse emailSendResponse){
|
||||
Long actionId = emailSendResponse.getUserActionId();
|
||||
|
||||
EmailLogEntity emailLog = emailLogRepository.findTopByUserActionIdAndEmailServiceTypeAndSendStatusOrderByIdDesc(
|
||||
actionId,
|
||||
EmailServiceTypeEnum.PEC_SERVICE.getValue(),
|
||||
StatusTypeEnum.SUCCESS.getValue()
|
||||
);
|
||||
if (emailLog != null) {
|
||||
switch (emailSendResponse.getEmailScenario()) {
|
||||
case APPLICATION_AMENDMENT_REQUESTED:
|
||||
case APPLICATION_AMENDMENT_REMINDER:
|
||||
updateApplicationAmendmentStatus(emailLog, emailSendResponse.getEmailScenario().getValue());
|
||||
break;
|
||||
|
||||
case USER_CREATION:
|
||||
case PASSWORD_RESET_REQUEST:
|
||||
updateUserEmailStatus(emailLog, emailSendResponse.getEmailScenario().getValue());
|
||||
break;
|
||||
|
||||
case APPLICATION_ADMISSIBLE:
|
||||
case APPLICATION_REJECTED:
|
||||
updateApplicationEvaluationStatus(emailLog, emailSendResponse.getEmailScenario().getValue());
|
||||
break;
|
||||
|
||||
default:
|
||||
log.warn("Unhandled email scenario: {}", emailSendResponse.getEmailScenario());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateApplicationAmendmentStatus(EmailLogEntity log, String scenario) {
|
||||
if (log.getAmendmentId() != null) {
|
||||
applicationAmendmentRequestRepository.findById(log.getAmendmentId()).ifPresent(amendment -> {
|
||||
if (updateEmailSendResponse(amendment.getEmailSendResponse(), scenario)) {
|
||||
applicationAmendmentRequestRepository.save(amendment);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void updateApplicationEvaluationStatus(EmailLogEntity log, String scenario) {
|
||||
if (log.getApplicationId() != null) {
|
||||
ApplicationEvaluationEntity evaluation = applicationEvaluationRepository.findByApplicationId(log.getApplicationId());
|
||||
if (evaluation != null && updateEmailSendResponse(evaluation.getEmailSendResponse(), scenario)) {
|
||||
applicationEvaluationRepository.save(evaluation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateUserEmailStatus(EmailLogEntity log, String scenario) {
|
||||
if (log.getUserId() != null) {
|
||||
userRepository.findById(log.getUserId()).ifPresent(user -> {
|
||||
if (updateEmailSendResponse(user.getEmailSendResponse(), scenario)) {
|
||||
userRepository.save(user);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private boolean updateEmailSendResponse(List<EmailSendResponse> responses, String scenario) {
|
||||
if (responses == null || responses.isEmpty()) return false;
|
||||
|
||||
for (EmailSendResponse response : responses) {
|
||||
if (scenario.equals(response.getEmailScenario().getValue())) {
|
||||
response.setIsEmailSend(true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public EmailSendResponse buildEmailSendResponseFromRequest(HttpServletRequest request) {
|
||||
Long userActionId = (Long) request.getAttribute(GepafinConstant.USER_ACTION_ID);
|
||||
List<EmailLogEntity> emailLogs = emailLogRepository.findByUserActionId(userActionId);
|
||||
List<EmailLogEntity> emailLogs = emailLogRepository.findByUserActionIdAndEmailServiceType(userActionId,EmailServiceTypeEnum.PEC_SERVICE.getValue());
|
||||
|
||||
boolean allSuccess = true;
|
||||
String emailScenario = null;
|
||||
|
||||
for (EmailLogEntity log : emailLogs) {
|
||||
|
||||
if (emailScenario == null) {
|
||||
emailScenario = log.getEmailType();
|
||||
}
|
||||
boolean isSuccess = EmailServiceTypeEnum.PEC_SERVICE.getValue().equals(log.getEmailServiceType()) &&
|
||||
StatusTypeEnum.SUCCESS.getValue().equals(log.getSendStatus());
|
||||
if (!isSuccess) {
|
||||
boolean isSuccess = StatusTypeEnum.SUCCESS.getValue().equals(log.getSendStatus());
|
||||
if (Boolean.FALSE.equals(isSuccess)) {
|
||||
allSuccess = false;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user