Skip to content

Commit

Permalink
refactor(api): use BaseScript when updating DPO info in certification…
Browse files Browse the repository at this point in the history
… center
  • Loading branch information
bpetetot committed Oct 8, 2024
1 parent 8dc1b6d commit e6df5af
Showing 1 changed file with 48 additions and 66 deletions.
114 changes: 48 additions & 66 deletions api/scripts/add-or-update-certification-centers-dpo-infos.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
import 'dotenv/config';

import * as url from 'node:url';

import _ from 'lodash';

import { disconnect } from '../db/knex-database-connection.js';
import { updateCertificationCenterDataProtectionOfficerInformation } from '../lib/domain/usecases/update-certification-center-data-protection-officer-information.js';
import * as dataProtectionOfficerRepository from '../lib/infrastructure/repositories/data-protection-officer-repository.js';
import { BaseScript } from '../src/shared/application/scripts/base-script.js';
import { checkCsvHeader, parseCsvWithHeader } from './helpers/csvHelpers.js';

const modulePath = url.fileURLToPath(import.meta.url);
const IS_LAUNCHED_FROM_CLI = process.argv[1] === modulePath;
const REQUIRED_FIELD_NAMES = ['certificationCenterId', 'firstName', 'lastName', 'email'];

export class AddOrUpdateCertificationCentersDpoInfosScript extends BaseScript {
constructor() {
super({
name: 'Add-or-update-certification-centers-dpo-infos',
description: 'This is script is adding or updating certification centers data protection officer information.',
permanent: true,
options: {
filePath: { type: 'string', describe: 'CSV File to process', demandOption: true },
},
});
}

async handle({ options, logger }) {
const { filePath } = options;

await checkCsvHeader({ filePath, requiredFieldNames: REQUIRED_FIELD_NAMES });

logger.info('Reading and parsing csv data file... ');
const dataProtectionOfficers = await parseCsvWithHeader(filePath, parsingOptions);

const errors = [];
for (const dataProtectionOfficer of dataProtectionOfficers) {
try {
await updateCertificationCenterDataProtectionOfficerInformation({
dataProtectionOfficer,
dataProtectionOfficerRepository,
});
} catch (error) {
errors.push({ dataProtectionOfficer, error });
}
}

if (errors.length === 0) return;

logger.info(`Errors occurs on ${errors.length} element!`);
errors.forEach((error) => {
logger.info(JSON.stringify(error.dataProtectionOfficer));
logger.error(error.error?.message);
});

throw new Error('Process done with errors');
}
}

const parsingOptions = {
skipEmptyLines: true,
header: true,
Expand All @@ -30,67 +68,11 @@ const parsingOptions = {
} else {
value = null;
}

return value;
},
};

async function _updateCertificationCentersDataProtectionOfficerInformation(filePath) {
const errors = [];

await checkCsvHeader({
filePath,
requiredFieldNames: REQUIRED_FIELD_NAMES,
});

console.log('Reading and parsing csv data file... ');

const dataProtectionOfficers = await parseCsvWithHeader(filePath, parsingOptions);

for (const dataProtectionOfficer of dataProtectionOfficers) {
try {
await updateCertificationCenterDataProtectionOfficerInformation({
dataProtectionOfficer,
dataProtectionOfficerRepository,
});
} catch (error) {
errors.push({ dataProtectionOfficer, error });
}
}

if (errors.length === 0) {
return;
}

console.log(`Errors occurs on ${errors.length} element!`);
errors.forEach((error) => {
console.log(JSON.stringify(error.dataProtectionOfficer));
console.error(error.error?.message);
});

throw new Error('Process done with errors');
}

async function main() {
console.log('Starting updating certification centers data protection officer information.');
console.time('Certification centers DPO updated');

const filePath = process.argv[2];
await _updateCertificationCentersDataProtectionOfficerInformation(filePath);

console.timeEnd('Certification centers DPO updated');
if (BaseScript.isExecutedDirectly(import.meta.url)) {
const script = new AddOrUpdateCertificationCentersDpoInfosScript();
await script.exec();
}

(async function () {
if (IS_LAUNCHED_FROM_CLI) {
try {
await main();
console.log('\nCertification centers DPO information updated with success!');
} catch (error) {
console.error(error?.message);
process.exitCode = 1;
} finally {
await disconnect();
}
}
})();

0 comments on commit e6df5af

Please sign in to comment.