Skip to content

Commit

Permalink
fix(#94): detect group/room name conflicts during creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumpy-Squirrel committed Nov 9, 2024
1 parent c9e14fc commit 2ad6d4f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/service/groups/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ func (g *groupService) CreateGroup(ctx context.Context, group *modelsv1.GroupCre
return "", common.NewBadRequest(ctx, common.GroupDataInvalid, validation)
}

// check for name conflicts
matchingIDs, err := g.DB.FindGroups(ctx, group.Name, 0, -1, nil)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return "", errGroupRead(ctx, err.Error())
}
}

if len(matchingIDs) > 0 {
return "", common.NewConflict(ctx, common.GroupDataDuplicate, common.Details("another group with this name already exists"))
}

// Create a new group in the database
groupID, err := g.DB.AddGroup(ctx, &entity.Group{
Name: group.Name,
Expand Down
12 changes: 12 additions & 0 deletions internal/service/rooms/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ func (r *roomService) CreateRoom(ctx context.Context, room *modelsv1.RoomCreate)
return "", common.NewBadRequest(ctx, common.RoomDataInvalid, validation)
}

// check for name conflicts
matchingIDs, err := r.DB.FindRooms(ctx, room.Name, 0, -1, 0, 0, nil)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return "", errRoomRead(ctx, err.Error())
}
}

if len(matchingIDs) > 0 {
return "", common.NewConflict(ctx, common.RoomDataDuplicate, common.Details("another room with this name already exists"))
}

roomID, err := r.DB.AddRoom(ctx, &entity.Room{
Name: room.Name,
Flags: collectFlags(room.Flags),
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/groups_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestGroupsDelete_UserNotMemberDeny(t *testing.T) {

docs.Given("Given an attendee with an active registration who is in a group")
id1 := setupExistingGroup(t, "kittens", false, "101")
_ = setupExistingGroup(t, "kittens", false, "202")
_ = setupExistingGroup(t, "puppies", false, "202")
token := tstValidUserToken(t, 202)

docs.When("When they attempt to delete a different group they are not a member of")
Expand Down

0 comments on commit 2ad6d4f

Please sign in to comment.