Skip to content

Commit

Permalink
merge develop into main (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvit authored Aug 21, 2024
2 parents 8e667d3 + 6df5bd3 commit 4e8df34
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 159 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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.10.3] - 2024-08-21
### Fixed
- Fix GET surveys time filtering [#75](https://github.com/rokwire/surveys-building-block/issues/75)

## [1.10.2] - 2024-08-06
### Fixed
- Use mongo db aggregation pipeline for get surveys and survey responses [#71](https://github.com/rokwire/surveys-building-block/issues/71)
Expand Down
4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Patches for **Surveys Building Block** in this repository will only be applied t

| Version | Supported |
| ------- | ------------------ |
| 1.10.2 | :white_check_mark: |
| < 1.10.2 | :x: |
| 1.10.3 | :white_check_mark: |
| < 1.10.3 | :x: |

## Reporting a Bug or Vulnerability

Expand Down
16 changes: 8 additions & 8 deletions core/model/surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,6 @@ type SurveyTimeFilter struct {
EndTimeBefore *time.Time `json:"end_time_before"`
}

// SurveyTimeFilterRequest wraps the time filter for surveys
type SurveyTimeFilterRequest struct {
StartTimeAfter *string `json:"start_time_after"`
StartTimeBefore *string `json:"start_time_before"`
EndTimeAfter *string `json:"end_time_after"`
EndTimeBefore *string `json:"end_time_before"`
}

// SurveysResponseData wraps the entire record
type SurveysResponseData struct {
ID string `json:"id"`
Expand Down Expand Up @@ -252,3 +244,11 @@ type SurveysResponseData struct {
EstimatedCompletionTime *int `json:"estimated_completion_time"`
Completed *bool `json:"completed"`
}

// SurveyTimeFilterRequest wraps the time filter for surveys
type SurveyTimeFilterRequest struct {
StartTimeAfter *string `json:"start_time_after"`
StartTimeBefore *string `json:"start_time_before"`
EndTimeAfter *string `json:"end_time_after"`
EndTimeBefore *string `json:"end_time_before"`
}
20 changes: 16 additions & 4 deletions driven/storage/adapter_surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,28 @@ func (a *Adapter) GetSurveysAndSurveyResponses(orgID string, appID string, creat
surveyFilter = append(surveyFilter, bson.E{Key: "calendar_event_id", Value: calendarEventID})
}
if timeFilter.StartTimeAfter != nil {
surveyFilter = append(surveyFilter, bson.E{Key: "start_date", Value: bson.M{"$gte": *timeFilter.StartTimeAfter}})
surveyFilter = append(surveyFilter, primitive.E{Key: "$or", Value: bson.A{
bson.M{"start_date": nil},
bson.M{"start_date": primitive.M{"$gte": *timeFilter.StartTimeAfter}},
}})
}
if timeFilter.StartTimeBefore != nil {
surveyFilter = append(surveyFilter, bson.E{Key: "start_date", Value: bson.M{"$lte": *timeFilter.StartTimeBefore}})
surveyFilter = append(surveyFilter, primitive.E{Key: "$or", Value: bson.A{
bson.M{"start_date": nil},
bson.M{"start_date": primitive.M{"$lte": *timeFilter.StartTimeBefore}},
}})
}
if timeFilter.EndTimeAfter != nil {
surveyFilter = append(surveyFilter, bson.E{Key: "end_date", Value: bson.M{"$gte": *timeFilter.EndTimeAfter}})
surveyFilter = append(surveyFilter, primitive.E{Key: "$or", Value: bson.A{
bson.M{"end_date": nil},
bson.M{"end_date": primitive.M{"$gte": *timeFilter.EndTimeAfter}},
}})
}
if timeFilter.EndTimeBefore != nil {
surveyFilter = append(surveyFilter, bson.E{Key: "end_date", Value: bson.M{"$lte": *timeFilter.EndTimeBefore}})
surveyFilter = append(surveyFilter, primitive.E{Key: "$or", Value: bson.A{
bson.M{"end_date": nil},
bson.M{"end_date": primitive.M{"$lte": *timeFilter.EndTimeBefore}},
}})
}

if public != nil {
Expand Down
40 changes: 19 additions & 21 deletions driver/web/apis_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"application/core"
"application/core/model"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -183,25 +181,6 @@ func (h AdminAPIsHandler) getSurvey(l *logs.Log, r *http.Request, claims *tokena
}

func (h AdminAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *tokenauth.Claims) logs.HTTPResponse {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionRead, logutils.TypeRequestBody, nil, err, http.StatusBadRequest, false)
}

var items *model.SurveyTimeFilterRequest
// If the body is empty or only contains whitespace, treat it as nil
if len(data) == 0 {
log.Println("Request body is empty, proceeding with default behavior.")
items = &model.SurveyTimeFilterRequest{StartTimeBefore: nil, StartTimeAfter: nil, EndTimeAfter: nil, EndTimeBefore: nil}
} else {
// Unmarshal the data into the items struct
err = json.Unmarshal(data, &items)
if err != nil {
log.Printf("Error unmarshaling request body: %v", err)
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}
}
filter := surveyTimeFilter(items)

surveyIDsRaw := r.URL.Query().Get("ids")
var surveyIDs []string
Expand Down Expand Up @@ -270,6 +249,25 @@ func (h AdminAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *token
completed = &valueCompleted
}

var timeFilterItems model.SurveyTimeFilterRequest
startsBeforeRaw := r.URL.Query().Get("starts_before")
if startsBeforeRaw != "" {
timeFilterItems.StartTimeBefore = &startsBeforeRaw
}
startsAfterRaw := r.URL.Query().Get("starts_after")
if startsAfterRaw != "" {
timeFilterItems.StartTimeAfter = &startsAfterRaw
}
endsBeforeRaw := r.URL.Query().Get("ends_before")
if endsBeforeRaw != "" {
timeFilterItems.EndTimeBefore = &endsBeforeRaw
}
endsAfterRaw := r.URL.Query().Get("ends_after")
if endsAfterRaw != "" {
timeFilterItems.EndTimeAfter = &endsAfterRaw
}
filter := surveyTimeFilter(&timeFilterItems)

surveys, surverysRsponse, err := h.app.Admin.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, nil, surveyIDs, surveyTypes, calendarEventID,
&limit, &offset, filter, public, archived, completed)
if err != nil {
Expand Down
96 changes: 75 additions & 21 deletions driver/web/apis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"application/core"
"application/core/model"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -57,25 +55,7 @@ func (h ClientAPIsHandler) getSurvey(l *logs.Log, r *http.Request, claims *token
}

func (h ClientAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *tokenauth.Claims) logs.HTTPResponse {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionRead, logutils.TypeRequestBody, nil, err, http.StatusBadRequest, false)
}

var items *model.SurveyTimeFilterRequest
// If the body is empty or only contains whitespace, treat it as nil
if len(data) == 0 {
log.Println("Request body is empty, proceeding with default behavior.")
items = &model.SurveyTimeFilterRequest{StartTimeBefore: nil, StartTimeAfter: nil, EndTimeAfter: nil, EndTimeBefore: nil}
} else {
// Unmarshal the data into the items struct
err = json.Unmarshal(data, &items)
if err != nil {
log.Printf("Error unmarshaling request body: %v", err)
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}
}
filter := surveyTimeFilter(items)
surveyIDsRaw := r.URL.Query().Get("ids")
var surveyIDs []string
if len(surveyIDsRaw) > 0 {
Expand Down Expand Up @@ -144,6 +124,25 @@ func (h ClientAPIsHandler) getSurveys(l *logs.Log, r *http.Request, claims *toke
completed = &valueCompleted
}

var timeFilterItems model.SurveyTimeFilterRequest
startsBeforeRaw := r.URL.Query().Get("starts_before")
if startsBeforeRaw != "" {
timeFilterItems.StartTimeBefore = &startsBeforeRaw
}
startsAfterRaw := r.URL.Query().Get("starts_after")
if startsAfterRaw != "" {
timeFilterItems.StartTimeAfter = &startsAfterRaw
}
endsBeforeRaw := r.URL.Query().Get("ends_before")
if endsBeforeRaw != "" {
timeFilterItems.EndTimeBefore = &endsBeforeRaw
}
endsAfterRaw := r.URL.Query().Get("ends_after")
if endsAfterRaw != "" {
timeFilterItems.EndTimeAfter = &endsAfterRaw
}
filter := surveyTimeFilter(&timeFilterItems)

surveys, surverysRsponse, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, nil, surveyIDs, surveyTypes, calendarEventID,
&limit, &offset, filter, public, archived, completed)
if err != nil {
Expand Down Expand Up @@ -520,7 +519,62 @@ 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, &claims.Subject, surveyIDs, surveyTypes, "", &limit, &offset, nil, nil, nil, nil)
publicStr := r.URL.Query().Get("public")

var public *bool

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

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
}

completedStr := r.URL.Query().Get("completed")

var completed *bool

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

var timeFilterItems model.SurveyTimeFilterRequest
startsBeforeRaw := r.URL.Query().Get("starts_before")
if startsBeforeRaw != "" {
timeFilterItems.StartTimeBefore = &startsBeforeRaw
}
startsAfterRaw := r.URL.Query().Get("starts_after")
if startsAfterRaw != "" {
timeFilterItems.StartTimeAfter = &startsAfterRaw
}
endsBeforeRaw := r.URL.Query().Get("ends_before")
if endsBeforeRaw != "" {
timeFilterItems.EndTimeBefore = &endsBeforeRaw
}
endsAfterRaw := r.URL.Query().Get("ends_after")
if endsAfterRaw != "" {
timeFilterItems.EndTimeAfter = &endsAfterRaw
}
filter := surveyTimeFilter(&timeFilterItems)

resData, _, err := h.app.Client.GetSurveys(claims.OrgID, claims.AppID, &claims.Subject, &claims.Subject, surveyIDs, surveyTypes, "", &limit, &offset, filter, public, archived, completed)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionGet, model.TypeSurvey, nil, err, http.StatusInternalServerError, true)
}
Expand Down
58 changes: 29 additions & 29 deletions driver/web/convertions_surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,6 @@ func updateSurveyRequestToSurvey(item model.SurveyRequest, id string) model.Surv
Public: item.Public, Archived: item.Archived, EstimatedCompletionTime: item.EstimatedCompletionTime}
}

func surveyTimeFilter(item *model.SurveyTimeFilterRequest) *model.SurveyTimeFilter {

filter := model.SurveyTimeFilter{}

if item.StartTimeBefore != nil {
beforeStartTime, _ := time.Parse(time.RFC3339, *item.StartTimeBefore)
filter.StartTimeBefore = &beforeStartTime
}
if item.StartTimeAfter != nil {
afterStartTime, _ := time.Parse(time.RFC3339, *item.StartTimeAfter)
filter.StartTimeAfter = &afterStartTime
}

if item.EndTimeBefore != nil {
beforeEndTime, _ := time.Parse(time.RFC3339, *item.EndTimeBefore)
filter.EndTimeBefore = &beforeEndTime
}
if item.EndTimeAfter != nil {
afterEndTime, _ := time.Parse(time.RFC3339, *item.EndTimeAfter)
filter.EndTimeAfter = &afterEndTime
}

return &model.SurveyTimeFilter{
StartTimeAfter: filter.StartTimeAfter,
StartTimeBefore: filter.StartTimeBefore,
EndTimeAfter: filter.EndTimeAfter,
EndTimeBefore: filter.EndTimeBefore}
}

func getSurveysResData(items []model.Survey, surveyResponses []model.SurveyResponse, completed *bool) []model.SurveysResponseData {
var list []model.SurveysResponseData

Expand Down Expand Up @@ -204,3 +175,32 @@ func sortIfpublicIsTrue(list []model.SurveysResponseData, public *bool) []model.

return result
}

func surveyTimeFilter(item *model.SurveyTimeFilterRequest) *model.SurveyTimeFilter {

filter := model.SurveyTimeFilter{}

if item.StartTimeBefore != nil {
beforeStartTime, _ := time.Parse(time.RFC3339, *item.StartTimeBefore)
filter.StartTimeBefore = &beforeStartTime
}
if item.StartTimeAfter != nil {
afterStartTime, _ := time.Parse(time.RFC3339, *item.StartTimeAfter)
filter.StartTimeAfter = &afterStartTime
}

if item.EndTimeBefore != nil {
beforeEndTime, _ := time.Parse(time.RFC3339, *item.EndTimeBefore)
filter.EndTimeBefore = &beforeEndTime
}
if item.EndTimeAfter != nil {
afterEndTime, _ := time.Parse(time.RFC3339, *item.EndTimeAfter)
filter.EndTimeAfter = &afterEndTime
}

return &model.SurveyTimeFilter{
StartTimeAfter: filter.StartTimeAfter,
StartTimeBefore: filter.StartTimeBefore,
EndTimeAfter: filter.EndTimeAfter,
EndTimeBefore: filter.EndTimeBefore}
}
Loading

0 comments on commit 4e8df34

Please sign in to comment.