-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lbac-1777: refactor import referentiel opco Constructys (#848)
* fix: refactor import referentiel opco Constructys * Update server/src/jobs/lba_recruteur/opco/constructys/constructysImporter.ts Co-authored-by: Kevin Barnoin <[email protected]> * fix: remove accents from emails --------- Co-authored-by: Kevin Barnoin <[email protected]>
- Loading branch information
1 parent
63c0b09
commit 354bb9a
Showing
13 changed files
with
111 additions
and
65 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
2 changes: 1 addition & 1 deletion
2
server/src/common/model/schema/referentielOpco/referentielOpco.schema.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
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
76 changes: 40 additions & 36 deletions
76
server/src/jobs/lba_recruteur/opco/constructys/constructysImporter.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,76 +1,80 @@ | ||
import { createReadStream } from "fs" | ||
import path from "path" | ||
|
||
import Joi from "joi" | ||
import { filterData, oleoduc, transformData, writeData } from "oleoduc" | ||
import { oleoduc, transformData, writeData } from "oleoduc" | ||
import { removeAccents } from "shared" | ||
import { OPCOS } from "shared/constants/recruteur" | ||
|
||
import { notifyToSlack } from "@/common/utils/slackUtils" | ||
import { prepareReferentielOpcoForInsert } from "@/services/opco.service" | ||
|
||
import __dirname from "../../../../common/dirname" | ||
import { logger } from "../../../../common/logger" | ||
import { ReferentielOpco } from "../../../../common/model/index" | ||
import { fileDownloader, parseCsv } from "../../../../common/utils/fileUtils" | ||
import config from "../../../../config" | ||
import { runScript } from "../../../scriptWrapper" | ||
|
||
const importer = async (filePath, remoteFileName, opco_label) => { | ||
logger.info("Downloading file...") | ||
await fileDownloader(filePath, remoteFileName, config.ftp.constructys) | ||
|
||
const importer = async (filePath: string, opco_label: OPCOS) => { | ||
logger.info(`Deleting collection entries for ${opco_label}...`) | ||
await ReferentielOpco.deleteMany({ opco_label }) | ||
|
||
logger.info("Importing Data...") | ||
|
||
const stat = { | ||
const stats = { | ||
error: 0, | ||
total: 0, | ||
imported: 0, | ||
} | ||
|
||
await oleoduc( | ||
createReadStream(filePath), | ||
parseCsv(), | ||
filterData((e) => e.Mails), | ||
parseCsv({ delimiter: ";", encoding: "latin1" /* identique à ISO-8859-1 */ }), | ||
transformData((e) => { | ||
const emails: string[] = [] | ||
const { Siret, Mails } = e | ||
|
||
const emailsArray = Mails.split(/,|;| /).filter((x) => x) | ||
const emailsArrayDuplicateFree = [...new Set(emailsArray)] | ||
|
||
for (const email of emailsArrayDuplicateFree) { | ||
stat.total++ | ||
const { error, value } = Joi.string().email().validate(email, { abortEarly: false }) | ||
|
||
if (error) { | ||
stat.error++ | ||
return | ||
} | ||
|
||
stat.imported++ | ||
emails.push(value) | ||
const { Siret } = e | ||
stats.total++ | ||
const csvEmailStr = e["Email du contact"] | ||
const emailsArray = removeAccents(csvEmailStr) | ||
.split(/,|;| /) | ||
.filter((x) => x) | ||
const referentielOpt = prepareReferentielOpcoForInsert({ opco_label, siret_code: Siret, emails: emailsArray }) | ||
if (referentielOpt) { | ||
stats.imported++ | ||
return referentielOpt | ||
} else { | ||
logger.error("could not import", { siret: Siret, emails: csvEmailStr }) | ||
stats.error++ | ||
return | ||
} | ||
|
||
return { siret_code: Siret, emails: [...new Set(emails)] } | ||
}), | ||
writeData( | ||
async ({ siret_code, emails }) => { | ||
await ReferentielOpco.create({ opco_label, siret_code, emails }) | ||
async (referentiel) => { | ||
const { siret_code } = referentiel | ||
await ReferentielOpco.findOneAndUpdate({ siret_code }, { $set: referentiel }, { upsert: true }).lean() | ||
}, | ||
{ parallel: 500 } | ||
) | ||
) | ||
|
||
logger.info("Data import done.") | ||
return stat | ||
await notifyToSlack({ | ||
subject: "import referentiel opco Constructys", | ||
message: `${stats.total} documents. ${stats.error} erreurs. ${stats.imported} mises à jour`, | ||
error: stats.error > 0, | ||
}) | ||
return stats | ||
} | ||
|
||
runScript(async () => { | ||
export const importReferentielOpcoFromConstructys = async () => { | ||
logger.info("Constructys data import starting...") | ||
|
||
logger.info("Downloading file...") | ||
const dirname = __dirname(import.meta.url) | ||
const filePath = path.resolve(dirname, "./constructys-data.csv") | ||
const remoteFileName = "CTYS_MATCHA.csv" | ||
const opco_label = "Constructys" | ||
await fileDownloader(filePath, remoteFileName, config.ftp.constructys) | ||
|
||
const result = await importer(filePath, remoteFileName, opco_label) | ||
logger.info("Importing file...") | ||
const opco_label = OPCOS.CONSTRUCTYS | ||
const result = await importer(filePath, opco_label) | ||
return result | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./assertUnreachable" | ||
export * from "./stringUtils" |
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,26 @@ | ||
import { describe, it, expect } from "vitest" | ||
|
||
import { removeAccents } from "./stringUtils" | ||
|
||
describe("stringUtils", () => { | ||
describe("removeAccents", () => { | ||
it("should remove standard accents", () => { | ||
expect(removeAccents("àâä")).toBe("aaa") | ||
expect(removeAccents("éêëè")).toBe("eeee") | ||
expect(removeAccents("ïî")).toBe("ii") | ||
expect(removeAccents("ôö")).toBe("oo") | ||
expect(removeAccents("üùû")).toBe("uuu") | ||
expect(removeAccents("ÿŷ")).toBe("yy") | ||
}) | ||
it("should remove ç => c", () => { | ||
expect(removeAccents("ç")).toBe("c") | ||
}) | ||
it("should remove accents from capital letters", () => { | ||
expect(removeAccents("ÄÂÊËÏÎÔÖÛÜŸŶ")).toBe("AAEEIIOOUUYY") | ||
}) | ||
it("should not change standard characters", () => { | ||
const unchanged = `&"'(-_)=$*µ$£%!§:/;.,?~#{}[]|^@\`` | ||
expect(removeAccents(unchanged)).toBe(unchanged) | ||
}) | ||
}) | ||
}) |
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 @@ | ||
export const removeAccents = (str: string) => str.normalize("NFD").replace(/[\u0300-\u036f]/g, "") |