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-9905 Error thrown when opening compensation requisition with legacy payee/null payee #4621

Merged
merged 5 commits into from
Jan 30, 2025
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 @@ -5,7 +5,7 @@ import { ApiGen_Concepts_CompensationRequisition } from '@/models/api/generated/
import { ApiGen_Concepts_CompReqPayee } from '@/models/api/generated/ApiGen_Concepts_CompReqPayee';
import { ApiGen_Concepts_InterestHolder } from '@/models/api/generated/ApiGen_Concepts_InterestHolder';
import { getEmptyBaseAudit } from '@/models/defaultInitializers';
import { exists, truncateName } from '@/utils';
import { exists, isValidId, truncateName } from '@/utils';
import { formatApiPersonNames } from '@/utils/personUtils';

import { PayeeType } from './PayeeTypeModel';
Expand Down Expand Up @@ -70,18 +70,20 @@ export class PayeeOption {

public static fromApi(compReqPayee: ApiGen_Concepts_CompReqPayee): PayeeOption {
let payee: PayeeOption = null;
if (compReqPayee.acquisitionOwnerId) {
if (isValidId(compReqPayee.acquisitionOwnerId)) {
payee = PayeeOption.createOwner(compReqPayee.acquisitionOwner, compReqPayee);
} else if (compReqPayee.acquisitionFileTeamId) {
} else if (isValidId(compReqPayee.acquisitionFileTeamId)) {
payee = PayeeOption.createTeamMember(compReqPayee.acquisitionFileTeam, compReqPayee);
} else if (compReqPayee.interestHolderId) {
} else if (isValidId(compReqPayee.interestHolderId)) {
payee = PayeeOption.createInterestHolder(compReqPayee.interestHolder, compReqPayee);
}
payee.rowVersion = compReqPayee.rowVersion;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this was one of the sources of NPE - when all payees are null then this variable is null so it cannot take an assignment of "rowVersion"

if (exists(payee)) {
payee.rowVersion = compReqPayee.rowVersion;
}
return payee;
}

public toApi(): ApiGen_Concepts_CompReqPayee {
public toApi(): ApiGen_Concepts_CompReqPayee | null {
const compReqPayeeModel: ApiGen_Concepts_CompReqPayee = {
...getEmptyBaseAudit(),
compensationRequisitionId: this.compensationRequisitionId,
Expand Down Expand Up @@ -110,7 +112,15 @@ export class PayeeOption {
break;
}

return compReqPayeeModel;
if (
isValidId(compReqPayeeModel.acquisitionFileTeamId) ||
isValidId(compReqPayeeModel.interestHolderId) ||
isValidId(compReqPayeeModel.acquisitionOwnerId)
) {
return compReqPayeeModel;
} else {
return null;
}
}

public static createOwner(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
import { useGenerateH120 } from '@/features/mapSideBar/acquisition/common/GenerateForm/hooks/useGenerateH120';
import { useApiContacts } from '@/hooks/pims-api/useApiContacts';
import {
mockAcquisitionFileResponse,
mockApiAcquisitionFileTeamOrganization,
mockApiAcquisitionFileTeamPerson,
} from '@/mocks/acquisitionFiles.mock';
import { useCompensationRequisitionRepository } from '@/hooks/repositories/useRequisitionCompensationRepository';
import { mockAcquisitionFileResponse } from '@/mocks/acquisitionFiles.mock';
import {
getMockApiDefaultCompensation,
getMockCompensationPropertiesReq,
} from '@/mocks/compensations.mock';
import { getEmptyPerson } from '@/mocks/contacts.mock';
import { emptyApiInterestHolder } from '@/mocks/interestHolder.mock';
import { getEmptyOrganization, getMockOrganization } from '@/mocks/organization.mock';
import { ApiGen_Concepts_InterestHolder } from '@/models/api/generated/ApiGen_Concepts_InterestHolder';
import { getMockOrganization } from '@/mocks/organization.mock';
import { ApiGen_CodeTypes_FileTypes } from '@/models/api/generated/ApiGen_CodeTypes_FileTypes';
import { ApiGen_Concepts_Person } from '@/models/api/generated/ApiGen_Concepts_Person';
import { getMockRepositoryObj, render, RenderOptions, waitForEffects } from '@/utils/test-utils';
import {
act,
getMockRepositoryObj,
render,
RenderOptions,
waitForEffects,
} from '@/utils/test-utils';

import {
CompensationRequisitionDetailContainer,
CompensationRequisitionDetailContainerProps,
} from './CompensationRequisitionDetailContainer';
import { CompensationRequisitionDetailViewProps } from './CompensationRequisitionDetailView';
import { ApiGen_CodeTypes_FileTypes } from '@/models/api/generated/ApiGen_CodeTypes_FileTypes';

vi.mock('@/hooks/pims-api/useApiContacts');
vi.mock('@/hooks/repositories/useRequisitionCompensationRepository');
vi.mock('@/features/mapSideBar/acquisition/common/GenerateForm/hooks/useGenerateH120');

const getPersonConceptFn = vi.fn();
const getOrganizationConceptFn = vi.fn();
const setEditMode = vi.fn();
const onGenerate = vi.fn();

const mockGetCompReqPropertiesApi = getMockRepositoryObj();
const mockPutCompReqPropertiesApi = getMockRepositoryObj();
const mockGetCompReqPayeesApi = getMockRepositoryObj([]);

vi.mock('@/hooks/repositories/useRequisitionCompensationRepository', () => ({
useCompensationRequisitionRepository: () => {
return {
getCompensationRequisitionProperties: mockGetCompReqPropertiesApi,
getCompensationRequisitionPayees: mockGetCompReqPropertiesApi,
};
},
}));

vi.mocked(useApiContacts).mockImplementation(
() =>
({
getPersonConcept: getPersonConceptFn,
getOrganizationConcept: getOrganizationConceptFn,
} as unknown as ReturnType<typeof useApiContacts>),
);

let viewProps: CompensationRequisitionDetailViewProps;
const CompensationViewComponent = (props: CompensationRequisitionDetailViewProps) => {
vi.mocked(useCompensationRequisitionRepository, { partial: true }).mockReturnValue({
getCompensationRequisitionProperties: mockGetCompReqPropertiesApi,
getCompensationRequisitionPayees: mockGetCompReqPayeesApi,
});

vi.mocked(useApiContacts, { partial: true }).mockReturnValue({
getPersonConcept: getPersonConceptFn,
getOrganizationConcept: getOrganizationConceptFn,
});

vi.mocked(useGenerateH120).mockReturnValue(onGenerate);

let viewProps: CompensationRequisitionDetailViewProps | undefined;

const TestView = (props: CompensationRequisitionDetailViewProps) => {
viewProps = props;
return <></>;
return <>Content Rendered</>;
};

describe('Compensation Detail View container', () => {
const setup = (
renderOptions: RenderOptions & { props?: Partial<CompensationRequisitionDetailContainerProps> },
const setup = async (
renderOptions: RenderOptions & {
props?: Partial<CompensationRequisitionDetailContainerProps>;
} = {},
) => {
const utils = render(
<CompensationRequisitionDetailContainer
View={CompensationViewComponent}
{...renderOptions.props}
View={TestView}
loading={renderOptions.props?.loading ?? false}
setEditMode={setEditMode}
compensation={renderOptions.props?.compensation ?? getMockApiDefaultCompensation()}
Expand All @@ -76,12 +76,17 @@ describe('Compensation Detail View container', () => {
},
);

// wait for useEffect to complete
await waitForEffects();

return {
...utils,
};
};

beforeEach(() => {
viewProps = undefined;

getPersonConceptFn.mockResolvedValue({
data: {
id: 1,
Expand All @@ -101,18 +106,34 @@ describe('Compensation Detail View container', () => {
vi.clearAllMocks();
});

it('renders the underlying form', async () => {
const { getByText } = await setup();

expect(getByText(/Content Rendered/)).toBeVisible();
expect(viewProps.fileType).toBe(ApiGen_CodeTypes_FileTypes.Acquisition);
expect(viewProps.clientConstant).toBe('034');
expect(viewProps.compensationPayees).toEqual([]);
});

it('makes request to get the properties selected for the compensation requisition', async () => {
const teamPerson = mockApiAcquisitionFileTeamPerson();
setup({
props: {
compensation: {
...getMockApiDefaultCompensation(),
},
},
});
await setup({ props: { compensation: getMockApiDefaultCompensation() } });

await waitForEffects();
expect(mockGetCompReqPropertiesApi.execute).toHaveBeenCalledWith(
ApiGen_CodeTypes_FileTypes.Acquisition,
1,
);
});

it('makes request to get the payees for the compensation requisition', async () => {
await setup({ props: { compensation: getMockApiDefaultCompensation() } });

expect(mockGetCompReqPayeesApi.execute).toHaveBeenCalledWith(1);
});

it('calls onGenerate when generation button is clicked', async () => {
await setup({ props: { compensation: getMockApiDefaultCompensation() } });

expect(mockGetCompReqPropertiesApi.execute).toHaveBeenCalled();
await act(async () => viewProps.onGenerate(viewProps.fileType, viewProps.compensation));
expect(onGenerate).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export interface CompensationRequisitionDetailContainerProps {
clientConstant: string;
loading: boolean;
setEditMode: (editMode: boolean) => void;
View: React.FunctionComponent<React.PropsWithChildren<CompensationRequisitionDetailViewProps>>;
View: React.FunctionComponent<CompensationRequisitionDetailViewProps>;
}

export const CompensationRequisitionDetailContainer: React.FunctionComponent<
React.PropsWithChildren<CompensationRequisitionDetailContainerProps>
CompensationRequisitionDetailContainerProps
> = ({ compensation, setEditMode, View, clientConstant, fileType, file, loading }) => {
const onGenerate = useGenerateH120();
const [compensationLeaseStakeHolders, setCompensationLeaseStakeHolders] = useState<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ import { createMemoryHistory } from 'history';

import Claims from '@/constants/claims';
import { Roles } from '@/constants/index';
import { mockAcquisitionFileResponse } from '@/mocks/acquisitionFiles.mock';
import {
getMockApiAcquisitionFileOwnerOrganization,
getMockApiAcquisitionFileOwnerPerson,
mockAcquisitionFileResponse,
mockApiAcquisitionFileTeamOrganization,
mockApiAcquisitionFileTeamPerson,
} from '@/mocks/acquisitionFiles.mock';
import {
emptyCompensationFinancial,
getMockApiCompensationWithProperty,
getMockApiDefaultCompensation,
getMockCompensationPropertiesReq,
getMockCompReqPayee,
} from '@/mocks/compensations.mock';
import {
getMockApiInterestHolderOrganization,
getMockApiInterestHolderPerson,
} from '@/mocks/interestHolders.mock';
import { ApiGen_CodeTypes_AcquisitionStatusTypes } from '@/models/api/generated/ApiGen_CodeTypes_AcquisitionStatusTypes';
import { ApiGen_CodeTypes_FileTypes } from '@/models/api/generated/ApiGen_CodeTypes_FileTypes';
import { ApiGen_Concepts_CompensationRequisition } from '@/models/api/generated/ApiGen_Concepts_CompensationRequisition';
import { ApiGen_Concepts_CompReqPayee } from '@/models/api/generated/ApiGen_Concepts_CompReqPayee';
import { toTypeCodeNullable } from '@/utils/formUtils';
import { act, render, RenderOptions, userEvent, waitForEffects } from '@/utils/test-utils';

Expand Down Expand Up @@ -42,7 +54,7 @@ describe('Compensation Detail View Component', () => {
clientConstant={renderOptions.props?.clientConstant ?? '034'}
onGenerate={onGenerate}
compensationLeaseStakeHolders={[]}
compensationPayees={[]}
compensationPayees={renderOptions?.props?.compensationPayees ?? []}
/>,
{
...renderOptions,
Expand Down Expand Up @@ -224,7 +236,7 @@ describe('Compensation Detail View Component', () => {
expect(compensationFinalizedDate).toHaveTextContent('Jun 12, 2024');
});

it('Displays the Product information', async () => {
it('displays the Product information', async () => {
const mockCompensation = getMockApiDefaultCompensation();
const { queryByTestId } = await setup({
claims: [Claims.COMPENSATION_REQUISITION_VIEW],
Expand All @@ -236,6 +248,108 @@ describe('Compensation Detail View Component', () => {
expect(queryByTestId('file-product')).toHaveTextContent('00048');
});

it.each([
[
'OWNERS',
[
{
...getMockCompReqPayee(1),
compensationRequisitionId: 11,
acquisitionOwner: getMockApiAcquisitionFileOwnerPerson(),
},
{
...getMockCompReqPayee(2),
compensationRequisitionId: 11,
acquisitionOwner: getMockApiAcquisitionFileOwnerOrganization(),
},
],
['JOHH DOE', 'FORTIS BC'],
],
[
'INTEREST HOLDERS',
[
{
...getMockCompReqPayee(1),
compensationRequisitionId: 11,
interestHolderId: 14,
interestHolder: getMockApiInterestHolderPerson(14),
},
{
...getMockCompReqPayee(2),
interestHolderId: 15,
interestHolder: getMockApiInterestHolderOrganization(15),
},
],
['Chester Tester', 'FORTIS BC'],
],
[
'ACQUITISION TEAM',
[
{
...getMockCompReqPayee(1),
acquisitionFileTeamId: 11,
acquisitionFileTeam: mockApiAcquisitionFileTeamPerson(11),
},
{
...getMockCompReqPayee(2),
acquisitionFileTeamId: 12,
acquisitionFileTeam: mockApiAcquisitionFileTeamOrganization(12),
},
],
['first last', 'ABC Inc'],
],
])(
'displays the compensation payees - for %s',
async (_: string, compReqPayees: ApiGen_Concepts_CompReqPayee[], expectedValues: string[]) => {
const { findByText } = await setup({
claims: [Claims.COMPENSATION_REQUISITION_EDIT],
props: {
compensationPayees: compReqPayees,
},
});

for (const expected of expectedValues) {
expect(await findByText(new RegExp(expected, 'i'))).toBeVisible();
}
},
);

it('displays the compensation payees - for LEGACY PAYEE', async () => {
const { findByText } = await setup({
claims: [Claims.COMPENSATION_REQUISITION_EDIT],
props: {
compensationPayees: [],
compensation: {
...getMockApiDefaultCompensation(),
legacyPayee: 'Legacy Test Value',
},
},
});

expect(await findByText(/Legacy Test Value/i)).toBeVisible();
});

it('displays empty string - for NULL PAYEES', async () => {
const { findByTestId } = await setup({
claims: [Claims.COMPENSATION_REQUISITION_EDIT],
props: {
compensationPayees: [
{
...getMockCompReqPayee(1),
acquisitionFileTeam: null,
acquisitionFileTeamId: null,
interestHolder: null,
interestHolderId: null,
acquisitionOwnerId: null,
acquisitionOwner: null,
},
],
},
});

expect(await findByTestId('comp-req-payees')).toHaveTextContent('');
});

it('calls onGenerate when generation button is clicked', async () => {
const { getByTitle } = await setup({});

Expand Down
Loading
Loading