Refactor form submission to use dynamic Discord URLs and emails
Updated form classes to return Optional URLs for Discord bot submissions. Refactored VerifyController to handle these Optionals and improved error handling when sending forms. Added receiver email method in form classes for more flexible form submissions.
This commit is contained in:
parent
f972436717
commit
caabe7b923
|
|
@ -4,10 +4,8 @@
|
|||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Update Jackson config and refactor JSON handling Introduced Jackson dependencies to replace Gson for JSON processing. Updated application properties and controllers to handle Jackson-specific exceptions. Refactored form serialization to use Jackson's `ObjectMapper` for better date handling and consistency.">
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/contact/ContactFormData.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/contact/ContactFormData.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/verify_mail/VerifyController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/verify_mail/VerifyController.java" afterDir="false" />
|
||||
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Add getDiscordBotUrl method to form classes Implemented getDiscordBotUrl in form classes for dynamic URL handling. Updated VerifyController to use this method for constructing Discord bot URIs. This enhances flexibility and maintainability in form submission handling.">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
|
@ -543,7 +541,15 @@
|
|||
<option name="project" value="LOCAL" />
|
||||
<updated>1722981496092</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="23" />
|
||||
<task id="LOCAL-00023" summary="Add getDiscordBotUrl method to form classes Implemented getDiscordBotUrl in form classes for dynamic URL handling. Updated VerifyController to use this method for constructing Discord bot URIs. This enhances flexibility and maintainability in form submission handling.">
|
||||
<option name="closed" value="true" />
|
||||
<created>1722981675331</created>
|
||||
<option name="number" value="00023" />
|
||||
<option name="presentableId" value="LOCAL-00023" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1722981675331</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="24" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
|
|
@ -584,6 +590,7 @@
|
|||
<MESSAGE value="Update CORS allowed origins configuration Consolidated the allowed origins into a single `allowedOrigins` call. This ensures both local and production environments are correctly configured for CORS." />
|
||||
<MESSAGE value="Add REST annotations and refactor properties file handling Introduced `@RestController` and `@RequestMapping` in `StaffAppController` for standardized API endpoints. Refactored properties file handling in `PropertiesLoader` and `PropertiesWriter` to simplify file creation logic." />
|
||||
<MESSAGE value="Update Jackson config and refactor JSON handling Introduced Jackson dependencies to replace Gson for JSON processing. Updated application properties and controllers to handle Jackson-specific exceptions. Refactored form serialization to use Jackson's `ObjectMapper` for better date handling and consistency." />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Update Jackson config and refactor JSON handling Introduced Jackson dependencies to replace Gson for JSON processing. Updated application properties and controllers to handle Jackson-specific exceptions. Refactored form serialization to use Jackson's `ObjectMapper` for better date handling and consistency." />
|
||||
<MESSAGE value="Add getDiscordBotUrl method to form classes Implemented getDiscordBotUrl in form classes for dynamic URL handling. Updated VerifyController to use this method for constructing Discord bot URIs. This enhances flexibility and maintainability in form submission handling." />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Add getDiscordBotUrl method to form classes Implemented getDiscordBotUrl in form classes for dynamic URL handling. Updated VerifyController to use this method for constructing Discord bot URIs. This enhances flexibility and maintainability in form submission handling." />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -6,6 +6,7 @@ import org.hibernate.validator.constraints.Length;
|
|||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Optional;
|
||||
|
||||
public class StaffAppFormData extends Form {
|
||||
|
||||
|
|
@ -109,8 +110,13 @@ public class StaffAppFormData extends Form {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getDiscordBotUrl() {
|
||||
return "http://discordbot:8001/api/apply/staffApplication";
|
||||
public Optional<String> getDiscordBotUrl() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReceiver() {
|
||||
return "apply@alttd.com";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import jakarta.validation.constraints.NotEmpty;
|
|||
import jakarta.validation.constraints.Pattern;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ContactFormData extends Form {
|
||||
|
||||
public ContactFormData(String username, String email, String question) {
|
||||
|
|
@ -37,8 +39,13 @@ public class ContactFormData extends Form {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getDiscordBotUrl() {
|
||||
return "http://discordbot:8001/api/contact/submitContactForm";
|
||||
public Optional<String> getDiscordBotUrl() {
|
||||
return Optional.of("http://discordbot:8001/api/contact/submitContactForm");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReceiver() {
|
||||
return "support@alttd.com";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class Form {
|
||||
|
||||
public String toJsonString() throws JsonProcessingException {
|
||||
|
|
@ -36,5 +38,7 @@ public abstract class Form {
|
|||
@Override
|
||||
public abstract String toString();
|
||||
|
||||
public abstract String getDiscordBotUrl();
|
||||
public abstract Optional<String> getDiscordBotUrl();
|
||||
|
||||
public abstract String getReceiver();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.form.Form;
|
||||
import com.alttd.forms.mail.mail_forms.MailForm;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import jakarta.validation.Valid;
|
||||
|
|
@ -18,6 +19,7 @@ import java.net.http.HttpClient;
|
|||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RestController
|
||||
|
|
@ -32,34 +34,14 @@ public class VerifyController {
|
|||
logger.trace("verificationData: {}", verificationData);
|
||||
return new FormQuery().getFormForCode(verificationData.code, verificationData.eMail).thenApply(result -> result.form()
|
||||
.map(form -> {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
//Discord bot url
|
||||
.uri(new URI(form.getDiscordBotUrl()))//TODO get uri from form
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
logger.trace("request: {}", request);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
logger.trace("response: {}", response);
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
//TODO strings to config
|
||||
return ResponseEntity.ok("Failed to send form to Discord, please contact us at admin@alttd.com");
|
||||
Optional<String> discordBotUrl = form.getDiscordBotUrl();
|
||||
if (discordBotUrl.isPresent()){
|
||||
Optional<ResponseEntity<String>> stringResponseEntity = sendDiscordMessage(discordBotUrl.get(), form);
|
||||
if (stringResponseEntity.isPresent()){
|
||||
return stringResponseEntity.get();
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to create URI for posting form, please contact us at admin@alttd.com");
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at admin@alttd.com");
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at admin@alttd.com");
|
||||
}
|
||||
MailForm.sendForm("akastijn@alttd.com", form);
|
||||
MailForm.sendForm(form.getReceiver(), form);
|
||||
try {
|
||||
return ResponseEntity.ok(form.toJsonString());
|
||||
} catch (JsonProcessingException e) {
|
||||
|
|
@ -71,4 +53,34 @@ public class VerifyController {
|
|||
).exceptionally(throwable -> ResponseEntity.internalServerError()
|
||||
.body("The server was unable to process your request, if this issue persists please contact admin@alttd.com"));
|
||||
}
|
||||
|
||||
private Optional<ResponseEntity<String>> sendDiscordMessage(String discordBotUrl, Form form) {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI(discordBotUrl))
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
logger.trace("request: {}", request);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
logger.trace("response: {}", response);
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
//TODO strings to config
|
||||
return Optional.of(ResponseEntity.ok("Failed to send form to Discord, please contact us at admin@alttd.com"));
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return Optional.of(ResponseEntity.ok("Unable to create URI for posting form, please contact us at admin@alttd.com"));
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return Optional.of(ResponseEntity.ok("Unable to send form to Discord, please contact us at admin@alttd.com"));
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return Optional.of(ResponseEntity.ok("Unable to send form to Discord, please contact us at admin@alttd.com"));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user