All files / src/common/utils groupUtils.ts

10% Statements 4/40
100% Branches 0/0
0% Functions 0/4
10% Lines 4/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61          1x                               1x                   1x                               1x                          
import { NewsletterGroup, NewsletterWithRelations } from '@common/types';
 
/**
 * Get the groups that a newsletter belongs to based on its source
 */
export const getNewsletterGroups = (
  newsletter: NewsletterWithRelations,
  allGroups: NewsletterGroup[]
): NewsletterGroup[] => {
  if (!newsletter.source || !allGroups.length) {
    return [];
  }
 
  return allGroups.filter(group =>
    group.sources?.some(source => source.id === newsletter.source?.id)
  );
};
 
/**
 * Get the IDs of groups that a newsletter belongs to
 */
export const getNewsletterGroupIds = (
  newsletter: NewsletterWithRelations,
  allGroups: NewsletterGroup[]
): string[] => {
  return getNewsletterGroups(newsletter, allGroups).map(group => group.id);
};
 
/**
 * Check if a newsletter belongs to any of the specified active group filters
 */
export const isNewsletterInActiveGroups = (
  newsletter: NewsletterWithRelations,
  activeGroupIds: string[],
  allGroups: NewsletterGroup[]
): boolean => {
  if (activeGroupIds.length === 0) {
    return true; // No group filter means include all
  }
 
  const newsletterGroupIds = getNewsletterGroupIds(newsletter, allGroups);
  return activeGroupIds.some(groupId => newsletterGroupIds.includes(groupId));
};
 
/**
 * Filter newsletters based on active group filters
 */
export const filterNewslettersByGroups = (
  newsletters: NewsletterWithRelations[],
  activeGroupIds: string[],
  allGroups: NewsletterGroup[]
): NewsletterWithRelations[] => {
  if (activeGroupIds.length === 0) {
    return newsletters; // No group filter means include all
  }
 
  return newsletters.filter(newsletter =>
    isNewsletterInActiveGroups(newsletter, activeGroupIds, allGroups)
  );
};