Hi everyone,
I’m working on a backend workflow in Bubble to calculate the working days for each month by subtracting holiday days from the total days in that month. Here’s the setup:
Data Overview
- Holiday Data (
paramlist1
):
Each holiday includes:
start_date
(Date)end_date
(Date)month_number
(Numeric representation of the month)date_count
(Total number of holiday days in that month: end - start)
Here’s the JavaScript I wrote in Run JavaScript action. I’m using the bubble_fn_calculate
function to return the results back to Bubble:
var months = [
{ month: "January", month_number: 1, total_days: 31 },
{ month: "February", month_number: 2, total_days: (new Date().getFullYear() % 4 === 0 && (new Date().getFullYear() % 100 !== 0 || new Date().getFullYear() % 400 === 0)) ? 29 : 28 },
{ month: "March", month_number: 3, total_days: 31 },
{ month: "April", month_number: 4, total_days: 30 },
{ month: "May", month_number: 5, total_days: 31 },
{ month: "June", month_number: 6, total_days: 30 },
{ month: "July", month_number: 7, total_days: 31 },
{ month: "August", month_number: 8, total_days: 31 },
{ month: "September", month_number: 9, total_days: 30 },
{ month: "October", month_number: 10, total_days: 31 },
{ month: "November", month_number: 11, total_days: 30 },
{ month: "December", month_number: 12, total_days: 31 }
];
// Retrieve the holidays list
var holidaysLength = properties.paramlist1.length();
var holidays = properties.paramlist1.get(0, holidaysLength); // List of holidays
var workingDaysList = [];
// Iterate through the list of months
months.forEach(month => {
// Find the holiday for the current month
var holiday = holidays.find(h => {
var startDate = h.get("start_date");
return startDate && startDate.getMonth() + 1 === month.month_number;
});
var holidayDays = holiday ? holiday.get("date_count") || 0 : 0;
var workingDays = month.total_days - holidayDays;
workingDaysList.push({
month: month.month,
working_days: workingDays
});
});
// Return the working days list to the Bubble workflow
bubble_fn_calculate(workingDaysList);
Problem is I dont know how to exactly show the result in the repeating group. Also don’t know if the code works correctly or not. Any help?