Skip to content

Commit 6c11075

Browse files
committed
Email service and systems are updated for visitor report.
1 parent d3b7cc5 commit 6c11075

File tree

9 files changed

+136
-25
lines changed

9 files changed

+136
-25
lines changed

app/src/components/Chat.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
22
import axios from 'axios';
33

44
export default function Chat({ botId }) {
5+
const [chatId, setChatId] = useState(-1);
56
const [botName, setBotName] = useState('');
67
const [messages, setMessages] = useState([]);
78
const [inputText, setInputText] = useState('');
@@ -20,7 +21,7 @@ export default function Chat({ botId }) {
2021
const generateCode = () => {
2122
const generatedCode = `
2223
<div id="semantiq-chatbot"></div>
23-
<script data-bot-id="${botId}" defer="defer" src="http://localhost:8080/static/main.ea9881ca.js"></script>
24+
<script data-bot-id="${botId}" defer="defer" src="http://localhost:8080/static/main.57a7bb31.js"></script>
2425
<script>
2526
var link = document.createElement("link");
2627
link.href = "http://localhost:8080/static/main.ba187d75.css";
@@ -51,9 +52,33 @@ export default function Chat({ botId }) {
5152
sendVote(1);
5253
};
5354

54-
const voteNegative = () => {
55-
sendVote(-1);
56-
};
55+
const voteNegative = async () => {
56+
sendVote(-1);
57+
const userEmail = prompt('If you want to contact a real person, leave your email address, otherwise leave it blank.');
58+
59+
if (userEmail) {
60+
const data = {
61+
chatId: chatId,
62+
userEmail: userEmail,
63+
chatBotId: botId
64+
};
65+
66+
try {
67+
const response = await axios.post('http://localhost:8080/api/email/report', data);
68+
69+
if (response.status === 200) {
70+
// Request successful, do something
71+
console.log('Email reported successfully');
72+
} else {
73+
console.error('Failed to report email');
74+
}
75+
} catch (error) {
76+
// Handle errors
77+
console.error('Error:', error);
78+
}
79+
}
80+
};
81+
5782

5883
const sendVote = (vote) => {
5984
// Determine the chatId for the vote
@@ -62,7 +87,6 @@ export default function Chat({ botId }) {
6287
// Sending a POST request to vote
6388
axios.post(`http://localhost:8080/api/botdata/${botId}/chats/${chatId}/${vote}`)
6489
.then(response => {
65-
// Handle successful vote response if needed
6690
console.log(`Voted ${vote === 1 ? 'positive' : 'negative'}`);
6791
})
6892
.catch(error => {
@@ -72,35 +96,39 @@ export default function Chat({ botId }) {
7296

7397
const sendMessage = (e) => {
7498
e.preventDefault();
75-
99+
76100
if (inputText.trim() === '') return;
77-
101+
78102
// Update messages state with the user message immediately
79103
const newUserMessage = { text: inputText, sender: 'user' };
80104
setMessages([...messages, newUserMessage]);
81105
setInputText('');
82-
106+
83107
// Send user message to the server via Axios POST request
84-
const chatId = messages.length === 0 ? -1 : messages[messages.length - 1].chatId;
85-
axios.post(`http://localhost:8080/api/bots/${botId}/chat/${chatId}`, {
108+
const currentChatId = messages.length === 0 ? -1 : messages[messages.length - 1].chatId;
109+
axios
110+
.post(`http://localhost:8080/api/bots/${botId}/chat/${currentChatId}`, {
86111
question: inputText,
87112
})
88-
.then(response => {
113+
.then((response) => {
89114
let newAssistantMessage = response.data.answer.content;
90115
const newChatId = response.data.chatId;
91-
116+
92117
// Remove unwanted content if it exists in the assistant's message
93118
if (newAssistantMessage.startsWith('{"content":"') && newAssistantMessage.endsWith('"}')) {
94119
newAssistantMessage = newAssistantMessage.substring(12, newAssistantMessage.length - 2);
95120
}
96-
121+
122+
// Update chatId state with the new chatId
123+
setChatId(newChatId);
124+
97125
// Update messages state with the new assistant message, preserving previous messages
98-
setMessages(prevMessages => [
126+
setMessages((prevMessages) => [
99127
...prevMessages,
100128
{ text: newAssistantMessage, sender: 'assistant', chatId: newChatId },
101129
]);
102130
})
103-
.catch(error => {
131+
.catch((error) => {
104132
console.error('Error sending message:', error);
105133
});
106134
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.semantiq.server.DTO;
2+
3+
public class ReportEmailDTO {
4+
private int chatId;
5+
private String userEmail;
6+
private int chatBotId;
7+
8+
// Constructors
9+
public ReportEmailDTO() {
10+
}
11+
12+
public ReportEmailDTO(int chatId, String userEmail, int chatBotId) {
13+
this.chatId = chatId;
14+
this.userEmail = userEmail;
15+
this.chatBotId = chatBotId;
16+
}
17+
18+
// Getters and setters
19+
public int getChatId() {
20+
return chatId;
21+
}
22+
23+
public void setChatId(int chatId) {
24+
this.chatId = chatId;
25+
}
26+
27+
public String getUserEmail() {
28+
return userEmail;
29+
}
30+
31+
public void setUserEmail(String userEmail) {
32+
this.userEmail = userEmail;
33+
}
34+
35+
public int getChatBotId() {
36+
return chatBotId;
37+
}
38+
39+
public void setChatBotId(int chatBotId) {
40+
this.chatBotId = chatBotId;
41+
}
42+
}
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
package com.semantiq.server.controller;
22

3+
import com.semantiq.server.DTO.ReportEmailDTO;
4+
import com.semantiq.server.service.ChatBotService.ChatBotService;
35
import com.semantiq.server.service.EmailService;
46
import com.semantiq.server.service.UserService;
7+
import jakarta.mail.MessagingException;
8+
import org.springframework.http.HttpStatus;
59
import org.springframework.http.ResponseEntity;
610
import org.springframework.stereotype.Controller;
711
import org.springframework.beans.factory.annotation.Autowired;
812
import org.springframework.web.bind.annotation.*;
913

1014
@Controller
1115
@RequestMapping("/api/email")
16+
@CrossOrigin(origins = "*")
1217
public class EmailController {
1318
private final EmailService emailService;
1419
private final UserService userService;
20+
private final ChatBotService chatBotService;
1521

1622
@Autowired
17-
public EmailController(EmailService emailService, UserService userService) {
23+
public EmailController(EmailService emailService, UserService userService, ChatBotService chatBotService) {
1824
this.emailService = emailService;
1925
this.userService = userService;
26+
this.chatBotService = chatBotService;
2027
}
2128

22-
@PostMapping("/send")
23-
public ResponseEntity <?> sendEmail(@RequestParam int userId, @RequestParam String message) {
29+
@PostMapping("/report")
30+
public ResponseEntity<?> sendReportEmail(@RequestBody ReportEmailDTO reportEmailDTO) {
31+
try {
32+
if (chatBotService.findChatBotById(reportEmailDTO.getChatBotId()) == null) {
33+
return new ResponseEntity<>("Bot not found", HttpStatus.NOT_FOUND);
34+
}
2435

25-
return null;
36+
String ownerEmail = chatBotService.findChatBotById(reportEmailDTO.getChatBotId()).getOwner().getEmail();
37+
emailService.sendReport(reportEmailDTO.getUserEmail(), ownerEmail, reportEmailDTO.getChatId());
38+
return new ResponseEntity<>("Mail sent.", HttpStatus.OK);
39+
} catch (MessagingException e) {
40+
return new ResponseEntity<>("Failed to send email.", HttpStatus.INTERNAL_SERVER_ERROR);
41+
}
2642
}
43+
2744
}

server/src/main/java/com/semantiq/server/service/EmailService.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,32 @@ public EmailService(JavaMailSender javaMailSender, UserRepo userRepo) {
2626
public void sendVerificationCode(String recipientEmail) throws MessagingException {
2727
int verificationCode = userRepo.findByEmail(recipientEmail).getVerificationCode();
2828
String subject = "Verification Code";
29-
String message = "Your verification code is: " + String.valueOf(verificationCode);
29+
String message = "Your verification code is: " + String.valueOf(verificationCode) +
30+
"\nEnter the code in the box on the sign-up screen or try logging in with this email to re-enter the code.";
3031

3132
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
3233
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
33-
helper.setFrom("your_email@example.com");
34+
helper.setFrom("semantiq.app@gmail.com");
3435
helper.setTo(recipientEmail);
3536
helper.setSubject(subject);
3637
helper.setText(message, true);
3738

3839
javaMailSender.send(mimeMessage);
3940
}
41+
42+
// New method for sending a report to the owner when the user is dissatisfied
43+
public void sendReport(String userEmail, String ownerEmail, int chatId) throws MessagingException {
44+
String subject = "User Dissatisfaction Report";
45+
String message = "User with email " + userEmail + " is dissatisfied with the chatbot's answer.\n"
46+
+ "Chat ID: " + chatId + "\n\nPlease review the conversation history on your panel.";
47+
48+
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
49+
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
50+
helper.setFrom("semantiq.app@gmail.com");
51+
helper.setTo(ownerEmail);
52+
helper.setSubject(subject);
53+
helper.setText(message, true);
54+
55+
javaMailSender.send(mimeMessage);
56+
}
4057
}

server/src/main/java/com/semantiq/server/service/configuration/WebConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public void addCorsMappings(CorsRegistry registry) {
2828
.allowedMethods("GET", "POST", "PUT", "DELETE")
2929
// Add more configurations if needed
3030
.allowCredentials(true);
31+
32+
// CORS configuration for /static/**
33+
registry.addMapping("/api/email/")
34+
.allowedOriginPatterns("*") // Use allowedOriginPatterns instead of allowedOrigins
35+
.allowedMethods("GET", "POST", "PUT", "DELETE")
36+
// Add more configurations if needed
37+
.allowCredentials(true);
3138
}
3239
}
3340

server/src/main/resources/Files/static/main.ea9881ca.js renamed to server/src/main/resources/Files/static/main.57a7bb31.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/src/main/resources/Files/static/main.ea9881ca.js.LICENSE.txt renamed to server/src/main/resources/Files/static/main.57a7bb31.js.LICENSE.txt

File renamed without changes.

server/src/main/resources/Files/static/main.57a7bb31.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/src/main/resources/Files/static/main.ea9881ca.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)