Skip to content

Commit

Permalink
fix(dashboard): migration pour cleaner les dossiers médicaux dupliqués
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Oct 6, 2023
1 parent 23a0566 commit 9768bdd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
25 changes: 24 additions & 1 deletion api/src/controllers/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { looseUuidRegex, dateRegex } = require("../utils");
const { capture } = require("../sentry");
const validateUser = require("../middleware/validateUser");
const { serializeOrganisation } = require("../utils/data-serializer");
const { Organisation, Person, Action, Comment, Report, Team, Service, sequelize, Group } = require("../db/sequelize");
const { Organisation, Person, Action, Comment, Report, Team, Service, sequelize, Group, MedicalFile } = require("../db/sequelize");

router.put(
"/:migrationName",
Expand Down Expand Up @@ -88,6 +88,29 @@ router.put(
}
}

if (req.params.migrationName === "clean-duplicated-medical-files") {
try {
z.array(z.string().regex(looseUuidRegex)).parse(req.body.medicalFileIdsToDelete);
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.medicalFilesToUpdate);
} catch (e) {
const error = new Error(`Invalid request in clean-duplicated-medical-files migration: ${e}`);
error.status = 400;
throw error;
}
for (const _id of req.body.medicalFileIdsToDelete) {
await MedicalFile.destroy({ where: { _id, organisation: req.user.organisation }, transaction: tx });
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.medicalFilesToUpdate) {
await MedicalFile.update({ encrypted, encryptedEntityKey }, { where: { _id }, transaction: tx, paranoid: false });
}
}

organisation.set({
migrations: [...(organisation.migrations || []), req.params.migrationName],
migrating: false,
Expand Down
64 changes: 64 additions & 0 deletions dashboard/src/components/DataMigrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { looseUuidRegex } from '../utils';
import { prepareCommentForEncryption } from '../recoil/comments';
import { prepareGroupForEncryption } from '../recoil/groups';
import { capture } from '../services/sentry';
import { defaultMedicalFileCustomFields, prepareMedicalFileForEncryption } from '../recoil/medicalFiles';

const LOADING_TEXT = 'Mise à jour des données de votre organisation…';

Expand Down Expand Up @@ -111,6 +112,69 @@ export default function useDataMigrator() {
migrationLastUpdateAt = response.organisation.migrationLastUpdateAt;
}
}

if (['admin', 'normal'].includes(user.role) && !organisation.migrations?.includes('clean-duplicated-medical-files')) {
setLoadingText(LOADING_TEXT);
const customFieldsMedicalFile = Array.isArray(organisation.customFieldsMedicalFile)
? organisation.customFieldsMedicalFile
: defaultMedicalFileCustomFields;
const medicalFiles = await API.get({
path: '/medical-file',
query: { organisation: organisationId, after: 0, withDeleted: false },
}).then((res) => res.decryptedData || []);

const medicalFilesPerPersonId = {};
const medicalFilesToUpdate = {};
const medicalFileIdsToDelete = [];
for (const newMedicalFile of medicalFiles) {
const personId = newMedicalFile.person;
if (medicalFilesPerPersonId[personId]) {
const existingMedicalFile = medicalFilesPerPersonId[personId];
const nextDocuments = {};
const nextComments = {};
for (const document of newMedicalFile.documents) {
nextDocuments[document._id] = document;
}
for (const document of existingMedicalFile.documents) {
nextDocuments[document._id] = document;
}
for (const comment of newMedicalFile.comments) {
nextComments[comment._id] = comment;
}
for (const comment of existingMedicalFile.comments) {
nextComments[comment._id] = comment;
}
medicalFilesPerPersonId[personId] = {
...newMedicalFile,
...existingMedicalFile,
documents: Object.values(nextDocuments),
comments: Object.values(nextComments),
};
medicalFileIdsToDelete.push(newMedicalFile._id);
medicalFilesToUpdate[personId] = medicalFilesPerPersonId[personId];
} else {
medicalFilesPerPersonId[personId] = newMedicalFile;
}
}

const encryptedMedicalFilesToUpdate = await Promise.all(
Object.values(medicalFilesToUpdate)
.map((medicalFile) => prepareMedicalFileForEncryption(customFieldsMedicalFile)(medicalFile))
.map(encryptItem)
);

console.log('encryptedMedicalFilesToUpdate', encryptedMedicalFilesToUpdate);
console.log('medicalFileIdsToDelete', medicalFileIdsToDelete);
const response = await API.put({
path: `/migration/clean-duplicated-medical-files`,
body: { medicalFileIdsToDelete, medicalFilesToUpdate: encryptedMedicalFilesToUpdate },
query: { migrationLastUpdateAt },
});
if (response.ok) {
setOrganisation(response.organisation);
migrationLastUpdateAt = response.organisation.migrationLastUpdateAt;
}
}
},
};
}
2 changes: 1 addition & 1 deletion dashboard/src/recoil/medicalFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const prepareMedicalFileForEncryption =
};
};

const defaultMedicalFileCustomFields: CustomField[] = [
export const defaultMedicalFileCustomFields: CustomField[] = [
{
name: 'numeroSecuriteSociale',
label: 'Numéro de sécurité sociale',
Expand Down

0 comments on commit 9768bdd

Please sign in to comment.