Skip to content

Commit

Permalink
fix "public" and "archived" filter and query
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Vitanov committed Jul 26, 2024
1 parent 75dfda1 commit 9570a84
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 74 deletions.
2 changes: 1 addition & 1 deletion core/app_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion core/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion core/app_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/interfaces/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/interfaces/driven.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 21 additions & 15 deletions driven/storage/adapter_surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 6 additions & 11 deletions driver/web/apis_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 6 additions & 41 deletions driver/web/apis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 9570a84

Please sign in to comment.