forked from rokwire/surveys-building-block
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [ID-34]Remove user data (rokwire#35) * set the core adapter in the application and set the delete logic file * in progress * set delete survey responses and delete surveys * set the Changelog.md * fix lint issues * mocks * remove unused code * Revert "mocks" This reverts commit 845dd92. * storage mocks * after go mod tidy * mocks * resolve comments * resolve comments --------- Co-authored-by: Stefan Vitanov <[email protected]> * update the version to 1.4.0 * update the dockerfile * [ID-38]Start/end date (rokwire#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 <[email protected]> * update the version to 1.5.0 * [ID-43]Fix "start_date" and "end_date" (rokwire#44) * set the response to start_date and end_date to be UNIX timestamp * set the Changelog.md * set start_date as nullable --------- Co-authored-by: Stefan Vitanov <[email protected]> * [ID-39]Add extras field to survey data (rokwire#45) * set the Changelog.md * add "extras" to the SurveyData doc * add "extras" to the model and set it as nullable --------- Co-authored-by: Stefan Vitanov <[email protected]> * [ID-37]Public flag (rokwire#46) * add public flag to the docs * add public to the model and the conversion * set the public field in the update filter and in the update conversion * add public to the query * fix * set public * add response to the admin get surveys API * set the mocks --------- Co-authored-by: Stefan Vitanov <[email protected]> * [ID-40]Archived flag (rokwire#47) * set the Changelog.md * fix the Changelog.md * set the docs * add archived in progress * set archived to the update filter * add archived to the storage filter * set the mocks --------- Co-authored-by: Stefan Vitanov <[email protected]> * update version to 1.6.0 * [ID-49]Add Estimated_completion_time (rokwire#50) * set the Changelog.md * set the docs * add to the model * fix var name in the model * set estimated_completion_time for create survey * set estimated_completion_time to the update --------- Co-authored-by: Stefan Vitanov <[email protected]> * update version to 1.7.0 * [ID-52]Fix "start_date" and "end_date" timestamp, "archived", "public" and set "complete" in the result (rokwire#53) * set the Changelog.md * fix the docs * remove unused code * Revert "remove unused code" This reverts commit eaa904c. * in progress * fix update survey * fix time filter for Get surveys * archived fix * fix the archived in admin API * fix public * set complete into the response * fix the Changelog * set the mocks * fix * fix the Changelog --------- Co-authored-by: Stefan Vitanov <[email protected]> * update version to 1.8.0 * [ID-55]Fix "public" and "archived" (rokwire#56) * set the Changelog.md * fix "public" and "archived" filter and query * mocks --------- Co-authored-by: Stefan Vitanov <[email protected]> * update version to 1.8.1 * [ID-58]Fix Get /surveys (rokwire#59) * fix the limit and remove claims.Subject * set the Changelog.md --------- Co-authored-by: Stefan Vitanov <[email protected]> * update version to 1.8.2 * [ID-61]Add "complete" field to show if the survey is completed (rokwire#62) * in progress * fix conversion * fix conversion and set the docs * add completed * add the Changelog.md * mocks --------- Co-authored-by: Stefan Vitanov <[email protected]> * update version to1.9.0 --------- Co-authored-by: stefanvitanov <[email protected]> Co-authored-by: Stefan Vitanov <[email protected]>
- Loading branch information
1 parent
4728744
commit 0d28682
Showing
30 changed files
with
1,268 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* Copyright (c) 2020 Board of Trustees of the University of Illinois. | ||
* All rights reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package core | ||
|
||
import ( | ||
"fmt" | ||
|
||
"application/core/interfaces" | ||
"application/core/model" | ||
corebb "application/driven/core" | ||
"time" | ||
|
||
"github.com/rokwire/logging-library-go/v2/logs" | ||
) | ||
|
||
type deleteDataLogic struct { | ||
logger logs.Logger | ||
|
||
serviceID string | ||
core *corebb.Adapter | ||
|
||
storage interfaces.Storage | ||
|
||
//delete data timer | ||
dailyDeleteTimer *time.Timer | ||
timerDone chan bool | ||
} | ||
|
||
func (d deleteDataLogic) start() error { | ||
|
||
//2. set up web tools timer | ||
go d.setupTimerForDelete() | ||
|
||
return nil | ||
} | ||
|
||
func (d deleteDataLogic) setupTimerForDelete() { | ||
d.logger.Info("Delete data timer") | ||
|
||
//cancel if active | ||
if d.dailyDeleteTimer != nil { | ||
d.logger.Info("setupTimerForDelete -> there is active timer, so cancel it") | ||
|
||
d.timerDone <- true | ||
d.dailyDeleteTimer.Stop() | ||
} | ||
|
||
//wait until it is the correct moment from the day | ||
location, err := time.LoadLocation("America/Chicago") | ||
if err != nil { | ||
d.logger.Errorf("Error getting location:%s\n", err.Error()) | ||
} | ||
now := time.Now().In(location) | ||
d.logger.Infof("setupTimerForDelete -> now - hours:%d minutes:%d seconds:%d\n", now.Hour(), now.Minute(), now.Second()) | ||
|
||
nowSecondsInDay := 60*60*now.Hour() + 60*now.Minute() + now.Second() | ||
desiredMoment := 14400 //4 AM | ||
|
||
var durationInSeconds int | ||
d.logger.Infof("setupTimerForDelete -> nowSecondsInDay:%d desiredMoment:%d\n", nowSecondsInDay, desiredMoment) | ||
if nowSecondsInDay <= desiredMoment { | ||
d.logger.Infof("setupTimerForDelete -> not delete process today, so the first process will be today") | ||
durationInSeconds = desiredMoment - nowSecondsInDay | ||
} else { | ||
d.logger.Infof("setupTimerForDelete -> the delete process has already been processed today, so the first process will be tomorrow") | ||
leftToday := 86400 - nowSecondsInDay | ||
durationInSeconds = leftToday + desiredMoment // the time which left today + desired moment from tomorrow | ||
} | ||
//log.Println(durationInSeconds) | ||
//duration := time.Second * time.Duration(3) | ||
duration := time.Second * time.Duration(durationInSeconds) | ||
d.logger.Infof("setupTimerForDelete -> first call after %s", duration) | ||
|
||
d.dailyDeleteTimer = time.NewTimer(duration) | ||
select { | ||
case <-d.dailyDeleteTimer.C: | ||
d.logger.Info("setupTimerForDelete -> delete timer expired") | ||
d.dailyDeleteTimer = nil | ||
|
||
d.process() | ||
case <-d.timerDone: | ||
// timer aborted | ||
d.logger.Info("setupTimerForDelete -> delete timer aborted") | ||
d.dailyDeleteTimer = nil | ||
} | ||
} | ||
|
||
func (d deleteDataLogic) process() { | ||
d.logger.Info("Deleting data process") | ||
|
||
//process work | ||
d.processDelete() | ||
|
||
//generate new processing after 24 hours | ||
duration := time.Hour * 24 | ||
d.logger.Infof("Deleting data process -> next call after %s", duration) | ||
d.dailyDeleteTimer = time.NewTimer(duration) | ||
select { | ||
case <-d.dailyDeleteTimer.C: | ||
d.logger.Info("Deleting data process -> timer expired") | ||
d.dailyDeleteTimer = nil | ||
|
||
d.process() | ||
case <-d.timerDone: | ||
// timer aborted | ||
d.logger.Info("Deleting data process -> timer aborted") | ||
d.dailyDeleteTimer = nil | ||
} | ||
} | ||
|
||
func (d deleteDataLogic) processDelete() { | ||
//load deleted accounts | ||
deletedMemberships, err := d.core.LoadDeletedMemberships() | ||
if err != nil { | ||
d.logger.Errorf("error on loading deleted accounts - %s", err) | ||
return | ||
} | ||
fmt.Print(deletedMemberships) | ||
//process by app org | ||
for _, appOrgSection := range deletedMemberships { | ||
d.logger.Infof("delete - [app-id:%s org-id:%s]", appOrgSection.AppID, appOrgSection.OrgID) | ||
|
||
accountsIDs := d.getAccountsIDs(appOrgSection.Memberships) | ||
if len(accountsIDs) == 0 { | ||
d.logger.Info("no accounts for deletion") | ||
continue | ||
} | ||
|
||
d.logger.Infof("accounts for deletion - %s", accountsIDs) | ||
|
||
//delete the data | ||
d.deleteAppOrgUsersData(appOrgSection.AppID, appOrgSection.OrgID, accountsIDs) | ||
} | ||
} | ||
|
||
func (d deleteDataLogic) deleteAppOrgUsersData(appID string, orgID string, accountsIDs []string) { | ||
// delete survey responses | ||
err := d.storage.DeleteSurveyResponsesWithIDs(appID, orgID, accountsIDs) | ||
if err != nil { | ||
d.logger.Errorf("error deleting the survey responses - %s", err) | ||
return | ||
} | ||
|
||
// delete surveys | ||
err = d.storage.DeleteSurveysWithIDs(appID, orgID, accountsIDs) | ||
if err != nil { | ||
d.logger.Errorf("error deleting the surveys - %s", err) | ||
return | ||
} | ||
|
||
} | ||
|
||
func (d deleteDataLogic) getAccountsIDs(memberships []model.DeletedMembership) []string { | ||
res := make([]string, len(memberships)) | ||
for i, item := range memberships { | ||
res[i] = item.AccountID | ||
} | ||
return res | ||
} | ||
|
||
// deleteLogic creates new deleteLogic | ||
func deleteLogic(coreAdapter corebb.Adapter, logger logs.Logger) deleteDataLogic { | ||
timerDone := make(chan bool) | ||
return deleteDataLogic{core: &coreAdapter, timerDone: timerDone, logger: logger} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.