Skip to content

Commit

Permalink
clean-up après reviews des devs
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineBda committed Oct 6, 2023
1 parent 57fbb2c commit 9959dde
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 36 deletions.
6 changes: 3 additions & 3 deletions shared/elasticsearch-document-adapter/src/cdtnDocuments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
AgreementDoc,
ContributionGenericDoc,
ContributionWithCCDoc,
GenericContributionDoc,
CustomizedContributionDoc,
EditorialContentDoc,
FicheTravailEmploiDoc,
} from "@shared/types";
Expand Down Expand Up @@ -169,7 +169,7 @@ export async function* cdtnDocumentsGen() {

logger.info("=== Contributions ===");
const contributions = await getDocumentBySource<
ContributionGenericDoc | ContributionWithCCDoc
GenericContributionDoc | CustomizedContributionDoc
>(SOURCES.CONTRIBUTIONS, getBreadcrumbs);

const ccnData = await getDocumentBySource<AgreementDoc>(SOURCES.CCN);
Expand Down
18 changes: 9 additions & 9 deletions shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export type FicheTravailEmploi = BaseHasuraDocument & {

export type ContributionComplete = BaseHasuraDocument & {
source: "contributions";
document: ContributionGenericDoc;
document: GenericContributionDoc;
};

export type ContributionFiltered = BaseHasuraDocument & {
source: "contributions";
document: ContributionWithCCDoc;
document: CustomizedContributionDoc;
};

export type LaborCodeArticle = BaseHasuraDocument & {
Expand Down Expand Up @@ -134,13 +134,13 @@ export interface TravailEmploiReference {
url: string;
}

export interface ContributionGenericDoc {
export interface GenericContributionDoc {
description: string;
answer: Answer;
agreementsWithAnswer: string[];
}

export interface ContributionWithCCDoc {
export interface CustomizedContributionDoc {
description: string;
answer: Answer;
idcc: string;
Expand Down Expand Up @@ -193,7 +193,7 @@ export interface AgreementContribAnswer {
index: string;
answer: string;
question: string;
references: ExternalRef[];
references: LegalRef[];
}

export interface ArticleTheme {
Expand Down Expand Up @@ -452,8 +452,8 @@ export type Answer = {
id: string;
content?: string;
contentType: string;
references: ExternalRef[];
linkedContent: BaseRef[];
references: LegalRef[];
linkedContent: CdtnRelatedContent[];
};

export type DilaRef = {
Expand All @@ -464,12 +464,12 @@ export type DilaRef = {
dila_container_id: string;
};

export type ExternalRef = {
export type LegalRef = {
title: string;
url?: string;
};

export type BaseRef = {
export type CdtnRelatedContent = {
source: string;
title: string;
slug: string;
Expand Down
11 changes: 6 additions & 5 deletions targets/ingester/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { SourceValues } from "@socialgouv/cdtn-sources";

import {
Answer,
ContributionGenericDoc,
ContributionWithCCDoc,
ExternalRef,
GenericContributionDoc,
CustomizedContributionDoc,
LegalRef,
FicheServicePublicDoc,
GenericAnswer,
} from "@shared/types";
Expand Down Expand Up @@ -43,7 +43,8 @@ export interface Question {
};
}

type Contribution = Document & (ContributionGenericDoc | ContributionWithCCDoc);
type Contribution = Document &
(GenericContributionDoc | CustomizedContributionDoc);

type LegiArticle = ExternalDocument & {
dateDebut: number;
Expand Down Expand Up @@ -85,7 +86,7 @@ type AgreementPage = Document & {

interface AgreementAnswer {
order: number;
references: ExternalRef[];
references: LegalRef[];
genericSlug: string;
question: string;
content?: string;
Expand Down
29 changes: 16 additions & 13 deletions targets/ingester/src/lib/fetchContributions/AnswerExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BaseRef } from "@shared/types";
import { ExternalRef } from "@shared/types";
import type { CdtnRelatedContent } from "@shared/types";
import { LegalRef } from "@shared/types";

import type { AnswerRaw } from "./types";
import { IndexedAgreement } from "@socialgouv/kali-data-types";
Expand All @@ -13,12 +13,15 @@ export class AnswerExtractor {
this.agreements = agreements;
}

public extractGenericAnswer(answers: AnswerRaw[]): GenericAnswer {
public extractGenericAnswer(
answers: AnswerRaw[],
questionText: string
): GenericAnswer {
const genericAnswer = answers.find(
(answer) => answer.agreement.id === "0000"
);
if (!genericAnswer) {
throw new Error("No generic answer found");
throw new Error(`No generic answer found for contrib: ${questionText}`);
}
const genericTextAnswer = this.toText(genericAnswer);
const answer: GenericAnswer = {
Expand All @@ -45,7 +48,7 @@ export class AnswerExtractor {
return answer;
}

private mapKaliRefs = (idcc: string, references: any[]): ExternalRef[] => {
private mapKaliRefs = (idcc: string, references: any[]): LegalRef[] => {
if (!references.length) return [];
const agreement = this.agreements.find(
(item) => this.comparableIdcc(item.num) === this.comparableIdcc(idcc)
Expand All @@ -61,7 +64,7 @@ export class AnswerExtractor {
});
};

private mapLegiRefs = (references: any[]): ExternalRef[] => {
private mapLegiRefs = (references: any[]): LegalRef[] => {
if (!references.length) return [];

return references.map((ref) => {
Expand All @@ -72,26 +75,26 @@ export class AnswerExtractor {
});
};

private mapOtherRefs = (references: any[]): ExternalRef[] => {
private mapOtherRefs = (references: LegalRef[]): LegalRef[] => {
if (!references.length) return [];

return references.map((ref: ExternalRef) => {
if (!ref.url) delete ref.url;
return ref;
return references.map(({ url, ...ref }) => {
if (!url) ref as LegalRef;
return { ...ref, url } as LegalRef;
});
};

private mapCdtnRefs = (references: any[]): BaseRef[] => {
private mapCdtnRefs = (references: any[]): CdtnRelatedContent[] => {
if (!references.length) return [];

return references
.map((ref) => {
return ref.document as BaseRef;
return ref.document as CdtnRelatedContent;
})
.sort(this.sortBy("title"));
};

private aggregateReferences(answer: AnswerRaw): ExternalRef[] {
private aggregateReferences(answer: AnswerRaw): LegalRef[] {
return this.mapKaliRefs(answer.agreement.id, answer.kali_references)
.concat(this.mapLegiRefs(answer.legi_references))
.concat(this.mapOtherRefs(answer.other_references));
Expand Down
5 changes: 4 additions & 1 deletion targets/ingester/src/lib/fetchContributions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export async function fetchContributions(): Promise<Question[]> {
const answerExtractor = new AnswerExtractor(agreements);

return questions.flatMap(({ answers, ...question }) => {
const genericAnswer = answerExtractor.extractGenericAnswer(answers);
const genericAnswer = answerExtractor.extractGenericAnswer(
answers,
question.content
);
return {
answers: {
conventions: answerExtractor.extractAgreementAnswers(
Expand Down
11 changes: 7 additions & 4 deletions targets/ingester/src/lib/fetchContributions/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { BaseRef, ExternalRef, FicheServicePublicDoc } from "@shared/types";
import {
CdtnRelatedContent,
FicheServicePublicDoc,
LegalRef,
} from "@shared/types";

export interface AgreementRaw {
id: string;
name: string;
parent_id: string; // en a-t-on besoin ?
}

export interface AnswerRaw {
Expand All @@ -22,8 +25,8 @@ export interface AnswerRaw {
title: string;
};
}[];
cdtn_references: BaseRef[];
other_references: ExternalRef[];
cdtn_references: CdtnRelatedContent[];
other_references: LegalRef[];
agreement: AgreementRaw;
}

Expand Down
2 changes: 1 addition & 1 deletion targets/ingester/src/transform/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default async function getContributionsDocuments(): Promise<
is_searchable: false,
slug: slugify(`${parseInt(idcc, 10)}-${title}`),
source: SOURCES.CONTRIBUTIONS,
text: `${idcc} ${title}`, // actuellement c'est comme ça mais si la CC a son propre contenu est-ce que ça ne devrait pas être dans text aussi ?
text: `${idcc} ${title}`,
title,
};
}
Expand Down

0 comments on commit 9959dde

Please sign in to comment.