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

PSP-7002 : H0443: Show Acq team (organization) on document #3533

Merged
merged 7 commits into from
Oct 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useApiContacts } from '@/hooks/pims-api/useApiContacts';
import { useAcquisitionProvider } from '@/hooks/repositories/useAcquisitionProvider';
import { useProperties } from '@/hooks/repositories/useProperties';
import { mockAcquisitionFileResponse } from '@/mocks/acquisitionFiles.mock';
import { getMockPerson } from '@/mocks/contacts.mock';
import { getMockOrganization } from '@/mocks/organization.mock';
import { Api_AcquisitionFile } from '@/models/api/AcquisitionFile';
import { Api_Property } from '@/models/api/Property';

Expand All @@ -18,6 +20,7 @@ const getPropertiesFn = jest.fn<Api_Property[], any[]>();
const generateFn = jest.fn();
const getAcquisitionFileFn = jest.fn<Api_AcquisitionFile | undefined, any[]>();
const getPersonConceptFn = jest.fn();
const getOrganizationConceptFn = jest.fn();

jest.mock('@/features/documents/hooks/useDocumentGenerationRepository');
(useDocumentGenerationRepository as jest.Mock).mockImplementation(() => ({
Expand All @@ -37,6 +40,7 @@ jest.mock('@/hooks/repositories/useProperties');
jest.mock('@/hooks/pims-api/useApiContacts');
(useApiContacts as jest.Mock).mockImplementation(() => ({
getPersonConcept: getPersonConceptFn,
getOrganizationConcept: getOrganizationConceptFn,
}));

let currentStore: MockStoreEnhanced<any, {}>;
Expand Down Expand Up @@ -67,13 +71,15 @@ const setup = (params?: { storeValues?: any; acquisitionResponse?: Api_Acquisiti
describe('useGenerateH0443 functions', () => {
it('makes requests to expected api endpoints', async () => {
const generate = setup();
await act(async () => {
await generate(0);
expect(generateFn).toHaveBeenCalled();
});
await act(async () => await generate(0));

expect(generateFn).toHaveBeenCalled();
});

it('makes requests to expected api endpoints for each required team member', async () => {
const organizationPersonMock = getMockPerson({ id: 3, firstName: 'JONH', surname: 'Doe' });
getPersonConceptFn.mockResolvedValue(Promise.resolve({ data: organizationPersonMock }));

const responseWithTeam: Api_AcquisitionFile = {
...mockAcquisitionFileResponse(),
acquisitionTeam: [
Expand All @@ -95,11 +101,122 @@ describe('useGenerateH0443 functions', () => {
};
const generate = setup({ acquisitionResponse: responseWithTeam });

await act(async () => {
await generate(0);
expect(generateFn).toHaveBeenCalled();
expect(getPersonConceptFn).toHaveBeenCalledTimes(2);
});
await act(async () => await generate(0));

expect(generateFn).toHaveBeenCalled();
expect(getPersonConceptFn).toHaveBeenCalledTimes(2);
});

it('makes requests to Organization when Property Coordinator is an organization with a Primary Contact', async () => {
const organizationPersonMock = getMockPerson({ id: 3, firstName: 'JONH', surname: 'Doe' });
getPersonConceptFn.mockResolvedValue(Promise.resolve({ data: organizationPersonMock }));

const organizationMock = getMockOrganization();
getOrganizationConceptFn.mockResolvedValue({ data: organizationMock });

const responseWithTeam: Api_AcquisitionFile = {
...mockAcquisitionFileResponse(),
acquisitionTeam: [
{
id: 1,
acquisitionFileId: 1,
teamProfileTypeCode: 'PROPCOORD',
rowVersion: 2,
organizationId: 100,
primaryContactId: 3,
},
],
};
const generate = setup({ acquisitionResponse: responseWithTeam });

await act(async () => await generate(0));

expect(generateFn).toHaveBeenCalled();
expect(getPersonConceptFn).toHaveBeenLastCalledWith(3);
expect(getOrganizationConceptFn).toHaveBeenCalledTimes(1);
});

it('makes requests to Organization when Property Coordinator is an organization and no Primary Contact', async () => {
const organizationPersonMock = getMockPerson({ id: 3, firstName: 'JONH', surname: 'Doe' });
getPersonConceptFn.mockResolvedValue(Promise.resolve({ data: organizationPersonMock }));

const organizationMock = getMockOrganization();
getOrganizationConceptFn.mockResolvedValue(Promise.resolve({ data: organizationMock }));

const responseWithTeam: Api_AcquisitionFile = {
...mockAcquisitionFileResponse(),
acquisitionTeam: [
{
id: 1,
acquisitionFileId: 1,
teamProfileTypeCode: 'PROPCOORD',
rowVersion: 2,
organizationId: 100,
},
],
};
const generate = setup({ acquisitionResponse: responseWithTeam });

await act(async () => await generate(0));

expect(generateFn).toHaveBeenCalled();
expect(getOrganizationConceptFn).toHaveBeenCalledTimes(1);
expect(getPersonConceptFn).not.toHaveBeenCalled();
});

it('makes requests to Organization when Property Agent is an organization with Primary Contact', async () => {
const organizationMock = getMockOrganization();
const organizationPersonMock = getMockPerson({ id: 3, firstName: 'JONH', surname: 'Doe' });
getOrganizationConceptFn.mockResolvedValue(Promise.resolve({ data: organizationMock }));
getPersonConceptFn.mockResolvedValue(Promise.resolve({ data: organizationPersonMock }));

const responseWithTeam: Api_AcquisitionFile = {
...mockAcquisitionFileResponse(),
acquisitionTeam: [
{
id: 2,
acquisitionFileId: 1,
teamProfileTypeCode: 'PROPAGENT',
organizationId: 2,
primaryContactId: 3,
rowVersion: 2,
},
],
};
const generate = setup({ acquisitionResponse: responseWithTeam });

await act(async () => generate(0));

expect(generateFn).toHaveBeenCalled();
expect(getOrganizationConceptFn).toHaveBeenCalledTimes(1);
expect(getPersonConceptFn).toHaveBeenCalledWith(3);
});

it('makes requests to Organization when Property Agent is an organization with no Primary Contact', async () => {
const organizationMock = getMockOrganization();
const organizationPersonMock = getMockPerson({ id: 3, firstName: 'JONH', surname: 'Doe' });
getOrganizationConceptFn.mockResolvedValue(Promise.resolve({ data: organizationMock }));
getPersonConceptFn.mockResolvedValue(Promise.resolve({ data: organizationPersonMock }));

const responseWithTeam: Api_AcquisitionFile = {
...mockAcquisitionFileResponse(),
acquisitionTeam: [
{
id: 2,
acquisitionFileId: 1,
teamProfileTypeCode: 'PROPAGENT',
organizationId: 2,
rowVersion: 2,
},
],
};
const generate = setup({ acquisitionResponse: responseWithTeam });

await act(async () => await generate(0));

expect(generateFn).toHaveBeenCalled();
expect(getOrganizationConceptFn).toHaveBeenCalledTimes(1);
expect(getPersonConceptFn).not.toHaveBeenCalled();
});

it('makes requests to expected api endpoints if there are properties', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Api_GeneratePerson } from '@/models/generate/GeneratePerson';
import { Api_GenerateProperty } from '@/models/generate/GenerateProperty';

export const useGenerateH0443 = () => {
const { getPersonConcept } = useApiContacts();
const { getPersonConcept, getOrganizationConcept } = useApiContacts();
const {
getAcquisitionFile: { execute: getAcquisitionFile },
} = useAcquisitionProvider();
Expand All @@ -29,19 +29,12 @@ export const useGenerateH0443 = () => {
const propertyCoordinator = file.acquisitionTeam?.find(
team => team.teamProfileTypeCode === 'PROPCOORD',
);
const coordinatorPerson = !!propertyCoordinator?.personId
? (await getPersonConcept(propertyCoordinator?.personId))?.data
: null;

// Retrieve Property Agent
const propertyAgent = file.acquisitionTeam?.find(
team => team.teamProfileTypeCode === 'PROPAGENT',
);

const agentPerson = !!propertyAgent?.personId
? (await getPersonConcept(propertyAgent?.personId))?.data
: null;

// Retrieve Properties
const filePropertiesIds =
file.fileProperties?.map(fp => fp.propertyId).filter((p): p is number => !!p) || [];
Expand All @@ -62,10 +55,57 @@ export const useGenerateH0443 = () => {
: [],
owner_names: owners.map<string>(x => getOwnerName(x)) || [],
owner_contact: contactOwner !== undefined ? new Api_GenerateOwner(contactOwner) : null,
property_coordinator: new Api_GeneratePerson(coordinatorPerson),
property_agent: new Api_GeneratePerson(agentPerson),
property_coordinator: null,
property_agent: null,
};

// Get the property coordinator by checking if it's a person or an org.
if (propertyCoordinator) {
if (propertyCoordinator.personId) {
const personConceptResponse = await getPersonConcept(propertyCoordinator?.personId);

h0443Data.property_coordinator = new Api_GeneratePerson(personConceptResponse.data);
} else if (propertyCoordinator.organizationId) {
const organizationConceptResponse = await getOrganizationConcept(
propertyCoordinator?.organizationId,
);

if (
Copy link
Collaborator

Choose a reason for hiding this comment

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

@eddherrera for this one, it seems like we don't set a property coordinator for an org unless there is a primary contact. Is that true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct: It was specified as an acceptance criteria.

If for an applicable case primary contact is not provided leave relevant fields blank

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that seemed odd to me as well when reviewing this PR because it is a different pattern of what we usually do, but JIRA was very clear about what they want...
image

Perhaps adding a comment to avoid future confusion?

organizationConceptResponse &&
organizationConceptResponse?.data &&
organizationConceptResponse?.data.organizationPersons?.length &&
propertyCoordinator.primaryContactId
) {
const personResponse = await getPersonConcept(propertyCoordinator.primaryContactId);

h0443Data.property_coordinator = new Api_GeneratePerson(personResponse.data);
}
}
}

// Get the property agent by checking if it's a person or an org.
if (propertyAgent) {
if (propertyAgent.personId) {
const personResponse = await getPersonConcept(propertyAgent?.personId);

h0443Data.property_agent = new Api_GeneratePerson(personResponse.data);
} else if (propertyAgent.organizationId) {
const organizationConceptResponse = await getOrganizationConcept(
propertyAgent?.organizationId,
);

if (
organizationConceptResponse.data &&
organizationConceptResponse?.data.organizationPersons?.length &&
propertyAgent.primaryContactId
) {
const personResponse = await getPersonConcept(propertyAgent.primaryContactId);

h0443Data.property_agent = new Api_GeneratePerson(personResponse.data);
}
}
}

const generatedFile = await generate({
templateType: FormDocumentType.H0443,
templateData: h0443Data,
Expand Down Expand Up @@ -102,6 +142,6 @@ interface H0443Data {
project_number: string;
project_name: string;
properties: Api_GenerateProperty[];
property_agent: Api_GeneratePerson;
property_coordinator: Api_GeneratePerson;
property_agent: Api_GeneratePerson | null;
property_coordinator: Api_GeneratePerson | null;
}
2 changes: 2 additions & 0 deletions source/frontend/src/mocks/acquisitionFiles.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export const mockAcquisitionFileResponse = (
{
id: 1,
acquisitionFileId: 1,
personId: 1,
person: {
id: 1,
isDisabled: false,
Expand All @@ -166,6 +167,7 @@ export const mockAcquisitionFileResponse = (
{
id: 2,
acquisitionFileId: 1,
personId: 3,
person: {
id: 3,
isDisabled: false,
Expand Down
Loading