diff --git a/packages/server/src/config/data.ts b/packages/server/src/config/data.ts index 8460f500..8d010f66 100644 --- a/packages/server/src/config/data.ts +++ b/packages/server/src/config/data.ts @@ -27,7 +27,7 @@ function toLockerSubsectionData(subsection: LockerSubsection): LockerSubsectionD function fromLockerSectionData(data: LockerSectionData): LockerSection { return { subsections: data.s.L.map((subsectionData) => fromLockerSubsectionData(subsectionData.M)), - disabled: data.d.L.map((disabled) => disabled.S), + disabled: data.d.NS.map((disabled) => parseInt(disabled)), height: parseInt(data.h?.N ?? '0') }; } @@ -35,7 +35,7 @@ function fromLockerSectionData(data: LockerSectionData): LockerSection { function toLockerSectionData(section: LockerSection): LockerSectionData { return { s: { L: section.subsections.map((ss) => ({ M: toLockerSubsectionData(ss) })) }, - d: { L: section.disabled.map((d) => ({ S: d })) }, + d: { NS: section.disabled.map((d) => `${d}`) }, h: { N: `${section.height}` } }; } diff --git a/packages/server/src/util/locker.ts b/packages/server/src/util/locker.ts index 63b232a4..1a103840 100644 --- a/packages/server/src/util/locker.ts +++ b/packages/server/src/util/locker.ts @@ -1,37 +1,33 @@ -export function isValidLocker( - config: ServiceConfig, - lockerId: string, - department: string -) { +export function isValidLocker(config: ServiceConfig, lockerId: string, department: string) { const buildings = config.buildings; const parsedLockerId = lockerId.split('-'); const buildingNum = parsedLockerId[0]; const lockerFloor = parsedLockerId[1]; const lockerSection = parsedLockerId[2].substr(0, 1); const lockerSectionNum = parseInt(parsedLockerId[2].substr(1)); - const selectedSections = buildings[buildingNum]?.lockers[lockerFloor]?.[lockerSection]?.subsections; + const selectedSection = buildings[buildingNum]?.lockers[lockerFloor]?.[lockerSection]; + const selectedSubsections = selectedSection?.subsections; if (parsedLockerId.length !== 3) return false; - if (!selectedSections) return false; - const section = selectedSections.find( + if (!selectedSubsections) return false; + const section = selectedSubsections.find( (sect) => sect.range[0] <= lockerSectionNum && sect.range[1] >= lockerSectionNum && + !selectedSection.disabled.includes(lockerSectionNum) && (department === undefined || department === sect.department) ); return section !== undefined; } -export function getLockerDepartment( - config: ServiceConfig, - lockerId: string -) { +export function getLockerDepartment(config: ServiceConfig, lockerId: string) { const buildings = config.buildings; const parsedLockerId = lockerId.split('-'); const buildingNum = parsedLockerId[0]; const lockerFloor = parsedLockerId[1]; const lockerSection = parsedLockerId[2].substr(0, 1); const lockerSectionNum = parseInt(parsedLockerId[2].substr(1)); - const selectedSections = buildings[buildingNum]?.lockers[lockerFloor]?.[lockerSection]?.subsections; + const selectedSections = + buildings[buildingNum]?.lockers[lockerFloor]?.[lockerSection]?.subsections; if (parsedLockerId.length !== 3) throw new Error('Given locker is not valid'); if (!selectedSections) Error('Given locker is not valid'); const section = selectedSections.find( @@ -39,4 +35,4 @@ export function getLockerDepartment( ); if (section) return section.department; else throw new Error('Given locker is not valid'); -} \ No newline at end of file +} diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index b67f72e2..acda95c7 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -106,13 +106,13 @@ type BuildingData = { type LockerSection = { subsections: LockerSubsection[]; - disabled: string[]; + disabled: number[]; height: number; }; type LockerSectionData = { s: { L: { M: LockerSubsectionData }[] }; - d: { L: { S: string }[] }; + d: { NS: string[] }; h: { N: string }; };