-
Notifications
You must be signed in to change notification settings - Fork 12
/
auxiliary_keeper.go
68 lines (54 loc) · 3.2 KB
/
auxiliary_keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package revoke
import (
"context"
baseData "github.com/AssetMantle/schema/go/data/base"
"github.com/AssetMantle/schema/go/documents/base"
errorConstants "github.com/AssetMantle/schema/go/errors/constants"
baseIDs "github.com/AssetMantle/schema/go/ids/base"
baseLists "github.com/AssetMantle/schema/go/lists/base"
baseProperties "github.com/AssetMantle/schema/go/properties/base"
constantProperties "github.com/AssetMantle/schema/go/properties/constants"
baseQualified "github.com/AssetMantle/schema/go/qualified/base"
"github.com/AssetMantle/modules/helpers"
"github.com/AssetMantle/modules/x/maintainers/constants"
"github.com/AssetMantle/modules/x/maintainers/key"
"github.com/AssetMantle/modules/x/maintainers/mappable"
"github.com/AssetMantle/modules/x/maintainers/record"
)
type auxiliaryKeeper struct {
mapper helpers.Mapper
}
var _ helpers.AuxiliaryKeeper = (*auxiliaryKeeper)(nil)
func (auxiliaryKeeper auxiliaryKeeper) Help(context context.Context, request helpers.AuxiliaryRequest) (helpers.AuxiliaryResponse, error) {
auxiliaryRequest := auxiliaryRequestFromInterface(request)
maintainers := auxiliaryKeeper.mapper.NewCollection(context)
fromMaintainerID := baseIDs.NewMaintainerID(base.PrototypeMaintainer().GetClassificationID(),
baseQualified.NewImmutables(baseLists.NewPropertyList(
baseProperties.NewMetaProperty(constantProperties.MaintainedClassificationIDProperty.GetKey(), baseData.NewIDData(auxiliaryRequest.MaintainedClassificationID)),
baseProperties.NewMetaProperty(constantProperties.IdentityIDProperty.GetKey(), baseData.NewIDData(auxiliaryRequest.FromID)),
)))
if Mappable := maintainers.Fetch(key.NewKey(fromMaintainerID)).GetMappable(key.NewKey(fromMaintainerID)); Mappable == nil {
return nil, errorConstants.EntityNotFound.Wrapf("maintainer with ID %s not found", fromMaintainerID.AsString())
} else if !mappable.GetMaintainer(Mappable).IsPermitted(constants.CanRemoveMaintainerPermission) {
return nil, errorConstants.NotAuthorized.Wrapf("maintainer with ID %s is not authorized to remove maintainers", fromMaintainerID.AsString())
}
toMaintainerID := baseIDs.NewMaintainerID(base.PrototypeMaintainer().GetClassificationID(),
baseQualified.NewImmutables(baseLists.NewPropertyList(
baseProperties.NewMetaProperty(constantProperties.MaintainedClassificationIDProperty.GetKey(), baseData.NewIDData(auxiliaryRequest.MaintainedClassificationID)),
baseProperties.NewMetaProperty(constantProperties.IdentityIDProperty.GetKey(), baseData.NewIDData(auxiliaryRequest.ToID)),
)))
if Mappable := maintainers.Fetch(key.NewKey(toMaintainerID)).GetMappable(key.NewKey(toMaintainerID)); Mappable == nil {
return nil, errorConstants.EntityNotFound.Wrapf("maintainer with ID %s not found", toMaintainerID.AsString())
} else {
maintainers.Remove(record.NewRecord(mappable.GetMaintainer(Mappable)))
}
return newAuxiliaryResponse(), nil
}
func (auxiliaryKeeper) Initialize(mapper helpers.Mapper, _ helpers.ParameterManager, _ []interface{}) helpers.Keeper {
return auxiliaryKeeper{mapper: mapper}
}
func keeperPrototype() helpers.AuxiliaryKeeper {
return auxiliaryKeeper{}
}