Skip to content

Commit

Permalink
fix: 5948 error dans les logs maxlistenersexceededwarning (#5966)
Browse files Browse the repository at this point in the history
* fix: MaxListenersExceededWarning

* fix: remove filter

* fix: theme throwing error maxlistener

* chore: update snap

* fix: ES query size

* chore: clean

* Revert "chore: clean"

This reverts commit d3a16b1.

* refactor: review

---------

Co-authored-by: victor <[email protected]>
  • Loading branch information
Viczei and victor authored Jun 20, 2024
1 parent aae81ab commit 3d63440
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ exports[`Plan du site should return a list for /plan-du-site 1`] = `
{
"label": "Démission",
"slug": "demission",
"title": "Démission",
},
{
"label": "Fin d’un CDD - CTT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ exports[`Themes getAllThemesAndSubThemes 1`] = `
{
"label": "Démission",
"slug": "demission",
"title": "Démission",
},
{
"label": "Fin d’un CDD - CTT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,20 @@ export function getThemeBySlugQuery(slug: string) {
sort: [{ position: { order: "asc" } }],
};
}

export function getThemeBySlugsQuery(slugs: string[]) {
return {
_source: ["title", "slug"],
query: {
bool: {
filter: [
{ terms: { slug: slugs } },
{ term: { isPublished: true } },
{ term: { source: SOURCES.THEMES } },
],
},
},
sort: [{ position: { order: "asc" } }],
size: 500,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
elasticDocumentsIndex,
NotFoundError,
} from "../../utils";
import { getAllThemesQuery, getThemeBySlugQuery } from "./queries";
import {
getAllThemesQuery,
getThemeBySlugQuery,
getThemeBySlugsQuery,
} from "./queries";

export const getAllThemes = async () => {
const body: any = getAllThemesQuery();
Expand All @@ -27,9 +31,7 @@ export const getAllThemesAndSubThemes = async () => {
const childrenSlugs = themes.flatMap((theme) =>
theme.children.map((child) => child.slug)
);
const data = await Promise.all(
childrenSlugs.map((slug) => getBySlugThemes(slug))
).catch(() => {
const data = await getBySlugsThemes(childrenSlugs).catch(() => {
return [];
});
const themesWithChildren = themes.map((theme) => {
Expand Down Expand Up @@ -70,3 +72,24 @@ export const getBySlugThemes = async (slug: string) => {
...theme._source,
};
};

export const getBySlugsThemes = async (slugs: string[]) => {
const body: any = getThemeBySlugsQuery(slugs);

const response = await elasticsearchClient.search<any>({
body,
index: elasticDocumentsIndex,
});

if (response.hits.hits.length === 0) {
throw new NotFoundError({
message: `There is no theme that match ${slugs.join(",")}`,
name: "THEME_NOT_FOUND",
cause: null,
});
}

const themes = response.hits.hits.map(({ _source }) => _source);

return themes;
};

0 comments on commit 3d63440

Please sign in to comment.