Skip to content

Commit

Permalink
feat: historique pour dossier médical
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Sep 11, 2023
1 parent b6ed48a commit be6e670
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 19 deletions.
16 changes: 15 additions & 1 deletion app/src/scenes/Persons/MedicalFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ const MedicalFile = ({ navigation, person, personDB, onUpdatePerson, updating, e

const onUpdateRequest = async (latestMedicalFile) => {
if (!latestMedicalFile) latestMedicalFile = medicalFile;

const historyEntry = {
date: new Date(),
user: user._id,
data: {},
};
for (const key in latestMedicalFile) {
if (!customFieldsMedicalFile.map((field) => field.name).includes(key)) continue;
if (latestMedicalFile[key] !== medicalFileDB[key]) {
historyEntry.data[key] = { oldValue: medicalFileDB[key], newValue: latestMedicalFile[key] };
}
}
if (!!Object.keys(historyEntry.data).length) latestMedicalFile.history = [...(medicalFileDB.history || []), historyEntry];

const response = await API.put({
path: `/medical-file/${medicalFileDB._id}`,
body: prepareMedicalFileForEncryption(customFieldsMedicalFile)({ ...medicalFileDB, ...latestMedicalFile }),
Expand Down Expand Up @@ -309,7 +323,7 @@ const MedicalFile = ({ navigation, person, personDB, onUpdatePerson, updating, e
<ButtonsContainer>
<Button
caption={editable ? 'Mettre à jour' : 'Modifier'}
onPress={editable ? onUpdateRequest : onEdit}
onPress={() => (editable ? onUpdateRequest() : onEdit())}
disabled={editable ? isUpdateDisabled && isMedicalFileUpdateDisabled : false}
loading={updating}
/>
Expand Down
5 changes: 5 additions & 0 deletions app/src/scenes/Persons/Person.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const Person = ({ route, navigation }) => {
outOfActiveList: person.outOfActiveList || false,
outOfActiveListReasons: person.outOfActiveListReasons || [],
documents: person.documents || [],
history: person.history || [],
};
},
[flattenedCustomFieldsPersons]
Expand Down Expand Up @@ -145,6 +146,10 @@ const Person = ({ route, navigation }) => {
}
if (!!Object.keys(historyEntry.data).length) personToUpdate.history = [...(oldPerson.history || []), historyEntry];

console.log('personToUpdate.history', personToUpdate.history);
console.log('oldPerson.history', oldPerson.history);
console.log('historyEntry', historyEntry);

const response = await API.put({
path: `/person/${personDB._id}`,
body: preparePersonForEncryption(personToUpdate),
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/recoil/medicalFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const customFieldsMedicalFileSelector = selector<CustomField[]>({
},
});

const encryptedFields = ['person', 'documents', 'comments'];
const encryptedFields = ['person', 'documents', 'comments', 'history'];

export const prepareMedicalFileForEncryption =
(customFieldsMedicalFile: CustomField[]) =>
Expand Down
39 changes: 34 additions & 5 deletions dashboard/src/scenes/person/components/EditModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,21 @@ export default function EditModal({ person, selectedPanel, onClose, isMedicalFil
body.entityKey = medicalFile.entityKey;
const bodyMedicalFile = body;

const historyEntry = {
date: new Date(),
user: user._id,
data: {},
};
for (const key in bodyMedicalFile) {
if (!customFieldsMedicalFile.map((field) => field.name).includes(key)) continue;
if (bodyMedicalFile[key] !== medicalFile[key]) {
historyEntry.data[key] = { oldValue: medicalFile[key], newValue: bodyMedicalFile[key] };
}
}
if (!!Object.keys(historyEntry.data).length) {
bodyMedicalFile.history = [...(medicalFile.history || []), historyEntry];
}

const mfResponse = await API.put({
path: `/medical-file/${medicalFile._id}`,
body: prepareMedicalFileForEncryption(customFieldsMedicalFile)({ ...medicalFile, ...bodyMedicalFile }),
Expand All @@ -316,13 +331,27 @@ export default function EditModal({ person, selectedPanel, onClose, isMedicalFil
const structureMedical = flattenedCustomFieldsPersons.find((e) => e.name === 'structureMedical');
const healthInsurances = flattenedCustomFieldsPersons.find((e) => e.name === 'healthInsurances');
if (structureMedical || healthInsurances) {
const bodySocial = {
...person,
structureMedical: structureMedical ? body.structureMedical : undefined,
healthInsurances: healthInsurances ? body.healthInsurances : undefined,
};

const historyEntry = {
date: new Date(),
user: user._id,
data: {},
};
for (const key in bodySocial) {
if (!allowedFieldsInHistory.includes(key)) continue;
if (bodySocial[key] !== person[key]) historyEntry.data[key] = { oldValue: person[key], newValue: bodySocial[key] };
if (!!Object.keys(historyEntry.data).length) bodySocial.history = [...cleanHistory(person.history || []), historyEntry];
}
if (!!Object.keys(historyEntry.data).length) bodySocial.history = [...(person.history || []), historyEntry];

const personResponse = await API.put({
path: `/person/${person._id}`,
body: preparePersonForEncryption({
...person,
structureMedical: structureMedical ? body.structureMedical : undefined,
healthInsurances: healthInsurances ? body.healthInsurances : undefined,
}),
body: preparePersonForEncryption(bodySocial),
});
if (personResponse.ok) {
const newPerson = personResponse.decryptedData;
Expand Down
47 changes: 36 additions & 11 deletions dashboard/src/scenes/person/components/PersonHistory.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useMemo } from 'react';
import { useRecoilValue } from 'recoil';
import UserName from '../../../components/UserName';
import { teamsState } from '../../../recoil/auth';
import { teamsState, userState } from '../../../recoil/auth';
import { personFieldsIncludingCustomFieldsSelector } from '../../../recoil/persons';
import { formatDateWithFullMonth, dayjsInstance } from '../../../services/date';
import { customFieldsMedicalFileSelector } from '../../../recoil/medicalFiles';

// FIX: there was a bug in history at some point, where the whole person was saved in the history
// this function removes those entries
Expand All @@ -15,9 +16,20 @@ export const cleanHistory = (history = []) => {
};

export default function PersonHistory({ person }) {
const personFieldsIncludingCustomFields = useRecoilValue(personFieldsIncludingCustomFieldsSelector);
const teams = useRecoilValue(teamsState);
const history = useMemo(() => cleanHistory(person.history || []).reverse(), [person.history]);
const personFieldsIncludingCustomFields = useRecoilValue(personFieldsIncludingCustomFieldsSelector);
const customFieldsMedicalFile = useRecoilValue(customFieldsMedicalFileSelector);
const allPossibleFields = [
...personFieldsIncludingCustomFields.map((f) => ({ ...f, isMedicalFile: false })),
...customFieldsMedicalFile.map((f) => ({ ...f, isMedicalFile: true })),
];
const user = useRecoilValue(userState);
const history = useMemo(() => {
const personHistory = cleanHistory(person.history || []);
if (!user.healthcareProfessional) return personHistory.reverse();
const medicalFileHistory = person.medicalFile?.history || [];
return [...personHistory, ...medicalFileHistory].sort((a, b) => new Date(b.date) - new Date(a.date));
}, [person.history, person.medicalFile?.history, user.healthcareProfessional]);

return (
<div>
Expand Down Expand Up @@ -52,7 +64,7 @@ export default function PersonHistory({ person }) {
</td>
<td className="tw-max-w-prose">
{Object.entries(h.data).map(([key, value]) => {
const personField = personFieldsIncludingCustomFields.find((f) => f.name === key);
const personField = allPossibleFields.find((f) => f.name === key);
if (key === 'merge') {
return (
<p className="tw-flex tw-flex-col" key={key}>
Expand All @@ -69,9 +81,13 @@ export default function PersonHistory({ person }) {
return (
<p className="tw-flex tw-flex-col" key={key}>
<span>{personField?.label} : </span>
<code>"{(value.oldValue || []).map((teamId) => teams.find((t) => t._id === teamId)?.name).join(', ')}"</code>
<code className="tw-text-main">
"{(value.oldValue || []).map((teamId) => teams.find((t) => t._id === teamId)?.name).join(', ')}"
</code>
<span></span>
<code>"{(value.newValue || []).map((teamId) => teams.find((t) => t._id === teamId)?.name).join(', ')}"</code>
<code className="tw-text-main">
"{(value.newValue || []).map((teamId) => teams.find((t) => t._id === teamId)?.name).join(', ')}"
</code>
</p>
);
}
Expand All @@ -80,22 +96,24 @@ export default function PersonHistory({ person }) {
return (
<p className="tw-flex tw-flex-col" key={key}>
<span>{personField?.label}: </span>
<code>{value.newValue.join(', ')}</code>
<code className="tw-text-main">{value.newValue.join(', ')}</code>
</p>
);
}
if (key === 'outOfActiveList') {
return (
<p className="tw-flex tw-flex-col" key={key}>
<span>{value.newValue === true ? 'Sortie de file active' : 'Réintégration dans la file active'}</span>
<span className="tw-text-main">
{value.newValue === true ? 'Sortie de file active' : 'Réintégration dans la file active'}
</span>
</p>
);
}
if (key === 'outOfActiveListDate') {
if (!value.newValue) return null;
return (
<p className="tw-flex tw-flex-col" key={key}>
<span>{formatDateWithFullMonth(value.newValue)}</span>
<span className="tw-text-main">{formatDateWithFullMonth(value.newValue)}</span>
</p>
);
}
Expand All @@ -106,8 +124,15 @@ export default function PersonHistory({ person }) {
data-test-id={`${personField?.label || 'Champs personnalisé supprimé'}: ${JSON.stringify(
value.oldValue || ''
)}${JSON.stringify(value.newValue)}`}>
{personField?.label || 'Champs personnalisé supprimé'} : <br />
<code>{JSON.stringify(value.oldValue || '')}</code><code>{JSON.stringify(value.newValue)}</code>
<span className="tw-inline-flex tw-w-full tw-items-center tw-justify-between">
{personField?.label || 'Champs personnalisé supprimé'} :
{personField.isMedicalFile && <i className="tw-text-xs"> Dossier médical</i>}
</span>
<br />
<code className={personField.isMedicalFile ? 'tw-text-blue-900' : 'tw-text-main'}>
{JSON.stringify(value.oldValue || '')}
</code>{' '}
<code className={personField.isMedicalFile ? 'tw-text-blue-900' : 'tw-text-main'}>{JSON.stringify(value.newValue)}</code>
</p>
);
})}
Expand Down
3 changes: 2 additions & 1 deletion e2e/persons_person-full-test-migrated-from-jest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ test("test", async ({ page }) => {

await page.getByRole("button", { name: "Historique" }).click();
await page.locator('[data-test-id="Autres pseudos\\: \\"\\" ➔ \\"test pseudo\\""]').click();

await page.locator('[data-test-id="Genre\\: \\"Femme\\" ➔ \\"Homme\\""]').click();
await page.locator('[data-test-id="Numéro de sécurité sociale\\: \\"\\" ➔ \\"082\\""]').click();
await page.getByRole("link", { name: "Personnes suivies" }).click();
await expect(page).toHaveURL("http://localhost:8090/person");
await page.getByText(personName).click();
Expand Down

0 comments on commit be6e670

Please sign in to comment.