This commit is contained in:
2023-03-14 14:45:51 +03:00
parent 42800ac4fe
commit f3a1a4c83f
202 changed files with 6199 additions and 155 deletions

View File

@@ -1,7 +1,7 @@
// API Actor API for managing account
package account
import "github.com/rudecs/decort-sdk/interfaces"
import "repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces"
// Structure for creating request to account
type Account struct {

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for adding permission to access to account for a user

View File

@@ -0,0 +1,86 @@
package account
// FilterByID returns ListAccounts with specified ID.
func (la ListAccounts) FilterByID(id uint64) ListAccounts {
predicate := func(ia ItemAccount) bool {
return ia.ID == id
}
return la.FilterFunc(predicate)
}
// FilterByName returns ListAccounts with specified Name.
func (la ListAccounts) FilterByName(name string) ListAccounts {
predicate := func(ia ItemAccount) bool {
return ia.Name == name
}
return la.FilterFunc(predicate)
}
// FilterByStatus returns ListAccounts with specified Status.
func (la ListAccounts) FilterByStatus(status string) ListAccounts {
predicate := func(ia ItemAccount) bool {
return ia.Status == status
}
return la.FilterFunc(predicate)
}
// FilterByUserGroupID returns ListAccounts with specified UserGroupID.
func (la ListAccounts) FilterByUserGroupID(userGroupID string) ListAccounts {
predicate := func(ia ItemAccount) bool {
acl := ia.ACL
for _, item := range acl {
if item.UserGroupID == userGroupID {
return true
}
}
return false
}
return la.FilterFunc(predicate)
}
// FilterByCompany returns ListAccounts with specified Company.
func (la ListAccounts) FilterByCompany(company string) ListAccounts {
predicate := func(ia ItemAccount) bool {
return ia.Company == company
}
return la.FilterFunc(predicate)
}
// FilterByCreatedBy returns ListAccounts created by specified user.
func (la ListAccounts) FilterByCreatedBy(createdBy string) ListAccounts {
predicate := func(ia ItemAccount) bool {
return ia.CreatedBy == createdBy
}
return la.FilterFunc(predicate)
}
// FilterFunc allows filtering ListAccounts based on a user-specified predicate.
func (la ListAccounts) FilterFunc(predicate func(ItemAccount) bool) ListAccounts {
var result ListAccounts
for _, item := range la {
if predicate(item) {
result = append(result, item)
}
}
return result
}
// FindOne returns first found ItemAccount.
// If none was found, returns an empty struct.
func (la ListAccounts) FindOne() ItemAccount {
if len(la) == 0 {
return ItemAccount{}
}
return la[0]
}

View File

@@ -0,0 +1,155 @@
package account
import (
"testing"
)
var accounts = ListAccounts{
ItemAccount{
Meta: []interface{}{},
InfoAccount: InfoAccount{
ACL: []ACL{
{
Explicit: true,
GUID: "",
Right: "CXDRAU",
Status: "CONFIRMED",
Type: "U",
UserGroupID: "not_really_timofey_tkachev_1@decs3o",
},
},
CreatedTime: 1676878820,
DeletedTime: 0,
ID: 132847,
Name: "std_2",
Status: "CONFIRMED",
UpdatedTime: 1676645275,
},
},
ItemAccount{
Meta: []interface{}{},
InfoAccount: InfoAccount{
ACL: []ACL{
{
Explicit: true,
GUID: "",
Right: "CXDRAU",
Status: "CONFIRMED",
Type: "U",
UserGroupID: "timofey_tkachev_1@decs3o",
},
},
CreatedTime: 1676645275,
DeletedTime: 1677723401,
ID: 132846,
Name: "std",
Status: "DELETED",
UpdatedTime: 1676645275,
},
},
ItemAccount{
Meta: []interface{}{},
InfoAccount: InfoAccount{
ACL: []ACL{
{
Explicit: true,
GUID: "",
Right: "CXDRAU",
Status: "CONFIRMED",
Type: "U",
UserGroupID: "timofey_tkachev_1@decs3o",
},
{
Explicit: true,
GUID: "",
Right: "CXDRAU",
Status: "CONFIRMED",
Type: "U",
UserGroupID: "second_account@decs3o",
},
},
CreatedTime: 1676883850,
DeletedTime: 1676883899,
ID: 132848,
Name: "std_broker",
Status: "DELETED",
UpdatedTime: 1676878820,
},
},
}
func TestFilterByID(t *testing.T) {
actual := accounts.FilterByID(132846).FindOne()
if actual.ID != 132846 {
t.Fatal("actual: ", actual.ID, " > expected: 132846")
}
}
func TestFilterByUserGroupId(t *testing.T) {
actual := accounts.FilterByUserGroupID("second_account@decs3o").FindOne()
for _, item := range actual.ACL {
if item.UserGroupID == "second_account@decs3o" {
return
}
}
t.Fatal("second_account@decs3o has not been found. expected 1 found")
}
func TestFilterByName(t *testing.T) {
actual := accounts.FilterByName("std_broker").FindOne()
if actual.Name != "std_broker" {
t.Fatal("actual: ", actual.Name, " >> expected: std_broker")
}
}
func TestFilterByStatus(t *testing.T) {
actual := accounts.FilterByStatus("DELETED")
if len(actual) != 2 {
t.Fatal("Expected 2 elements in slice, found: ", len(actual))
}
for _, item := range actual {
if item.Status != "DELETED" {
t.Fatal("expected DELETED, found: ", item.Status)
}
}
}
func TestFilterFunc(t *testing.T) {
actual := accounts.FilterFunc(func(ia ItemAccount) bool {
return ia.DeletedTime == 0
})
for _, item := range actual {
if item.DeletedTime != 0 {
t.Fatal("Expected DeletedTime = 0, found: ", item.DeletedTime)
}
}
}
func TestSortingByCreatedTime(t *testing.T) {
actual := accounts.SortByCreatedTime(false)
if actual[0].Name != "std" {
t.Fatal("Expected account std as earliest, found: ", actual[0].Name)
}
actual = accounts.SortByCreatedTime(true)
if actual[0].Name != "std_broker" {
t.Fatal("Expected account std_broker as latest, found: ", actual[0].Name)
}
}
func TestFilterEmpty(t *testing.T) {
actual := accounts.FilterByID(0)
if len(actual) != 0 {
t.Fatal("Expected 0 found, actual: ", len(actual))
}
}

View File

@@ -0,0 +1,43 @@
package account
import (
"encoding/json"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/serialization"
)
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
//
// In order to serialize with indent make sure to follow these guidelines:
// - First argument -> prefix
// - Second argument -> indent
func (la ListAccounts) Serialize(params ...string) (serialization.Serialized, error) {
if len(la) == 0 {
return []byte{}, nil
}
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(la, prefix, indent)
}
return json.Marshal(la)
}
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
//
// In order to serialize with indent make sure to follow these guidelines:
// - First argument -> prefix
// - Second argument -> indent
func (ia ItemAccount) Serialize(params ...string) (serialization.Serialized, error) {
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(ia, prefix, indent)
}
return json.Marshal(ia)
}

View File

@@ -0,0 +1,60 @@
package account
import "sort"
// SortByCreatedTime sorts ListAccounts by the CreatedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (la ListAccounts) SortByCreatedTime(inverse bool) ListAccounts {
if len(la) < 2 {
return la
}
sort.Slice(la, func(i, j int) bool {
if inverse {
return la[i].CreatedTime > la[j].CreatedTime
}
return la[i].CreatedTime < la[j].CreatedTime
})
return la
}
// SortByUpdatedTime sorts ListAccounts by the UpdatedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (la ListAccounts) SortByUpdatedTime(inverse bool) ListAccounts {
if len(la) < 2 {
return la
}
sort.Slice(la, func(i, j int) bool {
if inverse {
return la[i].UpdatedTime > la[j].UpdatedTime
}
return la[i].UpdatedTime < la[j].UpdatedTime
})
return la
}
// SortByDeletedTime sorts ListAccounts by the DeletedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (la ListAccounts) SortByDeletedTime(inverse bool) ListAccounts {
if len(la) < 2 {
return la
}
sort.Slice(la, func(i, j int) bool {
if inverse {
return la[i].DeletedTime > la[j].DeletedTime
}
return la[i].DeletedTime < la[j].DeletedTime
})
return la
}

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for update resource types in account