From 94660ea87e9c8c256383ad570552f107308f30ce Mon Sep 17 00:00:00 2001 From: "Gomez Goiri, Aitor" <aitor.gomez@tecnalia.com> Date: Fri, 25 Feb 2022 11:26:38 +0100 Subject: [PATCH] Forzing units to mass (kg or t) --- chaincode/controller/operations.go | 10 +++++---- chaincode/middleware/units.go | 35 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 chaincode/middleware/units.go diff --git a/chaincode/controller/operations.go b/chaincode/controller/operations.go index 3a956b8..f983790 100644 --- a/chaincode/controller/operations.go +++ b/chaincode/controller/operations.go @@ -71,12 +71,13 @@ func ContextOperations(m shared.AbstractChaincodeOperationManager) error { "asset-register", shared.WRITE_OP, []shared.MiddlewareInterface{ + m2.ValidateUnits, + m2.ValidateStatus, + m2.ValidateType, middleware.InjectAssetIdIfNone, middleware.RejectIfAssetExists, middleware.MarkAssetAsCreated, middleware.InjectAssetOwnerData, - m2.ValidateStatus, - m2.ValidateType, }, nil, assetController.SaveAbstractAsset, @@ -99,11 +100,12 @@ func ContextOperations(m shared.AbstractChaincodeOperationManager) error { "asset-update", shared.WRITE_OP, []shared.MiddlewareInterface{ + m2.ValidateUnits, + m2.ValidateOptionalStatus, + m2.ValidateOptionalType, middleware.RejectIfNoId, middleware.RejectIfAssetNotExist, middleware.MarkAssetAsModified, - m2.ValidateOptionalStatus, - m2.ValidateOptionalType, }, nil, assetController.OnlySameOrgAndRole(assetController.UpdateAsset), diff --git a/chaincode/middleware/units.go b/chaincode/middleware/units.go new file mode 100644 index 0000000..1c19d75 --- /dev/null +++ b/chaincode/middleware/units.go @@ -0,0 +1,35 @@ +/** + * type.go + * + * COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2022. + */ + +package middleware + +import ( + "errors" + "strings" + + "git.code.tecnalia.com/ledgerbuilder/sdk/shared" + "git.code.tecnalia.com/traceblock/sdk/middleware" +) + +var ( + ValidateUnits = shared.NewMiddlewareFunction("validate-units", _rejectIfInvalidUnits) + errInvalidUnits = errors.New("invalid units provided") +) + + +func _rejectIfInvalidUnits(stub shared.LedgerBuildrStubInterface, ctl shared.ControllerInterface, req shared.TXRequestInterface, requestAsset shared.LedgerBuildrAsset) (shared.LedgerBuildrAsset, error) { + asst, err := middleware.ConvertToTraceableAsset(requestAsset) + if err != nil { + return nil, err + } + + asst.Units = strings.ToLower(asst.Units) + if (asst.Units != "kg" && asst.Units != "t") { + return nil, errInvalidUnits + } + + return asst, nil +} \ No newline at end of file -- GitLab