From 32c8ab7cc49b2e1c9760b2c0d5436f34e22afd32 Mon Sep 17 00:00:00 2001 From: LionelB Date: Thu, 19 Sep 2024 16:54:43 +0200 Subject: [PATCH] feat(api): add exportableColumns getter on OrganizationImportFormat model --- .../organization-learner-import-formats.js | 2 +- .../models/OrganizationLearnerImportFormat.js | 3 ++ .../OrganizationLearnerImportFormat_test.js | 31 ++++++++++++++++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/api/db/seeds/data/common/organization-learner-import-formats.js b/api/db/seeds/data/common/organization-learner-import-formats.js index 8d73175a10f..b4d46721d46 100644 --- a/api/db/seeds/data/common/organization-learner-import-formats.js +++ b/api/db/seeds/data/common/organization-learner-import-formats.js @@ -77,7 +77,7 @@ export const organizationLearnerImportFormat = async function ({ databaseBuilder headers: [ { key: 1, name: 'Nom apprenant', property: 'lastName', required: true }, { key: 2, name: 'Prénom apprenant', property: 'firstName', required: true }, - { key: 3, name: 'Classe', required: true }, + { key: 3, name: 'Classe', required: true, config: { exportable: true } }, { key: 4, name: 'Date de naissance', required: true }, ], }, diff --git a/api/src/prescription/learner-management/domain/models/OrganizationLearnerImportFormat.js b/api/src/prescription/learner-management/domain/models/OrganizationLearnerImportFormat.js index 56d709d4412..7a372866620 100644 --- a/api/src/prescription/learner-management/domain/models/OrganizationLearnerImportFormat.js +++ b/api/src/prescription/learner-management/domain/models/OrganizationLearnerImportFormat.js @@ -75,6 +75,9 @@ class OrganizationLearnerImportFormat { }; }); } + get exportableColumns() { + return this.config.headers.flatMap(({ name, config }) => (config?.exportable ? { columnName: name } : [])); + } /** * @function diff --git a/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerImportFormat_test.js b/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerImportFormat_test.js index c3b099137d2..656560a2aeb 100644 --- a/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerImportFormat_test.js +++ b/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerImportFormat_test.js @@ -31,8 +31,8 @@ describe('Unit | Models | OrganizationLearnerImportFormat', function () { headers: [ { key: 1, name: 'Nom apprenant', property: 'lastName', required: true }, { key: 2, name: 'Prénom apprenant', property: 'firstName', required: true }, - { key: 3, name: 'catégorie', required: true }, - { key: 4, name: 'Date de naissance', required: true }, + { key: 3, name: 'catégorie', required: true, config: { exportable: true } }, + { key: 4, name: 'Date de naissance', required: true, config: { exportable: true } }, { key: 5, name: 'unicity key', required: true }, ], filterableColumns: [ @@ -256,8 +256,8 @@ describe('Unit | Models | OrganizationLearnerImportFormat', function () { expect(organizationLearnerImportFormat.headersFields).to.deep.equal([ { key: 1, name: 'Nom apprenant', property: 'lastName', required: true }, { key: 2, name: 'Prénom apprenant', property: 'firstName', required: true }, - { key: 3, name: 'catégorie', required: true }, - { key: 4, name: 'Date de naissance', required: true }, + { key: 3, name: 'catégorie', required: true, config: { exportable: true } }, + { key: 4, name: 'Date de naissance', required: true, config: { exportable: true } }, { key: 5, name: 'unicity key', required: true }, ]); }); @@ -283,4 +283,27 @@ describe('Unit | Models | OrganizationLearnerImportFormat', function () { }); }); }); + + describe('#exportableColumns', function () { + it('should return exportable columns', function () { + const organizationLearnerImportFormat = new OrganizationLearnerImportFormat( + organizationLearnerImportFormatPayload, + ); + expect(organizationLearnerImportFormat.exportableColumns).to.deep.equals([ + { columnName: 'catégorie' }, + { columnName: 'Date de naissance' }, + ]); + }); + + it('should return empty when there is no exportable columns', function () { + organizationLearnerImportFormatPayload.config.headers = [ + { key: 1, name: 'Nom apprenant', property: 'lastName', required: true }, + ]; + + const organizationLearnerImportFormat = new OrganizationLearnerImportFormat( + organizationLearnerImportFormatPayload, + ); + expect(organizationLearnerImportFormat.exportableColumns).lengthOf(0); + }); + }); });