From 04f5c2962a37e4e727a0f44c426416e1243dd352 Mon Sep 17 00:00:00 2001 From: jkingster Date: Fri, 7 Jun 2024 18:42:47 -0500 Subject: [PATCH] Fix: https://github.com/jkingster/QuickTicket/issues/59 --- .../quickticket/gui/alert/Alerts.java | 4 +- .../gui/alert/builder/InputDialogBuilder.java | 9 +- .../impl/ticket/TicketController.java | 75 ++++++++-------- .../impl/ticket/ViewerController.java | 87 +++++++++++-------- 4 files changed, 95 insertions(+), 80 deletions(-) diff --git a/src/main/java/io/jacobking/quickticket/gui/alert/Alerts.java b/src/main/java/io/jacobking/quickticket/gui/alert/Alerts.java index 971af5b..d264813 100644 --- a/src/main/java/io/jacobking/quickticket/gui/alert/Alerts.java +++ b/src/main/java/io/jacobking/quickticket/gui/alert/Alerts.java @@ -102,8 +102,8 @@ public static void showErrorOverride(final String title, final String content) { .showAndWait(); } - public static Optional showInput(final String title, final String content, final String defaultValue) { - return new InputDialogBuilder(defaultValue) + public static Optional showInput(final String title, final String content) { + return new InputDialogBuilder() .buildDialog(title, content) .result(); } diff --git a/src/main/java/io/jacobking/quickticket/gui/alert/builder/InputDialogBuilder.java b/src/main/java/io/jacobking/quickticket/gui/alert/builder/InputDialogBuilder.java index 2d14ceb..2f321d8 100644 --- a/src/main/java/io/jacobking/quickticket/gui/alert/builder/InputDialogBuilder.java +++ b/src/main/java/io/jacobking/quickticket/gui/alert/builder/InputDialogBuilder.java @@ -16,11 +16,9 @@ public class InputDialogBuilder { private final Dialog dialog; - private final String defaultValue; - public InputDialogBuilder(String defaultValue) { + public InputDialogBuilder() { this.dialog = new Dialog<>(); - this.defaultValue = defaultValue; initializeStyle(); } @@ -49,9 +47,10 @@ public InputDialogBuilder buildDialog(final String title, final String content) if (type == ButtonType.YES) { return textArea.getText(); } else if (type == ButtonType.NO) { - return null; + final String comment = textArea.getText(); + return comment.isEmpty() ? "" : comment; } - return defaultValue; + return ""; }); return this; } diff --git a/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/TicketController.java b/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/TicketController.java index 8776fe8..2d32386 100644 --- a/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/TicketController.java +++ b/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/TicketController.java @@ -217,43 +217,48 @@ private void addListener(final Label label, final ObservableList ta ticketModel.statusProperty().setValue(StatusType.RESOLVED); if (ticket.update(ticketModel, originalStatus)) { - Alerts.showInput( - "Notify Employee", - "Would you like to notify the employee the ticket is resolved along with adding an ending comment?", - "No resolving comment was added." - ).ifPresentOrElse(comment -> { - final EmployeeModel model = employee.getModel(ticketModel.getEmployeeId()); - if (model == null) { - Alerts.showError("Error notifying employee.", "Could not retrieve employee.", "Is there an employee attached to this ticket?"); - return; - } + pushNotifyAlert(ticketModel); + } + } - final String email = model.getEmail(); - if (email.isEmpty()) { - Alerts.showError( - "Error notifying employee.", - "Could not send notification e-mail.", - "There is no e-mail attached for this employee." - ); - return; - } + private void pushNotifyAlert(final TicketModel ticketModel) { + Alerts.showInput("Resolving Ticket", "Would you like to notify the employee this ticket is resolved? Please provide any closing comments.") + .ifPresentOrElse(resolvingComment -> { + if (resolvingComment.isEmpty()) { + postCommentOnTicket(ticketModel, "No resolving comment added."); + return; + } - new EmailBuilder(email, EmailBuilder.EmailType.RESOLVED) - .format( - ticketModel.getId(), - ticketModel.getTitle(), - DateUtil.formatDateTime(DateUtil.DateFormat.DATE_TIME_ONE, ticketModel.getCreation()), - model.getFullName(), - comment - ) - .email(emailConfig) - .setSubject(getSubject(ticketModel)) - .sendEmail(); - - postCommentOnTicket(ticketModel, comment); - }, () -> postCommentOnTicket(ticketModel, "No resolving comment.")); - ticketTable.refresh(); - } + final int employeeId = ticketModel.getEmployeeId(); + final EmployeeModel employeeModel = employee.getModel(employeeId); + if (employeeModel == null) { + Alerts.showError("Failed to send e-mail.", "Could not notify employee ticket is resolved.", "Could not fetch employee record."); + return; + } + + final String employeeEmail = employeeModel.getEmail(); + if (employeeEmail.isEmpty()) { + Alerts.showError("Failed to send e-mail.", "Could not notify employee ticket is resolved.", "Employee has no e-mail attached to employee record."); + return; + } + + postCommentOnTicket(ticketModel, resolvingComment); + sendResolvedEmail(ticketModel, employeeEmail, resolvingComment, employeeModel); + }, () -> postCommentOnTicket(ticketModel, "No resolving comment added.")); + } + + private void sendResolvedEmail(final TicketModel ticketModel, final String email, final String resolvingComment, final EmployeeModel employeeModel) { + new EmailBuilder(email, EmailBuilder.EmailType.RESOLVED) + .format( + ticketModel.getId(), + ticketModel.getTitle(), + DateUtil.formatDateTime(DateUtil.DateFormat.DATE_TIME_ONE, ticketModel.getCreation()), + employeeModel.getFullName(), + resolvingComment + ) + .email(emailConfig) + .setSubject(getSubject(ticketModel)) + .sendEmail(); } private String getSubject(final TicketModel ticketModel) { diff --git a/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/ViewerController.java b/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/ViewerController.java index 30a7969..3b097e4 100644 --- a/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/ViewerController.java +++ b/src/main/java/io/jacobking/quickticket/gui/controller/impl/ticket/ViewerController.java @@ -243,48 +243,59 @@ private void reloadPostUpdate(final SearchableComboBox box, final PopOver } @FXML private void onMarkResolved() { + final StatusType originalStatus = ticketModel.statusProperty().getValue(); ticketModel.statusProperty().setValue(StatusType.RESOLVED); - postSystemComment("System", "This ticket has been marked resolved."); - if (ticket.update(ticketModel)) { - refreshTable(); - - Alerts.showInput( - "Notify Employee", - "Would you like to notify the employee the ticket is resolved along with adding an ending comment?", - "No resolving comment was added." - ).ifPresentOrElse(comment -> { - final EmployeeModel model = employee.getModel(ticketModel.getEmployeeId()); - if (model == null) { - Alerts.showError("Error notifying employee.", "Could not retrieve employee.", "Is there an employee attached to this ticket?"); - return; - } + if (ticket.update(ticketModel, originalStatus)) { + System.out.println("?"); + pushNotifyAlert(ticketModel); + } + } - final String email = model.getEmail(); - if (email.isEmpty()) { - Alerts.showError( - "Error notifying employee.", - "Could not send notification e-mail.", - "There is no e-mail attached for this employee." - ); - return; - } + private void pushNotifyAlert(final TicketModel ticketModel) { + Alerts.showInput("Resolving Ticket", "Would you like to notify the employee this ticket is resolved? Please provide any closing comments.") + .ifPresentOrElse(resolvingComment -> { + if (resolvingComment.isEmpty()) { + postCommentOnTicket(ticketModel, "No resolving comment added."); + return; + } + + final int employeeId = ticketModel.getEmployeeId(); + final EmployeeModel employeeModel = employee.getModel(employeeId); + if (employeeModel == null) { + Alerts.showError("Failed to send e-mail.", "Could not notify employee ticket is resolved.", "Could not fetch employee record."); + return; + } + + final String employeeEmail = employeeModel.getEmail(); + if (employeeEmail.isEmpty()) { + Alerts.showError("Failed to send e-mail.", "Could not notify employee ticket is resolved.", "Employee has no e-mail attached to employee record."); + return; + } + + postCommentOnTicket(ticketModel, resolvingComment); + sendResolvedEmail(ticketModel, employeeEmail, resolvingComment, employeeModel); + }, () -> postCommentOnTicket(ticketModel, "No resolving comment added.")); + } - new EmailBuilder(email, EmailBuilder.EmailType.RESOLVED) - .format( - ticketModel.getId(), - ticketModel.getTitle(), - DateUtil.formatDateTime(DateUtil.DateFormat.DATE_TIME_ONE, ticketModel.getCreation()), - model.getFullName(), - comment - ) - .email(emailConfig) - .setSubject(getSubject(ticketModel)) - .sendEmail(); - - postSystemComment("Ticket Resolved", comment); - }, () -> postSystemComment("Ticket Resolved", "No resolving comment.")); - } + private void postCommentOnTicket(final TicketModel ticketModel, final String systemComment) { + comment.createModel(new Comment().setTicketId(ticketModel.getId()) + .setPostedOn(DateUtil.nowAsLocalDateTime(DateUtil.DateFormat.DATE_TIME_ONE)) + .setPost(String.format("[%s]: %s", "System", systemComment))); + } + + private void sendResolvedEmail(final TicketModel ticketModel, final String email, final String resolvingComment, final EmployeeModel employeeModel) { + new EmailBuilder(email, EmailBuilder.EmailType.RESOLVED) + .format( + ticketModel.getId(), + ticketModel.getTitle(), + DateUtil.formatDateTime(DateUtil.DateFormat.DATE_TIME_ONE, ticketModel.getCreation()), + employeeModel.getFullName(), + resolvingComment + ) + .email(emailConfig) + .setSubject(getSubject(ticketModel)) + .sendEmail(); } private String getSubject(final TicketModel ticketModel) {