Skip to content

Commit

Permalink
Merge pull request #289 from bcgov/feature/invitations
Browse files Browse the repository at this point in the history
Added endpoint for invitations
  • Loading branch information
SodhiA1 authored Nov 18, 2023
2 parents c6be88e + 1f98f33 commit 985fce9
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class URL {
public static final String MINISTRY_TEAMS = "/ministry-teams";
public static final String USER_SCHOOLS = "/user-schools";
public static final String USER_DISTRICTS = "/user-districts";
public static final String INVITATIONS = "/user-invitations";

private URL(){

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public EdxUser retrieveEdxUser(String id) {
return userMapper.toStructure(getService().retrieveEdxUserByID(UUIDUtil.fromString(id)));
}

@Override
public List<EdxActivationCode> findAllInvitations(String instituteType) {
return getService().getEdxUserInvitations(instituteType).stream().map(EDX_ACTIVATION_CODE_MAPPER::toStructure).collect(Collectors.toList());
}

@Override
public List<EdxUser> findEdxUsers(Optional<UUID> digitalId, Optional<UUID> schoolID, String firstName, String lastName, Optional<UUID> districtID) {
return getService().findEdxUsers(digitalId, schoolID, firstName, lastName, districtID).stream().map(userMapper::toStructure).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public interface EdxUsersEndpoint {
EdxUser retrieveEdxUser(@PathVariable String id);


@PreAuthorize("hasAuthority('SCOPE_READ_EDX_USERS')")
@GetMapping(URL.INVITATIONS)
@Transactional(readOnly = true)
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
List<EdxActivationCode> findAllInvitations(@RequestParam(name = "instituteType") String instituteType);

/**
* This api method will accept all or individual parameters and search the DB. if any parameter is null then it will be not included in the query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class EdxActivationCodeEntity {
@Column(name = "EDX_USER_ID")
UUID edxUserId;

@Column(name = "LINKED_EDX_USER_ID")
UUID linkedEdxUserId;

@NotNull(message = "isPrimary cannot be null")
@Column(name = "IS_PRIMARY")
Boolean isPrimary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public interface EdxActivationCodeRepository extends JpaRepository<EdxActivation
Optional<EdxActivationCodeEntity> findEdxActivationCodeEntitiesByDistrictIDAndIsPrimaryTrueAndSchoolIDIsNull(UUID districtID);

List<EdxActivationCodeEntity> findEdxActivationCodeEntitiesByEdxUserId(UUID edxUserId);

List<EdxActivationCodeEntity> findAllByDistrictIDIsNotNullAndIsPrimaryIsFalse();

List<EdxActivationCodeEntity> findAllBySchoolIDIsNotNullAndIsPrimaryIsFalse();
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public void purgeClosedMessages() {
documentRepository.deleteByCreateDateBefore(createDate);
secureExchangeRepository.deleteByCreateDateBefore(createDate);
log.info("Purged closed messages scheduler");

}

private LocalDateTime calculateCreateDateBasedOnMessageAge() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ public List<String> getEdxUserSchoolsList(String permissionCode) {
return schoolIDBytes.stream().map(school -> school.getSchoolID().toString()).distinct().toList();
}

public List<EdxActivationCodeEntity> getEdxUserInvitations(String instituteType) {
if(instituteType.equalsIgnoreCase(InstituteTypeCode.DISTRICT.toString())){
return edxActivationCodeRepository.findAllByDistrictIDIsNotNullAndIsPrimaryIsFalse();
}else if(instituteType.equalsIgnoreCase(InstituteTypeCode.SCHOOL.toString())){
return edxActivationCodeRepository.findAllBySchoolIDIsNotNullAndIsPrimaryIsFalse();
}
return edxActivationCodeRepository.findAll();
}

public List<String> getEdxUserDistrictsList(String permissionCode) {
List<EdxUserDistrictEntity> districtIDs = edxUserDistrictRepository.findDistrictsByPermission(permissionCode);
return districtIDs.stream().map(district -> district.getDistrictID().toString()).distinct().toList();
Expand Down Expand Up @@ -503,8 +512,8 @@ private EdxUserEntity updateEdxUserDetailsFromActivationCodeDetails(List<EdxUser
val edxUserEntity = getEdxUserRepository().findById(edxUsers.get(0).getEdxUserID()).get();
createEdxUserDetails(edxActivateUser, edxActivationCodeEntity, edxUserEntity);
updateAuditColumnsForEdxUserEntityUpdate(edxUserEntity, edxActivateUser);
val updatedUser = edxUserRepository.save(edxUserEntity);
expireActivationCodes(edxActivationCodeEntity, edxActivateUser);
EdxUserEntity updatedUser = edxUserRepository.save(edxUserEntity);
expireActivationCodes(edxActivationCodeEntity, edxActivateUser, updatedUser);
return updatedUser;
}

Expand All @@ -520,9 +529,9 @@ private EdxUserEntity createEdxUserDetailsFromActivationCodeDetails(EdxActivateU
val edxUserEntity = createEdxUserFromActivationCodeDetails(edxUser, edxActivateUser, edxActivationCodeEntity);
createEdxUserDetails(edxActivateUser, edxActivationCodeEntity, edxUserEntity);

val savedEntity = edxUserRepository.save(edxUserEntity);
EdxUserEntity savedEntity = edxUserRepository.save(edxUserEntity);
//expire the activationCodes
expireActivationCodes(edxActivationCodeEntity, edxActivateUser);
expireActivationCodes(edxActivationCodeEntity, edxActivateUser, savedEntity);
return savedEntity;
}

Expand Down Expand Up @@ -573,11 +582,12 @@ private void updateEdxUserAssociationsForDistrict(EdxUserEntity edxUserEntity, E
* @param edxActivationCode the edx activation code
* @param edxActivateUser the edx activate user
*/
private void expireActivationCodes(EdxActivationCodeEntity edxActivationCode, EdxActivateUser edxActivateUser) {
private void expireActivationCodes(EdxActivationCodeEntity edxActivationCode, EdxActivateUser edxActivateUser, EdxUserEntity user) {
val optionalEdxActivationCodeEntity = getEdxActivationCodeRepository().findById(edxActivationCode.getEdxActivationCodeId());
val activationCodeEntity = optionalEdxActivationCodeEntity.orElseThrow(() -> new EntityNotFoundException(EdxActivationCodeEntity.class, EDX_ACTIVATION_CODE_ID, edxActivationCode.getEdxActivationCodeId().toString()));
if (!activationCodeEntity.getIsPrimary()) {//expire only the personal activation code
activationCodeEntity.setExpiryDate(LocalDateTime.now());
activationCodeEntity.setLinkedEdxUserId(user.getEdxUserID());
activationCodeEntity.setUpdateUser(edxActivateUser.getUpdateUser());
activationCodeEntity.setUpdateDate(LocalDateTime.now());
getEdxActivationCodeRepository().save(activationCodeEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ public class EdxActivationCode extends BaseRequest implements Serializable {

String edxUserId;

String linkedEdxUserId;

String edxUserExpiryDate;
}
2 changes: 2 additions & 0 deletions api/src/main/resources/db/migration/V1.0.40__EDX_API.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE EDX_ACTIVATION_CODE ADD COLUMN LINKED_EDX_USER_ID UUID;

Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ void testRetrieveMinistryTeams_ShouldReturnOkStatus() throws Exception {
.andExpect(jsonPath("$.[0].description", is("THISISDESCRIPTION")));
}

@Test
void testFindAllSchoolInvitations_ShouldReturnOkStatus() throws Exception {
UUID validationCode = UUID.randomUUID();
UUID schoolID = UUID.randomUUID();
this.createActivationCodeTableDataForSchoolUser(this.edxActivationCodeRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxActivationRoleRepository, true,validationCode, 2, schoolID);
this.mockMvc.perform(get(URL.BASE_URL_USERS + URL.INVITATIONS + "?instituteType=SCHOOL")
.with(jwt().jwt((jwt) -> jwt.claim("scope", "READ_EDX_USERS"))))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)));
}

@Test
void testFindAllDistrictInvitations_ShouldReturnOkStatus() throws Exception {
UUID validationCode = UUID.randomUUID();
UUID districtID = UUID.randomUUID();
this.createActivationCodeTableDataForDistrictUser(this.edxActivationCodeRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxActivationRoleRepository, true,validationCode, 2, districtID);
UUID districtID2 = UUID.randomUUID();
this.createActivationCodeTableDataForDistrictUser(this.edxActivationCodeRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxActivationRoleRepository, true,validationCode, 2, districtID2);
this.mockMvc.perform(get(URL.BASE_URL_USERS + URL.INVITATIONS + "?instituteType=DISTRICT")
.with(jwt().jwt((jwt) -> jwt.claim("scope", "READ_EDX_USERS"))))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)));
}

@Test
void testRetrieveUsers_GivenValidID_ShouldReturnOkStatus() throws Exception {
var entity = this.createUserEntity(this.edxUserRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxUserSchoolRepository, this.edxUserDistrictRepository);
Expand Down

0 comments on commit 985fce9

Please sign in to comment.