Skip to content

Commit cfc0f1b

Browse files
authored
fix(smu-libraries): Switch SMU Libraries new API key (#143, PR #144)
* chore(libraries): add new types * fix(smu): Use library new apiKey
1 parent 4ef65a8 commit cfc0f1b

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed
Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import 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

208
export 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
}

src/screens/smu/components/widget/LibraryCapacities/components/LibraryOccupancySection/types.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,36 @@ export interface LibraryBackgroundImage {
2727
};
2828
};
2929
}
30+
31+
export interface LibraryApiResponse {
32+
success: boolean;
33+
message: string;
34+
payload: LibraryApiPayload;
35+
}
36+
37+
export interface LibraryApiPayload {
38+
zones: LibraryApiZone[];
39+
}
40+
41+
export interface LibraryApiZone {
42+
id: string;
43+
tenantId: string;
44+
locationId: string;
45+
parentZoneId: string | null;
46+
name: string;
47+
currentOccupancy: number;
48+
maxOccupancy: number;
49+
zoneTypeId: number;
50+
privateName: string | null;
51+
showOnDashboard: boolean;
52+
includeInLocationCount: boolean;
53+
enterCorrectionInterval: number;
54+
exitCorrectionInterval: number;
55+
subscriptionId: string;
56+
subscriptionShortName: string;
57+
locationName: string;
58+
tenantName: string;
59+
alarmEnabled: boolean;
60+
status: any[];
61+
zoneDataTypes: string[];
62+
}

0 commit comments

Comments
 (0)