|
17 | 17 | package attributes |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "encoding/json" |
20 | 21 | "errors" |
21 | 22 | "github.com/devtron-labs/devtron/internal/sql/repository" |
22 | 23 | "github.com/go-pg/pg" |
23 | 24 | "go.uber.org/zap" |
| 25 | + "reflect" |
24 | 26 | ) |
25 | 27 |
|
26 | 28 | type UserAttributesService interface { |
27 | 29 | AddUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error) |
28 | 30 | UpdateUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error) |
| 31 | + PatchUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error) |
29 | 32 | GetUserAttribute(request *UserAttributesDto) (*UserAttributesDto, error) |
30 | 33 | } |
31 | 34 |
|
@@ -95,6 +98,95 @@ func (impl UserAttributesServiceImpl) UpdateUserAttributes(request *UserAttribut |
95 | 98 | return request, nil |
96 | 99 | } |
97 | 100 |
|
| 101 | +func (impl UserAttributesServiceImpl) PatchUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error) { |
| 102 | + userAttribute, err := impl.GetUserAttribute(request) |
| 103 | + if err != nil { |
| 104 | + impl.logger.Errorw("error while getting user attributes during patch request", "req", request, "error", err) |
| 105 | + return nil, errors.New("error occurred while updating user attributes") |
| 106 | + } |
| 107 | + if userAttribute == nil { |
| 108 | + impl.logger.Info("no data found for request, so going to add instead of update", "req", request) |
| 109 | + attributes, err := impl.AddUserAttributes(request) |
| 110 | + if err != nil { |
| 111 | + impl.logger.Errorw("error in adding new user attributes", "req", request, "error", err) |
| 112 | + return nil, errors.New("error occurred while updating user attributes") |
| 113 | + } |
| 114 | + return attributes, nil |
| 115 | + } |
| 116 | + |
| 117 | + // Parse existing JSON |
| 118 | + var existingData map[string]interface{} |
| 119 | + if userAttribute.Value != "" { |
| 120 | + err = json.Unmarshal([]byte(userAttribute.Value), &existingData) |
| 121 | + if err != nil { |
| 122 | + impl.logger.Errorw("error parsing existing json value", "value", userAttribute.Value, "error", err) |
| 123 | + return nil, errors.New("error occurred while updating user attributes") |
| 124 | + } |
| 125 | + } else { |
| 126 | + existingData = make(map[string]interface{}) |
| 127 | + } |
| 128 | + |
| 129 | + // Parse new JSON |
| 130 | + var newData map[string]interface{} |
| 131 | + if request.Value != "" { |
| 132 | + err = json.Unmarshal([]byte(request.Value), &newData) |
| 133 | + if err != nil { |
| 134 | + impl.logger.Errorw("error parsing request json value", "value", request.Value, "error", err) |
| 135 | + return nil, errors.New("error occurred while updating user attributes") |
| 136 | + } |
| 137 | + } else { |
| 138 | + newData = make(map[string]interface{}) |
| 139 | + } |
| 140 | + |
| 141 | + // Check if there are any changes |
| 142 | + anyChanges := false |
| 143 | + |
| 144 | + // Merge the objects (patch style) |
| 145 | + for key, newValue := range newData { |
| 146 | + existingValue, exists := existingData[key] |
| 147 | + if !exists || !reflect.DeepEqual(existingValue, newValue) { |
| 148 | + existingData[key] = newValue |
| 149 | + anyChanges = true |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // If no changes, return the existing data |
| 154 | + if !anyChanges { |
| 155 | + impl.logger.Infow("no change detected, skipping update", "key", request.Key) |
| 156 | + return userAttribute, nil |
| 157 | + } |
| 158 | + |
| 159 | + // Convert back to JSON string |
| 160 | + mergedJson, err := json.Marshal(existingData) |
| 161 | + if err != nil { |
| 162 | + impl.logger.Errorw("error converting merged data to json", "data", existingData, "error", err) |
| 163 | + return nil, errors.New("error occurred while updating user attributes") |
| 164 | + } |
| 165 | + |
| 166 | + dao := &repository.UserAttributesDao{ |
| 167 | + EmailId: request.EmailId, |
| 168 | + Key: request.Key, |
| 169 | + Value: string(mergedJson), |
| 170 | + UserId: request.UserId, |
| 171 | + } |
| 172 | + |
| 173 | + err = impl.attributesRepository.UpdateDataValByKey(dao) |
| 174 | + if err != nil { |
| 175 | + impl.logger.Errorw("error in update attributes", "req", dao, "error", err) |
| 176 | + return nil, errors.New("error occurred while updating user attributes") |
| 177 | + } |
| 178 | + |
| 179 | + // Return the updated data |
| 180 | + result := &UserAttributesDto{ |
| 181 | + EmailId: request.EmailId, |
| 182 | + Key: request.Key, |
| 183 | + Value: string(mergedJson), |
| 184 | + UserId: request.UserId, |
| 185 | + } |
| 186 | + |
| 187 | + return result, nil |
| 188 | +} |
| 189 | + |
98 | 190 | func (impl UserAttributesServiceImpl) GetUserAttribute(request *UserAttributesDto) (*UserAttributesDto, error) { |
99 | 191 |
|
100 | 192 | dao := &repository.UserAttributesDao{ |
|
0 commit comments