Skip to content

Commit

Permalink
- updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Herrera committed May 31, 2024
1 parent 48f1324 commit 6e9f7b7
Show file tree
Hide file tree
Showing 13 changed files with 893 additions and 25 deletions.
5 changes: 0 additions & 5 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Security.Claims;
using System.Text;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.Extensions.Logging;
using Pims.Api.Helpers.Exceptions;
using Pims.Api.Models.CodeTypes;
Expand Down Expand Up @@ -92,10 +91,6 @@ public PimsLease GetById(long leaseId)
}
}

var checkListItems = lease.PimsLeaseChecklistItems.ToList();
AppendNewItemsToChecklist(lease, ref checkListItems);
lease.PimsLeaseChecklistItems = checkListItems;

return lease;
}

Expand Down
7 changes: 1 addition & 6 deletions source/backend/dal/Repositories/LeaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ public PimsLease Get(long id)
.Include(l => l.LeaseCategoryTypeCodeNavigation)
.Include(l => l.LeaseStatusTypeCodeNavigation)
.Include(l => l.PimsLeaseTenants)
.Include(l => l.PimsLeaseChecklistItems)
.ThenInclude(t => t.LeaseChklstItemTypeCodeNavigation)
.ThenInclude(s => s.LeaseChklstSectionTypeCodeNavigation)
.Include(l => l.PimsLeaseChecklistItems)
.ThenInclude(st => st.LeaseChklstItemStatusTypeCodeNavigation)
.Include(t => t.PimsPropertyImprovements)
.Include(l => l.PimsInsurances)
.Include(l => l.PimsSecurityDeposits)
Expand Down Expand Up @@ -974,7 +969,7 @@ public IEnumerable<PimsLeaseChklstItemType> GetAllChecklistItemTypes()
}

/// <summary>
/// Add a new cehclist item to the lease.
/// Add a new checklist item to the lease.
/// </summary>
/// <param name="checklistItem"></param>
/// <returns>New Checklist Item.</returns>
Expand Down
3 changes: 3 additions & 0 deletions source/backend/entities/Partials/LeaseChecklistItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Pims.Dal.Entities
{
/// <summary>
/// PimsLeaseChecklistItem class, provides an entity for the datamodel to manage a Lease checklist item.
/// </summary>
public partial class PimsLeaseChecklistItem : StandardIdentityBaseAppEntity<long>, IBaseAppEntity
{
[NotMapped]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const LeaseChecklistContainer: React.FunctionComponent<React.PropsWithChildren<L
onSuccess,
}) => {
const { lease } = React.useContext(LeaseStateContext);
// const handleEdit = () => {
// onEdit(true);
// };

return !!isEditing && !!onEdit ? (
<ProtectedComponent claims={[Claims.LEASE_EDIT]}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { IUpdateChecklistFormProps } from "@/features/mapSideBar/shared/tabs/checklist/update/UpdateChecklistForm";
import UpdateLeaseChecklistContainer, { IUpdateLeaseChecklistContainerProps } from "./UpdateLeaseChecklistContainer";
import { act, render, RenderOptions } from '@/utils/test-utils';
import { createRef } from "react";
import { noop } from "lodash";
import { lookupCodesSlice } from "@/store/slices/lookupCodes";
import { mockLookups } from "@/mocks/lookups.mock";
import { Claims } from "@/constants";
import { useLeaseRepository } from "@/hooks/repositories/useLeaseRepository";
import { mockLeaseChecklistItemsResponse } from "@/mocks/lease.mock";
import { ApiGen_Concepts_FileWithChecklist } from "@/models/api/generated/ApiGen_Concepts_FileWithChecklist";


// mock API service calls
vi.mock('@/hooks/repositories/useLeaseRepository');

type Provider = typeof useLeaseRepository;
const mockUpdateLeaseChecklist = vi.fn();

vi.mocked(useLeaseRepository).mockReturnValue({
putLeaseChecklist: {
error: undefined,
response: undefined,
execute: mockUpdateLeaseChecklist,
loading: false,
},
} as unknown as ReturnType<Provider>);

let viewProps: IUpdateChecklistFormProps | undefined;
const TestView: React.FC<IUpdateChecklistFormProps> = props => {
viewProps = props;
return <span>Content Rendered</span>;
};


describe('Update Lease Checklist Item Container', () => {
const setup = async (
renderOptions: RenderOptions & {
props?: Partial<IUpdateLeaseChecklistContainerProps>;
} = {},
) => {
const component = render(
<UpdateLeaseChecklistContainer
formikRef={renderOptions.props?.formikRef ?? createRef()}
View={TestView}
onSuccess={renderOptions.props?.onSuccess ?? noop}
/>,
{
store: {
[lookupCodesSlice.name]: { lookupCodes: mockLookups },
},
useMockAuthentication: true,
claims: renderOptions?.claims ?? [Claims.LEASE_EDIT],
...renderOptions,
},
);

return {
...component,
};
};

beforeEach(() => {
viewProps = undefined;
vi.resetAllMocks();
});

it('renders the underlying form', async () => {
const { getByText } = await setup();
expect(getByText(/Content Rendered/)).toBeVisible();
});

it('makes request to update the disposition checklist and returns the response', async () => {
await setup();
mockUpdateLeaseChecklist.mockResolvedValue(mockLeaseChecklistItemsResponse());

let updatedChecklist: ApiGen_Concepts_FileWithChecklist | undefined;
await act(async () => {
updatedChecklist = await viewProps?.onSave({} as ApiGen_Concepts_FileWithChecklist);
});

expect(mockUpdateLeaseChecklist).toHaveBeenCalled();
expect(updatedChecklist).toStrictEqual([...mockLeaseChecklistItemsResponse()]);
});

});
31 changes: 23 additions & 8 deletions source/frontend/src/features/leases/hooks/useLeaseDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function useLeaseDetail(leaseId?: number) {

const {
getLastUpdatedBy: { execute: getLastUpdatedBy, loading: getLastUpdatedByLoading },
getLeaseChecklist: { execute: getLeaseChecklist, loading: getLeaseChecklistLoading },
} = useLeaseRepository();

const getApiLeaseById = useApiRequestWrapper({
Expand All @@ -48,25 +49,38 @@ export function useLeaseDetail(leaseId?: number) {
const leaseTenantsPromise = getLeaseTenants(leaseId);
const propertyLeasesPromise = getPropertyLeases(leaseId);
const leaseTermsPromise = getLeaseTerms(leaseId);
const [lease, leaseTenants, propertyLeases, leaseTerms] = await Promise.all([
leasePromise,
leaseTenantsPromise,
propertyLeasesPromise,
leaseTermsPromise,
]);
const leaseChecklistPromise = getLeaseChecklist(leaseId);

const [lease, leaseTenants, propertyLeases, leaseTerms, leaseChecklistItems] =
await Promise.all([
leasePromise,
leaseTenantsPromise,
propertyLeasesPromise,
leaseTermsPromise,
leaseChecklistPromise,
]);
if (lease) {
const mergedLeases: ApiGen_Concepts_Lease = {
...lease,
tenants: leaseTenants ?? [],
fileProperties: propertyLeases ?? [],
terms: leaseTerms ?? [],
fileChecklistItems: leaseChecklistItems ?? [],
};
setLease(mergedLeases);
return mergedLeases;
}
return undefined;
}
}, [leaseId, getApiLeaseByIdFunc, setLease, getLeaseTenants, getPropertyLeases, getLeaseTerms]);
}, [
leaseId,
getApiLeaseByIdFunc,
getLeaseTenants,
getPropertyLeases,
getLeaseTerms,
getLeaseChecklist,
setLease,
]);

const fetchLastUpdatedBy = useCallback(async () => {
if (leaseId) {
Expand All @@ -90,7 +104,8 @@ export function useLeaseDetail(leaseId?: number) {
propertyLeasesLoading ||
leaseTenantsLoading ||
leaseTermsLoading ||
getLastUpdatedByLoading;
getLastUpdatedByLoading ||
getLeaseChecklistLoading;

useDeepCompareEffect(() => {
if (!lease) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('ChecklistView component', () => {
const utils = render(
<ChecklistView
apiFile={mockViewProps.apiFile}
showEditButton={true}
showEditButton={mockViewProps.showEditButton}
onEdit={mockViewProps.onEdit}
sectionTypeName={API.ACQUISITION_CHECKLIST_SECTION_TYPES}
editClaim={Claims.ACQUISITION_EDIT}
Expand Down
3 changes: 3 additions & 0 deletions source/frontend/src/hooks/pims-api/useApiLeases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import { ILeaseFilter } from '@/features/leases';
import { Api_LastUpdatedBy } from '@/models/api/File';
import { ApiGen_Base_Page } from '@/models/api/generated/ApiGen_Base_Page';
import { ApiGen_Concepts_FileChecklistItem } from '@/models/api/generated/ApiGen_Concepts_FileChecklistItem';
import { ApiGen_Concepts_FileWithChecklist } from '@/models/api/generated/ApiGen_Concepts_FileWithChecklist';
import { ApiGen_Concepts_Lease } from '@/models/api/generated/ApiGen_Concepts_Lease';
import { UserOverrideCode } from '@/models/api/UserOverrideCode';
Expand Down Expand Up @@ -60,6 +61,8 @@ export const useApiLeases = () => {
Accept: 'application/vnd.ms-excel',
},
}),
getLeaseChecklist: (leaseId: number) =>
api.get<ApiGen_Concepts_FileChecklistItem[]>(`/leases/${leaseId}/checklist`),
putLeaseChecklist: (lease: ApiGen_Concepts_FileWithChecklist) =>
api.put<ApiGen_Concepts_Lease>(`/leases/${lease?.id}/checklist`, lease.fileChecklistItems),
}),
Expand Down
18 changes: 16 additions & 2 deletions source/frontend/src/hooks/repositories/useLeaseRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback, useMemo } from 'react';

import { useApiRequestWrapper } from '@/hooks/util/useApiRequestWrapper';
import { Api_LastUpdatedBy } from '@/models/api/File';
import { ApiGen_Concepts_FileChecklistItem } from '@/models/api/generated/ApiGen_Concepts_FileChecklistItem';
import { ApiGen_Concepts_FileWithChecklist } from '@/models/api/generated/ApiGen_Concepts_FileWithChecklist';
import { ApiGen_Concepts_Lease } from '@/models/api/generated/ApiGen_Concepts_Lease';
import { useAxiosErrorHandler, useAxiosSuccessHandler } from '@/utils';
Expand All @@ -13,7 +14,7 @@ import { useApiLeases } from '../pims-api/useApiLeases';
* hook that interacts with the Lease API.
*/
export const useLeaseRepository = () => {
const { getLastUpdatedByApi, getApiLease, putLeaseChecklist } = useApiLeases();
const { getLastUpdatedByApi, getApiLease, putLeaseChecklist, getLeaseChecklist } = useApiLeases();

const getLastUpdatedBy = useApiRequestWrapper<
(leaseId: number) => Promise<AxiosResponse<Api_LastUpdatedBy, any>>
Expand All @@ -39,6 +40,18 @@ export const useLeaseRepository = () => {
onError: useAxiosErrorHandler('Failed to retreive lease.'),
});

const getLeaseChecklistApi = useApiRequestWrapper<
(leaseId: number) => Promise<AxiosResponse<ApiGen_Concepts_FileChecklistItem[], any>>
>({
requestFunction: useCallback(
async (leaseId: number) => await getLeaseChecklist(leaseId),
[getLeaseChecklist],
),
requestName: 'getApiLeaseChecklist',
onSuccess: useAxiosSuccessHandler(),
onError: useAxiosErrorHandler('Failed to retreive lease checklist.'),
});

const updateLeaseChecklistApi = useApiRequestWrapper<
(lease: ApiGen_Concepts_FileWithChecklist) => Promise<AxiosResponse<ApiGen_Concepts_Lease, any>>
>({
Expand All @@ -55,8 +68,9 @@ export const useLeaseRepository = () => {
() => ({
getLastUpdatedBy: getLastUpdatedBy,
getLease: getLease,
getLeaseChecklist: getLeaseChecklistApi,
putLeaseChecklist: updateLeaseChecklistApi,
}),
[getLastUpdatedBy, getLease, updateLeaseChecklistApi],
[getLastUpdatedBy, getLease, getLeaseChecklistApi, updateLeaseChecklistApi],
);
};
Loading

0 comments on commit 6e9f7b7

Please sign in to comment.