Skip to content

Commit

Permalink
script de modification des ids de permanences dans les cras
Browse files Browse the repository at this point in the history
  • Loading branch information
morgan committed Sep 14, 2023
1 parent 22c2524 commit 9e04b38
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/tasks/correction/correctionDoublonsPermanences.js
Original file line number Diff line number Diff line change
@@ -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`);
});

0 comments on commit 9e04b38

Please sign in to comment.