diff --git a/src/resources/groups.ts b/src/resources/groups.ts index e627d516..d5ece8e1 100644 --- a/src/resources/groups.ts +++ b/src/resources/groups.ts @@ -50,6 +50,22 @@ export class Groups extends Resource<{realm?: string}> { urlParamKeys: ['id'], }); + /** + * Set or create child. + * This will just set the parent if it exists. Create it and set the parent if the group doesn’t exist. + */ + + public setOrCreateChild = this.makeUpdateRequest< + {id: string}, + GroupRepresentation, + {id: string} + >({ + method: 'POST', + path: '/{id}/children', + urlParamKeys: ['id'], + returnResourceIdInLocationHeader: {field: 'id'}, + }); + /** * Members */ diff --git a/test/groups.spec.ts b/test/groups.spec.ts index d3cbd528..71a012a6 100644 --- a/test/groups.spec.ts +++ b/test/groups.spec.ts @@ -72,6 +72,26 @@ describe('Groups', function() { }); }); + it('set or create child', async () => { + const groupName = 'child-group'; + const groupId = this.currentGroup.id; + const childGroup = await this.kcAdminClient.groups.setOrCreateChild( + {id: groupId}, + {name: groupName}, + ); + + expect(childGroup.id).to.be.ok; + + const group = await this.kcAdminClient.groups.findOne({ + id: groupId, + }); + expect(group.subGroups[0]).to.deep.include({ + id: childGroup.id, + name: groupName, + path: `/${group.name}/${groupName}`, + }); + }); + /** * Role mappings */