-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add groups API to SDK #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ferreiro
wants to merge
2
commits into
main
Choose a base branch
from
groups-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,305 @@ | ||
| import { SmashSend } from '../index'; | ||
| import { HttpClient } from '../utils/http-client'; | ||
|
|
||
| jest.mock('../utils/http-client'); | ||
| const MockedHttpClient = HttpClient as jest.MockedClass<typeof HttpClient>; | ||
|
|
||
| describe('Groups API', () => { | ||
| let smashsend: SmashSend; | ||
| let mockHttpClient: jest.Mocked<HttpClient>; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| smashsend = new SmashSend('test-api-key'); | ||
| mockHttpClient = MockedHttpClient.mock.instances[0] as jest.Mocked<HttpClient>; | ||
| }); | ||
|
|
||
| describe('create() method', () => { | ||
| it('should create a group with publicId only', async () => { | ||
| const mockResponse = { | ||
| group: { | ||
| id: 'grp_123abc', | ||
| publicId: 'company_123', | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| }; | ||
|
|
||
| mockHttpClient.post.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.create({ | ||
| publicId: 'company_123', | ||
| }); | ||
|
|
||
| expect(mockHttpClient.post).toHaveBeenCalledWith('/groups', { | ||
| publicId: 'company_123', | ||
| }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
|
|
||
| it('should create a group with displayName and traits', async () => { | ||
| const mockResponse = { | ||
| group: { | ||
| id: 'grp_123abc', | ||
| publicId: 'company_456', | ||
| displayName: 'Acme Corp', | ||
| traits: { | ||
| industry: 'technology', | ||
| employees: 500, | ||
| }, | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| }; | ||
|
|
||
| mockHttpClient.post.mockResolvedValue(mockResponse); | ||
|
|
||
| const createData = { | ||
| publicId: 'company_456', | ||
| displayName: 'Acme Corp', | ||
| traits: { | ||
| industry: 'technology', | ||
| employees: 500, | ||
| }, | ||
| }; | ||
|
|
||
| const result = await smashsend.groups.create(createData); | ||
|
|
||
| expect(mockHttpClient.post).toHaveBeenCalledWith('/groups', createData); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
| }); | ||
|
|
||
| describe('list() method', () => { | ||
| it('should list groups without parameters', async () => { | ||
| const mockResponse = { | ||
| cursor: null, | ||
| hasMore: false, | ||
| items: [ | ||
| { | ||
| id: 'grp_123', | ||
| publicId: 'company_123', | ||
| displayName: 'Acme Corp', | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| { | ||
| id: 'grp_456', | ||
| publicId: 'company_456', | ||
| displayName: 'Beta Inc', | ||
| createdAt: '2024-01-16T10:30:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| mockHttpClient.get.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.list(); | ||
|
|
||
| expect(mockHttpClient.get).toHaveBeenCalledWith('/groups', { params: undefined }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
|
|
||
| it('should list groups with pagination parameters', async () => { | ||
| const mockResponse = { | ||
| cursor: 'next_cursor_123', | ||
| hasMore: true, | ||
| items: [ | ||
| { | ||
| id: 'grp_123', | ||
| publicId: 'company_123', | ||
| displayName: 'Acme Corp', | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| mockHttpClient.get.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.list({ limit: 10, cursor: 'prev_cursor' }); | ||
|
|
||
| expect(mockHttpClient.get).toHaveBeenCalledWith('/groups', { | ||
| params: { limit: 10, cursor: 'prev_cursor' }, | ||
| }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
| }); | ||
|
|
||
| describe('get() method', () => { | ||
| it('should get a group by ID', async () => { | ||
| const mockResponse = { | ||
| group: { | ||
| id: 'grp_123abc', | ||
| publicId: 'company_123', | ||
| displayName: 'Acme Corp', | ||
| traits: { industry: 'technology' }, | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| }; | ||
|
|
||
| mockHttpClient.get.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.get('grp_123abc'); | ||
|
|
||
| expect(mockHttpClient.get).toHaveBeenCalledWith('/groups/grp_123abc'); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
| }); | ||
|
|
||
| describe('update() method', () => { | ||
| it('should update a group displayName', async () => { | ||
| const mockResponse = { | ||
| group: { | ||
| id: 'grp_123abc', | ||
| publicId: 'company_123', | ||
| displayName: 'Acme Corporation', | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| updatedAt: '2024-01-20T15:00:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| }; | ||
|
|
||
| mockHttpClient.patch.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.update('grp_123abc', { | ||
| displayName: 'Acme Corporation', | ||
| }); | ||
|
|
||
| expect(mockHttpClient.patch).toHaveBeenCalledWith('/groups/grp_123abc', { | ||
| displayName: 'Acme Corporation', | ||
| }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
|
|
||
| it('should update a group traits', async () => { | ||
| const mockResponse = { | ||
| group: { | ||
| id: 'grp_123abc', | ||
| publicId: 'company_123', | ||
| displayName: 'Acme Corp', | ||
| traits: { employees: 600, revenue: '10M' }, | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| updatedAt: '2024-01-20T15:00:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| }; | ||
|
|
||
| mockHttpClient.patch.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.update('grp_123abc', { | ||
| traits: { employees: 600, revenue: '10M' }, | ||
| }); | ||
|
|
||
| expect(mockHttpClient.patch).toHaveBeenCalledWith('/groups/grp_123abc', { | ||
| traits: { employees: 600, revenue: '10M' }, | ||
| }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
| }); | ||
|
|
||
| describe('delete() method', () => { | ||
| it('should delete a group', async () => { | ||
| const mockResponse = { | ||
| group: { | ||
| id: 'grp_123abc', | ||
| publicId: 'company_123', | ||
| displayName: 'Acme Corp', | ||
| createdAt: '2024-01-15T10:30:00Z', | ||
| workspaceId: 'wks_456', | ||
| }, | ||
| isDeleted: true, | ||
| }; | ||
|
|
||
| mockHttpClient.delete.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.delete('grp_123abc'); | ||
|
|
||
| expect(mockHttpClient.delete).toHaveBeenCalledWith('/groups/grp_123abc'); | ||
| expect(result).toEqual(mockResponse); | ||
| expect(result.isDeleted).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('addContact() method', () => { | ||
| it('should add a contact to a group', async () => { | ||
| const mockResponse = { | ||
| groupId: 'grp_123abc', | ||
| contactId: 'ctc_456def', | ||
| addedAt: '2024-01-20T15:00:00Z', | ||
| }; | ||
|
|
||
| mockHttpClient.post.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.addContact('grp_123abc', 'ctc_456def'); | ||
|
|
||
| expect(mockHttpClient.post).toHaveBeenCalledWith('/groups/grp_123abc/contacts', { | ||
| contactId: 'ctc_456def', | ||
| }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
| }); | ||
|
|
||
| describe('removeContact() method', () => { | ||
| it('should remove a contact from a group', async () => { | ||
| const mockResponse = { | ||
| groupId: 'grp_123abc', | ||
| contactId: 'ctc_456def', | ||
| isRemoved: true, | ||
| }; | ||
|
|
||
| mockHttpClient.delete.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.removeContact('grp_123abc', 'ctc_456def'); | ||
|
|
||
| expect(mockHttpClient.delete).toHaveBeenCalledWith('/groups/grp_123abc/contacts/ctc_456def'); | ||
| expect(result).toEqual(mockResponse); | ||
| expect(result.isRemoved).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('listContacts() method', () => { | ||
| it('should list contacts in a group without parameters', async () => { | ||
| const mockResponse = { | ||
| cursor: null, | ||
| hasMore: false, | ||
| items: [ | ||
| { contactId: 'ctc_123', addedAt: '2024-01-15T10:30:00Z' }, | ||
| { contactId: 'ctc_456', addedAt: '2024-01-16T10:30:00Z' }, | ||
| ], | ||
| }; | ||
|
|
||
| mockHttpClient.get.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.listContacts('grp_123abc'); | ||
|
|
||
| expect(mockHttpClient.get).toHaveBeenCalledWith('/groups/grp_123abc/contacts', { | ||
| params: undefined, | ||
| }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
|
|
||
| it('should list contacts in a group with pagination', async () => { | ||
| const mockResponse = { | ||
| cursor: 'next_cursor_456', | ||
| hasMore: true, | ||
| items: [{ contactId: 'ctc_789', addedAt: '2024-01-17T10:30:00Z' }], | ||
| }; | ||
|
|
||
| mockHttpClient.get.mockResolvedValue(mockResponse); | ||
|
|
||
| const result = await smashsend.groups.listContacts('grp_123abc', { | ||
| limit: 5, | ||
| cursor: 'prev_cursor', | ||
| }); | ||
|
|
||
| expect(mockHttpClient.get).toHaveBeenCalledWith('/groups/grp_123abc/contacts', { | ||
| params: { limit: 5, cursor: 'prev_cursor' }, | ||
| }); | ||
| expect(result).toEqual(mockResponse); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upsert method is identical to create, missing upsert behavior
High Severity
The
upsertmethod is documented to "create or update a contact by email" if one already exists, but its implementation just delegates tothis.create(options), which POSTs to/contacts— the exact same behavior ascreate. Since the codebase has a separateupdatemethod that PUTs to/contacts/{id}, the POST endpoint likely doesn't perform upsert logic. Users relying onupsertin signup flows would likely get duplicate-key errors for existing contacts instead of the documented update behavior.Reviewed by Cursor Bugbot for commit 15af3d3. Configure here.