From 2ad6d4f4bb68acc9d2421cf6f1c5131ebdab0840 Mon Sep 17 00:00:00 2001 From: Jumpy Squirrel Date: Sat, 9 Nov 2024 14:55:28 +0100 Subject: [PATCH] fix(#94): detect group/room name conflicts during creation --- internal/service/groups/groups.go | 12 ++++++++++++ internal/service/rooms/rooms.go | 12 ++++++++++++ test/acceptance/groups_delete_test.go | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/internal/service/groups/groups.go b/internal/service/groups/groups.go index 909c364..4d59221 100644 --- a/internal/service/groups/groups.go +++ b/internal/service/groups/groups.go @@ -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, diff --git a/internal/service/rooms/rooms.go b/internal/service/rooms/rooms.go index 1ad535c..989b4ed 100644 --- a/internal/service/rooms/rooms.go +++ b/internal/service/rooms/rooms.go @@ -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), diff --git a/test/acceptance/groups_delete_test.go b/test/acceptance/groups_delete_test.go index f42678a..61a8e13 100644 --- a/test/acceptance/groups_delete_test.go +++ b/test/acceptance/groups_delete_test.go @@ -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")