From 9570a846258ff1bca03ab125bb81f500c399da8a Mon Sep 17 00:00:00 2001 From: Stefan Vitanov Date: Fri, 26 Jul 2024 09:22:49 +0300 Subject: [PATCH] fix "public" and "archived" filter and query --- core/app_admin.go | 2 +- core/app_client.go | 2 +- core/app_shared.go | 2 +- core/interfaces.go | 2 +- core/interfaces/core.go | 4 +-- core/interfaces/driven.go | 2 +- driven/storage/adapter_surveys.go | 36 +++++++++++++---------- driver/web/apis_admin.go | 17 ++++------- driver/web/apis_client.go | 47 ++++--------------------------- 9 files changed, 40 insertions(+), 74 deletions(-) diff --git a/core/app_admin.go b/core/app_admin.go index 5fa78a1..5f8f556 100644 --- a/core/app_admin.go +++ b/core/app_admin.go @@ -37,7 +37,7 @@ func (a appAdmin) GetSurvey(id string, orgID string, appID string) (*model.Surve } // GetSurvey returns surveys matching the provided query -func (a appAdmin) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, []model.SurveyResponse, error) { +func (a appAdmin) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, []model.SurveyResponse, error) { return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } diff --git a/core/app_client.go b/core/app_client.go index 8f2b7b4..d67bd55 100644 --- a/core/app_client.go +++ b/core/app_client.go @@ -36,7 +36,7 @@ func (a appClient) GetSurvey(id string, orgID string, appID string) (*model.Surv // GetSurvey returns surveys matching the provided query func (a appClient) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, - limit *int, offset *int, filter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, []model.SurveyResponse, error) { + limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, []model.SurveyResponse, error) { return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } diff --git a/core/app_shared.go b/core/app_shared.go index cadf9d8..83d7849 100644 --- a/core/app_shared.go +++ b/core/app_shared.go @@ -34,7 +34,7 @@ func (a appShared) getSurvey(id string, orgID string, appID string) (*model.Surv return a.app.storage.GetSurvey(id, orgID, appID) } -func (a appShared) getSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, []model.SurveyResponse, error) { +func (a appShared) getSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, []model.SurveyResponse, error) { surveys, err := a.app.storage.GetSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) if err != nil { return nil, nil, err diff --git a/core/interfaces.go b/core/interfaces.go index 86eec85..32b5c4e 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -20,7 +20,7 @@ import "application/core/model" type Shared interface { // Surveys getSurvey(id string, orgID string, appID string) (*model.Survey, error) - getSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, []model.SurveyResponse, error) + getSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, []model.SurveyResponse, error) createSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error) updateSurvey(survey model.Survey, userID string, externalIDs map[string]string, admin bool) error deleteSurvey(id string, orgID string, appID string, userID string, externalIDs map[string]string, admin bool) error diff --git a/core/interfaces/core.go b/core/interfaces/core.go index 91b78c7..cbd4762 100644 --- a/core/interfaces/core.go +++ b/core/interfaces/core.go @@ -30,7 +30,7 @@ type Default interface { type Client interface { // Surveys GetSurvey(id string, orgID string, appID string) (*model.Survey, error) - GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, []model.SurveyResponse, error) + GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, []model.SurveyResponse, error) CreateSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error) UpdateSurvey(survey model.Survey, userID string, externalIDs map[string]string) error DeleteSurvey(id string, orgID string, appID string, userID string, externalIDs map[string]string) error @@ -59,7 +59,7 @@ type Admin interface { // Surveys GetSurvey(id string, orgID string, appID string) (*model.Survey, error) - GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, []model.SurveyResponse, error) + GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, []model.SurveyResponse, error) CreateSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error) UpdateSurvey(survey model.Survey, userID string, externalIDs map[string]string) error DeleteSurvey(id string, orgID string, appID string, userID string, externalIDs map[string]string) error diff --git a/core/interfaces/driven.go b/core/interfaces/driven.go index 21ae591..db88dc3 100644 --- a/core/interfaces/driven.go +++ b/core/interfaces/driven.go @@ -33,7 +33,7 @@ type Storage interface { DeleteConfig(id string) error GetSurvey(id string, orgID string, appID string) (*model.Survey, error) - GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, error) + GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, error) CreateSurvey(survey model.Survey) (*model.Survey, error) UpdateSurvey(survey model.Survey, admin bool) error DeleteSurvey(id string, orgID string, appID string, creatorID string, admin bool) error diff --git a/driven/storage/adapter_surveys.go b/driven/storage/adapter_surveys.go index c79d070..f9c636e 100644 --- a/driven/storage/adapter_surveys.go +++ b/driven/storage/adapter_surveys.go @@ -37,7 +37,7 @@ func (a *Adapter) GetSurvey(id string, orgID string, appID string) (*model.Surve } // GetSurveys gets matching surveys -func (a *Adapter) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, timeFilter *model.SurveyTimeFilter, public bool, archived bool) ([]model.Survey, error) { +func (a *Adapter) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, timeFilter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, error) { filter := bson.D{ {Key: "org_id", Value: orgID}, {Key: "app_id", Value: appID}, @@ -70,22 +70,28 @@ func (a *Adapter) GetSurveys(orgID string, appID string, creatorID *string, surv filter = append(filter, primitive.E{Key: "end_date", Value: primitive.M{"$lte": *timeFilter.EndTimeBefore}}) } - if public == true { - filter = append(filter, bson.E{Key: "public", Value: true}) - } else { - filter = append(filter, bson.E{Key: "$or", Value: bson.A{ - bson.M{"public": false}, - bson.M{"public": bson.M{"$exists": false}}, - }}) + if public != nil { + if *public == true { + filter = append(filter, bson.E{Key: "public", Value: true}) + } else { + filter = append(filter, bson.E{Key: "$or", Value: bson.A{ + bson.M{"public": false}, + bson.M{"public": bson.M{"$exists": false}}, + bson.M{"public": nil}, + }}) + } } - if archived == true { - filter = append(filter, bson.E{Key: "archived", Value: true}) - } else { - filter = append(filter, bson.E{Key: "$or", Value: bson.A{ - bson.M{"archived": false}, - bson.M{"archived": bson.M{"$exists": false}}, - }}) + if archived != nil { + if *archived == true { + filter = append(filter, bson.E{Key: "archived", Value: true}) + } else { + filter = append(filter, bson.E{Key: "$or", Value: bson.A{ + bson.M{"archived": false}, + bson.M{"archived": bson.M{"$exists": false}}, + bson.M{"archived": nil}, + }}) + } } opts := options.Find() diff --git a/driver/web/apis_admin.go b/driver/web/apis_admin.go index 35999c8..277b1ea 100644 --- a/driver/web/apis_admin.go +++ b/driver/web/apis_admin.go @@ -236,31 +236,26 @@ func (h AdminAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *token } publicStr := r.URL.Query().Get("public") - var public bool + var public *bool if publicStr != "" { - valueArchived, err := strconv.ParseBool(publicStr) + valuePublic, err := strconv.ParseBool(publicStr) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) } - public = valueArchived - } else { - valueArchived := false - public = valueArchived + public = &valuePublic } + archivedStr := r.URL.Query().Get("archived") - var archived bool + var archived *bool if archivedStr != "" { valueArchived, err := strconv.ParseBool(archivedStr) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) } - archived = valueArchived - } else { - valueArchived := false - archived = valueArchived + archived = &valueArchived } surveys, surverysRsponse, err := h.app.Admin.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, surveyIDs, surveyTypes, calendarEventID, diff --git a/driver/web/apis_client.go b/driver/web/apis_client.go index d69f7eb..dcb4510 100644 --- a/driver/web/apis_client.go +++ b/driver/web/apis_client.go @@ -111,32 +111,26 @@ func (h ClientAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *toke publicStr := r.URL.Query().Get("public") - var public bool + var public *bool if publicStr != "" { - valueArchived, err := strconv.ParseBool(publicStr) + valuePublic, err := strconv.ParseBool(publicStr) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) } - public = valueArchived - } else { - valueArchived := false - public = valueArchived + public = &valuePublic } archivedStr := r.URL.Query().Get("archived") - var archived bool + var archived *bool if archivedStr != "" { valueArchived, err := strconv.ParseBool(archivedStr) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) } - archived = valueArchived - } else { - valueArchived := false - archived = valueArchived + archived = &valueArchived } surveys, surverysRsponse, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, surveyIDs, surveyTypes, calendarEventID, @@ -516,37 +510,8 @@ func (h ClientAPIsHandler) getCreatorSurveys(l *logs.Log, r *http.Request, claim } offset = intParsed } - publicStr := r.URL.Query().Get("public") - - var public bool - - if publicStr != "" { - valueArchived, err := strconv.ParseBool(publicStr) - if err != nil { - return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) - } - public = valueArchived - } else { - valueArchived := false - public = valueArchived - } - - archivedStr := r.URL.Query().Get("archived") - - var archived bool - - if archivedStr != "" { - valueArchived, err := strconv.ParseBool(archivedStr) - if err != nil { - return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) - } - archived = valueArchived - } else { - valueArchived := false - archived = valueArchived - } - resData, _, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, surveyIDs, surveyTypes, "", &limit, &offset, nil, public, archived) + resData, _, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, surveyIDs, surveyTypes, "", &limit, &offset, nil, nil, nil) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) }