From 854612870b95ae15cfd44da64751400cc3b7f12b Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Wed, 22 Nov 2023 16:39:25 +0100 Subject: [PATCH 1/3] =?UTF-8?q?fix(exports):=20supprime=20une=20fonction?= =?UTF-8?q?=20inutilis=C3=A9e,=20et=20d=C3=A9taille=20un=20objet=20de=20te?= =?UTF-8?q?st?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Features/ExportStrategies/BaseExporter.js | 12 +----------- .../Features/__fixtures__/record-for-exports.json | 13 ++++++++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/components/Features/ExportStrategies/BaseExporter.js b/src/components/Features/ExportStrategies/BaseExporter.js index 161db0872..da98fe51e 100644 --- a/src/components/Features/ExportStrategies/BaseExporter.js +++ b/src/components/Features/ExportStrategies/BaseExporter.js @@ -1,12 +1,6 @@ import { utils } from 'xlsx' import { cultureLabel, featureName } from "../index.js" -const frNumbers = new Intl.NumberFormat('fr-FR', { - style: 'decimal', - minimumSignificantDigits: 2, - maximumSignificantDigits: 2 -}) - export default class BaseExporter { label = '' extension = '' @@ -24,16 +18,12 @@ export default class BaseExporter { } } -export function humanNumbers (float) { - return frNumbers.format(float) -} - /** * * @param {Feature[]} features * @returns {String} */ -export function generateAutresInfos (features, { withName = true, withNotes = true, withDate = true, withSurface = true, withVariete = true, pivot = null, initialCulture } = {}) { +export function generateAutresInfos (features, { withDate = true, withName = true, withNotes = true, withSurface = true, withVariete = true, pivot = null, initialCulture } = {}) { return features.map(feature => { const name = withName ? featureName(feature, { ilotLabel: '', parcelleLabel: '', separator: '.', placeholder: '' }) : '' const notes = withNotes ? feature.properties.auditeur_notes : '' diff --git a/src/components/Features/__fixtures__/record-for-exports.json b/src/components/Features/__fixtures__/record-for-exports.json index 4b1edef86..8aa5b4579 100644 --- a/src/components/Features/__fixtures__/record-for-exports.json +++ b/src/components/Features/__fixtures__/record-for-exports.json @@ -2,7 +2,18 @@ "record_id": "054f0d70-c3da-448f-823e-81fcf7c2bf6e", "operator": { "id": 1, - "nom": "test" + "nom": "test", + "notifications": [ + { + "id": 1, + "status": "ACTIVE", + "organismeCertificateur": { + "id": 999, + "nom": "CartobiOC", + "numeroControleEu": "FR-BIO-999" + } + } + ] }, "parcelles": { "type": "FeatureCollection", From b20830a46909d34b4a825440c7326872547884b6 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Wed, 22 Nov 2023 16:40:16 +0100 Subject: [PATCH 2/3] feat(exports): export JSON par range, et ajout de la mention "semis le" --- .../Features/ExportStrategies/BaseExporter.js | 14 +++++++++---- .../ExportStrategies/BaseExporter.test.js | 20 +++++++++---------- .../ExportStrategies/CertisudExporter.test.js | 4 ++-- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/components/Features/ExportStrategies/BaseExporter.js b/src/components/Features/ExportStrategies/BaseExporter.js index da98fe51e..03930ce03 100644 --- a/src/components/Features/ExportStrategies/BaseExporter.js +++ b/src/components/Features/ExportStrategies/BaseExporter.js @@ -5,7 +5,7 @@ export default class BaseExporter { label = '' extension = '' mimetype = '' - origin = 'A1' + range = null constructor ({ featureCollection, operator, record }) { this.featureCollection = featureCollection @@ -14,7 +14,13 @@ export default class BaseExporter { } toJSON () { - return utils.sheet_to_json(this.getSheet(), { header: 1, origin }) + const ws = this.getSheet() + return utils.sheet_to_json(ws, { + blankrows: false, + defval: '', + header: 1, + range: this.range ?? ws['!ref'] + }) } } @@ -37,8 +43,8 @@ export function generateAutresInfos (features, { withDate = true, withName = tru // if we refine on a given culture, we certainly have a cell with its label // so we don't make it redundant pivot || (initialCulture === c.CPF) ? '' : cultureLabel(c, { withCode: true }), - withVariete ? c.variete : '', - withDate ? c.date_semis : '', + withVariete && c.variete ? c.variete : '', + withDate && c.date_semis ? `semis le ${c.date_semis}` : '', withSurface && c.surface ? `${c.surface}ha` : '' ].filter(d => d).join(', ')) ) diff --git a/src/components/Features/ExportStrategies/BaseExporter.test.js b/src/components/Features/ExportStrategies/BaseExporter.test.js index efd9d5193..5ff9d3b96 100644 --- a/src/components/Features/ExportStrategies/BaseExporter.test.js +++ b/src/components/Features/ExportStrategies/BaseExporter.test.js @@ -32,15 +32,15 @@ describe('generateAutresInfos', () => { } ] - expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou') + expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou') // in case we display by feature, and we have - expect(generateAutresInfos(features, { initialCulture: undefined })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou') - expect(generateAutresInfos(features, { initialCulture: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, 2023-03-31, 1ha ; 1.2, Coucou') + expect(generateAutresInfos(features, { initialCulture: undefined })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes, Coucou') + expect(generateAutresInfos(features, { initialCulture: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 1.2, Coucou') - expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes') - expect(generateAutresInfos(features, { withNotes: false, withName: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31, 1ha ; 01.13.41.1 Carottes') - expect(generateAutresInfos(features, { withNotes: false, withName: false, withVariete: false })).toBe('01.13.41.1 Carottes, 2023-03-31, 1ha ; 01.13.41.1 Carottes') + expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 1.2, 01.13.41.1 Carottes') + expect(generateAutresInfos(features, { withNotes: false, withName: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31, 1ha ; 01.13.41.1 Carottes') + expect(generateAutresInfos(features, { withNotes: false, withName: false, withVariete: false })).toBe('01.13.41.1 Carottes, semis le 2023-03-31, 1ha ; 01.13.41.1 Carottes') expect(generateAutresInfos(features, { withNotes: false, withName: false, withDate: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, 1ha ; 01.13.41.1 Carottes') expect(generateAutresInfos(features, { withNotes: false, withName: false, withDate: false, withSurface: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge ; 01.13.41.1 Carottes') }) @@ -83,9 +83,9 @@ describe('generateAutresInfos', () => { } ] - expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, 2022-02-01, Coucou') - expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, 2022-02-01') - expect(generateAutresInfos(features, { withNotes: false, withName: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, 2022-02-01') + expect(generateAutresInfos(features)).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, semis le 2022-02-01, Coucou') + expect(generateAutresInfos(features, { withNotes: false })).toBe('1.1, 01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 1.2, 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, semis le 2022-02-01') + expect(generateAutresInfos(features, { withNotes: false, withName: false })).toBe('01.13.41.1 Carottes, Chantenay à cœur rouge, semis le 2023-03-31 / 01.13.41.1 Carottes, Nantaise de Grasseval ; 01.13.41.1 Carottes / 01.13.42 Ail, Lautrec, semis le 2022-02-01') }) test('with two features with a pivot on a given culture', () => { @@ -126,7 +126,7 @@ describe('generateAutresInfos', () => { } ] - expect(generateAutresInfos(features, { pivot: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, 2023-03-31 / Nantaise de Grasseval, Coucou ! ; 1.2') + expect(generateAutresInfos(features, { pivot: '01.13.41.1' })).toBe('1.1, Chantenay à cœur rouge, semis le 2023-03-31 / Nantaise de Grasseval, Coucou ! ; 1.2') expect(generateAutresInfos(features, { pivot: '01.13.41.1', withDate: false, withNotes: false })).toBe('1.1, Chantenay à cœur rouge / Nantaise de Grasseval ; 1.2') }) }) diff --git a/src/components/Features/ExportStrategies/CertisudExporter.test.js b/src/components/Features/ExportStrategies/CertisudExporter.test.js index ce0c0bd36..b8013569d 100644 --- a/src/components/Features/ExportStrategies/CertisudExporter.test.js +++ b/src/components/Features/ExportStrategies/CertisudExporter.test.js @@ -38,7 +38,7 @@ describe('CertisudExporter', () => { 'Trèfle', // expect.closeTo(1.0), // in vite@5 + vitest@1 1.0464881572673355, - '4 feuilles, 2023-03-01', + '4 feuilles, semis le 2023-03-01', 'AB', new Date('2021-01-01T00:00:00.000Z'), ], @@ -46,7 +46,7 @@ describe('CertisudExporter', () => { 'Trèfle', // expect.closeTo(1.0), // in vite@5 + vitest@1 1.0464881572673355, - '4 feuilles, 2023-03-01', + '4 feuilles, semis le 2023-03-01', 'AB', new Date('2015-01-01T00:00:00.000Z'), ], From 83aebb030286a7d79f787a027d2cc5af56811a98 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Wed, 22 Nov 2023 16:41:27 +0100 Subject: [PATCH 3/3] =?UTF-8?q?fix(CertipaqExporter):=20vari=C3=A9t=C3=A9?= =?UTF-8?q?=20en=20colonne=20D,=20et=20test=20de=20l'export?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExportStrategies/CertipaqExporter.js | 17 +-- .../ExportStrategies/CertipaqExporter.test.js | 126 ++++++++++++++++++ 2 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 src/components/Features/ExportStrategies/CertipaqExporter.test.js diff --git a/src/components/Features/ExportStrategies/CertipaqExporter.js b/src/components/Features/ExportStrategies/CertipaqExporter.js index fee16f439..da559853a 100644 --- a/src/components/Features/ExportStrategies/CertipaqExporter.js +++ b/src/components/Features/ExportStrategies/CertipaqExporter.js @@ -67,14 +67,14 @@ const getSheet = ({ featureCollection, operator }) => { const culture = fromCodeCpf(props.cultures.at(0)?.CPF) return [ - // Commune + // Commune #A props.COMMUNE_LABEL, - // Ilot + // Ilot #B featureName({ properties: props }, { ilotLabel: '', parcelleLabel: '', separator: '_', placeholder: '' }), - // Culture + // Culture #C culture?.libelle_code_cpf ?? `[ERREUR] culture inconnue`, - // Variété / infos - '', + // Variété / infos #D + generateAutresInfos([{ id, geometry, properties: props }], { withDate: false, withName: false, withNotes: true, withSurface: false, withVariete: true, initialCulture: culture?.code_cpf }), // C0 - AB - C1 - C2 - C3 props.conversion_niveau === 'CONV' ? surfaceHa : '', props.conversion_niveau === 'AB' ? surfaceHa : '', @@ -83,8 +83,8 @@ const getSheet = ({ featureCollection, operator }) => { props.conversion_niveau === 'C3' ? surfaceHa : '', // Date conv props.engagement_date ? new Date(props.engagement_date) : '', - // Observation / date de semis - generateAutresInfos([{ id, geometry, properties: props }], { withName: false, initialCulture: culture?.code_cpf }), + // Observation / date de semis #K + generateAutresInfos([{ id, geometry, properties: props }], { withDate: true, withName: false, withNotes: false, withSurface: true, withVariete: false, initialCulture: culture?.code_cpf }), // Précédent '', // Anté précédent @@ -93,7 +93,7 @@ const getSheet = ({ featureCollection, operator }) => { '', // Date '', - // ParcelleId + // ParcelleId #P String(id), ] }), { origin: 'A6', cellDates: true }) @@ -159,6 +159,7 @@ class CertipaqExporter extends BaseExporter { label = "Tableur" extension = "csv" mimetype = "text/csv" + range = "A5:P999" getSheet() { return getSheet({ featureCollection: this.featureCollection, operator: this.operator } ) diff --git a/src/components/Features/ExportStrategies/CertipaqExporter.test.js b/src/components/Features/ExportStrategies/CertipaqExporter.test.js new file mode 100644 index 000000000..1ac1f78a9 --- /dev/null +++ b/src/components/Features/ExportStrategies/CertipaqExporter.test.js @@ -0,0 +1,126 @@ +import { describe, test, expect } from 'vitest' +import Exporter from './CertipaqExporter.js' +import record from '@/components/Features/__fixtures__/record-for-exports.json' assert { type: 'json' } + +describe('CertipaqExporter', () => { + test('list by features', () => { + const exporter = new Exporter({ + featureCollection: record.parcelles, + operator: record.operator, + record: record + }) + + const expectation = [ + [ + "Commune", + "Ilot", + "Culture", + "Variété / infos", + "C0", + "AB", + "C1", + "C2", + "C3", + "Date conv", + "Observation / date de semis", + "Précédent", + "Anté précédent", + "Produit", + "Date", + "Id. CartoBio" + ], + [ + '', + '', + 'Luzerne', + '', + '', + '', + '1,05', + '', + '', + new Date('2023-01-01T00:00:00.000Z'), + '', + '', + '', + '', + '', + '1' + ], + [ + '', + '', + 'Luzerne', + '', + '', + '', + '1,05', + '', + '', + new Date('2023-01-01T00:00:00.000Z'), + '', + '', + '', + '', + '', + '2' + ], + [ + '', + '', + 'Luzerne', + ' / 01.19.10.7 Trèfle, 4 feuilles', + '', + '1,05', + '', + '', + '', + new Date('2021-01-01T00:00:00.000Z'), + ' / 01.19.10.7 Trèfle, semis le 2023-03-01', + '', + '', + '', + '', + '3' + ], + [ + '', + '', + 'Trèfle', + '4 feuilles', + '', + '1,05', + '', + '', + '', + new Date('2015-01-01T00:00:00.000Z'), + 'semis le 2023-03-01', + '', + '', + '', + '', + '4' + ], + [ + '', + '', + '[ERREUR] culture inconnue', + '01.19.99 Culture inconnue', + '', + '', + '', + '', + '', + '', + '01.19.99 Culture inconnue', + '', + '', + '', + '', + '5' + ] + ] + + expect(exporter.toJSON()).toEqual(expectation) + }) +})