|
| 1 | +package com.igot.cb.consentacknowledge; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.igot.cb.cassandra.CassandraOperation; |
| 7 | +import com.igot.cb.model.ApiResponse; |
| 8 | +import com.igot.cb.util.AccessTokenValidator; |
| 9 | +import com.igot.cb.util.Constants; |
| 10 | +import com.igot.cb.util.ProjectUtil; |
| 11 | +import org.apache.commons.collections.MapUtils; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.springframework.http.HttpStatus; |
| 16 | +import org.springframework.stereotype.Service; |
| 17 | + |
| 18 | +import java.time.Instant; |
| 19 | +import java.time.ZoneId; |
| 20 | +import java.time.ZonedDateTime; |
| 21 | +import java.time.format.DateTimeFormatter; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +@Service |
| 28 | +public class ConsentAcknowledgeServiceImpl implements IConsentAcknowledgeService { |
| 29 | + |
| 30 | + private final Logger logger = LoggerFactory.getLogger(ConsentAcknowledgeServiceImpl.class); |
| 31 | + |
| 32 | + |
| 33 | + private final AccessTokenValidator accessTokenValidator; |
| 34 | + private final CassandraOperation cassandraOperation; |
| 35 | + private final ObjectMapper objectMapper; |
| 36 | + |
| 37 | + public ConsentAcknowledgeServiceImpl(AccessTokenValidator accessTokenValidator, CassandraOperation cassandraOperation, ObjectMapper objectMapper) { |
| 38 | + this.cassandraOperation = cassandraOperation; |
| 39 | + this.accessTokenValidator = accessTokenValidator; |
| 40 | + this.objectMapper = objectMapper; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Method to acknowledge declaration |
| 45 | + * Required fields in request body : contentId, consentId |
| 46 | + * Optional fields in request body : additionalData |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public ApiResponse acknowledgeDeclaration(Map<String, Object> requestDataBody, String authToken) { |
| 50 | + ApiResponse response = ProjectUtil.createDefaultResponse("api.consent.acknowledge.create"); |
| 51 | + Map<String, Object> requestDataMap = (Map<String, Object>) requestDataBody.get(Constants.REQUEST); |
| 52 | + String userId = accessTokenValidator.fetchUserIdFromAccessToken(authToken, response); |
| 53 | + if (StringUtils.isEmpty(userId)) { |
| 54 | + return response; |
| 55 | + } |
| 56 | + String errMsg = validateAcknowledgeDeclarationRequest(requestDataMap); |
| 57 | + if (!StringUtils.isEmpty(errMsg)) { |
| 58 | + ProjectUtil.errorResponse(response, errMsg, HttpStatus.BAD_REQUEST); |
| 59 | + return response; |
| 60 | + } |
| 61 | + String contentId = (String) requestDataMap.get(Constants.CONTENT_ID); |
| 62 | + String consentId = (String) requestDataMap.get(Constants.CONSENT_ID); |
| 63 | + Map<String, Object> additionalData = (Map<String, Object>) requestDataMap.get(Constants.ADDITIONAL_ATTRIBUTES); |
| 64 | + String additionalDataStr = null; |
| 65 | + try { |
| 66 | + additionalDataStr = objectMapper.writeValueAsString(additionalData); |
| 67 | + } catch (JsonProcessingException e) { |
| 68 | + logger.error("Error while converting additionalData to string", e); |
| 69 | + ProjectUtil.errorResponse(response,"Failed to Parse the additional attributes", HttpStatus.BAD_REQUEST); |
| 70 | + return response; |
| 71 | + } |
| 72 | + Map<String, Object> compositeKeyMap = new HashMap<>(); |
| 73 | + compositeKeyMap.put(Constants.USER_ID, userId); |
| 74 | + compositeKeyMap.put(Constants.CONSENT_ID, consentId); |
| 75 | + Map<String, Object> declarationDataMap = new HashMap<>(); |
| 76 | + ZoneId zoneId = ZoneId.of("Asia/Kolkata"); |
| 77 | + ZonedDateTime submittedAt = ZonedDateTime.ofInstant(Instant.now(), zoneId); |
| 78 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); |
| 79 | + declarationDataMap.put(Constants.SUBMITTED_BY, userId); |
| 80 | + declarationDataMap.put(Constants.ADDITIONAL_ATTRIBUTES, additionalDataStr); |
| 81 | + declarationDataMap.put(Constants.SUBMITTED_AT, submittedAt.format(formatter)); |
| 82 | + try { |
| 83 | + response = (ApiResponse) cassandraOperation.insertRecord(Constants.KEYSPACE_SUNBIRD, Constants.TABLE_DECLARATION_ACKNOWLEDGMENT, Constants.CONTENT_ID, contentId, compositeKeyMap, declarationDataMap); |
| 84 | + if (Constants.FAILED.equals(response.get(Constants.RESPONSE))) { |
| 85 | + logger.error("Error while inserting declaration acknowledgment record in DB: {}", |
| 86 | + response.get(Constants.ERROR_MESSAGE)); |
| 87 | + ProjectUtil.errorResponse( |
| 88 | + response, |
| 89 | + "Failed to acknowledge declaration. Please try again later.", |
| 90 | + HttpStatus.INTERNAL_SERVER_ERROR |
| 91 | + ); |
| 92 | + return response; |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + logger.error("Error while inserting declaration acknowledgment record in DB", e); |
| 96 | + ProjectUtil.errorResponse(response, "Failed to acknowledge declaration. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR); |
| 97 | + return response; |
| 98 | + } |
| 99 | + response.getParams().setStatus(Constants.OK); |
| 100 | + response.setResponseCode(HttpStatus.OK); |
| 101 | + Map<String, Object> result = new HashMap<>(); |
| 102 | + Map<String, Object> consentAckDetailsMap = new HashMap<>(); |
| 103 | + consentAckDetailsMap.put(Constants.CONTENT_ID, contentId); |
| 104 | + consentAckDetailsMap.put(Constants.CONSENT_ID, consentId); |
| 105 | + consentAckDetailsMap.put(Constants.USER_ID, userId); |
| 106 | + consentAckDetailsMap.put(Constants.MESSAGE, "Declaration acknowledged successfully"); |
| 107 | + result.put(Constants.RESPONSE,consentAckDetailsMap); |
| 108 | + response.getResult().putAll(result); |
| 109 | + return response; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Method to validate acknowledge declaration request |
| 114 | + */ |
| 115 | + private String validateAcknowledgeDeclarationRequest(Map<String, Object> requestData) { |
| 116 | + if (requestData == null) { |
| 117 | + return "Request data is missing."; |
| 118 | + } |
| 119 | + if (!requestData.containsKey(Constants.CONTENT_ID) |
| 120 | + || StringUtils.isEmpty(requestData.get(Constants.CONTENT_ID).toString())) { |
| 121 | + return "Missing or invalid contentId."; |
| 122 | + } |
| 123 | + if (!requestData.containsKey(Constants.CONSENT_ID) |
| 124 | + || StringUtils.isEmpty(requestData.get(Constants.CONSENT_ID).toString())) { |
| 125 | + return "Missing or invalid consentId."; |
| 126 | + } |
| 127 | + if (requestData.containsKey(Constants.ADDITIONAL_ATTRIBUTES)) { |
| 128 | + Map<String, Object> additionalData = (Map<String, Object>) requestData.get(Constants.ADDITIONAL_ATTRIBUTES); |
| 129 | + if (MapUtils.isEmpty(additionalData)) { |
| 130 | + return "Missing or invalid additionalData."; |
| 131 | + } |
| 132 | + } |
| 133 | + return ""; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * Method to get consent details by consentId |
| 139 | + */ |
| 140 | + @Override |
| 141 | + public ApiResponse getConsentDetails(String consentId, String authToken) { |
| 142 | + ApiResponse response = ProjectUtil.createDefaultResponse("api.consent.read"); |
| 143 | + String userId = accessTokenValidator.fetchUserIdFromAccessToken(authToken, response); |
| 144 | + if (StringUtils.isEmpty(userId)) { |
| 145 | + return response; |
| 146 | + } |
| 147 | + Map<String, Object> propertyMap = new HashMap<>(); |
| 148 | + propertyMap.put(Constants.CONSENT_ID, consentId); |
| 149 | + List<Map<String, Object>> consentDetails; |
| 150 | + try { |
| 151 | + consentDetails = cassandraOperation |
| 152 | + .getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, Constants.TABLE_CONSENT_DETAILS, propertyMap, Arrays.asList(Constants.CONSENT_ID, Constants.ADDITIONAL_DATA, Constants.DESCRIPTION), null); |
| 153 | + } catch (Exception e) { |
| 154 | + logger.error("Error while fetching consent details from DB", e); |
| 155 | + ProjectUtil.errorResponse(response, "Failed to fetch consent details. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR); |
| 156 | + return response; |
| 157 | + } |
| 158 | + Map<String, Object> consentDetailsMap = consentDetails.get(0); |
| 159 | + response.getParams().setStatus(Constants.OK); |
| 160 | + response.setResponseCode(HttpStatus.OK); |
| 161 | + Map<String, Object> result = new HashMap<>(); |
| 162 | + result.put(Constants.RESPONSE, consentDetailsMap); |
| 163 | + response.getResult().putAll(result); |
| 164 | + return response; |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + /** |
| 169 | + * Method to get consent acknowledgement details by contentId and consentId |
| 170 | + */ |
| 171 | + @Override |
| 172 | + public ApiResponse getConsentAcknowledgementDetails(String contentId, String consentId, String authToken) { |
| 173 | + ApiResponse response = ProjectUtil.createDefaultResponse("api.consent.acknowledgement.read"); |
| 174 | + String userId = accessTokenValidator.fetchUserIdFromAccessToken(authToken, response); |
| 175 | + if (StringUtils.isEmpty(userId)) { |
| 176 | + return response; |
| 177 | + } |
| 178 | + Map<String, Object> propertyMap = new HashMap<>(); |
| 179 | + propertyMap.put(Constants.CONSENT_ID, consentId); |
| 180 | + propertyMap.put(Constants.CONTENT_ID, contentId); |
| 181 | + propertyMap.put(Constants.USER_ID, userId); |
| 182 | + List<Map<String, Object>> consentAcknowledgementDetailsList; |
| 183 | + try { |
| 184 | + consentAcknowledgementDetailsList = cassandraOperation |
| 185 | + .getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, Constants.TABLE_DECLARATION_ACKNOWLEDGMENT, propertyMap, Arrays.asList(Constants.CONTENT_ID, Constants.CONSENT_ID, Constants.USER_ID, Constants.ADDITIONAL_ATTRIBUTES), null); |
| 186 | + } catch (Exception e) { |
| 187 | + logger.error("Error while fetching consent details from DB", e); |
| 188 | + ProjectUtil.errorResponse(response, "Failed to fetch consent Acknowledgement details. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR); |
| 189 | + return response; |
| 190 | + } |
| 191 | + Map<String, Object> consentAcknowledgementDetailsMap = consentAcknowledgementDetailsList.get(0); |
| 192 | + String additionAttributesStr = (String) consentAcknowledgementDetailsMap.get(Constants.ADDITIONAL_ATTRIBUTES); |
| 193 | + Map<String, Object> additionalAttributesMap = null; |
| 194 | + try { |
| 195 | + additionalAttributesMap = objectMapper.readValue(additionAttributesStr, new TypeReference<>() { |
| 196 | + }); |
| 197 | + } catch (JsonProcessingException e) { |
| 198 | + logger.error("Error while parsing additionalAttributes from string to map", e); |
| 199 | + ProjectUtil.errorResponse(response, "Failed to Parse the additional attributes", HttpStatus.INTERNAL_SERVER_ERROR); |
| 200 | + return response; |
| 201 | + } |
| 202 | + consentAcknowledgementDetailsMap.put(Constants.ADDITIONAL_ATTRIBUTES, additionalAttributesMap); |
| 203 | + response.getParams().setStatus(Constants.OK); |
| 204 | + response.setResponseCode(HttpStatus.OK); |
| 205 | + Map<String, Object> result = new HashMap<>(); |
| 206 | + result.put(Constants.RESPONSE, consentAcknowledgementDetailsMap); |
| 207 | + response.getResult().putAll(result); |
| 208 | + return response; |
| 209 | + } |
| 210 | +} |
0 commit comments