Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email transcript use case #33

Merged
merged 10 commits into from
Nov 30, 2023
32 changes: 22 additions & 10 deletions src/main/java/callhub/connect/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Document("message")
public class Message {

@Id
private String id;
private String content;
private LocalDate timeStamp;
private LocalDateTime timeStamp;
private String userId;
private String sessionId;
private Sender sender;

public Message(){}

public Message(String content, LocalDate timeStamp, String sessionId, Sender sender) {
public Message(String content, LocalDateTime timeStamp, String sessionId, Sender sender) {
this.content = content;
this.timeStamp = timeStamp;
this.sessionId = sessionId;
Expand All @@ -29,7 +27,7 @@ public Message(String content, LocalDate timeStamp, String sessionId, Sender sen

public Message(String content, String sessionId, Sender sender) {
this.content = content;
this.timeStamp = LocalDate.now();
this.timeStamp = LocalDateTime.now();
this.sessionId = sessionId;
this.sender = sender;
}
Expand All @@ -42,12 +40,26 @@ public void setContent(String content) {
this.content = content;
}

/**
* Returns the date the message was sent as a formatted string.
*
* @return A formatted string representing the date of the message in the format "Month dd, yyyy".
*/
public String getDateString() {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("LLLL dd, yyyy");
return this.timeStamp.format(dateFormatter);
}

private String getTimeStampString() {
DateFormat df = new SimpleDateFormat("HH:mm:ss a");
return df.format(this.timeStamp);
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
return this.timeStamp.format(timeFormatter);
}

/**
* Returns a string that formats the message with the time it was sent, its sender and its content.
* @return a formatted message.
*/
public String formattedMessage() {
return String.format("%s %s", this.getTimeStampString(), this.content);
return String.format("%s %s: %s", this.getTimeStampString(), this.sender.name(), this.content);
}
}
81 changes: 81 additions & 0 deletions src/main/java/callhub/connect/use_case/EmailController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package callhub.connect.use_case;

import callhub.connect.data_access.SessionRepository;
import callhub.connect.entities.Message;
import callhub.connect.entities.Session;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/email")
public class EmailController {

public SessionRepository sessionRepository;

public EmailController(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}

/**
* Retrieves the transcript of messages for a given session code.
*
* @param code The session code to identify the session.
* @return A ResponseEntity containing the transcript of messages as a String.
* If the session is not found, an error occurs.
*/
@GetMapping("/transcript/{code}")
public ResponseEntity<String> getTranscript(@PathVariable String code) {
HttpHeaders headers = new HttpHeaders();
boolean sessionExists = sessionRepository.existsById(code);
if (!sessionExists) {
new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND);
}

try {
Session session = sessionRepository.getSessionsByActiveAndCode(true, code);
ArrayList<Message> messagesList = session.getMessages();

StringBuilder transcript = new StringBuilder();
for (Message message : messagesList) {
transcript.append(message.formattedMessage()).append("\n");
}
return new ResponseEntity<>(transcript.toString(), headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST);
}
}

/**
* Retrieves the date messages were sent for a given session code, assuming all messages
* were sent on the same day as the first message.
*
* @param code The session code to identify the session.
* @return A ResponseEntity containing the date of messages as a String.
* If the session is not found, an error occurs.
*/
@GetMapping("/date/{code}")
public ResponseEntity<String> getDate(@PathVariable String code) {
HttpHeaders headers = new HttpHeaders();
boolean sessionExists = sessionRepository.existsById(code);
if (!sessionExists) {
new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND);
}

try {
Session session = sessionRepository.getSessionsByActiveAndCode(true, code);
ArrayList<Message> messagesList = session.getMessages();

Message firstMessage = messagesList.get(0);
return new ResponseEntity<>(firstMessage.getDateString(), headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST);
}
}
}
Loading