Skip to content
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

UTH-21: Get application profile #209

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"pino-multi-stream": "^6.0.0",
"pino-pretty": "^11.0.0",
"striptags": "^3.2.0",
"swagger-jsdoc": "^6.2.8"
"swagger-jsdoc": "^6.2.8",
"zod": "^3.23.8"
}
}
33 changes: 33 additions & 0 deletions src/adapters/leasing-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
ApplicantWithListing,
DetailedApplicant,
Tenant,
ApplicationProfile,
leasing,
} from 'onecore-types'
import { z } from 'zod'

import { AdapterResult } from './../types'
import config from '../../common/config'
Expand Down Expand Up @@ -510,12 +513,42 @@ const validatePropertyRentalRules = async (
}
}

type GetApplicationProfileResponseData = z.infer<
typeof leasing.GetApplicationProfileResponseDataSchema
>

async function getApplicationProfileByContactCode(
contactCode: string
): Promise<
AdapterResult<GetApplicationProfileResponseData, 'unknown' | 'not-found'>
> {
try {
const response = await axios.get<{
content: GetApplicationProfileResponseData
}>(`${tenantsLeasesServiceUrl}/contacts/${contactCode}/application-profile`)

if (response.status === 200) {
return { ok: true, data: response.data.content }
}

if (response.status === 404) {
return { ok: false, err: 'not-found' }
}

return { ok: false, err: 'unknown' }
} catch (err) {
logger.error(err, 'Error fetching application profile by contact code:')
return { ok: false, err: 'unknown' }
}
}

export {
addApplicantToWaitingList,
createLease,
getApplicantByContactCodeAndListingId,
getApplicantsAndListingByContactCode,
getApplicantsByContactCode,
getApplicationProfileByContactCode,
getContact,
getContactByContactCode,
getContactByPhoneNumber,
Expand Down
38 changes: 38 additions & 0 deletions src/adapters/tests/leasing-adapter/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,42 @@ describe('leasing-adapter', () => {
})
})
})

describe(leasingAdapter.getApplicationProfileByContactCode, () => {
it('returns not found when leasing responds with 404', async () => {
nock(config.tenantsLeasesService.url)
.get('/contacts/123/application-profile')
.reply(404)

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

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

it('returns unknown err when leasing responds with 500', async () => {
nock(config.tenantsLeasesService.url)
.get('/contacts/123/application-profile')
.reply(500)

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

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

it('returns ok and application profile when leasing responds with 200', async () => {
nock(config.tenantsLeasesService.url)
.get('/contacts/123/application-profile')
.reply(200, { content: { id: 1 } })

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

expect(result).toEqual({
ok: true,
data: expect.objectContaining({ id: 1 }),
})
})
})
})
62 changes: 61 additions & 1 deletion src/services/lease-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* course, there are always exceptions).
*/
import KoaRouter from '@koa/router'
import { GetActiveOfferByListingIdErrorCodes } from 'onecore-types'
import { GetActiveOfferByListingIdErrorCodes, leasing } from 'onecore-types'
import { logger, generateRouteMetadata } from 'onecore-utilities'
import { z } from 'zod'

import * as leasingAdapter from '../../adapters/leasing-adapter'
import { ProcessStatus } from '../../common/types'
Expand Down Expand Up @@ -1441,4 +1442,63 @@ export const routes = (router: KoaRouter) => {
}
}
)
/**
* @swagger
* /contacts/{contactCode}/application-profile:
* get:
* summary: Gets an application profile by contact code
* description: Retrieve application profile information by contact code.
* tags: [Contacts]
* parameters:
* - in: path
* name: contactCode
* required: true
* schema:
* type: string
* description: The contact code associated with the application profile.
* responses:
* 200:
* description: Successfully retrieved application profile.
* content:
* application/json:
* schema:
* type: object
* properties:
* data:
* type: object
* description: The application profile data.
* 404:
* description: Not found.
* 500:
* description: Internal server error. Failed to retrieve application profile information.
*/

type GetApplicationProfileResponseData = z.infer<
typeof leasing.GetApplicationProfileResponseDataSchema
>

router.get('(.*)/contacts/:contactCode/application-profile', async (ctx) => {
const metadata = generateRouteMetadata(ctx)
const profile = await leasingAdapter.getApplicationProfileByContactCode(
ctx.params.contactCode
)

if (!profile.ok) {
if (profile.err === 'not-found') {
ctx.status = 404
ctx.body = { error: 'not-found', ...metadata }
return
}

ctx.status = 500
ctx.body = { error: 'unknown', ...metadata }
return
}

ctx.status = 200
ctx.body = {
content: profile.data satisfies GetApplicationProfileResponseData,
...metadata,
}
})
}
54 changes: 49 additions & 5 deletions src/services/lease-service/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import request from 'supertest'
import Koa from 'koa'
import KoaRouter from '@koa/router'
import bodyParser from 'koa-bodyparser'
import { routes } from '../index'
import * as tenantLeaseAdapter from '../../../adapters/leasing-adapter'
import * as replyToOffer from '../../../processes/parkingspaces/internal/reply-to-offer'
import * as offerProcess from '../../../processes/parkingspaces/internal/create-offer'

import {
Lease,
ConsumerReport,
ReplyToOfferErrorCodes,
GetActiveOfferByListingIdErrorCodes,
ListingStatus,
UpdateListingStatusErrorCodes,
leasing,
} from 'onecore-types'

import { routes } from '../index'
import * as tenantLeaseAdapter from '../../../adapters/leasing-adapter'
import * as replyToOffer from '../../../processes/parkingspaces/internal/reply-to-offer'

import * as factory from '../../../../test/factories'
import { ProcessStatus } from '../../../common/types'

const app = new Koa()
const router = new KoaRouter()
routes(router)
Expand Down Expand Up @@ -606,4 +608,46 @@ describe('lease-service', () => {
expect(updateListingStatus).toHaveBeenCalledTimes(1)
})
})

describe('GET /contacts/:contactCode/application-profile', () => {
it('responds with 404 if not found', async () => {
jest
.spyOn(tenantLeaseAdapter, 'getApplicationProfileByContactCode')
.mockResolvedValueOnce({ ok: false, err: 'not-found' })

const res = await request(app.callback()).get(
'/contacts/1234/application-profile'
)

expect(res.status).toBe(404)
expect(res.body).toEqual({
error: 'not-found',
})
})

it('responds with 200 and application profile', async () => {
jest
.spyOn(tenantLeaseAdapter, 'getApplicationProfileByContactCode')
.mockResolvedValueOnce({
ok: true,
data: {
contactCode: '1234',
createdAt: new Date(),
expiresAt: null,
id: 1,
numAdults: 0,
numChildren: 0,
},
})

const res = await request(app.callback()).get(
'/contacts/1234/application-profile'
)

expect(res.status).toBe(200)
expect(() =>
leasing.GetApplicationProfileResponseDataSchema.parse(res.body.content)
).not.toThrow()
})
})
})
Loading