diff --git a/migrations/back_37/back_37.go b/migrations/back_37/back_37.go index 650bbaec3..cc65cc128 100644 --- a/migrations/back_37/back_37.go +++ b/migrations/back_37/back_37.go @@ -121,6 +121,23 @@ func createDatumHash(bsonData bson.M) (string, error) { return deduplicator.GenerateIdentityHash(identityFields) } +func updateIfExistsPumpSettingsBolus(bsonData bson.M) (interface{}, error) { + dataType, err := getValidatedString(bsonData, "type") + if err != nil { + return nil, err + } + if dataType == "pumpSettings" { + if bolus := bsonData["bolus"]; bolus != nil { + boluses, ok := bolus.(map[string]interface{}) + if !ok { + return nil, errors.Newf("pumpSettings.bolus is not the expected type %v", bolus) + } + return boluses, nil + } + } + return nil, nil +} + func getBGValuePlatformPrecision(mmolVal float64) float64 { if len(fmt.Sprintf("%v", mmolVal)) > 7 { mgdlVal := mmolVal * glucose.MmolLToMgdLConversionFactor @@ -233,17 +250,26 @@ func (m *Migration) migrateDocument(jfDatum bson.M) (bool, error) { return false, err } + updates := bson.M{} hash, err := createDatumHash(jfDatum) if err != nil { return false, err } - update := bson.M{ - "$set": bson.M{"_deduplicator": bson.M{"hash": hash}}, + + updates["_deduplicator"] = bson.M{"hash": hash} + + if boluses, err := updateIfExistsPumpSettingsBolus(jfDatum); err != nil { + return false, err + } else if boluses != nil { + updates["pumpSettings"] = bson.M{"boluses": boluses} } + result, err := m.dataRepository.UpdateOne(m.ctx, bson.M{ "_id": datumID, "modifiedTime": jfDatum["modifiedTime"], - }, update) + }, bson.M{ + "$set": updates, + }) if err != nil { return false, err diff --git a/migrations/back_37/back_37_test.go b/migrations/back_37/back_37_test.go index 6883beb23..3d3ccc63b 100644 --- a/migrations/back_37/back_37_test.go +++ b/migrations/back_37/back_37_test.go @@ -1,6 +1,13 @@ package main -import "testing" +import ( + "reflect" + "testing" + + "go.mongodb.org/mongo-driver/bson" + + pumpTest "github.com/tidepool-org/platform/data/types/settings/pump/test" +) func Test_getBGValuePlatformPrecision(t *testing.T) { @@ -38,3 +45,66 @@ func Test_getBGValuePlatformPrecision(t *testing.T) { }) } } + +func Test_updateIfExistsPumpSettingsBolus(t *testing.T) { + type args struct { + bsonData bson.M + } + + bolusData := map[string]interface{}{ + "bolous-1": pumpTest.NewBolus(), + "bolous-2": pumpTest.NewBolus(), + } + + tests := []struct { + name string + args args + want interface{} + wantErr bool + }{ + { + name: "when not pumpSettings", + args: args{ + bsonData: bson.M{"type": "other"}, + }, + want: nil, + wantErr: false, + }, + { + name: "pumpSettings but no bolus", + args: args{ + bsonData: bson.M{"type": "pumpSettings"}, + }, + want: nil, + wantErr: false, + }, + { + name: "pumpSettings bolus wrong type", + args: args{ + bsonData: bson.M{"type": "pumpSettings", "bolus": "wrong"}, + }, + want: nil, + wantErr: true, + }, + { + name: "pumpSettings bolus valid type", + args: args{ + bsonData: bson.M{"type": "pumpSettings", "bolus": bolusData}, + }, + want: bolusData, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := updateIfExistsPumpSettingsBolus(tt.args.bsonData) + if (err != nil) != tt.wantErr { + t.Errorf("updateIfExistsPumpSettingsBolus() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("updateIfExistsPumpSettingsBolus() = %v, want %v", got, tt.want) + } + }) + } +}