diff --git a/internal/auth/repository/eventsourcing/handler/user_membership.go b/internal/auth/repository/eventsourcing/handler/user_membership.go index 9835b9fac4..2643f84e31 100644 --- a/internal/auth/repository/eventsourcing/handler/user_membership.go +++ b/internal/auth/repository/eventsourcing/handler/user_membership.go @@ -179,6 +179,10 @@ func (m *UserMembership) processProject(event *models.Event) (err error) { return m.view.DeleteUserMembership(member.UserID, event.AggregateID, member.ObjectID, usr_model.MemberTypeProjectGrant, event.Sequence) case proj_es_model.ProjectChanged: err = m.updateProjectDisplayName(event) + case proj_es_model.ProjectRemoved: + return m.view.DeleteUserMembershipsByAggregateID(event.AggregateID, event.Sequence) + case proj_es_model.ProjectGrantRemoved: + return m.view.DeleteUserMembershipsByAggregateIDAndObjectID(event.AggregateID, member.ObjectID, event.Sequence) default: return m.view.ProcessedUserMembershipSequence(event.Sequence) } diff --git a/internal/auth/repository/eventsourcing/view/user_membership.go b/internal/auth/repository/eventsourcing/view/user_membership.go index b3bc810c76..bed9d84657 100644 --- a/internal/auth/repository/eventsourcing/view/user_membership.go +++ b/internal/auth/repository/eventsourcing/view/user_membership.go @@ -59,6 +59,22 @@ func (v *View) DeleteUserMembershipsByUserID(userID string, eventSequence uint64 return v.ProcessedUserMembershipSequence(eventSequence) } +func (v *View) DeleteUserMembershipsByAggregateID(aggregateID string, eventSequence uint64) error { + err := view.DeleteUserMembershipsByAggregateID(v.Db, userMembershipTable, aggregateID) + if err != nil { + return nil + } + return v.ProcessedUserMembershipSequence(eventSequence) +} + +func (v *View) DeleteUserMembershipsByAggregateIDAndObjectID(aggregateID, objectID string, eventSequence uint64) error { + err := view.DeleteUserMembershipsByAggregateIDAndObjectID(v.Db, userMembershipTable, aggregateID, objectID) + if err != nil { + return nil + } + return v.ProcessedUserMembershipSequence(eventSequence) +} + func (v *View) GetLatestUserMembershipSequence() (*repository.CurrentSequence, error) { return v.latestSequence(userMembershipTable) } diff --git a/internal/management/repository/eventsourcing/handler/user_membership.go b/internal/management/repository/eventsourcing/handler/user_membership.go index 763a446f1e..03e0a4aea4 100644 --- a/internal/management/repository/eventsourcing/handler/user_membership.go +++ b/internal/management/repository/eventsourcing/handler/user_membership.go @@ -167,6 +167,10 @@ func (m *UserMembership) processProject(event *models.Event) (err error) { return m.view.DeleteUserMembership(member.UserID, event.AggregateID, member.ObjectID, usr_model.MemberTypeProjectGrant, event.Sequence) case proj_es_model.ProjectChanged: err = m.updateProjectDisplayName(event) + case proj_es_model.ProjectRemoved: + return m.view.DeleteUserMembershipsByAggregateID(event.AggregateID, event.Sequence) + case proj_es_model.ProjectGrantRemoved: + return m.view.DeleteUserMembershipsByAggregateIDAndObjectID(event.AggregateID, member.ObjectID, event.Sequence) default: return m.view.ProcessedUserMembershipSequence(event.Sequence) } diff --git a/internal/management/repository/eventsourcing/view/user_membership.go b/internal/management/repository/eventsourcing/view/user_membership.go index 2a82ae89ce..3e39861380 100644 --- a/internal/management/repository/eventsourcing/view/user_membership.go +++ b/internal/management/repository/eventsourcing/view/user_membership.go @@ -55,6 +55,22 @@ func (v *View) DeleteUserMembershipsByUserID(userID string, eventSequence uint64 return v.ProcessedUserMembershipSequence(eventSequence) } +func (v *View) DeleteUserMembershipsByAggregateID(aggregateID string, eventSequence uint64) error { + err := view.DeleteUserMembershipsByAggregateID(v.Db, userMembershipTable, aggregateID) + if err != nil { + return nil + } + return v.ProcessedUserMembershipSequence(eventSequence) +} + +func (v *View) DeleteUserMembershipsByAggregateIDAndObjectID(aggregateID, objectID string, eventSequence uint64) error { + err := view.DeleteUserMembershipsByAggregateIDAndObjectID(v.Db, userMembershipTable, aggregateID, objectID) + if err != nil { + return nil + } + return v.ProcessedUserMembershipSequence(eventSequence) +} + func (v *View) GetLatestUserMembershipSequence() (*repository.CurrentSequence, error) { return v.latestSequence(userMembershipTable) } diff --git a/internal/user/repository/view/model/user_membership.go b/internal/user/repository/view/model/user_membership.go index bc2d2a6472..584c67b7bd 100644 --- a/internal/user/repository/view/model/user_membership.go +++ b/internal/user/repository/view/model/user_membership.go @@ -87,10 +87,10 @@ func (u *UserMembershipView) AppendEvent(event *models.Event) (err error) { err = u.setProjectMemberData(event) case proj_es_model.ProjectGrantMemberAdded: u.setRootData(event, model.MemberTypeProjectGrant) - err = u.setProjectMemberData(event) + err = u.setProjectGrantMemberData(event) case proj_es_model.ProjectGrantMemberChanged, proj_es_model.ProjectGrantMemberRemoved: - err = u.setProjectMemberData(event) + err = u.setProjectGrantMemberData(event) } return err } diff --git a/internal/user/repository/view/usermembership_view.go b/internal/user/repository/view/usermembership_view.go index 089b360504..a2082a06e0 100644 --- a/internal/user/repository/view/usermembership_view.go +++ b/internal/user/repository/view/usermembership_view.go @@ -83,3 +83,16 @@ func DeleteUserMembershipsByUserID(db *gorm.DB, table, userID string) error { delete := repository.PrepareDeleteByKey(table, model.UserMembershipSearchKey(usr_model.UserMembershipSearchKeyUserID), userID) return delete(db) } + +func DeleteUserMembershipsByAggregateID(db *gorm.DB, table, aggregateID string) error { + delete := repository.PrepareDeleteByKey(table, model.UserMembershipSearchKey(usr_model.UserMembershipSearchKeyAggregateID), aggregateID) + return delete(db) +} + +func DeleteUserMembershipsByAggregateIDAndObjectID(db *gorm.DB, table, aggregateID, objectID string) error { + delete := repository.PrepareDeleteByKeys(table, + repository.Key{Key: model.UserMembershipSearchKey(usr_model.UserMembershipSearchKeyAggregateID), Value: aggregateID}, + repository.Key{Key: model.UserMembershipSearchKey(usr_model.UserMembershipSearchKeyObjectID), Value: objectID}, + ) + return delete(db) +}