contact form with email
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.petshop.backend.controller;
|
||||
|
||||
import com.petshop.backend.entity.User;
|
||||
import com.petshop.backend.repository.UserRepository;
|
||||
import com.petshop.backend.service.EmailService;
|
||||
import com.petshop.backend.util.AuthenticationHelper;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/contact")
|
||||
public class ContactController {
|
||||
|
||||
private final EmailService emailService;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public ContactController(EmailService emailService, UserRepository userRepository) {
|
||||
this.emailService = emailService;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public record ContactRequest(
|
||||
@NotBlank @Size(max = 150) String subject,
|
||||
@NotBlank @Size(max = 2000) String body
|
||||
) {}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<Void> sendContactEmail(@Valid @RequestBody ContactRequest req) {
|
||||
Long userId = AuthenticationHelper.getAuthenticatedUserId();
|
||||
User user = userRepository.findById(userId).orElseThrow();
|
||||
emailService.sendContactMessage(user, req.subject(), req.body());
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,19 @@ public class EmailService {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendContactMessage(User user, String subject, String body) {
|
||||
if (user.getEmail() == null || user.getEmail().isBlank()) return;
|
||||
String html = """
|
||||
<div style="font-family:sans-serif;max-width:600px;margin:auto">
|
||||
<h2>Contact form message</h2>
|
||||
<p><strong>From:</strong> %s (%s)</p>
|
||||
<p><strong>Subject:</strong> %s</p>
|
||||
<hr/>
|
||||
<p style="white-space:pre-wrap">%s</p>
|
||||
</div>""".formatted(esc(firstName(user)), esc(user.getEmail()), esc(subject), esc(body));
|
||||
send(user.getId(), user.getEmail(), "Contact: " + subject, html);
|
||||
}
|
||||
|
||||
public void sendChatTranscript(Conversation conversation, List<Message> messages, User customer) {
|
||||
if (customer == null || customer.getEmail() == null || customer.getEmail().isBlank()) return;
|
||||
String subject = "Your PetShop support transcript";
|
||||
|
||||
Reference in New Issue
Block a user