package com.alttd.forms.contact; import com.alttd.forms.mail.verification.VerificationResult; import com.alttd.forms.mail.verification.Verify; import jakarta.validation.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.concurrent.CompletableFuture; @RestController @RequestMapping("/api/contact") public class ContactController { private static final Logger logger = LoggerFactory.getLogger(ContactController.class); @PostMapping("/submitContactForm") public CompletableFuture> submitForm(@Valid @RequestBody ContactFormData formData) { logger.debug(formData.toString()); CompletableFuture storeFormForVerificationCode = new StoreFormQuery().storeFormForVerificationCode(formData.email, formData); return storeFormForVerificationCode.thenCompose(code -> Verify.verifyEmail(formData.email, code).thenApply(verificationResult -> { if (verificationResult == VerificationResult.VERIFICATION_SENT) { //TODO if this is ok tell the user they have x min to verify if they fail to do so they have to remake the form return ResponseEntity.ok("User Data received and email verification sent."); } else { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("Failed to send verification email. Reason: " + verificationResult.name()); } })).exceptionally(throwable -> { logger.error("Failed to store form", throwable); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to store your form"); }); } }