Skip to content

Commit 21e8967

Browse files
authored
fix/Undefined VacantFrom Gets Todays Date (#39)
* Using the same helper function to handle undefined vacantFrom in createOffer as well as replyToOffer
1 parent 0624ef1 commit 21e8967

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

core/src/common/helpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Listing } from '@onecore/types'
2+
import * as utils from '../utils'
3+
4+
export const calculateVacantFrom = (listing: Listing): Date => {
5+
const todaysDate = utils.date.getUTCDateWithoutTime(new Date())
6+
const vacantDate = listing.rentalObject.vacantFrom
7+
? utils.date.getUTCDateWithoutTime(
8+
new Date(listing.rentalObject.vacantFrom)
9+
)
10+
: null
11+
const fromDate =
12+
vacantDate && vacantDate > todaysDate ? vacantDate : todaysDate
13+
14+
return fromDate
15+
}

core/src/processes/parkingspaces/internal/create-offer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import {
1515
ProcessError,
1616
} from '../../../common/types'
1717
import * as leasingAdapter from '../../../adapters/leasing-adapter'
18-
import * as propertyMgmtAdapter from '../../../adapters/property-management-adapter'
1918
import * as utils from '../../../utils'
2019
import * as communicationAdapter from '../../../adapters/communication-adapter'
2120
import { makeProcessError } from '../utils'
2221
import { sendNotificationToRole } from '../../../adapters/communication-adapter'
2322
import config from '../../../common/config'
23+
import { calculateVacantFrom } from '../../../common/helpers'
2424

2525
type CreateOfferError =
2626
| CreateOfferErrorCodes.NoListing
@@ -215,7 +215,7 @@ export const createOfferForInternalParkingSpace = async (
215215
firstName: eligibleApplicant.name
216216
? extractApplicantFirstName(eligibleApplicant.name)
217217
: '',
218-
availableFrom: new Date(listing.rentalObject.vacantFrom).toISOString(),
218+
availableFrom: calculateVacantFrom(listing).toISOString(),
219219
deadlineDate: new Date(offer.data.expiresAt).toISOString(),
220220
rent: String(listing.rentalObject.monthlyRent),
221221
type: listing.rentalObject.objectTypeCaption ?? '',

core/src/processes/parkingspaces/internal/reply-to-offer.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as communicationAdapter from '../../../adapters/communication-adapter'
1616
import { makeProcessError } from '../utils'
1717
import { AdapterResult } from '../../../adapters/types'
1818
import { createOfferForInternalParkingSpace } from './create-offer'
19-
import * as utils from '../../../utils'
19+
import { calculateVacantFrom } from '../../../common/helpers'
2020

2121
type ReplyToOfferError =
2222
| ReplyToOfferErrorCodes.NoOffer
@@ -110,21 +110,10 @@ export const acceptOffer = async (
110110
let leaseId: string
111111

112112
try {
113-
const todaysDate = utils.date.getUTCDateWithoutTime(new Date())
114-
const vacantDate = listing.rentalObject.vacantFrom
115-
? utils.date.getUTCDateWithoutTime(
116-
new Date(listing.rentalObject.vacantFrom)
117-
)
118-
: null
119-
const fromDate =
120-
vacantDate && vacantDate > todaysDate
121-
? vacantDate.toISOString()
122-
: todaysDate.toISOString()
123-
124113
const createLeaseResult = await leasingAdapter.createLease(
125114
listing.rentalObjectCode,
126115
offer.offeredApplicant.contactCode,
127-
fromDate,
116+
calculateVacantFrom(listing).toISOString(),
128117
'001'
129118
)
130119

core/src/processes/parkingspaces/internal/tests/create-offer.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,55 @@ describe('createOfferForInternalParkingSpace', () => {
498498
expect(expiresAt.getUTCHours()).toBe(23)
499499
expect(expiresAt.getUTCMinutes()).toBe(59)
500500
})
501+
502+
it('creates offer on a listing with an undefined vacantFrom date', async () => {
503+
const listing = factory.listing.build({ status: ListingStatus.Expired })
504+
jest
505+
.spyOn(leasingAdapter, 'getListingByListingId')
506+
.mockResolvedValue(listing)
507+
508+
jest.spyOn(leasingAdapter, 'getParkingSpaceByCode').mockResolvedValue({
509+
ok: true,
510+
data: factory.vacantParkingSpace
511+
.params({
512+
rentalObjectCode: listing.rentalObjectCode,
513+
vacantFrom: undefined,
514+
})
515+
.build(),
516+
})
517+
jest
518+
.spyOn(leasingAdapter, 'getDetailedApplicantsByListingId')
519+
.mockResolvedValueOnce({
520+
ok: true,
521+
data: factory.detailedApplicant.buildList(1),
522+
})
523+
jest
524+
.spyOn(leasingAdapter, 'getContactByContactCode')
525+
.mockResolvedValueOnce({ ok: true, data: factory.contact.build() })
526+
jest
527+
.spyOn(leasingAdapter, 'updateApplicantStatus')
528+
.mockResolvedValueOnce(null)
529+
530+
jest
531+
.spyOn(communicationAdapter, 'sendParkingSpaceOfferEmail')
532+
.mockResolvedValueOnce(null)
533+
534+
jest
535+
.spyOn(leasingAdapter, 'createOffer')
536+
.mockResolvedValueOnce({ ok: true, data: factory.offer.build() })
537+
538+
const updateOfferSentAtSpy = jest
539+
.spyOn(leasingAdapter, 'updateOfferSentAt')
540+
.mockResolvedValue({ ok: true, data: null })
541+
542+
const result = await createOfferForInternalParkingSpace(123)
543+
544+
expect(result).toEqual({
545+
processStatus: ProcessStatus.successful,
546+
data: null,
547+
httpStatus: 200,
548+
})
549+
550+
expect(updateOfferSentAtSpy).toHaveBeenCalledTimes(1)
551+
})
501552
})

0 commit comments

Comments
 (0)