Skip to content

Commit

Permalink
Update activity in CRM (#2091)
Browse files Browse the repository at this point in the history
* Update activity in CRM

https://eaflood.atlassian.net/browse/IWTF-4401

Update activity in CRM
  • Loading branch information
nabeelamir-defra authored Dec 13, 2024
1 parent ba4d1da commit 29130b3
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createActivity } from '../activity.queries.js'
import { createActivity, updateActivity } from '../activity.queries.js'
import { dynamicsClient } from '../../client/dynamics-client.js'

jest.mock('dynamics-web-api', () => {
Expand All @@ -11,26 +11,26 @@ jest.mock('dynamics-web-api', () => {

describe('Activity Service', () => {
describe('createActivity', () => {
const mockResponse = {
const getSuccessResponse = () => ({
'@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 = {
const getErrorResponse = () => ({
'@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)
dynamicsClient.executeUnboundAction.mockResolvedValue(getSuccessResponse())

await createActivity('contact-identifier-123', 2023)

Expand All @@ -42,11 +42,12 @@ describe('Activity Service', () => {
})

it('should return the CRM response correctly', async () => {
dynamicsClient.executeUnboundAction.mockResolvedValue(mockResponse)
const successResponse = getSuccessResponse()
dynamicsClient.executeUnboundAction.mockResolvedValue(successResponse)

const result = await createActivity('contact-identifier-123', 2024)

expect(result).toEqual(mockResponse)
expect(result).toEqual(successResponse)
})

it('should handle error in dynamicsClient response', async () => {
Expand All @@ -57,7 +58,7 @@ describe('Activity Service', () => {
})

it('should handle the case where activity creation fails', async () => {
dynamicsClient.executeUnboundAction.mockResolvedValue(errorResponse)
dynamicsClient.executeUnboundAction.mockResolvedValue(getErrorResponse())

const result = await createActivity('invalid-contact-id', 2024)

Expand All @@ -69,4 +70,64 @@ describe('Activity Service', () => {
})
})
})

describe('updateActivity', () => {
const getSuccessResponse = () => ({
'@odata.context': 'https://dynamics.om/api/data/v9.1/defra_UpdateRCRActivityResponse',
ReturnStatus: 'success',
SuccessMessage: 'RCR Activity - updated successfully',
ErrorMessage: null,
oDataContext: 'https://dynamics.com/api/data/v9.1/defra_UpdateRCRActivityResponse'
})

const getErrorResponse = () => ({
'@odata.context': 'https://dynamics.om/api/data/v9.1/defra_UpdateRCRActivityResponse',
RCRActivityId: null,
ReturnStatus: 'error',
SuccessMessage: '',
ErrorMessage: 'Failed to update activity',
oDataContext: 'https://dynamics.com/api/data/v9.1/defra_UpdateRCRActivityResponse'
})

it('should call dynamicsClient with correct parameters', async () => {
dynamicsClient.executeUnboundAction.mockResolvedValue(getSuccessResponse())

await updateActivity('contact-identifier-123', 2023)

expect(dynamicsClient.executeUnboundAction).toHaveBeenCalledWith('defra_UpdateRCRActivity', {
ContactId: 'contact-identifier-123',
ActivityStatus: 'SUBMITTED',
Season: 2023
})
})

it('should return the CRM response correctly', async () => {
const successResponse = getSuccessResponse()
dynamicsClient.executeUnboundAction.mockResolvedValue(successResponse)

const result = await updateActivity('contact-identifier-123', 2024)

expect(result).toEqual(successResponse)
})

it('should handle error in dynamicsClient response', async () => {
const error = new Error('Failed to update activity')
dynamicsClient.executeUnboundAction.mockRejectedValue(error)

await expect(updateActivity('contact-identifier-123', 2024)).rejects.toThrow('Failed to update activity')
})

it('should handle the case where activity creation fails', async () => {
dynamicsClient.executeUnboundAction.mockResolvedValue(getErrorResponse())

const result = await updateActivity('invalid-contact-id', 2024)

expect(result).toMatchObject({
RCRActivityId: null,
ReturnStatus: 'error',
SuccessMessage: '',
ErrorMessage: 'Failed to update activity'
})
})
})
})
23 changes: 23 additions & 0 deletions packages/dynamics-lib/src/queries/activity.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ export const createActivity = (contactId, season) => {

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

/**
* Updates 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 updateActivity = (contactId, season) => {
const request = {
ContactId: contactId,
ActivityStatus: 'SUBMITTED',
Season: season
}

return dynamicsClient.executeUnboundAction('defra_UpdateRCRActivity', request)
}

0 comments on commit 29130b3

Please sign in to comment.