Skip to content

Commit

Permalink
Feat:UTH-117 Get Republished Listings by Rental Object Code (#210)
Browse files Browse the repository at this point in the history
* Feat:UTH-117 Get Republished Listings by Rental Object Code

* Create Note Of Interest-process for Parking spaces is now getting listing by new route that only returns active listings. This fixes a bug where it wasn't possible to get listings by rental object code that had been republished and imported again

Co-authored-by: Andreas Lundqvist <[email protected]>
  • Loading branch information
mittistormen and momentiris authored Nov 6, 2024
1 parent bda60f7 commit 283e04f
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/adapters/leasing-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ export {
createNewListing,
deleteListing,
getListingByListingId,
getListingByRentalObjectCode,
getActiveListingByRentalObjectCode,
getListingsWithApplicants,
syncInternalParkingSpacesFromXpand,
updateListingStatus,
Expand Down
11 changes: 7 additions & 4 deletions src/adapters/leasing-adapter/listings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import config from '../../common/config'

const tenantsLeasesServiceUrl = config.tenantsLeasesService.url

const getListingByRentalObjectCode = async (
const getActiveListingByRentalObjectCode = async (
rentalObjectCode: string
): Promise<AdapterResult<Listing | undefined, 'not-found' | 'unknown'>> => {
try {
const res = await axios.get(
`${tenantsLeasesServiceUrl}/listings/by-code/${rentalObjectCode}`
`${tenantsLeasesServiceUrl}/listings/active/by-code/${rentalObjectCode}`
)

if (res.status == HttpStatusCode.NotFound) {
Expand All @@ -27,7 +27,10 @@ const getListingByRentalObjectCode = async (

return { ok: true, data: res.data.content }
} catch (error) {
logger.error(error, 'Error fetching listing by rental object code:', error)
logger.error(
error,
'Error fetching active listing by rental object code:'
)
return { ok: false, err: 'unknown' }
}
}
Expand Down Expand Up @@ -207,7 +210,7 @@ const getExpiredListingsWithNoOffers = async (): Promise<
}

export {
getListingByRentalObjectCode,
getActiveListingByRentalObjectCode,
getListingsWithApplicants,
createNewListing,
applyForListing,
Expand Down
46 changes: 46 additions & 0 deletions src/adapters/tests/leasing-adapter/listings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,49 @@ describe(leasingAdapter.updateListingStatus, () => {
expect(result).toEqual({ ok: true, data: null })
})
})

describe(leasingAdapter.getActiveListingByRentalObjectCode, () => {
it('returns err if leasing responds with 404', async () => {
nock(config.tenantsLeasesService.url)
.get('/listings/active/by-code/123')
.reply(404)

const result =
await leasingAdapter.getActiveListingByRentalObjectCode('123')

expect(result).toEqual({ ok: false, err: 'not-found' })
})

it('returns err if leasing responds with 500', async () => {
nock(config.tenantsLeasesService.url)
.get('/listings/active/by-code/123')
.reply(500)

const result =
await leasingAdapter.getActiveListingByRentalObjectCode('123')

expect(result).toEqual({ ok: false, err: 'unknown' })
})

it('returns an active listing', async () => {
nock(config.tenantsLeasesService.url)
.get('/listings/active/by-code/123')
.reply(200, {
content: factory.listing.build({
rentalObjectCode: '123',
status: ListingStatus.Active,
}),
})

const result =
await leasingAdapter.getActiveListingByRentalObjectCode('123')

expect(result).toMatchObject({
ok: true,
data: expect.objectContaining({
rentalObjectCode: '123',
status: ListingStatus.Active,
}),
})
})
})
42 changes: 31 additions & 11 deletions src/adapters/tests/leasing-adapter/offers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,10 @@ describe(leasingAdapter.getOfferByOfferId, () => {
})

it('should return 404 on not found', async () => {
nock(config.tenantsLeasesService.url)
.get('/offers/123')
.reply(404, {
content: {
ok: false,
err: 'not-found',
},
})
nock(config.tenantsLeasesService.url).get('/offers/123').reply(404)

const res = await leasingAdapter.getOfferByOfferId(123)

expect(res.ok).toBe(false)
if (!res.ok) expect(res.err).toBe('not-found')
expect(res).toEqual({ ok: false, err: 'not-found' })
})
})

Expand All @@ -77,6 +68,35 @@ describe(leasingAdapter.getOffersForContact, () => {
assert(result.ok)
expect(result.data).toEqual([expect.objectContaining({ id: offer.id })])
})

it('should return 404 on not found', async () => {
nock(config.tenantsLeasesService.url)
.get('/contacts/P174965/offers')
.reply(404)

const res = await leasingAdapter.getOffersForContact('P174965')
expect(res).toEqual({ ok: false, err: 'not-found' })
})

it('should return an empty array if no offers are found', async () => {
nock(config.tenantsLeasesService.url)
.get('/contacts/P174965/offers')
.reply(200, {
content: [],
})

const res = await leasingAdapter.getOffersForContact('P174965')
expect(res).toEqual({ ok: true, data: [] })
})

it('should handle server errors gracefully', async () => {
nock(config.tenantsLeasesService.url)
.get('/contacts/P174965/offers')
.reply(500)

const res = await leasingAdapter.getOffersForContact('P174965')
expect(res).toEqual({ ok: false, err: 'unknown' })
})
})

describe(leasingAdapter.updateOfferSentAt, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getContact,
getLeasesForPnr,
addApplicantToWaitingList,
getListingByRentalObjectCode,
getActiveListingByRentalObjectCode,
createNewListing,
applyForListing,
getInternalCreditInformation,
Expand Down Expand Up @@ -190,7 +190,7 @@ export const createNoteOfInterestForInternalParkingSpace = async (
//todo: refactor get and create listing to new func ->
//todo: should return the listing regardless of if it exists previously or is being created
let listingAdapterResult =
await getListingByRentalObjectCode(parkingSpaceId)
await getActiveListingByRentalObjectCode(parkingSpaceId)
if (listingAdapterResult?.ok) {
log.push(
`Annons med id ${listingAdapterResult.data?.id} existerar sedan tidigare i onecore-leasing`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
.spyOn(leasingAdapter, 'getApplicantByContactCodeAndListingId')
.mockResolvedValue({ content: mockedApplicant })

const getListingByRentalObjectCodeSpy = jest
.spyOn(leasingAdapter, 'getListingByRentalObjectCode')
const getActiveListingByRentalObjectCodeSpy = jest
.spyOn(leasingAdapter, 'getActiveListingByRentalObjectCode')
.mockResolvedValue({
ok: true,
data: sharedListing,
Expand All @@ -101,10 +101,12 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
.spyOn(communicationAdapter, 'sendNotificationToRole')
.mockResolvedValue({})

jest.spyOn(leasingAdapter, 'getListingByRentalObjectCode').mockResolvedValue({
ok: true,
data: sharedListing,
})
jest
.spyOn(leasingAdapter, 'getActiveListingByRentalObjectCode')
.mockResolvedValue({
ok: true,
data: sharedListing,
})
jest.spyOn(leasingAdapter, 'createNewListing').mockResolvedValue({
ok: true,
data: sharedListing,
Expand Down Expand Up @@ -313,7 +315,7 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
'Additional'
)

expect(getListingByRentalObjectCodeSpy).toHaveBeenCalledWith('foo')
expect(getActiveListingByRentalObjectCodeSpy).toHaveBeenCalledWith('foo')
})

it("creates new listing if it hasn't been added already", async () => {
Expand All @@ -322,7 +324,7 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
getLeasesForPnrSpy.mockResolvedValue(mockedLeases)
getInternalCreditInformationSpy.mockResolvedValue(true)
applyForListingSpy.mockResolvedValue({ status: 201 } as any)
getListingByRentalObjectCodeSpy.mockResolvedValue({
getActiveListingByRentalObjectCodeSpy.mockResolvedValue({
ok: false,
err: 'not-found',
})
Expand Down Expand Up @@ -364,7 +366,7 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
ok: true,
data: sharedListing,
})
getListingByRentalObjectCodeSpy.mockResolvedValue({
getActiveListingByRentalObjectCodeSpy.mockResolvedValue({
ok: true,
data: sharedListing,
})
Expand Down Expand Up @@ -409,7 +411,7 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
ok: true,
data: sharedListing,
})
getListingByRentalObjectCodeSpy.mockResolvedValue({
getActiveListingByRentalObjectCodeSpy.mockResolvedValue({
ok: false,
err: 'not-found',
})
Expand Down Expand Up @@ -441,7 +443,7 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
ok: false,
err: 'conflict',
} as any)
getListingByRentalObjectCodeSpy.mockResolvedValue({
getActiveListingByRentalObjectCodeSpy.mockResolvedValue({
ok: true,
data: factory.listing.build({ status: ListingStatus.Active }),
})
Expand Down Expand Up @@ -470,7 +472,7 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
applyForListingSpy.mockResolvedValue({
status: HttpStatusCode.Ok,
} as any)
getListingByRentalObjectCodeSpy.mockResolvedValue({
getActiveListingByRentalObjectCodeSpy.mockResolvedValue({
ok: true,
data: sharedListing,
})
Expand Down Expand Up @@ -505,7 +507,7 @@ describe('createNoteOfInterestForInternalParkingSpace', () => {
ok: false,
err: 'conflict',
})
getListingByRentalObjectCodeSpy.mockResolvedValue({
getActiveListingByRentalObjectCodeSpy.mockResolvedValue({
ok: true,
data: sharedListing,
})
Expand Down

0 comments on commit 283e04f

Please sign in to comment.