From 0dd56c0c7580e93aada6cf64721ed4d9ded81639 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 16 Jul 2025 09:51:55 -0600 Subject: [PATCH] Couple more updates to shiftcontroller to make it more robust --- map/app/controllers/shiftsController.js | 31 ++++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/map/app/controllers/shiftsController.js b/map/app/controllers/shiftsController.js index fe6f364..60358f9 100644 --- a/map/app/controllers/shiftsController.js +++ b/map/app/controllers/shiftsController.js @@ -157,15 +157,24 @@ class ShiftsController { }); } - if (shift['Current Volunteers'] >= shift['Max Volunteers']) { + // Calculate current volunteers dynamically + let currentVolunteers = 0; + if (config.nocodb.shiftSignupsSheetId) { + const allSignups = await nocodbService.getAll(config.nocodb.shiftSignupsSheetId); + const confirmedSignups = (allSignups.list || []).filter(signup => + signup['Shift ID'] === parseInt(shiftId) && signup.Status === 'Confirmed' + ); + currentVolunteers = confirmedSignups.length; + } + + if (currentVolunteers >= shift['Max Volunteers']) { return res.status(400).json({ success: false, error: 'Shift is full' }); } - // Check if already signed up - get all signups and filter - const allSignups = await nocodbService.getAll(config.nocodb.shiftSignupsSheetId); + // Check if already signed up - we already have allSignups from above const existingSignup = (allSignups.list || []).find(signup => { return signup['Shift ID'] === parseInt(shiftId) && signup['User Email'] === userEmail && @@ -190,10 +199,10 @@ class ShiftsController { logger.info('Created signup:', signup); - // Update shift volunteer count + // Update shift volunteer count with calculated value await nocodbService.update(config.nocodb.shiftsSheetId, shiftId, { - 'Current Volunteers': (shift['Current Volunteers'] || 0) + 1, - 'Status': shift['Current Volunteers'] + 1 >= shift['Max Volunteers'] ? 'Full' : 'Open' + 'Current Volunteers': currentVolunteers + 1, + 'Status': currentVolunteers + 1 >= shift['Max Volunteers'] ? 'Full' : 'Open' }); res.json({ @@ -245,10 +254,14 @@ class ShiftsController { 'Status': 'Cancelled' }); - // Update shift volunteer count + // Calculate current volunteers dynamically after cancellation + const allSignupsAfter = await nocodbService.getAll(config.nocodb.shiftSignupsSheetId); + const confirmedSignupsAfter = (allSignupsAfter.list || []).filter(s => + s['Shift ID'] === parseInt(shiftId) && s.Status === 'Confirmed' + ); + const newCount = confirmedSignupsAfter.length; + const shift = await nocodbService.getById(config.nocodb.shiftsSheetId, shiftId); - const newCount = Math.max(0, (shift['Current Volunteers'] || 0) - 1); - await nocodbService.update(config.nocodb.shiftsSheetId, shiftId, { 'Current Volunteers': newCount, 'Status': newCount >= shift['Max Volunteers'] ? 'Full' : 'Open'