22
33import cat .udl .eps .softarch .demo .domain .Business ;
44import cat .udl .eps .softarch .demo .domain .Inventory ;
5+ import cat .udl .eps .softarch .demo .domain .InventoryStatus ;
6+ import cat .udl .eps .softarch .demo .domain .InventoryType ;
57import cat .udl .eps .softarch .demo .repository .InventoryRepository ;
68import org .slf4j .Logger ;
79import org .slf4j .LoggerFactory ;
10+ import java .time .LocalDateTime ;
811import org .springframework .data .rest .core .annotation .*;
912import org .springframework .security .access .AccessDeniedException ;
1013import org .springframework .security .core .Authentication ;
1417@ Component
1518@ RepositoryEventHandler
1619public class InventoryEventHandler {
17-
1820 private final Logger logger = LoggerFactory .getLogger (InventoryEventHandler .class );
1921 private final InventoryRepository inventoryRepository ;
2022
@@ -25,29 +27,49 @@ public InventoryEventHandler(InventoryRepository inventoryRepository) {
2527 @ HandleBeforeCreate
2628 public void handleBeforeCreate (Inventory inventory ) {
2729 logger .info ("Before creating inventory: {}" , inventory );
28-
2930 Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
30-
31- // 1. Validació
3231 if (auth == null || !auth .isAuthenticated ()) {
3332 throw new AccessDeniedException ("Must be logged in to create inventory" );
3433 }
3534
36-
37- Object principal = auth .getPrincipal ();
38-
39- if (principal instanceof Business ) {
40- Business business = (Business ) principal ;
35+ if (auth .getPrincipal () instanceof Business business ) {
4136 inventory .setBusiness (business );
4237 } else {
43- throw new AccessDeniedException ("Only Business accounts can create inventories. You are: " + principal . getClass (). getSimpleName () );
38+ throw new AccessDeniedException ("Only Business accounts can create inventories" );
4439 }
40+
41+ if (inventory .getStatus () == null )
42+ inventory .setStatus (InventoryStatus .ACTIVE );
43+ if (inventory .getType () == null )
44+ inventory .setType (InventoryType .WAREHOUSE );
45+ inventory .setLastUpdated (LocalDateTime .now ());
4546 }
4647
4748 @ HandleBeforeSave
4849 public void handleBeforeSave (Inventory inventory ) {
4950 logger .info ("Before updating inventory: {}" , inventory );
5051 checkOwnership (inventory );
52+
53+ // ONLY sync if products are loaded.
54+ // If not loaded, respect the totalStock value coming from the UI/request.
55+ if (inventory .getProducts () != null && !inventory .getProducts ().isEmpty ()) {
56+ inventory .syncTotalStock ();
57+ }
58+
59+ // Capacity logic
60+ if (inventory .getCapacity () != null && inventory .getCapacity () > 0 ) {
61+ if (inventory .getTotalStock () >= inventory .getCapacity ()) {
62+ if (inventory .getStatus () != InventoryStatus .CLOSED
63+ && inventory .getStatus () != InventoryStatus .MAINTENANCE ) {
64+ inventory .setStatus (InventoryStatus .FULL );
65+ logger .info ("Inventory {} reached capacity. Status set to FULL." , inventory .getId ());
66+ }
67+ } else if (inventory .getStatus () == InventoryStatus .FULL ) {
68+ inventory .setStatus (InventoryStatus .ACTIVE );
69+ logger .info ("Inventory {} has space. Status restored to ACTIVE." , inventory .getId ());
70+ }
71+ }
72+ inventory .setLastUpdated (LocalDateTime .now ());
5173 }
5274
5375 @ HandleBeforeDelete
@@ -59,13 +81,10 @@ public void handleBeforeDelete(Inventory inventory) {
5981 private void checkOwnership (Inventory inventory ) {
6082 Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
6183 String currentUsername = auth .getName ();
62-
6384 if (inventory .getBusiness () != null && !inventory .getBusiness ().getUsername ().equals (currentUsername )) {
64- boolean isAdmin = auth .getAuthorities ().stream ()
65- .anyMatch (a -> a .getAuthority ().equals ("ROLE_ADMIN" ));
66-
85+ boolean isAdmin = auth .getAuthorities ().stream ().anyMatch (a -> a .getAuthority ().equals ("ROLE_ADMIN" ));
6786 if (!isAdmin ) {
68- throw new AccessDeniedException ("You can only modify your own inventory" );
87+ throw new AccessDeniedException ("You are not the owner of this inventory" );
6988 }
7089 }
7190 }
0 commit comments