aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/helpers/convert-wp-thematic-to-thematic.ts
blob: 9aa1896d53e593e175a4b3b57a3a004f5144f41c (plain)
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
import type {
  PageLink,
  Thematic,
  WPPostPreview,
  WPThematic,
} from '../../../types';
import { ROUTES } from '../../../utils/constants';
import {
  getUniquePageLinks,
  sortPageLinksByName,
} from '../../../utils/helpers';
import { convertPostPreviewToArticlePreview } from './convert-post-preview-to-article-preview';
import { convertWPTopicPreviewToPageLink } from './convert-taxonomy-to-page-link';
import { convertWPImgToImg } from './convert-wp-image-to-img';

const getRelatedTopicsFrom = (posts: WPPostPreview[]): PageLink[] => {
  const topics: PageLink[] = [];

  for (const post of posts) {
    if (
      post.acfPosts &&
      'postsInTopic' in post.acfPosts &&
      post.acfPosts.postsInTopic
    ) {
      topics.push(
        ...post.acfPosts.postsInTopic.map(convertWPTopicPreviewToPageLink)
      );
    }
  }

  return getUniquePageLinks(topics).sort(sortPageLinksByName);
};

export const convertWPThematicToThematic = (thematic: WPThematic): Thematic => {
  return {
    content: thematic.contentParts.afterMore,
    intro: thematic.contentParts.beforeMore,
    meta: {
      articles: thematic.acfThematics?.postsInThematic?.map(
        convertPostPreviewToArticlePreview
      ),
      cover: thematic.featuredImage
        ? convertWPImgToImg(thematic.featuredImage.node)
        : undefined,
      dates: {
        publication: thematic.date,
        update: thematic.modified,
      },
      seo: {
        description: thematic.seo.metaDesc,
        title: thematic.seo.title,
      },
      relatedTopics: thematic.acfThematics?.postsInThematic
        ? getRelatedTopicsFrom(thematic.acfThematics.postsInThematic)
        : undefined,
    },
    slug: `${ROUTES.THEMATICS}/${thematic.slug}`,
    title: thematic.title,
  };
};