aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/helpers/convert-wp-thematic-to-thematic.ts
blob: 5f8d7fc5b22d0526f93eba2ddddd46491ef13523 (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
61
62
63
64
65
66
67
68
69
70
import type {
  PageLink,
  Thematic,
  WPPostPreview,
  WPThematic,
} from '../../../types';
import { ROUTES } from '../../../utils/constants';
import {
  getUniquePageLinks,
  sortPageLinksByName,
  updateContentTree,
} 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 = ({
  acfThematics,
  contentParts,
  databaseId,
  date,
  featuredImage,
  modified,
  seo,
  slug,
  title,
}: WPThematic): Thematic => {
  return {
    content: updateContentTree(contentParts.afterMore),
    id: databaseId,
    intro: contentParts.beforeMore,
    meta: {
      articles: acfThematics?.postsInThematic?.map(
        convertPostPreviewToArticlePreview
      ),
      cover: featuredImage ? convertWPImgToImg(featuredImage.node) : undefined,
      dates: {
        publication: date,
        update: modified,
      },
      seo: {
        description: seo.metaDesc,
        title: seo.title,
      },
      relatedTopics: acfThematics?.postsInThematic
        ? getRelatedTopicsFrom(acfThematics.postsInThematic)
        : undefined,
    },
    slug: `${ROUTES.THEMATICS}/${slug}`,
    title,
  };
};