feat(groups): unique names

This commit is contained in:
Daniel Hougaard
2025-01-16 01:01:01 +01:00
parent b8115d481c
commit c4d0896609
9 changed files with 103 additions and 12 deletions
@@ -88,6 +88,13 @@ export const groupServiceFactory = ({
if (!hasRequiredPriviledges)
throw new ForbiddenRequestError({ message: "Failed to create a more privileged group" });
const existingGroup = await groupDAL.findOne({ orgId: actorOrgId, name });
if (existingGroup) {
throw new BadRequestError({
message: `Failed to create group with name '${name}'. Group with the same name already exists`
});
}
const group = await groupDAL.create({
name,
slug: slug || slugify(`${name}-${alphaNumericNanoId(4)}`),
@@ -145,6 +152,16 @@ export const groupServiceFactory = ({
if (isCustomRole) customRole = customOrgRole;
}
if (name) {
const existingGroup = await groupDAL.findOne({ orgId: actorOrgId, name });
if (existingGroup && existingGroup.id !== id) {
throw new BadRequestError({
message: `Failed to update group with name '${name}'. Group with the same name already exists`
});
}
}
const [updatedGroup] = await groupDAL.update(
{
id: group.id
@@ -790,6 +790,18 @@ export const scimServiceFactory = ({
});
const newGroup = await groupDAL.transaction(async (tx) => {
const conflictingGroup = await groupDAL.findOne({
name: displayName,
orgId
});
if (conflictingGroup) {
throw new ScimRequestError({
detail: `Group with name '${displayName}' already exists in the organization`,
status: 409
});
}
const group = await groupDAL.create(
{
name: displayName,