]> git.sesse.net Git - skvidarsync/commitdiff
Fix an issue where one could be deleted from the database even if we were unable...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 28 Oct 2023 11:56:55 +0000 (13:56 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 28 Oct 2023 11:56:55 +0000 (13:56 +0200)
bin/sync.pl

index 3f07537389ce496e1655b2df1e0104f6baa95256..6c4db28fd3ccff67766b0bed1c394bd6988943dc 100644 (file)
@@ -331,6 +331,7 @@ for my $userid (@attending_userids) {
 
        # See if we can find them in the spreadsheet.
        if (!exists($seen_names{lc $real_name})) {
+               # TODO: Perhaps move this logic further down, for consistency?
                skv_log("$slack_name ($userid) er påmeldt på Slack, og er mappet til $real_name, men var ikke i noen gruppe.");
        } else {
                my $seen = $seen_names{lc $real_name};
@@ -373,20 +374,25 @@ for my $real_name (keys %want_names) {
 }
 for my $real_name (keys %have_names) {
        next if (exists($want_names{$real_name}));
-       skv_log("Fjerner at $real_name skal på trening.");
-       push @diffs, [
-               $real_name,
-               {
-                       backgroundColor => {
-                               red => 1,
-                               green => 1,
-                               blue => 1,
-                               alpha => 0
+       if (!exists($seen_names{lc $real_name})) {
+               # TODO: This can somehow come if we try to add someone who's not in the sheet, too?
+               skv_log("Ønsket å fjerne at $real_name skulle på trening, men de var ikke i regnearket lenger.");
+       } else {
+               skv_log("Fjerner at $real_name skal på trening.");
+               push @diffs, [
+                       $real_name,
+                       {
+                               backgroundColor => {
+                                       red => 1,
+                                       green => 1,
+                                       blue => 1,
+                                       alpha => 0
+                               }
                        }
-               }
-       ];
-       $dbh->do('DELETE FROM applied WHERE channel=? AND ts=? AND name=?', undef,
-               $config::invitation_channel, $invitation_ts, $real_name);
+               ];
+               $dbh->do('DELETE FROM applied WHERE channel=? AND ts=? AND name=?', undef,
+                       $config::invitation_channel, $invitation_ts, $real_name);
+       }
 }
 
 my @yellow_updates = ();
@@ -396,33 +402,27 @@ if (scalar @diffs > 0) {
                my $real_name = $diff->[0];
 
                # See if we can find them in the spreadsheet.
-               if (!exists($seen_names{lc $real_name})) {
-                       # This can only happen on deletes; we don't add such cases to the positive diff.
-                       # FIXME we'd still delete them from the database.
-                       skv_log("Ønsket å fjerne at $real_name skulle på trening, men de var ikke i regnearket lenger.");
-               } else {
-                       my $seen = $seen_names{lc $real_name};
-                       die if (scalar @$seen > 1);
-                       my $rowno = $seen->[0][1];
-                       my $colno = $seen->[0][2];
-                       push @yellow_updates, {
-                               updateCells => {
-                                       rows => [{
-                                               values => [{
-                                                       userEnteredFormat => $diff->[1]
-                                               }]
-                                       }],
-                                       fields => 'userEnteredFormat.backgroundColor',
-                                       range => {
-                                               sheetId => $tab_id,
-                                               startRowIndex => $rowno,
-                                               endRowIndex => $rowno + 1,
-                                               startColumnIndex => $colno,
-                                               endColumnIndex => $colno + 1
-                                       }
+               my $seen = $seen_names{lc $real_name};
+               die if (!defined($seen) || scalar @$seen > 1);
+               my $rowno = $seen->[0][1];
+               my $colno = $seen->[0][2];
+               push @yellow_updates, {
+                       updateCells => {
+                               rows => [{
+                                       values => [{
+                                               userEnteredFormat => $diff->[1]
+                                       }]
+                               }],
+                               fields => 'userEnteredFormat.backgroundColor',
+                               range => {
+                                       sheetId => $tab_id,
+                                       startRowIndex => $rowno,
+                                       endRowIndex => $rowno + 1,
+                                       startColumnIndex => $colno,
+                                       endColumnIndex => $colno + 1
                                }
-                       };
-               }
+                       }
+               };
        }
 }