diff --git a/internal/cmd/start_api.go b/internal/cmd/start_api.go index 0e874ea..6e15e92 100644 --- a/internal/cmd/start_api.go +++ b/internal/cmd/start_api.go @@ -11,6 +11,7 @@ import ( v2HttpPaths "github.com/bb-consent/api/internal/http_path/v2" "github.com/bb-consent/api/internal/iam" "github.com/bb-consent/api/internal/middleware" + "github.com/bb-consent/api/internal/migrate" privacyDashboard "github.com/bb-consent/api/internal/privacy_dashboard" "github.com/bb-consent/api/internal/rbac" "github.com/bb-consent/api/internal/sms" @@ -95,6 +96,10 @@ func StartApiCmdHandler(cmd *cobra.Command, args []string) { tenant.SingleTenantConfiguration(loadedConfig) } + // Applying migration + log.Println("Applying migrate") + migrate.Migrate() + // Router router := mux.NewRouter() v2HttpPaths.SetRoutes(router, authEnforcer) diff --git a/internal/migrate/migrate.go b/internal/migrate/migrate.go new file mode 100644 index 0000000..662d9b5 --- /dev/null +++ b/internal/migrate/migrate.go @@ -0,0 +1,68 @@ +package migrate + +import ( + "context" + "fmt" + + "github.com/bb-consent/api/internal/dataagreement" + "github.com/bb-consent/api/internal/policy" + "go.mongodb.org/mongo-driver/bson" +) + +func Migrate() { + migrateThirdPartyDataSharingToTrueInPolicyCollection() + migrateThirdPartyDataSharingToTrueInDataAgreementsCollection() +} + +func migrateThirdPartyDataSharingToTrueInPolicyCollection() { + policyCollection := policy.Collection() + + filter := bson.M{"thirdpartydatasharing": ""} + update := bson.M{"$set": bson.M{"thirdpartydatasharing": true}} + _, err := policyCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } + filter = bson.M{"thirdpartydatasharing": "true"} + update = bson.M{"$set": bson.M{"thirdpartydatasharing": true}} + _, err = policyCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } + filter = bson.M{"thirdpartydatasharing": "false"} + update = bson.M{"$set": bson.M{"thirdpartydatasharing": true}} + _, err = policyCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } +} + +func migrateThirdPartyDataSharingToTrueInDataAgreementsCollection() { + dataAgreementCollection := dataagreement.Collection() + + filter := bson.M{"policy.thirdpartydatasharing": ""} + update := bson.M{"$set": bson.M{"policy.thirdpartydatasharing": true}} + + _, err := dataAgreementCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } + + filter = bson.M{"policy.thirdpartydatasharing": "true"} + update = bson.M{"$set": bson.M{"policy.thirdpartydatasharing": true}} + + _, err = dataAgreementCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } + + filter = bson.M{"policy.thirdpartydatasharing": "false"} + update = bson.M{"$set": bson.M{"policy.thirdpartydatasharing": true}} + + _, err = dataAgreementCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } + // TODO: Handle impact towards revisions + +}