1515package domain
1616
1717import (
18+ "errors"
1819 "time"
1920
21+ "github.com/bucketeer-io/bucketeer/pkg/uuid"
2022 proto "github.com/bucketeer-io/bucketeer/proto/account"
2123 environmentproto "github.com/bucketeer-io/bucketeer/proto/environment"
2224)
2325
26+ var (
27+ errSearchFilterNotFound = errors .New ("account: search filter not found" )
28+ )
29+
2430type AccountV2 struct {
2531 * proto.AccountV2
2632}
@@ -46,6 +52,7 @@ func NewAccountV2(
4652 Disabled : false ,
4753 CreatedAt : now ,
4854 UpdatedAt : now ,
55+ SearchFilters : nil ,
4956 }}
5057}
5158
@@ -106,3 +113,51 @@ func (a *AccountV2) Disable() error {
106113 a .UpdatedAt = time .Now ().Unix ()
107114 return nil
108115}
116+
117+ func (a * AccountV2 ) AddSearchFilter (
118+ name string ,
119+ query string ,
120+ targetType proto.FilterTargetType ,
121+ environmentID string , defaultFilter bool ) error {
122+ id , err := uuid .NewUUID ()
123+ if err != nil {
124+ return err
125+ }
126+
127+ searchFilter := & proto.SearchFilter {
128+ Id : id .String (),
129+ Name : name ,
130+ Query : query ,
131+ FilterTargetType : targetType ,
132+ EnvironmentId : environmentID ,
133+ DefaultFilter : defaultFilter ,
134+ }
135+ a .AccountV2 .SearchFilters = append (a .AccountV2 .SearchFilters , searchFilter )
136+ a .UpdatedAt = time .Now ().Unix ()
137+ return nil
138+ }
139+
140+ func (a * AccountV2 ) DeleteSearchFilter (id string ) error {
141+ for i , f := range a .AccountV2 .SearchFilters {
142+ if f .Id == id {
143+ a .AccountV2 .SearchFilters = append (a .AccountV2 .SearchFilters [:i ], a .AccountV2 .SearchFilters [i + 1 :]... )
144+ if len (a .AccountV2 .SearchFilters ) == 0 {
145+ a .AccountV2 .SearchFilters = nil
146+ }
147+ a .UpdatedAt = time .Now ().Unix ()
148+ return nil
149+ }
150+ }
151+ return errSearchFilterNotFound
152+ }
153+
154+ func (a * AccountV2 ) UpdateSearchFilter (searchFilter * proto.SearchFilter ) error {
155+ for i , f := range a .AccountV2 .SearchFilters {
156+ if f .Id == searchFilter .Id {
157+ a .AccountV2 .SearchFilters [i ] = searchFilter
158+ a .UpdatedAt = time .Now ().Unix ()
159+ return nil
160+ }
161+ }
162+ return errSearchFilterNotFound
163+ }
0 commit comments