diff --git a/src/tasks/correction/correctionDoublonsPermanences.js b/src/tasks/correction/correctionDoublonsPermanences.js new file mode 100644 index 0000000..b27087a --- /dev/null +++ b/src/tasks/correction/correctionDoublonsPermanences.js @@ -0,0 +1,41 @@ +#!/usr/bin/env node +'use strict'; +const csv = require('csv-parser'); +const fs = require('fs'); +const { encrypt } = require('../../utils/encrypt'); +const { execute } = require('../utils'); + +const updateCraWithIdPermanence = dbDatalake => async (id, ids) => await dbDatalake.collection('cras').updateMany( + { 'permanenceId': { '$in': ids } }, + { 'permanenceId': id } +); + +execute(__filename, async ({ logger, dbDatalake }) => { + + const permanences = []; + const promises = []; + let nbMaj = 0; + + logger.info('Modification des ids suite à la fusion des doublons de permanences'); + + fs.createReadStream('data/imports/permanences-doublons.csv') + .pipe(csv({ separator: ';' })) + .on('data', data => permanences.push(data)) + .on('end', () => { + permanences.forEach(permanence => { + promises.push(new Promise(async resolve => { + const id = encrypt(permanence.idPermanence.toString()); + const ids = []; + permanence.doublons.split(',').forEach(id => { + ids.push(encrypt(id.toString())); + }); + await updateCraWithIdPermanence(dbDatalake)(id, ids); + nbMaj++; + resolve(); + })); + }); + Promise.all(promises); + }); + + logger.info(`${nbMaj} CRAs mis à jour`); +});