From 6b10463ee3368dd516e80fa4e7b650897a3f52c3 Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Thu, 6 Oct 2022 08:22:10 +0530 Subject: [PATCH] Added notification facility for bulk upload --- .../common/util/CbExtServerProperties.java | 33 +++++++++++++++++++ .../org/sunbird/common/util/Constants.java | 1 + .../org/sunbird/common/util/ProjectUtil.java | 11 ++++++- .../profile/controller/ProfileController.java | 7 ++-- .../profile/service/ProfileService.java | 2 +- .../profile/service/ProfileServiceImpl.java | 28 +++++++++++++++- .../storage/service/StorageServiceImpl.java | 8 +++-- src/main/resources/application.properties | 3 ++ 8 files changed, 85 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java index 1b10ad74f..23a43ac16 100644 --- a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java +++ b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java @@ -369,6 +369,15 @@ public class CbExtServerProperties { @Value("${user.bulk.upload.container.name}") private String bulkUploadContainerName; + @Value("${user.bulk.upload.email.template}") + private String bulkUploadEmailTemplate; + + @Value("${user.bulk.upload.email.notification.list}") + private String bulkUploadEmailNotificationList; + + @Value("${user.bulk.upload.email.notification.subject}") + private String bulkUploadEmailNotificationSubject; + public String getUserAssessmentSubmissionDuration() { return userAssessmentSubmissionDuration; } @@ -1347,4 +1356,28 @@ public String getBulkUploadContainerName() { public void setBulkUploadContainerName(String bulkUploadContainerName) { this.bulkUploadContainerName = bulkUploadContainerName; } + + public String getBulkUploadEmailTemplate() { + return bulkUploadEmailTemplate; + } + + public void setBulkUploadEmailTemplate(String bulkUploadEmailTemplate) { + this.bulkUploadEmailTemplate = bulkUploadEmailTemplate; + } + + public List getBulkUploadEmailNotificationList() { + return Arrays.asList(bulkUploadEmailNotificationList.split(",", -1)); + } + + public void setBulkUploadEmailNotificationList(String bulkUploadEmailNotificationList) { + this.bulkUploadEmailNotificationList = bulkUploadEmailNotificationList; + } + + public String getBulkUploadEmailNotificationSubject() { + return bulkUploadEmailNotificationSubject; + } + + public void setBulkUploadEmailNotificationSubject(String bulkUploadEmailNotificationSubject) { + this.bulkUploadEmailNotificationSubject = bulkUploadEmailNotificationSubject; + } } \ No newline at end of file diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index dc10c578a..eccc7eba7 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -507,6 +507,7 @@ public class Constants { public static final String LAST_UPDATED_ON = "lastUpdatedOn"; public static final String DESCENDING_ORDER = "desc"; public static final String X_AUTH_USER_ORG_ID = "x-authenticated-user-orgid"; + public static final String X_AUTH_USER_ORG_NAME = "x-authenticated-user-orgname"; public static final String API_USER_SIGNUP = "api.user.signup"; public static final String API_USER_BULK_UPLOAD = "api.user.bulk.upload"; public static final String API_USER_BULK_UPLOAD_STATUS = "api.user.bulk.upload.status"; diff --git a/src/main/java/org/sunbird/common/util/ProjectUtil.java b/src/main/java/org/sunbird/common/util/ProjectUtil.java index 26019821c..b1925524f 100644 --- a/src/main/java/org/sunbird/common/util/ProjectUtil.java +++ b/src/main/java/org/sunbird/common/util/ProjectUtil.java @@ -1,6 +1,10 @@ package org.sunbird.common.util; import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.HashMap; +import java.util.Map; + import org.apache.commons.lang.StringUtils; import org.joda.time.DateTime; import org.springframework.http.HttpStatus; @@ -75,7 +79,12 @@ public static SBApiResponse createDefaultResponse(String api) { return response; } - + public static Map getDefaultHeaders() { + Map headers = new HashMap(); + headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON); + return headers; + } + public enum Method { GET, POST, diff --git a/src/main/java/org/sunbird/profile/controller/ProfileController.java b/src/main/java/org/sunbird/profile/controller/ProfileController.java index 791d1b95d..56611e770 100644 --- a/src/main/java/org/sunbird/profile/controller/ProfileController.java +++ b/src/main/java/org/sunbird/profile/controller/ProfileController.java @@ -68,9 +68,9 @@ private ResponseEntity adminMigrateUser(@RequestHeader(Constants.X_AUTH_TOKEN SBApiResponse response = profileService.migrateUser(request, userToken, authToken); return new ResponseEntity<>(response, response.getResponseCode()); } - + @PostMapping("/user/v1/ext/signup") - public ResponseEntity userSignup(@RequestBody Map request) { + public ResponseEntity userSignup(@RequestBody Map request) { SBApiResponse response = profileService.userSignup(request); return new ResponseEntity<>(response, response.getResponseCode()); } @@ -78,8 +78,9 @@ public ResponseEntity userSignup(@RequestBody Map request) { @PostMapping("/user/v1/bulkupload") public ResponseEntity bulkUpload(@RequestParam(value = "file", required = true) MultipartFile multipartFile, @RequestHeader(Constants.X_AUTH_USER_ORG_ID) String rootOrgId, + @RequestHeader(Constants.X_AUTH_USER_ORG_NAME) String orgName, @RequestHeader(Constants.X_AUTH_USER_ID) String userId) { - SBApiResponse uploadResponse = profileService.bulkUpload(multipartFile, rootOrgId, userId); + SBApiResponse uploadResponse = profileService.bulkUpload(multipartFile, rootOrgId, orgName, userId); return new ResponseEntity<>(uploadResponse, uploadResponse.getResponseCode()); } diff --git a/src/main/java/org/sunbird/profile/service/ProfileService.java b/src/main/java/org/sunbird/profile/service/ProfileService.java index 0109b63a6..d885c50de 100644 --- a/src/main/java/org/sunbird/profile/service/ProfileService.java +++ b/src/main/java/org/sunbird/profile/service/ProfileService.java @@ -22,7 +22,7 @@ public interface ProfileService { SBApiResponse userSignup(Map request); - SBApiResponse bulkUpload(MultipartFile mFile, String orgId, String userId); + SBApiResponse bulkUpload(MultipartFile mFile, String orgId, String orgName, String userId); SBApiResponse getBulkUploadDetails(String orgId); } diff --git a/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java b/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java index 671654a5d..2c24b86df 100644 --- a/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java +++ b/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java @@ -607,7 +607,7 @@ public SBApiResponse userSignup(Map request) { } @Override - public SBApiResponse bulkUpload(MultipartFile mFile, String orgId, String userId) { + public SBApiResponse bulkUpload(MultipartFile mFile, String orgId, String orgName, String userId) { SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_USER_BULK_UPLOAD); try { SBApiResponse uploadResponse = storageService.uploadFile(mFile, serverConfig.getBulkUploadContainerName()); @@ -639,6 +639,7 @@ public SBApiResponse bulkUpload(MultipartFile mFile, String orgId, String userId response.setResponseCode(HttpStatus.OK); response.getResult().putAll(uploadedFile); + sendBulkUploadNotification(orgId, orgName, (String) uploadResponse.getResult().get(Constants.URL)); } catch (Exception e) { setErrorData(response, String.format("Failed to process user bulk upload request. Error: ", e.getMessage())); @@ -1184,4 +1185,29 @@ private String validateSignupRequest(Map requestData) { return strBuilder.toString(); } + + private void sendBulkUploadNotification(String orgId, String orgName, String fileUrl) { + for (String email : serverConfig.getBulkUploadEmailNotificationList()) { + if (StringUtils.isBlank(email)) { + return; + } + } + Map request = new HashMap<>(); + Map requestBody = new HashMap(); + requestBody.put(Constants.BODY, Constants.HELLO); + requestBody.put(Constants.EMAIL_TEMPLATE_TYPE, serverConfig.getBulkUploadEmailTemplate()); + requestBody.put(Constants.LINK, fileUrl); + requestBody.put(Constants.MODE, Constants.EMAIL); + requestBody.put(Constants.ORG_NAME, orgName); + requestBody.put(Constants.ORG_ID, orgId); + requestBody.put(Constants.RECIPIENT_EMAILS, serverConfig.getBulkUploadEmailNotificationList()); + requestBody.put(Constants.SET_PASSWORD_LINK, true); + requestBody.put(Constants.SUBJECT, serverConfig.getBulkUploadEmailNotificationSubject()); + + request.put(Constants.REQUEST, requestBody); + + outboundRequestHandlerService.fetchResultUsingPost( + serverConfig.getSbUrl() + serverConfig.getSbSendNotificationEmailPath(), request, + ProjectUtil.getDefaultHeaders()); + } } diff --git a/src/main/java/org/sunbird/storage/service/StorageServiceImpl.java b/src/main/java/org/sunbird/storage/service/StorageServiceImpl.java index 034ebc1a7..0b93dfe25 100644 --- a/src/main/java/org/sunbird/storage/service/StorageServiceImpl.java +++ b/src/main/java/org/sunbird/storage/service/StorageServiceImpl.java @@ -48,8 +48,9 @@ public void init() { public SBApiResponse uploadFile(MultipartFile mFile, String containerName) { SBApiResponse response = new SBApiResponse(); response.setId(Constants.API_FILE_UPLOAD); + File file = null; try { - File file = new File(System.currentTimeMillis() + "_" + mFile.getOriginalFilename()); + file = new File(System.currentTimeMillis() + "_" + mFile.getOriginalFilename()); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write(mFile.getBytes()); @@ -57,7 +58,6 @@ public SBApiResponse uploadFile(MultipartFile mFile, String containerName) { String objectKey = containerName + "/" + file.getName(); String url = storageService.upload(serverProperties.getCloudContainerName(), file.getAbsolutePath(), objectKey, Option.apply(false), Option.apply(1), Option.apply(5), Option.empty()); - file.delete(); Map uploadedFile = new HashMap<>(); uploadedFile.put(Constants.NAME, file.getName()); uploadedFile.put(Constants.URL, url); @@ -71,6 +71,10 @@ public SBApiResponse uploadFile(MultipartFile mFile, String containerName) { response.getParams().setErrmsg("Failed to upload file. Exception: " + e.getMessage()); response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); return response; + } finally { + if (file != null) { + file.delete(); + } } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9fe45a75f..123e02cdb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -241,3 +241,6 @@ user.registration.welcome.email.subject=Welcome to iGOT Karmayogi... Activate yo #Bulk Upload feature user.bulk.upload.status.fields=rootOrgId,identifier,fileName,filePath,dateCreatedOn,status,comment,dateUpdatedOn user.bulk.upload.container.name=bulkupload +user.bulk.upload.email.template=bulkUploadNotification +user.bulk.upload.email.notification.list= +user.bulk.upload.email.notification.subject=Request for bulk upload User!