diff --git a/server/src/common/utils/fileUtils.ts b/server/src/common/utils/fileUtils.ts index b3e904179f..bf375ce8b6 100644 --- a/server/src/common/utils/fileUtils.ts +++ b/server/src/common/utils/fileUtils.ts @@ -1,48 +1,16 @@ -import path from "path" - -import csvToJson from "convert-csv-to-json" import { Options as CsvParseOptions, parse } from "csv-parse" import { isEmpty, pickBy } from "lodash-es" import XLSX from "xlsx" import config from "../../config" -import __dirname from "../dirname" import { FTPClient } from "./ftpUtils" -export const readJsonFromCsvFile = (localPath) => { - return csvToJson.getJsonFromCsv(localPath) -} - export const readXLSXFile = (localPath) => { const workbook = XLSX.readFile(localPath, { codepage: 65001 }) return { sheet_name_list: workbook.SheetNames, workbook } } -export const createXLSXFile = (data, localPath) => { - const workbook = XLSX.utils.book_new() - XLSX.utils.book_append_sheet(workbook, XLSX.utils.json_to_sheet(data), "data") - - // @ts-expect-error writeFileAsync Cb function is not properly typed - XLSX.writeFileAsync(path.join(localPath), workbook, (e) => { - if (e) { - console.log(e) - throw new Error("La génération du fichier excel à échoué : ", e) - } - }) -} - -export const convertIntoBuffer = (workbook) => { - return XLSX.write(workbook, { type: "buffer", bookType: "xlsx" }) -} - -export const removeLine = (data, regex) => { - return data - .split("\n") - .filter((val) => !regex.test(val)) - .join("\n") -} - export const prepareMessageForMail = (data) => { const result = data ? data.replace(/(<([^>]+)>)/gi, "") : data return result ? result.replace(/\r\n|\r|\n/gi, "
") : result diff --git a/server/src/jobs/campaign/generateCsvForCampaign.ts b/server/src/jobs/campaign/generateCsvForCampaign.ts deleted file mode 100644 index 9292e8037a..0000000000 --- a/server/src/jobs/campaign/generateCsvForCampaign.ts +++ /dev/null @@ -1,151 +0,0 @@ -import axios from "axios" - -import __dirname from "../../common/dirname" -import { FormationCatalogue } from "../../common/model/index" -import { asyncForEach } from "../../common/utils/asyncUtils" -import { createXLSXFile } from "../../common/utils/fileUtils" -import { getDistanceInKm } from "../../common/utils/geolib" -import { runScript } from "../scriptWrapper" - -// @ts-expect-error -// eslint-disable-next-line import/no-unresolved, import/extensions -import jeunes from "./jeunes.json" assert { type: "json" } - -const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) - -runScript(async () => { - const buffer: any[] = [] - - await asyncForEach(jeunes, async (jeune: any, index) => { - if (!jeune.email_contact) return - - console.log(`${index}/${jeunes.length}`) - - let formation: any[] = [] - - formation = await FormationCatalogue.find({ - $or: [ - { - etablissement_formateur_siret: jeune.siret_etablissement, - }, - { - etablissement_gestionnaire_siret: jeune.siret_etablissement, - }, - ], - - cfd: jeune.formation_cfd, - tags: { $in: ["2022"] }, - published: true, - catalogue_published: true, - }) - - if (!formation.length) { - formation = await FormationCatalogue.find({ - $or: [ - { - etablissement_formateur_uai: jeune.uai_etablissement, - }, - { - etablissement_gestionnaire_uai: jeune.uai_etablissement, - }, - ], - cfd: jeune.formation_cfd, - tags: { $in: ["2022"] }, - published: true, - catalogue_published: true, - }) - } - - if (formation.length === 0) { - buffer.push({ - email: jeune.email_contact, - lien: "https://labonnealternance.apprentissage.beta.gouv.fr&utm_source=campagne-mna&utm_medium=email&utm_campaign=jeunessanscontrat1222", - prenom: jeune.prenom_apprenant, - libelle_formation: jeune.libelle_long_formation, - siret: jeune.siret_etablissement, - }) - } - - if (formation.length > 1) { - if (!jeune.code_commune_insee_apprenant) return - try { - const { - data: { features }, - } = await axios.get(`https://api-adresse.data.gouv.fr/search/?q=${jeune.code_commune_insee_apprenant}&type=municipality`) - - if (!features.length) { - buffer.push({ - email: jeune.email_contact, - lien: "https://labonnealternance.apprentissage.beta.gouv.fr&utm_source=campagne-mna&utm_medium=email&utm_campaign=jeunessanscontrat1222", - prenom: jeune.prenom_apprenant, - libelle_formation: jeune.libelle_long_formation, - siret: jeune.siret_etablissement, - }) - return - } - - const [origineLat, origineLong] = features[0].geometry.coordinates - - const distanceBuffer: any[] = [] - //prendre la formation la plus proche par rapport à la géoloc trouvé - formation.map((form) => { - if (!form.lieu_formation_geo_coordonnees) return - - const [destLong, destLat] = form.lieu_formation_geo_coordonnees.split(",") - const distanceEnKm = getDistanceInKm({ origin: { latitude: origineLat, longitude: origineLong }, destination: { latitude: destLat, longitude: destLong } }) - distanceBuffer.push({ distance: distanceEnKm, form }) - }) - - //récupérer tous les distances, les classer par ordre croissant, prendre le premier. - distanceBuffer.sort((a, b) => a.distance - b.distance) - - const closestFormation = distanceBuffer[0].form - - if (!closestFormation.rome_codes.length || closestFormation.lieu_formation_geo_coordonnees === "null" || closestFormation.lieu_formation_geo_coordonnees == undefined) { - return - } - - const romes = closestFormation.rome_codes.toString() - const [lat, long] = closestFormation.lieu_formation_geo_coordonnees.split(",") - const url = `https://labonnealternance.apprentissage.beta.gouv.fr/recherche-emploi?&display=list&romes=${romes}&radius=60&lat=${lat}&lon=${long}&utm_source=campagne-mna&utm_medium=email&utm_campaign=jeunessanscontrat1222` - - buffer.push({ - email: jeune.email_contact, - lien: url, - prenom: jeune.prenom_apprenant, - libelle_formation: jeune.libelle_long_formation, - siret: jeune.siret_etablissement, - }) - - // Delay calls to avoid BAN from api-adresse.data.gouv.fr - await delay(800) - } catch (error) { - console.error(error) - } - } - - if (formation.length === 1) { - const x = formation[0] - - if (!x.rome_codes.length || x.lieu_formation_geo_coordonnees === "null" || x.lieu_formation_geo_coordonnees == undefined) { - return - } - - const romes = x.rome_codes.toString() - const [lat, long] = x.lieu_formation_geo_coordonnees.split(",") - const url = `https://labonnealternance.apprentissage.beta.gouv.fr/recherche-emploi?&display=list&romes=${romes}&radius=60&lat=${lat}&lon=${long}&utm_source=campagne-mna&utm_medium=email&utm_campaign=jeunessanscontrat1222` - - buffer.push({ - email: jeune.email_contact, - lien: url, - prenom: jeune.prenom_apprenant, - libelle_formation: jeune.libelle_long_formation, - siret: jeune.siret_etablissement, - }) - } - }) - - const filePath = __dirname(import.meta.url) - - createXLSXFile(buffer, `${filePath}/jeunes.xlsx`) -}) diff --git a/server/src/jobs/campaign/generateLbacLinksForParcoursup.ts b/server/src/jobs/campaign/generateLbacLinksForParcoursup.ts deleted file mode 100644 index d5eb9b2df9..0000000000 --- a/server/src/jobs/campaign/generateLbacLinksForParcoursup.ts +++ /dev/null @@ -1,141 +0,0 @@ -import fs from "fs" -import { URL } from "url" - -import { Appointment } from "../../common/model/index" -import { asyncForEach } from "../../common/utils/asyncUtils" -import { runScript } from "../scriptWrapper" - -const lbaDomain = "https://labonnealternance.apprentissage.beta.gouv.fr" -const utmData = "&utm_source=lba&utm_medium=email&utm_campaign=promotion-emploi-jeunes-rdva-ps" - -const aggregateQuery = [ - { - $match: { - appointment_origin: "PARCOURSUP", - created_at: { - $gt: new Date("Tue, 17 Jan 2023 00:00:00 GMT"), - }, - }, - }, - { - $sort: { - applicant_id: 1, - created_at: -1, - }, - }, - { - $group: { - _id: "$applicant_id", - created_at: { - $first: "$created_at", - }, - cfa_formateur_siret: { - $first: "$cfa_formateur_siret", - }, - applicant_id: { - $first: "$applicant_id", - }, - cle_ministere_educatif: { - $first: "$cle_ministere_educatif", - }, - }, - }, - { - $lookup: { - from: "users", - let: { - applicantId: { - $toObjectId: "$applicant_id", - }, - }, - pipeline: [ - { - $match: { - $expr: { - $eq: ["$_id", "$$applicantId"], - }, - }, - }, - ], - as: "candidat", - }, - }, - { - $unwind: { - path: "$candidat", - }, - }, - { - $lookup: { - from: "etablissements", - localField: "cfa_formateur_siret", - foreignField: "formateur_siret", - as: "etablissement", - }, - }, - { - $unwind: { - path: "$etablissement", - }, - }, - { - $lookup: { - from: "formationcatalogues", - localField: "cle_ministere_educatif", - foreignField: "cle_ministere_educatif", - as: "formationcatalogues", - }, - }, - { - $unwind: { - path: "$formationcatalogues", - }, - }, - { - $project: { - prenom: "$candidat.firstname", - nom: "$candidat.lastname", - email: "$candidat.email", - etablissement: "$etablissement.raison_sociale", - ville: "$etablissement.formateur_city", - clef_me: "$cle_ministere_educatif", - lieu_formation_geo_coordonnees: "$formationcatalogues.lieu_formation_geo_coordonnees", - rome_codes: "$formationcatalogues.rome_codes", - }, - }, -] - -const filePath = "voeux_parcoursup_lien_lbac.csv" - -function formatCsvLine(obj) { - return Object.values(obj).join("\t") -} - -const buildLien = (voeu) => { - const latLon = voeu.lieu_formation_geo_coordonnees.split(",") - return new URL(`${lbaDomain}/recherche-emploi?&romes=${voeu.rome_codes}&lat=${latLon[0]}&lon=${latLon[1]}&radius=60${utmData}`).toString() -} - -const header = "lien\tapplicant_id\tprenom\tnom\temail\tvoeu_etablissement_raison_sociale\tvoeu_etablissement_ville\tclef_ministere_educatif\tlatlon\tcodes_romes\n" - -/** - * Construit un fichier csv contenant une proposition de recherche pour chaque candidat ayant pris au moins un RDV depuis PARCOURSUP - */ -runScript(async () => { - const voeux = await Appointment.aggregate(aggregateQuery) - - const voeuxAvecLiens: any[] = [] - await asyncForEach(voeux, async (voeu) => { - await voeuxAvecLiens.push(formatCsvLine({ lien: buildLien(voeu), ...voeu })) - }) - - const csvContent = header + voeuxAvecLiens.join("\n") - - fs.writeFile(filePath, csvContent, (err) => { - if (err) { - console.error("Une erreur est survenue lors de l'écriture du fichier :", err) - } else { - console.info("Le fichier a été écrit avec succès.") - } - }) -})