-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathBeneficiaryConsentController.java
More file actions
124 lines (101 loc) · 5.1 KB
/
BeneficiaryConsentController.java
File metadata and controls
124 lines (101 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.controller.beneficiaryConsent;
import com.iemr.common.data.beneficiaryConsent.BeneficiaryConsentRequest;
import com.iemr.common.exception.OtpRateLimitException;
import com.iemr.common.service.beneficiaryOTPHandler.BeneficiaryOTPHandler;
import com.iemr.common.utils.mapper.InputMapper;
import com.iemr.common.utils.response.OutputResponse;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.ws.rs.core.MediaType;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping(value = { "/beneficiaryConsent" })
@RestController
public class BeneficiaryConsentController {
final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
@Autowired
private BeneficiaryOTPHandler beneficiaryOTPHandler;
@Operation(summary = "Send Consent")
@RequestMapping(value = "/sendConsent", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
public String sendConsent(@Param(value = "{\"mobNo\":\"String\"}") @RequestBody String requestOBJ) {
OutputResponse response = new OutputResponse();
try {
BeneficiaryConsentRequest obj = InputMapper.gson().fromJson(requestOBJ, BeneficiaryConsentRequest.class);
String success = beneficiaryOTPHandler.sendOTP(obj); // method name unchanged if internal logic still uses 'OTP'
logger.info(success.toString());
response.setResponse(success);
} catch (OtpRateLimitException e) {
logger.warn("OTP rate limit hit for sendConsent: " + e.getMessage());
response.setError(429, e.getMessage());
} catch (Exception e) {
response.setError(500, "error : " + e);
}
return response.toString();
}
@Operation(summary = "Validate Consent")
@RequestMapping(value = "/validateConsent", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
public String validateConsent(@Param(value = "{\"mobNo\":\"String\",\"otp\":\"Integer\"}") @RequestBody String requestOBJ) {
OutputResponse response = new OutputResponse();
try {
BeneficiaryConsentRequest obj = InputMapper.gson().fromJson(requestOBJ, BeneficiaryConsentRequest.class);
JSONObject responseOBJ = beneficiaryOTPHandler.validateOTP(obj);
if (responseOBJ != null)
response.setResponse(responseOBJ.toString());
else
response.setError(500, "failure");
} catch (Exception e) {
logger.error("error in validating Consent : " + e);
response.setError(500, "error : " + e);
}
return response.toString();
}
@Operation(summary = "Resend Consent")
@RequestMapping(value = "/resendConsent", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
public String resendConsent(@Param(value = "{\"mobNo\":\"String\"}") @RequestBody String requestOBJ) {
logger.info(requestOBJ.toString());
OutputResponse response = new OutputResponse();
try {
BeneficiaryConsentRequest obj = InputMapper.gson().fromJson(requestOBJ, BeneficiaryConsentRequest.class);
String success = beneficiaryOTPHandler.resendOTP(obj);
logger.info(success.toString());
if (success.contains("otp"))
response.setResponse(success);
else
response.setError(500, "failure");
} catch (OtpRateLimitException e) {
logger.warn("OTP rate limit hit for resendConsent: " + e.getMessage());
response.setError(429, e.getMessage());
} catch (Exception e) {
logger.error("error in re-sending Consent : " + e);
response.setError(500, "error : " + e);
}
return response.toString();
}
}