@@ -3,16 +3,12 @@ package mongo
33import (
44 "context"
55 "fmt"
6- "time"
76
87 "go.mongodb.org/mongo-driver/bson"
98 "go.mongodb.org/mongo-driver/mongo"
109 "go.mongodb.org/mongo-driver/mongo/options"
1110
1211 "github.com/tidepool-org/platform/alerts"
13- "github.com/tidepool-org/platform/data/types/blood/glucose"
14- "github.com/tidepool-org/platform/data/types/blood/glucose/continuous"
15- "github.com/tidepool-org/platform/data/types/dosingdecision"
1612 "github.com/tidepool-org/platform/errors"
1713 structuredmongo "github.com/tidepool-org/platform/store/structured/mongo"
1814)
@@ -24,7 +20,7 @@ type alertsRepo structuredmongo.Repository
2420//
2521// Once set, UploadID, UserID, and FollowedUserID cannot be changed. This is to prevent a
2622// user from granting themselves access to another data set.
27- func (r * alertsRepo ) Upsert (ctx context.Context , conf * alerts.Config ) error {
23+ func (a * alertsRepo ) Upsert (ctx context.Context , conf * alerts.Config ) error {
2824 opts := options .Update ().SetUpsert (true )
2925 filter := bson.D {
3026 {Key : "userId" , Value : conf .UserID },
@@ -35,28 +31,28 @@ func (r *alertsRepo) Upsert(ctx context.Context, conf *alerts.Config) error {
3531 "$set" : bson.M {"alerts" : conf .Alerts , "activity" : conf .Activity },
3632 "$setOnInsert" : filter ,
3733 }
38- _ , err := r .UpdateOne (ctx , filter , doc , opts )
34+ _ , err := a .UpdateOne (ctx , filter , doc , opts )
3935 if err != nil {
4036 return fmt .Errorf ("upserting alerts.Config: %w" , err )
4137 }
4238 return nil
4339}
4440
4541// Delete will delete the given Config.
46- func (r * alertsRepo ) Delete (ctx context.Context , cfg * alerts.Config ) error {
47- _ , err := r .DeleteMany (ctx , r .filter (cfg ), nil )
42+ func (a * alertsRepo ) Delete (ctx context.Context , cfg * alerts.Config ) error {
43+ _ , err := a .DeleteMany (ctx , a .filter (cfg ), nil )
4844 if err != nil {
4945 return fmt .Errorf ("upserting alerts.Config: %w" , err )
5046 }
5147 return nil
5248}
5349
5450// List will retrieve any Configs that are defined by followers of the given user.
55- func (r * alertsRepo ) List (ctx context.Context , followedUserID string ) ([]* alerts.Config , error ) {
51+ func (a * alertsRepo ) List (ctx context.Context , followedUserID string ) ([]* alerts.Config , error ) {
5652 filter := bson.D {
5753 {Key : "followedUserId" , Value : followedUserID },
5854 }
59- cursor , err := r .Find (ctx , filter , nil )
55+ cursor , err := a .Find (ctx , filter , nil )
6056 if err != nil {
6157 return nil , errors .Wrapf (err , "Unable to list alerts.Config(s) for followed user %s" , followedUserID )
6258 }
@@ -72,8 +68,8 @@ func (r *alertsRepo) List(ctx context.Context, followedUserID string) ([]*alerts
7268}
7369
7470// Get will retrieve the given Config.
75- func (r * alertsRepo ) Get (ctx context.Context , cfg * alerts.Config ) (* alerts.Config , error ) {
76- res := r .FindOne (ctx , r .filter (cfg ), nil )
71+ func (a * alertsRepo ) Get (ctx context.Context , cfg * alerts.Config ) (* alerts.Config , error ) {
72+ res := a .FindOne (ctx , a .filter (cfg ), nil )
7773 if res .Err () != nil {
7874 return nil , fmt .Errorf ("getting alerts.Config: %w" , res .Err ())
7975 }
@@ -85,8 +81,8 @@ func (r *alertsRepo) Get(ctx context.Context, cfg *alerts.Config) (*alerts.Confi
8581}
8682
8783// EnsureIndexes to maintain index constraints.
88- func (r * alertsRepo ) EnsureIndexes () error {
89- repo := structuredmongo .Repository (* r )
84+ func (a * alertsRepo ) EnsureIndexes () error {
85+ repo := structuredmongo .Repository (* a )
9086 return (& repo ).CreateAllIndexes (context .Background (), []mongo.IndexModel {
9187 {
9288 Keys : bson.D {
@@ -100,61 +96,9 @@ func (r *alertsRepo) EnsureIndexes() error {
10096 })
10197}
10298
103- func (r * alertsRepo ) filter (cfg * alerts.Config ) interface {} {
99+ func (a * alertsRepo ) filter (cfg * alerts.Config ) interface {} {
104100 return bson.D {
105101 {Key : "userId" , Value : cfg .UserID },
106102 {Key : "followedUserId" , Value : cfg .FollowedUserID },
107103 }
108104}
109-
110- type alertsDataRepo structuredmongo.Repository
111-
112- func (d * alertsDataRepo ) GetAlertableData (ctx context.Context ,
113- params alerts.GetAlertableDataParams ) (* alerts.GetAlertableDataResponse , error ) {
114-
115- if params .End .IsZero () {
116- params .End = time .Now ()
117- }
118-
119- cursor , err := d .getAlertableData (ctx , params , dosingdecision .Type )
120- if err != nil {
121- return nil , err
122- }
123- dosingDecisions := []* dosingdecision.DosingDecision {}
124- if err := cursor .All (ctx , & dosingDecisions ); err != nil {
125- return nil , errors .Wrap (err , "Unable to load alertable dosing documents" )
126- }
127- cursor , err = d .getAlertableData (ctx , params , continuous .Type )
128- if err != nil {
129- return nil , err
130- }
131- glucoseData := []* glucose.Glucose {}
132- if err := cursor .All (ctx , & glucoseData ); err != nil {
133- return nil , errors .Wrap (err , "Unable to load alertable glucose documents" )
134- }
135- response := & alerts.GetAlertableDataResponse {
136- DosingDecisions : dosingDecisions ,
137- Glucose : glucoseData ,
138- }
139-
140- return response , nil
141- }
142-
143- func (d * alertsDataRepo ) getAlertableData (ctx context.Context ,
144- params alerts.GetAlertableDataParams , typ string ) (* mongo.Cursor , error ) {
145-
146- selector := bson.M {
147- "_active" : true ,
148- "uploadId" : params .UploadID ,
149- "type" : typ ,
150- "_userId" : params .UserID ,
151- "time" : bson.M {"$gte" : params .Start , "$lte" : params .End },
152- }
153- findOptions := options .Find ().SetSort (bson.D {{Key : "time" , Value : - 1 }})
154- cursor , err := d .Find (ctx , selector , findOptions )
155- if err != nil {
156- format := "Unable to find alertable %s data in dataset %s"
157- return nil , errors .Wrapf (err , format , typ , params .UploadID )
158- }
159- return cursor , nil
160- }
0 commit comments