Skip to content

feat: DAH-3399 use unit status rather than iterating over leases #725

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

Merged
merged 6 commits into from
May 14, 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 @@ -20,11 +20,13 @@ import {
getApplicationMembers,
totalSetAsidesForPref
} from 'components/supplemental_application/utils/supplementalApplicationUtils'
import { UNIT_STATUS_OCCUPIED } from 'utils/consts'
import { useAppContext } from 'utils/customHooks'
import { CurrencyField, FieldError, Label, SelectField } from 'utils/form/final_form/Field'
import { MultiDateField } from 'utils/form/final_form/MultiDateField'
import { areLeaseAndRentalAssistancesValid } from 'utils/form/formSectionValidations'
import formUtils from 'utils/formUtils'
import { useFeatureFlag } from 'utils/hooks/useFeatureFlag'
import { addLayeredPreferenceFields } from 'utils/layeredPreferenceUtil'
import { pluck } from 'utils/utils'

Expand Down Expand Up @@ -119,6 +121,8 @@ const LeaseActions = ({
}

const Lease = ({ form, values }) => {
const { unleashFlag: unitStatusFlagEnabled } = useFeatureFlag('partners.unitStatus', false)

const [
{
supplementalApplicationData: { supplemental: state }
Expand Down Expand Up @@ -155,17 +159,24 @@ const Lease = ({ form, values }) => {

/**
* available units fit the following criteria
* - if it doesn't have any leases
* - if there are leases, they cannot be in draft or signed status
* - if the unit has an application, it must match the current one
* - if unitStatusFlagEnabled, check unit status is not occupied
* - else
* - if it doesn't have any leases
* - if there are leases, they cannot be in draft or signed status
* - if the unit has an application, it must match the current one
*
* todo: clean up feature flag code
*/
const unavailableStatuses = ['Draft', 'Signed']
const availableUnits = state.units.filter(
(unit) =>
const availableUnits = state.units.filter((unit) => {
if (unitStatusFlagEnabled) return unit.status && unit.status !== UNIT_STATUS_OCCUPIED

return (
!Array.isArray(unit.leases) ||
!unit.leases.some((lease) => unavailableStatuses.includes(lease.lease_status)) ||
unit.leases.some((lease) => lease.application_id === state.application.id)
)
)
})

const availableUnitsOptions = formUtils.toOptions(
map(availableUnits, pluck('id', 'unit_number', 'priority_type'))
Expand Down
1 change: 1 addition & 0 deletions app/javascript/utils/consts.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const LISTING_TYPE_FIRST_COME_FIRST_SERVED = 'First Come, First Served'
export const UNIT_STATUS_OCCUPIED = 'Occupied'
2 changes: 1 addition & 1 deletion app/services/force/units_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def units_and_leases_for_listing(listing_id)
.to_soql

result = builder.from(:Unit__c)
.select("Id, Priority_Type__c, AMI_chart_type__c, Max_AMI_for_Qualifying_Unit__c, Unit_Number__c, Unit_Type__c, AMI_chart_year__c, (#{lease_query})")
.select("Id, Priority_Type__c, AMI_chart_type__c, Max_AMI_for_Qualifying_Unit__c, Unit_Number__c, Unit_Type__c, AMI_chart_year__c, Status__c, (#{lease_query})")
.where_eq('Listing__c', listing_id, :string)
.transform_results { |results| massage(results) }
.query
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, screen, fireEvent, within, act } from '@testing-library/react'
import { useFlag as useFlagUnleash, useFlagsStatus } from '@unleash/proxy-client-react'
import { cloneDeep } from 'lodash'
import selectEvent from 'react-select-event'

Expand All @@ -21,6 +22,13 @@ const getWindowUrl = (id) => `/lease-ups/applications/${id}`
const ID_NO_AVAILABLE_UNITS = 'idwithnoavailableunits'
const ID_WITH_TOTAL_MONTHLY_RENT = 'idwithtotalmonthlyrent'

jest.mock('@unleash/proxy-client-react')
useFlagUnleash.mockImplementation(() => false)
useFlagsStatus.mockImplementation(() => ({
flagsError: false,
flagsReady: true
}))

/**
* TODO: instead of mocking apiService, we should probably be mocking one level up (actions.js).
*/
Expand Down Expand Up @@ -90,6 +98,7 @@ jest.mock('apiService', () => {
unit_type: 'studio',
priority_type: null,
max_ami_for_qualifying_unit: 50,
status: 'Occupied',
leases: [
{
application_id: 'testId',
Expand All @@ -107,13 +116,15 @@ jest.mock('apiService', () => {
id: 'unit_without_priority',
unit_number: 'unit without priority',
priority_type: null,
max_ami_for_qualifying_unit: 50
max_ami_for_qualifying_unit: 50,
status: 'Available'
}),
_merge(mockedUnits[1], {
id: 'unit_with_priority',
unit_number: 'unit with priority',
priority_type: 'Hearing/Vision impairments',
max_ami_for_qualifying_unit: 50
max_ami_for_qualifying_unit: 50,
status: 'Available'
})
]
},
Expand Down Expand Up @@ -655,6 +666,22 @@ describe('SupplementalApplicationPage', () => {
expect(screen.getByTestId('accessibility-available-count').textContent).toBe('0')
})
})

describe('partners.unitStatus feature toggle', () => {
beforeEach(async () => {
await getWrapper()
})

test('shows available units based on leases when toggle is off', () => {
useFlagUnleash.mockImplementation(() => false)
expect(screen.getByTestId('total-available-count').textContent).toBe('2')
})

test('shows available units based on unit status when toggle is on', () => {
useFlagUnleash.mockImplementation(() => true)
expect(screen.getByTestId('total-available-count').textContent).toBe('2')
})
})
})

describe('Status Sidebar', () => {
Expand Down
Loading