Skip to content

Commit 90abf11

Browse files
MIM-551: adds endpoints in property (#40)
1 parent 142a0d7 commit 90abf11

File tree

7 files changed

+760
-92
lines changed

7 files changed

+760
-92
lines changed

core/src/adapters/property-base-adapter/generated/api-types.ts

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ export interface paths {
696696
};
697697
};
698698
};
699-
"/facilities/rental-id/{rentalId}": {
699+
"/facilities/by-rental-id/{rentalId}": {
700700
/**
701701
* Get a facility by rental ID
702702
* @description Returns a facility with the specified rental ID
@@ -726,6 +726,66 @@ export interface paths {
726726
};
727727
};
728728
};
729+
"/facilities/by-property-code/{propertyCode}": {
730+
/**
731+
* Get facilities by property code
732+
* @description Returns a list of facilities for the specified property code
733+
*/
734+
get: {
735+
parameters: {
736+
path: {
737+
/** @description The property code of the property */
738+
propertyCode: string;
739+
};
740+
};
741+
responses: {
742+
/** @description Successfully retrieved the facilities */
743+
200: {
744+
content: {
745+
"application/json": components["schemas"]["GetFacilitiesByPropertyCodeResponse"];
746+
};
747+
};
748+
/** @description Facilities not found */
749+
404: {
750+
content: never;
751+
};
752+
/** @description Internal server error */
753+
500: {
754+
content: never;
755+
};
756+
};
757+
};
758+
};
759+
"/facilities/by-building-code/{buildingCode}": {
760+
/**
761+
* Get facilities by building code
762+
* @description Returns a list of facilities for the specified building code
763+
*/
764+
get: {
765+
parameters: {
766+
path: {
767+
/** @description The building code of the building */
768+
buildingCode: string;
769+
};
770+
};
771+
responses: {
772+
/** @description Successfully retrieved the facilities */
773+
200: {
774+
content: {
775+
"application/json": components["schemas"]["GetFacilitiesByBuildingCodeResponse"];
776+
};
777+
};
778+
/** @description Facilities not found */
779+
404: {
780+
content: never;
781+
};
782+
/** @description Internal server error */
783+
500: {
784+
content: never;
785+
};
786+
};
787+
};
788+
};
729789
"/health": {
730790
/**
731791
* Check system health status
@@ -1394,6 +1454,88 @@ export interface components {
13941454
};
13951455
};
13961456
};
1457+
GetFacilitiesByPropertyCodeResponse: {
1458+
content: ({
1459+
id: string;
1460+
code: string;
1461+
name: string | null;
1462+
entrance: string | null;
1463+
deleted: boolean;
1464+
type: {
1465+
code: string;
1466+
name: string | null;
1467+
};
1468+
rentalInformation: ({
1469+
apartmentNumber: string | null;
1470+
rentalId: string | null;
1471+
type: {
1472+
code: string;
1473+
name: string | null;
1474+
};
1475+
}) | null;
1476+
property: {
1477+
id: string | null;
1478+
name: string | null;
1479+
code: string | null;
1480+
};
1481+
building: {
1482+
id: string | null;
1483+
name: string | null;
1484+
code: string | null;
1485+
};
1486+
areaSize: number | null;
1487+
})[];
1488+
_links: {
1489+
self: {
1490+
href: string;
1491+
};
1492+
link: {
1493+
href: string;
1494+
templated: boolean;
1495+
};
1496+
};
1497+
};
1498+
GetFacilitiesByBuildingCodeResponse: {
1499+
content: ({
1500+
id: string;
1501+
code: string;
1502+
name: string | null;
1503+
entrance: string | null;
1504+
deleted: boolean;
1505+
type: {
1506+
code: string;
1507+
name: string | null;
1508+
};
1509+
rentalInformation: ({
1510+
apartmentNumber: string | null;
1511+
rentalId: string | null;
1512+
type: {
1513+
code: string;
1514+
name: string | null;
1515+
};
1516+
}) | null;
1517+
property: {
1518+
id: string | null;
1519+
name: string | null;
1520+
code: string | null;
1521+
};
1522+
building: {
1523+
id: string | null;
1524+
name: string | null;
1525+
code: string | null;
1526+
};
1527+
areaSize: number | null;
1528+
})[];
1529+
_links: {
1530+
self: {
1531+
href: string;
1532+
};
1533+
link: {
1534+
href: string;
1535+
templated: boolean;
1536+
};
1537+
};
1538+
};
13971539
};
13981540
responses: never;
13991541
parameters: never;

core/src/adapters/property-base-adapter/index.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ export async function getFacilityByRentalId(
397397
> {
398398
try {
399399
const fetchResponse = await client().GET(
400-
'/facilities/rental-id/{rentalId}',
400+
'/facilities/by-rental-id/{rentalId}',
401401
{
402402
params: { path: { rentalId } },
403403
}
@@ -475,3 +475,64 @@ export async function getMaintenanceUnitsByPropertyCode(
475475
return { ok: false, err: 'unknown' }
476476
}
477477
}
478+
479+
type GetFacilitiesByPropertyCodeResponse =
480+
components['schemas']['GetFacilitiesByPropertyCodeResponse']['content']
481+
482+
export async function getFacilitiesByPropertyCode(
483+
propertyCode: string
484+
): Promise<
485+
AdapterResult<GetFacilitiesByPropertyCodeResponse, 'not-found' | 'unknown'>
486+
> {
487+
try {
488+
const fetchResponse = await client().GET(
489+
'/facilities/by-property-code/{propertyCode}',
490+
{
491+
params: { path: { propertyCode } },
492+
}
493+
)
494+
495+
if (fetchResponse.response.status === 404) {
496+
return { ok: false, err: 'not-found' }
497+
}
498+
499+
if (!fetchResponse.data?.content) {
500+
return { ok: false, err: 'unknown' }
501+
}
502+
503+
return { ok: true, data: fetchResponse.data.content }
504+
} catch (err) {
505+
logger.error({ err }, 'property-base-adapter.getFacilitiesByPropertyCode')
506+
return { ok: false, err: 'unknown' }
507+
}
508+
}
509+
510+
type GetFacilitiesByBuildingCodeResponse =
511+
components['schemas']['GetFacilitiesByBuildingCodeResponse']['content']
512+
export async function getFacilitiesByBuildingCode(
513+
buildingCode: string
514+
): Promise<
515+
AdapterResult<GetFacilitiesByBuildingCodeResponse, 'not-found' | 'unknown'>
516+
> {
517+
try {
518+
const fetchResponse = await client().GET(
519+
'/facilities/by-building-code/{buildingCode}',
520+
{
521+
params: { path: { buildingCode } },
522+
}
523+
)
524+
525+
if (fetchResponse.response.status === 404) {
526+
return { ok: false, err: 'not-found' }
527+
}
528+
529+
if (!fetchResponse.data?.content) {
530+
return { ok: false, err: 'unknown' }
531+
}
532+
533+
return { ok: true, data: fetchResponse.data.content }
534+
} catch (err) {
535+
logger.error({ err }, 'property-base-adapter.getFacilitiesByBuildingCode')
536+
return { ok: false, err: 'unknown' }
537+
}
538+
}

core/src/services/property-base-service/index.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,4 +1381,148 @@ export const routes = (router: KoaRouter) => {
13811381
ctx.body = { error: 'Internal server error', ...metadata }
13821382
}
13831383
})
1384+
1385+
/**
1386+
* @swagger
1387+
* /propertyBase/facilities/by-property-code/{propertyCode}:
1388+
* get:
1389+
* summary: Get facilities by property code.
1390+
* description: Returns all facilities belonging to a property.
1391+
* tags:
1392+
* - Property base Service
1393+
* security:
1394+
* - bearerAuth: []
1395+
* parameters:
1396+
* - in: path
1397+
* name: propertyCode
1398+
* required: true
1399+
* schema:
1400+
* type: string
1401+
* description: The code of the property for which to retrieve facilities.
1402+
* responses:
1403+
* 200:
1404+
* description: Successfully retrieved the facilities.
1405+
* content:
1406+
* application/json:
1407+
* schema:
1408+
* type: object
1409+
* properties:
1410+
* content:
1411+
* type: array
1412+
* items:
1413+
* type: object
1414+
* 404:
1415+
* description: Facilities not found.
1416+
* 500:
1417+
* description: Internal server error.
1418+
*/
1419+
router.get('(.*)/facilities/by-property-code/:propertyCode', async (ctx) => {
1420+
const metadata = generateRouteMetadata(ctx)
1421+
const { propertyCode } = ctx.params
1422+
1423+
logger.info(`GET /facilities/by-property-code/${propertyCode}`, metadata)
1424+
1425+
try {
1426+
const result =
1427+
await propertyBaseAdapter.getFacilitiesByPropertyCode(propertyCode)
1428+
1429+
if (!result.ok) {
1430+
if (result.err === 'not-found') {
1431+
ctx.status = 404
1432+
ctx.body = { error: 'Facilities not found', ...metadata }
1433+
return
1434+
}
1435+
1436+
logger.error(
1437+
result.err,
1438+
'Error getting facilities from property-base',
1439+
metadata
1440+
)
1441+
ctx.status = 500
1442+
ctx.body = { error: 'Internal server error', ...metadata }
1443+
return
1444+
}
1445+
1446+
ctx.body = {
1447+
content: result.data satisfies Array<schemas.FacilityDetails>,
1448+
...metadata,
1449+
}
1450+
} catch (error) {
1451+
logger.error(error, 'Internal server error', metadata)
1452+
ctx.status = 500
1453+
ctx.body = { error: 'Internal server error', ...metadata }
1454+
}
1455+
})
1456+
1457+
/**
1458+
* @swagger
1459+
* /propertyBase/facilities/by-building-code/{buildingCode}:
1460+
* get:
1461+
* summary: Get facilities by building code.
1462+
* description: Returns all facilities belonging to a building.
1463+
* tags:
1464+
* - Property base Service
1465+
* security:
1466+
* - bearerAuth: []
1467+
* parameters:
1468+
* - in: path
1469+
* name: buildingCode
1470+
* required: true
1471+
* schema:
1472+
* type: string
1473+
* description: The code of the building for which to retrieve facilities.
1474+
* responses:
1475+
* 200:
1476+
* description: Successfully retrieved the facilities.
1477+
* content:
1478+
* application/json:
1479+
* schema:
1480+
* type: object
1481+
* properties:
1482+
* content:
1483+
* type: array
1484+
* items:
1485+
* type: object
1486+
* 404:
1487+
* description: Facilities not found.
1488+
* 500:
1489+
* description: Internal server error.
1490+
*/
1491+
router.get('(.*)/facilities/by-building-code/:buildingCode', async (ctx) => {
1492+
const metadata = generateRouteMetadata(ctx)
1493+
const { buildingCode } = ctx.params
1494+
1495+
logger.info(`GET /facilities/by-building-code/${buildingCode}`, metadata)
1496+
1497+
try {
1498+
const result =
1499+
await propertyBaseAdapter.getFacilitiesByBuildingCode(buildingCode)
1500+
1501+
if (!result.ok) {
1502+
if (result.err === 'not-found') {
1503+
ctx.status = 404
1504+
ctx.body = { error: 'Facilities not found', ...metadata }
1505+
return
1506+
}
1507+
1508+
logger.error(
1509+
result.err,
1510+
'Error getting facilities from property-base',
1511+
metadata
1512+
)
1513+
ctx.status = 500
1514+
ctx.body = { error: 'Internal server error', ...metadata }
1515+
return
1516+
}
1517+
1518+
ctx.body = {
1519+
content: result.data satisfies Array<schemas.FacilityDetails>,
1520+
...metadata,
1521+
}
1522+
} catch (error) {
1523+
logger.error(error, 'Internal server error', metadata)
1524+
ctx.status = 500
1525+
ctx.body = { error: 'Internal server error', ...metadata }
1526+
}
1527+
})
13841528
}

0 commit comments

Comments
 (0)