From edc7cb6fedd46469c608994c0a642af43f6d5e1a Mon Sep 17 00:00:00 2001 From: Cody Seibert Date: Mon, 10 Dec 2018 18:17:33 -0500 Subject: [PATCH 1/2] attach respondent info to case --- shared/src/business/entities/Case.js | 10 ++- shared/src/business/useCases/getCase.js | 2 +- shared/src/business/useCases/getCase.test.js | 18 +++-- shared/src/business/useCases/getUser.js | 2 +- shared/src/business/useCases/getUser.test.js | 8 +-- .../useCases/respondent/fileAnswer.js | 6 ++ .../useCases/respondent/fileAnswer.test.js | 66 ++++++++++++++++++- 7 files changed, 96 insertions(+), 16 deletions(-) diff --git a/shared/src/business/entities/Case.js b/shared/src/business/entities/Case.js index c414f4568fc..40b0cc6f1ad 100644 --- a/shared/src/business/entities/Case.js +++ b/shared/src/business/entities/Case.js @@ -6,7 +6,7 @@ const uuidVersions = { version: ['uuidv4'], }; -const docketNumberMatcher = /^\d{3,5}-\d{2}$/; +const docketNumberMatcher = /^(\d{3,5}-\d{2})$/; /** * Case * @param rawCase @@ -49,6 +49,9 @@ joiValidationDecorator( .regex(docketNumberMatcher) .required(), respondentId: joi.string().optional(), + respondentFirstName: joi.string().optional(), + respondentLastName: joi.string().optional(), + respondentBar: joi.string().optional(), irsSendDate: joi .date() .iso() @@ -144,6 +147,11 @@ Case.isValidDocketNumber = docketNumber => { ); }; +Case.stripLeadingZeros = docketNumber => { + const [number, year] = docketNumber.split('-'); + return `${parseInt(number)}-${year}`; +}; + Case.prototype.preValidate = function() { return Case.isValidDocketNumber(this.docketNumber); }; diff --git a/shared/src/business/useCases/getCase.js b/shared/src/business/useCases/getCase.js index dc20670b964..aebc932283a 100644 --- a/shared/src/business/useCases/getCase.js +++ b/shared/src/business/useCases/getCase.js @@ -27,7 +27,7 @@ exports.getCase = async ({ userId, caseId, applicationContext }) => { caseRecord = await applicationContext .getPersistenceGateway() .getCaseByDocketNumber({ - docketNumber: caseId, + docketNumber: Case.stripLeadingZeros(caseId), applicationContext, }); } diff --git a/shared/src/business/useCases/getCase.test.js b/shared/src/business/useCases/getCase.test.js index 342099e021b..408c1bab792 100644 --- a/shared/src/business/useCases/getCase.test.js +++ b/shared/src/business/useCases/getCase.test.js @@ -1,5 +1,6 @@ const assert = require('assert'); const { getCase } = require('./getCase'); +const sinon = require('sinon'); const documents = [ { @@ -82,15 +83,14 @@ describe('Get case', () => { }); it('success case by docket number', async () => { + const getCaseByDocketNumberStub = sinon.stub().resolves({ + docketNumber: '00101-00', + caseId: 'c54ba5a9-b37b-479d-9201-067ec6e335bb', + documents, + }); applicationContext = { getPersistenceGateway: () => ({ - getCaseByDocketNumber: () => { - return Promise.resolve({ - docketNumber: '00101-00', - caseId: 'c54ba5a9-b37b-479d-9201-067ec6e335bb', - documents, - }); - }, + getCaseByDocketNumber: getCaseByDocketNumberStub, }), environment: { stage: 'local' }, }; @@ -100,6 +100,10 @@ describe('Get case', () => { applicationContext, }); assert.equal(caseRecord.caseId, 'c54ba5a9-b37b-479d-9201-067ec6e335bb'); + assert.equal( + getCaseByDocketNumberStub.getCall(0).args[0].docketNumber, + '101-00', + ); }); it('failure case by docket number', async () => { diff --git a/shared/src/business/useCases/getUser.js b/shared/src/business/useCases/getUser.js index 46c54afb2aa..c2e64d85956 100644 --- a/shared/src/business/useCases/getUser.js +++ b/shared/src/business/useCases/getUser.js @@ -4,7 +4,7 @@ const User = require('../entities/User'); * @param userId * @returns {User} */ -exports.getUser = userId => { +exports.getUser = async userId => { try { return new User({ userId }); } catch (err) { diff --git a/shared/src/business/useCases/getUser.test.js b/shared/src/business/useCases/getUser.test.js index 29b8ed1d150..5a54e46c021 100644 --- a/shared/src/business/useCases/getUser.test.js +++ b/shared/src/business/useCases/getUser.test.js @@ -4,23 +4,23 @@ const { getUser } = require('./getUser'); describe('Get user', () => { it('Success taxpayer', async () => { - const user = getUser('taxpayer'); + const user = await getUser('taxpayer'); assert.equal(user.userId, 'taxpayer'); assert.equal(user.role, 'taxpayer'); }); it('Success petitionsclerk', async () => { - const user = getUser('petitionsclerk'); + const user = await getUser('petitionsclerk'); assert.equal(user.userId, 'petitionsclerk'); assert.equal(user.role, 'petitionsclerk'); }); it('Success intakeclerk', async () => { - const user = getUser('intakeclerk'); + const user = await getUser('intakeclerk'); assert.equal(user.userId, 'intakeclerk'); assert.equal(user.role, 'intakeclerk'); }); it('Failure', async () => { try { - getUser('Bad actor'); + await getUser('Bad actor'); } catch (e) { assert.equal(e.message, 'Username is incorrect'); } diff --git a/shared/src/business/useCases/respondent/fileAnswer.js b/shared/src/business/useCases/respondent/fileAnswer.js index 0e915eb952d..2ceb9b31b92 100644 --- a/shared/src/business/useCases/respondent/fileAnswer.js +++ b/shared/src/business/useCases/respondent/fileAnswer.js @@ -15,6 +15,8 @@ exports.fileAnswer = async ({ ); } + const user = await applicationContext.getUseCases().getUser(userId); + //upload to S3 return uuid const answerDocumentId = await applicationContext .getPersistenceGateway() @@ -32,6 +34,10 @@ exports.fileAnswer = async ({ const caseWithAnswer = new Case({ ...caseToUpdate, + respondentId: userId, + respondentBar: user.barNumber, + respondentFirstName: user.firstName, + respondentLastName: user.lastName, documents: [...caseToUpdate.documents, answerDocumentMetadata], }); diff --git a/shared/src/business/useCases/respondent/fileAnswer.test.js b/shared/src/business/useCases/respondent/fileAnswer.test.js index 5cbd169c921..a0c0a730a6b 100644 --- a/shared/src/business/useCases/respondent/fileAnswer.test.js +++ b/shared/src/business/useCases/respondent/fileAnswer.test.js @@ -1,4 +1,8 @@ const { fileAnswer } = require('./fileAnswer'); +const sinon = require('sinon'); +const expect = require('chai').expect; +const chai = require('chai'); +chai.use(require('chai-string')); describe('fileAnswer', () => { let applicationContext; @@ -17,8 +21,60 @@ describe('fileAnswer', () => { }, ]; + it('should attach the respondent information to the case when calling updateCase', async () => { + const updateCaseStub = sinon.stub().resolves({ + docketNumber: '00101-18', + caseId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859', + documents, + }); + applicationContext = { + getPersistenceGateway: () => { + return { + uploadDocument: () => + Promise.resolve('a6b81f4d-1e47-423a-8caf-6d2fdc3d3859'), + }; + }, + getUseCases: () => { + return { + updateCase: updateCaseStub, + getUser: () => + Promise.resolve({ + firstName: 'bob', + lastName: 'marley', + barNumber: '12345', + }), + }; + }, + environment: { stage: 'local' }, + }; + await fileAnswer({ + answerDocument: 'abc', + userId: 'respondent', + caseToUpdate: { + documents, + docketNumber: '00101-18', + caseId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859', + }, + applicationContext, + }); + expect(updateCaseStub.getCall(0).args[0].caseDetails).to.contain({ + respondentId: 'respondent', + respondentFirstName: 'bob', + respondentLastName: 'marley', + respondentBar: '12345', + }); + }); + it('throws an error is the entity returned from persistence is invalid', async () => { applicationContext = { + getUseCases: () => ({ + getUser: () => + Promise.resolve({ + firstName: 'bob', + lastName: 'marley', + barNumber: '12345', + }), + }), getPersistenceGateway: () => { return { uploadDocument: () => Promise.resolve('abc'), @@ -41,7 +97,7 @@ describe('fileAnswer', () => { } catch (err) { error = err; } - expect(error.message).toContain('problem in body or url '); + expect(error.message).to.contain('problem in body or url '); }); it('throws an error is the entity returned from persistence is invalid', async () => { @@ -59,6 +115,12 @@ describe('fileAnswer', () => { docketNumber: '00101-18', caseId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859', }), + getUser: () => + Promise.resolve({ + firstName: 'bob', + lastName: 'marley', + barNumber: '12345', + }), }; }, environment: { stage: 'local' }, @@ -78,6 +140,6 @@ describe('fileAnswer', () => { } catch (err) { error = err; } - expect(error.message).toContain('The entity was invalid'); + expect(error.message).to.contain('The entity was invalid'); }); }); From bb086bfd00d5bcff671e2755037294b634998eb5 Mon Sep 17 00:00:00 2001 From: Cody Seibert Date: Mon, 10 Dec 2018 18:30:05 -0500 Subject: [PATCH 2/2] rename to respondentBarNumber --- shared/src/business/entities/Case.js | 2 +- shared/src/business/useCases/respondent/fileAnswer.js | 2 +- shared/src/business/useCases/respondent/fileAnswer.test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shared/src/business/entities/Case.js b/shared/src/business/entities/Case.js index 40b0cc6f1ad..5fdeea31513 100644 --- a/shared/src/business/entities/Case.js +++ b/shared/src/business/entities/Case.js @@ -51,7 +51,7 @@ joiValidationDecorator( respondentId: joi.string().optional(), respondentFirstName: joi.string().optional(), respondentLastName: joi.string().optional(), - respondentBar: joi.string().optional(), + respondentBarNumber: joi.string().optional(), irsSendDate: joi .date() .iso() diff --git a/shared/src/business/useCases/respondent/fileAnswer.js b/shared/src/business/useCases/respondent/fileAnswer.js index 2ceb9b31b92..377ea062dde 100644 --- a/shared/src/business/useCases/respondent/fileAnswer.js +++ b/shared/src/business/useCases/respondent/fileAnswer.js @@ -35,7 +35,7 @@ exports.fileAnswer = async ({ const caseWithAnswer = new Case({ ...caseToUpdate, respondentId: userId, - respondentBar: user.barNumber, + respondentBarNumber: user.barNumber, respondentFirstName: user.firstName, respondentLastName: user.lastName, documents: [...caseToUpdate.documents, answerDocumentMetadata], diff --git a/shared/src/business/useCases/respondent/fileAnswer.test.js b/shared/src/business/useCases/respondent/fileAnswer.test.js index a0c0a730a6b..e4516228cfc 100644 --- a/shared/src/business/useCases/respondent/fileAnswer.test.js +++ b/shared/src/business/useCases/respondent/fileAnswer.test.js @@ -61,7 +61,7 @@ describe('fileAnswer', () => { respondentId: 'respondent', respondentFirstName: 'bob', respondentLastName: 'marley', - respondentBar: '12345', + respondentBarNumber: '12345', }); });