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

Lease Last Updated By | PSP-6933 #3512

Merged
merged 10 commits into from
Oct 26, 2023
15 changes: 15 additions & 0 deletions source/backend/api/Areas/Leases/Controllers/LeaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@
return new JsonResult(mapped);
}

/// <summary>
/// Gets the specified lease last updated-by information.
/// </summary>
/// <returns></returns>
[HttpGet("{id:long}/updateInfo")]
[HasPermission(Permissions.LeaseView)]
[Produces("application/json")]
[ProducesResponseType(typeof(Dal.Entities.Models.LastUpdatedByModel), 200)]
[SwaggerOperation(Tags = new[] { "lease" })]
public IActionResult GetLastUpdatedBy(long id)
{
var lastUpdated = _leaseService.GetLastUpdateInformation(id);
return new JsonResult(lastUpdated);
}

Check warning on line 91 in source/backend/api/Areas/Leases/Controllers/LeaseController.cs

View check run for this annotation

Codecov / codecov/patch

source/backend/api/Areas/Leases/Controllers/LeaseController.cs#L88-L91

Added lines #L88 - L91 were not covered by tests

/// <summary>
/// Add the specified lease. Allows the user to override the normal restriction on adding properties already associated to a lease.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions source/backend/api/Services/ILeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface ILeaseService

PimsLease GetById(long leaseId);

LastUpdatedByModel GetLastUpdateInformation(long leaseId);

Paged<PimsLease> GetPage(LeaseFilter filter, bool? all = false);

PimsLease Add(PimsLease lease, IEnumerable<UserOverrideCode> userOverrides);
Expand Down
8 changes: 8 additions & 0 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
return lease;
}

public LastUpdatedByModel GetLastUpdateInformation(long leaseId)
{
_logger.LogInformation("Retrieving last updated-by information...");
_user.ThrowIfNotAuthorized(Permissions.LeaseView);

Check warning on line 91 in source/backend/api/Services/LeaseService.cs

View check run for this annotation

Codecov / codecov/patch

source/backend/api/Services/LeaseService.cs#L89-L91

Added lines #L89 - L91 were not covered by tests

return _leaseRepository.GetLastUpdateBy(leaseId);
}

Check warning on line 94 in source/backend/api/Services/LeaseService.cs

View check run for this annotation

Codecov / codecov/patch

source/backend/api/Services/LeaseService.cs#L93-L94

Added lines #L93 - L94 were not covered by tests

public Paged<PimsLease> GetPage(LeaseFilter filter, bool? all = false)
{
_logger.LogInformation("Getting lease page {filter}", filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface ILeaseRepository : IRepository<PimsLease>

PimsLease GetNoTracking(long id);

LastUpdatedByModel GetLastUpdateBy(long leaseId);

Paged<PimsLease> GetPage(LeaseFilter filter, HashSet<short> regions);

IList<PimsLeaseDocument> GetAllLeaseDocuments(long leaseId);
Expand Down
417 changes: 417 additions & 0 deletions source/backend/dal/Repositories/LeaseRepository.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
import DepositsContainer from './DepositsContainer';
import { FormLeaseDeposit } from './models/FormLeaseDeposit';

const onSuccessMock = jest.fn();

const mockAxios = new MockAdapter(axios);
jest.mock('@react-keycloak/web');
(useKeycloak as jest.Mock).mockReturnValue({
Expand All @@ -44,7 +46,7 @@ const setup = (renderOptions: RenderOptions & { lease?: LeaseFormModel } = {}):
}}
>
<Formik onSubmit={noop} initialValues={renderOptions.lease ?? new LeaseFormModel()}>
<DepositsContainer />
<DepositsContainer onSuccess={onSuccessMock} />
</Formik>
</LeaseStateContext.Provider>,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import { LeaseDepositForm } from './models/LeaseDepositForm';
import * as Styled from './styles';

export interface IDepositsContainerProps {}
export interface IDepositsContainerProps {
onSuccess: () => void;
}

export const DepositsContainer: React.FunctionComponent<
React.PropsWithChildren<IDepositsContainerProps>
> = () => {
> = props => {
const { lease } = useContext(LeaseStateContext);
const {
getSecurityDeposits: {
Expand Down Expand Up @@ -105,6 +107,7 @@
setDepositToDelete(undefined);
setDeleteModalWarning(false);
getSecurityDeposits(lease.id);
props.onSuccess();

Check warning on line 110 in source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx#L110

Added line #L110 was not covered by tests
}
};

Expand All @@ -114,6 +117,7 @@
setDepositReturnToDelete(undefined);
setDeleteReturnModalWarning(false);
getSecurityDeposits(lease.id);
props.onSuccess();

Check warning on line 120 in source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx#L120

Added line #L120 was not covered by tests
}
};

Expand All @@ -130,6 +134,7 @@
setEditDepositValue(FormLeaseDeposit.createEmpty(lease.id));
setShowEditModal(false);
getSecurityDeposits(lease.id);
props.onSuccess();

Check warning on line 137 in source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx#L137

Added line #L137 was not covered by tests
}
} else {
console.error('Lease information incomplete');
Expand Down Expand Up @@ -179,6 +184,7 @@
setDepositReturnToDelete(undefined);
setShowReturnEditModal(false);
getSecurityDeposits(lease.id);
props.onSuccess();

Check warning on line 187 in source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/features/leases/detail/LeasePages/deposits/DepositsContainer.tsx#L187

Added line #L187 was not covered by tests
}
} else {
console.error('Lease information incomplete');
Expand Down Expand Up @@ -214,6 +220,7 @@
onSave={async (notes: string) => {
lease?.id && (await updateSecurityDepositNote(lease.id, notes));
setEditNotes(false);
props.onSuccess();
}}
onCancel={() => {
setEditNotes(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const storeState = {
};
const mockAxios = new MockAdapter(axios);

const onSuccessMock = jest.fn();

const SaveButton = () => {
const { submitForm } = useFormikContext();
return <button onClick={submitForm}>Save</button>;
Expand Down Expand Up @@ -51,6 +53,7 @@ describe('Add Improvements container component', () => {
onEdit={noop}
improvements={renderOptions.improvements ?? []}
loading={false}
onSuccess={onSuccessMock}
>
<SaveButton />
</AddImprovementsContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ interface IAddImprovementsContainerProps {
onEdit?: (isEditing: boolean) => void;
improvements: Api_PropertyImprovement[];
loading: boolean;
onSuccess: () => void;
}

export const AddImprovementsContainer: React.FunctionComponent<
React.PropsWithChildren<IAddImprovementsContainerProps>
> = ({ formikRef, onEdit, children, loading, improvements }) => {
> = ({ formikRef, onEdit, children, loading, improvements, onSuccess }) => {
const { lease } = useContext(LeaseStateContext);
const { updatePropertyImprovements } = usePropertyImprovementRepository();
const { getByType } = useLookupCodeHelpers();
Expand All @@ -44,6 +45,7 @@ export const AddImprovementsContainer: React.FunctionComponent<
);
formikRef?.current?.resetForm({ values: form });
onEdit && onEdit(false);
onSuccess();
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const storeState = {
};
const mockAxios = new MockAdapter(axios);

const onSuccessMock = jest.fn();

describe('Improvements Container component', () => {
const setup = async (
renderOptions: RenderOptions & { improvements?: Partial<ILeaseImprovementForm>[] } = {},
Expand All @@ -37,6 +39,7 @@ describe('Improvements Container component', () => {
<ImprovementsContainer
isEditing={false}
formikRef={React.createRef()}
onSuccess={onSuccessMock}
></ImprovementsContainer>
</LeaseStateContext.Provider>,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Improvements } from './Improvements';

export const ImprovementsContainer: React.FunctionComponent<
React.PropsWithChildren<LeasePageProps>
> = ({ isEditing, formikRef, onEdit }) => {
> = ({ isEditing, formikRef, onEdit, onSuccess }) => {
const { lease } = useContext(LeaseStateContext);
const {
getPropertyImprovements: { execute: getPropertyImprovements, loading, response: improvements },
Expand Down Expand Up @@ -44,6 +44,7 @@ export const ImprovementsContainer: React.FunctionComponent<
onEdit={onEdit}
improvements={improvements ?? []}
loading={loading}
onSuccess={onSuccess}
/>
</ProtectedComponent>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
isEditing,
onEdit,
formikRef,
onSuccess,
}) => {
const { hasClaim } = useKeycloakWrapper();
const [showCancelModal, setShowCancelModal] = useState(false);
Expand Down Expand Up @@ -49,10 +50,11 @@
if (updatedInsurance) {
leaseId && (await getInsurances(leaseId));
onEdit && onEdit(false);
onSuccess();

Check warning on line 53 in source/frontend/src/features/leases/detail/LeasePages/insurance/InsuranceContainer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/features/leases/detail/LeasePages/insurance/InsuranceContainer.tsx#L53

Added line #L53 was not covered by tests
}
}
},
[getInsurances, leaseId, onEdit, updateInsurances],
[getInsurances, leaseId, onEdit, updateInsurances, onSuccess],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
export const TermPaymentsContainer: React.FunctionComponent<
React.PropsWithChildren<LeasePageProps>
> = ({ formikRef }) => {
> = ({ formikRef, onSuccess }) => {
const { lease } = useContext(LeaseStateContext);
const generateH1005a = useGenerateH1005a(lease);
const [editModalValues, setEditModalValues] = useState<FormLeaseTerm | undefined>(undefined);
Expand Down Expand Up @@ -67,7 +67,7 @@
setDeleteModalWarning,
setConfirmDeleteModalValues,
comfirmDeleteModalValues,
} = useDeleteTermsPayments(deleteLeaseTerm, refreshLeaseTerms);
} = useDeleteTermsPayments(deleteLeaseTerm, refreshLeaseTerms, onSuccess);

/**
* Send the save request (either an update or an add). Use the response to update the parent lease.
Expand All @@ -82,9 +82,10 @@
const response = await getLeaseTerms.execute(leaseId);
setTerms(response ?? []);
setEditModalValues(undefined);
onSuccess();

Check warning on line 85 in source/frontend/src/features/leases/detail/LeasePages/payment/TermPaymentsContainer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/features/leases/detail/LeasePages/payment/TermPaymentsContainer.tsx#L85

Added line #L85 was not covered by tests
}
},
[addLeaseTerm, getLeaseTerms, gstDecimal, leaseId, updateLeaseTerm],
[addLeaseTerm, getLeaseTerms, gstDecimal, leaseId, updateLeaseTerm, onSuccess],
);

/**
Expand All @@ -101,10 +102,11 @@
const response = await getLeaseTerms.execute(leaseId);
setTerms(response ?? []);
setEditPaymentModalValues(undefined);
onSuccess();
}
}
},
[leaseId, updateLeasePayment, addLeasePayment, getLeaseTerms],
[leaseId, updateLeasePayment, addLeasePayment, getLeaseTerms, onSuccess],
);

const onEdit = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const storeState = {
[lookupCodesSlice.name]: { lookupCodes: mockLookups },
};
const setLease = jest.fn();
const onSuccessMock = jest.fn();

describe('TermsPaymentsContainer component', () => {
const setup = async (
Expand All @@ -93,7 +94,11 @@ describe('TermsPaymentsContainer component', () => {
}}
>
<Formik initialValues={renderOptions.initialValues ?? {}} onSubmit={noop}>
<TermPaymentsContainer formikRef={React.createRef()} isEditing={false} />
<TermPaymentsContainer
formikRef={React.createRef()}
isEditing={false}
onSuccess={onSuccessMock}
/>
</Formik>
</LeaseStateContext.Provider>,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
export const useDeleteTermsPayments = (
deleteLeaseTerm: IResponseWrapper<(term: Api_LeaseTerm) => Promise<AxiosResponse<boolean, any>>>,
getLeaseTerms: (leaseId: number) => Promise<void>,
onSuccess: () => void,
) => {
const [comfirmDeleteModalValues, setConfirmDeleteModalValues] = useState<
IDeleteConfirmationModal | undefined
Expand All @@ -36,9 +37,10 @@
});
if (deleted && lease?.id) {
getLeaseTerms(lease.id);
onSuccess();

Check warning on line 40 in source/frontend/src/features/leases/detail/LeasePages/payment/hooks/useDeleteTermsPayments.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/features/leases/detail/LeasePages/payment/hooks/useDeleteTermsPayments.tsx#L40

Added line #L40 was not covered by tests
}
},
[deleteLeaseTerm, lease, getLeaseTerms],
[deleteLeaseTerm, lease, getLeaseTerms, onSuccess],
);

/**
Expand Down Expand Up @@ -96,10 +98,11 @@
);
if (deleted && leaseId) {
getLeaseTerms(leaseId);
onSuccess();
}
}
},
[deleteLeasePayment, leaseId, getLeaseTerms],
[deleteLeasePayment, leaseId, getLeaseTerms, onSuccess],
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jest.mock('@/hooks/repositories/useLeaseTenantRepository');
const getPersonConcept = jest.fn();
const updateTenants = jest.fn().mockResolvedValue({ ...defaultApiLease, id: 1 });
const onEdit = jest.fn();
const onSuccess = jest.fn();

const history = createMemoryHistory();
const storeState = {
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('AddLeaseTenantContainer component', () => {
View={View}
onEdit={onEdit}
tenants={renderOptions.tenants ?? []}
onSuccess={onSuccess}
>
<SaveButton />
</AddLeaseTenantContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ interface IAddLeaseTenantContainerProps {
formikRef: React.RefObject<FormikProps<LeaseFormModel>>;
onEdit?: (isEditing: boolean) => void;
tenants: FormTenant[];
onSuccess: () => void;
View: React.FunctionComponent<
React.PropsWithChildren<IAddLeaseTenantFormProps & IPrimaryContactWarningModalProps>
>;
}

export const AddLeaseTenantContainer: React.FunctionComponent<
React.PropsWithChildren<IAddLeaseTenantContainerProps>
> = ({ formikRef, onEdit, children, View, tenants: initialTenants }) => {
> = ({ formikRef, onEdit, children, View, tenants: initialTenants, onSuccess }) => {
const { lease } = useContext(LeaseStateContext);
const [tenants, setTenants] = useState<FormTenant[]>(initialTenants);
const [selectedContacts, setSelectedContacts] = useState<IContactSearchResult[]>(
tenants.map(t => FormTenant.toContactSearchResult(t)) || [],
);
const [showContactManager, setShowContactManager] = React.useState<boolean>(false);
const [handleSubmit, setHandleSubmit] = useState<Function | undefined>(undefined);

const {
updateLeaseTenants,
getLeaseTenants: { execute: getLeaseTenants, loading },
} = useLeaseTenantRepository();

const leaseId = lease?.id;
useEffect(() => {
const tenantFunc = async () => {
Expand Down Expand Up @@ -113,6 +116,7 @@ export const AddLeaseTenantContainer: React.FunctionComponent<
}),
});
onEdit && onEdit(false);
onSuccess();
}
} finally {
formikRef?.current?.setSubmitting(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const TenantContainer: React.FunctionComponent<React.PropsWithChildren<LeasePage
isEditing,
formikRef,
onEdit,
onSuccess,
}) => {
const { lease } = useContext(LeaseStateContext);
const {
Expand All @@ -36,6 +37,7 @@ const TenantContainer: React.FunctionComponent<React.PropsWithChildren<LeasePage
onEdit={onEdit}
tenants={formTenants}
View={AddLeaseTenantForm}
onSuccess={onSuccess}
/>
</ProtectedComponent>
) : (
Expand Down
Loading
Loading