Skip to content

Commit

Permalink
implement endpoint and db logic to remove all data from a participant
Browse files Browse the repository at this point in the history
  • Loading branch information
phev8 committed Jun 21, 2020
1 parent 7b142f6 commit cba4674
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
9 changes: 9 additions & 0 deletions pkg/dbs/studydb/db_study-participants.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func (dbService *StudyDBService) SaveParticipantState(instanceID string, studyKe
return elem, err
}

func (dbService *StudyDBService) DeleteParticipantState(instanceID string, studyKey string, pID string) error {
ctx, cancel := dbService.getContext()
defer cancel()

filter := bson.M{"participantID": pID}
_, err := dbService.collectionRefStudyParticipant(instanceID, studyKey).DeleteOne(ctx, filter)
return err
}

// FindParticipantState retrieves the participant state for a given participant from a study
func (dbService *StudyDBService) FindAndExecuteOnParticipantsStates(
instanceID string,
Expand Down
16 changes: 16 additions & 0 deletions pkg/dbs/studydb/db_study-responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,19 @@ func (dbService *StudyDBService) PerfomActionForSurveyResponses(
}
return nil
}

func (dbService *StudyDBService) DeleteSurveyResponses(instanceID string, studyKey string, query ResponseQuery) (count int64, err error) {
ctx, cancel := dbService.getContext()
defer cancel()

if query.ParticipantID == "" {
return 0, errors.New("participant id must be defined")
}
filter := bson.M{"participantID": query.ParticipantID}
if query.SurveyKey != "" {
filter["key"] = query.SurveyKey
}

res, err := dbService.collectionRefSurveyResponses(instanceID, studyKey).DeleteMany(ctx, filter)
return res.DeletedCount, err
}
38 changes: 32 additions & 6 deletions pkg/service/studyflow_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/influenzanet/study-service/pkg/api"
"github.com/influenzanet/study-service/pkg/dbs/studydb"
"github.com/influenzanet/study-service/pkg/types"
"github.com/influenzanet/study-service/pkg/utils"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -372,10 +373,35 @@ func (s *studyServiceServer) DeleteParticipantData(ctx context.Context, req *api
return nil, status.Error(codes.InvalidArgument, "missing argument")
}

// get all studies
// for each study:
// get participant ID
// remove participant if any
// remove responses for participantID
return nil, status.Error(codes.Unimplemented, "unimplemented")
studies, err := s.studyDBservice.GetStudiesByStatus(req.InstanceId, "", true)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

profileIDs := []string{req.ProfilId}
profileIDs = append(profileIDs, req.OtherProfileIds...)

for _, study := range studies {
for _, profileID := range profileIDs {
// ParticipantID
participantID, err := s.profileIDToParticipantID(req.InstanceId, study.Key, profileID)
if err != nil {
log.Printf("DeleteParticipantData: %v", err)
continue
}
err = s.studyDBservice.DeleteParticipantState(req.InstanceId, study.Key, participantID)
if err != nil {
continue
}
_, err = s.studyDBservice.DeleteSurveyResponses(req.InstanceId, study.Key, studydb.ResponseQuery{ParticipantID: participantID})
if err != nil {
continue
}
}

}
return &api.ServiceStatus{
Status: api.ServiceStatus_NORMAL,
Msg: "all responses deleted",
}, nil
}

0 comments on commit cba4674

Please sign in to comment.