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

Devex 1187: PractitionerDocument #4167

Merged
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
8 changes: 7 additions & 1 deletion shared/src/business/entities/PractitionerDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PRACTITIONER_DOCUMENT_TYPES_MAP } from './EntityConstants';
import { PractitionerDocument } from './PractitionerDocument';
import { applicationContext } from '../test/createTestApplicationContext';

describe('Document', () => {
describe('PractitionerDocument', () => {
it('should create a valid document when passed all required fields', () => {
const document = new PractitionerDocument(
{
Expand All @@ -13,6 +13,7 @@ describe('Document', () => {
},
{ applicationContext },
);

expect(document.isValid()).toBeTruthy();
});

Expand All @@ -26,6 +27,7 @@ describe('Document', () => {
},
{ applicationContext },
);

expect(document.isValid()).toBeFalsy();
});

Expand All @@ -39,6 +41,7 @@ describe('Document', () => {
},
{ applicationContext },
);

expect(document.isValid()).toBeFalsy();
});

Expand All @@ -54,6 +57,7 @@ describe('Document', () => {
},
{ applicationContext },
);

expect(document.isValid()).toBeFalsy();
});

Expand All @@ -69,6 +73,7 @@ describe('Document', () => {
},
{ applicationContext },
);

expect(document.isValid()).toBeTruthy();
});

Expand All @@ -85,6 +90,7 @@ describe('Document', () => {
},
{ applicationContext },
);

expect(document.isValid()).toBeTruthy();
});
});
78 changes: 37 additions & 41 deletions shared/src/business/entities/PractitionerDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JoiValidationConstants } from './JoiValidationConstants';
import { JoiValidationEntity } from './JoiValidationEntity';
import { JoiValidationEntity_New } from '@shared/business/entities/joiValidationEntity/JoiValidationEntity_New';
import {
MAX_PRACTITIONER_DOCUMENT_DESCRIPTION_CHARACTERS,
PRACTITIONER_DOCUMENT_TYPES,
Expand All @@ -8,63 +8,59 @@ import {
import { createISODateString } from '../utilities/DateHandler';
import joi from 'joi';

export class PractitionerDocument extends JoiValidationEntity {
public categoryType: string;
export class PractitionerDocument extends JoiValidationEntity_New {
public categoryName: string;
public location: string;
public practitionerDocumentFileId: string;
public categoryType: string;
public description: string;
public fileName: string;
public location: string;
public practitionerDocumentFileId: string;
public uploadDate: string;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(rawDocument, { applicationContext }) {
super('Document');
this.categoryType = rawDocument.categoryType;

this.categoryName = rawDocument.categoryName;
this.categoryType = rawDocument.categoryType;
this.description = rawDocument.description;
this.fileName = rawDocument.fileName;
this.location = rawDocument.location;
this.practitionerDocumentFileId =
rawDocument.practitionerDocumentFileId ??
applicationContext.getUniqueId();
this.description = rawDocument.description;
this.fileName = rawDocument.fileName;
this.uploadDate = rawDocument.uploadDate || createISODateString();
}

getErrorToMessageMap() {
return {
categoryName: 'Enter a category name',
categoryType: 'Enter a category type',
location: 'Enter a location',
};
}
static VALIDATION_RULES = {
categoryName: JoiValidationConstants.STRING.required().messages({
'*': 'Enter a category name',
}),
categoryType: JoiValidationConstants.STRING.valid(
...Object.values(PRACTITIONER_DOCUMENT_TYPES),
)
.required()
.messages({ '*': 'Enter a category type' }),
description: JoiValidationConstants.STRING.max(
MAX_PRACTITIONER_DOCUMENT_DESCRIPTION_CHARACTERS,
)
.optional()
.allow(''),
fileName: JoiValidationConstants.STRING.required(),
location: JoiValidationConstants.STRING.when('categoryType', {
is: PRACTITIONER_DOCUMENT_TYPES_MAP.CERTIFICATE_OF_GOOD_STANDING,
otherwise: joi.optional().allow(null),
then: joi.required(),
}).messages({ '*': 'Enter a location' }),
practitionerDocumentFileId:
JoiValidationConstants.UUID.required().description(
'System-generated unique ID for the documents. If the document is associated with a document in S3, this is also the S3 document key.',
),
uploadDate: JoiValidationConstants.ISO_DATE.required(),
};

getValidationRules() {
return {
categoryName: JoiValidationConstants.STRING.required(),
categoryType: JoiValidationConstants.STRING.valid(
...Object.values(PRACTITIONER_DOCUMENT_TYPES),
).required(),
description: JoiValidationConstants.STRING.max(
MAX_PRACTITIONER_DOCUMENT_DESCRIPTION_CHARACTERS,
)
.optional()
.allow(''),
fileName: JoiValidationConstants.STRING.required(),
location: JoiValidationConstants.STRING.when('categoryType', {
is: PRACTITIONER_DOCUMENT_TYPES_MAP.CERTIFICATE_OF_GOOD_STANDING,
otherwise: joi.optional().allow(null),
then: joi.required(),
}),
practitionerDocumentFileId:
JoiValidationConstants.UUID.required().description(
'System-generated unique ID for the documents. If the document is associated with a document in S3, this is also the S3 document key.',
),
uploadDate: JoiValidationConstants.ISO_DATE.required(),
};
return PractitionerDocument.VALIDATION_RULES;
}
}

declare global {
type RawPractitionerDocument = ExcludeMethods<PractitionerDocument>;
}
export type RawPractitionerDocument = ExcludeMethods<PractitionerDocument>;
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ describe('deleteCorrespondenceById', () => {
applicationContext,
},
);
expect(myCase.correspondence.length).toEqual(1);

expect(myCase.correspondence!.length).toEqual(1);

myCase.deleteCorrespondenceById({
correspondenceId: mockCorrespondence.correspondenceId,
});
expect(myCase.correspondence.length).toEqual(0);

expect(myCase.correspondence!.length).toEqual(0);
expect(
myCase.correspondence.find(
myCase.correspondence!.find(
d => d.correspondenceId === mockCorrespondence.correspondenceId,
),
).toBeUndefined();
Expand All @@ -37,10 +40,10 @@ describe('deleteCorrespondenceById', () => {
applicationContext,
},
);
expect(myCase.correspondence.length).toEqual(1);
expect(myCase.correspondence!.length).toEqual(1);
myCase.deleteCorrespondenceById({
correspondenceId: '1234',
});
expect(myCase.correspondence.length).toEqual(1);
expect(myCase.correspondence!.length).toEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { applicationContext } from '../../test/createTestApplicationContext';
import { validateAddPractitionerDocumentFormInteractor } from './validateAddPractitionerDocumentFormInteractor';

describe('validateAddPractitionerInteractor', () => {
it('returns the expected errors object on an empty form', () => {
it('should return validations errors when the form is empty', () => {
const errors = validateAddPractitionerDocumentFormInteractor(
applicationContext,
{
Expand All @@ -13,14 +13,14 @@ describe('validateAddPractitionerInteractor', () => {
},
);

expect(Object.keys(errors)).toEqual([
'categoryName',
'categoryType',
'fileName',
]);
expect(errors).toEqual({
categoryName: expect.anything(),
categoryType: expect.anything(),
fileName: expect.anything(),
});
});

it('should return null when the practitioner object is valid', () => {
it('should return null when the practitioner document is valid', () => {
const errors = validateAddPractitionerDocumentFormInteractor(
applicationContext,
{
Expand All @@ -33,7 +33,7 @@ describe('validateAddPractitionerInteractor', () => {
expect(errors).toBeNull();
});

it('should return null when then form data for good standing documents is valid', () => {
it('should return null when the form data for a good standing document is valid', () => {
const errors = validateAddPractitionerDocumentFormInteractor(
applicationContext,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { PractitionerDocument } from '../../entities/PractitionerDocument';
import { TValidationError } from '@shared/business/entities/joiValidationEntity/helper';

/**
* validateAddPractitionerDocumentFormInteractor
*
* @param {object} applicationContext the application context
* @param {object} providers the providers object
* @param {object} providers.stampMotionForm the stamp motion form
* @returns {object} errors if there are any, otherwise null
*/
export const validateAddPractitionerDocumentFormInteractor = (
applicationContext,
applicationContext: IApplicationContext,
form,
) => {
const errors = new PractitionerDocument(
): TValidationError | null => {
return new PractitionerDocument(
{
categoryName: form.categoryName,
categoryType: form.categoryType,
Expand All @@ -22,7 +15,5 @@ export const validateAddPractitionerDocumentFormInteractor = (
{
applicationContext,
},
).getFormattedValidationErrors();

return errors || null;
).getValidationErrors();
};