Skip to content

Commit

Permalink
Merge pull request #81 from CaritasDeutschland/update-consultant-admi…
Browse files Browse the repository at this point in the history
…n-api

Update consultant admin api
  • Loading branch information
pago4b authored Dec 18, 2020
2 parents a2df676 + 3354a45 commit af773b6
Show file tree
Hide file tree
Showing 51 changed files with 1,322 additions and 419 deletions.
24 changes: 22 additions & 2 deletions api/useradminservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ paths:
operationId: updateConsultant
requestBody:
content:
'application/hal+json':
'application/json':
schema:
$ref: '#/components/schemas/UpdateConsultantDTO'
required: true
Expand All @@ -141,7 +141,7 @@ paths:
200:
description: OK - consultant was updated successfully
content:
'application/json':
'application/hal+json':
schema:
$ref: '#/components/schemas/ConsultantAdminResponseDTO'
400:
Expand Down Expand Up @@ -549,15 +549,23 @@ components:
username:
type: string
example: "max.mustermann"
minLength: 1
maxLength: 255
firstname:
type: string
example: "Max"
minLength: 1
maxLength: 255
lastname:
type: string
example: "Mustermann"
minLength: 1
maxLength: 255
email:
type: string
example: "[email protected]"
minLength: 1
maxLength: 255
formalLanguage:
type: boolean
example: true
Expand All @@ -567,6 +575,8 @@ components:
absenceMessage:
type: string
example: "I am absent until..."
minLength: 1
maxLength: 10000

UpdateConsultantDTO:
type: object
Expand All @@ -580,12 +590,18 @@ components:
firstname:
type: string
example: "Max"
minLength: 1
maxLength: 255
lastname:
type: string
example: "Mustermann"
minLength: 1
maxLength: 255
email:
type: string
example: "[email protected]"
minLength: 1
maxLength: 255
formalLanguage:
type: boolean
example: true
Expand All @@ -595,6 +611,8 @@ components:
absenceMessage:
type: string
example: "I am absent until..."
minLength: 1
maxLength: 10000

ConsultantDTO:
type: object
Expand Down Expand Up @@ -737,6 +755,8 @@ components:
example: 15
role:
type: string
minLength: 1
maxLength: 255

ViolationDTO:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public ResponseEntity<RootDTO> getRoot() {
/**
* Entry point to retrieve sessions.
*
* @param page Number of page where to start in the query (1 = first page) (required)
* @param perPage Number of items which are being returned (required)
* @param page Number of page where to start in the query (1 = first page) (required)
* @param perPage Number of items which are being returned (required)
* @param sessionFilter The filters to restrict results (optional)
* @return an entity containing the filtered sessions
*/
Expand All @@ -70,7 +70,7 @@ public ResponseEntity<SessionAdminResultDTO> getSessions(@NotNull @Valid Integer
/**
* Entry point to retrieve all consulting types.
*
* @param page Number of page where to start in the query (1 = first page) (required)
* @param page Number of page where to start in the query (1 = first page) (required)
* @param perPage Number of items which are being returned per page (required)
* @return an entity containing the consulting types as {@link ConsultingTypeAdminResultDTO}
*/
Expand Down Expand Up @@ -121,14 +121,15 @@ public ResponseEntity<Void> markConsultantForDeletion(@PathVariable String consu
/**
* Entry point to update a consultant.
*
* @param consultantId consultant id (required)
* @param consultantId consultant id (required)
* @param updateConsultantDTO (required)
* @return {@link ConsultantAdminResponseDTO}
*/
@Override
public ResponseEntity<ConsultantAdminResponseDTO> updateConsultant(
@PathVariable String consultantId, @Valid UpdateConsultantDTO updateConsultantDTO) {
return null;
return ResponseEntity
.ok(this.consultantAdminFacade.updateConsultant(consultantId, updateConsultantDTO));
}

/**
Expand All @@ -139,18 +140,18 @@ public ResponseEntity<ConsultantAdminResponseDTO> updateConsultant(
*/
@Override
public ResponseEntity<ConsultantAdminResponseDTO> getConsultant(String consultantId) {
ConsultantAdminResponseDTO responseDTO = this.consultantAdminFacade.findConsultant(consultantId);
ConsultantAdminResponseDTO responseDTO = this.consultantAdminFacade
.findConsultant(consultantId);
return ResponseEntity.ok(responseDTO);
}

/**
* Entry point to retrieve consultants.
*
* @param page Number of page where to start in the query (1 &#x3D; first page)
* (required)
* @param perPage Number of items which are being returned per page (required)
* @param page Number of page where to start in the query (1 &#x3D; first page) (required)
* @param perPage Number of items which are being returned per page (required)
* @param consultantFilter The filter parameters to search for. If no filter is set all consultant
* are being returned. (optional)
* are being returned. (optional)
* @return an entity containing the filtered sessions
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.caritas.cob.userservice.api.model.ConsultantResponseDTO;
import de.caritas.cob.userservice.api.model.ConsultantSearchResultDTO;
import de.caritas.cob.userservice.api.model.CreateConsultantDTO;
import de.caritas.cob.userservice.api.model.UpdateConsultantDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -40,8 +41,8 @@ public ConsultantAdminResponseDTO findConsultant(String consultantId) {
* generates a {@link ConsultantSearchResultDTO} containing hal links.
*
* @param consultantFilter the filter object containing filter values
* @param page the current requested page
* @param perPage the amount of items in one page
* @param page the current requested page
* @param perPage the amount of items in one page
* @return the result list
*/
public ConsultantSearchResultDTO findFilteredConsultants(Integer page, Integer perPage,
Expand All @@ -61,13 +62,26 @@ public ConsultantAdminResponseDTO createNewConsultant(CreateConsultantDTO create
return this.consultantAdminService.createNewConsultant(createConsultantDTO);
}

/**
* Updates a {@link Consultant} based on the {@link UpdateConsultantDTO} input.
*
* @param consultantId the id of the consultant to update
* @param updateConsultantDTO the input data used for {@link Consultant} update
* @return the generated and persisted {@link Consultant} representation as {@link
* ConsultantAdminResponseDTO}
*/
public ConsultantAdminResponseDTO updateConsultant(String consultantId,
UpdateConsultantDTO updateConsultantDTO) {
return this.consultantAdminService.updateConsultant(consultantId, updateConsultantDTO);
}

/**
* Returns all Agencies for the given consultantId.
*
* @param consultantId id of the consultant
* @return the list of agencies for the given consultant
*/
public ConsultantAgencyAdminResultDTO findConsultantAgencies(String consultantId) {
return consultantAgencyAdminService.findConsultantAgencies(consultantId);
return this.consultantAgencyAdminService.findConsultantAgencies(consultantId);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package de.caritas.cob.userservice.api.admin.service.consultant;

import de.caritas.cob.userservice.api.admin.service.consultant.create.ConsultantCreatorService;
import de.caritas.cob.userservice.api.admin.service.consultant.update.ConsultantUpdateService;
import de.caritas.cob.userservice.api.exception.httpresponses.NoContentException;
import de.caritas.cob.userservice.api.model.ConsultantAdminResponseDTO;
import de.caritas.cob.userservice.api.model.ConsultantResponseDTO;
import de.caritas.cob.userservice.api.model.CreateConsultantDTO;
import de.caritas.cob.userservice.api.model.UpdateConsultantDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.consultant.ConsultantRepository;
import lombok.NonNull;
Expand All @@ -20,6 +22,7 @@ public class ConsultantAdminService {

private final @NonNull ConsultantRepository consultantRepository;
private final @NonNull ConsultantCreatorService consultantCreatorService;
private final @NonNull ConsultantUpdateService consultantUpdateService;

/**
* Finds a {@link Consultant} by the given consultant id and throws a {@link NoContentException}
Expand Down Expand Up @@ -51,4 +54,20 @@ public ConsultantAdminResponseDTO createNewConsultant(CreateConsultantDTO create
.buildResponseDTO();
}

/**
* Updates a new {@link Consultant} based on the {@link UpdateConsultantDTO} input.
*
* @param consultantId the id of consultant to be updated
* @param updateConsultantDTO the input data used for {@link Consultant} update
* @return the generated and persisted {@link Consultant} representation as {@link
* ConsultantAdminResponseDTO}
*/
public ConsultantAdminResponseDTO updateConsultant(String consultantId,
UpdateConsultantDTO updateConsultantDTO) {
Consultant updatedConsultant = this.consultantUpdateService.updateConsultant(consultantId,
updateConsultantDTO);

return ConsultantResponseDTOBuilder.getInstance(updatedConsultant)
.buildResponseDTO();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.apache.commons.lang3.BooleanUtils.isTrue;
import static org.hibernate.validator.internal.util.CollectionHelper.asSet;

import de.caritas.cob.userservice.api.admin.service.consultant.validation.ConsultantInputValidator;
import de.caritas.cob.userservice.api.admin.service.consultant.validation.CreateConsultantDTOAbsenceInputAdapter;
import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatLoginException;
import de.caritas.cob.userservice.api.helper.UserHelper;
Expand All @@ -14,7 +16,7 @@
import de.caritas.cob.userservice.api.service.ConsultantImportService.ImportRecord;
import de.caritas.cob.userservice.api.service.ConsultantService;
import de.caritas.cob.userservice.api.service.RocketChatService;
import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientHelper;
import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientService;
import java.util.Set;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,7 +30,7 @@
@RequiredArgsConstructor
public class ConsultantCreatorService {

private final @NonNull KeycloakAdminClientHelper keycloakAdminClientHelper;
private final @NonNull KeycloakAdminClientService keycloakAdminClientService;
private final @NonNull RocketChatService rocketChatService;
private final @NonNull ConsultantService consultantService;
private final @NonNull UserHelper userHelper;
Expand All @@ -42,6 +44,8 @@ public class ConsultantCreatorService {
* @return the generated {@link Consultant}
*/
public Consultant createNewConsultant(CreateConsultantDTO createConsultantDTO) {
this.consultantInputValidator.validateAbsence(
new CreateConsultantDTOAbsenceInputAdapter(createConsultantDTO));
ConsultantCreationInput consultantCreationInput =
new CreateConsultantDTOCreationInputAdapter(createConsultantDTO, this.userHelper);
return createNewConsultant(consultantCreationInput, asSet(CONSULTANT.getValue()));
Expand All @@ -66,8 +70,8 @@ private Consultant createNewConsultant(ConsultantCreationInput consultantCreatio
String keycloakUserId = createKeycloakUser(consultantCreationInput);

String password = userHelper.getRandomPassword();
keycloakAdminClientHelper.updatePassword(keycloakUserId, password);
roles.forEach(roleName -> this.keycloakAdminClientHelper.updateRole(keycloakUserId, roleName));
keycloakAdminClientService.updatePassword(keycloakUserId, password);
roles.forEach(roleName -> this.keycloakAdminClientService.updateRole(keycloakUserId, roleName));

String rocketChatUserId =
createRocketChatUser(consultantCreationInput, keycloakUserId, password);
Expand All @@ -83,7 +87,7 @@ private String createKeycloakUser(ConsultantCreationInput consultantCreationInpu
this.consultantInputValidator.validateUserDTO(userDto);

KeycloakCreateUserResponseDTO response =
this.keycloakAdminClientHelper
this.keycloakAdminClientService
.createKeycloakUser(userDto, consultantCreationInput.getFirstName(),
consultantCreationInput.getLastName());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.caritas.cob.userservice.api.admin.service.consultant.update;

import de.caritas.cob.userservice.api.admin.service.consultant.validation.ConsultantInputValidator;
import de.caritas.cob.userservice.api.admin.service.consultant.validation.UpdateConsultantDTOAbsenceInputAdapter;
import de.caritas.cob.userservice.api.exception.httpresponses.BadRequestException;
import de.caritas.cob.userservice.api.model.UpdateConsultantDTO;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.service.ConsultantService;
import de.caritas.cob.userservice.api.service.RocketChatService;
import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientService;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
* Service class to provide update functionality for consultants.
*/
@Service
@RequiredArgsConstructor
public class ConsultantUpdateService {

private final @NonNull KeycloakAdminClientService keycloakAdminClientService;
private final @NonNull ConsultantService consultantService;
private final @NonNull ConsultantInputValidator consultantInputValidator;
private final @NonNull RocketChatService rocketChatService;

/**
* Updates the basic data of consultant with given id.
*
* @param consultantId the id of the consultant to update
* @param updateConsultantDTO the update input data
* @return the updated persisted {@link Consultant}
*/
public Consultant updateConsultant(String consultantId, UpdateConsultantDTO updateConsultantDTO) {
this.consultantInputValidator
.validateAbsence(new UpdateConsultantDTOAbsenceInputAdapter(updateConsultantDTO));

Consultant consultant =
this.consultantService.getConsultant(consultantId)
.orElseThrow(() -> new BadRequestException(
String.format("Consultant with id %s does not exist", consultantId)));

UserDTO userDTO = buildValidatedUserDTO(updateConsultantDTO, consultant);
this.keycloakAdminClientService.updateUserData(consultant.getId(), userDTO,
updateConsultantDTO.getFirstname(), updateConsultantDTO.getLastname());

this.rocketChatService.updateUser(consultant.getRocketChatId(), updateConsultantDTO);

return updateDatabaseConsultant(updateConsultantDTO, consultant);
}

private UserDTO buildValidatedUserDTO(UpdateConsultantDTO updateConsultantDTO,
Consultant consultant) {
UserDTO userDTO = new UserDTO();
userDTO.setEmail(updateConsultantDTO.getEmail());
userDTO.setUsername(consultant.getUsername());

this.consultantInputValidator.validateUserDTO(userDTO);
return userDTO;
}

private Consultant updateDatabaseConsultant(UpdateConsultantDTO updateConsultantDTO,
Consultant consultant) {
consultant.setFirstName(updateConsultantDTO.getFirstname());
consultant.setLastName(updateConsultantDTO.getLastname());
consultant.setEmail(updateConsultantDTO.getEmail());
consultant.setLanguageFormal(updateConsultantDTO.getFormalLanguage());
consultant.setAbsent(updateConsultantDTO.getAbsent());
consultant.setAbsenceMessage(updateConsultantDTO.getAbsenceMessage());
consultant.setUpdateDate(LocalDateTime.now(ZoneOffset.UTC));

return this.consultantService.saveConsultant(consultant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.caritas.cob.userservice.api.admin.service.consultant.validation;

public interface AbsenceInputValidation {

boolean isAbsent();

String absenceMessage();

}
Loading

0 comments on commit af773b6

Please sign in to comment.