-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): add sdk methods for organization apis (#4826)
* feat(node): add methods for organization apis * docs(node): fix organization docs * docs(node): fix organization docs
- Loading branch information
1 parent
bb18a27
commit 34dad81
Showing
6 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/node/src/lib/organizations/organizations.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export interface IOrganizations { | ||
list(); | ||
create(payload: IOrganizationCreatePayload); | ||
rename(payload: IOrganizationRenamePayload); | ||
getCurrent(); | ||
removeMember(memberId: string); | ||
updateMemberRole( | ||
memberId: string, | ||
payload: IOrganizationUpdateMemberRolePayload | ||
); | ||
getMembers(); | ||
updateBranding(payload: IOrganizationBrandingPayload); | ||
} | ||
|
||
export interface IOrganizationCreatePayload { | ||
name: string; | ||
logo?: string; | ||
} | ||
|
||
export interface IOrganizationRenamePayload { | ||
name: string; | ||
} | ||
|
||
export interface IOrganizationUpdateMemberRolePayload { | ||
role: string; | ||
} | ||
|
||
export interface IOrganizationBrandingPayload { | ||
logo: string; | ||
color: string; | ||
fontColor?: string; | ||
contentBackground?: string; | ||
fontFamily: string; | ||
} |
170 changes: 170 additions & 0 deletions
170
packages/node/src/lib/organizations/organizations.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { Novu } from '../novu'; | ||
import axios from 'axios'; | ||
|
||
const mockConfig = { | ||
apiKey: '1234', | ||
}; | ||
|
||
const mockedOrganization = { | ||
_id: '649070af750b25b4ac8a4704', | ||
__v: 0, | ||
name: 'Test Organization', | ||
branding: { | ||
logo: 'https://s3.us-east-1.amazonaws.com/bucket/key.jpeg', | ||
color: '#ff5517', | ||
fontFamily: 'Lato', | ||
}, | ||
createdAt: '2023-06-19T15:13:51.961Z', | ||
updatedAt: '2023-06-19T15:13:51.966Z', | ||
}; | ||
|
||
const mockedMember = { | ||
_id: '649070af750b25b4ac8a4759', | ||
memberStatus: 'active', | ||
_userId: '649070afaa9e50289df420d8', | ||
roles: ['admin'], | ||
_organizationId: mockedOrganization._id, | ||
createdAt: mockedOrganization.createdAt, | ||
updatedAt: mockedOrganization.createdAt, | ||
__v: 0, | ||
id: '649070af750b25b4ac8a4759', | ||
user: { | ||
_id: '649070afaa9e50289df420d8', | ||
firstName: 'john', | ||
lastName: 'doe', | ||
email: '[email protected]', | ||
profilePicture: | ||
'https://gravatar.com/avatar/fd876f8cd6a58277fc664d47ea10ad19?d=mp', | ||
createdAt: '2023-03-07T13:32:54.573Z', | ||
id: '649070afaa9e50289df420d8', | ||
}, | ||
}; | ||
|
||
jest.mock('axios'); | ||
|
||
describe('Novu Node.js package - Organizations class', () => { | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
let novu: Novu; | ||
|
||
const methods = ['get', 'post', 'put', 'delete', 'patch']; | ||
|
||
beforeEach(() => { | ||
mockedAxios.create.mockReturnThis(); | ||
novu = new Novu(mockConfig.apiKey); | ||
}); | ||
|
||
afterEach(() => { | ||
methods.forEach((method) => { | ||
mockedAxios[method].mockClear(); | ||
}); | ||
}); | ||
|
||
it('should list organizations', async () => { | ||
const mockedResponse = { | ||
data: [mockedOrganization], | ||
}; | ||
mockedAxios.get.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.list(); | ||
|
||
expect(mockedAxios.get).toBeCalled(); | ||
expect(result).toStrictEqual(mockedResponse); | ||
}); | ||
|
||
it('should create new organization', async () => { | ||
const organizationName = 'New Organization'; | ||
const mockedResponse = { | ||
data: { | ||
...mockedOrganization, | ||
name: organizationName, | ||
}, | ||
}; | ||
mockedAxios.post.mockResolvedValue(mockedResponse); | ||
|
||
const payload = { name: organizationName }; | ||
const result = await novu.organizations.create(payload); | ||
|
||
expect(mockedAxios.post).toBeCalledWith('/organizations', payload); | ||
expect(result).toStrictEqual(mockedResponse); | ||
}); | ||
|
||
it('should rename current organization', async () => { | ||
const newName = 'Renamed Organization'; | ||
const mockedResponse = { | ||
data: { | ||
name: newName, | ||
}, | ||
}; | ||
mockedAxios.patch.mockResolvedValue(mockedResponse); | ||
|
||
const payload = { name: newName }; | ||
const result = await novu.organizations.rename(payload); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.patch).toBeCalledWith('/organizations', payload); | ||
}); | ||
|
||
it('should fetch current organization', async () => { | ||
const mockedResponse = { data: mockedOrganization }; | ||
mockedAxios.get.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.getCurrent(); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.get).toBeCalledWith('/organizations/me'); | ||
}); | ||
|
||
it('should remove member from current organization', async () => { | ||
const mockedResponse = { data: mockedMember }; | ||
mockedAxios.delete.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.removeMember(mockedMember.id); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.delete).toBeCalledWith( | ||
`/organizations/members/${mockedMember.id}` | ||
); | ||
}); | ||
|
||
it('should update member role in current organization', async () => { | ||
const mockedResponse = { data: mockedMember }; | ||
mockedAxios.put.mockResolvedValue(mockedResponse); | ||
|
||
const payload = { role: 'admin' }; | ||
const result = await novu.organizations.updateMemberRole( | ||
mockedMember.id, | ||
payload | ||
); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.put).toBeCalledWith( | ||
`/organizations/members/${mockedMember.id}/roles`, | ||
payload | ||
); | ||
}); | ||
|
||
it('should fetch all members of current organization', async () => { | ||
const mockedResponse = { data: [mockedMember] }; | ||
mockedAxios.get.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.getMembers(); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.get).toBeCalledWith('/organizations/members'); | ||
}); | ||
|
||
it('should update branding details of current organization', async () => { | ||
const payload = { | ||
logo: 'https://s3.us-east-1.amazonaws.com/bucket/key.jpeg', | ||
color: '#000000', | ||
fontFamily: 'Lato', | ||
}; | ||
const mockedResponse = { data: payload }; | ||
mockedAxios.put.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.updateBranding(payload); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.put).toBeCalledWith('/organizations/branding', payload); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { WithHttp } from '../novu.interface'; | ||
import { | ||
IOrganizations, | ||
IOrganizationCreatePayload, | ||
IOrganizationRenamePayload, | ||
IOrganizationUpdateMemberRolePayload, | ||
IOrganizationBrandingPayload, | ||
} from './organizations.interface'; | ||
|
||
export class Organizations extends WithHttp implements IOrganizations { | ||
list() { | ||
return this.http.get('/organizations'); | ||
} | ||
|
||
create(payload: IOrganizationCreatePayload) { | ||
return this.http.post('/organizations', payload); | ||
} | ||
|
||
rename(payload: IOrganizationRenamePayload) { | ||
return this.http.patch('/organizations', payload); | ||
} | ||
|
||
getCurrent() { | ||
return this.http.get('/organizations/me'); | ||
} | ||
|
||
removeMember(memberId: string) { | ||
return this.http.delete(`/organizations/members/${memberId}`); | ||
} | ||
|
||
updateMemberRole( | ||
memberId: string, | ||
payload: IOrganizationUpdateMemberRolePayload | ||
) { | ||
return this.http.put(`/organizations/members/${memberId}/roles`, payload); | ||
} | ||
|
||
getMembers() { | ||
return this.http.get('/organizations/members'); | ||
} | ||
|
||
updateBranding(payload: IOrganizationBrandingPayload) { | ||
return this.http.put('/organizations/branding', payload); | ||
} | ||
} |