Skip to content

Commit

Permalink
fix: clean observations (#1824)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudambro authored Jan 10, 2024
1 parent 2d0a7aa commit 2e565ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions api/src/controllers/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ router.put(
encryptedEntityKey: z.string(),
})
).parse(req.body.encryptedObservations);
z.array(z.string().regex(looseUuidRegex)).parse(req.body.observationIdsToDelete);
} catch (e) {
const error = new Error(`Invalid request in ${req.params.migrationName}: ${e}`);
error.status = 400;
throw error;
}
for (const _id of req.body.observationIdsToDelete) {
await TerritoryObservation.destroy({ where: { _id, organisation: req.user.organisation }, transaction: tx });
}
for (const { _id, encrypted, encryptedEntityKey } of req.body.encryptedObservations) {
await TerritoryObservation.update({ encrypted, encryptedEntityKey }, { where: { _id }, transaction: tx, paranoid: false });
}
Expand Down
18 changes: 11 additions & 7 deletions dashboard/src/components/DataMigrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,25 @@ export default function useDataMigrator() {
query: { organisation: organisationId, after: 0, withDeleted: false },
}).then((res) => res.decryptedData || []);

const newObservations = observationsRes.map((e) => {
const observedAt = !isNaN(Number(e.observedAt)) // i.e. is timestamp
? dayjsInstance(Number(e.observedAt)).toISOString()
: dayjsInstance(e.observedAt ?? e.createdAt).toISOString();
const newObservations = observationsRes.map((obs) => {
const observedAt = !isNaN(Number(obs.observedAt)) // i.e. is timestamp
? dayjsInstance(Number(obs.observedAt)).toISOString()
: dayjsInstance(obs.observedAt ?? obs.createdAt).toISOString();
return {
...e,
...obs,
user: obs.user ?? user._id, // in case of old observations missing user
observedAt,
};
});

const encryptedObservations = await Promise.all(newObservations.map(prepareObsForEncryption(customFieldsObs)).map(encryptItem));
const observationIdsToDelete = newObservations.filter((obs) => !obs.territory || !obs.team).map((obs) => obs._id);
const observationsWithFullData = newObservations.filter((obs) => !!obs.territory && !!obs.team);

const encryptedObservations = await Promise.all(observationsWithFullData.map(prepareObsForEncryption(customFieldsObs)).map(encryptItem));

const response = await API.put({
path: `/migration/reformat-observedAt-observations`,
body: { encryptedObservations },
body: { encryptedObservations, observationIdsToDelete },
query: { migrationLastUpdateAt },
});
if (response.ok) {
Expand Down

0 comments on commit 2e565ba

Please sign in to comment.