Skip to content

Commit

Permalink
feat: add getRoles for organization (#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Jan 9, 2024
1 parent 8d5cb06 commit ee78332
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/adapters/REST/endpoints/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { RawAxiosRequestHeaders } from 'axios'
import type { AxiosInstance } from 'contentful-sdk-core'
import copy from 'fast-copy'
import { SetOptional } from 'type-fest'
import { CollectionProp, GetSpaceParams, QueryParams } from '../../../common-types'
import {
CollectionProp,
GetOrganizationParams,
GetSpaceParams,
QueryParams,
} from '../../../common-types'
import { CreateRoleProps, RoleProps } from '../../../entities/role'
import { RestEndpoint } from '../types'
import * as raw from './raw'
Expand All @@ -24,6 +29,15 @@ export const getMany: RestEndpoint<'Role', 'getMany'> = (
})
}

export const getManyForOrganization: RestEndpoint<'Role', 'getManyForOrganization'> = (
http: AxiosInstance,
params: GetOrganizationParams & QueryParams
) => {
return raw.get<CollectionProp<RoleProps>>(http, `/organizations/${params.organizationId}/roles`, {
params: normalizeSelect(params.query),
})
}

export const create: RestEndpoint<'Role', 'create'> = (
http: AxiosInstance,
params: GetSpaceParams,
Expand Down
5 changes: 5 additions & 0 deletions lib/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ type MRInternal<UA extends boolean> = {

(opts: MROpts<'Role', 'get', UA>): MRReturn<'Role', 'get'>
(opts: MROpts<'Role', 'getMany', UA>): MRReturn<'Role', 'getMany'>
(opts: MROpts<'Role', 'getManyForOrganization', UA>): MRReturn<'Role', 'getManyForOrganization'>
(opts: MROpts<'Role', 'create', UA>): MRReturn<'Role', 'create'>
(opts: MROpts<'Role', 'createWithId', UA>): MRReturn<'Role', 'createWithId'>
(opts: MROpts<'Role', 'update', UA>): MRReturn<'Role', 'update'>
Expand Down Expand Up @@ -1459,6 +1460,10 @@ export type MRActions = {
Role: {
get: { params: GetSpaceParams & { roleId: string }; return: RoleProps }
getMany: { params: GetSpaceParams & QueryParams; return: CollectionProp<RoleProps> }
getManyForOrganization: {
params: GetOrganizationParams & QueryParams
return: CollectionProp<RoleProps>
}
create: {
params: GetSpaceParams
payload: CreateRoleProps
Expand Down
25 changes: 25 additions & 0 deletions lib/create-organization-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function createOrganizationApi(makeRequest: MakeRequest) {
const { wrapAppKey, wrapAppKeyCollection } = entities.appKey
const { wrapAppDetails } = entities.appDetails
const { wrapAppAction, wrapAppActionCollection } = entities.appAction
const { wrapRoleCollection } = entities.role

return {
/**
Expand Down Expand Up @@ -486,6 +487,30 @@ export default function createOrganizationApi(makeRequest: MakeRequest) {
payload: data,
}).then((data) => wrapOrganizationInvitation(makeRequest, data))
},
/**
* Gets a collection of Roles
* @return Promise for a collection of Roles
* @example ```javascript
* const contentful = require('contentful-management')
*
* const client = contentful.createClient({
* accessToken: '<content_management_api_key>'
* })
*
* client.getOrganization('<org_id>')
* .then((org) => org.getRoles())
* .then((response) => console.log(response.items))
* .catch(console.error)
* ```
*/
getRoles(query: QueryOptions = {}) {
const raw = this.toPlainObject() as OrganizationProp
return makeRequest({
entityType: 'Role',
action: 'getManyForOrganization',
params: { organizationId: raw.sys.id, query: createRequestConfig({ query }).params },
}).then((data) => wrapRoleCollection(makeRequest, data))
},
/**
* Creates an app definition
* @param Object representation of the App Definition to be created
Expand Down
3 changes: 3 additions & 0 deletions lib/plain/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ export type PlainClientAPI = {
getMany(
params: OptionalDefaults<GetSpaceParams & QueryParams>
): Promise<CollectionProp<RoleProps>>
getManyForOrganization(
params: OptionalDefaults<GetOrganizationParams & QueryParams>
): Promise<CollectionProp<RoleProps>>
create(
params: OptionalDefaults<GetSpaceParams>,
data: CreateRoleProps,
Expand Down
1 change: 1 addition & 0 deletions lib/plain/plain-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ export const createPlainClient = (
role: {
get: wrap(wrapParams, 'Role', 'get'),
getMany: wrap(wrapParams, 'Role', 'getMany'),
getManyForOrganization: wrap(wrapParams, 'Role', 'getManyForOrganization'),
create: wrap(wrapParams, 'Role', 'create'),
createWithId: wrap(wrapParams, 'Role', 'createWithId'),
update: wrap(wrapParams, 'Role', 'update'),
Expand Down
11 changes: 10 additions & 1 deletion test/integration/role-integration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initClient, createTestSpace, generateRandomId } from '../helpers'
import { initClient, createTestSpace, generateRandomId, getTestOrganization } from '../helpers'
import { after, before, describe, test } from 'mocha'
import { expect } from 'chai'

Expand All @@ -25,9 +25,11 @@ const roleDefinition = {
}

describe('Role Api', function () {
let org
let space

before(async () => {
org = await getTestOrganization()
space = await createTestSpace(initClient(), 'Role')
})

Expand All @@ -44,6 +46,13 @@ describe('Role Api', function () {
})
})

test('Gets roles for organization', async () => {
return org.getRoles().then((response) => {
expect(response.sys, 'sys').ok
expect(response.items, 'fields').ok
})
})

test('Create role with id', async () => {
const id = generateRandomId('role')
return space.createRoleWithId(id, roleDefinition).then((role) => {
Expand Down

0 comments on commit ee78332

Please sign in to comment.