Skip to content

Commit 1468133

Browse files
authored
Feat: UTH-216 Show Error Message when no Valid Housing Contract can be Found (#260)
* Adapter can now return no-valid-housing-contract * Route now returns error corresponding to the RFC 7807 standard * Route now using RouteErrorResponse * Using version 3.5.1 of onecore-types
1 parent bb5511d commit 1468133

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"koa2-swagger-ui": "^5.10.0",
7070
"lodash": "^4.17.21",
7171
"odoo-await": "^3.4.1",
72-
"onecore-types": "^3.4.0",
72+
"onecore-types": "^3.5.1",
7373
"onecore-utilities": "^1.1.0",
7474
"pino": "^9.1.0",
7575
"pino-elasticsearch": "^8.0.0",

src/adapters/leasing-adapter/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,27 @@ const getContactByContactCode = async (
144144

145145
const getTenantByContactCode = async (
146146
contactCode: string
147-
): Promise<AdapterResult<Tenant, 'unknown'>> => {
147+
): Promise<
148+
AdapterResult<
149+
Tenant,
150+
'unknown' | 'no-valid-housing-contract' | 'contact-not-found'
151+
>
152+
> => {
148153
try {
149154
const res = await axios.get(
150155
`${tenantsLeasesServiceUrl}/tenants/contactCode/${contactCode}`
151156
)
152157

153-
if (!res.data.content) return { ok: false, err: 'unknown' }
158+
if (!res.data.content) {
159+
return { ok: false, err: 'unknown' }
160+
}
154161

155162
return { ok: true, data: res.data.content }
156163
} catch (err) {
157164
logger.error({ err }, 'leasing-adapter.getTenantByContactCode')
165+
166+
if (err instanceof AxiosError)
167+
return { ok: false, err: err.response?.data?.type }
158168
return { ok: false, err: 'unknown' }
159169
}
160170
}

src/services/lease-service/index.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*/
88
import KoaRouter from '@koa/router'
99
import dayjs from 'dayjs'
10-
import { GetActiveOfferByListingIdErrorCodes } from 'onecore-types'
10+
import {
11+
GetActiveOfferByListingIdErrorCodes,
12+
RouteErrorResponse,
13+
} from 'onecore-types'
1114
import { logger, generateRouteMetadata } from 'onecore-utilities'
1215
import { z } from 'zod'
1316

@@ -455,15 +458,78 @@ export const routes = (router: KoaRouter) => {
455458
}
456459
})
457460

461+
/**
462+
* @swagger
463+
* /tenants/contactCode/{contactCode}:
464+
* get:
465+
* summary: Get tenant by contact code
466+
* tags:
467+
* - Lease service
468+
* description: Retrieves a tenant based on the provided contact code.
469+
* parameters:
470+
* - in: path
471+
* name: contactCode
472+
* required: true
473+
* schema:
474+
* type: string
475+
* description: The contact code used to identify the contact.
476+
* responses:
477+
* 200:
478+
* description: Successfully retrieved tenant information.
479+
* content:
480+
* application/json:
481+
* schema:
482+
* type: object
483+
* properties:
484+
* data:
485+
* type: object
486+
* description: The tenant data.
487+
* 404:
488+
* description: Not found.
489+
* 500:
490+
* description: Internal server error. Failed to retrieve Tenant information.
491+
* security:
492+
* - bearerAuth: []
493+
*/
458494
router.get('(.*)/tenants/contactCode/:contactCode', async (ctx) => {
459495
const metadata = generateRouteMetadata(ctx)
460496
const res = await leasingAdapter.getTenantByContactCode(
461497
ctx.params.contactCode
462498
)
463499

464500
if (!res.ok) {
501+
if (res.err === 'contact-not-found') {
502+
ctx.status = 404
503+
ctx.body = {
504+
type: res.err,
505+
title: 'Contact not found',
506+
status: 404,
507+
...metadata,
508+
} satisfies RouteErrorResponse
509+
return
510+
}
511+
512+
if (res.err === 'no-valid-housing-contract') {
513+
ctx.status = 500
514+
ctx.body = {
515+
type: res.err,
516+
title: 'No valid housing contract found',
517+
status: 500,
518+
detail:
519+
'A housing contract needs to be current or upcoming to be a valid contract when applying for a parking space.',
520+
...metadata,
521+
} satisfies RouteErrorResponse
522+
523+
return
524+
}
525+
465526
ctx.status = 500
466-
ctx.body = { error: 'Internal server error', ...metadata }
527+
ctx.body = {
528+
type: res.err,
529+
title: 'Internal server error',
530+
status: 500,
531+
...metadata,
532+
} satisfies RouteErrorResponse
467533
return
468534
}
469535

0 commit comments

Comments
 (0)