@@ -15,6 +15,7 @@ import (
1515 "fmt"
1616 "net/http"
1717 "path/filepath"
18+ "reflect"
1819 "strconv"
1920 "strings"
2021 "time"
@@ -280,7 +281,21 @@ func UpdateObligation(c *gin.Context) {
280281 if err := db .DB .Transaction (func (tx * gorm.DB ) error {
281282 // https://gorm.io/docs/context.html#Context-in-Hooks-x2F-Callbacks
282283 ctx := context .WithValue (context .Background (), models .ContextKey ("oldObligation" ), & oldObligation )
283- if err := tx .WithContext (ctx ).Omit ("Licenses" , "Topic" ).Updates (& newObligation ).Error ; err != nil {
284+
285+ if err := tx .WithContext (ctx ).Omit ("Licenses" , "Topic" , "ExternalRef" ).Updates (& newObligation ).Error ; err != nil {
286+ return err
287+ }
288+
289+ // Overwrite values of existing keys, add new key value pairs and remove keys with null values.
290+ if err := tx .Debug ().Model (& models.Obligation {}).Where (& models.Obligation {Id : newObligation .Id }).UpdateColumn ("external_ref" , gorm .Expr ("jsonb_strip_nulls(COALESCE(external_ref, '{}'::jsonb) || ?)" , updates .ExternalRef )).Error ; err != nil {
291+ er := models.LicenseError {
292+ Status : http .StatusInternalServerError ,
293+ Message : "Failed to update license" ,
294+ Error : err .Error (),
295+ Path : c .Request .URL .Path ,
296+ Timestamp : time .Now ().Format (time .RFC3339 ),
297+ }
298+ c .JSON (http .StatusInternalServerError , er )
284299 return err
285300 }
286301
@@ -683,6 +698,32 @@ func addChangelogsForObligation(tx *gorm.DB, userId int64,
683698
684699 utils .AddChangelog ("Text Updatable" , oldObligation .TextUpdatable , newObligation .TextUpdatable , & changes )
685700
701+ oldLicenseExternalRef := oldObligation .ExternalRef .Data ()
702+ oldExternalRefVal := reflect .ValueOf (oldLicenseExternalRef )
703+ typesOf := oldExternalRefVal .Type ()
704+
705+ newLicenseExternalRef := newObligation .ExternalRef .Data ()
706+ newExternalRefVal := reflect .ValueOf (newLicenseExternalRef )
707+
708+ for i := 0 ; i < oldExternalRefVal .NumField (); i ++ {
709+ fieldName := typesOf .Field (i ).Name
710+
711+ switch typesOf .Field (i ).Type .String () {
712+ case "*boolean" :
713+ oldFieldPtr , _ := oldExternalRefVal .Field (i ).Interface ().(* bool )
714+ newFieldPtr , _ := newExternalRefVal .Field (i ).Interface ().(* bool )
715+ utils .AddChangelog (fmt .Sprintf ("External Reference %s" , fieldName ), oldFieldPtr , newFieldPtr , & changes )
716+ case "*string" :
717+ oldFieldPtr , _ := oldExternalRefVal .Field (i ).Interface ().(* string )
718+ newFieldPtr , _ := newExternalRefVal .Field (i ).Interface ().(* string )
719+ utils .AddChangelog (fmt .Sprintf ("External Reference %s" , fieldName ), oldFieldPtr , newFieldPtr , & changes )
720+ case "*int" :
721+ oldFieldPtr , _ := oldExternalRefVal .Field (i ).Interface ().(* int )
722+ newFieldPtr , _ := newExternalRefVal .Field (i ).Interface ().(* int )
723+ utils .AddChangelog (fmt .Sprintf ("External Reference %s" , fieldName ), oldFieldPtr , newFieldPtr , & changes )
724+ }
725+ }
726+
686727 if len (changes ) != 0 {
687728 audit := models.Audit {
688729 UserId : userId ,
0 commit comments