Skip to content

Commit

Permalink
Merge pull request #678 from Onlineberatung/DELPHI-46
Browse files Browse the repository at this point in the history
Delphi 46
  • Loading branch information
tkuzynow authored Jan 3, 2024
2 parents 274d04d + ded60bf commit d8e0794
Show file tree
Hide file tree
Showing 33 changed files with 405 additions and 70 deletions.
33 changes: 33 additions & 0 deletions api/userservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,18 @@ components:
type: string
format: date-time
writeOnly: true
createdAt:
type: string
format: time
example: 2023-10-23T12:05:00.000Z
chatAgencies:
type: array
items:
$ref: '#/components/schemas/AgencyDTO'
hintMessage:
type: string
example: "Hint"
maxLength: 300

SessionUserDTO:
type: object
Expand Down Expand Up @@ -2394,6 +2406,12 @@ components:
lastName:
type: string
example: Mustermann
username:
type: string
example: beraterXYZ
displayName:
type: string
example: Berater XYZ

UserSessionListResponseDTO:
type: object
Expand Down Expand Up @@ -2768,6 +2786,10 @@ components:
topic:
type: string
example: Wöchentliche Drogenberatung
agencyId:
type: integer
format: int64
example: 7
startDate:
type: string
format: date
Expand All @@ -2782,12 +2804,23 @@ components:
repetitive:
type: boolean
example: false
hintMessage:
type: string
example: "Hint"
maxLength: 300

CreateChatResponseDTO:
type: object
required:
- groupId
properties:
createdAt:
type: string
format: time
example: 2019-10-23T12:05:00.000Z
creatorDisplayName:
type: string
example: Max M.
groupId:
type: string
example: WCET6GWir78pNMyyD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ private HttpHeaders httpHeaders() {
var systemUser = rcCredentialHelper.getSystemUserSneaky();

var httpHeaders = new HttpHeaders();
httpHeaders.add(HEADER_AUTH_TOKEN, systemUser.getRocketChatToken());
httpHeaders.add(HEADER_USER_ID, systemUser.getRocketChatUserId());
if (systemUser != null) {
httpHeaders.add(HEADER_AUTH_TOKEN, systemUser.getRocketChatToken());
httpHeaders.add(HEADER_USER_ID, systemUser.getRocketChatUserId());
}

return httpHeaders;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,22 @@ public UpdateUser updateUserOf(String chatUserId, String displayName) {

public Optional<Map<String, Object>> mapOfUserResponse(
ResponseEntity<UserInfoResponseDTO> userResponse) {
var body = userResponse.getBody();
if (nonNull(body)) {
var user = body.getUser();
var map = new HashMap<String, Object>();
map.put("id", user.getId());
if (nonNull(user.getUsername())) {
map.put("username", user.getUsername());
}
if (nonNull(user.getName())) {
map.put("displayName", user.getName());
}
if (nonNull(userResponse)) {
var body = userResponse.getBody();
if (nonNull(body)) {
var user = body.getUser();
var map = new HashMap<String, Object>();
map.put("id", user.getId());
if (nonNull(user.getUsername())) {
map.put("username", user.getUsername());
}
if (nonNull(user.getName())) {
map.put("displayName", user.getName());
}

return Optional.of(map);
return Optional.of(map);
}
}

return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,6 @@ public ResponseEntity<CreateChatResponseDTO> createChatV2(@RequestBody ChatDTO c

var callingConsultant = this.userAccountProvider.retrieveValidatedConsultant();
var response = createChatFacade.createChatV2(chatDTO, callingConsultant);

return new ResponseEntity<>(response, HttpStatus.CREATED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static de.caritas.cob.userservice.api.helper.UserHelper.CHAT_TOPIC_MAX_LENGTH;
import static de.caritas.cob.userservice.api.helper.UserHelper.CHAT_TOPIC_MIN_LENGTH;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
Expand All @@ -20,6 +21,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

Expand All @@ -42,6 +44,7 @@ public class ChatDTO {
@NotNull(message = "{chat.startDate.invalid}")
@ApiModelProperty(required = true, example = "2019-10-23", position = 1)
@JsonProperty("startDate")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate startDate;

@DateTimeFormat(pattern = "HH:mm")
Expand All @@ -62,10 +65,22 @@ public class ChatDTO {
@JsonProperty("repetitive")
private boolean repetitive;

@ApiModelProperty(required = true, example = "5", position = 5)
@Min(value = 0, message = "{chat.agencyId.invalid}")
@JsonProperty("agencyId")
private Long agencyId;

@ApiModelProperty(required = true, example = "5", position = 6)
@Length(max = 300, message = "{chat.hintMessage.invalid}")
@JsonProperty("hintMessage")
private String hintMessage;

@Override
public String toString() {
return "ChatDTO [topic="
+ topic
+ ", agencyId="
+ agencyId
+ ", startDate="
+ startDate
+ ", startTime="
Expand All @@ -74,6 +89,10 @@ public String toString() {
+ duration
+ ", repetitive="
+ repetitive
+ ", agencyId="
+ agencyId
+ ", hintMessage="
+ hintMessage
+ "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -69,4 +70,10 @@ public class UserChatDTO {
@JsonIgnore private LocalDateTime startDateWithTime;

@ApiModelProperty private LastMessageDTO e2eLastMessage;

@ApiModelProperty private String createdAt;

@ApiModelProperty private List<AgencyDTO> assignedAgencies;

@ApiModelProperty private String hintMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public Chat convertToEntity(ChatDTO chatDTO, Consultant consultant, AgencyDTO ag
.repetitive(isTrue(chatDTO.isRepetitive()))
// Note that the repetition interval can only be weekly atm.
.chatInterval(isTrue(chatDTO.isRepetitive()) ? ChatInterval.WEEKLY : null)
.updateDate(nowInUtc());
.updateDate(nowInUtc())
.createDate(nowInUtc())
.hintMessage(chatDTO.getHintMessage());

if (nonNull(agencyDTO)) {
builder.consultingTypeId(agencyDTO.getConsultingType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.caritas.cob.userservice.api.adapters.web.dto.AgencyDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ChatDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.CreateChatResponseDTO;
import de.caritas.cob.userservice.api.exception.httpresponses.BadRequestException;
import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatAddUserToGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatCreateGroupException;
Expand All @@ -16,6 +17,7 @@
import de.caritas.cob.userservice.api.model.Chat;
import de.caritas.cob.userservice.api.model.ChatAgency;
import de.caritas.cob.userservice.api.model.Consultant;
import de.caritas.cob.userservice.api.model.ConsultantAgency;
import de.caritas.cob.userservice.api.service.ChatService;
import de.caritas.cob.userservice.api.service.agency.AgencyService;
import java.util.function.BiFunction;
Expand All @@ -32,7 +34,6 @@ public class CreateChatFacade {
private final @NonNull RocketChatService rocketChatService;
private final @NonNull AgencyService agencyService;
private final @NonNull ChatConverter chatConverter;

/**
* Creates a chat in MariaDB, it's relation to the agency and Rocket.Chat-room.
*
Expand Down Expand Up @@ -65,7 +66,10 @@ private CreateChatResponseDTO createChat(
rcGroupId = createRocketChatGroupWithTechnicalUser(chatDTO, chat);
chat.setGroupId(rcGroupId);
chatService.saveChat(chat);
return new CreateChatResponseDTO().groupId(rcGroupId);

return new CreateChatResponseDTO()
.groupId(rcGroupId)
.createdAt(chat.getCreateDate().toString());
} catch (InternalServerErrorException e) {
doRollback(chat, rcGroupId);
throw e;
Expand All @@ -86,14 +90,33 @@ private Chat saveChatV1(Consultant consultant, ChatDTO chatDTO) {
}

private Chat saveChatV2(Consultant consultant, ChatDTO chatDTO) {
assertAgencyIdIsNotNull(chatDTO);
Chat chat = chatService.saveChat(chatConverter.convertToEntity(chatDTO, consultant));
consultant
.getConsultantAgencies()
.forEach(
consultantAgency -> createChatAgencyRelation(chat, consultantAgency.getAgencyId()));
ConsultantAgency foundConsultantAgency =
findConsultantAgencyForGivenChatAgency(consultant, chatDTO);
createChatAgencyRelation(chat, foundConsultantAgency.getAgencyId());
return chat;
}

private void assertAgencyIdIsNotNull(ChatDTO chatDTO) {
if (chatDTO.getAgencyId() == null) {
throw new BadRequestException("Agency id must not be null");
}
}

private ConsultantAgency findConsultantAgencyForGivenChatAgency(
Consultant consultant, ChatDTO chatDTO) {
return consultant.getConsultantAgencies().stream()
.filter(agency -> agency.getAgencyId().equals(chatDTO.getAgencyId()))
.findFirst()
.orElseThrow(
() ->
new BadRequestException(
String.format(
"Consultant with id %s is not assigned to agency with id %s",
consultant.getId(), chatDTO.getAgencyId())));
}

private void createChatAgencyRelation(Chat chat, Long agencyId) {
chatService.saveChatAgencyRelation(new ChatAgency(chat, agencyId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.userservice.api.adapters.web.dto.AbsenceDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantSessionResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.GroupSessionListResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.UserSessionListResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.mapping.UserDtoMapper;
import de.caritas.cob.userservice.api.helper.Helper;
import de.caritas.cob.userservice.api.model.Consultant;
import de.caritas.cob.userservice.api.port.in.AccountManaging;
import de.caritas.cob.userservice.api.service.ConsultantService;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class ConsultantDataFacade {

private final @NonNull ConsultantService consultantService;
Expand Down Expand Up @@ -71,4 +77,25 @@ public void addConsultantDisplayNameToSessionList(UserSessionListResponseDTO use
}
});
}

public void addConsultantDisplayNameToSessionList(
List<ConsultantSessionResponseDTO> consultantSessionResponseDTOs) {
consultantSessionResponseDTOs.stream()
.forEach(
session -> {
var chatOwner = session.getConsultant();
if (nonNull(chatOwner) && nonNull(chatOwner.getUsername())) {
try {
Optional<Map<String, Object>> consultantByUsername =
accountManager.findConsultantByUsername(chatOwner.getUsername());
if (consultantByUsername.isPresent()) {
chatOwner.setDisplayName(
userDtoMapper.displayNameOf(consultantByUsername.get()));
}
} catch (Exception e) {
log.error("Error while fetching consultant by username: {}", e.getMessage());
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.text.SimpleDateFormat;

public class JsonSerializationUtils {

Expand All @@ -18,6 +20,10 @@ public static <T> T deserializeFromJsonString(String jsonString, Class<T> clazz)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
objectMapper.setDateFormat(dateFormat);
objectMapper.registerModule(new JavaTimeModule());

return objectMapper.readValue(jsonString, clazz);
} catch (JsonProcessingException e) {
throw new RuntimeJsonMappingException(e.getMessage());
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/caritas/cob/userservice/api/model/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public enum ChatInterval {
@Column(name = "update_date")
private LocalDateTime updateDate;

@Column(name = "create_date")
private LocalDateTime createDate;

@Column(name = "hint_message")
private String hintMessage;

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -22,6 +23,7 @@
@NoArgsConstructor
@Getter
@Setter
@Builder
public class ChatAgency {

@Id
Expand Down
Loading

0 comments on commit d8e0794

Please sign in to comment.