File tree Expand file tree Collapse file tree 4 files changed +46
-4
lines changed
services/property-base-service Expand file tree Collapse file tree 4 files changed +46
-4
lines changed Original file line number Diff line number Diff line change @@ -360,9 +360,7 @@ type GetMaintenanceUnitsByBuildingCodeResponse =
360360
361361export async function getMaintenanceUnitsByBuildingCode (
362362 buildingCode : string
363- ) : Promise <
364- AdapterResult < GetMaintenanceUnitsByBuildingCodeResponse , 'unknown' >
365- > {
363+ ) : Promise < AdapterResult < GetMaintenanceUnitsByBuildingCodeResponse , unknown > > {
366364 try {
367365 const fetchResponse = await client ( ) . GET (
368366 '/maintenance-units/by-building-code/{code}' ,
@@ -375,13 +373,17 @@ export async function getMaintenanceUnitsByBuildingCode(
375373 return { ok : true , data : fetchResponse . data . content }
376374 }
377375
376+ if ( fetchResponse . response . status === 404 ) {
377+ return { ok : false , err : 'not-found' }
378+ }
379+
378380 return { ok : false , err : 'unknown' }
379381 } catch ( err ) {
380382 logger . error (
381383 { err } ,
382384 '@onecore/property-adapter.getMaintenanceUnitsByBuildingCode'
383385 )
384- return { ok : false , err : 'unknown' }
386+ return { ok : false , err }
385387 }
386388}
387389
Original file line number Diff line number Diff line change @@ -536,6 +536,21 @@ describe('@onecore/property-adapter', () => {
536536 } )
537537 } )
538538
539+ it ( 'returns not-found if building code is not found' , async ( ) => {
540+ mockServer . use (
541+ http . get (
542+ `${ config . propertyBaseService . url } /maintenance-units/by-building-code/123-123` ,
543+ ( ) => new HttpResponse ( null , { status : 404 } )
544+ )
545+ )
546+
547+ const result =
548+ await propertyBaseAdapter . getMaintenanceUnitsByBuildingCode ( '123-123' )
549+
550+ expect ( result . ok ) . toBe ( false )
551+ if ( ! result . ok ) expect ( result . err ) . toBe ( 'not-found' )
552+ } )
553+
539554 it ( 'returns err if request fails' , async ( ) => {
540555 mockServer . use (
541556 http . get (
Original file line number Diff line number Diff line change @@ -1031,6 +1031,12 @@ export const routes = (router: KoaRouter) => {
10311031 )
10321032
10331033 if ( ! result . ok ) {
1034+ if ( result . err === 'not-found' ) {
1035+ ctx . status = 404
1036+ ctx . body = { error : 'No maintenance units found' , ...metadata }
1037+ return
1038+ }
1039+
10341040 logger . error (
10351041 result . err ,
10361042 'Error getting maintenance units from property-base' ,
@@ -1041,6 +1047,12 @@ export const routes = (router: KoaRouter) => {
10411047 return
10421048 }
10431049
1050+ if ( result . data . length === 0 ) {
1051+ ctx . status = 404
1052+ ctx . body = { error : 'No maintenance units found' , ...metadata }
1053+ return
1054+ }
1055+
10441056 ctx . body = {
10451057 content : result . data satisfies Array < schemas . MaintenanceUnit > ,
10461058 ...metadata ,
Original file line number Diff line number Diff line change @@ -420,6 +420,19 @@ describe('@onecore/property-service', () => {
420420 ) . not . toThrow ( )
421421 } )
422422
423+ it ( 'returns 404 if building code is not found' , async ( ) => {
424+ const getMaintenanceUnitsSpy = jest
425+ . spyOn ( propertyBaseAdapter , 'getMaintenanceUnitsByBuildingCode' )
426+ . mockResolvedValueOnce ( { ok : false , err : 'not-found' } )
427+
428+ const res = await request ( app . callback ( ) ) . get (
429+ '/propertyBase/maintenance-units/by-building-code/123-456'
430+ )
431+
432+ expect ( res . status ) . toBe ( 404 )
433+ expect ( getMaintenanceUnitsSpy ) . toHaveBeenCalledWith ( '123-456' )
434+ } )
435+
423436 it ( 'returns 500 if no maintenance units can be retrieved' , async ( ) => {
424437 const getMaintenanceUnitsSpy = jest
425438 . spyOn ( propertyBaseAdapter , 'getMaintenanceUnitsByBuildingCode' )
You can’t perform that action at this time.
0 commit comments