Skip to content

Commit

Permalink
Merge pull request #41 from EATSTEAK/feat/locker_disabled
Browse files Browse the repository at this point in the history
비활성화된 사물함에 대한 대여 불가 처리
  • Loading branch information
Twince authored Aug 21, 2022
2 parents 388547d + 508b016 commit 5a6c306
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/server/src/config/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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')
};
}

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}` }
};
}
Expand Down
24 changes: 10 additions & 14 deletions packages/server/src/util/locker.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
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(
(sect) => sect.range[0] <= lockerSectionNum && sect.range[1] >= lockerSectionNum
);
if (section) return section.department;
else throw new Error('Given locker is not valid');
}
}
4 changes: 2 additions & 2 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

Expand Down

0 comments on commit 5a6c306

Please sign in to comment.