From 6065ad5c57c4092cf5b42349b15960f6fab73522 Mon Sep 17 00:00:00 2001
From: "Adam.Slachta" <slachta.a@nekom.com>
Date: Wed, 12 Apr 2023 11:48:28 +0200
Subject: [PATCH] feature/SS-1216 - bookstock on requested storageplacePk if it
 is free. If not free, than take any storageplace fulfilling conditions from
 the request (storage,isSellable)

---
 server/commons.go                 | 20 +++++++-------------
 server/grpcserver.go              | 30 +++++++++++++++---------------
 server/httpserver.go              | 18 +++++++++---------
 server/storagecalculation.go      |  3 +--
 stockconnector/DatabaseHandler.go | 16 ++++++++++++++--
 stockconnector/DbMock.go          |  2 +-
 6 files changed, 47 insertions(+), 42 deletions(-)

diff --git a/server/commons.go b/server/commons.go
index b6effd1..b398c42 100644
--- a/server/commons.go
+++ b/server/commons.go
@@ -15,7 +15,7 @@ type StockHandler struct {
 	Db oc.DbHandlerI
 }
 
-func (oh *StockHandler) PutStockForItemUUIDandStoraplace(ctx context.Context, requestObject *oc.PutRequestObject, token string, clientuuid string) *oc.Response {
+func (oh *StockHandler) PutStockForItemUUIDandStoraplace(ctx context.Context, requestObject *oc.PutRequestObject, storagecalcRequest *oc.RequestObject, token string, clientuuid string) *oc.Response {
 	var response oc.Response
 	var stockResponse oc.Stockresponse
 	clientpk, _ := backend.GetPkFromUuid(clientuuid)
@@ -29,7 +29,9 @@ func (oh *StockHandler) PutStockForItemUUIDandStoraplace(ctx context.Context, re
 		itemuuid, _ := backend.GetUuidfromPk(itemFromDatabase.Pk)
 		requestObject.Itemuuid = itemuuid
 	}
-	stock, err := oh.Db.PutStockForItemUUIDandStoraplace(requestObject, abc)
+
+	freeStoregaplacesResponse := oh.GetFreeStorageplacesforItem(storagecalcRequest, abc.Client)
+	stock, err := oh.Db.PutStockForItemUUIDandStoraplace(requestObject, freeStoregaplacesResponse.Storageplaces, abc)
 	if err != nil {
 		log.Println(err)
 		return InternalServerErrorResponse()
@@ -95,7 +97,7 @@ func (oh *StockHandler) GetWebshopSellableStockByItemUUID(requestObject *oc.Requ
 	return &response
 }
 
-func (oh *StockHandler) GetStockbyItemAndStorageName(ctx context.Context, requestObject *oc.RequestObject) *oc.Response {
+func (oh *StockHandler) GetStockbyItemAndStorageName(requestObject *oc.RequestObject) *oc.Response {
 	var response oc.Response
 	var stockResponse oc.Stockresponse
 	stock, err := oh.Db.GetStockbyItemAndStorageName(requestObject, requestObject.Client)
@@ -109,7 +111,7 @@ func (oh *StockHandler) GetStockbyItemAndStorageName(ctx context.Context, reques
 	return &response
 }
 
-func (oh *StockHandler) GetSellableStockbyItem(ctx context.Context, requestObject *oc.RequestObject) *oc.Response {
+func (oh *StockHandler) GetSellableStockbyItem(requestObject *oc.RequestObject) *oc.Response {
 	var response oc.Response
 	var stockResponse oc.Stockresponse
 
@@ -123,7 +125,7 @@ func (oh *StockHandler) GetSellableStockbyItem(ctx context.Context, requestObjec
 	return &response
 }
 
-func (oh *StockHandler) GetReservedStockbyItem(ctx context.Context, requestObject *oc.RequestObject) *oc.Response {
+func (oh *StockHandler) GetReservedStockbyItem(requestObject *oc.RequestObject) *oc.Response {
 	var response oc.Response
 	var stockResponse oc.Stockresponse
 
@@ -475,14 +477,6 @@ func BadRequestResponse(errorMessage string) *oc.Response {
 	}
 }
 
-func NotFoundResponse() *oc.Response {
-	return &oc.Response{
-		Success:      false,
-		ErrorMessage: "Requested entity does not exist",
-		ErrorType:    "NotFound",
-	}
-}
-
 func InternalServerErrorResponse() *oc.Response {
 	return &oc.Response{
 		Success:      false,
diff --git a/server/grpcserver.go b/server/grpcserver.go
index abcd3bc..1554e46 100644
--- a/server/grpcserver.go
+++ b/server/grpcserver.go
@@ -65,14 +65,14 @@ func (server *stockConnectorServer) PutStockForItemUUIDandStoraplace(ctx context
 
 	if auth {
 		var storagename []*oc.Storageplace
+		requestObjectForStoragecalc := oc.RequestObject{
+			Itemuuid:    requestObject.Itemuuid,
+			Itemnumber:  requestObject.Itemnumber,
+			Storagename: requestObject.Warehouse,
+			Client:      clientuuid,
+			Quantity:    requestObject.StockAmount,
+		}
 		if len(requestObject.Storagename) == 0 {
-			requestObjectForStoragecalc := oc.RequestObject{
-				Itemuuid:    requestObject.Itemuuid,
-				Itemnumber:  requestObject.Itemnumber,
-				Storagename: requestObject.Warehouse,
-				Client:      clientuuid,
-				Quantity:    requestObject.StockAmount,
-			}
 			storageplaceResponse, errStorageplace := server.GetFreeStorageplacesforItemnumber(ctx, &requestObjectForStoragecalc)
 			if errStorageplace != nil {
 				response = InternalServerErrorResponse()
@@ -82,7 +82,7 @@ func (server *stockConnectorServer) PutStockForItemUUIDandStoraplace(ctx context
 				storagename = storageplaceResponse.Storageplaces
 			}
 		}
-		response = server.Handler.PutStockForItemUUIDandStoraplace(ctx, requestObject, token, clientuuid)
+		response = server.Handler.PutStockForItemUUIDandStoraplace(ctx, requestObject, &requestObjectForStoragecalc, token, clientuuid)
 		if len(storagename) > 0 {
 			response.Stock.Storageplaces = storagename[0]
 		}
@@ -136,7 +136,7 @@ func (server *stockConnectorServer) GetStockbyItemUUID(ctx context.Context, requ
 	auth, _ := server.Auth.CanAccess(token, uuid, callerId)
 
 	if auth {
-		response = server.Handler.GetStockbyItemAndStorageName(ctx, requestObject)
+		response = server.Handler.GetStockbyItemAndStorageName(requestObject)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -165,7 +165,7 @@ func (server *stockConnectorServer) GetStockbyItemnumber(ctx context.Context, re
 		if requestObject.Client == "" {
 			requestObject.Client, _ = backend.GetUuidfromPk(abc.Client)
 		}
-		response = server.Handler.GetStockbyItemAndStorageName(ctx, requestObject)
+		response = server.Handler.GetStockbyItemAndStorageName(requestObject)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -180,7 +180,7 @@ func (server *stockConnectorServer) GetSellableStockbyItemUUID(ctx context.Conte
 
 	if auth {
 
-		response = server.Handler.GetSellableStockbyItem(ctx, requestObject)
+		response = server.Handler.GetSellableStockbyItem(requestObject)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -197,7 +197,7 @@ func (server *stockConnectorServer) GetSellableStockbyItemnumber(ctx context.Con
 		if requestObject.Client == "" {
 			requestObject.Client, _ = backend.GetUuidfromPk(abc.Client)
 		}
-		response = server.Handler.GetSellableStockbyItem(ctx, requestObject)
+		response = server.Handler.GetSellableStockbyItem(requestObject)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -211,7 +211,7 @@ func (server *stockConnectorServer) GetReservedStockbyItemUUID(ctx context.Conte
 	auth, _ := server.Auth.CanAccess(token, uuid, callerId)
 
 	if auth {
-		response = server.Handler.GetReservedStockbyItem(ctx, requestObject)
+		response = server.Handler.GetReservedStockbyItem(requestObject)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -225,7 +225,7 @@ func (server *stockConnectorServer) GetReservedStockbyItemnumber(ctx context.Con
 	auth, _ := server.Auth.CanAccess(token, uuid, callerId)
 
 	if auth {
-		response = server.Handler.GetReservedStockbyItem(ctx, requestObject)
+		response = server.Handler.GetReservedStockbyItem(requestObject)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -239,7 +239,7 @@ func (server *stockConnectorServer) GetFreeStorageplacesforItemnumber(ctx contex
 	auth, appbrandclient := server.Auth.CanAccess(token, uuid, callerId)
 
 	if auth {
-		response = server.Handler.GetFreeStorageplacesforItem(ctx, requestObject, appbrandclient.Client)
+		response = server.Handler.GetFreeStorageplacesforItem(requestObject, appbrandclient.Client)
 	} else {
 		response = AccessDeniedStorageplaceResponse()
 	}
diff --git a/server/httpserver.go b/server/httpserver.go
index 307b2fa..0a152b5 100644
--- a/server/httpserver.go
+++ b/server/httpserver.go
@@ -29,7 +29,7 @@ func GetStockbyItemUUID(w http.ResponseWriter, req *http.Request) {
 	request = &oc.RequestObject{Itemuuid: itemuuid, Storagename: storagename, Client: uuid}
 	parsedRequest := request.(*oc.RequestObject)
 	if auth {
-		response = oh.GetStockbyItemAndStorageName(context.Background(), parsedRequest)
+		response = oh.GetStockbyItemAndStorageName(parsedRequest)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -48,7 +48,7 @@ func GetStockbyItemnumber(w http.ResponseWriter, req *http.Request) {
 	request = &oc.RequestObject{Itemnumber: itemuuid, Storagename: storagename, Client: uuid}
 	parsedRequest := request.(*oc.RequestObject)
 	if auth {
-		response = oh.GetStockbyItemAndStorageName(context.Background(), parsedRequest)
+		response = oh.GetStockbyItemAndStorageName(parsedRequest)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -81,7 +81,7 @@ func GetFreeStorageplaces(w http.ResponseWriter, req *http.Request) {
 	}
 	parsedRequest := request.(*oc.RequestObject)
 	if auth {
-		response = oh.GetFreeStorageplacesforItem(context.Background(), parsedRequest, pkFromUuid)
+		response = oh.GetFreeStorageplacesforItem(parsedRequest, pkFromUuid)
 	} else {
 		response = AccessDeniedStorageplaceResponse()
 	}
@@ -116,13 +116,13 @@ func PutStock(w http.ResponseWriter, req *http.Request) {
 		Quantity:    int64(quantityInt),
 	}
 	clientpk, _ := backend.GetPkFromUuid(uuid)
-	storageplaceResponse := oh.GetFreeStorageplacesforItem(context.Background(), &requestObjectForStoragecalc, clientpk)
+	storageplaceResponse := oh.GetFreeStorageplacesforItem(&requestObjectForStoragecalc, clientpk)
 	parsedRequest := request.(*oc.PutRequestObject)
 	if storageplaceResponse != nil && len(storageplaceResponse.Storageplaces) > 0 {
 		parsedRequest.Storagename = storageplaceResponse.Storageplaces[0].Name
 	}
 	if auth {
-		response = oh.PutStockForItemUUIDandStoraplace(context.Background(), parsedRequest, authToken(req), uuid)
+		response = oh.PutStockForItemUUIDandStoraplace(context.Background(), parsedRequest, &requestObjectForStoragecalc, authToken(req), uuid)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -140,7 +140,7 @@ func GetSellableStockbyItemUUID(w http.ResponseWriter, req *http.Request) {
 	request = &oc.RequestObject{Itemuuid: itemuuid, Client: uuid}
 	parsedRequest := request.(*oc.RequestObject)
 	if auth {
-		response = oh.GetSellableStockbyItem(context.Background(), parsedRequest)
+		response = oh.GetSellableStockbyItem(parsedRequest)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -158,7 +158,7 @@ func GetSellableStockbyItemnumber(w http.ResponseWriter, req *http.Request) {
 	request = &oc.RequestObject{Itemnumber: itemuuid, Client: uuid}
 	parsedRequest := request.(*oc.RequestObject)
 	if auth {
-		response = oh.GetSellableStockbyItem(context.Background(), parsedRequest)
+		response = oh.GetSellableStockbyItem(parsedRequest)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -176,7 +176,7 @@ func GetReservedStockbyItemUUID(w http.ResponseWriter, req *http.Request) {
 	request = &oc.RequestObject{Itemnumber: itemuuid, Client: uuid}
 	parsedRequest := request.(*oc.RequestObject)
 	if auth {
-		response = oh.GetReservedStockbyItem(context.Background(), parsedRequest)
+		response = oh.GetReservedStockbyItem(parsedRequest)
 	} else {
 		response = AccessDeniedResponse()
 	}
@@ -194,7 +194,7 @@ func GetReservedStockbyItemnumber(w http.ResponseWriter, req *http.Request) {
 	request = &oc.RequestObject{Itemnumber: itemuuid, Client: uuid}
 	parsedRequest := request.(*oc.RequestObject)
 	if auth {
-		response = oh.GetReservedStockbyItem(context.Background(), parsedRequest)
+		response = oh.GetReservedStockbyItem(parsedRequest)
 	} else {
 		response = AccessDeniedResponse()
 	}
diff --git a/server/storagecalculation.go b/server/storagecalculation.go
index 92afaa7..f79ac4b 100644
--- a/server/storagecalculation.go
+++ b/server/storagecalculation.go
@@ -5,13 +5,12 @@ import (
 	oc "gitlab.nekom.com/microservices/apistock/stockconnector"
 	do "gitlab.nekom.com/microservices/apistock/stockconnector/dataobjects"
 	"gitlab.nekom.com/microservices/goutils/backend"
-	"golang.org/x/net/context"
 	"log"
 )
 
 const CALCULATION_PUFFER = 1.1
 
-func (oh *StockHandler) GetFreeStorageplacesforItem(ctx context.Context, requestObject *oc.RequestObject, client int) *oc.StorageplaceResponse {
+func (oh *StockHandler) GetFreeStorageplacesforItem(requestObject *oc.RequestObject, client int) *oc.StorageplaceResponse {
 	response := &oc.StorageplaceResponse{}
 	var freeStorageplaces []do.StorageplaceDto
 	storageplaceResponse, validationFailed := validateStorageplaceRequest(requestObject)
diff --git a/stockconnector/DatabaseHandler.go b/stockconnector/DatabaseHandler.go
index 5cde668..30467ae 100644
--- a/stockconnector/DatabaseHandler.go
+++ b/stockconnector/DatabaseHandler.go
@@ -56,7 +56,7 @@ type DbHandlerI interface {
 	GetSellableStockbyItem(*RequestObject, string) (*do.StockDto, error)
 	GetWebshopSellableStockByItemUUID(int, int, int, int) (float64, error)
 	GetReservedStockbyItem(*RequestObject, string) (*do.StockDto, error)
-	PutStockForItemUUIDandStoraplace(*PutRequestObject, backend.AppBrandClient) (*do.StockDto, error)
+	PutStockForItemUUIDandStoraplace(*PutRequestObject, []*Storageplace, backend.AppBrandClient) (*do.StockDto, error)
 	GetItem(string, string, int) (do.ItemDto, error)
 	GetFreeStorageplaces(string, int) ([]do.StorageplaceDto, error)
 	GetStorageplacesWithItem(do.ItemDto, bool, string, int) ([]do.StorageplaceDto, error)
@@ -323,7 +323,7 @@ func updateItemStockTransactionQuantities(itemStockTransaction *do.ItemStockTran
 	}
 }
 
-func (db *DbHandler) PutStockForItemUUIDandStoraplace(putRequestObject *PutRequestObject, abc backend.AppBrandClient) (*do.StockDto, error) {
+func (db *DbHandler) PutStockForItemUUIDandStoraplace(putRequestObject *PutRequestObject, freeStorageplaces []*Storageplace, abc backend.AppBrandClient) (*do.StockDto, error) {
 
 	tx, err := Db.Beginx()
 	if err != nil {
@@ -347,6 +347,18 @@ func (db *DbHandler) PutStockForItemUUIDandStoraplace(putRequestObject *PutReque
 			log.Println(err)
 			return &do.StockDto{}, err
 		}
+		if freeStorageplaces != nil {
+			isRequestedStorageplaceFree := false
+			for _, freeStorageplace := range freeStorageplaces {
+				if freeStorageplace.Uuid == putRequestObject.Storageplaceuuid {
+					isRequestedStorageplaceFree = true
+					break
+				}
+			}
+			if !isRequestedStorageplaceFree {
+				storageplacePk, _ = backend.GetPkFromUuid(freeStorageplaces[0].Uuid)
+			}
+		}
 	} else {
 		storageplacePk, err = db.GetStorageplacePkByNameAndClient(abc.Client, putRequestObject.Storagename)
 		if err != nil {
diff --git a/stockconnector/DbMock.go b/stockconnector/DbMock.go
index e857517..a264d10 100644
--- a/stockconnector/DbMock.go
+++ b/stockconnector/DbMock.go
@@ -98,7 +98,7 @@ func (db *MockDatabaseHandler) GetReservedStockbyItem(requestObject *RequestObje
 	}
 	return &do.StockDto{}, nil
 }
-func (db *MockDatabaseHandler) PutStockForItemUUIDandStoraplace(requestObject *PutRequestObject, abc backend.AppBrandClient) (*do.StockDto, error) {
+func (db *MockDatabaseHandler) PutStockForItemUUIDandStoraplace(requestObject *PutRequestObject, storageplaces []*Storageplace, abc backend.AppBrandClient) (*do.StockDto, error) {
 	args := db.Called(requestObject, abc)
 	if args != nil {
 		if val, ok := args.Get(0).(do.StockDto); ok {
-- 
GitLab