-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alerts): ajout d'un feature de notification pour les alertes de …
…la dares (#1004) * fix: alert * fix: alert * fix: alert * feat: ajout de la 1979 * fix: alert * fix: autoscroll
- Loading branch information
Showing
18 changed files
with
324 additions
and
5 deletions.
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
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,34 @@ | ||
import axios from "axios"; | ||
import { extractXlsxFromUrl } from "../scrapping"; | ||
|
||
jest.mock("axios"); | ||
|
||
describe("extractXlsxFromUrl", () => { | ||
it("should extract xlsx file from url", async () => { | ||
const url = "https://example.com/files"; | ||
const html = ` | ||
<html> | ||
<body> | ||
<a href="file1.xlsx">File 1</a> | ||
<a href="file2.xlsx">File 2</a> | ||
</body> | ||
</html> | ||
`; | ||
(axios.get as jest.Mock).mockResolvedValueOnce({ data: html }); | ||
const result = await extractXlsxFromUrl(url); | ||
expect(result).toBe("file1.xlsx"); | ||
}); | ||
|
||
it("should throw error if no xlsx file found", async () => { | ||
const url = "https://example.com/files"; | ||
const html = ` | ||
<html> | ||
<body> | ||
<a href="file1.pdf">File 1</a> | ||
</body> | ||
</html> | ||
`; | ||
(axios.get as jest.Mock).mockResolvedValueOnce({ data: html }); | ||
await expect(extractXlsxFromUrl(url)).rejects.toThrow("No xlsx file found"); | ||
}); | ||
}); |
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,4 @@ | ||
export const URL_SCRAPING = | ||
"https://code.travail.gouv.fr/fiche-ministere-travail/conventions-collectives-nomenclatures"; | ||
export const URL_KALI = | ||
"https://raw.githubusercontent.com/SocialGouv/kali-data/master/data/index.json"; |
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,46 @@ | ||
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) | ||
); | ||
|
||
const exceedingAgreementsFromKali = supportedCcIndexJson.filter( | ||
(ccXlsx) => !supportedCcXlsx.find((ccIndex) => ccIndex.num === ccXlsx.num) | ||
); | ||
|
||
return { missingAgreementsFromDares, exceedingAgreementsFromKali }; | ||
} |
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,21 @@ | ||
import axios from "axios"; | ||
import fs from "fs"; | ||
import os from "os"; | ||
import path from "path"; | ||
|
||
export async function downloadFileInTempFolder( | ||
url: string, | ||
nameOfFile: string | ||
): Promise<string> { | ||
const tempDir = os.tmpdir(); | ||
const filePath = path.join(tempDir, nameOfFile); | ||
|
||
const response = await axios.get(url, { responseType: "stream" }); | ||
const writer = fs.createWriteStream(filePath); | ||
response.data.pipe(writer); | ||
|
||
return new Promise((resolve, reject) => { | ||
writer.on("finish", () => resolve(filePath)); | ||
writer.on("error", reject); | ||
}); | ||
} |
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,13 @@ | ||
import { URL_KALI, URL_SCRAPING } from "./config"; | ||
import { getDifferenceBetweenIndexAndDares } from "./difference"; | ||
import { downloadFileInTempFolder } from "./download"; | ||
import { saveDiff } from "./save"; | ||
import { extractXlsxFromUrl } from "./scrapping"; | ||
|
||
export const runDares = async () => { | ||
const xlsxUrl = await extractXlsxFromUrl(URL_SCRAPING); | ||
const xlsxPath = await downloadFileInTempFolder(xlsxUrl, "dares.xlsx"); | ||
const indexPath = await downloadFileInTempFolder(URL_KALI, "index.json"); | ||
const diff = await getDifferenceBetweenIndexAndDares(xlsxPath, indexPath); | ||
await saveDiff(diff); | ||
}; |
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,70 @@ | ||
import { AlertRepository } from "../repositories/AlertRepository"; | ||
import { DaresAlertInsert, Diff } from "./types"; | ||
import { client } from "@shared/graphql-client"; | ||
|
||
export const saveDiff = async (diff: Diff) => { | ||
const alertRepository = new AlertRepository(client); | ||
|
||
const alertsRemovedToSave: DaresAlertInsert[] = | ||
diff.exceedingAgreementsFromKali.map((agreement) => ({ | ||
info: { | ||
id: agreement.num, | ||
}, | ||
status: "todo", | ||
repository: "dares", | ||
ref: "v0", | ||
changes: { | ||
type: "dares", | ||
title: agreement.name, | ||
ref: agreement.num.toString(), | ||
date: new Date(), | ||
modified: [], | ||
removed: [ | ||
{ | ||
name: agreement.name, | ||
num: agreement.num, | ||
}, | ||
], | ||
added: [], | ||
documents: [], | ||
}, | ||
})); | ||
|
||
const alertsAddedToSave: DaresAlertInsert[] = | ||
diff.missingAgreementsFromDares.map((agreement) => ({ | ||
info: { | ||
id: agreement.num, | ||
}, | ||
status: "todo", | ||
repository: "dares", | ||
ref: "v0", | ||
changes: { | ||
type: "dares", | ||
title: agreement.name, | ||
ref: agreement.num.toString(), | ||
date: new Date(), | ||
modified: [], | ||
added: [ | ||
{ | ||
name: agreement.name, | ||
num: agreement.num, | ||
}, | ||
], | ||
removed: [], | ||
documents: [], | ||
}, | ||
})); | ||
|
||
const alertsToSave = [...alertsAddedToSave, ...alertsRemovedToSave]; | ||
|
||
const inserts = await Promise.allSettled( | ||
alertsToSave.map((alert) => alertRepository.saveAlertDares(alert)) | ||
); | ||
|
||
inserts.forEach((insert) => { | ||
if (insert.status === "fulfilled") { | ||
const { ref, repository: repo, info } = insert.value; | ||
console.log(`insert alert for ${ref} on ${repo} (${info.id})`); | ||
} | ||
}); | ||
}; |
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,12 @@ | ||
import axios from "axios"; | ||
|
||
export const extractXlsxFromUrl = async (url: string) => { | ||
const response = await axios.get(url); | ||
const html = response.data; | ||
const regex = /href="([^"]*\.xlsx)"/g; | ||
const match = regex.exec(html); | ||
if (!match) { | ||
throw new Error("No xlsx file found"); | ||
} | ||
return match[1]; | ||
}; |
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,13 @@ | ||
import { DaresAlert } from "@shared/types"; | ||
|
||
export interface Diff { | ||
missingAgreementsFromDares: Agreement[]; | ||
exceedingAgreementsFromKali: Agreement[]; | ||
} | ||
|
||
export interface Agreement { | ||
name: string; | ||
num: number; | ||
} | ||
|
||
export type DaresAlertInsert = Omit<DaresAlert, "id">; |
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
Oops, something went wrong.