Skip to content

Commit

Permalink
Feat/1467 create response types poc (#60)
Browse files Browse the repository at this point in the history
* feat:1443 refactor current tests to use factories

* feat: 1443 handle upcoming housing contracts

* feat: 1443 clean

* Feat: 1467 use detailedApplicant response type
  • Loading branch information
driatic authored May 22, 2024
1 parent 194bc53 commit 77a6feb
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 50 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"koa-bodyparser": "^4.4.1",
"koa-pino-logger": "^4.0.0",
"mssql": "^9.3.2",
"onecore-types": "^1.18.0",
"personnummer": "^3.2.1",
"prettier": "^3.2.5",
"randexp": "^0.5.3",
Expand Down
9 changes: 7 additions & 2 deletions src/services/lease-service/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import KoaRouter from '@koa/router'
import { Applicant, ApplicantStatus, Listing } from 'onecore-types'
import {
Applicant,
ApplicantStatus,
Listing,
DetailedApplicant,
} from 'onecore-types'

import {
getContactByContactCode,
Expand Down Expand Up @@ -509,7 +514,7 @@ export const routes = (router: KoaRouter) => {
return
}

const applicants: any = []
const applicants: DetailedApplicant[] = []

if (listing.applicants) {
for (const applicant of listing.applicants) {
Expand Down
54 changes: 36 additions & 18 deletions src/services/lease-service/priority-list-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {
Applicant,
DetailedApplicant,
Lease,
Listing,
ParkingSpaceApplicationCategory,
Expand All @@ -20,7 +21,9 @@ import {
} from './adapters/xpand/tenant-lease-adapter'
import { leaseTypes } from '../../constants/leaseTypes'

const getDetailedApplicantInformation = async (applicant: Applicant) => {
const getDetailedApplicantInformation = async (
applicant: Applicant
): Promise<DetailedApplicant> => {
try {
const applicantFromXpand = await getContactByContactCode(
applicant.contactCode,
Expand Down Expand Up @@ -76,7 +79,6 @@ const getDetailedApplicantInformation = async (applicant: Applicant) => {

const parkingSpaces = parseLeasesForParkingSpaces(activeAndUpcomingLeases)

//todo: define the proper interface type to return
return {
...applicant,
queuePoints: waitingListForInternalParkingSpace.queuePoints,
Expand All @@ -91,12 +93,11 @@ const getDetailedApplicantInformation = async (applicant: Applicant) => {
}
}

//todo: should use and return defined interface type
const addPriorityToApplicantsBasedOnRentalRules = (
listing: Listing,
applicants: any[]
applicants: DetailedApplicant[]
) => {
const applicantsWithAssignedPriority: any[] = [] //todo: use defined interface
const applicantsWithAssignedPriority: DetailedApplicant[] = []
for (const applicant of applicants) {
applicantsWithAssignedPriority.push(
assignPriorityToApplicantBasedOnRentalRules(listing, applicant)
Expand All @@ -106,23 +107,30 @@ const addPriorityToApplicantsBasedOnRentalRules = (
return applicantsWithAssignedPriority
}

const sortApplicantsBasedOnRentalRules = (applicants: any[]): any[] => {
const sortApplicantsBasedOnRentalRules = (
applicants: DetailedApplicant[]
): DetailedApplicant[] => {
return Array.from(applicants).sort((a, b) => {
//undefined priority is the lowest priority
const aPriority =
a.priority !== undefined ? a.priority : Number.MAX_SAFE_INTEGER
const bPriority =
b.priority !== undefined ? b.priority : Number.MAX_SAFE_INTEGER

//sort by priority (ascending)
if (a.priority !== b.priority) {
return a.priority - b.priority
if (aPriority !== bPriority) {
return aPriority - bPriority
}

//sort by queue points (descending)
return b.queuePoints - a.queuePoints
})
}

//todo: should use and return defined interface type from onecore-types
const assignPriorityToApplicantBasedOnRentalRules = (
listing: Listing,
applicant: any
): any => {
applicant: DetailedApplicant
): DetailedApplicant => {
if (applicant.listingId !== listing.id) {
throw new Error(
`applicant ${applicant.contactCode} does not belong to listing ${listing.id}`
Expand All @@ -132,10 +140,10 @@ const assignPriorityToApplicantBasedOnRentalRules = (
//priority 1

//Applicant has no active parking space contract and is tenant in same area as listing
if (!applicant.parkingSpaceContracts.length) {
if (!applicant.parkingSpaceContracts?.length) {
if (applicant.currentHousingContract) {
if (
applicant.currentHousingContract.residentialArea.code ===
applicant.currentHousingContract?.residentialArea?.code ===
listing.districtCode
) {
return {
Expand All @@ -148,7 +156,7 @@ const assignPriorityToApplicantBasedOnRentalRules = (
//Applicant has no active parking space contract and has upcoming housing contract in same area as listing
if (applicant.upcomingHousingContract) {
if (
applicant.upcomingHousingContract.residentialArea.code ===
applicant.upcomingHousingContract?.residentialArea?.code ===
listing.districtCode
) {
return {
Expand All @@ -161,7 +169,7 @@ const assignPriorityToApplicantBasedOnRentalRules = (

//Applicant has 1 active contract for parking space and wishes to replace current parking space
if (
applicant.parkingSpaceContracts.length === 1 &&
applicant.parkingSpaceContracts?.length === 1 &&
applicant.applicationType === 'Replace'
) {
return {
Expand All @@ -174,7 +182,7 @@ const assignPriorityToApplicantBasedOnRentalRules = (

//Applicant has 1 active parking space contract and wishes to rent an additional parking space
if (
applicant.parkingSpaceContracts.length === 1 &&
applicant.parkingSpaceContracts?.length === 1 &&
applicant.applicationType === 'Additional'
) {
return {
Expand All @@ -185,6 +193,7 @@ const assignPriorityToApplicantBasedOnRentalRules = (

//Applicant has more than 1 active parking space contract and wishes to replace 1 parking space contract
if (
applicant.parkingSpaceContracts &&
applicant.parkingSpaceContracts.length > 1 &&
applicant.applicationType === 'Replace'
) {
Expand All @@ -196,11 +205,20 @@ const assignPriorityToApplicantBasedOnRentalRules = (

//priority 3

//Applicant has more 2 or more active parking space and wishes to rent an additional parking space
//Applicant has 2 or more active parking space and wishes to rent an additional parking space
if (
applicant.parkingSpaceContracts &&
applicant.parkingSpaceContracts.length >= 2
) {
return {
...applicant,
priority: 3,
}
}

return {
...applicant,
priority: 3,
priority: undefined,
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/services/lease-service/tests/factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Factory } from 'fishery'
import {
DetailedApplicant,
Lease,
LeaseStatus,
Listing,
Expand Down Expand Up @@ -41,32 +42,32 @@ const LeaseFactory = Factory.define<Lease>(({ sequence }) => ({
},
}))

//todo: use properly defined interface
const ApplicantFactory = Factory.define<any, { currentHousingContract: Lease }>(
({ sequence, params }) => ({
id: `${sequence}`,
const DetailedApplicantFactory = Factory.define<DetailedApplicant>(
({ sequence }) => ({
id: sequence,
name: 'Test Testsson',
nationalRegistrationNumber: '199404084924',
contactCode: `P${158769 + sequence}`,
applicationDate: new Date().toISOString(),
applicationDate: new Date(),
applicationType: 'Additional',
status: 1,
listingId: `${sequence}`,
listingId: sequence, //maybe keep as undefined?
queuePoints: 10,
address: {
street: 'Aromas väg 8B',
number: '',
postalCode: '73439',
city: 'Hallstahammar',
},
currentHousingContract: params.currentHousingContract,
upcomingHousingContract: params.upcomingHousingContract,
currentHousingContract: undefined,
upcomingHousingContract: undefined,
parkingSpaceContracts: [],
priority: 0,
priority: undefined,
})
)

const ListingFactory = Factory.define<Listing>(({ sequence }) => ({
id: sequence + 1,
id: sequence,
rentalObjectCode: `R${sequence + 1000}`,
address: 'Sample Address',
monthlyRent: 1000,
Expand All @@ -91,11 +92,11 @@ const OfferFactory = Factory.define<Offer>(({ sequence }) => ({
expiresAt: new Date(),
id: sequence,
listingId: 1,
offeredApplicant: ApplicantFactory.build(),
offeredApplicant: DetailedApplicantFactory.build(),
selectedApplicants: [],
sentAt: null,
status: OfferStatus.Active,
createdAt: new Date(),
}))

export { LeaseFactory, ApplicantFactory, ListingFactory, OfferFactory }
export { LeaseFactory, DetailedApplicantFactory, ListingFactory, OfferFactory }
4 changes: 3 additions & 1 deletion src/services/lease-service/tests/offers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('offers', () => {
const expected = { ...offer, expiresAt: offer.expiresAt.toISOString() }

expect(res.status).toBe(201)
expect(res.body.data).toEqual(expected)
expect(res.body.data.createdAt).toBeDefined()
expect(res.body.data.listingId).toEqual(expected.listingId)
expect(res.body.data.expiresAt).toEqual(expected.expiresAt)
})
})
})
Loading

0 comments on commit 77a6feb

Please sign in to comment.