Skip to content

Commit

Permalink
Added notification facility for bulk upload
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Oct 6, 2022
1 parent e7bce21 commit 6b10463
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 8 deletions.
33 changes: 33 additions & 0 deletions src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<String> 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;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/sunbird/common/util/ProjectUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -75,7 +79,12 @@ public static SBApiResponse createDefaultResponse(String api) {
return response;
}


public static Map<String, String> getDefaultHeaders() {
Map<String, String> headers = new HashMap<String, String>();
headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
return headers;
}

public enum Method {
GET,
POST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,19 @@ 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<String,Object> request) {
public ResponseEntity<?> userSignup(@RequestBody Map<String, Object> request) {
SBApiResponse response = profileService.userSignup(request);
return new ResponseEntity<>(response, response.getResponseCode());
}

@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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface ProfileService {

SBApiResponse userSignup(Map<String, Object> request);

SBApiResponse bulkUpload(MultipartFile mFile, String orgId, String userId);
SBApiResponse bulkUpload(MultipartFile mFile, String orgId, String orgName, String userId);

SBApiResponse getBulkUploadDetails(String orgId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public SBApiResponse userSignup(Map<String, Object> 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());
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -1184,4 +1185,29 @@ private String validateSignupRequest(Map<String, Object> requestData) {

return strBuilder.toString();
}

private void sendBulkUploadNotification(String orgId, String orgName, String fileUrl) {
for (String email : serverConfig.getBulkUploadEmailNotificationList()) {
if (StringUtils.isBlank(email)) {
return;
}
}
Map<String, Object> request = new HashMap<>();
Map<String, Object> requestBody = new HashMap<String, Object>();
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ 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());
fos.close();
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<String, String> uploadedFile = new HashMap<>();
uploadedFile.put(Constants.NAME, file.getName());
uploadedFile.put(Constants.URL, url);
Expand All @@ -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();
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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!

0 comments on commit 6b10463

Please sign in to comment.