Skip to content

Commit

Permalink
fix: bug
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgfr committed Jun 18, 2024
1 parent c724fbf commit ee325e8
Showing 1 changed file with 67 additions and 51 deletions.
118 changes: 67 additions & 51 deletions targets/ingester/src/transform/fiche-travail-emploi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import slugify from "@socialgouv/cdtn-slugify";
import { SOURCES } from "@socialgouv/cdtn-sources";
import type { FicheTravailEmploi } from "@socialgouv/fiches-travail-data-types";
import got from "got";
import pMap from "p-map";

import { getJson } from "../lib/getJson";
import {
articleToReference,
createReferenceResolver,
} from "../lib/referenceResolver";
import { Code } from "@socialgouv/legi-data-types";

const URL_EXPORT = process.env.URL_EXPORT || "http://localhost:8787";
const URL_EXPORT = process.env.URL_EXPORT ?? "http://localhost:8787";

export default async function getFicheTravailEmploi(pkgName: string) {
const [fichesMT, cdt] = await Promise.all([
Expand All @@ -17,59 +20,15 @@ export default async function getFicheTravailEmploi(pkgName: string) {
`@socialgouv/legi-data/data/LEGITEXT000006072050.json`
),
]);
const resolveCdtReference = createReferenceResolver(cdt);
const result = await Promise.all(
fichesMT.map(async ({ pubId, sections, ...content }) => {
const result = await pMap(
fichesMT,
async ({ pubId, sections, ...content }) => {
const sectionsWithGlossary = await fetchSections(sections, cdt);
return {
id: pubId,
...content,
is_searchable: true,
sections: await Promise.all(
sections.map(async ({ references, ...section }) => {
const resultProcess: any = await fetch(URL_EXPORT + "/glossary", {
body: JSON.stringify({
type: "html",
content: section.html,
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
})
.then((response) => {
if (!response.ok) {
throw new Error(
`HTTP error on glossary! status: ${response.status}`
);
}
return response.json();
})
.catch((error) => {
throw new Error(`Error on glossary! ${error}`);
});
const htmlWithGlossary = resultProcess.result;

return {
...section,
htmlWithGlossary,
references: Object.keys(references).flatMap((key) => {
if (key !== "LEGITEXT000006072050") {
return [];
}
const { articles } = references[key];
return articles.flatMap(({ id }) => {
const maybeArticle = resolveCdtReference(
id
) as LegiData.CodeArticle[];
if (maybeArticle.length !== 1) {
return [];
}
return articleToReference(maybeArticle[0]);
});
}),
};
})
),
sections: sectionsWithGlossary,
slug: slugify(content.title),
source: SOURCES.SHEET_MT_PAGE,
/**
Expand All @@ -78,7 +37,64 @@ export default async function getFicheTravailEmploi(pkgName: string) {
*/
text: "",
};
})
},
{ concurrency: 1 }
);

return result;
}

const fetchSections = async (
sections: FicheTravailEmploi["sections"],
cdt: Code
) => {
const resolveCdtReference = createReferenceResolver(cdt);

return await pMap(
sections,
async ({ references, ...section }) => {
let htmlWithGlossary = section.html;
const fetchResult: any = await got
.post(`${URL_EXPORT}/glossary`, {
json: {
type: "html",
content: section.html,
},
responseType: "json",
headers: {
"Content-Type": "application/json",
},
})
.json();

if (!fetchResult?.result) {
console.error(
`Error with glossary API, result : ${fetchResult} for ${section.html}`
);
} else {
htmlWithGlossary = fetchResult.result;
}

return {
...section,
htmlWithGlossary,
references: Object.keys(references).flatMap((key) => {
if (key !== "LEGITEXT000006072050") {
return [];
}
const { articles } = references[key];
return articles.flatMap(({ id }) => {
const maybeArticle = resolveCdtReference(
id
) as LegiData.CodeArticle[];
if (maybeArticle.length !== 1) {
return [];
}
return articleToReference(maybeArticle[0]);
});
}),
};
},
{ concurrency: 1 }
);
};

0 comments on commit ee325e8

Please sign in to comment.