From cba467406ce7f7c70af092954f68a70d7e55bc28 Mon Sep 17 00:00:00 2001 From: phev8 Date: Sun, 21 Jun 2020 22:51:18 +0200 Subject: [PATCH] implement endpoint and db logic to remove all data from a participant --- pkg/dbs/studydb/db_study-participants.go | 9 ++++++ pkg/dbs/studydb/db_study-responses.go | 16 ++++++++++ pkg/service/studyflow_endpoints.go | 38 ++++++++++++++++++++---- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/pkg/dbs/studydb/db_study-participants.go b/pkg/dbs/studydb/db_study-participants.go index 904b48b..61132d0 100644 --- a/pkg/dbs/studydb/db_study-participants.go +++ b/pkg/dbs/studydb/db_study-participants.go @@ -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, diff --git a/pkg/dbs/studydb/db_study-responses.go b/pkg/dbs/studydb/db_study-responses.go index 34f2c44..dda9c83 100644 --- a/pkg/dbs/studydb/db_study-responses.go +++ b/pkg/dbs/studydb/db_study-responses.go @@ -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 +} diff --git a/pkg/service/studyflow_endpoints.go b/pkg/service/studyflow_endpoints.go index f56afda..abae061 100644 --- a/pkg/service/studyflow_endpoints.go +++ b/pkg/service/studyflow_endpoints.go @@ -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" @@ -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 }