From 9d2671bea10bba7c668e5fb18d6d63977e1abb15 Mon Sep 17 00:00:00 2001 From: shankaragoudab <140387294+shankaragoudab@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:44:19 +0530 Subject: [PATCH] added discussion report api --- .../controller/DiscussionController.java | 7 ++ .../discussion/service/DiscussionService.java | 2 + .../service/impl/DiscussionServiceImpl.java | 92 +++++++++++++++++++ .../com/igot/cb/pores/util/Constants.java | 8 ++ .../EsFieldsmapping/discussionEsMapping.json | 3 + 5 files changed, 112 insertions(+) diff --git a/src/main/java/com/igot/cb/discussion/controller/DiscussionController.java b/src/main/java/com/igot/cb/discussion/controller/DiscussionController.java index 580818e..fb1a617 100644 --- a/src/main/java/com/igot/cb/discussion/controller/DiscussionController.java +++ b/src/main/java/com/igot/cb/discussion/controller/DiscussionController.java @@ -71,4 +71,11 @@ public ResponseEntity downVote(@PathVariable String discussionId, ApiResponse response = discussionService.downVote(discussionId, token); return new ResponseEntity<>(response, response.getResponseCode()); } + + @PostMapping("/report") + public ResponseEntity report(@RequestBody Map reportData, + @RequestHeader(Constants.X_AUTH_TOKEN) String token) { + ApiResponse response = discussionService.report(token, reportData); + return new ResponseEntity<>(response, response.getResponseCode()); + } } diff --git a/src/main/java/com/igot/cb/discussion/service/DiscussionService.java b/src/main/java/com/igot/cb/discussion/service/DiscussionService.java index 123e6e2..2471d8e 100644 --- a/src/main/java/com/igot/cb/discussion/service/DiscussionService.java +++ b/src/main/java/com/igot/cb/discussion/service/DiscussionService.java @@ -24,4 +24,6 @@ public interface DiscussionService { ApiResponse upVote(String discussionId, String token); ApiResponse downVote(String discussionId, String token); + + ApiResponse report(String token, Map reportData); } diff --git a/src/main/java/com/igot/cb/discussion/service/impl/DiscussionServiceImpl.java b/src/main/java/com/igot/cb/discussion/service/impl/DiscussionServiceImpl.java index 034b72e..0e74d60 100644 --- a/src/main/java/com/igot/cb/discussion/service/impl/DiscussionServiceImpl.java +++ b/src/main/java/com/igot/cb/discussion/service/impl/DiscussionServiceImpl.java @@ -79,6 +79,7 @@ public ApiResponse createDiscussion(JsonNode discussionDetails, String token) { ((ObjectNode) discussionDetails).put(Constants.UP_VOTE_COUNT,0L); ((ObjectNode) discussionDetails).put(Constants.DOWN_VOTE_COUNT,0L); ((ObjectNode) discussionDetails).put(Constants.MEDIA,discussionDetails.get(Constants.MEDIA)); + ((ObjectNode) discussionDetails).put(Constants.STATUS,Constants.ACTIVE); DiscussionEntity jsonNodeEntity = new DiscussionEntity(); Timestamp currentTime = new Timestamp(System.currentTimeMillis()); @@ -663,4 +664,95 @@ public ApiResponse upVote(String discussionId, String token) { public ApiResponse downVote(String discussionId, String token) { return vote(discussionId, token, Constants.DOWN); } + + @Override + public ApiResponse report(String token, Map reportData) { + log.info("DiscussionService::report: Reporting discussion"); + ApiResponse response = ProjectUtil.createDefaultResponse("discussion.report"); + String errorMsg = validateReportPayload(reportData); + if (StringUtils.isNotEmpty(errorMsg)) { + return returnErrorMsg(errorMsg, HttpStatus.BAD_REQUEST, response, Constants.FAILED); + } + + String userId = accessTokenValidator.verifyUserToken(token); + if (StringUtils.isBlank(userId) || Constants.UNAUTHORIZED.equals(userId)) { + return returnErrorMsg(Constants.INVALID_AUTH_TOKEN, HttpStatus.UNAUTHORIZED, response, Constants.FAILED); + } + + try { + String discussionId = (String) reportData.get(Constants.DISCUSSION_ID); + Optional discussionDbData = discussionRepository.findById(discussionId); + if (!discussionDbData.isPresent()) { + return returnErrorMsg(Constants.DISCUSSION_NOT_FOUND, HttpStatus.NOT_FOUND, response, Constants.FAILED); + } + + DiscussionEntity discussionEntity = discussionDbData.get(); + if (!discussionEntity.getIsActive()) { + return returnErrorMsg(Constants.DISCUSSION_IS_INACTIVE, HttpStatus.CONFLICT, response, Constants.FAILED); + } + + JsonNode data = discussionEntity.getData(); + String currentStatus = data.has(Constants.STATUS) ? data.get(Constants.STATUS).asText() : null; + + if (Constants.SUSPENDED.equals(currentStatus)) { + return returnErrorMsg(Constants.DISCUSSION_SUSPENDED, HttpStatus.ALREADY_REPORTED, response, Constants.FAILED); + } + + ((ObjectNode) data).put(Constants.STATUS, Constants.SUSPENDED); + ArrayNode reportedByNode = data.has(Constants.REPORTED_BY) ? (ArrayNode) data.get(Constants.REPORTED_BY) : objectMapper.createArrayNode(); + reportedByNode.add(userId); + ((ObjectNode) data).put(Constants.REPORTED_REASON, objectMapper.valueToTree(reportData.get(Constants.REPORTED_REASON))); + ((ObjectNode) data).put(Constants.REPORTED_BY, reportedByNode); + + discussionEntity.setData(data); + discussionRepository.save(discussionEntity); + log.info("DiscussionService::report: Discussion entity updated successfully"); + + ObjectNode jsonNode = objectMapper.createObjectNode(); + jsonNode.setAll((ObjectNode) discussionEntity.getData()); + Map map = objectMapper.convertValue(jsonNode, Map.class); + esUtilService.addDocument(cbServerProperties.getDiscussionEntity(), Constants.INDEX_TYPE, discussionId, map, cbServerProperties.getElasticDiscussionJsonPath()); + cacheService.putCache(Constants.DISCUSSION_CACHE_PREFIX + discussionId, jsonNode); + return response; + } catch (Exception e) { + log.error("DiscussionService::report: Failed to report discussion", e); + return returnErrorMsg(Constants.DISCUSSION_REPORT_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, response, Constants.FAILED); + } + } + + private String validateReportPayload(Map reportData) { + StringBuffer errorMsg = new StringBuffer(); + List errList = new ArrayList<>(); + + if (reportData.containsKey(Constants.DISCUSSION_ID) && StringUtils.isBlank((String) reportData.get(Constants.DISCUSSION_ID))){ + errList.add(Constants.DISCUSSION_ID); + } + if (reportData.containsKey(Constants.REPORTED_REASON)) { + Object reportedReasonObj = reportData.get(Constants.REPORTED_REASON); + if (reportedReasonObj instanceof List) { + List reportedReasonList = (List) reportedReasonObj; + if (reportedReasonList.isEmpty()) { + errList.add(Constants.REPORTED_REASON); + } else if (reportedReasonList.contains("Others")) { + if (!reportData.containsKey(Constants.OTHER_REASON) || + StringUtils.isBlank((String) reportData.get(Constants.OTHER_REASON))) { + errList.add(Constants.OTHER_REASON); + } + } + } else { + errList.add(Constants.REPORTED_REASON); + } + } + if (!errList.isEmpty()) { + errorMsg.append("Failed Due To Missing Params - ").append(errList).append("."); + } + return errorMsg.toString(); + } + + private ApiResponse returnErrorMsg(String error, HttpStatus type, ApiResponse response, String status) { + response.setResponseCode(type); + response.getParams().setErr(error); + response.setMessage(status); + return response; + } } diff --git a/src/main/java/com/igot/cb/pores/util/Constants.java b/src/main/java/com/igot/cb/pores/util/Constants.java index c5d8b37..4feb529 100644 --- a/src/main/java/com/igot/cb/pores/util/Constants.java +++ b/src/main/java/com/igot/cb/pores/util/Constants.java @@ -131,6 +131,14 @@ public class Constants { public static final String UP_VOTE_COUNT = "upVoteCount"; public static final String DOWN_VOTE_COUNT = "downVoteCount"; public static final String DISCUSSION_VOTE_API = "discussion.vote"; + public static final String STATUS = "status"; + public static final String REPORTED_BY = "reportedBy"; + public static final String SUSPENDED = "suspended"; + public static final String REPORTED_REASON = "reportedDueTo"; + public static final String OTHER_REASON = "otherReasons"; + public static final String ACTIVE = "active"; + public static final String DISCUSSION_SUSPENDED = "discussion Already suspended"; + public static final String DISCUSSION_REPORT_FAILED = "Failed to report discussion"; private Constants() { } } diff --git a/src/main/resources/EsFieldsmapping/discussionEsMapping.json b/src/main/resources/EsFieldsmapping/discussionEsMapping.json index 420d7c8..c0f1bd8 100644 --- a/src/main/resources/EsFieldsmapping/discussionEsMapping.json +++ b/src/main/resources/EsFieldsmapping/discussionEsMapping.json @@ -54,5 +54,8 @@ }, "parentDiscussionId": { "type": "keyword" + }, + "status": { + "type": "keyword" } } \ No newline at end of file