Skip to content

Commit

Permalink
Add user delete hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
StJudeWasHere committed Sep 11, 2024
1 parent 09ed999 commit 2aad118
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
17 changes: 6 additions & 11 deletions internal/services/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,7 @@ func (c *Container) InitReportManager() {

// Create the user service.
func (c *Container) InitUserService() {
repository := &struct {
*repository.UserRepository
*repository.ProjectRepository
*repository.CrawlRepository
}{
c.userRepository,
c.projectRepository,
c.crawlRepository,
}

c.UserService = NewUserService(repository)
c.UserService = NewUserService(c.userRepository)
}

// Create the Project service.
Expand All @@ -173,6 +163,11 @@ func (c *Container) InitProjectService() {
}

c.ProjectService = NewProjectService(repository)

// UserService DeleteHooks are called when a user is deleted.
// Add a DeleteHook so it deletes all user projects and crawl
// data when a user is deleted.
c.UserService.AddDeleteHook(c.ProjectService.DeleteAllUserProjects)
}

// Create the ProjectView service.
Expand Down
10 changes: 10 additions & 0 deletions internal/services/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type (
DisableProject(*models.Project)
UpdateProject(p *models.Project) error
FindProjectById(id int, uid int) (models.Project, error)
FindProjectsByUser(userId int) []models.Project

DeleteProjectCrawls(*models.Project)
}
Expand Down Expand Up @@ -78,3 +79,12 @@ func (s *ProjectService) DeleteProject(p *models.Project) {
func (s *ProjectService) UpdateProject(p *models.Project) error {
return s.repository.UpdateProject(p)
}

// Delete all user projects and crawl data.
func (s *ProjectService) DeleteAllUserProjects(user *models.User) {
projects := s.repository.FindProjectsByUser(user.Id)
for _, p := range projects {
s.repository.DeleteProjectCrawls(&p)
s.repository.DeleteProject(&p)
}
}
5 changes: 3 additions & 2 deletions internal/services/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type projectTestRepository struct{}
func (s *projectTestRepository) SaveProject(project *models.Project, userId int) {}
func (s *projectTestRepository) DeleteProject(project *models.Project) {}
func (s *projectTestRepository) DisableProject(project *models.Project) {}
func (s *projectTestRepository) UpdateProject(p *models.Project) error {
return nil
func (s *projectTestRepository) UpdateProject(p *models.Project) error { return nil }
func (p *projectTestRepository) FindProjectsByUser(uid int) []models.Project {
return []models.Project{}
}
func (s *projectTestRepository) FindProjectById(id, uid int) (models.Project, error) {
p := models.Project{}
Expand Down
22 changes: 12 additions & 10 deletions internal/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ var (
)

type (
DeleteHook func(user *models.User)

UserServiceRepository interface {
UserSignup(email, hashedPassword string) (*models.User, error)
FindUserByEmail(email string) (*models.User, error)
UserUpdatePassword(email, hashedPassword string) error
DeleteUser(user *models.User) error
DisableUser(user *models.User) error

DeleteProjectCrawls(project *models.Project)

DeleteProject(project *models.Project)
FindProjectsByUser(userId int) []models.Project
}

UserService struct {
repository UserServiceRepository
repository UserServiceRepository
deleteHooks []DeleteHook
}
)

Expand Down Expand Up @@ -120,16 +118,20 @@ func (s *UserService) UpdatePassword(user *models.User, currentPassword, newPass
func (s *UserService) DeleteUser(user *models.User) {
s.repository.DisableUser(user)
go func() {
projects := s.repository.FindProjectsByUser(user.Id)
for _, p := range projects {
s.repository.DeleteProjectCrawls(&p)
s.repository.DeleteProject(&p)
for _, h := range s.deleteHooks {
h(user)
}

s.repository.DeleteUser(user)
}()
}

// AddDeleteHook adds a new hook function that will be called when the user is deleted.
// This is used for user data clean up.
func (s *UserService) AddDeleteHook(hook DeleteHook) {
s.deleteHooks = append(s.deleteHooks, hook)
}

// Validate the password to make sure it follows certain criteria.
func (s *UserService) validPassword(password string) bool {
return len(password) > 1
Expand Down
5 changes: 0 additions & 5 deletions internal/services/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ func (s *userTestRepository) DeleteUser(u *models.User) error {
func (s *userTestRepository) DisableUser(*models.User) error {
return nil
}
func (p *userTestRepository) DeleteProject(*models.Project) {}
func (p *userTestRepository) FindProjectsByUser(uid int) []models.Project {
return []models.Project{}
}
func (p *userTestRepository) DeleteProjectCrawls(*models.Project) {}

var userService = services.NewUserService(&userTestRepository{})

Expand Down

0 comments on commit 2aad118

Please sign in to comment.