desktop: added confirmation to change onwer and only admins can change this

This commit is contained in:
Alex
2026-04-12 20:01:06 -06:00
parent 5f7f40f98a
commit 1f801d7486

View File

@@ -17,6 +17,7 @@ import org.example.petshopdesktop.api.dto.pet.PetRequest;
import org.example.petshopdesktop.api.dto.pet.PetResponse;
import org.example.petshopdesktop.api.endpoints.DropdownApi;
import org.example.petshopdesktop.api.endpoints.PetApi;
import org.example.petshopdesktop.auth.UserSession;
import org.example.petshopdesktop.models.Pet;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.DesktopImageSupport;
@@ -25,6 +26,8 @@ import org.example.petshopdesktop.util.FilePickerSupport;
import java.io.File;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public class PetDialogController {
@@ -92,6 +95,7 @@ public class PetDialogController {
private Long pendingCustomerId = null;
private Long pendingStoreId = null;
private Long originalCustomerId = null;
private ObservableList<String> statusList = FXCollections.observableArrayList(
"Available", "Adopted", "Owned", "Pending"
@@ -177,7 +181,7 @@ public class PetDialogController {
if (cbPetStatus.getSelectionModel().getSelectedItem() == null){
errorMsg += "Status is required";
}
if ("Owned".equalsIgnoreCase(selectedStatus) && cbCustomer.getValue() == null) {
if ("Owned".equalsIgnoreCase(selectedStatus) && cbCustomer.getValue() == null && UserSession.getInstance().isAdmin()) {
errorMsg += "Customer is required for Owned status\n";
}
boolean storeRequired = requiresStore(selectedStatus) && !"Adopted".equalsIgnoreCase(selectedStatus);
@@ -201,6 +205,18 @@ public class PetDialogController {
errorMsg += Validator.isPositiveInteger(txtPetAge.getText(), "Age");
if(errorMsg.isEmpty()){
if ("Edit".equals(mode) && UserSession.getInstance().isAdmin()) {
Long newCustomerId = cbCustomer.getValue() != null ? cbCustomer.getValue().getId() : null;
if (!Objects.equals(originalCustomerId, newCustomerId)) {
Alert confirm = new Alert(Alert.AlertType.CONFIRMATION);
confirm.setHeaderText("Confirm Owner Change");
confirm.setContentText("Are you sure you want to reassign this pet to a different owner?");
Optional<ButtonType> result = confirm.showAndWait();
if (result.isEmpty() || result.get() != ButtonType.OK) {
return;
}
}
}
PetRequest request = buildPetRequest();
try {
if(mode.equals("Add")) {
@@ -381,6 +397,7 @@ public class PetDialogController {
refreshImagePreview();
pendingCustomerId = pet.getCustomerId() > 0 ? pet.getCustomerId() : null;
originalCustomerId = pendingCustomerId;
pendingStoreId = pet.getStoreId() > 0 ? pet.getStoreId() : null;
for (String status : cbPetStatus.getItems()) {
@@ -472,9 +489,10 @@ public class PetDialogController {
}
private void updateStatusFieldVisibility(String status) {
boolean needsCustomer = "Owned".equalsIgnoreCase(status) || "Adopted".equalsIgnoreCase(status);
boolean statusNeedsCustomer = "Owned".equalsIgnoreCase(status) || "Adopted".equalsIgnoreCase(status);
boolean needsCustomer = statusNeedsCustomer && UserSession.getInstance().isAdmin();
boolean storeBased = requiresStore(status);
boolean needsPrice = !needsCustomer;
boolean needsPrice = !statusNeedsCustomer;
setFieldVisibility(vbCustomerField, needsCustomer);
setFieldVisibility(vbStoreField, storeBased);
setFieldVisibility(vbPriceField, needsPrice);