|
| 1 | +package com.iemr.common.controller.videocall; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.json.JSONObject; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RestController; |
| 17 | + |
| 18 | +import com.iemr.common.model.videocall.UpdateCallRequest; |
| 19 | +import com.iemr.common.model.videocall.VideoCallRequest; |
| 20 | +import com.iemr.common.service.videocall.VideoCallService; |
| 21 | +import com.iemr.common.utils.response.OutputResponse; |
| 22 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 23 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import jakarta.servlet.http.HttpServletRequest; |
| 26 | + |
| 27 | +import org.springframework.web.bind.annotation.RequestBody; |
| 28 | + |
| 29 | +@RestController |
| 30 | +@RequestMapping(value = "/video-consultation") |
| 31 | +public class VideoCallController { |
| 32 | + final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private VideoCallService videoCallService; |
| 36 | + |
| 37 | + @PostMapping(value = "/generate-link", produces = MediaType.APPLICATION_JSON_VALUE, headers = "Authorization") |
| 38 | + public ResponseEntity<Map<String, String>> generateJitsiLink() { |
| 39 | + Map<String, String> response = new HashMap<>(); |
| 40 | + try { |
| 41 | + String link = videoCallService.generateMeetingLink(); |
| 42 | + response.put("meetingLink", link); |
| 43 | + return ResponseEntity.ok(response); |
| 44 | + } catch (Exception e) { |
| 45 | + response.put("error", e.getMessage()); |
| 46 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | +@PostMapping(value = "/send-link", produces = MediaType.APPLICATION_JSON_VALUE, headers = "Authorization") |
| 51 | +public String sendVideoLink(@RequestBody String requestModel, HttpServletRequest request) { |
| 52 | + OutputResponse response = new OutputResponse(); |
| 53 | + |
| 54 | + try { |
| 55 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 56 | + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 57 | + VideoCallRequest requestData = objectMapper.readValue(requestModel, VideoCallRequest.class); |
| 58 | + String serviceResponse = videoCallService.sendMeetingLink(requestData); |
| 59 | + |
| 60 | + return serviceResponse; |
| 61 | + |
| 62 | + } catch (Exception e) { |
| 63 | + logger.error("send MeetingLink failed with error: " + e.getMessage(), e); |
| 64 | + response.setError(e); |
| 65 | + return response.toString(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +@PostMapping(value = "/update-call-status", produces = MediaType.APPLICATION_JSON_VALUE, headers = "Authorization") |
| 70 | +public ResponseEntity<String> updateCallStatus(@RequestBody UpdateCallRequest requestModel, HttpServletRequest request) { |
| 71 | + OutputResponse response = new OutputResponse(); |
| 72 | + |
| 73 | + try { |
| 74 | + if (requestModel.getMeetingLink() == null || requestModel.getCallStatus() == null) { |
| 75 | + throw new IllegalArgumentException("Meeting Link and Status are required"); |
| 76 | + } |
| 77 | + |
| 78 | + String result = videoCallService.updateCallStatus(requestModel); |
| 79 | + |
| 80 | + JSONObject responseObj = new JSONObject(); |
| 81 | + responseObj.put("status", "success"); |
| 82 | + responseObj.put("message", result); |
| 83 | + response.setResponse(responseObj.toString()); |
| 84 | + |
| 85 | + } catch (IllegalArgumentException e) { |
| 86 | + logger.error("Validation error: " + e.getMessage(), e); |
| 87 | + return ResponseEntity.badRequest().body("{\"status\":\"error\",\"message\":\"" + e.getMessage() + "\"}"); |
| 88 | + } catch (Exception e) { |
| 89 | + logger.error("updateCallStatus failed with error: " + e.getMessage(), e); |
| 90 | + response.setError(e); |
| 91 | + } |
| 92 | + |
| 93 | + return ResponseEntity.ok(response.toString()); |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +} |
0 commit comments