Skip to content

Commit

Permalink
feat: support favoriting topics
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-C committed Nov 25, 2024
1 parent bb82826 commit c136408
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/api/folderResourceMetaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import { IAudioMetaInformation } from "@ndla/types-backend/audio-api";
import { IImageMetaInformationV2 } from "@ndla/types-backend/image-api";
import { ILearningPathSummaryV2 } from "@ndla/types-backend/learningpath-api";
import { Node } from "@ndla/types-taxonomy";
import { fetchArticles } from "./articleApi";
import { fetchAudio } from "./audioApi";
import { searchConcepts } from "./conceptApi";
import { fetchFolder } from "./folderApi";
import { fetchImage } from "./imageApi";
import { searchNodes } from "./taxonomyApi";
import { queryNodes, searchNodes } from "./taxonomyApi";
import { fetchVideo } from "./videoApi";
import { defaultLanguage } from "../config";
import {
Expand All @@ -28,8 +29,18 @@ import {
GQLQueryFolderResourceMetaSearchArgs,
} from "../types/schema";
import { articleToMeta, learningpathToMeta } from "../utils/apiHelpers";
import { getArticleIdFromUrn } from "../utils/articleHelpers";

type MetaType = "article" | "learningpath" | "multidisciplinary" | "concept" | "image" | "audio" | "video" | "folder";
type MetaType =
| "article"
| "learningpath"
| "multidisciplinary"
| "concept"
| "image"
| "audio"
| "video"
| "folder"
| "topic";

const findResourceTypes = (result: Node | undefined, context: ContextWithLoaders): GQLFolderResourceResourceType[] => {
const ctx = result?.contexts?.[0];
Expand Down Expand Up @@ -101,6 +112,9 @@ export const fetchFolderResourceMeta = async (
} else if (resource.resourceType === "multidisciplinary") {
const res = await fetchAndTransformResourceMeta([resource], context, "multidisciplinary");
return res[0] ?? null;
} else if (resource.resourceType === "topic") {
const res = await fetchTopicMeta([resource], context, "topic");
return res[0] ?? null;
} else if (resource.resourceType === "image") {
const res = await fetchImageMeta([resource], context, "image");
return res[0] ?? null;
Expand All @@ -120,6 +134,34 @@ export const fetchFolderResourceMeta = async (
throw Error(`Resource type '${resource.resourceType}' not supported`);
};

export const fetchTopicMeta = async (
resources: GQLFolderResourceMetaSearchInput[] | undefined,
context: ContextWithLoaders,
type: MetaType,
): Promise<GQLFolderResourceMeta[]> => {
if (!resources?.length) return [];
const ids = resources.map((r) => r.id);
const topics = await queryNodes({ ids, language: context.language, nodeType: "TOPIC" }, context);

const articles = await fetchArticles(
topics.map((t) => (t.contentUri ? getArticleIdFromUrn(t.contentUri) : undefined)).filter((t): t is string => !!t),
context,
);

return ids.map((id) => {
const node = topics.find((t) => t.id === id);
const article = articles.find((a) => a?.id.toString() === getArticleIdFromUrn(node?.contentUri ?? ""));
return {
id,
title: article?.title.title ?? node?.name ?? "",
type: type.toUpperCase(),
description: article?.metaDescription.metaDescription ?? "",
metaImage: article?.metaImage,
resourceTypes: findResourceTypes(node, context),
};
});
};

export const fetchFolderMeta = async (
resources: GQLFolderResourceMetaSearchInput[] | undefined,
context: ContextWithLoaders,
Expand Down

0 comments on commit c136408

Please sign in to comment.