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

fix: alerts dares #1481

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 22 additions & 0 deletions targets/alert-cli/src/dares/__mocks__/getAgreementsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { GetAgreementData } from "../getAgreementsData";

export async function getAgreements(): Promise<GetAgreementData[]> {
return Promise.resolve([
{
name: "Convention collective nationale des transports routiers et activités auxiliaires du transport",
num: 16,
},
{
name: "Convention collective nationale de l'industrie textile du 1er février 1951. Etendue par arrêté du 17 décembre 1951, rectificatif du 13 janvier 1952, mise à jour le 29 mai 1979, en vigueur le 1er octobre 1979. Etendue par arrêté du 23 octobre 1979. JONC 12 janvier 1980 ",
num: 18,
},
{
name: "Convention collective nationale des établissements privés d'hospitalisation, de soins, de cure et de garde à but non lucratif (FEHAP, convention de 1951)",
num: 29,
},
{
name: "Convention collective nationale des industries chimiques et connexes",
num: 44,
},
]);
}
19 changes: 19 additions & 0 deletions targets/alert-cli/src/dares/__mocks__/getDaresData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Agreement } from "../types";

export const getDaresData = async (): Promise<Agreement[]> => {
return Promise.resolve([
{
name: "Convention collective nationale des transports routiers et activités auxiliaires du transport",
num: 16,
},
{ name: "Convention collective nationale de l'industrie textile", num: 18 },
{
name: "Convention collective nationale des établissements privés d'hospitalisation, de soins, de cure et de garde à but non lucratif (FEHAP, convention de 1951)",
num: 29,
},
{
name: "Convention collective nationale de l'Import-export et du Commerce international",
num: 43,
},
]);
};
24 changes: 24 additions & 0 deletions targets/alert-cli/src/dares/__tests__/difference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getDifferenceBetweenIndexAndDares } from "../difference";

jest.mock("../getAgreementsData");
jest.mock("../getDaresData");

describe("getDifferenceBetweenIndexAndDares", () => {
it("should work", async () => {
const result = await getDifferenceBetweenIndexAndDares();
expect(result).toEqual({
addedAgreementsFromDares: [
{
name: "Convention collective nationale de l'Import-export et du Commerce international",
num: 43,
},
],
removedAgreementsFromDares: [
{
name: "Convention collective nationale des industries chimiques et connexes",
num: 44,
},
],
});
});
});
53 changes: 53 additions & 0 deletions targets/alert-cli/src/dares/__tests__/getDaresData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getDaresData } from "../getDaresData";

jest.mock("../fetchDaresXlsx", () => {
return {
fetchDaresXlsx: () => {
return [
{
name: "Liste IDCC-Publication",
data: [
["IDENTIFIANT DE CONVENTION COLLECTIVE (IDCC) : "],
[
null,
"CODES EN VIGUEUR POUR LE REMPLISSAGE de la DADS-U et de la DSN",
],
[],
["IDCC", "TITRE DE LA CONVENTION"],
[
"0016",
"Convention collective nationale des transports routiers et activités auxiliaires du transport",
],
["0018", "Convention collective nationale de l'industrie textile"],
[
"0029",
"Convention collective nationale des établissements privés d'hospitalisation, de soins, de cure et de garde à but non lucratif (FEHAP, convention de 1951)",
],
["9998", "___Convention non encore en vigueur___"],
["9999", "___Sans convention collective___"],
],
},
];
},
};
});

describe("getDaresData", () => {
it("should work", async () => {
const result = await getDaresData();
expect(result).toEqual([
{
name: "Convention collective nationale des transports routiers et activités auxiliaires du transport",
num: 16,
},
{
name: "Convention collective nationale de l'industrie textile",
num: 18,
},
{
name: "Convention collective nationale des établissements privés d'hospitalisation, de soins, de cure et de garde à but non lucratif (FEHAP, convention de 1951)",
num: 29,
},
]);
});
});
56 changes: 16 additions & 40 deletions targets/alert-cli/src/dares/difference.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
import xlsx from "node-xlsx";
import { Diff, Agreement } from "./types";
import fs from "fs";

export function getDifferenceBetweenIndexAndDares(
pathDares: string,
pathIndex: string
): Diff {
const workSheetsFromFile = xlsx.parse(pathDares);

const supportedCcXlsx: Agreement[] = [];

workSheetsFromFile[0].data.forEach((row: string[]) => {
const ccNumber = parseInt(row[0]);
const ccName = row[1];
if (ccNumber && ccName) {
const ccNameWithoutParenthesis = ccName
.replace(/\(.*annexée.*\)/gi, "")
.trim();
supportedCcXlsx.push({
name: ccNameWithoutParenthesis,
num: ccNumber,
});
}
});

const dataJson = JSON.parse(fs.readFileSync(pathIndex, "utf8"));

const supportedCcIndexJson: Agreement[] = dataJson.map((cc: any) => {
return {
name: cc.title,
num: cc.num,
};
});

const missingAgreementsFromDares: Agreement[] = supportedCcXlsx.filter(
(ccIndex) =>
!supportedCcIndexJson.find((ccXlsx) => ccXlsx.num === ccIndex.num)
import { getDaresData } from "./getDaresData";
import { getAgreements } from "./getAgreementsData";

export async function getDifferenceBetweenIndexAndDares(): Promise<Diff> {
const daresDataList = await getDaresData();
const AgreementDataList = await getAgreements();

const addedAgreementsFromDares: Agreement[] = daresDataList.filter(
(daresData) =>
!AgreementDataList.find(
(agreementData) => agreementData.num === daresData.num
)
);

const exceedingAgreementsFromKali = supportedCcIndexJson.filter(
(ccXlsx) => !supportedCcXlsx.find((ccIndex) => ccIndex.num === ccXlsx.num)
const removedAgreementsFromDares = AgreementDataList.filter(
(agreementData) =>
!daresDataList.find((daresData) => daresData.num === agreementData.num)
);

return { missingAgreementsFromDares, exceedingAgreementsFromKali };
return { addedAgreementsFromDares, removedAgreementsFromDares };
}
13 changes: 13 additions & 0 deletions targets/alert-cli/src/dares/fetchDaresXlsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { extractDaresXlsxFromMT } from "./scrapping";
import { downloadFileInTempFolder } from "./download";
import xlsx from "node-xlsx";

export const fetchDaresXlsx = async () => {
const xlsxUrl = await extractDaresXlsxFromMT();
const xlsxPath = await downloadFileInTempFolder(xlsxUrl, "dares.xlsx");
const workSheetsFromFile: {
name: string;
data: any[][];

Check warning on line 10 in targets/alert-cli/src/dares/fetchDaresXlsx.ts

View workflow job for this annotation

GitHub Actions / Lint (alert-cli)

Unexpected any. Specify a different type
}[] = xlsx.parse(xlsxPath);
return workSheetsFromFile;
};
37 changes: 37 additions & 0 deletions targets/alert-cli/src/dares/getAgreementsData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { gqlClient } from "@shared/utils";

const getAgreementQuery = `
query queryAgreements {
agreements: agreement_agreements(
where: { id : { _neq: "0000" }},
order_by: { id: asc }) {
id
name
}
}
`;

export interface GetAgreementData {
num: number;
name: string;
}

interface GetAgreementQueryResult {
agreements: {
id: string;
name: string;
}[];
}

export async function getAgreements(): Promise<GetAgreementData[]> {
const client = gqlClient();
const result = await client
.query<GetAgreementQueryResult>(getAgreementQuery, {})
.toPromise();
if (!result || !result.data || !result.data?.agreements) {

Check warning on line 31 in targets/alert-cli/src/dares/getAgreementsData.ts

View workflow job for this annotation

GitHub Actions / Lint (alert-cli)

Prefer using an optional chain expression instead, as it's more concise and easier to read

Check warning on line 31 in targets/alert-cli/src/dares/getAgreementsData.ts

View workflow job for this annotation

GitHub Actions / Lint (alert-cli)

Unnecessary conditional, value is always falsy

Check warning on line 31 in targets/alert-cli/src/dares/getAgreementsData.ts

View workflow job for this annotation

GitHub Actions / Lint (alert-cli)

Unnecessary conditional, value is always falsy

Check warning on line 31 in targets/alert-cli/src/dares/getAgreementsData.ts

View workflow job for this annotation

GitHub Actions / Lint (alert-cli)

Unnecessary optional chain on a non-nullish value
Viczei marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("An error occured on hasura agreement loading");
}
return result.data.agreements.map(({ name, id }) => {
return { name, num: parseInt(id) };
});
}
30 changes: 30 additions & 0 deletions targets/alert-cli/src/dares/getDaresData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Agreement } from "./types";
import { fetchDaresXlsx } from "./fetchDaresXlsx";

export const getDaresData = async () => {
const workSheetsFromFile: {
name: string;
data: any[][];

Check warning on line 7 in targets/alert-cli/src/dares/getDaresData.ts

View workflow job for this annotation

GitHub Actions / Lint (alert-cli)

Unexpected any. Specify a different type
}[] = await fetchDaresXlsx();

return workSheetsFromFile[0].data.reduce<Agreement[]>((arr, row) => {
const ccNumber = parseInt(row[0]);

Check warning on line 11 in targets/alert-cli/src/dares/getDaresData.ts

View workflow job for this annotation

GitHub Actions / Lint (alert-cli)

Unsafe argument of type `any` assigned to a parameter of type `string`
if ([9998, 9999].indexOf(ccNumber) !== -1) {
return arr;
}
const ccName = row[1] as string;
if (ccNumber && ccName) {
const ccNameWithoutParenthesis = ccName
.replace(/\(.*annexée.*\)/gi, "")
.trim();
return [
...arr,
{
name: ccNameWithoutParenthesis,
num: ccNumber,
},
];
}
return arr;
}, []);
};
8 changes: 1 addition & 7 deletions targets/alert-cli/src/dares/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { URL_KALI } from "./config";
import { getDifferenceBetweenIndexAndDares } from "./difference";
import { downloadFileInTempFolder } from "./download";
import { saveDiff } from "./save";
import { extractDaresXlsxFromMT } from "./scrapping";

export const runDares = async () => {
const xlsxUrl = await extractDaresXlsxFromMT();
const xlsxPath = await downloadFileInTempFolder(xlsxUrl, "dares.xlsx");
const indexPath = await downloadFileInTempFolder(URL_KALI, "index.json");
const diff = await getDifferenceBetweenIndexAndDares(xlsxPath, indexPath);
const diff = await getDifferenceBetweenIndexAndDares();
await saveDiff(diff);
};
4 changes: 2 additions & 2 deletions targets/alert-cli/src/dares/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const saveDiff = async (diff: Diff) => {
const alertRepository = new AlertRepository(gqlClient());

const alertsRemovedToSave: DaresAlertInsert[] =
diff.exceedingAgreementsFromKali.map((agreement) => ({
diff.removedAgreementsFromDares.map((agreement) => ({
info: {
id: agreement.num,
},
Expand All @@ -31,7 +31,7 @@ export const saveDiff = async (diff: Diff) => {
}));

const alertsAddedToSave: DaresAlertInsert[] =
diff.missingAgreementsFromDares.map((agreement) => ({
diff.addedAgreementsFromDares.map((agreement) => ({
info: {
id: agreement.num,
},
Expand Down
4 changes: 2 additions & 2 deletions targets/alert-cli/src/dares/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DaresAlert } from "@socialgouv/cdtn-types";

export interface Diff {
missingAgreementsFromDares: Agreement[];
exceedingAgreementsFromKali: Agreement[];
addedAgreementsFromDares: Agreement[];
removedAgreementsFromDares: Agreement[];
}

export interface Agreement {
Expand Down
22 changes: 11 additions & 11 deletions targets/alert-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ export async function run(

async function main() {
await runDares();
const githubToken = process.env.GITHUB_TOKEN;
if (!githubToken) {
throw new Error("GITHUB_TOKEN is not defined");
}
const api = new GithubApi(githubToken);
const sourceRepository = new SourcesRepository(gqlClient());
const alertRepository = new AlertRepository(gqlClient());
const ficheSPRepository = new FicheSPRepository(gqlClient());
const ficheSpIds = await ficheSPRepository.getFicheServicePublicIds();
const alertDetector = new AlertDetector(api, ficheSpIds);
await run(api, sourceRepository, alertRepository, alertDetector);
// const githubToken = process.env.GITHUB_TOKEN;
// if (!githubToken) {
// throw new Error("GITHUB_TOKEN is not defined");
// }
// const api = new GithubApi(githubToken);
// const sourceRepository = new SourcesRepository(gqlClient());
// const alertRepository = new AlertRepository(gqlClient());
// const ficheSPRepository = new FicheSPRepository(gqlClient());
// const ficheSpIds = await ficheSPRepository.getFicheServicePublicIds();
// const alertDetector = new AlertDetector(api, ficheSpIds);
// await run(api, sourceRepository, alertRepository, alertDetector);
}

main().catch((error) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TRIGGER IF EXISTS audit_trigger_row on search.prequalified_documents;
DROP TRIGGER IF EXISTS audit_trigger_row on search.prequalified;
DROP TRIGGER IF EXISTS audit_trigger_row on information.informations_references;
DROP TRIGGER IF EXISTS audit_trigger_row on information.informations_contents_references;
DROP TRIGGER IF EXISTS audit_trigger_row on information.informations_contents_blocks_contents;
DROP TRIGGER IF EXISTS audit_trigger_row on information.informations_contents_blocks;
DROP TRIGGER IF EXISTS audit_trigger_row on information.informations_contents;
DROP TRIGGER IF EXISTS audit_trigger_row on information.informations;
DROP TRIGGER IF EXISTS audit_trigger_row on agreement.agreements;

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
select audit.audit_table('agreement.agreements'::regclass, true, true);
select audit.audit_table('information.informations'::regclass, true, true);
select audit.audit_table('information.informations_contents'::regclass, true, true);
select audit.audit_table('information.informations_contents_blocks'::regclass, true, true);
select audit.audit_table('information.informations_contents_blocks_contents'::regclass, true, true);
select audit.audit_table('information.informations_contents_references'::regclass, true, true);
select audit.audit_table('information.informations_references'::regclass, true, true);
select audit.audit_table('search.prequalified'::regclass, true, true);
select audit.audit_table('search.prequalified_documents'::regclass, true, true);
Comment on lines +1 to +9
Copy link
Contributor Author

Choose a reason for hiding this comment

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

J'ai mis toutes les récentes tables qu'on avait pas mis dans audit. J'en ai peut être oublié.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
delete from alerts
where repository = 'dares'
and status = 'todo';
Loading