Skip to content

Commit

Permalink
Merge pull request #100 from CaritasDeutschland/feture-delete-consult…
Browse files Browse the repository at this point in the history
…ant-agency-relation

Feture delete consultant agency relation
  • Loading branch information
mobo4b authored Feb 12, 2021
2 parents c62e4a4 + f81f9ba commit 2f2bc8f
Show file tree
Hide file tree
Showing 153 changed files with 1,888 additions and 1,147 deletions.
17 changes: 16 additions & 1 deletion api/useradminservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ paths:
summary: 'Change an agency from team-agency to default and vice-versa and switch the
associated consultant accounts to team consultant or default consultant. Ongoing team
counselings change to 1:1 when converting agency to default status. 1:1 counselings remain
single counselings when swapping the agency to team-agency. [Authorization: Role: user-admin]'
single counselings when swapping the agency to team-agency. This endpoint must be used only
internally from AgencyService to ensure the regarding agency has been flagged correctly too.
[Authorization: Role: user-admin]'
operationId: changeAgencyType
parameters:
- name: agencyId
Expand Down Expand Up @@ -779,6 +781,19 @@ components:
updateDate:
type: string
example: "2019-08-23T08:52:05"
deleteDate:
type: string
example: "2019-08-23T08:52:05"
_links:
$ref: '#/components/schemas/ConsultantAgencyLinks'

ConsultantAgencyLinks:
type: object
required:
- delete
properties:
delete:
$ref: '#/components/schemas/HalLink'

CreateConsultantAgencyDTO:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public ResponseEntity<Void> createConsultantAgency(@PathVariable String consulta
return new ResponseEntity<>(HttpStatus.CREATED);
}

/**
* Entry point to delete a consultant agency relation.
*
* @param consultantId Consultant Id (required)
* @param agencyId Agency Id (required)
*/
@Override
public ResponseEntity<Void> deleteConsultantAgency(String consultantId, Long agencyId) {
this.consultantAdminFacade.markConsultantAgencyForDeletion(consultantId, agencyId);
return new ResponseEntity<>(HttpStatus.OK);
}

/**
* Entry point to mark a consultant for deletion.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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.consultantAgency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgency;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -121,4 +121,13 @@ public void changeAgencyType(Long agencyId, AgencyTypeDTO agencyTypeDTO) {
}
}

/**
* Marks the {@link ConsultantAgency} as deleted.
*
* @param consultantId the consultant id
* @param agencyId the agency id
*/
public void markConsultantAgencyForDeletion(String consultantId, Long agencyId) {
this.consultantAgencyAdminService.markConsultantAgencyForDeletion(consultantId, agencyId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import de.caritas.cob.userservice.api.model.ViolationDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.consultant.ConsultantRepository;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgencyRepository;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgencyRepository;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import de.caritas.cob.userservice.api.admin.report.service.AgencyAdminService;
import de.caritas.cob.userservice.api.model.ViolationDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgencyRepository;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgencyRepository;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import de.caritas.cob.userservice.api.repository.session.Session;
import de.caritas.cob.userservice.api.repository.session.SessionRepository;
import de.caritas.cob.userservice.api.repository.session.SessionStatus;
import de.caritas.cob.userservice.api.service.RocketChatService;
import de.caritas.cob.userservice.api.service.rocketchat.RocketChatService;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import de.caritas.cob.userservice.api.model.ViolationDTO;
import de.caritas.cob.userservice.api.repository.user.User;
import de.caritas.cob.userservice.api.repository.user.UserRepository;
import de.caritas.cob.userservice.api.repository.userAgency.UserAgencyRepository;
import de.caritas.cob.userservice.api.repository.useragency.UserAgencyRepository;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import de.caritas.cob.userservice.api.admin.report.service.AgencyAdminService;
import de.caritas.cob.userservice.api.model.ViolationDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgencyRepository;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgencyRepository;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import de.caritas.cob.userservice.agencyadminserivce.generated.web.model.AgencyAdminResponseDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgency;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import static java.util.Objects.isNull;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

import de.caritas.cob.userservice.api.admin.controller.UserAdminController;
import de.caritas.cob.userservice.api.admin.hallink.HalLinkBuilder;
import de.caritas.cob.userservice.api.model.ConsultantAgencyAdminDTO;
import de.caritas.cob.userservice.api.model.ConsultantAgencyAdminResultDTO;
import de.caritas.cob.userservice.api.model.ConsultantAgencyAdminResultLinks;
import de.caritas.cob.userservice.api.model.ConsultantAgencyLinks;
import de.caritas.cob.userservice.api.model.HalLink;
import de.caritas.cob.userservice.api.model.HalLink.MethodEnum;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgency;
import de.caritas.cob.userservice.generated.api.admin.controller.UseradminApi;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -78,12 +79,21 @@ private List<ConsultantAgencyAdminDTO> buildSessionAdminResult() {
}

private ConsultantAgencyAdminDTO fromConsultantAgency(ConsultantAgency consultantAgency) {

ConsultantAgencyLinks consultantAgencyLinks = new ConsultantAgencyLinks()
.delete(buildDeleteConsultantAgencyLink(consultantAgency));
return new ConsultantAgencyAdminDTO()
.agencyId(consultantAgency.getAgencyId())
.consultantId(consultantAgency.getConsultant().getId())
.createDate(String.valueOf(consultantAgency.getCreateDate()))
.updateDate((String.valueOf(consultantAgency.getUpdateDate())));
.updateDate((String.valueOf(consultantAgency.getUpdateDate())))
.deleteDate(String.valueOf(consultantAgency.getDeleteDate()))
.links(consultantAgencyLinks);
}

private HalLink buildDeleteConsultantAgencyLink(ConsultantAgency consultantAgency) {
return buildHalLink(methodOn(UseradminApi.class)
.deleteConsultantAgency(consultantAgency.getConsultant().getId(),
consultantAgency.getAgencyId()), MethodEnum.DELETE);
}

private ConsultantAgencyAdminResultLinks buildResultLinks() {
Expand All @@ -92,7 +102,7 @@ private ConsultantAgencyAdminResultLinks buildResultLinks() {
}

private HalLink buildSelfLink() {
return buildHalLink(methodOn(UserAdminController.class).getConsultantAgency(consultantId),
return buildHalLink(methodOn(UseradminApi.class).getConsultantAgency(consultantId),
MethodEnum.GET);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package de.caritas.cob.userservice.api.admin.service.agency;

import static de.caritas.cob.userservice.api.exception.httpresponses.customheader.HttpStatusExceptionReason.CONSULTANT_AGENCY_RELATION_DOES_NOT_EXIST;
import static de.caritas.cob.userservice.api.exception.httpresponses.customheader.HttpStatusExceptionReason.CONSULTANT_IS_THE_LAST_OF_AGENCY_AND_AGENCY_HAS_OPEN_ENQUIRIES;
import static de.caritas.cob.userservice.api.exception.httpresponses.customheader.HttpStatusExceptionReason.CONSULTANT_IS_THE_LAST_OF_AGENCY_AND_AGENCY_IS_STILL_ACTIVE;
import static de.caritas.cob.userservice.api.repository.session.SessionStatus.INITIAL;
import static de.caritas.cob.userservice.api.repository.session.SessionStatus.NEW;
import static de.caritas.cob.userservice.localdatetime.CustomLocalDateTime.nowInUtc;
import static java.util.Collections.singletonList;
import static java.util.Objects.isNull;
import static org.apache.commons.collections.CollectionUtils.isEmpty;
import static org.apache.commons.lang3.BooleanUtils.isFalse;

import de.caritas.cob.userservice.api.exception.AgencyServiceHelperException;
import de.caritas.cob.userservice.api.exception.httpresponses.BadRequestException;
import de.caritas.cob.userservice.api.exception.httpresponses.CustomValidationHttpStatusException;
import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.exception.httpresponses.NotFoundException;
import de.caritas.cob.userservice.api.model.AgencyDTO;
import de.caritas.cob.userservice.api.model.ConsultantAgencyAdminResultDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.consultant.ConsultantRepository;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgencyRepository;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgency;
import de.caritas.cob.userservice.api.repository.consultantagency.ConsultantAgencyRepository;
import de.caritas.cob.userservice.api.repository.session.Session;
import de.caritas.cob.userservice.api.repository.session.SessionRepository;
import de.caritas.cob.userservice.api.repository.session.SessionStatus;
import de.caritas.cob.userservice.api.service.LogService;
import de.caritas.cob.userservice.api.service.helper.AgencyServiceHelper;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -130,4 +140,72 @@ private void removeTeamConsultantFlag(Consultant consultant) {
this.consultantRepository.save(consultant);
}

/**
* Marks an {@link ConsultantAgency} as deleted if consultant is not the last in agency or agency
* is offline and no open enquiries are assigned.
*
* @param consultantId the consultant id
* @param agencyId the agency id
*/
public void markConsultantAgencyForDeletion(String consultantId, Long agencyId) {
List<ConsultantAgency> consultantAgencies =
this.consultantAgencyRepository.findByConsultantIdAndAgencyId(consultantId, agencyId);
if (isEmpty(consultantAgencies)) {
throw new CustomValidationHttpStatusException(CONSULTANT_AGENCY_RELATION_DOES_NOT_EXIST);
}
consultantAgencies.stream()
.filter(consultantAgency -> isNull(consultantAgency.getDeleteDate()))
.forEach(this::markAsDeleted);
}

private void markAsDeleted(ConsultantAgency consultantAgency) {
validateForDeletion(consultantAgency);
consultantAgency.setDeleteDate(nowInUtc());
this.consultantAgencyRepository.save(consultantAgency);
}

private void validateForDeletion(ConsultantAgency consultantAgency) {
if (isTheLastConsultantInAgency(consultantAgency)) {
if (isAgencyStillActive(consultantAgency)) {
throw new CustomValidationHttpStatusException(
CONSULTANT_IS_THE_LAST_OF_AGENCY_AND_AGENCY_IS_STILL_ACTIVE);
}
if (hasOpenEnquiries(consultantAgency)) {
throw new CustomValidationHttpStatusException(
CONSULTANT_IS_THE_LAST_OF_AGENCY_AND_AGENCY_HAS_OPEN_ENQUIRIES);
}
}
}

private boolean isTheLastConsultantInAgency(ConsultantAgency consultantAgency) {
return this.consultantAgencyRepository.findByAgencyId(consultantAgency.getAgencyId())
.stream()
.filter(relation -> isNull(relation.getDeleteDate()))
.allMatch(sameConsultantAgencyRelation(consultantAgency));
}

private Predicate<ConsultantAgency> sameConsultantAgencyRelation(
ConsultantAgency consultantAgency) {
return relation -> relation.equals(consultantAgency);
}

private boolean isAgencyStillActive(ConsultantAgency consultantAgency) {
try {
AgencyDTO agency = this.agencyServiceHelper.getAgency(consultantAgency.getAgencyId());
return isFalse(agency.getOffline());
} catch (AgencyServiceHelperException e) {
throw new InternalServerErrorException(e.getMessage());
}
}

private boolean hasOpenEnquiries(ConsultantAgency consultantAgency) {
Long agencyId = consultantAgency.getAgencyId();
return hasSessionWithStatus(agencyId, NEW) || hasSessionWithStatus(agencyId, INITIAL);
}

private boolean hasSessionWithStatus(Long agencyId, SessionStatus status) {
return !this.sessionRepository.findByAgencyIdAndStatusAndConsultantIsNull(agencyId, status)
.isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import de.caritas.cob.userservice.api.repository.consultant.ConsultantRepository;
import de.caritas.cob.userservice.api.repository.session.Session;
import de.caritas.cob.userservice.api.service.LogService;
import de.caritas.cob.userservice.api.service.RocketChatService;
import de.caritas.cob.userservice.api.service.rocketchat.RocketChatService;
import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientService;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.caritas.cob.userservice.api.admin.service.consultant.create;

import static de.caritas.cob.userservice.localdatetime.CustomLocalDateTime.nowInUtc;

import java.time.LocalDateTime;

/**
Expand Down Expand Up @@ -27,8 +29,12 @@ interface ConsultantCreationInput {

boolean isLanguageFormal();

LocalDateTime getCreateDate();
default LocalDateTime getCreateDate() {
return nowInUtc();
}

LocalDateTime getUpdateDate();
default LocalDateTime getUpdateDate() {
return nowInUtc();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
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.rocketchat.RocketChatService;
import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientService;
import java.util.Set;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import de.caritas.cob.userservice.api.helper.UserHelper;
import de.caritas.cob.userservice.api.model.CreateConsultantDTO;
import java.time.LocalDateTime;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

/**
* Adapter class to provide a {@link ConsultantCreationInput} based on a
* {@link CreateConsultantDTO}.
* Adapter class to provide a {@link ConsultantCreationInput} based on a {@link
* CreateConsultantDTO}.
*/
@RequiredArgsConstructor
public class CreateConsultantDTOCreationInputAdapter implements ConsultantCreationInput {
Expand Down Expand Up @@ -118,23 +117,4 @@ public boolean isLanguageFormal() {
return isTrue(this.createConsultantDTO.getFormalLanguage());
}

/**
* Provides the created date.
*
* @return the created date
*/
@Override
public LocalDateTime getCreateDate() {
return LocalDateTime.now();
}

/**
* Provides the updated date.
*
* @return the updated date
*/
@Override
public LocalDateTime getUpdateDate() {
return LocalDateTime.now();
}
}
Loading

0 comments on commit 2f2bc8f

Please sign in to comment.