Skip to content

Commit f0d0dc6

Browse files
Henrik Perssonjohanneskarlsson
authored andcommitted
MIM-551: adds endpoints in property
1 parent 0e784c4 commit f0d0dc6

File tree

2 files changed

+289
-0
lines changed

2 files changed

+289
-0
lines changed

services/property/src/adapters/facility-adapter.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,107 @@ export const getFacilityByRentalId = async (rentalId: string) => {
8080
}
8181
}
8282

83+
export const getFacilitiesByPropertyCode = async (propertyCode: string) => {
84+
try {
85+
const result = await prisma.propertyStructure.findMany({
86+
where: {
87+
propertyCode,
88+
propertyObject: { objectTypeId: 'balok' },
89+
},
90+
select: {
91+
propertyObject: {
92+
select: {
93+
facility: {
94+
select: {
95+
id: true,
96+
location: true,
97+
entrance: true,
98+
usage: true,
99+
name: true,
100+
code: true,
101+
propertyObjectId: true,
102+
availableFrom: true,
103+
availableTo: true,
104+
deleteMark: true,
105+
facilityType: { select: { code: true, name: true } },
106+
},
107+
},
108+
},
109+
},
110+
},
111+
})
112+
113+
assert(result, 'property-structure-not-found')
114+
115+
const facilities = result
116+
.map((item) => item.propertyObject.facility)
117+
.filter((facility) => facility)
118+
119+
return trimStrings(facilities)
120+
} catch (err) {
121+
if (
122+
err instanceof Error &&
123+
err.message === 'property-structure-not-found'
124+
) {
125+
return null
126+
}
127+
128+
logger.error({ err }, 'facility-adapter.getFacilitiesByPropertyCode')
129+
throw err
130+
}
131+
}
132+
133+
export const getFacilitiesByBuildingCode = async (buildingCode: string) => {
134+
try {
135+
const result = await prisma.propertyStructure.findMany({
136+
where: {
137+
buildingCode,
138+
propertyObject: { objectTypeId: 'balok' },
139+
},
140+
select: {
141+
buildingCode: true,
142+
propertyObject: {
143+
select: {
144+
facility: {
145+
select: {
146+
id: true,
147+
location: true,
148+
entrance: true,
149+
usage: true,
150+
name: true,
151+
code: true,
152+
propertyObjectId: true,
153+
availableFrom: true,
154+
availableTo: true,
155+
deleteMark: true,
156+
facilityType: { select: { code: true, name: true } },
157+
},
158+
},
159+
},
160+
},
161+
},
162+
})
163+
164+
assert(result, 'property-structure-not-found')
165+
166+
const facilities = result
167+
.map((item) => item.propertyObject.facility)
168+
.filter((facility) => facility)
169+
170+
return trimStrings(facilities)
171+
} catch (err) {
172+
if (
173+
err instanceof Error &&
174+
err.message === 'property-structure-not-found'
175+
) {
176+
return null
177+
}
178+
179+
logger.error({ err }, 'facility-adapter.getFacilitiesByPropertyCode')
180+
throw err
181+
}
182+
}
183+
83184
export const getFacilitySizeByRentalId = async (rentalId: string) => {
84185
try {
85186
const propertyInfo = await prisma.propertyStructure.findFirst({

services/property/src/routes/facilities.ts

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import KoaRouter from '@koa/router'
22
import { logger, generateRouteMetadata } from '@onecore/utilities'
33

44
import {
5+
getFacilitiesByBuildingCode,
6+
getFacilitiesByPropertyCode,
57
getFacilityByRentalId,
68
getFacilitySizeByRentalId,
79
} from '@src/adapters/facility-adapter'
@@ -102,4 +104,190 @@ export const routes = (router: KoaRouter) => {
102104
ctx.body = { reason: errorMessage, ...metadata }
103105
}
104106
})
107+
108+
/**
109+
* @swagger
110+
* /facilities/property-code/{propertyCode}:
111+
* get:
112+
* summary: Get facilities by property code
113+
* description: Returns a list of facilities for the specified property code
114+
* tags:
115+
* - Facilities
116+
* parameters:
117+
* - in: path
118+
* name: propertyCode
119+
* required: true
120+
* schema:
121+
* type: string
122+
* description: The property code of the property
123+
* responses:
124+
* 200:
125+
* description: Successfully retrieved the facilities
126+
* content:
127+
* application/json:
128+
* schema:
129+
* $ref: '#/components/schemas/GetFacilityByRentalIdResponse'
130+
* 404:
131+
* description: Facility not found
132+
* 500:
133+
* description: Internal server error
134+
*/
135+
router.get('(.*)/facilities/property-code/:propertyCode', async (ctx) => {
136+
const metadata = generateRouteMetadata(ctx)
137+
logger.info(
138+
`GET /facilities/property-code/${ctx.params.propertyCode}`,
139+
metadata
140+
)
141+
142+
try {
143+
const facilities = await getFacilitiesByPropertyCode(
144+
ctx.params.propertyCode
145+
)
146+
if (!facilities) {
147+
ctx.status = 404
148+
ctx.body = { reason: 'facility-not-found', ...metadata }
149+
return
150+
}
151+
152+
const payload = facilities
153+
154+
// const payload: GetFacilityByRentalIdResponse = {
155+
// content: {
156+
// id: facility.propertyObject.facility.id,
157+
// code: facility.propertyObject.facility.code,
158+
// name: facility.propertyObject.facility.name,
159+
// entrance: facility.propertyObject.facility.entrance,
160+
// deleted: Boolean(facility.propertyObject.facility.deleteMark),
161+
// type: {
162+
// code: facility.propertyObject.facility.facilityType.code,
163+
// name: facility.propertyObject.facility.facilityType.name,
164+
// },
165+
// areaSize: areaSize?.value ?? null,
166+
// building: {
167+
// id: facility.buildingId,
168+
// code: facility.buildingCode,
169+
// name: facility.buildingName,
170+
// },
171+
// property: {
172+
// id: facility.propertyId,
173+
// code: facility.propertyCode,
174+
// name: facility.propertyName,
175+
// },
176+
// rentalInformation: {
177+
// rentalId: facility.rentalId,
178+
// apartmentNumber:
179+
// facility.propertyObject.rentalInformation.apartmentNumber,
180+
// type: {
181+
// code: facility.propertyObject.rentalInformation
182+
// .rentalInformationType.code,
183+
// name: facility.propertyObject.rentalInformation
184+
// .rentalInformationType.name,
185+
// },
186+
// },
187+
// },
188+
// ...metadata,
189+
// }
190+
191+
ctx.status = 200
192+
ctx.body = payload
193+
} catch (err) {
194+
logger.error(err, 'Error fetching facility by rental id')
195+
ctx.status = 500
196+
const errorMessage = err instanceof Error ? err.message : 'unknown error'
197+
ctx.body = { reason: errorMessage, ...metadata }
198+
}
199+
})
200+
201+
/**
202+
* @swagger
203+
* /facilities/building-code/{buildingCode}:
204+
* get:
205+
* summary: Get facilities by building code
206+
* description: Returns a list of facilities for the specified building code
207+
* tags:
208+
* - Facilities
209+
* parameters:
210+
* - in: path
211+
* name: buildingCode
212+
* required: true
213+
* schema:
214+
* type: string
215+
* description: The building code of the building
216+
* responses:
217+
* 200:
218+
* description: Successfully retrieved the facilities
219+
* content:
220+
* application/json:
221+
* schema:
222+
* $ref: '#/components/schemas/GetFacilityByRentalIdResponse'
223+
* 404:
224+
* description: Facility not found
225+
* 500:
226+
* description: Internal server error
227+
*/
228+
router.get('(.*)/facilities/building-code/:buildingCode', async (ctx) => {
229+
const metadata = generateRouteMetadata(ctx)
230+
logger.info(
231+
`GET /facilities/building-code/${ctx.params.buildingCode}`,
232+
metadata
233+
)
234+
235+
try {
236+
const facilities = await getFacilitiesByBuildingCode(
237+
ctx.params.buildingCode
238+
)
239+
if (!facilities) {
240+
ctx.status = 404
241+
ctx.body = { reason: 'facility-not-found', ...metadata }
242+
return
243+
}
244+
245+
const payload = facilities
246+
247+
// const payload: GetFacilityByRentalIdResponse = {
248+
// content: {
249+
// id: facility.propertyObject.facility.id,
250+
// code: facility.propertyObject.facility.code,
251+
// name: facility.propertyObject.facility.name,
252+
// entrance: facility.propertyObject.facility.entrance,
253+
// deleted: Boolean(facility.propertyObject.facility.deleteMark),
254+
// type: {
255+
// code: facility.propertyObject.facility.facilityType.code,
256+
// name: facility.propertyObject.facility.facilityType.name,
257+
// },
258+
// areaSize: areaSize?.value ?? null,
259+
// building: {
260+
// id: facility.buildingId,
261+
// code: facility.buildingCode,
262+
// name: facility.buildingName,
263+
// },
264+
// property: {
265+
// id: facility.propertyId,
266+
// code: facility.propertyCode,
267+
// name: facility.propertyName,
268+
// },
269+
// rentalInformation: {
270+
// rentalId: facility.rentalId,
271+
// apartmentNumber:
272+
// facility.propertyObject.rentalInformation.apartmentNumber,
273+
// type: {
274+
// code: facility.propertyObject.rentalInformation
275+
// .rentalInformationType.code,
276+
// name: facility.propertyObject.rentalInformation
277+
// .rentalInformationType.name,
278+
// },
279+
// },
280+
// },
281+
// ...metadata,
282+
// }
283+
284+
ctx.status = 200
285+
ctx.body = payload
286+
} catch (err) {
287+
logger.error(err, 'Error fetching facility by rental id')
288+
ctx.status = 500
289+
const errorMessage = err instanceof Error ? err.message : 'unknown error'
290+
ctx.body = { reason: errorMessage, ...metadata }
291+
}
292+
})
105293
}

0 commit comments

Comments
 (0)