Skip to content

Commit

Permalink
4.8.2 org create fix (#216)
Browse files Browse the repository at this point in the history
* Fixes for org create issue

* Fix for org list and updating rootOrgId
  • Loading branch information
karthik-tarento authored May 10, 2023
1 parent 1d7081d commit e994582
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ public interface OrgHierarchyRepository extends JpaRepository<OrgHierarchy, Inte
@Query(value = "UPDATE org_hierarchy_v2 set sborgid=?2, sbrootorgid=?3 where channel=?1", nativeQuery = true)
void updateSbOrgIdAndSbOrgRootIdForChannel(String channel, String sbOrgId, String sbRootOrgId);

@Query(value = "SELECT sborgid from org_hierarchy_v2 where sbrootorgid=?1", nativeQuery=true)
@Query(value = "SELECT sborgid from org_hierarchy_v2 where sbrootorgid=?1", nativeQuery = true)
List<String> findAllBySbRootOrgId(String sbRootOrgId);

@Query(value = "SELECT sborgid from org_hierarchy_v2 where sbrootorgid in (?1)", nativeQuery = true)
List<String> fetchL2LevelOrgList(List<String> orgIdList);

@Query(value = "SELECT * from org_hierarchy_v2 where mapid in (?1)", nativeQuery = true)
List<OrgHierarchy> searchOrgForL1MapId(Set<String> l1MapIdSet);

OrgHierarchy findByOrgNameAndParentMapId(String orgName, String parentMapId);
}
96 changes: 74 additions & 22 deletions src/main/java/org/sunbird/org/service/ExtendedOrgServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,84 @@ public SBApiResponse createOrg(Map<String, Object> request, String userToken) {
}

Map<String, Object> requestData = (Map<String, Object>) request.get(Constants.REQUEST);

String channelName = prepareChannelName((String) requestData.get(Constants.PARENT_MAP_ID), requestData);
String orgId = checkOrgExist((String) requestData.get(Constants.CHANNEL), userToken);
String orgType = (String) requestData.get(Constants.ORGANIZATION_TYPE);
if (StringUtils.isBlank(channelName)) {
// We tried to construct channelName but returred empty.
// So orgType could be only ministry / state.
String channelName = null;
boolean dbUpdateRequired = false;
boolean orgCreatedWithNewChannel = false;

if (StringUtils.isEmpty(orgId)) {
// There is no org exist for given Channel. We can simply create the same in
// system.
orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), userToken);
if (StringUtils.isBlank(orgId)) {
response.getParams().setErrmsg("Failed to create organisation in Sunbird.");
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
return response;
}
dbUpdateRequired = true;
} else {
// The channel already exist. We need to check OrgHierarchy table for duplicate.
if (Constants.STATE.equalsIgnoreCase(orgType)
|| Constants.MINISTRY.equalsIgnoreCase(orgType)) {
channelName = (String) requestData.get(Constants.CHANNEL);
// We are not allowing duplicates @ L1 -- Need to throw error
response.getParams().setErrmsg("Organisation is already exist.");
response.setResponseCode(HttpStatus.BAD_REQUEST);
return response;
} else {
log.error("Failed to identify channelName.", null);
// Need to treat this as error.
OrgHierarchy existingDBRecord = orgRepository.findByOrgNameAndParentMapId(
(String) requestData.get(Constants.CHANNEL),
(String) requestData.get(Constants.PARENT_MAP_ID));
if (existingDBRecord == null) {
channelName = prepareChannelName((String) requestData.get(Constants.PARENT_MAP_ID),
requestData);
channelName = channelName + (String) requestData.get(Constants.CHANNEL);
requestData.put(Constants.CHANNEL, channelName);
orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), userToken);
if (StringUtils.isBlank(orgId)) {
response.getParams().setErrmsg("Failed to create organisation in Sunbird.");
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
return response;
}
dbUpdateRequired = true;
orgCreatedWithNewChannel = true;
} else if (StringUtils.isBlank(existingDBRecord.getSbOrgId())) {
existingDBRecord.setSbOrgId(orgId);
if (StringUtils.isEmpty(existingDBRecord.getSbRootOrgId())) {
existingDBRecord.setSbRootOrgId(fetchRootOrgId(requestData));
}
orgRepository.save(existingDBRecord);
} else if (existingDBRecord.getSbOrgId().equalsIgnoreCase(orgId)) {
response.getParams().setErrmsg("Duplicate Record Found in OrgHierarchy. Contact Admin");
response.setResponseCode(HttpStatus.BAD_REQUEST);
return response;
}
}

} else {
channelName = channelName + (String) requestData.get(Constants.CHANNEL);
requestData.put(Constants.CHANNEL, channelName);
}

String orgId = checkOrgExist((String) requestData.get(Constants.CHANNEL), userToken);

if (StringUtils.isEmpty(orgId)) {
orgId = createOrgInSunbird(request, (String) requestData.get(Constants.CHANNEL), userToken);
if (dbUpdateRequired) {
OrgHierarchy existingDBRecord = orgRepository.findByOrgNameAndParentMapId(
(String) requestData.get(Constants.CHANNEL), (String) requestData.get(Constants.PARENT_MAP_ID));
if (existingDBRecord != null) {
existingDBRecord.setSbOrgId(orgId);
if (StringUtils.isEmpty(existingDBRecord.getSbRootOrgId())) {
existingDBRecord.setSbRootOrgId(fetchRootOrgId(requestData));
}
orgRepository.save(existingDBRecord);
} else {
// We just created with given channel name. but this is new record in
// orgHierarchy...
// By calling prepareChannelName we will update L1 and L2 details.
prepareChannelName((String) requestData.get(Constants.PARENT_MAP_ID), requestData);
channelName = (String) requestData.get(Constants.CHANNEL);
orgCreatedWithNewChannel = true;
}
}

if (!StringUtils.isEmpty(orgId)) {
if (orgCreatedWithNewChannel) {
Map<String, Object> updateRequest = new HashMap<String, Object>();
String orgName = (String) requestData.get(Constants.ORG_NAME);
updateRequest.put(Constants.CHANNEL, channelName);
updateRequest.put(Constants.CHANNEL, (String) requestData.get(Constants.CHANNEL));
updateRequest.put(Constants.SB_ORG_ID, orgId);
updateRequest.put(Constants.ORG_NAME, orgName);
updateRequest.put(Constants.SB_ORG_TYPE, orgType);
Expand Down Expand Up @@ -142,9 +191,6 @@ public SBApiResponse createOrg(Map<String, Object> request, String userToken) {
}
response.getResult().put(Constants.ORGANIZATION_ID, orgId);
response.getResult().put(Constants.RESPONSE, Constants.SUCCESS);
} else {
response.getParams().setErrmsg("Failed to create organisation in Sunbird.");
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
log.error(e);
Expand Down Expand Up @@ -197,8 +243,14 @@ public SBApiResponse orgExtSearch(Map<String, Object> request) throws Exception
Map<String, Object> filters = (Map<String, Object>) requestData.get(Constants.FILTERS);
String sbRootOrgId = (String) filters.get(Constants.SB_ROOT_ORG_ID);

//sbRootOrgId is State Id. Let's get all the children (i.e. departments)
List<String> orgIdList = orgRepository.findAllBySbRootOrgId(sbRootOrgId);

if (CollectionUtils.isNotEmpty(orgIdList)) {
List<String> orgIdChildList = orgRepository.fetchL2LevelOrgList(orgIdList);
if(CollectionUtils.isNotEmpty(orgIdChildList)) {
orgIdList.addAll(orgIdChildList);
}
SBApiOrgSearchRequest orgSearchRequest = new SBApiOrgSearchRequest();
orgSearchRequest.getFilters().setId(orgIdList);
if (!ProjectUtil.isStringNullOREmpty((String) requestData.get(Constants.QUERY))) {
Expand Down Expand Up @@ -505,7 +557,7 @@ private void fetchMapIdFromDB(Map<String, Object> requestData) {
OrgHierarchy existingOrg = orgList.get(0);
// Check given parentMapId is same as existing record parentMapId.
if (existingOrg.getParentMapId().equalsIgnoreCase((String) requestData.get(Constants.PARENT_MAP_ID))) {
requestData.put(Constants.MAP_ID, existingOrg.getParentMapId());
requestData.put(Constants.MAP_ID, existingOrg.getMapId());
}
}
}
Expand Down

0 comments on commit e994582

Please sign in to comment.