From 92682c218b4cce350d570964437db95bc55e01f8 Mon Sep 17 00:00:00 2001 From: Andre-Diamond <32074058+Andre-Diamond@users.noreply.github.com> Date: Thu, 15 Feb 2024 07:46:07 +0200 Subject: [PATCH] *danger of duplicate summaries* - [Date: 2024-02-14] Fixes #71 --- components/ArchiveSummaries.tsx | 36 +++++++++++++++++++++++---------- utils/filterFormData.ts | 4 ++++ utils/getsummaries.js | 19 ++++++++--------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/components/ArchiveSummaries.tsx b/components/ArchiveSummaries.tsx index f9b97b9..7110713 100644 --- a/components/ArchiveSummaries.tsx +++ b/components/ArchiveSummaries.tsx @@ -56,30 +56,44 @@ const ArchiveSummaries = () => { } }; - async function handleSubmit(e: any) { + async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); - if (myVariable.summary.confirmed == false) { + + // Check if there are any confirmed summaries with the same date + const isDuplicateConfirmedSummary = myVariable.summaries.some((summary: any) => { + // Convert both dates to Date objects to strip off the time part + const summaryDate = new Date(summary.date).toDateString(); + const formDataDate = new Date(formData.date).toDateString(); + //console.log(summaryDate, formDataDate, summaryDate === formDataDate && summary.confirmed); // For debugging purposes + // Compare the date strings + return summaryDate === formDataDate && summary.confirmed; + }); + + if (isDuplicateConfirmedSummary) { + alert('A confirmed summary for this date already exists.'); + setLoading(false); + return; // Exit the function early + } + + if (!myVariable.summary.confirmed) { const data = await updateGitbook(formData); - //console.log("returned from util function", formData) if (data) { - setMyVariable({ - ...myVariable, + setMyVariable(prevState => ({ + ...prevState, summary: { - ...myVariable.summary, + ...prevState.summary, confirmed: true, }, - }); + })); } - //console.log(myVariable) await sendDiscordMessage(myVariable, renderedMarkdown); - //console.log(myVariable, "renderedMarkdown", renderedMarkdown) } else { - alert('Summary already archived') + alert('Summary already archived'); } setLoading(false); - } + } return (
diff --git a/utils/filterFormData.ts b/utils/filterFormData.ts index dbb69ae..7a74d1d 100644 --- a/utils/filterFormData.ts +++ b/utils/filterFormData.ts @@ -2,6 +2,10 @@ type AnyObj = { [key: string]: any }; export function filterFormData(data: AnyObj): AnyObj { + + if (data === null || data === undefined || Object.keys(data).length === 0) { + return {}; // Return an empty object or some default value as appropriate + } if (!data.confirmed) { return data; // Return the original data unmodified if not confirmed } diff --git a/utils/getsummaries.js b/utils/getsummaries.js index a21266e..f755f06 100644 --- a/utils/getsummaries.js +++ b/utils/getsummaries.js @@ -22,9 +22,8 @@ import { supabase } from "../lib/supabaseClient"; export async function getSummaries(workgroup_id) { let summaries = []; // Initialize an array to hold the summaries let summary = {}; - let lastConfirmedDate = null; - let lastUpdatedAt = null; let lastConfirmedMeeting = null; + let confirmedDates = new Set(); async function getMeetingSummaries() { try { @@ -41,17 +40,19 @@ export async function getSummaries(workgroup_id) { if (summaryData) { // Find the last confirmed meeting lastConfirmedMeeting = summaryData.find(data => data.confirmed === true); - if (lastConfirmedMeeting) { - lastConfirmedDate = lastConfirmedMeeting.date; - lastUpdatedAt = lastConfirmedMeeting.updated_at; - } + summaryData.forEach(data => { + if (data.confirmed) confirmedDates.add(data.date); + }); // Fetch the user details for each summary for (const data of summaryData) { - // Exclude meetings where confirmed == true and all meetings with an earlier date and updated_at date than the last meeting where confirmed == true - /*if (data.confirmed === true || new Date(data.date) < new Date(lastConfirmedDate)) { //Might have to add this back in later - || new Date(data.updated_at) < new Date(lastUpdatedAt) + const isSameDateAsLastConfirmed = lastConfirmedMeeting && new Date(data.date).getTime() === new Date(lastConfirmedMeeting.date).getTime(); + const isDateWithOtherConfirmed = confirmedDates.has(data.date) && (!lastConfirmedMeeting || (lastConfirmedMeeting && !isSameDateAsLastConfirmed)); + + // Skip if it's not the last confirmed and it's a date with another confirmed summary + if (data.confirmed && !isSameDateAsLastConfirmed || isDateWithOtherConfirmed) { continue; - }*/ + } const { data: userData, error: userError } = await supabase .from('users')