From 0cabd410d8041a00395ed9f7c4b5b8e88961f247 Mon Sep 17 00:00:00 2001 From: Marc Hyeler Date: Mon, 12 Aug 2024 16:36:55 -0700 Subject: [PATCH] add new level available logic --- controllers/app/appData.js | 41 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/controllers/app/appData.js b/controllers/app/appData.js index 59d2b20..5189709 100644 --- a/controllers/app/appData.js +++ b/controllers/app/appData.js @@ -121,7 +121,7 @@ export async function queryAvailableLockers() { // Iterate over each floor in the current building for (const floor of floors) { - // Store the count for the current floor + // get levels let lockerArr = await Locker.findAll({ where: { "location.Building": {[Op.eq]: building}, // Extract building number @@ -134,25 +134,44 @@ export async function queryAvailableLockers() { model: User, }] }); - - let emptyLockerCount = 0; + // get levels in area let levels = []; - - //todo fix count not correct not sure if count is even needed, just make sure return array is not null for (let locker of lockerArr) { - if (!locker.Users || locker.Users.length === 0) { - emptyLockerCount++; - } - if (!levels.includes(locker.location.Level)) { levels.push(locker.location.Level); } } - if (emptyLockerCount === 0) continue; + let finalLevels = []; + for (let level in levels) { + let postLockerArr = await Locker.findAll({ + where: { + "location.Building": {[Op.eq]: building}, + "location.Floor": {[Op.eq]: floor}, + "location.Level": {[Op.eq]: level}, + [Op.or]: [ + {"status": {[Op.is]: null}}, // Include records where status is null + {"status": {[Op.not]: 1}} // Include records where status is not equal to 1 + ] + }, include: [{ + model: User, + }] + }); + + let emptyLockerCount = 0; + for (let locker of postLockerArr) { + if (!locker.Users || locker.Users.length === 0) { + emptyLockerCount++; + } + } + + if (emptyLockerCount !== 0) { + finalLevels.push(level) + } + } floorCounts[floor] = { - "Levels": levels, + "Levels": finalLevels, }; }