#!/usr/bin/perl use strict; use warnings; use JSON; # Read input.json file my $input_file = "input.json"; open(my $fh, '<', $input_file) or die "Failed to open file: $!"; my $json_data = do { local $/; <$fh> }; close($fh); # Parse JSON data my $data = decode_json($json_data); # Initialize the output data structure my $output_data = { "channel" => "#snl-out-list", "blocks" => [ { "type" => "section", "text" => { "type" => "mrkdwn", "text" => "*Absences within the next 7 days:*" } } ], "username" => "My Bot" }; # Track the processed full names and their absence lines my %processed_full_names; my %absence_lines; # Iterate over each absence record in the input data foreach my $record (@$data) { my $fullName = $record->{fullName}; my $startDate = $record->{startDate}; my $endDate = $record->{endDate}; my $duration = $record->{duration}; my $absenceType = $record->{absenceType}; # Construct the absence line my $absence_line = "*$startDate" . ($startDate eq $endDate ? "" : " - $endDate") . "*: $duration hours: $absenceType"; # Append the absence line to the respective full name's absence lines push @{$absence_lines{$fullName}}, $absence_line; } # Sort full names by last name in ascending order my @sorted_full_names = sort { my ($ln1) = ($a =~ /(\w+)$/); my ($ln2) = ($b =~ /(\w+)$/); lc($ln1) cmp lc($ln2); } keys %absence_lines; # Iterate over each full name and their absence lines foreach my $fullName (@sorted_full_names) { # Create the absence section my $absence_section = { "type" => "section", "text" => { "type" => "mrkdwn", "text" => ":palm_tree: $fullName" } }; # Append the absence section to the output data push @{$output_data->{blocks}}, $absence_section; # Iterate over each absence line for the current full name foreach my $absence_line (@{$absence_lines{$fullName}}) { # Create the absence field my $absence_field = { "type" => "section", "text" => { "type" => "mrkdwn", "text" => $absence_line } }; # Append the absence field to the output data push @{$output_data->{blocks}}, $absence_field; } # Mark the full name as processed $processed_full_names{$fullName} = 1; } # Encode the output data as JSON my $output_json = encode_json($output_data); # Write the output JSON to output.json file my $output_file = "output.json"; open(my $out_fh, '>', $output_file) or die "Failed to open file: $!"; print $out_fh $output_json; close($out_fh);