@@ -2,6 +2,8 @@ import KoaRouter from '@koa/router'
22import { logger , generateRouteMetadata } from '@onecore/utilities'
33
44import {
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