Skip to content

Commit 2becf74

Browse files
committed
refactor
1 parent 1949e9d commit 2becf74

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

pkg/attributes/UserAttributesService.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"github.com/devtron-labs/devtron/internal/sql/repository"
23+
"github.com/devtron-labs/devtron/pkg/attributes/adapter"
2324
"github.com/devtron-labs/devtron/pkg/attributes/bean"
2425
"github.com/go-pg/pg"
2526
"go.uber.org/zap"
@@ -118,22 +119,22 @@ func (impl UserAttributesServiceImpl) PatchUserAttributes(request *UserAttribute
118119

119120
existingData, err := impl.parseJSONValue(existingAttribute.Value, "existing")
120121
if err != nil {
122+
impl.logger.Errorw("error in parsing json", "existingAttribute.Value", existingAttribute.Value, "error", err)
121123
return nil, err
122124
}
123125

124126
newData, err := impl.parseJSONValue(request.Value, "request")
125127
if err != nil {
128+
impl.logger.Errorw("error in parsing request json", "request.Value", request.Value, "error", err)
126129
return nil, err
127130
}
128131

129-
// Merge the data
130132
hasChanges := impl.mergeUserAttributesData(existingData, newData)
131133
if !hasChanges {
132134
impl.logger.Infow("no changes detected, skipping update", "key", request.Key)
133135
return existingAttribute, nil
134136
}
135137

136-
// Update in database and return result
137138
return impl.updateAttributeInDatabase(request, existingData)
138139
}
139140

@@ -156,14 +157,12 @@ func (impl UserAttributesServiceImpl) parseJSONValue(jsonValue, context string)
156157

157158
// updateAttributeInDatabase updates the merged data in the database
158159
func (impl UserAttributesServiceImpl) updateAttributeInDatabase(request *UserAttributesDto, mergedData map[string]interface{}) (*UserAttributesDto, error) {
159-
// Convert merged data back to JSON
160160
mergedJSON, err := json.Marshal(mergedData)
161161
if err != nil {
162162
impl.logger.Errorw("error converting merged data to JSON", "data", mergedData, "error", err)
163163
return nil, errors.New("error occurred while processing user attributes")
164164
}
165165

166-
// Create DAO for database update
167166
dao := &repository.UserAttributesDao{
168167
EmailId: request.EmailId,
169168
Key: request.Key,
@@ -179,17 +178,7 @@ func (impl UserAttributesServiceImpl) updateAttributeInDatabase(request *UserAtt
179178
}
180179

181180
// Build and return response
182-
return impl.buildResponseDTO(request, string(mergedJSON)), nil
183-
}
184-
185-
// buildResponseDTO creates the response DTO
186-
func (impl UserAttributesServiceImpl) buildResponseDTO(request *UserAttributesDto, mergedValue string) *UserAttributesDto {
187-
return &UserAttributesDto{
188-
EmailId: request.EmailId,
189-
Key: request.Key,
190-
Value: mergedValue,
191-
UserId: request.UserId,
192-
}
181+
return adapter.BuildResponseDTO(request, string(mergedJSON)), nil
193182
}
194183

195184
// mergeUserAttributesData merges newData into existingData with special handling for resources
@@ -264,19 +253,6 @@ func (impl UserAttributesServiceImpl) mergeResourceTypes(existingResources, newR
264253
return hasChanges
265254
}
266255

267-
// initializeSupportedResourceTypes ensures all supported resource types are initialized
268-
func (impl UserAttributesServiceImpl) initializeSupportedResourceTypes(existingResources map[string]interface{}) {
269-
supportedResourceTypes := []string{"cluster", "job", "app-group", "application/devtron-application"}
270-
271-
for _, resourceType := range supportedResourceTypes {
272-
if existingResources[resourceType] == nil {
273-
existingResources[resourceType] = map[string]interface{}{
274-
"recently-visited": []interface{}{},
275-
}
276-
}
277-
}
278-
}
279-
280256
func (impl UserAttributesServiceImpl) GetUserAttribute(request *UserAttributesDto) (*UserAttributesDto, error) {
281257

282258
dao := &repository.UserAttributesDao{

pkg/attributes/adapter/adapter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package adapter
2+
3+
import "github.com/devtron-labs/devtron/pkg/attributes"
4+
5+
func BuildResponseDTO(request *attributes.UserAttributesDto, mergedValue string) *attributes.UserAttributesDto {
6+
return &attributes.UserAttributesDto{
7+
EmailId: request.EmailId,
8+
Key: request.Key,
9+
Value: mergedValue,
10+
UserId: request.UserId,
11+
}
12+
}

0 commit comments

Comments
 (0)