Skip to content
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

Create activity in CRM #2072

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/dynamics-lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './queries/concession-proof.queries.js'
export * from './queries/pocl-validation-error.queries.js'
export * from './queries/recurring-payments.queries.js'
export * from './queries/contact.queries.js'
export * from './queries/activity.queries.js'

// Framework functionality
export * from './client/util.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { createActivity } 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('createActivity', () => {
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 createActivity('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 createActivity('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(createActivity('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 createActivity('invalid-contact-id', 2024)

expect(result).toMatchObject({
RCRActivityId: null,
ReturnStatus: 'error',
SuccessMessage: '',
ErrorMessage: 'Failed to create activity'
})
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are fine - however, they could have been a bit simpler. The pattern we use when returning the value of a mocked function is to set the resolved / return value to be a symbol.

})
})
24 changes: 24 additions & 0 deletions packages/dynamics-lib/src/queries/activity.queries.js
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 createActivity = (contactId, season) => {
const request = {
ContactId: contactId,
ActivityStatus: 'STARTED',
Season: season
}

return dynamicsClient.executeUnboundAction('defra_CreateRCRActivity', request)
}
Loading