Skip to content

Commit

Permalink
feat(dashboard): possibilité de télécharger un export des consultatio…
Browse files Browse the repository at this point in the history
…ns depuis les stats
  • Loading branch information
Arnaud AMBROSELLI committed Nov 6, 2023
1 parent db22cbe commit e135eba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
40 changes: 37 additions & 3 deletions dashboard/src/scenes/data-import-export/ExportFormattedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import { teamsState, userState } from '../../recoil/auth';
import API from '../../services/api';
import { customFieldsObsSelector } from '../../recoil/territoryObservations';
import { territoriesState } from '../../recoil/territory';
import { flattenedCustomFieldsConsultationsSelector } from '../../recoil/consultations';

// Source: https://tailwindui.com/components/application-ui/elements/dropdowns
export default function ExportFormattedData({ personCreated, personUpdated, actions, rencontres, passages, observations }) {
export default function ExportFormattedData({ personCreated, personUpdated, actions, rencontres, passages, observations, consultations }) {
const teams = useRecoilValue(teamsState);
const persons = useRecoilValue(personsState);
const territories = useRecoilValue(territoriesState);
const user = useRecoilValue(userState);
const personFieldsIncludingCustomFields = useRecoilValue(personFieldsIncludingCustomFieldsSelector);
const customFieldsObs = useRecoilValue(customFieldsObsSelector);
const consultationsFields = useRecoilValue(flattenedCustomFieldsConsultationsSelector);
const [users, setUsers] = useState([]);

async function fetchUsers() {
Expand Down Expand Up @@ -65,7 +67,7 @@ export default function ExportFormattedData({ personCreated, personUpdated, acti
Groupe: action.group,
Structure: action.structure,
'Avec heure': action.withTime ? 'Oui' : 'Non',
Équipe: action.teams?.length ? action.teams.map((t) => teams.find((action) => action._id === t)?.name).join(', ') : action.team,
Équipe: action.teams?.length ? action.teams.map((t) => teams.find((team) => team._id === t)?.name).join(', ') : action.team,
Urgent: action.urgent ? 'Oui' : 'Non',
Statut: action.status,
'Complétée le': action.completedAt ? dayjsInstance(action.completedAt).format('YYYY-MM-DD HH:mm') : '',
Expand All @@ -76,6 +78,32 @@ export default function ExportFormattedData({ personCreated, personUpdated, acti
};
};

const transformConsultation = (loadedUsers) => (consultation) => {
return {
id: consultation._id,
Équipe: consultation.teams?.length ? consultation.teams.map((t) => teams.find((team) => team._id === t)?.name).join(', ') : consultation.team,
'Avec heure': consultation.withTime ? 'Oui' : 'Non',
Statut: consultation.status,
'Personne suivie - Nom': persons.find((p) => p._id === consultation.person)?.name,
'Personne suivie - id': persons.find((p) => p._id === consultation.person)?._id,
Type: consultation.type,
...consultationsFields.reduce((fields, field) => {
if (['date', 'date-with-time'].includes(field.type))
fields[field.label || field.name] = consultation[field.name]
? dayjsInstance(consultation[field.name]).format(field.type === 'date' ? 'YYYY-MM-DD' : 'YYYY-MM-DD HH:mm')
: '';
else if (['boolean'].includes(field.type)) fields[field.label || field.name] = consultation[field.name] ? 'Oui' : 'Non';
else if (['yes-no'].includes(field.type)) fields[field.label || field.name] = consultation[field.name];
else if (Array.isArray(consultation[field.name])) fields[field.label || field.name] = consultation[field.name].join(', ');
else fields[field.label || field.name] = consultation[field.name];
return fields;
}, {}),
'Créée par': loadedUsers.find((u) => u._id === consultation.user)?.name,
'Créée le': dayjsInstance(consultation.createdAt).format('YYYY-MM-DD HH:mm'),
'Mise à jour le': dayjsInstance(consultation.updatedAt).format('YYYY-MM-DD HH:mm'),
};
};

const transformRencontre = (loadedUsers) => (rencontre) => {
return {
id: rencontre._id,
Expand Down Expand Up @@ -105,7 +133,6 @@ export default function ExportFormattedData({ personCreated, personUpdated, acti
};

const transformObservation = (loadedUsers) => (observation) => {
console.log(observation, customFieldsObs);
return {
id: observation._id,
'Territoire - Nom': territories.find((t) => t._id === observation.territory)?.name,
Expand Down Expand Up @@ -186,6 +213,13 @@ export default function ExportFormattedData({ personCreated, personUpdated, acti
);
}}
/>
<MenuItem
text="Consultations"
onClick={async () => {
const loadedUsers = await fetchUsers();
exportXlsx('Consultations', consultations.map(transformConsultation(loadedUsers)));
}}
/>
<MenuItem
text="Rencontres"
onClick={async () => {
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/scenes/stats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ const Stats = () => {
personCreated={personsCreated}
personUpdated={personsUpdated}
actions={actionsWithDetailedGroupAndCategories}
consultations={consultationsFilteredByPersons}
/>
</div>
</div>
Expand Down

0 comments on commit e135eba

Please sign in to comment.