|
|
|
|
@ -19,6 +19,7 @@ import de.srsoftware.tools.Path;
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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); |
|
|
|
|
|