Skip to content

Commit

Permalink
[ID-37]Public flag (#46)
Browse files Browse the repository at this point in the history
* add public flag to the docs

* add public to the model and the conversion

* set the public field in the update filter and in the update conversion

* add public to the query

* fix

* set public

* add response to the admin get surveys API

* set the mocks

---------

Co-authored-by: Stefan Vitanov <[email protected]>
  • Loading branch information
stefanvit and Stefan Vitanov authored Jul 23, 2024
1 parent dc54d4e commit 2b56931
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 27 deletions.
4 changes: 2 additions & 2 deletions core/app_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ 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) ([]model.Survey, error) {
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter)
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) ([]model.Survey, error) {
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public)
}

// GetAllSurveyResponses returns survey responses matching the provided query
Expand Down
4 changes: 2 additions & 2 deletions core/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ 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) ([]model.Survey, error) {
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter)
limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool) ([]model.Survey, error) {
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public)
}

// CreateSurvey creates a new survey
Expand Down
4 changes: 2 additions & 2 deletions core/app_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ 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) ([]model.Survey, error) {
return a.app.storage.GetSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter)
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) ([]model.Survey, error) {
return a.app.storage.GetSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public)
}

func (a appShared) createSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error) {
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) ([]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) ([]model.Survey, 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) ([]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) ([]model.Survey, 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) ([]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) ([]model.Survey, 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) ([]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) ([]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
18 changes: 9 additions & 9 deletions core/interfaces/mocks/Storage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/model/surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Survey struct {
CalendarEventID string `json:"calendar_event_id" bson:"calendar_event_id"`
StartDate *time.Time `json:"start_date" bson:"start_date"`
EndDate *time.Time `json:"end_date" bson:"end_date"`
Public *bool `json:"public" bson:"public"`
}

// SurveyResponseAnonymous represents an anonymized survey response
Expand Down Expand Up @@ -196,6 +197,7 @@ type SurveyRequest struct {
CalendarEventID string `json:"calendar_event_id" bson:"calendar_event_id"`
StartDate *int64 `json:"start_date" bson:"start_date"`
EndDate *int64 `json:"end_date" bson:"end_date"`
Public *bool `json:"public" bson:"public"`
}

// SurveyTimeFilter wraps the time filter for surveys
Expand Down
7 changes: 6 additions & 1 deletion 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) ([]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) ([]model.Survey, error) {
filter := bson.D{
{Key: "org_id", Value: orgID},
{Key: "app_id", Value: appID},
Expand Down Expand Up @@ -70,6 +70,10 @@ 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 != nil {
filter = append(filter, bson.E{Key: "public", Value: public})
}

opts := options.Find()
if limit != nil {
opts.SetLimit(int64(*limit))
Expand Down Expand Up @@ -131,6 +135,7 @@ func (a *Adapter) UpdateSurvey(survey model.Survey, admin bool) error {
"sub_rules": survey.SubRules,
"start_date": survey.StartDate,
"end_date": survey.EndDate,
"public": survey.Public,
"date_updated": now,
}}

Expand Down
20 changes: 19 additions & 1 deletion driver/web/apis_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"log"
"net/http"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -233,12 +234,29 @@ func (h AdminAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *token
}
offset = intParsed
}
publicStr := r.URL.Query().Get("public")

resData, err := h.app.Admin.GetSurveys(claims.OrgID, claims.AppID, nil, surveyIDs, surveyTypes, calendarEventID, &limit, &offset, filter)
var public *bool

if publicStr != "" {
value, err := strconv.ParseBool(publicStr)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}
public = &value
}

resData, err := h.app.Admin.GetSurveys(claims.OrgID, claims.AppID, nil, surveyIDs, surveyTypes, calendarEventID, &limit, &offset, filter, public)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}

surveys := surveysToSurveyRequests(resData)

sort.Slice(surveys, func(i, j int) bool {
return surveys[i].DateCreated.After(surveys[j].DateCreated)
})

rdata, err := json.Marshal(resData)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionMarshal, logutils.TypeResponseBody, nil, err, http.StatusInternalServerError, false)
Expand Down
21 changes: 19 additions & 2 deletions driver/web/apis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"log"
"net/http"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -108,14 +109,30 @@ func (h ClientAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *toke
offset = intParsed
}

publicStr := r.URL.Query().Get("public")

var public *bool

if publicStr != "" {
value, err := strconv.ParseBool(publicStr)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}
public = &value
}

resData, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, nil, surveyIDs, surveyTypes, calendarEventID,
&limit, &offset, filter)
&limit, &offset, filter, public)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}

surveys := surveysToSurveyRequests(resData)

sort.Slice(surveys, func(i, j int) bool {
return surveys[i].DateCreated.After(surveys[j].DateCreated)
})

rdata, err := json.Marshal(surveys)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionMarshal, logutils.TypeResponseBody, nil, err, http.StatusInternalServerError, false)
Expand Down Expand Up @@ -477,7 +494,7 @@ func (h ClientAPIsHandler) getCreatorSurveys(l *logs.Log, r *http.Request, claim
offset = intParsed
}

resData, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, surveyIDs, surveyTypes, "", &limit, &offset, nil)
resData, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, surveyIDs, surveyTypes, "", &limit, &offset, nil, nil)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}
Expand Down
6 changes: 3 additions & 3 deletions driver/web/convertions_surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func surveyRequestToSurvey(claims *tokenauth.Claims, item model.SurveyRequest) m
MoreInfo: item.MoreInfo, Data: item.Data, Scored: item.Scored, ResultRules: item.ResultRules, ResultJSON: item.ResultJSON,
SurveyStats: item.SurveyStats, Sensitive: item.Sensitive, Anonymous: item.Anonymous, DefaultDataKey: item.DefaultDataKey,
DefaultDataKeyRule: item.DefaultDataKeyRule, Constants: item.Constants, Strings: item.Strings, SubRules: item.SubRules,
ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue}
ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue, Public: item.Public}
}

func surveyToSurveyRequest(item model.Survey) model.SurveyRequest {
Expand All @@ -45,7 +45,7 @@ func surveyToSurveyRequest(item model.Survey) model.SurveyRequest {
Type: item.Type, SurveyStats: item.SurveyStats, Sensitive: item.Sensitive, Anonymous: item.Anonymous, DefaultDataKey: item.DefaultDataKey,
DefaultDataKeyRule: item.DefaultDataKeyRule, Constants: item.Constants, Strings: item.Strings, SubRules: item.SubRules,
ResponseKeys: item.ResponseKeys, DateCreated: item.DateCreated, CalendarEventID: item.CalendarEventID, StartDate: &startDateUnixTimestamp,
EndDate: &endDateUnixTimestamp}
EndDate: &endDateUnixTimestamp, Public: item.Public}
}

func surveysToSurveyRequests(items []model.Survey) []model.SurveyRequest {
Expand Down Expand Up @@ -75,7 +75,7 @@ func updateSurveyRequestToSurvey(claims *tokenauth.Claims, item model.SurveyRequ
MoreInfo: item.MoreInfo, Data: item.Data, Scored: item.Scored, ResultRules: item.ResultRules, ResultJSON: item.ResultJSON,
SurveyStats: item.SurveyStats, Sensitive: item.Sensitive, Anonymous: item.Anonymous, DefaultDataKey: item.DefaultDataKey,
DefaultDataKeyRule: item.DefaultDataKeyRule, Constants: item.Constants, Strings: item.Strings, SubRules: item.SubRules,
ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue}
ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue, Public: item.Public}
}

func surveyTimeFilter(item *model.SurveyTimeFilterRequest) *model.SurveyTimeFilter {
Expand Down
26 changes: 26 additions & 0 deletions driver/web/docs/gen/def.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ paths:
explode: false
schema:
type: number
- name: public
in: query
description: Shows if the survery is public or not
required: false
style: simple
explode: false
schema:
type: boolean
requestBody:
description: Get survey time filter request body
required: false
Expand Down Expand Up @@ -943,6 +951,21 @@ paths:
explode: false
schema:
type: number
- name: public
in: query
description: Shows if the survery is public or not
required: false
style: simple
explode: false
schema:
type: boolean
requestBody:
description: Get survey time filter request body
required: false
content:
application/json:
schema:
$ref: '#/paths/~1api~1surveys/get/requestBody/content/application~1json/schema'
responses:
'200':
description: Success
Expand Down Expand Up @@ -1475,6 +1498,9 @@ components:
format: int64
nullable: true
description: UNIX timestamp
public:
type: boolean
nullable: true
SurveyData:
type: object
properties:
Expand Down
15 changes: 15 additions & 0 deletions driver/web/docs/resources/admin/surveys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ get:
explode: false
schema:
type: number
- name: public
in: query
description: Shows if the survery is public or not
required: false
style: simple
explode: false
schema:
type: boolean
requestBody:
description: Get survey time filter request body
required: false
content:
application/json:
schema:
$ref: "../../schemas/surveys/SurveyTimeFilter.yaml"
responses:
200:
description: Success
Expand Down
8 changes: 8 additions & 0 deletions driver/web/docs/resources/client/surveys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ get:
explode: false
schema:
type: number
- name: public
in: query
description: Shows if the survery is public or not
required: false
style: simple
explode: false
schema:
type: boolean
requestBody:
description: Get survey time filter request body
required: false
Expand Down
5 changes: 4 additions & 1 deletion driver/web/docs/schemas/surveys/Survey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ properties:
type: integer
format: int64
nullable: true
description: "UNIX timestamp"
description: "UNIX timestamp"
public:
type: boolean
nullable: true

0 comments on commit 2b56931

Please sign in to comment.