Skip to content

Commit

Permalink
Merged 4.0.1-autoOrgCreate branch
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Jul 6, 2022
1 parent 072bed0 commit e09fe3f
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ private String validateOrgSearchReq(Map<String, Object> requestData) {
private String checkOrgExist(String channel, String userToken) {
Map<String, String> headers = new HashMap<String, String>();
headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
headers.put(Constants.X_AUTH_TOKEN, userToken);
if (StringUtils.isNotEmpty(userToken)) {
headers.put(Constants.X_AUTH_TOKEN, userToken);
}
Map<String, Object> filterMap = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
Expand Down Expand Up @@ -288,7 +290,9 @@ private String createOrgInSunbird(Map<String, Object> request, String channel, S
String url = configProperties.getSbUrl() + configProperties.getLmsOrgCreatePath();
Map<String, String> headers = new HashMap<String, String>();
headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
headers.put(Constants.X_AUTH_TOKEN, userToken);
if (StringUtils.isNotEmpty(userToken)) {
headers.put(Constants.X_AUTH_TOKEN, userToken);
}

Map<String, Object> apiResponse = (Map<String, Object>) outboundService.fetchResultUsingPost(url, request,
headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ public class UserRegistrationInfo {
private String firstName;
private String lastName;
private String email;
private String deptId;
private String deptName;
private String position;
private String source;
private String orgName;
private String channel;
private String organisationType;
private String organisationSubType;
private String mapId;
private String sbRootOrgId;
private String sbOrgId;

public String getRegistrationCode() {
return registrationCode;
Expand Down Expand Up @@ -48,22 +53,6 @@ public void setEmail(String email) {
this.email = email;
}

public String getDeptId() {
return deptId;
}

public void setDeptId(String deptId) {
this.deptId = deptId;
}

public String getDeptName() {
return deptName;
}

public void setDeptName(String deptName) {
this.deptName = deptName;
}

public String getPosition() {
return position;
}
Expand All @@ -80,4 +69,59 @@ public void setSource(String source) {
this.source = source;
}

public String getOrgName() {
return orgName;
}

public void setOrgName(String orgName) {
this.orgName = orgName;
}

public String getChannel() {
return channel;
}

public void setChannel(String channel) {
this.channel = channel;
}

public String getOrganisationType() {
return organisationType;
}

public void setOrganisationType(String organisationType) {
this.organisationType = organisationType;
}

public String getOrganisationSubType() {
return organisationSubType;
}

public void setOrganisationSubType(String organisationSubType) {
this.organisationSubType = organisationSubType;
}

public String getMapId() {
return mapId;
}

public void setMapId(String mapId) {
this.mapId = mapId;
}

public String getSbRootOrgId() {
return sbRootOrgId;
}

public void setSbRootOrgId(String sbRootOrgId) {
this.sbRootOrgId = sbRootOrgId;
}

public String getSbOrgId() {
return sbOrgId;
}

public void setSbOrgId(String sbOrgId) {
this.sbOrgId = sbOrgId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private WfRequest wfRequestObj(UserRegistration userRegistration) {
wfRequest.setUserId(uuid);
wfRequest.setActorUserId(uuid);
wfRequest.setApplicationId(userRegistration.getRegistrationCode());
wfRequest.setDeptName(userRegistration.getDeptName());
wfRequest.setDeptName(userRegistration.getOrgName());
wfRequest.setServiceName(serverProperties.getUserRegistrationWorkFlowServiceName());
wfRequest.setUpdateFieldValues(Arrays.asList(new HashMap<>()));
return wfRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.sunbird.common.util.IndexerService;
import org.sunbird.core.exception.ApplicationLogicError;
import org.sunbird.core.producer.Producer;
import org.sunbird.org.service.ExtendedOrgService;
import org.sunbird.portal.department.model.DeptPublicInfo;
import org.sunbird.user.registration.model.UserRegistration;
import org.sunbird.user.registration.model.UserRegistrationInfo;
Expand Down Expand Up @@ -79,6 +80,9 @@ public class UserRegistrationServiceImpl implements UserRegistrationService {
@Autowired
RedisCacheMgr redisCacheMgr;

@Autowired
ExtendedOrgService extOrgService;

@Override
public SBApiResponse registerUser(UserRegistrationInfo userRegInfo) {
SBApiResponse response = createDefaultResponse(Constants.USER_REGISTRATION_REGISTER_API);
Expand Down Expand Up @@ -106,6 +110,7 @@ public SBApiResponse registerUser(UserRegistrationInfo userRegInfo) {
serverProperties.getEsProfileIndexType(), regDocument.getRegistrationCode(),
mapper.convertValue(regDocument, Map.class));
} else {
updateValues(regDocument, userRegInfo);
regDocument.setStatus(UserRegistrationStatus.CREATED.name());
status = indexerService.updateEntity(serverProperties.getUserRegistrationIndex(),
serverProperties.getEsProfileIndexType(), regDocument.getRegistrationCode(),
Expand Down Expand Up @@ -192,6 +197,24 @@ public void initiateCreateUserFlow(String registrationCode) {
*/
LOGGER.info("Initiated User Creation flow for Reg. Code :: " + registrationCode);
UserRegistration userReg = getUserRegistrationForRegCode(registrationCode);

// Create the org if it's not already onboarded.
if (StringUtils.isEmpty(userReg.getSbOrgId())) {
SBApiResponse orgResponse = extOrgService.createOrg(getOrgCreateRequest(userReg), StringUtils.EMPTY);
if (orgResponse.getResponseCode() == HttpStatus.OK) {
String orgId = (String) orgResponse.getResult().get(Constants.ORGANIZATION_ID);
userReg.setSbOrgId(orgId);
LOGGER.info(String.format("Auto on-boarded organisation with Name: %s, MapId: %s, OrgId: %s",
userReg.getOrgName(), userReg.getMapId(), userReg.getSbOrgId()));
} else {
try {
LOGGER.error("Failed to auto onboard organisation. Error: "
+ (new ObjectMapper()).writeValueAsString(orgResponse));
} catch (Exception e) {
}
}
}

UserRegistrationStatus regStatus = UserRegistrationStatus.WF_APPROVED;
if (userUtilityService.createUser(userReg)) {
LOGGER.info("Successfully completed user creation flow.");
Expand Down Expand Up @@ -241,11 +264,11 @@ private String validateRegisterationPayload(UserRegistrationInfo userRegInfo) {
if (StringUtils.isBlank(userRegInfo.getEmail())) {
errList.add("Email");
}
if (StringUtils.isBlank(userRegInfo.getDeptId())) {
errList.add("DeptId");
if (StringUtils.isBlank(userRegInfo.getSbOrgId()) && StringUtils.isBlank(userRegInfo.getMapId())) {
errList.add("DeptId [or] MapId is mandatory.");
}
if (StringUtils.isBlank(userRegInfo.getDeptName())) {
errList.add("Department");
if (StringUtils.isBlank(userRegInfo.getOrgName())) {
errList.add("OrgName");
}
if (StringUtils.isBlank(userRegInfo.getPosition())) {
errList.add("Position");
Expand Down Expand Up @@ -282,18 +305,19 @@ private UserRegistration getRegistrationObject(UserRegistrationInfo userRegInfo)
userRegistration.setFirstName(userRegInfo.getFirstName());
userRegistration.setLastName(userRegInfo.getLastName());
userRegistration.setEmail(userRegInfo.getEmail());
userRegistration.setDeptId(userRegInfo.getDeptId());
userRegistration.setDeptName(userRegInfo.getDeptName());
userRegistration.setSbOrgId(userRegInfo.getSbOrgId());
userRegistration.setOrgName(userRegInfo.getOrgName());
userRegistration.setChannel(userRegistration.getChannel());
userRegistration.setSbRootOrgId(userRegInfo.getSbRootOrgId());
userRegistration.setPosition(userRegInfo.getPosition());
userRegistration.setSource(userRegInfo.getSource());
if (userRegInfo.getDeptId().equalsIgnoreCase(serverProperties.getCustodianOrgId())) {
userRegistration.setProposedDeptName(userRegInfo.getDeptName());
userRegistration.setDeptName(serverProperties.getCustodianOrgName());
}
userRegistration.setMapId(userRegInfo.getMapId());
userRegistration.setOrganisationType(userRegInfo.getOrganisationType());
userRegistration.setOrganisationSubType(userRegInfo.getOrganisationSubType());

if (StringUtils.isBlank(userRegInfo.getRegistrationCode())) {
userRegistration.setRegistrationCode(serverProperties.getUserRegCodePrefix() + "-"
+ userRegistration.getDeptName() + "-" + RandomStringUtils.random(8, Boolean.TRUE, Boolean.TRUE));
+ userRegistration.getMapId() + "-" + RandomStringUtils.random(8, Boolean.TRUE, Boolean.TRUE));
userRegistration.setCreatedOn(new Date().getTime());
} else {
userRegistration.setUpdatedOn(new Date().getTime());
Expand Down Expand Up @@ -463,4 +487,27 @@ private List<DeptPublicInfo> getDepartmentDetails() throws Exception {
redisCacheMgr.putCache(Constants.DEPARTMENT_LIST_CACHE_NAME, deptListMap);
return orgList;
}

private Map<String, Object> getOrgCreateRequest(UserRegistration userReg) {
Map<String, Object> orgRequestBody = new HashMap<String, Object>();
Map<String, Object> orgRequest = new HashMap<String, Object>();
orgRequest.put(Constants.ORG_NAME, userReg.getOrgName());
orgRequest.put(Constants.CHANNEL, userReg.getOrgName());
orgRequest.put(Constants.ORGANIZATION_TYPE, userReg.getOrganisationType());
orgRequest.put(Constants.ORGANIZATION_SUB_TYPE, userReg.getOrganisationSubType());
orgRequest.put(Constants.MAP_ID, userReg.getMapId());
orgRequest.put(Constants.IS_TENANT, true);
orgRequest.put(Constants.SB_ROOT_ORG_ID, userReg.getSbRootOrgId());
orgRequestBody.put(Constants.REQUEST, orgRequest);
return orgRequestBody;
}

private void updateValues(UserRegistration userReg, UserRegistrationInfo userRegInfo) {
userReg.setOrgName(userRegInfo.getOrgName());
userReg.setChannel(userRegInfo.getOrgName());
userReg.setOrganisationType(userRegInfo.getOrganisationType());
userReg.setOrganisationSubType(userRegInfo.getOrganisationSubType());
userReg.setSbRootOrgId(userRegInfo.getSbRootOrgId());
userReg.setSbOrgId(userRegInfo.getSbOrgId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public boolean createUser(UserRegistration userRegistration) {
Map<String, Object> request = new HashMap<>();
Map<String, Object> requestBody = new HashMap<String, Object>();
requestBody.put(Constants.EMAIL, userRegistration.getEmail());
requestBody.put(Constants.CHANNEL, userRegistration.getDeptName());
requestBody.put(Constants.CHANNEL, userRegistration.getOrgName());
requestBody.put(Constants.FIRSTNAME, userRegistration.getFirstName());
requestBody.put(Constants.LASTNAME, userRegistration.getLastName());
requestBody.put(Constants.EMAIL_VERIFIED, true);
Expand Down Expand Up @@ -245,7 +245,7 @@ public boolean updateUser(UserRegistration userRegistration) {
Map<String, Object> profileDetails = new HashMap<String, Object>();
profileDetails.put(Constants.MANDATORY_FIELDS_EXISTS, false);
Map<String, Object> employementDetails = new HashMap<String, Object>();
employementDetails.put(Constants.DEPARTMENTNAME, userRegistration.getDeptName());
employementDetails.put(Constants.DEPARTMENTNAME, userRegistration.getOrgName());
profileDetails.put(Constants.EMPLOYMENTDETAILS, employementDetails);
Map<String, Object> personalDetails = new HashMap<String, Object>();
personalDetails.put(Constants.FIRSTNAME.toLowerCase(), userRegistration.getFirstName());
Expand All @@ -261,7 +261,7 @@ public boolean updateUser(UserRegistration userRegistration) {
List<Map<String, Object>> professionalDetailsList = new ArrayList<Map<String, Object>>();
professionalDetailsList.add(professionDetailObj);
profileDetails.put(Constants.PROFESSIONAL_DETAILS, professionalDetailsList);

requestBody.put(Constants.PROFILE_DETAILS, profileDetails);
request.put(Constants.REQUEST, requestBody);
Map<String, Object> readData = (Map<String, Object>) outboundRequestHandlerService
Expand All @@ -277,7 +277,7 @@ public boolean assignRole(UserRegistration userRegistration) {
boolean retValue = false;
Map<String, Object> request = new HashMap<>();
Map<String, Object> requestBody = new HashMap<String, Object>();
requestBody.put(Constants.ORGANIZATION_ID, userRegistration.getDeptId());
requestBody.put(Constants.ORGANIZATION_ID, userRegistration.getSbOrgId());
requestBody.put(Constants.USER_ID, userRegistration.getUserId());
requestBody.put(Constants.ROLES, Arrays.asList(Constants.PUBLIC));
request.put(Constants.REQUEST, requestBody);
Expand All @@ -289,7 +289,7 @@ public boolean assignRole(UserRegistration userRegistration) {
printMethodExecutionResult("AssignRole", userRegistration.toMininumString(), retValue);
return retValue;
}

@Override
public boolean createNodeBBUser(UserRegistration userRegistration) {
boolean retValue = false;
Expand Down Expand Up @@ -341,7 +341,7 @@ public boolean sendWelcomeEmail(String activationLink, UserRegistration userRegi
requestBody.put(Constants.FIRSTNAME, userRegistration.getFirstName());
requestBody.put(Constants.LINK, activationLink);
requestBody.put(Constants.MODE, Constants.EMAIL);
requestBody.put(Constants.ORG_NAME, userRegistration.getDeptName());
requestBody.put(Constants.ORG_NAME, userRegistration.getOrgName());
requestBody.put(Constants.RECIPIENT_EMAILS, Arrays.asList(userRegistration.getEmail()));
requestBody.put(Constants.SET_PASSWORD_LINK, true);
requestBody.put(Constants.SUBJECT, props.getWelcomeEmailSubject());
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ user.enable.multidept.mapping=false
#learner.service.url=learner-service:9000
sb.service.url=http://learner-service:9000
sb.org.search.path=/v1/org/search
sb.org.create.path=/v1/org/create
sb.org.create.path=/private/v1/org/create
sb.service.user.create.path=/v3/user/create
sb.service.reset.password.path=/private/user/v1/password/reset
sb.service.send.notify.email.path=/private/user/v1/notification/email
Expand Down

0 comments on commit e09fe3f

Please sign in to comment.