Skip to content

Commit c227d04

Browse files
henrikhenrikperssonHenrik Persson
andauthored
MIM-589: Add endpoint for getting maintenance units by property code (#82)
* Add endpoint for getting maintenance units by property code * Simplify query and improve error handling --------- Co-authored-by: Henrik Persson <henrik.persson@iteam.com>
1 parent aba0b56 commit c227d04

File tree

3 files changed

+118
-10
lines changed

3 files changed

+118
-10
lines changed

packages/property-base/src/adapters/maintenance-units-adapter.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { trimStrings } from '@src/utils/data-conversion'
22
import { prisma } from './db'
3+
import { MaintenanceUnit } from '@src/types/maintenance-unit'
34

45
export const getMaintenanceUnitsByRentalId = async (rentalId: string) => {
56
/**
@@ -55,11 +56,52 @@ export const getMaintenanceUnitsByRentalId = async (rentalId: string) => {
5556
rentalPropertyId: rentalPropertyInfoTrimmed.rentalId,
5657
code: item.maintenanceUnit?.code,
5758
caption: item?.maintenanceUnit?.name,
58-
type: item.maintenanceUnit?.maintenanceUnitType?.name,
59-
estateCode: rentalPropertyInfoTrimmed.propertyCode,
60-
estate: rentalPropertyInfoTrimmed.propertyName,
59+
type: item.maintenanceUnit?.maintenanceUnitType?.name ?? null,
60+
propertyCode: rentalPropertyInfoTrimmed.propertyCode,
61+
propertyName: rentalPropertyInfoTrimmed.propertyName,
6162
}
6263
})
6364

6465
return maintenanceUnitPropertyStructuresMapped
6566
}
67+
68+
export const getMaintenanceUnitsByPropertyCode = async (
69+
propertyCode: string
70+
): Promise<MaintenanceUnit[]> => {
71+
const maintenanceUnits = await prisma.maintenanceUnit.findMany({
72+
where: {
73+
propertyStructures: {
74+
some: {
75+
propertyCode: propertyCode,
76+
},
77+
},
78+
},
79+
select: {
80+
id: true,
81+
code: true,
82+
name: true,
83+
maintenanceUnitType: {
84+
select: {
85+
name: true,
86+
},
87+
},
88+
propertyStructures: {
89+
select: {
90+
propertyCode: true,
91+
propertyName: true,
92+
},
93+
},
94+
},
95+
})
96+
97+
return trimStrings(maintenanceUnits).map((item) => {
98+
return {
99+
id: item.id,
100+
code: item.code,
101+
caption: item.name,
102+
type: item.maintenanceUnitType?.name ?? null,
103+
propertyCode: item.propertyStructures[0]?.propertyCode ?? null,
104+
propertyName: item.propertyStructures[0]?.propertyName ?? null,
105+
}
106+
})
107+
}

packages/property-base/src/routes/maintenance-units.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import KoaRouter from '@koa/router'
2-
import { getMaintenanceUnitsByRentalId } from '@src/adapters/maintenance-units-adapter'
3-
import { MaintenanceUnitSchema } from '@src/types/maintenance-unit'
2+
import {
3+
getMaintenanceUnitsByRentalId,
4+
getMaintenanceUnitsByPropertyCode,
5+
} from '@src/adapters/maintenance-units-adapter'
6+
import {
7+
MaintenanceUnit,
8+
MaintenanceUnitSchema,
9+
} from '@src/types/maintenance-unit'
410
import { generateRouteMetadata, logger } from 'onecore-utilities'
511

612
/**
@@ -70,4 +76,64 @@ export const routes = (router: KoaRouter) => {
7076
ctx.body = { reason: errorMessage, ...metadata }
7177
}
7278
})
79+
80+
/**
81+
* @swagger
82+
* /maintenance-units/by-property-code/{code}:
83+
* get:
84+
* summary: Get all maintenance units for a specific property code
85+
* description: |
86+
* Retrieves all maintenance units associated with a given property code.
87+
* tags:
88+
* - Maintenance units
89+
* parameters:
90+
* - in: path
91+
* name: code
92+
* required: true
93+
* schema:
94+
* type: string
95+
* description: The code of the property or which to retrieve maintenance units.
96+
* responses:
97+
* 200:
98+
* description: Successfully retrieved the maintenance units.
99+
* content:
100+
* application/json:
101+
* schema:
102+
* type: object
103+
* properties:
104+
* content:
105+
* type: array
106+
* items:
107+
* $ref: '#/components/schemas/MaintenanceUnit'
108+
* 400:
109+
* description: Invalid query parameters.
110+
* 500:
111+
* description: Internal server error.
112+
*/
113+
router.get('(.*)/maintenance-units/by-property-code/:code', async (ctx) => {
114+
const metadata = generateRouteMetadata(ctx)
115+
const { code } = ctx.params
116+
logger.info(`GET /maintenance-units/by-property-code/${code}`, metadata)
117+
118+
try {
119+
const response = await getMaintenanceUnitsByPropertyCode(code)
120+
121+
if (!response) {
122+
ctx.status = 404
123+
return
124+
}
125+
126+
ctx.body = {
127+
content: response satisfies MaintenanceUnit[],
128+
...metadata,
129+
}
130+
} catch (err) {
131+
logger.error(
132+
`Error fetching maintenance units for property code ${code}: ${err}`
133+
)
134+
ctx.status = 500
135+
const errorMessage = err instanceof Error ? err.message : 'unknown error'
136+
ctx.body = { reason: errorMessage, ...metadata }
137+
}
138+
})
73139
}

packages/property-base/src/types/maintenance-unit.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { z } from 'zod'
22

33
export const MaintenanceUnitSchema = z.object({
44
id: z.string(),
5-
rentalPropertyId: z.string(),
5+
rentalPropertyId: z.string().optional(),
66
code: z.string(),
7-
caption: z.string(),
7+
caption: z.string().nullable(),
88
type: z.string().nullable(),
9-
estateCode: z.string(),
10-
estate: z.string(),
9+
propertyCode: z.string().nullable(),
10+
propertyName: z.string().nullable(),
1111
})
1212

13-
export type MaintenanceUnitInfo = z.infer<typeof MaintenanceUnitSchema>
13+
export type MaintenanceUnit = z.infer<typeof MaintenanceUnitSchema>

0 commit comments

Comments
 (0)