Skip to content

Commit

Permalink
Merge pull request #57 from ezpaarse-project/idp-to-abes-id
Browse files Browse the repository at this point in the history
add 3 middlewares
  • Loading branch information
felixleo22 authored Jan 24, 2025
2 parents 7ba07be + 31a2275 commit 794e08a
Show file tree
Hide file tree
Showing 16 changed files with 1,902 additions and 0 deletions.
386 changes: 386 additions & 0 deletions abesid-to-etab/Etablissements.csv

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions abesid-to-etab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# idp-to-abes-id

Add information about institution with abesID

## Enriched fields

| Name | Type | Description |
| --- | --- | --- |
| siren | String | IDP. |
| institutionName | String | IDP. |
| institutionType | String | Type of institution. |
| institutionAddress | String | Postal address of institution. |
| institutionCity | String | City of institution. |
| institutionPhone | String | Phone of institution. |
| institutionContact | String | Contact of institution. |
| institutionEmail | String | Contact email of institution. |
| institutionIpRange | String | IP range of institution. |

## Prerequisites

idp-to-abes-id enrichment middleware needs abes-id.

**You must use idp-to-abes-id after filter, parser, deduplicator middleware.**

## Headers

+ **idp-to-abes-id-source-field** : Fields in the ec for enrichment. "abes-id" by default.
+ **idp-to-abes-id-enriched-fields** : Enriched fields in the CE.
```json
{
"Siren": "siren",
"Nom de l'etablissement": "institutionName",
"Type de l'etablissement": "institutionType",
"Adresse de l'etablissement": "institutionAddress",
"Ville": "institutionCity",
"Telephone contact": "institutionPhone",
"Nom et prenom contact": "institutionContact",
"Adresse mail contact": "institutionEmail",
"IP validees": "institutionIpRange"
}
```
By default.
Example : "<Field in csv file>": "<Field in EC>"
Is not recommended to update field in csv file.

### Example

## How to use

### ezPAARSE admin interface

You can add idp-to-abes-id by default to all your enrichments, To do this, go to the middleware section of administration.

![image](./docs/admin-interface.png)

### ezPAARSE process interface

You can use idp-to-abes-id for an enrichment process. You just add the middleware.

![image](./docs/process-interface.png)

### ezp

You can use idp-to-abes-id for an enrichment process with [ezp](https://github.com/ezpaarse-project/node-ezpaarse) like this:

```bash
# enrich with one file
ezp process <path of your file> \
--host <host of your ezPAARSE instance> \
--settings <settings-id> \
--header "ezPAARSE-Middlewares: idp-to-abes-id" \
--out ./result.csv

# enrich with multiples files
ezp bulk <path of your directory> \
--host <host of your ezPAARSE instance> \
--settings <settings-id> \
--header "ezPAARSE-Middlewares: idp-to-abes-id"

```

### curl

You can use idp-to-abes-id for an enrichment process with curl like this:

```bash
curl -X POST -v http://localhost:59599 \
-H "ezPAARSE-Middlewares: idp-to-abes-id" \
-H "Log-Format-Ezproxy: <line format>" \
-F "file=@<log file path>"

```
113 changes: 113 additions & 0 deletions abesid-to-etab/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict';

const fs = require('fs');
const path = require('path');
const { parse } = require('csv-parse');

const parseCSVToJSON = (filePath) => {
return new Promise((resolve, reject) => {
const results = [];

const parser = fs.createReadStream(filePath).pipe(
parse({
columns: (header) => header.map((h) => h.trim()),
delimiter: ';',
skip_empty_lines: true,
})
);

parser.on('data', (row) => {
results.push(row);
});

parser.on('end', () => {
const data = results.reduce((acc, item) => {
acc[item['Identifiant Abes']] = {
['Siren']: item['Siren'],
['Nom de l\'etablissement']: item['Nom de l\'etablissement'],
['Type de l\'etablissement']: item['Type de l\'etablissement'],
['Adresse de l\'etablissement']: item['Adresse de l\'etablissement'],
['Ville']: item['Ville'],
['Telephone contact']: item['Telephone contact'],
['Nom et prenom contact']: item['Nom et prenom contact'],
['Adresse mail contact']: item['Adresse mail contact'],
['IP validees']: item['IP validees'],
};
return acc;
}, {});
resolve(data);
});

parser.on('error', (err) => {
reject(err);
});
});
};

module.exports = function () {
const job = this.job;
const report = this.report;
const req = this.request;
const logger = this.logger;

const sourceField = req.header('abesid-to-etab-source-field') || 'abes-id';
let enrichedFields = req.header('abesid-to-etab-enriched-fields');


if (enrichedFields) {
try {
enrichedFields = JSON.parse(enrichedFields);
} catch (err) {
const error = new Error(`[abesid-to-etab]: Cannot parse enrichedFields ${err}`);
error.status = 500;
return error;
}
} else {
enrichedFields = {
'Siren': 'siren',
'Nom de l\'etablissement': 'institutionName',
'Type de l\'etablissement': 'institutionType',
'Adresse de l\'etablissement': 'institutionAddress',
'Ville': 'institutionCity',
'Telephone contact': 'institutionPhone',
'Nom et prenom contact': 'institutionContact',
'Adresse mail contact': 'institutionEmail',
'IP validees': 'institutionIpRange'
}
}

let institutions = {};

const filePath = path.resolve(__dirname, 'Etablissements.csv');

return new Promise((resolve, reject) => {
parseCSVToJSON(filePath)
.then((jsonData) => {
institutions = jsonData;
logger.info('[abesid-to-etab]: Successfully read CSV File');
resolve(process);
})
.catch((err) => {
logger.error('[abesid-to-etab]: Cannot read CSV File', err);
this.job._stop(err);
reject(err);
});
});

function process(ec, next) {
if (!ec || !ec[sourceField]) { return next(); }

if (institutions[ec[sourceField]]) {
const dataFromCSV = institutions[ec[sourceField]];

Object.entries(enrichedFields).forEach(([keyFromCSV, enrichedKey]) => {
if (ec[enrichedKey]) {
return;
}
ec[enrichedKey] = dataFromCSV[keyFromCSV];
});
}

next();
}
};
58 changes: 58 additions & 0 deletions abesid-to-etab/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

const { contextify } = require('../mock');
const mw = require('.');
const { expect } = require('chai');

const ecs = [
{ 'abes-id': 'ABES5SYHNO3YL' },
{ 'login': 'ABES5SYHNO3YL' },
];


describe('abesid-to-etab', () => {
it('Should enrich institution info with "abes-id" as source field', async () => {
const process = await contextify(mw);
const ec = ecs[0];
process(ec, () => {});
expect(ec).to.have.property('abes-id', 'ABES5SYHNO3YL');
expect(ec).to.have.property('siren', '200034528');
expect(ec).to.have.property('institutionName', 'CHU Martinique');
expect(ec).to.have.property('institutionType', 'CHR-CHU');
expect(ec).to.have.property('institutionAddress', 'CHU Maartinique 97261 CS 90632 CEDEX');
expect(ec).to.have.property('institutionCity', 'Fort-de-France');
expect(ec).to.have.property('institutionPhone', '596722037');
expect(ec).to.have.property('institutionContact', 'ALPHONSE Sylvie');
expect(ec).to.have.property('institutionEmail', '[email protected]');
expect(ec).to.have.property('institutionIpRange', '95.138.124.9, 5.187.120.33, 95.138.127.113, 213.16.1.153, 213.16.2.141, 213.16.1.154');
});

it('Should enrich institution info with "login" as source field', async () => {
const process = await contextify(mw, (ctx) => {
ctx.request.headers['abesid-to-etab-source-field'] = 'login';
});
const ec = ecs[1];
process(ec, () => {});
expect(ec).to.have.property('login', 'ABES5SYHNO3YL');
expect(ec).to.have.property('siren', '200034528');
expect(ec).to.have.property('institutionName', 'CHU Martinique');
expect(ec).to.have.property('institutionType', 'CHR-CHU');
expect(ec).to.have.property('institutionAddress', 'CHU Maartinique 97261 CS 90632 CEDEX');
expect(ec).to.have.property('institutionCity', 'Fort-de-France');
expect(ec).to.have.property('institutionPhone', '596722037');
expect(ec).to.have.property('institutionContact', 'ALPHONSE Sylvie');
expect(ec).to.have.property('institutionEmail', '[email protected]');
expect(ec).to.have.property('institutionIpRange', '95.138.124.9, 5.187.120.33, 95.138.127.113, 213.16.1.153, 213.16.2.141, 213.16.1.154');
});

it('Should enrich "custom id" with "login" as source field', async () => {
const process = await contextify(mw, (ctx) => {
ctx.request.headers['abesid-to-etab-source-field'] = 'login';
ctx.request.headers['abesid-to-etab-enriched-fields'] = '{ "Siren": "custom-id" }';
});
const ec = ecs[1];
process(ec, () => {});
expect(ec).to.have.property('login', 'ABES5SYHNO3YL');
expect(ec).to.have.property('custom-id', '200034528');
});
});
70 changes: 70 additions & 0 deletions idp-to-abesid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# idp-to-abesid

Add ABES ID based on IDP.

## Enriched fields

| Name | Type | Description |
| --- | --- | --- |
| abes-id | String | ABES ID. |

## Prerequisites

## Prerequisites

idp-to-abesid enrichment middleware needs idp in ec.

**You must use idp-to-abesid after filter, parser, deduplicator middleware.**

## Headers

+ **idp-to-abesid-source-field** : Fields in the ec for enrichment. "login" by default.
+ **idp-to-abesid-enriched-field** : Enriched fields in the CE. "abes-id" by default

### Example

## How to use

### ezPAARSE admin interface

You can add idp-to-abesid by default to all your enrichments, To do this, go to the middleware section of administration.

![image](./docs/admin-interface.png)

### ezPAARSE process interface

You can use idp-to-abesid for an enrichment process. You just add the middleware.

![image](./docs/process-interface.png)

### ezp

You can use idp-to-abesid for an enrichment process with [ezp](https://github.com/ezpaarse-project/node-ezpaarse) like this:

```bash
# enrich with one file
ezp process <path of your file> \
--host <host of your ezPAARSE instance> \
--settings <settings-id> \
--header "ezPAARSE-Middlewares: idp-to-abesid" \
--out ./result.csv

# enrich with multiples files
ezp bulk <path of your directory> \
--host <host of your ezPAARSE instance> \
--settings <settings-id> \
--header "ezPAARSE-Middlewares: idp-to-abesid"

```

### curl

You can use idp-to-abesid for an enrichment process with curl like this:

```bash
curl -X POST -v http://localhost:59599 \
-H "ezPAARSE-Middlewares: idp-to-abesid" \
-H "Log-Format-Ezproxy: <line format>" \
-F "file=@<log file path>"

```
Loading

0 comments on commit 794e08a

Please sign in to comment.