Skip to content

Commit

Permalink
Add clientDeactivateThreshold in project
Browse files Browse the repository at this point in the history
Add clientDeactivateThreshold in Project to set client’s deactivation threshold in individual projects for housekeeping.
This will subtitute previousw DefaultHousekeepingDeactivateThreshold in global housekeeping configuration.
  • Loading branch information
krapie committed Feb 17, 2023
1 parent 3d9fe9d commit 858d58e
Show file tree
Hide file tree
Showing 28 changed files with 610 additions and 340 deletions.
21 changes: 13 additions & 8 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package converter

import (
"fmt"
gotime "time"

protoTypes "github.com/gogo/protobuf/types"

Expand Down Expand Up @@ -69,14 +70,15 @@ func FromProject(pbProject *api.Project) (*types.Project, error) {
return nil, fmt.Errorf("convert updatedAt to timestamp: %w", err)
}
return &types.Project{
ID: types.ID(pbProject.Id),
Name: pbProject.Name,
AuthWebhookURL: pbProject.AuthWebhookUrl,
AuthWebhookMethods: pbProject.AuthWebhookMethods,
PublicKey: pbProject.PublicKey,
SecretKey: pbProject.SecretKey,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
ID: types.ID(pbProject.Id),
Name: pbProject.Name,
AuthWebhookURL: pbProject.AuthWebhookUrl,
AuthWebhookMethods: pbProject.AuthWebhookMethods,
ClientDeactivateThreshold: gotime.Duration(pbProject.ClientDeactivateThreshold),
PublicKey: pbProject.PublicKey,
SecretKey: pbProject.SecretKey,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}, nil
}

Expand Down Expand Up @@ -663,6 +665,9 @@ func FromUpdatableProjectFields(pbProjectFields *api.UpdatableProjectFields) (*t
if pbProjectFields.AuthWebhookMethods != nil {
updatableProjectFields.AuthWebhookMethods = &pbProjectFields.AuthWebhookMethods.Methods
}
if pbProjectFields.ClientDeactivateThreshold != 0 {
updatableProjectFields.ClientDeactivateThreshold = gotime.Duration(pbProjectFields.ClientDeactivateThreshold)
}

return updatableProjectFields, nil
}
20 changes: 12 additions & 8 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ func ToProject(project *types.Project) (*api.Project, error) {
}

return &api.Project{
Id: project.ID.String(),
Name: project.Name,
AuthWebhookUrl: project.AuthWebhookURL,
AuthWebhookMethods: project.AuthWebhookMethods,
PublicKey: project.PublicKey,
SecretKey: project.SecretKey,
CreatedAt: pbCreatedAt,
UpdatedAt: pbUpdatedAt,
Id: project.ID.String(),
Name: project.Name,
AuthWebhookUrl: project.AuthWebhookURL,
AuthWebhookMethods: project.AuthWebhookMethods,
ClientDeactivateThreshold: int64(project.ClientDeactivateThreshold),
PublicKey: project.PublicKey,
SecretKey: project.SecretKey,
CreatedAt: pbCreatedAt,
UpdatedAt: pbUpdatedAt,
}, nil
}

Expand Down Expand Up @@ -514,5 +515,8 @@ func ToUpdatableProjectFields(fields *types.UpdatableProjectFields) (*api.Updata
} else {
pbUpdatableProjectFields.AuthWebhookMethods = nil
}
if fields.ClientDeactivateThreshold != 0 {
pbUpdatableProjectFields.ClientDeactivateThreshold = int64(fields.ClientDeactivateThreshold)
}
return pbUpdatableProjectFields, nil
}
4 changes: 4 additions & 0 deletions api/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type Project struct {
// AuthWebhookMethods is the methods that run the authorization webhook.
AuthWebhookMethods []string `json:"auth_webhook_methods"`

// ClientDeactivateThreshold is the time after which clients in
// specific project are considered deactivate for housekeeping.
ClientDeactivateThreshold time.Duration `bson:"client_deactivate_threshold"`

// PublicKey is the API key of this project.
PublicKey string `json:"public_key"`

Expand Down
6 changes: 5 additions & 1 deletion api/types/updatable_project_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package types

import (
"errors"
"time"

"github.com/go-playground/validator/v10"
)
Expand All @@ -36,11 +37,14 @@ type UpdatableProjectFields struct {

// AuthWebhookMethods is the methods that run the authorization webhook.
AuthWebhookMethods *[]string `bson:"auth_webhook_methods,omitempty" validate:"omitempty,invalid_webhook_method"`

// ClientDeactivateThreshold is the time after which clients in specific project are considered deactivate.
ClientDeactivateThreshold time.Duration `bson:"client_deactivate_threshold,omitempty" validate:"omitempty"`
}

// Validate validates the UpdatableProjectFields.
func (i *UpdatableProjectFields) Validate() error {
if i.Name == nil && i.AuthWebhookURL == nil && i.AuthWebhookMethods == nil {
if i.Name == nil && i.AuthWebhookURL == nil && i.AuthWebhookMethods == nil && i.ClientDeactivateThreshold == 0 {
return ErrEmptyProjectFields
}

Expand Down
21 changes: 13 additions & 8 deletions api/types/updatable_project_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package types_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand All @@ -33,10 +34,12 @@ func TestUpdatableProjectFields(t *testing.T) {
string(types.AttachDocument),
string(types.WatchDocuments),
}
newClientDeactivateThreshold := 1 * time.Hour
fields := &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
ClientDeactivateThreshold: newClientDeactivateThreshold,
}
assert.NoError(t, fields.Validate())

Expand All @@ -49,8 +52,9 @@ func TestUpdatableProjectFields(t *testing.T) {
assert.NoError(t, fields.Validate())

fields = &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
ClientDeactivateThreshold: newClientDeactivateThreshold,
}
assert.NoError(t, fields.Validate())

Expand All @@ -59,9 +63,10 @@ func TestUpdatableProjectFields(t *testing.T) {
"InvalidMethods",
}
fields = &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
ClientDeactivateThreshold: newClientDeactivateThreshold,
}
assert.ErrorAs(t, fields.Validate(), &invalidFieldsError)
})
Expand Down
Loading

0 comments on commit 858d58e

Please sign in to comment.