From 00f15463311211e8c66830bd4b93541f53ddc40c Mon Sep 17 00:00:00 2001 From: stefanvitanov <75947010+stefanvit@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:07:21 +0300 Subject: [PATCH 1/5] [ID-43]Fix "start_date" and "end_date" (#44) * set the response to start_date and end_date to be UNIX timestamp * set the Changelog.md * set start_date as nullable --------- Co-authored-by: Stefan Vitanov --- CHANGELOG.md | 3 ++ core/model/surveys.go | 4 +-- driver/web/apis_client.go | 4 ++- driver/web/convertions_surveys.go | 32 +++++++++++++++++---- driver/web/docs/gen/def.yaml | 1 + driver/web/docs/schemas/surveys/Survey.yaml | 1 + 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05fd5a9..1e1dee6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Fix "start_date" and "end_date" [#43](https://github.com/rokwire/surveys-building-block/issues/43) + ## [1.5.0] - 2024-007-22 ### Added - Start/end date [#38](https://github.com/rokwire/surveys-building-block/issues/38) diff --git a/core/model/surveys.go b/core/model/surveys.go index 00d091a..a267eb7 100644 --- a/core/model/surveys.go +++ b/core/model/surveys.go @@ -63,7 +63,7 @@ type Survey struct { DateCreated time.Time `json:"date_created" bson:"date_created"` DateUpdated *time.Time `json:"date_updated" bson:"date_updated"` CalendarEventID string `json:"calendar_event_id" bson:"calendar_event_id"` - StartDate time.Time `json:"start_date" bson:"start_date"` + StartDate *time.Time `json:"start_date" bson:"start_date"` EndDate *time.Time `json:"end_date" bson:"end_date"` } @@ -193,7 +193,7 @@ type SurveyRequest struct { DateCreated time.Time `json:"date_created" bson:"date_created"` DateUpdated *time.Time `json:"date_updated" bson:"date_updated"` CalendarEventID string `json:"calendar_event_id" bson:"calendar_event_id"` - StartDate int64 `json:"start_date" bson:"start_date"` + StartDate *int64 `json:"start_date" bson:"start_date"` EndDate *int64 `json:"end_date" bson:"end_date"` } diff --git a/driver/web/apis_client.go b/driver/web/apis_client.go index f6788dd..1ae99b6 100644 --- a/driver/web/apis_client.go +++ b/driver/web/apis_client.go @@ -114,7 +114,9 @@ func (h ClientAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *toke return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) } - rdata, err := json.Marshal(resData) + surveys := surveysToSurveyRequests(resData) + + rdata, err := json.Marshal(surveys) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionMarshal, logutils.TypeResponseBody, nil, err, http.StatusInternalServerError, false) } diff --git a/driver/web/convertions_surveys.go b/driver/web/convertions_surveys.go index c98931e..0739e97 100644 --- a/driver/web/convertions_surveys.go +++ b/driver/web/convertions_surveys.go @@ -10,7 +10,11 @@ import ( func surveyRequestToSurvey(claims *tokenauth.Claims, item model.SurveyRequest) model.Survey { item.Type = "user" //start - startValueValue := time.Unix(int64(item.StartDate), 0) + var startValue *time.Time + if item.StartDate != nil { + startValueValue := time.Unix(int64(*item.StartDate), 0) + startValue = &startValueValue + } //end var endValue *time.Time if item.EndDate != nil { @@ -22,11 +26,15 @@ 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: startValueValue, EndDate: endValue} + ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue} } func surveyToSurveyRequest(item model.Survey) model.SurveyRequest { - startDateUnixTimestamp := item.StartDate.Unix() + var startDateUnixTimestamp int64 + if item.StartDate != nil { + startDateUnixTimestamp = item.StartDate.Unix() + } + var endDateUnixTimestamp int64 if item.EndDate != nil { endDateUnixTimestamp = item.EndDate.Unix() @@ -36,14 +44,26 @@ func surveyToSurveyRequest(item model.Survey) model.SurveyRequest { MoreInfo: item.MoreInfo, Data: item.Data, Scored: item.Scored, ResultRules: item.ResultRules, ResultJSON: item.ResultJSON, 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, + ResponseKeys: item.ResponseKeys, DateCreated: item.DateCreated, CalendarEventID: item.CalendarEventID, StartDate: &startDateUnixTimestamp, EndDate: &endDateUnixTimestamp} } +func surveysToSurveyRequests(items []model.Survey) []model.SurveyRequest { + list := make([]model.SurveyRequest, len(items)) + for index := range items { + list[index] = surveyToSurveyRequest(items[index]) + } + return list +} + func updateSurveyRequestToSurvey(claims *tokenauth.Claims, item model.SurveyRequest, id string) model.Survey { item.Type = "user" //start - startValueValue := time.Unix(int64(item.StartDate), 0) + var startValue *time.Time + if item.StartDate != nil { + startValueTime := time.Unix(int64(*item.StartDate), 0) + startValue = &startValueTime + } //end var endValue *time.Time if item.EndDate != nil { @@ -55,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: startValueValue, EndDate: endValue} + ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue} } func surveyTimeFilter(item *model.SurveyTimeFilterRequest) *model.SurveyTimeFilter { diff --git a/driver/web/docs/gen/def.yaml b/driver/web/docs/gen/def.yaml index 87d9a45..fb824b2 100644 --- a/driver/web/docs/gen/def.yaml +++ b/driver/web/docs/gen/def.yaml @@ -1468,6 +1468,7 @@ components: start_date: type: integer format: int64 + nullable: true description: UNIX timestamp end_date: type: integer diff --git a/driver/web/docs/schemas/surveys/Survey.yaml b/driver/web/docs/schemas/surveys/Survey.yaml index a326237..ad7ed2d 100644 --- a/driver/web/docs/schemas/surveys/Survey.yaml +++ b/driver/web/docs/schemas/surveys/Survey.yaml @@ -60,6 +60,7 @@ properties: start_date: type: integer format: int64 + nullable: true description: "UNIX timestamp" end_date: type: integer From dc54d4ef16e0eba74cf16e126b6d2e0ff0289602 Mon Sep 17 00:00:00 2001 From: stefanvitanov <75947010+stefanvit@users.noreply.github.com> Date: Tue, 23 Jul 2024 12:23:54 +0300 Subject: [PATCH 2/5] [ID-39]Add extras field to survey data (#45) * set the Changelog.md * add "extras" to the SurveyData doc * add "extras" to the model and set it as nullable --------- Co-authored-by: Stefan Vitanov --- CHANGELOG.md | 2 ++ core/model/surveys.go | 23 ++++++++++--------- driver/web/docs/gen/def.yaml | 3 +++ .../web/docs/schemas/surveys/SurveyData.yaml | 3 +++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e1dee6..406952a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Add extras field to survey data [#39](https://github.com/rokwire/surveys-building-block/issues/39) ### Fixed - Fix "start_date" and "end_date" [#43](https://github.com/rokwire/surveys-building-block/issues/43) diff --git a/core/model/surveys.go b/core/model/surveys.go index a267eb7..16511ec 100644 --- a/core/model/surveys.go +++ b/core/model/surveys.go @@ -92,17 +92,18 @@ type SurveyStats struct { // SurveyData is data stored for a Survey type SurveyData struct { - Section *string `json:"section,omitempty" bson:"section,omitempty"` - Sections []string `json:"sections,omitempty" bson:"sections,omitempty"` - AllowSkip bool `json:"allow_skip" bson:"allow_skip"` - Text string `json:"text" bson:"text"` - MoreInfo string `json:"more_info" bson:"more_info"` - DefaultFollowUpKey *string `json:"default_follow_up_key" bson:"default_follow_up_key"` - DefaultResponseRule *string `json:"default_response_rule" bson:"default_response_rule"` - FollowUpRule *string `json:"follow_up_rule" bson:"follow_up_rule"` - ScoreRule *string `json:"score_rule" bson:"score_rule"` - Replace bool `json:"replace" bson:"replace"` - Response interface{} `json:"response" bson:"response"` + Section *string `json:"section,omitempty" bson:"section,omitempty"` + Sections []string `json:"sections,omitempty" bson:"sections,omitempty"` + AllowSkip bool `json:"allow_skip" bson:"allow_skip"` + Text string `json:"text" bson:"text"` + MoreInfo string `json:"more_info" bson:"more_info"` + DefaultFollowUpKey *string `json:"default_follow_up_key" bson:"default_follow_up_key"` + DefaultResponseRule *string `json:"default_response_rule" bson:"default_response_rule"` + FollowUpRule *string `json:"follow_up_rule" bson:"follow_up_rule"` + ScoreRule *string `json:"score_rule" bson:"score_rule"` + Replace bool `json:"replace" bson:"replace"` + Response interface{} `json:"response" bson:"response"` + Extras *map[string]interface{} `json:"extras" bson:"extras"` Type string `json:"type" bson:"type"` diff --git a/driver/web/docs/gen/def.yaml b/driver/web/docs/gen/def.yaml index fb824b2..51d0bb5 100644 --- a/driver/web/docs/gen/def.yaml +++ b/driver/web/docs/gen/def.yaml @@ -1498,6 +1498,9 @@ components: type: boolean response: type: object + extras: + type: object + nullable: true type: type: string correct_answer: diff --git a/driver/web/docs/schemas/surveys/SurveyData.yaml b/driver/web/docs/schemas/surveys/SurveyData.yaml index f904639..2e44a5f 100644 --- a/driver/web/docs/schemas/surveys/SurveyData.yaml +++ b/driver/web/docs/schemas/surveys/SurveyData.yaml @@ -20,6 +20,9 @@ properties: type: boolean response: type: object + extras: + type: object + nullable: true type: type: string correct_answer: From 2b56931c59d1a1823be30390112f673cad9e0fba Mon Sep 17 00:00:00 2001 From: stefanvitanov <75947010+stefanvit@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:30:47 +0300 Subject: [PATCH 3/5] [ID-37]Public flag (#46) * 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 --- core/app_admin.go | 4 +-- core/app_client.go | 4 +-- core/app_shared.go | 4 +-- core/interfaces.go | 2 +- core/interfaces/core.go | 4 +-- core/interfaces/driven.go | 2 +- core/interfaces/mocks/Storage.go | 18 ++++++------- core/model/surveys.go | 2 ++ driven/storage/adapter_surveys.go | 7 ++++- driver/web/apis_admin.go | 20 +++++++++++++- driver/web/apis_client.go | 21 +++++++++++++-- driver/web/convertions_surveys.go | 6 ++--- driver/web/docs/gen/def.yaml | 26 +++++++++++++++++++ driver/web/docs/resources/admin/surveys.yaml | 15 +++++++++++ driver/web/docs/resources/client/surveys.yaml | 8 ++++++ driver/web/docs/schemas/surveys/Survey.yaml | 5 +++- 16 files changed, 121 insertions(+), 27 deletions(-) diff --git a/core/app_admin.go b/core/app_admin.go index daa39ed..74ef56c 100644 --- a/core/app_admin.go +++ b/core/app_admin.go @@ -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 diff --git a/core/app_client.go b/core/app_client.go index 283897f..ff62db3 100644 --- a/core/app_client.go +++ b/core/app_client.go @@ -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 diff --git a/core/app_shared.go b/core/app_shared.go index 58915c3..57beeb3 100644 --- a/core/app_shared.go +++ b/core/app_shared.go @@ -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) { diff --git a/core/interfaces.go b/core/interfaces.go index b5d54c0..b6c6c3f 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) ([]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 diff --git a/core/interfaces/core.go b/core/interfaces/core.go index b7bc8e4..ddb7f8d 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) ([]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 @@ -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 diff --git a/core/interfaces/driven.go b/core/interfaces/driven.go index 8922e88..c8a2a46 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) ([]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 diff --git a/core/interfaces/mocks/Storage.go b/core/interfaces/mocks/Storage.go index c1dc2c7..ff7082a 100644 --- a/core/interfaces/mocks/Storage.go +++ b/core/interfaces/mocks/Storage.go @@ -503,9 +503,9 @@ func (_m *Storage) GetSurveyResponses(orgID *string, appID *string, userID *stri return r0, r1 } -// GetSurveys provides a mock function with given fields: orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter -func (_m *Storage) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter) ([]model.Survey, error) { - ret := _m.Called(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter) +// GetSurveys provides a mock function with given fields: orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public +func (_m *Storage) 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) { + ret := _m.Called(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) if len(ret) == 0 { panic("no return value specified for GetSurveys") @@ -513,19 +513,19 @@ func (_m *Storage) GetSurveys(orgID string, appID string, creatorID *string, sur var r0 []model.Survey var r1 error - if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter) ([]model.Survey, error)); ok { - return rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter) + if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool) ([]model.Survey, error)); ok { + return rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) } - if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter) []model.Survey); ok { - r0 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter) + if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool) []model.Survey); ok { + r0 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]model.Survey) } } - if rf, ok := ret.Get(1).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter) error); ok { - r1 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter) + if rf, ok := ret.Get(1).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool) error); ok { + r1 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) } else { r1 = ret.Error(1) } diff --git a/core/model/surveys.go b/core/model/surveys.go index 16511ec..d163e47 100644 --- a/core/model/surveys.go +++ b/core/model/surveys.go @@ -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 @@ -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 diff --git a/driven/storage/adapter_surveys.go b/driven/storage/adapter_surveys.go index b1fb97b..353fde9 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) ([]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}, @@ -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)) @@ -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, }} diff --git a/driver/web/apis_admin.go b/driver/web/apis_admin.go index 2d7cadb..8bcee01 100644 --- a/driver/web/apis_admin.go +++ b/driver/web/apis_admin.go @@ -21,6 +21,7 @@ import ( "io/ioutil" "log" "net/http" + "sort" "strconv" "strings" "time" @@ -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) diff --git a/driver/web/apis_client.go b/driver/web/apis_client.go index 1ae99b6..3dd66dc 100644 --- a/driver/web/apis_client.go +++ b/driver/web/apis_client.go @@ -21,6 +21,7 @@ import ( "io/ioutil" "log" "net/http" + "sort" "strconv" "strings" "time" @@ -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) @@ -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) } diff --git a/driver/web/convertions_surveys.go b/driver/web/convertions_surveys.go index 0739e97..84f728e 100644 --- a/driver/web/convertions_surveys.go +++ b/driver/web/convertions_surveys.go @@ -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 { @@ -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 { @@ -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 { diff --git a/driver/web/docs/gen/def.yaml b/driver/web/docs/gen/def.yaml index 51d0bb5..3ea96af 100644 --- a/driver/web/docs/gen/def.yaml +++ b/driver/web/docs/gen/def.yaml @@ -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 @@ -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 @@ -1475,6 +1498,9 @@ components: format: int64 nullable: true description: UNIX timestamp + public: + type: boolean + nullable: true SurveyData: type: object properties: diff --git a/driver/web/docs/resources/admin/surveys.yaml b/driver/web/docs/resources/admin/surveys.yaml index fb68dfc..95f3090 100644 --- a/driver/web/docs/resources/admin/surveys.yaml +++ b/driver/web/docs/resources/admin/surveys.yaml @@ -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 diff --git a/driver/web/docs/resources/client/surveys.yaml b/driver/web/docs/resources/client/surveys.yaml index ce8075a..0ec236c 100644 --- a/driver/web/docs/resources/client/surveys.yaml +++ b/driver/web/docs/resources/client/surveys.yaml @@ -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 diff --git a/driver/web/docs/schemas/surveys/Survey.yaml b/driver/web/docs/schemas/surveys/Survey.yaml index ad7ed2d..c50670c 100644 --- a/driver/web/docs/schemas/surveys/Survey.yaml +++ b/driver/web/docs/schemas/surveys/Survey.yaml @@ -66,4 +66,7 @@ properties: type: integer format: int64 nullable: true - description: "UNIX timestamp" \ No newline at end of file + description: "UNIX timestamp" + public: + type: boolean + nullable: true \ No newline at end of file From c56970e07ee898f76898f9f9d9221618c59bcaf8 Mon Sep 17 00:00:00 2001 From: stefanvitanov <75947010+stefanvit@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:16:33 +0300 Subject: [PATCH 4/5] [ID-40]Archived flag (#47) * set the Changelog.md * fix the Changelog.md * set the docs * add archived in progress * set archived to the update filter * add archived to the storage filter * set the mocks --------- Co-authored-by: Stefan Vitanov --- CHANGELOG.md | 4 ++++ core/app_admin.go | 4 ++-- core/app_client.go | 4 ++-- core/app_shared.go | 4 ++-- core/interfaces.go | 2 +- core/interfaces/core.go | 4 ++-- core/interfaces/driven.go | 2 +- core/interfaces/mocks/Storage.go | 18 +++++++++--------- core/model/surveys.go | 2 ++ driven/storage/adapter_surveys.go | 7 ++++++- driver/web/apis_admin.go | 13 ++++++++++++- driver/web/apis_client.go | 16 ++++++++++++++-- driver/web/convertions_surveys.go | 8 +++++--- driver/web/docs/gen/def.yaml | 19 +++++++++++++++++++ driver/web/docs/resources/admin/surveys.yaml | 8 ++++++++ driver/web/docs/resources/client/surveys.yaml | 10 +++++++++- driver/web/docs/schemas/surveys/Survey.yaml | 5 ++++- 17 files changed, 102 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 406952a..c749a89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Archived flag [#40](https://github.com/rokwire/surveys-building-block/issues/40) +### Added +- Public flag [#37](https://github.com/rokwire/surveys-building-block/issues/37) +### Added - Add extras field to survey data [#39](https://github.com/rokwire/surveys-building-block/issues/39) ### Fixed - Fix "start_date" and "end_date" [#43](https://github.com/rokwire/surveys-building-block/issues/43) diff --git a/core/app_admin.go b/core/app_admin.go index 74ef56c..1b97152 100644 --- a/core/app_admin.go +++ b/core/app_admin.go @@ -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, public *bool) ([]model.Survey, error) { - return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) +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, error) { + return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } // GetAllSurveyResponses returns survey responses matching the provided query diff --git a/core/app_client.go b/core/app_client.go index ff62db3..51e4e50 100644 --- a/core/app_client.go +++ b/core/app_client.go @@ -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, public *bool) ([]model.Survey, error) { - return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) + limit *int, offset *int, filter *model.SurveyTimeFilter, public *bool, archived *bool) ([]model.Survey, error) { + return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } // CreateSurvey creates a new survey diff --git a/core/app_shared.go b/core/app_shared.go index 57beeb3..f6fc596 100644 --- a/core/app_shared.go +++ b/core/app_shared.go @@ -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, public *bool) ([]model.Survey, error) { - return a.app.storage.GetSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) +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, error) { + return a.app.storage.GetSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } func (a appShared) createSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error) { diff --git a/core/interfaces.go b/core/interfaces.go index b6c6c3f..159fd3f 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) ([]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, 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 ddb7f8d..f5d18c5 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) ([]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, 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) ([]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, 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 c8a2a46..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) ([]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/core/interfaces/mocks/Storage.go b/core/interfaces/mocks/Storage.go index ff7082a..266a3fe 100644 --- a/core/interfaces/mocks/Storage.go +++ b/core/interfaces/mocks/Storage.go @@ -503,9 +503,9 @@ func (_m *Storage) GetSurveyResponses(orgID *string, appID *string, userID *stri return r0, r1 } -// GetSurveys provides a mock function with given fields: orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public -func (_m *Storage) 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) { - ret := _m.Called(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) +// GetSurveys provides a mock function with given fields: orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived +func (_m *Storage) 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) { + ret := _m.Called(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) if len(ret) == 0 { panic("no return value specified for GetSurveys") @@ -513,19 +513,19 @@ func (_m *Storage) GetSurveys(orgID string, appID string, creatorID *string, sur var r0 []model.Survey var r1 error - if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool) ([]model.Survey, error)); ok { - return rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) + if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool, *bool) ([]model.Survey, error)); ok { + return rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } - if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool) []model.Survey); ok { - r0 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) + if rf, ok := ret.Get(0).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool, *bool) []model.Survey); ok { + r0 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]model.Survey) } } - if rf, ok := ret.Get(1).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool) error); ok { - r1 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public) + if rf, ok := ret.Get(1).(func(string, string, *string, []string, []string, string, *int, *int, *model.SurveyTimeFilter, *bool, *bool) error); ok { + r1 = rf(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter, public, archived) } else { r1 = ret.Error(1) } diff --git a/core/model/surveys.go b/core/model/surveys.go index d163e47..f73c3ff 100644 --- a/core/model/surveys.go +++ b/core/model/surveys.go @@ -66,6 +66,7 @@ type Survey struct { StartDate *time.Time `json:"start_date" bson:"start_date"` EndDate *time.Time `json:"end_date" bson:"end_date"` Public *bool `json:"public" bson:"public"` + Archived *bool `json:"archived" bson:"archived"` } // SurveyResponseAnonymous represents an anonymized survey response @@ -198,6 +199,7 @@ type SurveyRequest struct { StartDate *int64 `json:"start_date" bson:"start_date"` EndDate *int64 `json:"end_date" bson:"end_date"` Public *bool `json:"public" bson:"public"` + Archived *bool `json:"archived" bson:"archived"` } // SurveyTimeFilter wraps the time filter for surveys diff --git a/driven/storage/adapter_surveys.go b/driven/storage/adapter_surveys.go index 353fde9..ed104d2 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) ([]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}, @@ -74,6 +74,10 @@ func (a *Adapter) GetSurveys(orgID string, appID string, creatorID *string, surv filter = append(filter, bson.E{Key: "public", Value: public}) } + if archived != nil { + filter = append(filter, bson.E{Key: "archived", Value: archived}) + } + opts := options.Find() if limit != nil { opts.SetLimit(int64(*limit)) @@ -136,6 +140,7 @@ func (a *Adapter) UpdateSurvey(survey model.Survey, admin bool) error { "start_date": survey.StartDate, "end_date": survey.EndDate, "public": survey.Public, + "archived": survey.Archived, "date_updated": now, }} diff --git a/driver/web/apis_admin.go b/driver/web/apis_admin.go index 8bcee01..6ffcfa6 100644 --- a/driver/web/apis_admin.go +++ b/driver/web/apis_admin.go @@ -245,8 +245,19 @@ func (h AdminAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *token } public = &value } + archivedStr := r.URL.Query().Get("archived") - resData, err := h.app.Admin.GetSurveys(claims.OrgID, claims.AppID, nil, surveyIDs, surveyTypes, calendarEventID, &limit, &offset, filter, public) + 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 + } + + resData, err := h.app.Admin.GetSurveys(claims.OrgID, claims.AppID, nil, surveyIDs, surveyTypes, calendarEventID, &limit, &offset, filter, public, archived) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) } diff --git a/driver/web/apis_client.go b/driver/web/apis_client.go index 3dd66dc..a116b2b 100644 --- a/driver/web/apis_client.go +++ b/driver/web/apis_client.go @@ -121,8 +121,20 @@ func (h ClientAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *toke public = &value } + 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 + } + resData, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, nil, surveyIDs, surveyTypes, calendarEventID, - &limit, &offset, filter, public) + &limit, &offset, filter, public, archived) if err != nil { return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true) } @@ -494,7 +506,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, nil) + 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) } diff --git a/driver/web/convertions_surveys.go b/driver/web/convertions_surveys.go index 84f728e..e315062 100644 --- a/driver/web/convertions_surveys.go +++ b/driver/web/convertions_surveys.go @@ -26,7 +26,8 @@ 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, Public: item.Public} + ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue, + Public: item.Public, Archived: item.Archived} } func surveyToSurveyRequest(item model.Survey) model.SurveyRequest { @@ -45,7 +46,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, Public: item.Public} + EndDate: &endDateUnixTimestamp, Public: item.Public, Archived: item.Archived} } func surveysToSurveyRequests(items []model.Survey) []model.SurveyRequest { @@ -75,7 +76,8 @@ 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, Public: item.Public} + ResponseKeys: item.ResponseKeys, CalendarEventID: item.CalendarEventID, StartDate: startValue, EndDate: endValue, + Public: item.Public, Archived: item.Archived} } func surveyTimeFilter(item *model.SurveyTimeFilterRequest) *model.SurveyTimeFilter { diff --git a/driver/web/docs/gen/def.yaml b/driver/web/docs/gen/def.yaml index 3ea96af..12351e8 100644 --- a/driver/web/docs/gen/def.yaml +++ b/driver/web/docs/gen/def.yaml @@ -100,6 +100,14 @@ paths: explode: false schema: type: boolean + - name: archived + in: query + description: Shows if the survery is archived or not + required: false + style: simple + explode: false + schema: + type: boolean requestBody: description: Get survey time filter request body required: false @@ -959,6 +967,14 @@ paths: explode: false schema: type: boolean + - name: archived + in: query + description: Shows if the survery is archived or not + required: false + style: simple + explode: false + schema: + type: boolean requestBody: description: Get survey time filter request body required: false @@ -1501,6 +1517,9 @@ components: public: type: boolean nullable: true + archived: + type: boolean + nullable: true SurveyData: type: object properties: diff --git a/driver/web/docs/resources/admin/surveys.yaml b/driver/web/docs/resources/admin/surveys.yaml index 95f3090..c3d19aa 100644 --- a/driver/web/docs/resources/admin/surveys.yaml +++ b/driver/web/docs/resources/admin/surveys.yaml @@ -56,6 +56,14 @@ get: explode: false schema: type: boolean + - name: archived + in: query + description: Shows if the survery is archived or not + required: false + style: simple + explode: false + schema: + type: boolean requestBody: description: Get survey time filter request body required: false diff --git a/driver/web/docs/resources/client/surveys.yaml b/driver/web/docs/resources/client/surveys.yaml index 0ec236c..c1c8cb4 100644 --- a/driver/web/docs/resources/client/surveys.yaml +++ b/driver/web/docs/resources/client/surveys.yaml @@ -53,7 +53,15 @@ get: style: simple explode: false schema: - type: boolean + type: boolean + - name: archived + in: query + description: Shows if the survery is archived or not + required: false + style: simple + explode: false + schema: + type: boolean requestBody: description: Get survey time filter request body required: false diff --git a/driver/web/docs/schemas/surveys/Survey.yaml b/driver/web/docs/schemas/surveys/Survey.yaml index c50670c..e7f10a2 100644 --- a/driver/web/docs/schemas/surveys/Survey.yaml +++ b/driver/web/docs/schemas/surveys/Survey.yaml @@ -69,4 +69,7 @@ properties: description: "UNIX timestamp" public: type: boolean - nullable: true \ No newline at end of file + nullable: true + archived: + type: boolean + nullable: true \ No newline at end of file From 3b87c57664979cf47e1fcb3b14439e28224ea9f7 Mon Sep 17 00:00:00 2001 From: Stefan Vitanov Date: Tue, 23 Jul 2024 16:24:27 +0300 Subject: [PATCH 5/5] update version to 1.6.0 --- CHANGELOG.md | 1 + SECURITY.md | 4 ++-- driver/web/docs/gen/def.yaml | 2 +- driver/web/docs/index.yaml | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c749a89..c5d18a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [1.6.0] - 2024-007-23 ### Added - Archived flag [#40](https://github.com/rokwire/surveys-building-block/issues/40) ### Added diff --git a/SECURITY.md b/SECURITY.md index e64ab2a..ac399f1 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -6,8 +6,8 @@ Patches for **Surveys Building Block** in this repository will only be applied t | Version | Supported | | ------- | ------------------ | -| 1.5.0 | :white_check_mark: | -| < 1.5.0 | :x: | +| 1.6.0 | :white_check_mark: | +| < 1.6.0 | :x: | ## Reporting a Bug or Vulnerability diff --git a/driver/web/docs/gen/def.yaml b/driver/web/docs/gen/def.yaml index 12351e8..74b1f31 100644 --- a/driver/web/docs/gen/def.yaml +++ b/driver/web/docs/gen/def.yaml @@ -2,7 +2,7 @@ openapi: 3.0.3 info: title: Rokwire Surveys Building Block API description: Surveys Building Block API Documentation - version: 1.5.0 + version: 1.6.0 servers: - url: 'https://api.rokwire.illinois.edu/surveys' description: Production server diff --git a/driver/web/docs/index.yaml b/driver/web/docs/index.yaml index d0e8bc5..e30a2db 100644 --- a/driver/web/docs/index.yaml +++ b/driver/web/docs/index.yaml @@ -2,7 +2,7 @@ openapi: 3.0.3 info: title: Rokwire Surveys Building Block API description: Surveys Building Block API Documentation - version: 1.5.0 + version: 1.6.0 servers: - url: 'https://api.rokwire.illinois.edu/surveys' description: Production server