implemented moving of items

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-20 22:25:47 +02:00
parent b1517edc31
commit 715d2b0f31
5 changed files with 75 additions and 26 deletions

View File

@@ -7,13 +7,16 @@ public class Constants {
public static final String BELOW = "below";
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
public static final String ITEM = "item";
public static final String ITEM_ID = "item_id";
public static final String ITEMS = "items";
public static final String LOCATIONS = "locations";
public static final String MOVE_ITEM = "move_item";
public static final String OF_USER = "of_user";
public static final String PROPERTY_ID = "prop_id";
public static final String TABLE_ITEMS = "items";
public static final String TABLE_ITEM_PROPERTIES = "item_props";
public static final String TABLE_LOCATIONS = "locations";
public static final String TABLE_PROPERTIES = "properties";
public static final String TARGET = "target";
}

View File

@@ -19,6 +19,7 @@ import de.srsoftware.tools.Path;
import de.srsoftware.tools.SessionToken;
import de.srsoftware.umbrella.core.BaseHandler;
import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.api.StockService;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.*;
@@ -38,6 +39,14 @@ public class StockModule extends BaseHandler implements StockService {
ModuleRegistry.add(this);
}
private boolean assigned(Owner owner, UmbrellaUser user){
owner = owner.resolve();
if (owner instanceof UmbrellaUser u && user.id() == u.id()) return true;
if (owner instanceof Company comp) return companyService().membership(comp.id(),user.id());
return false;
}
@Override
public boolean doGet(Path path, HttpExchange ex) throws IOException {
addCors(ex);
@@ -71,7 +80,11 @@ public class StockModule extends BaseHandler implements StockService {
Optional<Token> token = SessionToken.from(ex).map(Token::of);
var user = userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
return patchItem(user.get(),ex);
return switch (path.pop()){
case MOVE_ITEM -> patchMove(user.get(), path,ex);
case null -> patchItem(user.get(),ex);
default -> super.doPatch(path,ex);
};
} catch (UmbrellaException e){
return send(ex,e);
}
@@ -167,6 +180,25 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,stockDb.save(item));
}
private boolean patchMove(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(ITEM) instanceof Number itemId)) throw missingFieldException(ITEM);
if (!(json.get(TARGET) instanceof Number locationId)) throw missingFieldException(TARGET);
var item = stockDb.loadItem(itemId.longValue());
var itemOwner = item.owner().resolve();
if (!assigned(itemOwner,user)) throw forbidden("You are not allowed to alter the location of \"{0}\"!",item.name());
var target = stockDb.loadLocation(locationId.longValue());
var locOwner = target.resolve().owner().resolve();
if (!assigned(locOwner,user)) throw forbidden("You are not allowed to modify \"{0}\"!",target.name());
if (!locOwner.equals(itemOwner)) throw unprocessable("You may not move items from one owner ({0}) to another ({1})",itemOwner,locOwner);
stockDb.save(item.location(target));
return sendContent(ex,item);
}
private boolean postProperty(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(FIELD_ITEM) instanceof JSONObject itemData)) throw missingFieldException(FIELD_ITEM);