diff --git a/shared/src/business/entities/CaseMessage.js b/shared/src/business/entities/CaseMessage.js index 7ed0739b5c7..6110cf86fde 100644 --- a/shared/src/business/entities/CaseMessage.js +++ b/shared/src/business/entities/CaseMessage.js @@ -218,4 +218,10 @@ CaseMessage.prototype.markAsCompleted = function ({ message, user }) { return this; }; +CaseMessage.prototype.addAttachment = function (attachmentToAdd) { + this.attachments.push(attachmentToAdd); + + return this; +}; + module.exports = { CaseMessage }; diff --git a/shared/src/business/entities/CaseMessage.test.js b/shared/src/business/entities/CaseMessage.test.js index 8c56c97b713..8df00577e29 100644 --- a/shared/src/business/entities/CaseMessage.test.js +++ b/shared/src/business/entities/CaseMessage.test.js @@ -115,7 +115,43 @@ describe('CaseMessage', () => { createdAt: expect.anything(), isCompleted: true, }); - expect(1).toEqual(1); + }); + }); + + describe('addAttachment', () => { + it('should add the passed in attachment to the attachments array', () => { + const message = new CaseMessage( + { + caseId: '3079c990-cc6c-4b99-8fca-8e31f2d9e7a8', + caseStatus: 'General Docket - Not at Issue', + caseTitle: 'Test Petitioner', + createdAt: '2019-01-01T17:29:13.122Z', + docketNumber: '123-45', + docketNumberWithSuffix: '123-45S', + from: 'gg', + fromSection: 'petitions', + fromUserId: '6805d1ab-18d0-43ec-bafb-654e83405416', + message: 'hello world', + subject: 'hey!', + to: 'bob', + toSection: 'petitions', + toUserId: '6805d1ab-18d0-43ec-bafb-654e83405416', + }, + { applicationContext }, + ); + + message.addAttachment({ + documentId: '1f63acc7-d3f1-4115-9310-0570559a023a', + documentTitle: 'Petition', + }); + + expect(message.isValid()).toBeTruthy(); + expect(message.attachments).toMatchObject([ + { + documentId: '1f63acc7-d3f1-4115-9310-0570559a023a', + documentTitle: 'Petition', + }, + ]); }); }); }); diff --git a/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.js b/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.js index aa34f04c57a..e6409155e42 100644 --- a/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.js +++ b/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.js @@ -3,6 +3,7 @@ const { ROLE_PERMISSIONS, } = require('../../../authorization/authorizationClientService'); const { Case } = require('../../entities/cases/Case'); +const { CaseMessage } = require('../../entities/CaseMessage'); const { Document } = require('../../entities/Document'); const { UnauthorizedError } = require('../../../errors/errors'); @@ -123,5 +124,28 @@ exports.fileCourtIssuedOrderInteractor = async ({ caseToUpdate: caseEntity.validate().toRawObject(), }); + if (documentMetadata.messageId) { + const message = await applicationContext + .getPersistenceGateway() + .getCaseMessage({ + applicationContext, + caseId, + messageId: documentMetadata.messageId, + }); + + const caseMessageEntity = new CaseMessage(message, { + applicationContext, + }).validate(); + caseMessageEntity.addAttachment({ + documentId: documentEntity.documentId, + documentTitle: documentEntity.documentTitle, + }); + + await applicationContext.getPersistenceGateway().updateCaseMessage({ + applicationContext, + caseMessage: caseMessageEntity.validate().toRawObject(), + }); + } + return caseEntity.toRawObject(); }; diff --git a/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.test.js b/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.test.js index 5f2d7728bc8..e466d719fbb 100644 --- a/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.test.js +++ b/shared/src/business/useCases/courtIssuedOrder/fileCourtIssuedOrderInteractor.test.js @@ -60,6 +60,7 @@ describe('fileCourtIssuedOrderInteractor', () => { preferredTrialCity: 'Fresno, California', procedureType: 'Regular', role: ROLES.petitioner, + status: 'New', userId: 'ddd6c900-388b-4151-8014-b3378076bfb0', }; @@ -250,6 +251,55 @@ describe('fileCourtIssuedOrderInteractor', () => { ).not.toEqual(-1); }); + it('should add order document to case message if a messageId is passed in', async () => { + applicationContext.getPersistenceGateway().getCaseMessage.mockReturnValue({ + caseId: caseRecord.caseId, + caseStatus: caseRecord.status, + caseTitle: 'Petitioner', + createdAt: '2019-03-01T21:40:46.415Z', + docketNumber: caseRecord.docketNumber, + from: 'Test Petitionsclerk', + fromSection: 'petitions', + fromUserId: '4791e892-14ee-4ab1-8468-0c942ec379d2', + message: 'hey there', + messageId: 'a10d6855-f3ee-4c11-861c-c7f11cba4dff', + parentMessageId: '31687a1e-3640-42cd-8e7e-a8e6df39ce9a', + subject: 'hello', + to: 'Test Petitionsclerk2', + toSection: 'petitions', + toUserId: '449b916e-3362-4a5d-bf56-b2b94ba29c12', + }); + + await fileCourtIssuedOrderInteractor({ + applicationContext, + documentMetadata: { + caseId: caseRecord.caseId, + docketNumber: '45678-18', + documentTitle: 'Order to do anything', + documentType: 'Order', + eventCode: 'O', + messageId: '6c1fd626-c1e1-4367-bca6-e00f9ef98cf5', + signedAt: '2019-03-01T21:40:46.415Z', + signedByUserId: mockUserId, + signedJudgeName: 'Dredd', + }, + primaryDocumentFileId: 'c54ba5a9-b37b-479d-9201-067ec6e335bb', + }); + + expect( + applicationContext.getPersistenceGateway().updateCaseMessage, + ).toHaveBeenCalled(); + expect( + applicationContext.getPersistenceGateway().updateCaseMessage.mock + .calls[0][0].caseMessage.attachments, + ).toEqual([ + { + documentId: 'c54ba5a9-b37b-479d-9201-067ec6e335bb', + documentTitle: 'Order to do anything', + }, + ]); + }); + it('should throw an error if fails to parse pdf', async () => { applicationContext .getUtilities() diff --git a/shared/src/persistence/dynamo/messages/getCaseMessage.js b/shared/src/persistence/dynamo/messages/getCaseMessage.js new file mode 100644 index 00000000000..198b7c6d42e --- /dev/null +++ b/shared/src/persistence/dynamo/messages/getCaseMessage.js @@ -0,0 +1,20 @@ +const { get } = require('../../dynamodbClientService'); + +/** + * getCaseMessage + * + * @param {object} providers the providers object + * @param {object} providers.applicationContext the application context + * @param {string} providers.caseId the id of the case + * @param {string} providers.messageId the id of the message + * @returns {object} the created case message + */ +exports.getCaseMessage = async ({ applicationContext, caseId, messageId }) => { + return await get({ + Key: { + pk: `case|${caseId}`, + sk: `message|${messageId}`, + }, + applicationContext, + }); +}; diff --git a/shared/src/persistence/dynamo/messages/getCaseMessage.test.js b/shared/src/persistence/dynamo/messages/getCaseMessage.test.js new file mode 100644 index 00000000000..d3e9ef12673 --- /dev/null +++ b/shared/src/persistence/dynamo/messages/getCaseMessage.test.js @@ -0,0 +1,37 @@ +const { + applicationContext, +} = require('../../../business/test/createTestApplicationContext'); +const { getCaseMessage } = require('./getCaseMessage'); + +const mockCaseMessage = { + caseId: 'b3f09a45-b27c-4383-acc1-2ab1f99e6725', + createdAt: '2019-03-01T21:40:46.415Z', + from: 'Test Petitionsclerk', + fromSection: 'petitions', + fromUserId: '4791e892-14ee-4ab1-8468-0c942ec379d2', + message: 'hey there', + messageId: 'a10d6855-f3ee-4c11-861c-c7f11cba4dff', + subject: 'hello', + to: 'Test Petitionsclerk2', + toSection: 'petitions', + toUserId: '449b916e-3362-4a5d-bf56-b2b94ba29c12', +}; + +describe('getCaseMessage', () => { + beforeAll(() => { + applicationContext.environment.stage = 'dev'; + applicationContext.getDocumentClient().get.mockReturnValue({ + promise: () => Promise.resolve({ Item: mockCaseMessage }), + }); + }); + + it('retrieves the case message from persistence', async () => { + const retrievedMessage = await getCaseMessage({ + applicationContext, + caseId: mockCaseMessage.caseId, + messageId: mockCaseMessage.messageId, + }); + + expect(retrievedMessage).toEqual(mockCaseMessage); + }); +}); diff --git a/web-api/src/applicationContext.js b/web-api/src/applicationContext.js index 64092cfd991..f5d9da15fbd 100644 --- a/web-api/src/applicationContext.js +++ b/web-api/src/applicationContext.js @@ -383,6 +383,9 @@ const { const { getCaseInventoryReportInteractor, } = require('../../shared/src/business/useCases/caseInventoryReport/getCaseInventoryReportInteractor'); +const { + getCaseMessage, +} = require('../../shared/src/persistence/dynamo/messages/getCaseMessage'); const { getCaseMessagesByCaseId, } = require('../../shared/src/persistence/dynamo/messages/getCaseMessagesByCaseId'); @@ -1241,6 +1244,7 @@ module.exports = appContextUser => { getCaseByDocketNumber, getCaseDeadlinesByCaseId, getCaseInventoryReport, + getCaseMessage, getCaseMessageThreadByParentId, getCaseMessagesByCaseId, getCasesByCaseIds, diff --git a/web-api/storage/fixtures/seed/case-messages.json b/web-api/storage/fixtures/seed/case-messages.json index be5a55411f7..0dc7e8b5e1c 100644 --- a/web-api/storage/fixtures/seed/case-messages.json +++ b/web-api/storage/fixtures/seed/case-messages.json @@ -1,6 +1,6 @@ [ { - "caseId": "0bc59d5b-c2b9-41f7-92a7-03b8cadffcc0", + "caseId": "28caad58-df69-4a4d-af15-6554aee7fbd6", "caseStatus": "General Docket - Not at Issue", "createdAt": "2020-06-05T18:02:25.280Z", "caseTitle": "Bill Burr", @@ -13,7 +13,7 @@ "gsi1pk": "message|eb0a139a-8951-4de1-8b83-f02a27504105", "message": "hello!", "messageId": "eb0a139a-8951-4de1-8b83-f02a27504105", - "pk": "case|0bc59d5b-c2b9-41f7-92a7-03b8cadffcc0", + "pk": "case|28caad58-df69-4a4d-af15-6554aee7fbd6", "sk": "message|eb0a139a-8951-4de1-8b83-f02a27504105", "parentMessageId": "eb0a139a-8951-4de1-8b83-f02a27504105", "subject": "message to myself",