11import type { NextApiRequest , NextApiResponse } from 'next' ;
22
3- import { LibraryOccupancyAPI } from '../../../screens/smu/components/widget/LibraryCapacities/components/LibraryOccupancySection/types' ;
4-
5- // NOTE: (hello@amostan.me) This is a subset of the data returned from the api
6- interface Zone {
7- name : string ;
8- locationName : string ;
9- currentOccupancy : number ;
10- maxOccupancy : number ;
11- }
12-
13- // NOTE: (hello@amostan.me) The API request returns the typo
14- const LIBRARY_KEYS = [
15- 'LKS BUIDLING COUNT' ,
16- 'LKS BUILDING COUNT' ,
17- 'KGC BUILDING COUNT' ,
18- ] ;
3+ import {
4+ LibraryApiResponse ,
5+ LibraryOccupancyAPI ,
6+ } from '../../../screens/smu/components/widget/LibraryCapacities/components/LibraryOccupancySection/types' ;
197
208export default async function getLibrariesOccupancy (
219 _ : NextApiRequest ,
@@ -36,33 +24,28 @@ export default async function getLibrariesOccupancy(
3624 } ) ;
3725
3826 // NOTE: (hello@amostan.me) Retrieve and rebuild occupancy data
39- const occupancyJson = await occupancyRes . json ( ) ;
40- const zones : Zone [ ] = occupancyJson ?. payload ?. zones ?? [ ] ;
41- const filteredZones = zones . filter ( ( zone ) => {
42- return LIBRARY_KEYS . includes ( zone . name ) ;
43- } ) ;
44-
45- const lksZone = filteredZones . find ( ( zone ) =>
46- zone . name . includes ( 'lks' . toUpperCase ( ) )
47- ) ;
48-
49- const kgcZone = filteredZones . find ( ( zone ) =>
50- zone . name . includes ( 'kgc' . toUpperCase ( ) )
51- ) ;
52-
53- const buildOccupancy : LibraryOccupancyAPI [ ] = [ ] ;
27+ const occupancyJson : LibraryApiResponse = await occupancyRes . json ( ) ;
28+ if ( ! occupancyJson . success ) {
29+ res . status ( 503 ) . end ( ) ;
30+ return ;
31+ }
5432
55- buildOccupancy . push ( {
56- key : 'lks' ,
57- maxOccupancy : lksZone ?. maxOccupancy ?? 1800 ,
58- currentOccupancy : lksZone ?. currentOccupancy ?? 0 ,
59- } ) ;
33+ const zones = occupancyJson . payload . zones ;
34+ const lksZone = zones ?. [ 20 ] ;
35+ const kgcZone = zones ?. [ 27 ] ;
6036
61- buildOccupancy . push ( {
62- key : 'kgc' ,
63- maxOccupancy : kgcZone ?. maxOccupancy ?? 500 ,
64- currentOccupancy : kgcZone ?. currentOccupancy ?? 0 ,
65- } ) ;
37+ const buildOccupancy : LibraryOccupancyAPI [ ] = [
38+ {
39+ key : 'lks' ,
40+ maxOccupancy : lksZone ?. maxOccupancy ?? 1800 ,
41+ currentOccupancy : lksZone ?. currentOccupancy ?? 0 ,
42+ } ,
43+ {
44+ key : 'kgc' ,
45+ maxOccupancy : kgcZone ?. maxOccupancy ?? 500 ,
46+ currentOccupancy : kgcZone ?. currentOccupancy ?? 0 ,
47+ } ,
48+ ] ;
6649
6750 res . status ( 200 ) . json ( buildOccupancy ) ;
6851}
0 commit comments