Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#411] Provision all group admins as Event admins. #413

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added
- Provision all group admins as Event admins [#411](https://github.com/rokwire/groups-building-block/issues/411)

## [1.28.0] - 2024-01-30
### Changed
- Disable automatic event memership provision for group events [#411](https://github.com/rokwire/groups-building-block/issues/411)
Expand Down
86 changes: 61 additions & 25 deletions core/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,16 @@ func (app *Application) setupSyncManagedGroupTimer() {
func (app *Application) createCalendarEventForGroups(clientID string, adminIdentifiers []model.AdminsIdentifiers, current *model.User, event map[string]interface{}, groupIDs []string) (map[string]interface{}, []string, error) {
memberships, err := app.findGroupMemberships(clientID, model.MembershipFilter{
GroupIDs: groupIDs,
UserID: &current.ID,
Statuses: []string{"admin"},
})
if err != nil {
return nil, nil, err
}

if len(memberships.Items) > 0 {
var newGroupIDs []string
for _, membership := range memberships.Items {
newGroupIDs = append(newGroupIDs, membership.GroupID)
}

adminIdentifiers = append(adminIdentifiers, model.AdminsIdentifiers{
AccountID: &current.ID,
ExternalID: &current.ExternalID,
})
if memberships.GetMembershipByAccountID(current.ID) != nil {
mergedAdminIdentifiers, newGroupIDs := app.buildAdminIDs(current, memberships, adminIdentifiers)

createdEvent, err := app.calendar.CreateCalendarEvent(adminIdentifiers, current.ID, event, current.OrgID, current.AppID)
createdEvent, err := app.calendar.CreateCalendarEvent(mergedAdminIdentifiers, current.ID, event, current.OrgID, current.AppID)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -196,6 +187,60 @@ func (app *Application) createCalendarEventForGroups(clientID string, adminIdent
return nil, nil, nil
}

func (app *Application) buildAdminIDs(current *model.User, collection model.MembershipCollection, admins []model.AdminsIdentifiers) ([]model.AdminsIdentifiers, []string) {
var newGroupIDs []string
var newGroupIDsMapping = map[string]bool{}
var newAdminIDsBulMapping = map[string]bool{}
var newAdminIDsValMapping = map[string]model.AdminsIdentifiers{}

// Reconstruct admins list
for _, admin := range admins {
userID := admin.AccountID
newAdminIDsBulMapping[*userID] = true
newAdminIDsValMapping[*userID] = admin
}

// Current user as default admin
newAdminIDsBulMapping[current.ID] = true
currentUserID := current.ID
currentExternalID := current.ExternalID
newAdminIDsValMapping[current.ID] = model.AdminsIdentifiers{
AccountID: &currentUserID,
ExternalID: &currentExternalID,
}

// Construct new admin mappings and resolve duplications
for _, membership := range collection.Items {
if membership.ExternalID != "" || membership.UserID != "" {
newGroupIDsMapping[membership.GroupID] = true
userID := membership.UserID
var externalID *string
if membership.ExternalID != "" {
externalID = &membership.ExternalID
}
if !newAdminIDsBulMapping[userID] {
newAdminIDsBulMapping[userID] = true
newAdminIDsValMapping[userID] = model.AdminsIdentifiers{
AccountID: &userID,
ExternalID: externalID,
}
}
}
}

// Construct group IDs
for groupID := range newGroupIDsMapping {
newGroupIDs = append(newGroupIDs, groupID)
}

// Construct admin IDs
for adminID := range newAdminIDsValMapping {
admins = append(admins, newAdminIDsValMapping[adminID])
}

return admins, newGroupIDs
}

func (app *Application) createCalendarEventForGroupsMembers(clientID string, orgID string, appID string, eventID string, groupIDs []string, members []model.ToMember) error {
for _, groupID := range groupIDs {

Expand Down Expand Up @@ -232,23 +277,14 @@ func (app *Application) createCalendarEventForGroupsMembers(clientID string, org
func (app *Application) createCalendarEventSingleGroup(clientID string, current *model.User, event map[string]interface{}, groupID string, members []model.ToMember) (map[string]interface{}, []model.ToMember, error) {
memberships, err := app.findGroupMemberships(clientID, model.MembershipFilter{
GroupIDs: []string{groupID},
UserID: &current.ID,
Statuses: []string{"admin"},
})
if err != nil {
return nil, nil, err
}

if len(memberships.Items) > 0 {
var groupIDs []string
for _, membership := range memberships.Items {
groupIDs = append(groupIDs, membership.GroupID)
}

adminIdentifiers := []model.AdminsIdentifiers{{
AccountID: &current.ID,
ExternalID: &current.ExternalID,
}}
if memberships.GetMembershipByAccountID(current.ID) != nil {
adminIdentifiers, newGroupIDs := app.buildAdminIDs(nil, memberships, nil)

createdEvent, err := app.calendar.CreateCalendarEvent(adminIdentifiers, current.ID, event, current.OrgID, current.AppID)
if err != nil {
Expand All @@ -259,8 +295,8 @@ func (app *Application) createCalendarEventSingleGroup(clientID string, current
var mappedGroupIDs []string
eventID := createdEvent["id"].(string)

for _, groupID := range groupIDs {
mapping, err := app.storage.CreateEvent(clientID, eventID, groupID, members, &model.Creator{
for _, constructedGroupID := range newGroupIDs {
mapping, err := app.storage.CreateEvent(clientID, eventID, constructedGroupID, members, &model.Creator{
UserID: current.ID,
Name: current.Name,
Email: current.Email,
Expand Down
Loading