Wix blog and database on VELO

Question:
Hello everyone, everything good?

I came to ask for your help. I’m trying to show the URGENT SECTION if there is a post published on the current date that contains the URGENT tag. The tag will be consulted in the “tags” field of the “Blog/Tags” database, the posts are in the “Blog/Posts” database, within the “Blog/Posts” database there is a “tags” field that does reference to the “posts” field that shows the posts that contain the tags. The post’s publication date is in the “Blog/Posts” database, which contains the “publishedDate” field, where the post’s publication date is stored. However, despite having already edited other codes, I still haven’t managed to do it. Someone help me, please!

Product:
Editor Wix

Additional information:

import wixData from 'wix-data';

async function checkForUrgentPost() {
    try {
        // Get the current date
        const currentDate = new Date();

        // Format the current date to match the format of "publishedDate" in the database
        const formattedDate = currentDate.toLocaleDateString('en-US', {
            day: '2-digit',
            month: 'short',
            year: 'numeric'
        }).replace('.', '');

        // Format the month abbreviation to match the database format
        const formattedMonth = formattedDate.substr(3, 3);

        // Replace the abbreviated month in formattedDate
        const formattedDateForComparison = formattedDate.replace(formattedMonth, formattedMonth.toUpperCase());

        console.log("Formatted Date for Comparison:", formattedDateForComparison);

        // Query the "Blog/Tags" database to find tags with the "URGENTE" label
        const tagsResult = await wixData.query("Blog/Tags")
            .eq("label", "URGENTE")
            .find();

        console.log("Tags with URGENTE label:", tagsResult);

        // If there are tags with the "URGENTE" label
        if (tagsResult.items.length > 0) {
            // Check each tag to see if it contains references to posts published today
            const urgentPostPublishedToday = tagsResult.items.some(tag => {
                // Check if the tag has posts and if any of the references to posts match the publishedDate of today
                return tag.posts && tag.posts.some(postRef => {
                    // Format the publishedDate of the post reference to match the format of "formattedDate"
                    const postPublishedDate = postRef.publishedDate.replace('.', '');

                    console.log("Post Published Date:", postPublishedDate);

                    return postPublishedDate === formattedDateForComparison;
                });
            });

            console.log("Urgent Post Published Today:", urgentPostPublishedToday);

            // If there are posts associated with the "URGENTE" tag published today, show the "URGENTE" section
            if (urgentPostPublishedToday) {
                $w("#secaoUrgente").show();
            } else {
                $w("#secaoUrgente").hide();
            }
        } else {
            // Hide the "URGENTE" section if there are no tags with the "URGENTE" label
            $w("#secaoUrgente").hide();
        }
    } catch (error) {
        console.error("Error:", error);
    }
}

// Call the function to check for urgent posts
checkForUrgentPost();

My guess is that postPublishedDate and formattedDateForComparison aren’t matching up correctly. Both are Date objects though so you may want to try simplifying the comparisons first and comparing the dates directly.

There’s libraries like date-fns (which can be installed) that provide a lot of helper functions for doing these types of comparisons. For example differenceInCalendarDays can be used to see how far apart 2 dates are. If they’re 0 days apart then they’re the same day. There’s an equivalent function for hours so the comparison could be “within 24 hours” too depending on how you want to define a day.

Probably also worth splitting up the logic between checking for a given tag AND checking if the posts are on the same day. Would reduce the nested code and can then if(isUrgente && isSameDay)