Skip to content

Commit ec76430

Browse files
authored
Add GET /organization/:orgId/roles support (#262)
1 parent 1affad9 commit ec76430

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

src/main/kotlin/com/workos/organizations/OrganizationsApi.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.workos.common.models.Order
1010
import com.workos.organizations.models.Organization
1111
import com.workos.organizations.models.OrganizationList
1212
import com.workos.organizations.types.OrganizationDomainDataOptions
13+
import com.workos.roles.models.RoleList
1314

1415
/**
1516
* The OrganizationsApi provides convenience methods for working with WorkOS Organizations.
@@ -299,4 +300,11 @@ class OrganizationsApi(private val workos: WorkOS) {
299300

300301
return workos.put("/organizations/$id", Organization::class.java, config)
301302
}
303+
304+
/**
305+
* Retrieve a list of roles for the given organization.
306+
*/
307+
fun listOrganizationRoles(organizationId: String): RoleList {
308+
return workos.get("/organizations/$organizationId/roles", RoleList::class.java)
309+
}
302310
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.workos.roles.models
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
import com.fasterxml.jackson.annotation.JsonProperty
5+
6+
/**
7+
* Represents a WorkOS Organization resource. This class is not meant to be
8+
* instantiated directly.
9+
*
10+
* @param obj The unique object identifier type of the record.
11+
* @param id The unique identifier for the Role.
12+
* @param name The name of the Role.
13+
* @param slug The slug of the Role.
14+
* @param description The description of the Role.
15+
* @param type The type of the Role.
16+
* @param createdAt The timestamp of when the Role was created.
17+
* @param updatedAt The timestamp of when the Role was updated.
18+
*/
19+
data class Role
20+
@JsonCreator constructor(
21+
@JvmField
22+
@JsonProperty("object")
23+
val obj: String = "role",
24+
25+
@JvmField
26+
val id: String,
27+
28+
@JvmField
29+
val name: String,
30+
31+
@JvmField
32+
val slug: String,
33+
34+
@JvmField
35+
val description: String? = null,
36+
37+
@JvmField
38+
val type: RoleType,
39+
40+
@JvmField
41+
@JsonProperty("created_at")
42+
val createdAt: String,
43+
44+
@JvmField
45+
@JsonProperty("updated_at")
46+
val updatedAt: String
47+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.workos.roles.models
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
5+
/**
6+
* A list of WorkOS [Role] resources. This class is not meant to be
7+
* instantiated directly.
8+
*
9+
* @param data A list of [Role].
10+
*/
11+
data class RoleList
12+
@JsonCreator constructor(
13+
@JvmField
14+
val data: List<Role>,
15+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.workos.roles.models
2+
3+
import com.fasterxml.jackson.annotation.JsonValue
4+
5+
/**
6+
* An enumeration of types of [Role]
7+
*
8+
* @param type The Role type string value.
9+
*/
10+
enum class RoleType(@JsonValue val type: String) {
11+
Environment("EnvironmentRole"),
12+
Organization("OrganizationRole")
13+
}

src/test/kotlin/com/workos/test/organizations/OrganizationsApiTest.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,56 @@ class OrganizationsApiTest : TestBase() {
582582
assertEquals(data["organizationDomainName"], organization.name)
583583
assertEquals(data["organizationDomainId"], organization.domains[0].id)
584584
}
585+
586+
@Test
587+
fun listOrganizationRolesShouldReturnPayload() {
588+
val workos = createWorkOSClient()
589+
590+
val organizationId = "org_01FJYCNTB6VC4K5R8BTF86286Q"
591+
592+
stubResponse(
593+
"/organizations/$organizationId/roles",
594+
"""{
595+
"object": "list",
596+
"data": [
597+
{
598+
"object": "role",
599+
"id": "role_01EHQMYV6MBK39QC5PZXHY59C5",
600+
"name": "Admin",
601+
"slug": "admin",
602+
"description": null,
603+
"type": "EnvironmentRole",
604+
"created_at": "2024-01-01T00:00:00.000Z",
605+
"updated_at": "2024-01-01T00:00:00.000Z"
606+
},
607+
{
608+
"object": "role",
609+
"id": "role_01EHQMYV6MBK39QC5PZXHY59C3",
610+
"name": "Member",
611+
"slug": "member",
612+
"description": null,
613+
"type": "EnvironmentRole",
614+
"created_at": "2024-01-01T00:00:00.000Z",
615+
"updated_at": "2024-01-01T00:00:00.000Z"
616+
},
617+
{
618+
"object": "role",
619+
"id": "role_01EHQMYV6MBK39QC5PZXHY59C3",
620+
"name": "OrganizationMember",
621+
"slug": "org-member",
622+
"description": null,
623+
"type": "OrganizationRole",
624+
"created_at": "2024-01-01T00:00:00.000Z",
625+
"updated_at": "2024-01-01T00:00:00.000Z"
626+
}
627+
]
628+
}"""
629+
)
630+
631+
val (roles) = workos.organizations.listOrganizationRoles(organizationId)
632+
633+
assertEquals("role_01EHQMYV6MBK39QC5PZXHY59C5", roles.get(0).id)
634+
assertEquals("Admin", roles.get(0).name)
635+
assertEquals("admin", roles.get(0).slug)
636+
}
585637
}

0 commit comments

Comments
 (0)