diff --git a/discovery/store.go b/discovery/store.go index 9e0b3780e..9936279f9 100644 --- a/discovery/store.go +++ b/discovery/store.go @@ -233,6 +233,9 @@ func (s *sqlStore) search(serviceID string, query map[string]string) ([]vc.Verif Where("service_id = ?", serviceID). Joins("inner join discovery_credential ON discovery_credential.presentation_id = discovery_presentation.id") stmt = store.CredentialStore{}.BuildSearchStatement(stmt, "discovery_credential.credential_id", query) + // prevent duplicates + // GROUP BY `discovery_presentation`.`service_id`, `discovery_presentation`.`credential_subject_id` + stmt = stmt.Group("discovery_presentation.service_id, discovery_presentation.credential_subject_id") var matches []presentationRecord if err := stmt.Preload("Credentials").Preload("Credentials.Credential").Find(&matches).Error; err != nil { diff --git a/discovery/store_test.go b/discovery/store_test.go index d675905a6..b782cb0a4 100644 --- a/discovery/store_test.go +++ b/discovery/store_test.go @@ -196,6 +196,18 @@ func Test_sqlStore_search(t *testing.T) { require.Len(t, actualVPs, 1) assert.Equal(t, vpAlice.ID.String(), actualVPs[0].ID.String()) }) + t.Run("find all", func(t *testing.T) { + vps := []vc.VerifiablePresentation{vpAlice, vpBob} + c := setupStore(t, storageEngine.GetSQLDatabase()) + for _, vp := range vps { + err := c.add(testServiceID, vp, 0) + require.NoError(t, err) + } + + actualVPs, err := c.search(testServiceID, map[string]string{}) + require.NoError(t, err) + require.Len(t, actualVPs, 2) + }) t.Run("not found", func(t *testing.T) { vps := []vc.VerifiablePresentation{vpAlice, vpBob} c := setupStore(t, storageEngine.GetSQLDatabase())