Skip to content

Commit

Permalink
migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Sep 8, 2023
1 parent 7f611e2 commit 5a8bdb5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
23 changes: 23 additions & 0 deletions api/src/controllers/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,29 @@ router.put(
}
}

if (req.params.migrationName === "integrate-comments-in-actions-history") {
try {
z.array(z.string().regex(looseUuidRegex)).parse(req.body.commentIdsToDelete);
z.array(
z.object({
_id: z.string().regex(looseUuidRegex),
encrypted: z.string(),
encryptedEntityKey: z.string(),
})
).parse(req.body.actionsToUpdate);
} catch (e) {
const error = new Error(`Invalid request in reports-from-real-date-to-date-id migration: ${e}`);
error.status = 400;
throw error;
}
for (const _id of req.body.commentIdsToDelete) {
await Comment.destroy({ where: { _id, organisation: req.user.organisation }, transaction: tx });
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.actionsToUpdate) {
await Action.update({ encrypted, encryptedEntityKey }, { where: { _id }, transaction: tx, paranoid: false });
}
}

organisation.set({
migrations: [...(organisation.migrations || []), req.params.migrationName],
migrating: false,
Expand Down
55 changes: 54 additions & 1 deletion dashboard/src/components/DataMigrator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import { prepareActionForEncryption } from '../recoil/actions';
import { mappedIdsToLabels, prepareActionForEncryption } from '../recoil/actions';
import { organisationState, userState } from '../recoil/auth';
import { usePreparePersonForEncryption } from '../recoil/persons';
import { prepareReportForEncryption } from '../recoil/reports';
Expand Down Expand Up @@ -403,6 +403,59 @@ export default function useDataMigrator() {
migrationLastUpdateAt = response.organisation.migrationLastUpdateAt;
}
}

if (!organisation.migrations?.includes('integrate-comments-in-actions-history')) {
setLoadingText(LOADING_TEXT);
const comments = await API.get({
path: '/comment',
query: { organisation: organisationId, after: 0, withDeleted: false },
}).then((res) => res.decryptedData || []);
const actions = await API.get({
path: '/action',
query: { organisation: organisationId, after: 0, withDeleted: false },
}).then((res) => res.decryptedData || []);

const actionsPerId = {};
for (const action of actions) {
actionsPerId[action._id] = action;
}
const actionsToUpdate = {};
const commentIdsToDelete = [];

for (const comment of comments) {
if (!comment.action) continue;
if (!comment.comment.includes("a changé le status de l'action: ")) continue;
const action = actionsToUpdate[comment.action] ?? actionsPerId[comment.action];
if (!action) {
commentIdsToDelete.push(comment._id);
continue;
}
if (!action.history) action.history = [];
const statusName = comment.comment.split("a changé le status de l'action: ")[1];
const statusId = mappedIdsToLabels.find((e) => e.name === statusName)?._id;
action.history.push({
user: comment.user,
date: comment.createdAt,
data: {
status: { oldValue: '', newValue: statusId },
},
});
actionsToUpdate[comment.action] = action;
commentIdsToDelete.push(comment._id);
}

const encryptedActionsToUpdate = await Promise.all(Object.values(actionsToUpdate).map(prepareActionForEncryption).map(encryptItem));

const response = await API.put({
path: `/migration/integrate-comments-in-actions-history`,
body: { commentIdsToDelete, actionsToUpdate: encryptedActionsToUpdate },
query: { migrationLastUpdateAt },
});
if (response.ok) {
setOrganisation(response.organisation);
migrationLastUpdateAt = response.organisation.migrationLastUpdateAt;
}
}
},
};
}

0 comments on commit 5a8bdb5

Please sign in to comment.