-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added activity query with unit tests
- Loading branch information
1 parent
27b6658
commit 157b877
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
packages/dynamics-lib/src/queries/__tests__/activity.queries.spec.js
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,72 @@ | ||
import { createCRMActivity } from '../activity.queries.js' | ||
import { dynamicsClient } from '../../client/dynamics-client.js' | ||
|
||
jest.mock('dynamics-web-api', () => { | ||
return jest.fn().mockImplementation(() => { | ||
return { | ||
executeUnboundAction: jest.fn() | ||
} | ||
}) | ||
}) | ||
|
||
describe('Activity Service', () => { | ||
describe('createCRMActivity', () => { | ||
const mockResponse = { | ||
'@odata.context': 'https://dynamics.com/api/data/v9.1/defra_CreateRCRActivityResponse', | ||
RCRActivityId: 'abc123', | ||
ReturnStatus: 'success', | ||
SuccessMessage: 'RCR Activity - created successfully', | ||
ErrorMessage: null, | ||
oDataContext: 'https://dynamics.com/api/data/v9.1/defra_CreateRCRActivityResponse' | ||
} | ||
|
||
const errorResponse = { | ||
'@odata.context': 'https://dynamics.com/api/data/v9.1/.defra_CreateRCRActivityResponse', | ||
RCRActivityId: null, | ||
ReturnStatus: 'error', | ||
SuccessMessage: '', | ||
ErrorMessage: 'Failed to create activity', | ||
oDataContext: 'https://dynamics.com/api/data/v9.1/$metadata#Microsoft.Dynamics.CRM.defra_CreateRCRActivityResponse' | ||
} | ||
|
||
it('should call dynamicsClient with correct parameters', async () => { | ||
dynamicsClient.executeUnboundAction.mockResolvedValue(mockResponse) | ||
|
||
await createCRMActivity('contact-identifier-123', 2023) | ||
|
||
expect(dynamicsClient.executeUnboundAction).toHaveBeenCalledWith('defra_CreateRCRActivity', { | ||
ContactId: 'contact-identifier-123', | ||
ActivityStatus: 'STARTED', | ||
Season: 2023 | ||
}) | ||
}) | ||
|
||
it('should return the CRM response correctly', async () => { | ||
dynamicsClient.executeUnboundAction.mockResolvedValue(mockResponse) | ||
|
||
const result = await createCRMActivity('contact-identifier-123', 2024) | ||
|
||
expect(result).toEqual(mockResponse) | ||
}) | ||
|
||
it('should handle error in dynamicsClient response', async () => { | ||
const error = new Error('Failed to create activity') | ||
dynamicsClient.executeUnboundAction.mockRejectedValue(error) | ||
|
||
await expect(createCRMActivity('contact-identifier-123', 2024)).rejects.toThrow('Failed to create activity') | ||
}) | ||
|
||
it('should handle the case where activity creation fails', async () => { | ||
dynamicsClient.executeUnboundAction.mockResolvedValue(errorResponse) | ||
|
||
const result = await createCRMActivity('invalid-contact-id', 2024) | ||
|
||
expect(result).toMatchObject({ | ||
RCRActivityId: null, | ||
ReturnStatus: 'error', | ||
SuccessMessage: '', | ||
ErrorMessage: 'Failed to create activity' | ||
}) | ||
}) | ||
}) | ||
}) |
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,24 @@ | ||
import { dynamicsClient } from '../client/dynamics-client.js' | ||
|
||
/** | ||
* Creates an RCR Activity in Microsoft Dynamics CRM. | ||
* | ||
* @param {string} contactId - The ID of the contact associated with the activity. | ||
* @param {number} season - The season year for which the activity is being created. | ||
* @returns {Promise<Object>} - A promise that resolves to the response from Dynamics CRM. | ||
* @property {string} [email protected] - The OData context URL of the response. | ||
* @property {string} response.RCRActivityId - The unique identifier of the created RCR activity. | ||
* @property {string} response.ReturnStatus - The status of the activity creation operation (e.g., 'success'). | ||
* @property {string} response.SuccessMessage - A message indicating successful creation of the activity. | ||
* @property {string|null} response.ErrorMessage - An error message if the activity creation failed, otherwise null. | ||
* @property {string} response.oDataContext - The OData context URL of the response. | ||
*/ | ||
export const createCRMActivity = (contactId, season) => { | ||
const request = { | ||
ContactId: contactId, | ||
ActivityStatus: 'STARTED', | ||
Season: season | ||
} | ||
|
||
return dynamicsClient.executeUnboundAction('defra_CreateRCRActivity', request) | ||
} |