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

added map struct for external ids #320

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Add External IDs to map struct [#239](https://github.com/rokwire/groups-building-block/issues/239)

## [1.9.0] - 2022-12-07
### Added
Expand Down
21 changes: 11 additions & 10 deletions core/model/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package model

// MembershipFilter Wraps all possible filters for getting group members call
type MembershipFilter struct {
ID *string `json:"id"` // membership id
GroupIDs []string `json:"group_ids"` // list of group ids
UserID *string `json:"user_id"` // core user id
UserIDs []string `json:"user_ids"` // core user ids
ExternalID *string `json:"external_id"` // core user external id
NetID *string `json:"net_id"` // core user net id
Name *string `json:"name"` // member's name
Statuses []string `json:"statuses"` // lest of membership statuses
Offset *int64 `json:"offset"` // result offset
Limit *int64 `json:"limit"` // result limit
ID *string `json:"id"` // membership id
GroupIDs []string `json:"group_ids"` // list of group ids
UserID *string `json:"user_id"` // core user id
UserIDs []string `json:"user_ids"` // core user ids
ExternalID *string `json:"external_id"` // core user external id
NetID *string `json:"net_id"` // core user net id
Name *string `json:"name"` // member's name
Statuses []string `json:"statuses"` // lest of membership statuses
Offset *int64 `json:"offset"` // result offset
Limit *int64 `json:"limit"` // result limit
ExternalIDs map[string]string `json:"external_ids` //Map of external ids
Comment on lines +5 to +15
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we gonna support a map wih key value pairs for all external ids, then I would highly appriaciate to take out the legacy field and make a migration.

} // @name MembershipFilter

// GroupsFilter Wraps all possible filters for getting a group
Expand Down
20 changes: 11 additions & 9 deletions core/model/group_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ func (c *MembershipCollection) GetMembersAsNotificationRecipients(predicate mute

// GroupMembership represents the membership of a user to a given group
type GroupMembership struct {
ID string `json:"id" bson:"_id"`
ClientID string `json:"client_id" bson:"client_id"`
GroupID string `json:"group_id" bson:"group_id"`
UserID string `json:"user_id" bson:"user_id"`
ExternalID string `json:"external_id" bson:"external_id"`
Name string `json:"name" bson:"name"`
NetID string `json:"net_id" bson:"net_id"`
Email string `json:"email" bson:"email"`
PhotoURL string `json:"photo_url" bson:"photo_url"`
ID string `json:"id" bson:"_id"`
ClientID string `json:"client_id" bson:"client_id"`
GroupID string `json:"group_id" bson:"group_id"`
UserID string `json:"user_id" bson:"user_id"`
ExternalID string `json:"external_id" bson:"external_id"`
Name string `json:"name" bson:"name"`
NetID string `json:"net_id" bson:"net_id"`
Email string `json:"email" bson:"email"`
PhotoURL string `json:"photo_url" bson:"photo_url"`
ExternalIDs map[string]string `json:"external_ids" bson:"external_ids"`

// TODO: This is dangerous code-breaking change. There are existing clients that may use it in the old way.
Status string `json:"status" bson:"status"` //pending, member, rejected
Expand Down Expand Up @@ -221,6 +222,7 @@ func (m *GroupMembership) ToMember() Member {
DateCreated: m.DateCreated,
DateUpdated: m.DateUpdated,
DateAttended: m.DateAttended,
ExternalIDs: m.ExternalIDs,
}
}

Expand Down
39 changes: 21 additions & 18 deletions core/model/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@
package model

import (
"github.com/google/uuid"
"groups/driven/notifications"
"time"

"github.com/google/uuid"
)

// Member represents group member entity
type Member struct {
ID string `json:"id" bson:"id"`
UserID string `json:"user_id" bson:"user_id"`
ExternalID string `json:"external_id" bson:"external_id"`
Name string `json:"name" bson:"name"`
NetID string `json:"net_id" bson:"net_id"`
Email string `json:"email" bson:"email"`
PhotoURL string `json:"photo_url" bson:"photo_url"`
Status string `json:"status" bson:"status"` //pending, member, admin, rejected
RejectReason string `json:"reject_reason" bson:"reject_reason"`
MemberAnswers []MemberAnswer `json:"member_answers" bson:"member_answers"`
ID string `json:"id" bson:"id"`
UserID string `json:"user_id" bson:"user_id"`
ExternalID string `json:"external_id" bson:"external_id"`
Name string `json:"name" bson:"name"`
NetID string `json:"net_id" bson:"net_id"`
Email string `json:"email" bson:"email"`
PhotoURL string `json:"photo_url" bson:"photo_url"`
Status string `json:"status" bson:"status"` //pending, member, admin, rejected
RejectReason string `json:"reject_reason" bson:"reject_reason"`
MemberAnswers []MemberAnswer `json:"member_answers" bson:"member_answers"`
ExternalIDs map[string]string `json:"external_ids" bson:"external_ids"`

DateCreated time.Time `json:"date_created" bson:"date_created"`
DateUpdated *time.Time `json:"date_updated" bson:"date_updated"`
Expand Down Expand Up @@ -166,11 +168,12 @@ func (m *Member) ToShortMemberRecord() ShortMemberRecord {

// ShortMemberRecord represents group short member entity only with important identifiers
type ShortMemberRecord struct {
ID string `json:"id" bson:"id"`
UserID string `json:"user_id" bson:"user_id"`
ExternalID string `json:"external_id" bson:"external_id"`
Name string `json:"name" bson:"name"`
NetID string `json:"net_id" bson:"net_id"`
Email string `json:"email" bson:"email"`
Status string `json:"status" bson:"status"` //pending, member, admin, rejected
ID string `json:"id" bson:"id"`
UserID string `json:"user_id" bson:"user_id"`
ExternalID string `json:"external_id" bson:"external_id"`
Name string `json:"name" bson:"name"`
NetID string `json:"net_id" bson:"net_id"`
Email string `json:"email" bson:"email"`
Status string `json:"status" bson:"status"` //pending, member, admin, rejected
ExternalIDs string ``
} //@name ShortMemberRecord
1 change: 1 addition & 0 deletions core/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type User struct {
ClientID string `bson:"client_id"`
Permissions []string `bson:"permissions"`
OriginalToken string
ExternalIDs map[string]string
} // @name User

// ToCreator coverts to Creator
Expand Down
7 changes: 7 additions & 0 deletions core/services_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func (app *Application) createMembership(clientID string, current *model.User, g
} else {
log.Printf("error app.createMembership() - unable to find user: %s", err)
}
} else if membership.NetID != "" {
user, err := app.storage.FindUser(clientID, membership.NetID, true)
if err == nil && user != nil {
membership.ApplyFromUserIfEmpty(user)
} else {
log.Printf("error app.createMembership() - unable to find user: %s", err)
}
}

err := app.storage.CreateMembership(clientID, current, group, membership)
Expand Down
3 changes: 3 additions & 0 deletions driven/storage/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func (sa *Adapter) LoginUser(clientID string, current *model.User) error {
primitive.E{Key: "external_id", Value: current.ExternalID},
primitive.E{Key: "net_id", Value: current.NetID},
primitive.E{Key: "date_updated", Value: now},
primitive.E{Key: "external_ids", Value: current.ExternalIDs},
}},
}
_, err = sa.db.groupMemberships.UpdateManyWithContext(context, filter, update, nil)
Expand Down Expand Up @@ -452,6 +453,7 @@ func (sa *Adapter) CreateGroup(clientID string, current *model.User, group *mode
castedMemberships = append(castedMemberships, membership)
}
} else if current != nil {
externalIDs := map[string]string{"netID": current.NetID, "externalID": current.ExternalID}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use under scores instead of camel names. Camel names are not wrong approach. The idea is just to keep common standard within the project.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in this way of thoughts externalID is better to be renamed to uin, uiuc_uin if we gonna support more such attributes and external ids...

CC @shurwit

castedMemberships = append(castedMemberships, model.GroupMembership{
ID: uuid.NewString(),
GroupID: insertedID,
Expand All @@ -464,6 +466,7 @@ func (sa *Adapter) CreateGroup(clientID string, current *model.User, group *mode
Status: "admin", // TODO needs more consideration (status vs flag)
Admin: true,
DateCreated: now,
ExternalIDs: externalIDs,
})
}

Expand Down
7 changes: 6 additions & 1 deletion driven/storage/adapter_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"groups/core/model"
"log"
"time"

"go.mongodb.org/mongo-driver/mongo"

"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand Down Expand Up @@ -170,6 +171,10 @@ func (sa *Adapter) FindGroupMembershipsWithContext(ctx TransactionContext, clien
matchFilter = append(matchFilter, bson.E{"name", primitive.Regex{fmt.Sprintf(`%s`, *filter.Name), "i"}})
}

if filter.ExternalIDs != nil {
matchFilter = append(matchFilter, bson.E{"external_ids", filter.ExternalIDs})
}

findOptions := options.FindOptions{
Sort: bson.D{
{"members.status", 1},
Expand Down
4 changes: 2 additions & 2 deletions driver/web/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ type Adapter struct {

// @title Rokwire Groups Building Block API
// @description Rokwire Groups Building Block API Documentation.
// @version 1.8.0
// @version 1.7.5
// @host localhost
// @BasePath /gr
// @schemes http
// @schemes https

// @securityDefinitions.apikey APIKeyAuth
// @in header
Expand Down
3 changes: 3 additions & 0 deletions driver/web/rest/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ func (h *ApisHandler) CreatePendingMember(clientID string, current *model.User,
NetID: current.NetID,
Email: current.Email,
MemberAnswers: mAnswers,
ExternalIDs: current.ExternalIDs,
}

if requestData.NotificationsPreferences != nil {
Expand Down Expand Up @@ -1122,6 +1123,7 @@ func (h *ApisHandler) CreateMember(clientID string, current *model.User, w http.
return
}

externalIDs := map[string]string{"netID": current.NetID, "externalID": current.ExternalID}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above/below

member := model.GroupMembership{
GroupID: groupID,
UserID: requestData.UserID,
Expand All @@ -1132,6 +1134,7 @@ func (h *ApisHandler) CreateMember(clientID string, current *model.User, w http.
PhotoURL: requestData.PhotoURL,
Status: requestData.Status,
DateAttended: requestData.DateAttended,
ExternalIDs: externalIDs,
}

err = h.app.Services.CreateMembership(clientID, current, group, &member)
Expand Down