-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f81e68f
feat: set publish information to contriibutions
f4cdea4
feat: implement export update status
735c7bb
chore: update TU
74b922f
chore: eslint
cbd9751
chore: clean
d6e391c
fix: mistype
40bfa47
chore: clean
ab7e46e
chore: clean
aa99502
chore: fix optional type
8ee2b9c
refactor: review
053b906
fix: publishing status not displaying
101c285
fix: build
6c28b9c
feat: add migration
800c8d1
feat: script migration update export_id
e269a0f
feat: add migratrion script relation contrib/doc
7356762
fix: maj not running
2cc2dda
Merge branch 'master' into 1406-contrib-affichage-des-status-sur-ladmin
18eee26
refactor: review
0837d9f
chore: clean
0e4f7cf
chore: clean
0a65902
fix: à publier sur page réponse
641e499
Merge branch 'master' into 1406-contrib-affichage-des-status-sur-ladmin
2b9da39
chore: review
c554821
chore: review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...rt-elasticsearch/src/ingester/contributions/__tests__/filterContributionToPublish.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
...ts/export-elasticsearch/src/ingester/contributions/fetchContributionDocumentsToPublish.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
1 change: 1 addition & 0 deletions
1
targets/export-elasticsearch/src/ingester/contributions/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./helpers"; | ||
export * from "./generate"; | ||
export * from "./fetchContributionDocumentsToPublish"; |
33 changes: 33 additions & 0 deletions
33
targets/export-elasticsearch/src/ingester/documents/fetchLastExportStatus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
50 changes: 50 additions & 0 deletions
50
targets/export-elasticsearch/src/ingester/documents/updateExportStatuses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 :