Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 1406 contrib affichage des status sur ladmin #1425

Merged
merged 24 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions shared/types/src/elastic/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SourceRoute } from "@socialgouv/cdtn-sources";
import { ContributionsAnswers } from "../hasura";

export type DocumentElasticWithSource<
T,
Expand All @@ -11,6 +12,11 @@ export type Breadcrumb = {
slug: string;
};

export type Export = {
id: string;
createdAt: string;
};

export type DocumentElastic<T extends SourceRoute = SourceRoute> = {
id: string;
cdtnId: string;
Expand All @@ -23,6 +29,8 @@ export type DocumentElastic<T extends SourceRoute = SourceRoute> = {
excludeFromSearch: boolean;
metaDescription: string;
refs: DocumentRef[];
contribution?: ContributionsAnswers;
export?: Export;
};

export type RelatedDocument<T extends SourceRoute = SourceRoute> = {
Expand Down
7 changes: 7 additions & 0 deletions shared/types/src/hasura/contributions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { HasuraDocument } from "./common";
import { FicheServicePublicDoc } from "./fiche-sp";

export type ContributionStatus = {
createdAt: string;
status: string;
};

export type ContributionsAnswers = {
id: string;
content: string | null;
Expand All @@ -14,6 +19,8 @@ export type ContributionsAnswers = {
content_fiche_sp: ContributionContentFicheSp | null;
message_block_generic_no_CDT: string | null;
agreement: ContributionAgreement;
updatedAt: string;
statuses?: ContributionStatus[];
};

export type ContributionQuestion = {
Expand Down
15 changes: 13 additions & 2 deletions targets/export-elasticsearch/src/ingester/cdtnDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import {
} from "./common/fetchCdtnAdminDocuments";
import { splitArticle } from "./fichesTravailSplitter";
import { getVersions } from "./versions";
import { generateContributions } from "./contributions";
import {
fetchContributionDocumentToPublish,
generateContributions,
} from "./contributions";
import { generateAgreements } from "./agreements";
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";
import { updateExportStatuses } from "./documents/updateExportStatuses";
import { getGlossary } from "./common/fetchGlossary";

/**
Expand Down Expand Up @@ -54,14 +58,17 @@ export async function getDuplicateSlugs(allDocuments: any) {
}

export async function cdtnDocumentsGen(
updateDocs: (source: string, documents: unknown[]) => Promise<void>
updateDocs: (source: string, documents: unknown[]) => Promise<void>,
isProd: boolean
) {
let documentsCount: Partial<ExportEsStatus["documentsCount"]> = {};

const themes = await fetchThemes();

const getBreadcrumbs = buildGetBreadcrumbs(themes);

const contributionsToPublish = await fetchContributionDocumentToPublish();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est dommage qu'on soit obligé de fetch deux fois les contributions, on aurait pu éviter cela en vérifiant une propriété des contributions, non ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On ne fetch pas toutes les contribs là je crois, on ne récupère que celle qui ont été modifiées : https://github.com/SocialGouv/cdtn-admin/pull/1425/files#diff-046031753264baf1637cef36c0bc23cd316e32a73e7250a81133410e2c0eed4fR8

Je pense que le nommage peut être amélioré ici pour éviter cette confusion :

Suggested change
const contributionsToPublish = await fetchContributionDocumentToPublish();
const editedContributionsSinceLastPublish = await fetchEditedContributionDocument();


logger.info("=== Courriers ===");
const modelesDeCourriers = await getDocumentBySource(
SOURCES.LETTERS,
Expand Down Expand Up @@ -328,6 +335,10 @@ export async function cdtnDocumentsGen(
},
]);

if (isProd && contributionsToPublish) {
await updateExportStatuses(contributionsToPublish);
}

logger.info("=== Save the documents length ===");
documentsCount = {
...documentsCount,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import {
ContributionDocumentJson,
ContributionsAnswers,
DocumentElasticWithSource,
} from "@socialgouv/cdtn-types";
import { filterContributionDocumentsToPublish } from "../fetchContributionDocumentsToPublish";

const contributionMock: ContributionsAnswers = {
id: "d61a3e21-9bf1-4199-b9d7-944c2382750c",
updatedAt: "2024-03-25T17:25:33.002376+00:00",
agreement: {
id: "id",
kali_id: "",
name: "",
},
cdtn_references: [],
content: "",
content_fiche_sp: {
initial_id: "",
document: {
date: "",
description: "",
raw: "",
referencedTexts: [],
url: "",
},
},
content_type: "ANSWER",
description: "",
kali_references: [],
legi_references: [],
message_block_generic_no_CDT: "",
other_references: [],
question: {
content: "",
id: "",
order: 1,
},
statuses: [],
};

const doc: DocumentElasticWithSource<ContributionDocumentJson> = {
id: "",
title: "",
excludeFromSearch: true,
slug: "",
source: "contributions",
text: "",
isPublished: true,
metaDescription: "",
references: [],
linkedContent: [],
questionIndex: 1,
questionName: "",
questionId: "",
genericAnswerId: "",
type: "cdt",
idcc: "",
refs: [],
breadcrumbs: [],
cdtnId: "",
contentType: "ANSWER",
description: "",
contribution: contributionMock,
};

describe("filterContributionDocumentsToPublish", () => {
it("should send the list of contributions which have been updated after last export", () => {
const result = filterContributionDocumentsToPublish([
{
...doc,
cdtnId: "388f8d7860",
export: {
id: "id",
createdAt: "2024-06-03T08:10:03.697249+00:00",
},
contribution: {
...contributionMock,
statuses: [
{
status: "TO_PUBLISH",
createdAt: "2024-03-27T14:26:28.387898+00:00",
},
],
},
},
]);
expect(result?.length).toBe(0);
});
it("should send the list of contributions which have been updated after last export2", () => {
const result = filterContributionDocumentsToPublish([
{
...doc,
cdtnId: "388f8d7860",
export: {
id: "id",
createdAt: "2024-06-03T08:10:03.697249+00:00",
},
contribution: {
...contributionMock,
statuses: [
{
status: "TO_PUBLISH",
createdAt: "2024-06-27T14:26:28.387898+00:00",
},
],
},
},
]);
expect(result?.length).toBe(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
ContributionDocumentJson,
DocumentElasticWithSource,
} from "@socialgouv/cdtn-types";
import { context } from "../context";
import { gqlClient } from "@shared/utils";

export const fetchContributionDocumentsQuery = `
query fetchContributions {
documents(where: {source: {_eq: "contributions"}}) {
cdtnId: cdtn_id
export {
createdAt: created_at
}
contribution {
id
updatedAt: updated_at
statuses(where:{status: {_eq: "TO_PUBLISH"}}, order_by: {created_at: desc}, limit: 1) {
status
createdAt: created_at
}
}
}
}
`;

interface HasuraReturn {
documents: [DocumentElasticWithSource<ContributionDocumentJson>] | undefined;
}

export function filterContributionDocumentsToPublish(
contributionDocs:
| DocumentElasticWithSource<ContributionDocumentJson>[]
| undefined
): DocumentElasticWithSource<ContributionDocumentJson>[] | undefined {
return contributionDocs?.filter((doc) => {
const exportDate = doc.export?.createdAt
? new Date(doc.export.createdAt).getTime()
: 0;
const statusDate = doc.contribution?.statuses?.length
? new Date(doc.contribution.statuses[0].createdAt).getTime()
: 0;
return statusDate > exportDate;
});
}

export async function fetchContributionDocumentToPublish(): Promise<
DocumentElasticWithSource<ContributionDocumentJson>[] | undefined
> {
const HASURA_GRAPHQL_ENDPOINT =
context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
const HASURA_GRAPHQL_ENDPOINT_SECRET =
context.get("cdtnAdminEndpointSecret") || "admin1";
const res = await gqlClient({
graphqlEndpoint: HASURA_GRAPHQL_ENDPOINT,
adminSecret: HASURA_GRAPHQL_ENDPOINT_SECRET,
})
.query<HasuraReturn>(fetchContributionDocumentsQuery, {})
.toPromise();
if (res.error) {
throw res.error;
}
return filterContributionDocumentsToPublish(res.data?.documents);
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./helpers";
export * from "./generate";
export * from "./fetchContributionDocumentsToPublish";
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Export } from "@socialgouv/cdtn-types";
import { context } from "../context";
import { gqlClient } from "@shared/utils";

export const fetchLastExportStatusQuery = `
query lastExportStatus {
exportEsStatus: export_es_status(order_by: {created_at: desc}, limit: 1) {
id
}
}
`;

interface HasuraReturn {
exportEsStatus: [Export] | undefined
}


export async function fetchLastExportStatus(): Promise<Export | undefined> {
const HASURA_GRAPHQL_ENDPOINT =
context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
const HASURA_GRAPHQL_ENDPOINT_SECRET =
context.get("cdtnAdminEndpointSecret") || "admin1";
const res = await gqlClient({
graphqlEndpoint: HASURA_GRAPHQL_ENDPOINT,
adminSecret: HASURA_GRAPHQL_ENDPOINT_SECRET,
})
.query<HasuraReturn>(fetchLastExportStatusQuery, {})
.toPromise();
if (res.error) {
throw res.error;
}
return res.data?.exportEsStatus?.[0];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { fetchLastExportStatus } from "./fetchLastExportStatus";
import { gqlClient } from "@shared/utils";
import { context } from "../context";
import {
ContributionDocumentJson,
DocumentElasticWithSource,
} from "@socialgouv/cdtn-types";

export const updateToLastExportStatusMutation = `mutation updateToLastExportStatus($cdtnIds: [String!], $exportId: uuid) {
updateDocuments: update_documents(
where: {cdtn_id: {_in: $cdtnIds}},
_set: {
exportId: $exportId
}
) {
returning {
cdtn_id
}
}
}`;

export async function updateExportStatuses(
contributionsToPublish: DocumentElasticWithSource<ContributionDocumentJson>[]
) {
const HASURA_GRAPHQL_ENDPOINT =
context.get("cdtnAdminEndpoint") || "http://localhost:8080/v1/graphql";
const HASURA_GRAPHQL_ENDPOINT_SECRET =
context.get("cdtnAdminEndpointSecret") || "admin1";
const exportStatus = await fetchLastExportStatus();

if (!exportStatus?.id) {
return;
}
const cdtnIds = contributionsToPublish.map(
(contribution) => contribution.cdtnId
);
const { id: exportId } = exportStatus;

if (cdtnIds.length) {
const res = await gqlClient({
graphqlEndpoint: HASURA_GRAPHQL_ENDPOINT,
adminSecret: HASURA_GRAPHQL_ENDPOINT_SECRET,
})
.mutation(updateToLastExportStatusMutation, { cdtnIds, exportId })
.toPromise();
if (res.error) {
throw res.error;
}
}
}
Loading
Loading