aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/helpers/convert-wp-topic-to-topic.ts
blob: 60e5548ccc048dc2c2ffcb8a73e6a4f6e8cf85cc (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
import type { PageLink, Topic, WPPostPreview, WPTopic } from '../../../types';
import { ROUTES } from '../../../utils/constants';
import {
  getUniquePageLinks,
  sortPageLinksByName,
  updateContentTree,
} from '../../../utils/helpers';
import { convertPostPreviewToArticlePreview } from './convert-post-preview-to-article-preview';
import { convertWPThematicPreviewToPageLink } from './convert-taxonomy-to-page-link';
import { convertWPImgToImg } from './convert-wp-image-to-img';

const getRelatedThematicsFrom = (posts: WPPostPreview[]): PageLink[] => {
  const thematics: PageLink[] = [];

  for (const post of posts) {
    if (
      post.acfPosts &&
      'postsInThematic' in post.acfPosts &&
      post.acfPosts.postsInThematic
    ) {
      thematics.push(
        ...post.acfPosts.postsInThematic.map(convertWPThematicPreviewToPageLink)
      );
    }
  }

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

export const convertWPTopicToTopic = ({
  acfTopics,
  contentParts,
  databaseId,
  date,
  featuredImage,
  modified,
  seo,
  slug,
  title,
}: WPTopic): Topic => {
  return {
    content: updateContentTree(contentParts.afterMore),
    id: databaseId,
    intro: contentParts.beforeMore,
    meta: {
      articles: acfTopics?.postsInTopic?.map(
        convertPostPreviewToArticlePreview
      ),
      cover: featuredImage ? convertWPImgToImg(featuredImage.node) : undefined,
      dates: {
        publication: date,
        update: modified,
      },
      seo: {
        description: seo.metaDesc,
        title: seo.title,
      },
      relatedThematics: acfTopics?.postsInTopic
        ? getRelatedThematicsFrom(acfTopics.postsInTopic)
        : undefined,
      website: acfTopics?.officialWebsite ?? undefined,
    },
    slug: `${ROUTES.TOPICS}/${slug}`,
    title,
  };
};