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

CA Task Id 37 - Validate Response from Persistence Layer #282

Merged
merged 14 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from 8 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
6 changes: 3 additions & 3 deletions efcms-service/src/cases/createCase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Create case lambda', function() {
beforeEach(function() {
sinon.stub(client, 'put').resolves({
userId: 'userId',
docketNumber: '456789-18',
docketNumber: '56789-18',
documents,
caseId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
createdAt: '',
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('Create case lambda', function() {
beforeEach(function() {
sinon.stub(client, 'put').resolves({
userId: 'userId',
docketNumber: '456789-18',
docketNumber: '56789-18',
documents: documents,
createdAt: '',
});
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('Create case lambda', function() {
return lambdaTester(createCase.create)
.event(post)
.expectResolve(err => {
expect(err.body).to.startsWith('"The case was invalid');
expect(err.body).to.startsWith('"The entity was invalid');
});
});
});
Expand Down
19 changes: 17 additions & 2 deletions efcms-service/src/cases/getCase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@ describe('Get case lambda', function() {
const MOCK_CASE = {
userId: 'userId',
caseId: 'c54ba5a9-b37b-479d-9201-067ec6e335bb',
docketNumber: '456789-18',
createdAt: '',
docketNumber: '56789-18',
status: 'new',
createdAt: new Date().toISOString(),
documents: [
{
documentId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
documentType: 'Petition',
},
{
documentId: 'b6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
documentType: 'Petition',
},
{
documentId: 'c6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
documentType: 'Petition',
},
]
};

describe('success - no cases exist in database', function() {
Expand Down
16 changes: 8 additions & 8 deletions efcms-service/src/cases/getCases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ chai.use(require('chai-string'));
describe('Get cases lambda', function() {
const documents = [
{
documentId: '123456789',
documentType: 'stin',
documentId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
documentType: 'Petition',
},
{
documentId: '123456780',
documentType: 'stin',
documentId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
documentType: 'Petition',
},
{
documentId: '123456781',
documentType: 'stin',
documentId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
documentType: 'Petition',
},
];

Expand All @@ -33,8 +33,8 @@ describe('Get cases lambda', function() {
sinon.stub(client, 'batchGet').resolves([
{
userId: 'userId',
caseId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859A',
docketNumber: '456789-18',
caseId: 'a6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
docketNumber: '56789-18',
documents,
createdAt: '',
},
Expand Down
176 changes: 74 additions & 102 deletions shared/src/business/entities/Case.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,11 @@
const { joiValidationDecorator } = require('./JoiValidationDecorator');
const joi = require('joi-browser');
const uuidv4 = require('uuid/v4');

const uuidVersions = {
version: ['uuidv4'],
};

/**
* schema definition
*/
const caseSchema = joi.object().keys({
caseId: joi
.string()
.uuid(uuidVersions)
.optional(),
userId: joi
.string()
// .uuid(uuidVersions)
.optional(),
createdAt: joi
.date()
.iso()
.optional(),
docketNumber: joi
.string()
.regex(/^[0-9]{5}-[0-9]{2}$/)
.optional(),
respondentId: joi.string().optional(),
irsSendDate: joi
.date()
.iso()
.optional(),
payGovId: joi.string().optional(),
payGovDate: joi
.date()
.iso()
.optional(),
status: joi
.string()
.regex(/^(new)|(general)$/)
.optional(),
documents: joi
.array()
.min(3)
.items(
joi.object({
documentId: joi
.string()
.uuid(uuidVersions)
.required(),
userId: joi
.string()
// .uuid(uuidVersions)
.optional(),
documentType: joi.string().required(),
validated: joi.boolean().optional(),
reviewDate: joi
.date()
.iso()
.optional(),
reviewUser: joi.string().optional(),
status: joi.string().optional(),
servedDate: joi
.date()
.iso()
.optional(),
createdAt: joi
.date()
.iso()
.optional(),
}),
)
.required(),
});

/**
* Case
* @param rawCase
Expand All @@ -95,40 +28,73 @@ function Case(rawCase) {
);
}

/**
* isValid
* @returns {boolean}
*/
Case.prototype.isValid = function isValid() {
return joi.validate(this, caseSchema).error === null;
};
/**
* getValidationError
* @returns {*}
*/
Case.prototype.getValidationError = function getValidationError() {
return joi.validate(this, caseSchema).error;
};

/**
* validate
*/
Case.prototype.validate = function validate() {
if (!this.isValid()) {
throw new Error('The case was invalid ' + this.getValidationError());
}
};

/**
* validateWithError
* will throw the error provided if the case entity is invalid
*/
Case.prototype.validateWithError = function validate(error) {
if (!this.isValid()) {
error.message = `${error.message} ${this.getValidationError()}`;
throw error;
}
};
joiValidationDecorator(
Case,
joi.object().keys({
caseId: joi
.string()
.uuid(uuidVersions)
.optional(),
userId: joi
.string()
// .uuid(uuidVersions)
.optional(),
createdAt: joi
.date()
.iso()
.optional(),
docketNumber: joi
.string()
.regex(/^[0-9]{5}-[0-9]{2}$/)
.required(),
respondentId: joi.string().optional(),
irsSendDate: joi
.date()
.iso()
.optional(),
payGovId: joi.string().optional(),
payGovDate: joi
.date()
.iso()
.optional(),
status: joi
.string()
.regex(/^(new)|(general)$/)
.optional(),
documents: joi
.array()
.min(3)
.items(
joi.object({
documentId: joi
.string()
.uuid(uuidVersions)
.required(),
userId: joi
.string()
// .uuid(uuidVersions)
.optional(),
documentType: joi.string().required(),
validated: joi.boolean().optional(),
reviewDate: joi
.date()
.iso()
.optional(),
reviewUser: joi.string().optional(),
status: joi.string().optional(),
servedDate: joi
.date()
.iso()
.optional(),
createdAt: joi
.date()
.iso()
.optional(),
}),
)
.required(),
}),
);

/**
* isPetitionPackageReviewed
Expand All @@ -145,10 +111,12 @@ Case.prototype.markAsSentToIRS = function(sendDate) {
this.irsSendDate = sendDate;
this.status = 'general';
this.documents.every(document => (document.status = 'served'));
return this;
};

Case.prototype.markAsPaidByPayGov = function(payGovDate) {
this.payGovDate = payGovDate;
return this;
};

/**
Expand All @@ -174,6 +142,10 @@ Case.isValidDocketNumber = docketNumber => {
);
};

Case.prototype.preValidate = function() {
return Case.isValidDocketNumber(this.docketNumber);
};

/**
* documentTypes
* @type {{petitionFile: string, requestForPlaceOfTrial: string, statementOfTaxpayerIdentificationNumber: string}}
Expand Down
3 changes: 3 additions & 0 deletions shared/src/business/entities/Case.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const assert = require('assert');
const Case = require('./Case');

const A_VALID_CASE = {
docketNumber: '00101-18',

Choose a reason for hiding this comment

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

We've updated the docket number pattern to remove leading zeroes. Although these tests will probably pass, you may want to adjust based on the very-recently-merged code.

documents: [
{
documentId: 'c6b81f4d-1e47-423a-8caf-6d2fdc3d3859',
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('Case entity', () => {
caseId: '241edd00-1d94-40cd-9374-8d1bc7ae6d7b',
createdAt: '2018-11-21T20:58:28.192Z',
status: 'new',
docketNumber: '00101-18',
documents: A_VALID_CASE.documents,
};
const myCase = new Case(previouslyCreatedCase);
Expand All @@ -59,6 +61,7 @@ describe('Case entity', () => {
status: 'new',
documents: A_VALID_CASE.documents,
payGovId: '1234',
docketNumber: '00101-18',
};
const myCase = new Case(previouslyCreatedCase);
assert.ok(myCase.isValid());
Expand Down
31 changes: 12 additions & 19 deletions shared/src/business/entities/CaseInitiator.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
const { joiValidationDecorator } = require('./JoiValidationDecorator');
const joi = require('joi-browser');

/**
* constructor
* @param rawCaseInitiator
* @constructor
*/
function CaseInitiator(rawCaseInitiator) {
this.petitionFile = rawCaseInitiator.petitionFile;
this.requestForPlaceOfTrial = rawCaseInitiator.requestForPlaceOfTrial;
this.statementOfTaxpayerIdentificationNumber =
rawCaseInitiator.statementOfTaxpayerIdentificationNumber;
Object.assign(this, rawCaseInitiator);
}

/**
* isValid
* @returns {boolean}
*/
CaseInitiator.prototype.isValid = function isValid() {
return (
!!this.petitionFile &&
!!this.requestForPlaceOfTrial &&
!!this.statementOfTaxpayerIdentificationNumber
);
};

CaseInitiator.prototype.exportObject = function exportObject() {
return Object.assign({}, this);
};
joiValidationDecorator(
CaseInitiator,
joi.object().keys({
petitionFile: joi.object().required(),
requestForPlaceOfTrial: joi.object().required(),
statementOfTaxpayerIdentificationNumber: joi.object().required(),
}),
);

module.exports = CaseInitiator;
7 changes: 7 additions & 0 deletions shared/src/business/entities/CaseInitiator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ describe('Petition entity', () => {
});
assert.ok(!caseDetail.isValid());
});
it('Creates an invalid petition', () => {
const caseDetail = new CaseInitiator({
requestForPlaceOfTrial: new Blob(['blob']),
statementOfTaxpayerIdentificationNumber: undefined,
});
assert.ok(!caseDetail.isValid());
});
});
Loading