@@ -112,36 +112,52 @@ func isActivityAndActivityOnly(updatedFields []string) bool {
112112 return hasActivity
113113}
114114
115+ // deviceDataIdentifiers are common to DosingDecision and Glucose.
116+ //
117+ // They facilitate queries of the latest data for alerts evaluation.
118+ //
119+ // We're expecting either a [glucose.Glucose], or a [dosingdecision.DosingDecision] to be
120+ // present, but we don't know which it is, and it doesn't matter, because all we need are
121+ // the UserID and UploadID fields, which are present in both, via [types.Base].
122+ //
123+ // TODO: use types.Base instead
124+ type deviceDataIdentifiers struct {
125+ UploadID string `json:"uploadId,omitempty" bson:"uploadId,omitempty"`
126+ UserID string `json:"-" bson:"_userId,omitempty"`
127+ }
128+
115129func (c * Consumer ) consumeDeviceData (ctx context.Context ,
116130 session sarama.ConsumerGroupSession , msg * sarama.ConsumerMessage ) error {
117131
118- datum := & Glucose {}
119- if _ , err := unmarshalMessageValue (msg .Value , datum ); err != nil {
120- return err
121- }
122132 lgr := c .logger (ctx )
123- lgr .WithField ("data" , datum ).Info ("consuming a device data message" )
124133
125- if datum .UserID == nil {
126- return errors .New ("Unable to retrieve alerts configs: userID is nil" )
134+ id := & deviceDataIdentifiers {}
135+ if _ , err := unmarshalMessageValue (msg .Value , id ); err != nil {
136+ return errors .Wrap (err , "Unable to unmarshal device data message" )
137+ }
138+
139+ lgr .WithField ("identifiers" , id ).Info ("consuming a device data message" )
140+
141+ if id .UserID == "" {
142+ return errors .New ("Unable to retrieve alerts configs: userID is empty" )
127143 }
128- if datum .UploadID == nil {
129- return errors .New ("Unable to retrieve alerts configs: DataSetID is nil " )
144+ if id .UploadID == "" {
145+ return errors .New ("Unable to retrieve alerts configs: DataSetID is empty " )
130146 }
131- ctx = log .NewContextWithLogger (ctx , lgr .WithField ("followedUserID" , * datum .UserID ))
147+ ctx = log .NewContextWithLogger (ctx , lgr .WithField ("followedUserID" , id .UserID ))
132148 lastComm := alerts.LastCommunication {
133- UserID : * datum .UserID ,
149+ UserID : id .UserID ,
134150 LastReceivedDeviceData : time .Now (),
135- DataSetID : * datum .UploadID ,
151+ DataSetID : id .UploadID ,
136152 }
137153 err := c .LastCommunications .RecordReceivedDeviceData (ctx , lastComm )
138154 if err != nil {
139155 lgr .WithError (err ).Info ("Unable to record device data received" )
140156 }
141- notes , err := c .Evaluator .EvaluateData (ctx , * datum .UserID , * datum .UploadID )
157+ notes , err := c .Evaluator .EvaluateData (ctx , id .UserID , id .UploadID )
142158 if err != nil {
143159 format := "Unable to evalaute device data triggered event for user %s"
144- return errors .Wrapf (err , format , * datum .UserID )
160+ return errors .Wrapf (err , format , id .UserID )
145161 }
146162 for idx , note := range notes {
147163 lgr .WithField ("idx" , idx ).WithField ("note" , note ).Debug ("notes" )
0 commit comments