Skip to content

Commit 24ff6f2

Browse files
stefanvitStefan Vitanov
andauthored
[ID-38]Start/end date (#41)
* set the Changelog.md * set start_date and end_date fields if they are missing * set the docs * remove unused code * make start_date and end_date int in the model * set start_date and end date * add start_date and end_date to updateSurvey * set the POST /surveys * fix PUT admin/surveys * time filter for survey * set request as not required * add filter for start_date and end_date filter for GET surveys * mockery * set request body as nullable * fix filter * fix app and org id filter --------- Co-authored-by: Stefan Vitanov <stefan@inabyte.com>
1 parent 3fa64f3 commit 24ff6f2

18 files changed

Lines changed: 345 additions & 63 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Start/end date [#38](https://github.com/rokwire/surveys-building-block/issues/38)
10+
811
## [1.4.0] - 2024-007-11
912
### Added
1013
- Remove user data [#34](https://github.com/rokwire/surveys-building-block/issues/34)

core/app_admin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func (a appAdmin) GetSurvey(id string, orgID string, appID string) (*model.Surve
3737
}
3838

3939
// GetSurvey returns surveys matching the provided query
40-
func (a appAdmin) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error) {
41-
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset)
40+
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) {
41+
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter)
4242
}
4343

4444
// GetAllSurveyResponses returns survey responses matching the provided query

core/app_client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ func (a appClient) GetSurvey(id string, orgID string, appID string) (*model.Surv
3535
}
3636

3737
// GetSurvey returns surveys matching the provided query
38-
func (a appClient) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error) {
39-
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset)
38+
func (a appClient) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string,
39+
limit *int, offset *int, filter *model.SurveyTimeFilter) ([]model.Survey, error) {
40+
return a.app.shared.getSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter)
4041
}
4142

4243
// CreateSurvey creates a new survey

core/app_shared.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func (a appShared) getSurvey(id string, orgID string, appID string) (*model.Surv
3434
return a.app.storage.GetSurvey(id, orgID, appID)
3535
}
3636

37-
func (a appShared) getSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error) {
38-
return a.app.storage.GetSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset)
37+
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) {
38+
return a.app.storage.GetSurveys(orgID, appID, creatorID, surveyIDs, surveyTypes, calendarEventID, limit, offset, filter)
3939
}
4040

4141
func (a appShared) createSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error) {

core/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import "application/core/model"
2020
type Shared interface {
2121
// Surveys
2222
getSurvey(id string, orgID string, appID string) (*model.Survey, error)
23-
getSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error)
23+
getSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter) ([]model.Survey, error)
2424
createSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error)
2525
updateSurvey(survey model.Survey, userID string, externalIDs map[string]string, admin bool) error
2626
deleteSurvey(id string, orgID string, appID string, userID string, externalIDs map[string]string, admin bool) error

core/interfaces/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Default interface {
3030
type Client interface {
3131
// Surveys
3232
GetSurvey(id string, orgID string, appID string) (*model.Survey, error)
33-
GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error)
33+
GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter) ([]model.Survey, error)
3434
CreateSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error)
3535
UpdateSurvey(survey model.Survey, userID string, externalIDs map[string]string) error
3636
DeleteSurvey(id string, orgID string, appID string, userID string, externalIDs map[string]string) error
@@ -59,7 +59,7 @@ type Admin interface {
5959

6060
// Surveys
6161
GetSurvey(id string, orgID string, appID string) (*model.Survey, error)
62-
GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error)
62+
GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter) ([]model.Survey, error)
6363
CreateSurvey(survey model.Survey, externalIDs map[string]string) (*model.Survey, error)
6464
UpdateSurvey(survey model.Survey, userID string, externalIDs map[string]string) error
6565
DeleteSurvey(id string, orgID string, appID string, userID string, externalIDs map[string]string) error

core/interfaces/driven.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Storage interface {
3333
DeleteConfig(id string) error
3434

3535
GetSurvey(id string, orgID string, appID string) (*model.Survey, error)
36-
GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error)
36+
GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int, filter *model.SurveyTimeFilter) ([]model.Survey, error)
3737
CreateSurvey(survey model.Survey) (*model.Survey, error)
3838
UpdateSurvey(survey model.Survey, admin bool) error
3939
DeleteSurvey(id string, orgID string, appID string, creatorID string, admin bool) error

core/interfaces/mocks/Storage.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/model/surveys.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type Survey struct {
6363
DateCreated time.Time `json:"date_created" bson:"date_created"`
6464
DateUpdated *time.Time `json:"date_updated" bson:"date_updated"`
6565
CalendarEventID string `json:"calendar_event_id" bson:"calendar_event_id"`
66+
StartDate time.Time `json:"start_date" bson:"start_date"`
67+
EndDate *time.Time `json:"end_date" bson:"end_date"`
6668
}
6769

6870
// SurveyResponseAnonymous represents an anonymized survey response
@@ -165,3 +167,48 @@ type DeletedMembership struct {
165167
AccountID string `json:"account_id"`
166168
Context *map[string]interface{} `json:"context,omitempty"`
167169
}
170+
171+
// SurveyRequest wraps the wraps the request for the surveys
172+
type SurveyRequest struct {
173+
ID string `json:"id" bson:"_id"`
174+
CreatorID string `json:"creator_id" bson:"creator_id"`
175+
OrgID string `json:"org_id" bson:"org_id"`
176+
AppID string `json:"app_id" bson:"app_id"`
177+
Title string `json:"title" bson:"title"`
178+
MoreInfo *string `json:"more_info" bson:"more_info"`
179+
Data map[string]SurveyData `json:"data" bson:"data"`
180+
Scored bool `json:"scored" bson:"scored"`
181+
ResultRules string `json:"result_rules" bson:"result_rules"`
182+
ResultJSON string `json:"result_json" bson:"result_json"`
183+
Type string `json:"type" bson:"type"`
184+
SurveyStats *SurveyStats `json:"stats" bson:"stats"`
185+
Sensitive bool `json:"sensitive" bson:"sensitive"`
186+
Anonymous bool `json:"anonymous" bson:"anonymous"`
187+
DefaultDataKey *string `json:"default_data_key" bson:"default_data_key"`
188+
DefaultDataKeyRule *string `json:"default_data_key_rule" bson:"default_data_key_rule"`
189+
Constants map[string]interface{} `json:"constants" bson:"constants"`
190+
Strings map[string]interface{} `json:"strings" bson:"strings"`
191+
SubRules map[string]interface{} `json:"sub_rules" bson:"sub_rules"`
192+
ResponseKeys []string `json:"response_keys" bson:"response_keys"`
193+
DateCreated time.Time `json:"date_created" bson:"date_created"`
194+
DateUpdated *time.Time `json:"date_updated" bson:"date_updated"`
195+
CalendarEventID string `json:"calendar_event_id" bson:"calendar_event_id"`
196+
StartDate int64 `json:"start_date" bson:"start_date"`
197+
EndDate *int64 `json:"end_date" bson:"end_date"`
198+
}
199+
200+
// SurveyTimeFilter wraps the time filter for surveys
201+
type SurveyTimeFilter struct {
202+
StartTimeAfter *time.Time `json:"start_time_after"`
203+
StartTimeBefore *time.Time `json:"start_time_before"`
204+
EndTimeAfter *time.Time `json:"end_time_after"`
205+
EndTimeBefore *time.Time `json:"end_time_before"`
206+
}
207+
208+
// SurveyTimeFilterRequest wraps the time filter for surveys
209+
type SurveyTimeFilterRequest struct {
210+
StartTimeAfter *int64 `json:"start_time_after"`
211+
StartTimeBefore *int64 `json:"start_time_before"`
212+
EndTimeAfter *int64 `json:"end_time_after"`
213+
EndTimeBefore *int64 `json:"end_time_before"`
214+
}

driven/storage/adapter_surveys.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/rokwire/logging-library-go/v2/errors"
2222
"github.com/rokwire/logging-library-go/v2/logutils"
2323
"go.mongodb.org/mongo-driver/bson"
24+
"go.mongodb.org/mongo-driver/bson/primitive"
2425
"go.mongodb.org/mongo-driver/mongo/options"
2526
)
2627

@@ -36,19 +37,37 @@ func (a *Adapter) GetSurvey(id string, orgID string, appID string) (*model.Surve
3637
}
3738

3839
// GetSurveys gets matching surveys
39-
func (a *Adapter) GetSurveys(orgID string, appID string, creatorID *string, surveyIDs []string, surveyTypes []string, calendarEventID string, limit *int, offset *int) ([]model.Survey, error) {
40-
filter := bson.M{"org_id": orgID, "app_id": appID}
40+
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) {
41+
filter := bson.D{
42+
{Key: "org_id", Value: orgID},
43+
{Key: "app_id", Value: appID},
44+
}
45+
4146
if creatorID != nil {
42-
filter["creator_id"] = *creatorID
47+
filter = append(filter, bson.E{Key: "creator_id", Value: *creatorID})
4348
}
4449
if len(surveyIDs) > 0 {
45-
filter["_id"] = bson.M{"$in": surveyIDs}
50+
filter = append(filter, bson.E{Key: "_id", Value: bson.M{"$in": surveyIDs}})
4651
}
4752
if len(surveyTypes) > 0 {
48-
filter["type"] = bson.M{"$in": surveyTypes}
53+
filter = append(filter, bson.E{Key: "type", Value: bson.M{"$in": surveyTypes}})
54+
}
55+
if calendarEventID != "" {
56+
filter = append(filter, bson.E{Key: "calendar_event_id", Value: calendarEventID})
57+
}
58+
59+
if timeFilter.StartTimeAfter != nil {
60+
filter = append(filter, primitive.E{Key: "start_date", Value: primitive.M{"$gte": *timeFilter.StartTimeAfter}})
61+
}
62+
if timeFilter.StartTimeBefore != nil {
63+
filter = append(filter, primitive.E{Key: "start_date", Value: primitive.M{"$lte": *timeFilter.StartTimeBefore}})
64+
}
65+
66+
if timeFilter.EndTimeAfter != nil {
67+
filter = append(filter, primitive.E{Key: "end_date", Value: primitive.M{"$gte": *timeFilter.EndTimeAfter}})
4968
}
50-
if len(calendarEventID) > 0 {
51-
filter["calendar_event_id"] = calendarEventID
69+
if timeFilter.EndTimeBefore != nil {
70+
filter = append(filter, primitive.E{Key: "end_date", Value: primitive.M{"$lte": *timeFilter.EndTimeBefore}})
5271
}
5372

5473
opts := options.Find()
@@ -58,10 +77,23 @@ func (a *Adapter) GetSurveys(orgID string, appID string, creatorID *string, surv
5877
if offset != nil {
5978
opts.SetSkip(int64(*offset))
6079
}
80+
if timeFilter.StartTimeBefore != nil {
81+
opts.SetSort(bson.D{{Key: "start_date", Value: -1}})
82+
} else if timeFilter.StartTimeAfter != nil {
83+
opts.SetSort(bson.D{{Key: "start_date", Value: 1}})
84+
}
85+
86+
if timeFilter.EndTimeBefore != nil {
87+
opts.SetSort(bson.D{{Key: "end_date", Value: -1}})
88+
} else if timeFilter.EndTimeAfter != nil {
89+
opts.SetSort(bson.D{{Key: "end_date", Value: 1}})
90+
91+
}
92+
6193
var results []model.Survey
6294
err := a.db.surveys.Find(a.context, filter, &results, opts)
6395
if err != nil {
64-
return nil, errors.WrapErrorAction(logutils.ActionFind, model.TypeSurvey, filterArgs(filter), err)
96+
return nil, err
6597
}
6698
return results, nil
6799
}
@@ -97,6 +129,8 @@ func (a *Adapter) UpdateSurvey(survey model.Survey, admin bool) error {
97129
"constants": survey.Constants,
98130
"strings": survey.Strings,
99131
"sub_rules": survey.SubRules,
132+
"start_date": survey.StartDate,
133+
"end_date": survey.EndDate,
100134
"date_updated": now,
101135
}}
102136

0 commit comments

Comments
 (0)