Skip to content

Commit

Permalink
feat: 1308 admin extraction des prqualifis (#1315)
Browse files Browse the repository at this point in the history
* refactor: prequalified

* feat: refonte de la page requêtes préqualifiées

* feat: keep only one contained button

* fix: build

* fix: ts

* chore: clean

* fix: creation edit failure

* fix: memory failure

* feat: implementation nouvelle prequalif dans alert-cli

* chore: review

---------

Co-authored-by: Victor <[email protected]>
  • Loading branch information
Viczei and Victor authored Apr 2, 2024
1 parent 7a47a22 commit cef0ceb
Show file tree
Hide file tree
Showing 53 changed files with 1,319 additions and 215 deletions.
1 change: 1 addition & 0 deletions shared/types/export/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./agreements";
export * from "./contributions";
export * from "./global";
export * from "./prequalified";
11 changes: 11 additions & 0 deletions shared/types/export/prequalified.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DocumentElasticWithSource } from "./global";

type Prequalified = {
source: "prequalified";
variants: string[];
};

export type PrequalifiedElasticDocument = Omit<
DocumentElasticWithSource<Prequalified>,
"slug"
>;
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,45 @@ import { describe, expect, it } from "@jest/globals";
import { getDocumentsWithRelations } from "../getDocumentsWithRelations";
import { SOURCES } from "@socialgouv/cdtn-sources";

jest.mock("../getAllDocumentsBySource", () => ({
getDocumentsWithRelationsBySource: async () =>
jest.mock("../createDocumentsFetcher", () => ({
createDocumentsFetcher: () => async () =>
Promise.resolve([
{
cdtnId: "1",
contentRelations: [
{
document: {
initialId: "F2839",
},
status: "fulfilled",
value: {
data: {
documents: [
{
cdtnId: "1",
isPublished: true,
contentRelations: [
{
position: 1,
document: {
initialId: "F2839",
slug: "slug1",
source: "contributions",
title: "contribution1",
},
},
],
source: "themes",
title: "Handicap",
},
],
},
],
source: "themes",
title: "Handicap",
},
},
]),
}));

jest.mock("../fetchPrequalified", () => ({
fetchPrequalified: async () =>
Promise.resolve([
{
cdtnId: "2",
isPublished: true,
source: "prequalified",
contentRelations: [
{
document: {
Expand All @@ -32,19 +54,19 @@ jest.mock("../getAllDocumentsBySource", () => ({
},
},
],
source: "prequalified",
title: "procédure licenciement pour inaptitude",
},
{
cdtnId: "3",
isPublished: true,
source: "prequalified",
contentRelations: [
{
document: {
initialId: "F2839",
},
},
],
source: "prequalified",
title: "complement-salaire pole emploi",
},
]),
Expand Down
54 changes: 54 additions & 0 deletions targets/alert-cli/src/diff/shared/createDocumentsFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SourceValues } from "@socialgouv/cdtn-sources";
import {
AllDocumentsBySourceResult,
AllDocumentsWithRelationBySourceResult,
CountDocumentsBySourceResult,
countDocumentsBySourceQuery,
getAllDocumentsBySourceQuery,
} from "./getDocumentQuery.gql";
import { gqlClient } from "@shared/utils";
import { batchPromises } from "../../utils/batch-promises";

const PAGE_SIZE = 100;
const JOB_CONCURENCY = 3;

export const createDocumentsFetcher =
<
T extends
| AllDocumentsBySourceResult
| AllDocumentsWithRelationBySourceResult
>(
gqlRequest = getAllDocumentsBySourceQuery
) =>
async (source: SourceValues[]) => {
const countResult = await gqlClient()
.query<CountDocumentsBySourceResult>(countDocumentsBySourceQuery, {
source,
})
.toPromise();

if (countResult.error || !countResult.data) {
console.error(countResult.error && "no data received");
throw new Error("getSources");
}

const { count } = countResult.data.documents_aggregate.aggregate;

const pages = Array.from(
{ length: Math.ceil(count / PAGE_SIZE) },
(_, i) => i
);
const documentResults = await batchPromises(
pages,
async (page) =>
gqlClient()
.query<T>(gqlRequest, {
limit: PAGE_SIZE,
offset: page * PAGE_SIZE,
source,
})
.toPromise(),
JOB_CONCURENCY
);
return documentResults;
};
41 changes: 41 additions & 0 deletions targets/alert-cli/src/diff/shared/fetchPrequalified.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { gqlClient } from "@shared/utils";
import { HasuraDocumentWithRelations } from "./getDocumentQuery.gql";

const fetchPrequalifiedQuery = `
query fetch_prequalified {
search_prequalified {
cdtnId: id
title
contentRelations: documents(order_by: {order: asc}) {
position: order
document {
initialId: initial_id
slug
source
title
}
}
}
}
`;

interface HasuraReturn {
search_prequalified: HasuraDocumentWithRelations[] | undefined;
}

export async function fetchPrequalified(): Promise<
HasuraDocumentWithRelations[] | undefined
> {
const res = await gqlClient()
.query<HasuraReturn>(fetchPrequalifiedQuery)
.toPromise();
if (res.error) {
throw res.error;
}

return res.data?.search_prequalified?.map((prequalif) => ({
...prequalif,
isPublished: true,
source: "prequalified",
}));
}
60 changes: 10 additions & 50 deletions targets/alert-cli/src/diff/shared/getAllDocumentsBySource.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,18 @@
import { gqlClient } from "@shared/utils";
import type { SourceValues } from "@socialgouv/cdtn-sources";
import { SOURCES, type SourceValues } from "@socialgouv/cdtn-sources";
import memoizee from "memoizee";

import { batchPromises } from "../../utils/batch-promises";
import type {
AllDocumentsBySourceResult,
AllDocumentsWithRelationBySourceResult,
CountDocumentsBySourceResult,
HasuraDocumentForAlert,
HasuraDocumentWithRelations,
} from "./getDocumentQuery.gql";
import {
countDocumentsBySourceQuery,
getAllDocumentsBySourceQuery,
getAllDocumentsWithRelationsBySourceQuery,
} from "./getDocumentQuery.gql";

const PAGE_SIZE = 100;
const JOB_CONCURENCY = 3;

const createDocumentsFetcher =
<
T extends
| AllDocumentsBySourceResult
| AllDocumentsWithRelationBySourceResult
>(
gqlRequest = getAllDocumentsBySourceQuery
) =>
async (source: SourceValues[]) => {
const countResult = await gqlClient()
.query<CountDocumentsBySourceResult>(countDocumentsBySourceQuery, {
source,
})
.toPromise();

if (countResult.error || !countResult.data) {
console.error(countResult.error && "no data received");
throw new Error("getSources");
}

const { count } = countResult.data.documents_aggregate.aggregate;

const pages = Array.from(
{ length: Math.ceil(count / PAGE_SIZE) },
(_, i) => i
);
const documentResults = await batchPromises(
pages,
async (page) =>
gqlClient()
.query<T>(gqlRequest, {
limit: PAGE_SIZE,
offset: page * PAGE_SIZE,
source,
})
.toPromise(),
JOB_CONCURENCY
);
return documentResults;
};
import { fetchPrequalified } from "./fetchPrequalified";
import { createDocumentsFetcher } from "./createDocumentsFetcher";

export async function _getDocumentsBySource(
source: SourceValues[]
Expand Down Expand Up @@ -87,12 +41,18 @@ export async function _getDocumentsWithRelationsBySource(
getAllDocumentsWithRelationsBySourceQuery
);
const documentResults = await fetchDocuments(source);
const documents = documentResults.flatMap((result) => {
let documents = documentResults.flatMap((result) => {
if (result.status === "fulfilled" && result.value.data) {
return result.value.data.documents;
}
return [];
});
if (source.includes(SOURCES.PREQUALIFIED)) {
const fetchedPrequalified = await fetchPrequalified();
if (fetchedPrequalified) {
documents = documents.concat(fetchedPrequalified);
}
}
return documents;
}

Expand Down
14 changes: 8 additions & 6 deletions targets/alert-cli/src/diff/shared/getDocumentQuery.gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export type HasuraDocumentForAlert = Pick<
cdtnId: string;
};

export type AllDocumentsBySourceResult = {
export interface AllDocumentsBySourceResult {
documents: HasuraDocumentForAlert[];
};
}

export const countDocumentsBySourceQuery = `
query coundDocumentsBySource($source:[String!]){
Expand All @@ -37,13 +37,13 @@ query coundDocumentsBySource($source:[String!]){
}
}`;

export type CountDocumentsBySourceResult = {
export interface CountDocumentsBySourceResult {
documents_aggregate: {
aggregate: {
count: number;
};
};
};
}

export const getAllDocumentsWithRelationsBySourceQuery = `
query($source: [String!], $limit:Int=10,$offset:Int=0 ) {
Expand All @@ -70,6 +70,7 @@ query($source: [String!], $limit:Int=10,$offset:Int=0 ) {
}
}
`;

export type HasuraDocumentWithRelations = Pick<
HasuraDocument,
"source" | "title"
Expand All @@ -83,6 +84,7 @@ export type HasuraDocumentWithRelations = Pick<
};
}[];
};
export type AllDocumentsWithRelationBySourceResult = {

export interface AllDocumentsWithRelationBySourceResult {
documents: HasuraDocumentWithRelations[];
};
}
27 changes: 4 additions & 23 deletions targets/export-elasticsearch/src/ingester/cdtnDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { generateAgreements } from "./agreements";
import { getGlossary } from "./common/fetchGlossary";
import { fetchThemes } from "./themes/fetchThemes";
import { updateExportEsStatusWithDocumentsCount } from "./exportStatus/updateExportEsStatusWithDocumentsCount";
import { generatePrequalified } from "./prequalified";
import { generateEditorialContents } from "./informations/generate";
import { populateRelatedDocuments } from "./common/populateRelatedDocuments";
import { mergeRelatedDocumentsToEditorialContents } from "./informations/mergeRelatedDocumentsToEditorialContents";
Expand Down Expand Up @@ -362,32 +363,12 @@ export async function cdtnDocumentsGen(
await updateDocs(SOURCES.HIGHLIGHTS, highlightsWithContrib);

logger.info("=== PreQualified Request ===");
const prequalified = await getDocumentBySourceWithRelation(
SOURCES.PREQUALIFIED,
getBreadcrumbs
);
const prequalifiedWithContrib = prequalified.map((prequalif) => ({
...prequalif,
refs: prequalif.refs.map((ref) => {
if (!ref.description) {
const foundContrib = newGeneratedContributions.find(
(newGeneratedContribution) => {
return newGeneratedContribution.cdtnId === ref.cdtnId;
}
);
return {
...ref,
description: foundContrib?.description,
};
}
return ref;
}),
}));
const prequalified = await generatePrequalified();
documentsCount = {
...documentsCount,
[SOURCES.PREQUALIFIED]: prequalifiedWithContrib.length,
[SOURCES.PREQUALIFIED]: prequalified.length,
};
await updateDocs(SOURCES.PREQUALIFIED, prequalifiedWithContrib);
await updateDocs(SOURCES.PREQUALIFIED, prequalified);

logger.info("=== glossary ===");
documentsCount = {
Expand Down
Loading

0 comments on commit cef0ceb

Please sign in to comment.