Skip to content

Commit

Permalink
avniproject/avni-webapp#1254 | Fix import of groupPrivileges for old …
Browse files Browse the repository at this point in the history
…bundles without PrivilegeType info
  • Loading branch information
himeshr committed Oct 1, 2024
1 parent 733ad47 commit 5cd554b
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public List<GroupPrivilege> getAllGroupPrivileges(long groupId) {
public void savePrivilegesFromBundle(GroupPrivilegeBundleContract[] groupPrivilegeBundleContracts, Organisation organisation) {
List<GroupPrivilege> groupPrivileges = groupPrivilegeRepository.findByImplVersion(GroupPrivilege.IMPL_VERSION);
Map<PrivilegeType, Privilege> privilegeMapByType = privilegeRepository.findAll().stream().collect(Collectors.toMap(Privilege::getType, Function.identity()));
Map<String, Privilege> privilegeMapByTypeUUID = privilegeRepository.findAll().stream().collect(Collectors.toMap(Privilege::getUuid, Function.identity()));
List<SubjectType> subjectTypes = subjectTypeRepository.findAll();
List<Program> programs = programRepository.findAll();
List<EncounterType> encounterTypes = encounterTypeRepository.findAll();
Expand All @@ -187,10 +188,13 @@ public void savePrivilegesFromBundle(GroupPrivilegeBundleContract[] groupPrivile
Arrays.stream(groupPrivilegeBundleContracts).forEach(groupPrivilegeBundleContract -> {
try {
Group targetedGroup = getGroup(groupPrivilegeBundleContract, organisation, groups);
Privilege bundleContractPrivilege = Objects.isNull(groupPrivilegeBundleContract.getPrivilegeType())
? privilegeMapByTypeUUID.get(groupPrivilegeBundleContract.getPrivilegeUUID())
: privilegeMapByType.get(groupPrivilegeBundleContract.getPrivilegeType());
GroupPrivilege groupPrivilege = groupPrivileges.stream().filter(gp ->
Objects.equals(targetedGroup.getUuid(), gp.getGroupUuid())
// rely on type since privilege uuid could be different across different installations
&& Objects.equals(groupPrivilegeBundleContract.getPrivilegeType(), gp.getPrivilege().getType())
// rely on type since bundleContractPrivilege uuid could be different across different installations
&& Objects.equals(bundleContractPrivilege.getType(), gp.getPrivilege().getType())
&& Objects.equals(groupPrivilegeBundleContract.getSubjectTypeUUID(), gp.getSubjectTypeUuid())
&& Objects.equals(groupPrivilegeBundleContract.getProgramUUID(), gp.getProgramUuid())
&& Objects.equals(groupPrivilegeBundleContract.getProgramEncounterTypeUUID(), gp.getProgramEncounterTypeUuid())
Expand All @@ -201,7 +205,7 @@ public void savePrivilegesFromBundle(GroupPrivilegeBundleContract[] groupPrivile
groupPrivilege = new GroupPrivilege();
//don't use uuid from request for bundle uploads since there could be records with matching uuid with older impl_version in db and unique org_uuid constraint is violated
groupPrivilege.assignUUID();
groupPrivilege.setPrivilege(privilegeMapByType.get(groupPrivilegeBundleContract.getPrivilegeType()));
groupPrivilege.setPrivilege(bundleContractPrivilege);
groupPrivilege.setSubjectType(CollectionUtil.findByUuid(subjectTypes, groupPrivilegeBundleContract.getSubjectTypeUUID()));
groupPrivilege.setProgram(CollectionUtil.findByUuid(programs, groupPrivilegeBundleContract.getProgramUUID()));
groupPrivilege.setEncounterType(CollectionUtil.findByUuid(encounterTypes, groupPrivilegeBundleContract.getEncounterTypeUUID()));
Expand Down

0 comments on commit 5cd554b

Please sign in to comment.