Skip to content
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
18 changes: 8 additions & 10 deletions core/src/adapters/leasing-adapter/listings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ type GetListingsParams = {
listingCategory?: 'PARKING_SPACE' | 'APARTMENT' | 'STORAGE'
published?: boolean
rentalRule?: 'SCORED' | 'NON_SCORED'
validToRentForContactCode?: string
}

const getListings = async (
Expand All @@ -238,11 +237,6 @@ const getListings = async (
if (params.published !== undefined)
queryParams.append('published', params.published.toString())
if (params.rentalRule) queryParams.append('rentalRule', params.rentalRule)
if (params.validToRentForContactCode)
queryParams.append(
'validToRentForContactCode',
params.validToRentForContactCode
)

try {
const response = await axios.get(
Expand All @@ -251,17 +245,21 @@ const getListings = async (

if (response.status !== 200) {
logger.error(
{ status: response.status, data: response.data },
`Error getting listings from leasing, by: published ${params.published}, rentalRule ${params.rentalRule} and validToRentForContactCode ${params.validToRentForContactCode}`
{
status: response.status,
data: response.data,
query: queryParams.toString(),
},
`Error getting listings from leasing`
)
return { ok: false, err: 'unknown' }
}

return { ok: true, data: response.data.content }
} catch (error) {
logger.error(
error,
`Unknown error fetching listings by published ${params.published}, rentalRule ${params.rentalRule} and validToRentForContactCode ${params.validToRentForContactCode}`
{ error: error, queryParams: queryParams.toString() },
`Unknown error fetching listings by published`
)
return { ok: false, err: 'unknown' }
}
Expand Down
20 changes: 20 additions & 0 deletions core/src/services/lease-service/helpers/lease.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Tenant } from '@onecore/types'

export const isTenantAllowedToRentAParkingSpaceInThisResidentialArea = (
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice 👏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ja!

residentialAreaCode: string,
tenant: Tenant
) => {
if (
tenant.upcomingHousingContract &&
tenant.upcomingHousingContract.residentialArea?.code === residentialAreaCode
) {
return true
}

if (
tenant.currentHousingContract &&
tenant.currentHousingContract.residentialArea?.code === residentialAreaCode
) {
return true
}
}
58 changes: 55 additions & 3 deletions core/src/services/lease-service/listings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { z } from 'zod'
import * as leasingAdapter from '../../adapters/leasing-adapter'
import * as internalParkingSpaceProcesses from '../../processes/parkingspaces/internal'
import { ProcessStatus } from '../../common/types'
import { isTenantAllowedToRentAParkingSpaceInThisResidentialArea } from './helpers/lease'

export const routes = (router: KoaRouter) => {
/**
Expand Down Expand Up @@ -75,11 +76,12 @@ export const routes = (router: KoaRouter) => {
})
const query = querySchema.safeParse(ctx.query)

logger.debug({ query }, 'Parsed query parameters for GET /listings')

const result = await leasingAdapter.getListings({
listingCategory: query.data?.listingCategory,
published: query.data?.published,
rentalRule: query.data?.rentalRule,
validToRentForContactCode: query.data?.validToRentForContactCode,
})

if (!result.ok) {
Expand Down Expand Up @@ -114,8 +116,58 @@ export const routes = (router: KoaRouter) => {
})
.filter((item): item is Listing => !!item)

ctx.status = 200
ctx.body = { content: listingsWithRentalObjects, ...metadata }
logger.info(
{
numberOfListings: listingsWithRentalObjects.length,
},
'Listings Retrieved from Leasing GET /listings'
)

if (!query.data?.validToRentForContactCode) {
ctx.status = 200
ctx.body = { content: listingsWithRentalObjects, ...metadata }
} else {
//filter listings on validToRentForContactCode
const tenantResult = await leasingAdapter.getTenantByContactCode(
query.data?.validToRentForContactCode
)
let isTenant = true

if (!tenantResult.ok) {
if (tenantResult.err === 'contact-not-tenant') {
isTenant = false
} else {
ctx.status = 500
ctx.body = { error: 'Tenant could not be retrieved', ...metadata }
return
}
}

var listings = listingsWithRentalObjects.filter((listing) => {
return (
listing.rentalRule == 'NON_SCORED' || //all NON_SCORED will be included
(listing.rentalRule == 'SCORED' &&
isTenant &&
tenantResult.ok &&
listing.rentalObject.residentialAreaCode &&
isTenantAllowedToRentAParkingSpaceInThisResidentialArea(
listing.rentalObject.residentialAreaCode,
tenantResult.data
)) // all SCORED where tenant is allowed to rent will be included
)
})

logger.debug(
{
numberOfListings: listings.length,
contactCode: query.data?.validToRentForContactCode,
},
'Listings filtered on contact GET /listings'
)

ctx.status = 200
ctx.body = { content: listings, ...metadata }
}
} catch (error) {
logger.error(error, 'Error fetching listings with rental objects')
ctx.status = 500
Expand Down
69 changes: 68 additions & 1 deletion core/src/services/lease-service/tests/listings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ describe('GET /listings', () => {
})
const parkingSpace = factory.vacantParkingSpace.build({
rentalObjectCode: '12345',
residentialAreaCode: 'AREA123',
})

const getListingsSpy = jest
Expand All @@ -113,17 +114,83 @@ describe('GET /listings', () => {
.spyOn(tenantLeaseAdapter, 'getParkingSpaces')
.mockResolvedValueOnce({ ok: true, data: [parkingSpace] })

jest
.spyOn(tenantLeaseAdapter, 'getTenantByContactCode')
.mockResolvedValueOnce({
ok: true,
data: factory.tenant.build({
currentHousingContract: { residentialArea: { code: 'AREA123' } },
}),
})

const res = await request(app.callback()).get(
'/listings?validToRentForContactCode=abc123'
)

expect(getListingsSpy).toHaveBeenCalledWith({
validToRentForContactCode: 'abc123',
listingCategory: undefined,
published: undefined,
rentalRule: undefined,
})
expect(res.status).toBe(200)
expect(res.body).toEqual({
content: [expect.objectContaining({ id: 1337 })],
})
})

it('responds with a filtered list with filter on validToRentForContactCode', async () => {
const listings = [
factory.listing.build({
id: 1337,
rentalObjectCode: '12345',
}),
factory.listing.build({
id: 1339,
rentalObjectCode: '32345',
}),
]
const parkingSpaces = [
factory.vacantParkingSpace.build({
rentalObjectCode: '12345',
residentialAreaCode: 'AREA123',
}),
factory.vacantParkingSpace.build({
rentalObjectCode: '32345',
residentialAreaCode: 'ANOTHER_AREA',
}),
]

const getListingsSpy = jest
.spyOn(tenantLeaseAdapter, 'getListings')
.mockResolvedValueOnce({ ok: true, data: listings })

jest
.spyOn(tenantLeaseAdapter, 'getParkingSpaces')
.mockResolvedValueOnce({ ok: true, data: parkingSpaces })

jest
.spyOn(tenantLeaseAdapter, 'getTenantByContactCode')
.mockResolvedValueOnce({
ok: true,
data: factory.tenant.build({
currentHousingContract: { residentialArea: { code: 'AREA123' } },
}),
})

const res = await request(app.callback()).get(
'/listings?validToRentForContactCode=abc123'
)

expect(getListingsSpy).toHaveBeenCalledWith({
listingCategory: undefined,
published: undefined,
rentalRule: undefined,
})
expect(res.status).toBe(200)
expect(res.body).toEqual({
content: [expect.objectContaining({ id: 1337 })],
})
expect(res.body.content).toHaveLength(1)
})

it('responds with 500 on error', async () => {
Expand Down
Loading