Skip to content

Commit

Permalink
Email Notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Sowmya-Raghuram committed Aug 18, 2024
1 parent e8660a0 commit 272a986
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.sunbird.serve.need.models.Need.Need;
import com.sunbird.serve.need.models.Need.NeedPlan;
import com.sunbird.serve.need.models.enums.NeedStatus;
import com.sunbird.serve.need.models.enums.NeedDeliverableStatus;
import com.sunbird.serve.need.models.request.NeedDeliverableRequest;
import com.sunbird.serve.need.models.request.DeliverableDetailsRequest;
import com.sunbird.serve.need.models.request.OutputParametersRequest;
Expand All @@ -20,6 +21,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.reactive.function.client.WebClient;


@Service
public class NeedDeliverableService {
Expand All @@ -29,6 +35,8 @@ public class NeedDeliverableService {
private final OutputParametersRepository outputParametersRepository;
private final NeedRepository needRepository;
private final NeedPlanRepository needPlanRepository;
@Autowired
private WebClient.Builder webClientBuilder;
private static final Logger logger = LoggerFactory.getLogger(NeedDeliverableService.class);

@Autowired
Expand Down Expand Up @@ -83,6 +91,39 @@ public NeedDeliverable updateNeedDeliverable(UUID needDeliverableId, NeedDeliver
existingNeedDeliverable.setStatus(request.getStatus());
existingNeedDeliverable.setDeliverableDate(request.getDeliverableDate());

//email notification for cancelled sessions
if(request.getStatus() == NeedDeliverableStatus.Cancelled){
NeedPlan needPlan = needPlanRepository.findById(UUID.fromString(request.getNeedPlanId()))
.orElseThrow(() -> new NoSuchElementException("Need Plan not found with ID: " + request.getNeedPlanId()));
// Fetch the Need using the NeedId from the NeedPlan
Need need = needRepository.findById(UUID.fromString(needPlan.getNeedId()))
.orElseThrow(() -> new NoSuchElementException("Need not found with ID: " + needPlan.getNeedId()));
// Construct the API URL
String apiUrl = "http://serve-v1.evean.net/api/v1/serve-fulfill/fulfillment/sendEmail";

// Prepare the request body with the necessary details
Map<String, Object> apiRequestBody = new HashMap<>();
apiRequestBody.put("scenarioType", "CancelSession");
apiRequestBody.put("needId", need.getId());
apiRequestBody.put("deliverableDetails", existingNeedDeliverable);

// Make the API call
webClientBuilder.build()
.post()
.uri(apiUrl)
.bodyValue(apiRequestBody)
.retrieve()
.bodyToMono(String.class)
.doOnSuccess(response -> {

})
.doOnError(e -> {
// Handle errors during the API call
throw new RuntimeException("Error occurred while calling the fulfill microservice: " + e.getMessage(), e);
})
.block(); // Block if you want to make this a synchronous call
}

// Save the updated need deliverable
NeedDeliverable updatedNeedDeliverable = needDeliverableRepository.save(existingNeedDeliverable);

Expand Down

0 comments on commit 272a986

Please sign in to comment.