Couple more updates to shiftcontroller to make it more robust
This commit is contained in:
parent
a5bd0e9939
commit
0dd56c0c75
@ -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({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Shift is full'
|
error: 'Shift is full'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if already signed up - get all signups and filter
|
// Check if already signed up - we already have allSignups from above
|
||||||
const allSignups = await nocodbService.getAll(config.nocodb.shiftSignupsSheetId);
|
|
||||||
const existingSignup = (allSignups.list || []).find(signup => {
|
const existingSignup = (allSignups.list || []).find(signup => {
|
||||||
return signup['Shift ID'] === parseInt(shiftId) &&
|
return signup['Shift ID'] === parseInt(shiftId) &&
|
||||||
signup['User Email'] === userEmail &&
|
signup['User Email'] === userEmail &&
|
||||||
@ -190,10 +199,10 @@ class ShiftsController {
|
|||||||
|
|
||||||
logger.info('Created signup:', signup);
|
logger.info('Created signup:', signup);
|
||||||
|
|
||||||
// Update shift volunteer count
|
// Update shift volunteer count with calculated value
|
||||||
await nocodbService.update(config.nocodb.shiftsSheetId, shiftId, {
|
await nocodbService.update(config.nocodb.shiftsSheetId, shiftId, {
|
||||||
'Current Volunteers': (shift['Current Volunteers'] || 0) + 1,
|
'Current Volunteers': currentVolunteers + 1,
|
||||||
'Status': shift['Current Volunteers'] + 1 >= shift['Max Volunteers'] ? 'Full' : 'Open'
|
'Status': currentVolunteers + 1 >= shift['Max Volunteers'] ? 'Full' : 'Open'
|
||||||
});
|
});
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
@ -245,10 +254,14 @@ class ShiftsController {
|
|||||||
'Status': 'Cancelled'
|
'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 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, {
|
await nocodbService.update(config.nocodb.shiftsSheetId, shiftId, {
|
||||||
'Current Volunteers': newCount,
|
'Current Volunteers': newCount,
|
||||||
'Status': newCount >= shift['Max Volunteers'] ? 'Full' : 'Open'
|
'Status': newCount >= shift['Max Volunteers'] ? 'Full' : 'Open'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user