{"size": 2572, "ext": "js", "max_stars_repo_path": "src/utils/groupNotifications.js", "max_stars_repo_name": "akshay-rajput/kvell-app", "max_stars_repo_stars_event_min_datetime": "2021-08-05T13:14:22.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-13T16:07:45.000Z", "max_issues_repo_path": "src/utils/groupNotifications.js", "max_issues_repo_name": "akshay-rajput/kvell-app", "max_issues_repo_issues_event_min_datetime": "2021-07-06T06:23:19.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-09T06:40:37.000Z", "max_forks_repo_path": "src/utils/groupNotifications.js", "max_forks_repo_name": "akshay-rajput/kvell-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 40.8253968254, "max_line_length": 133, "alphanum_fraction": 0.5458786936}
export const groupNotifications = (notifications, groupingKey) =>{
    if(notifications?.length > 0 && groupingKey){
        // group notifications by post id
        // each postId will have an array of notifications
        let groupedByKey = notifications.reduce((acc, currentNotification) => {
            // if postId key doesn't exist, create it and assign empty array as value
            if (!acc[currentNotification[groupingKey]]) {
                acc[currentNotification[groupingKey]] = [];
            }
            
            // implement grouping
            acc[currentNotification[groupingKey]].push(currentNotification);

            return acc;
        }, {});

        // console.log("grouping... ", groupedByKey);

        let allNotifications = [];

        for (const key in groupedByKey) {
            let likedNotificationGroup = {};
            let commentNotificationGroup = {};

            if (groupedByKey.hasOwnProperty(key)) {
                const postNotifications = groupedByKey[key];

                // if this post has notifications,
                if(postNotifications.length > 0){
                    // notification with type: like
                    let liked = postNotifications.filter(singleNotification => singleNotification.notificationType === "Like")
                    let commented = postNotifications.filter(singleNotification => singleNotification.notificationType === "Comment")
                    // console.log('liedk: ', liked);
                    if(liked.length > 0){
                        // console.log("liked 1: ", liked[0])
                        likedNotificationGroup = liked[0];
                        likedNotificationGroup.otherCount = liked.length - 1;
                    }

                    if(commented.length > 0){
                        // console.log("liked 1: ", liked[0])
                        commentNotificationGroup = commented[0];
                        commentNotificationGroup.otherCount = commented.length - 1;
                    }
                }
            }

            if(Object.keys(likedNotificationGroup).length > 0){
                allNotifications.push(likedNotificationGroup);
            }

            if(Object.keys(commentNotificationGroup).length > 0){
                allNotifications.push(commentNotificationGroup);
            }
        }

        // sort notifications to get latest first
        allNotifications.sort((a,b)=> {
            return new Date(b.createdAt) - new Date(a.createdAt);
        });
        return allNotifications;
    }
}