Skip to content

Commit

Permalink
fix: observations metadata (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudambro authored Jan 15, 2024
1 parent 91e35ce commit 3952449
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion api/src/controllers/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ router.put(
}
// End of example of migration.
*/
if (req.params.migrationName === "reformat-observedAt-observations") {
if (req.params.migrationName === "reformat-observedAt-observations-fixed") {
try {
z.array(
z.object({
Expand Down
77 changes: 42 additions & 35 deletions dashboard/src/components/DataMigrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { prepareMedicalFileForEncryption } from '../recoil/medicalFiles';
import { preparePassageForEncryption } from '../recoil/passages';
import { prepareRencontreForEncryption } from '../recoil/rencontres';
import { prepareTerritoryForEncryption } from '../recoil/territory';
import { customFieldsObsSelector, prepareObsForEncryption } from '../recoil/territoryObservations';
import { customFieldsObsSelector, defaultCustomFields, prepareObsForEncryption } from '../recoil/territoryObservations';
import { preparePlaceForEncryption } from '../recoil/places';
import { prepareRelPersonPlaceForEncryption } from '../recoil/relPersonPlace';

Expand All @@ -29,7 +29,6 @@ export default function useDataMigrator() {
const setLoadingText = useSetRecoilState(loadingTextState);
const user = useRecoilValue(userState);
const setOrganisation = useSetRecoilState(organisationState);
const customFieldsObs = useRecoilValue(customFieldsObsSelector);

const preparePersonForEncryption = usePreparePersonForEncryption();

Expand Down Expand Up @@ -67,43 +66,51 @@ export default function useDataMigrator() {
}
// End of example of migration.
*/
// if (!organisation.migrations?.includes('reformat-observedAt-observations')) {
// // some observedAt are timestamp, some are date
// // it messes up the filtering by date in stats
// setLoadingText(LOADING_TEXT);
// const observationsRes = await API.get({
// path: '/territory-observation',
// query: { organisation: organisationId, after: 0, withDeleted: false },
// }).then((res) => res.decryptedData || []);
if (!organisation.migrations?.includes('reformat-observedAt-observations-fixed')) {
// some observedAt are timestamp, some are date
// it messes up the filtering by date in stats
setLoadingText(LOADING_TEXT);
const observationsRes = await API.get({
path: '/territory-observation',
query: { organisation: organisationId, after: 0, withDeleted: false },
}).then((res) => res.decryptedData || []);

const observationIdsToDelete = {}; // we create an object for the loop line 87 to be fast enough
for (const observation of observationsRes) {
if (!observation.territory || !observation.team) {
observationIdsToDelete[observation._id] = true;
}
}

// 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 {
// ...obs,
// user: obs.user ?? user._id, // in case of old observations missing user
// observedAt,
// };
// });
const observationsWithFullData = observationsRes
.filter((obs) => !observationIdsToDelete[obs._id])
.map((obs) => {
const observedAt = !isNaN(Number(obs.observedAt)) // i.e. is timestamp
? dayjsInstance(Number(obs.observedAt)).toISOString()
: dayjsInstance(obs.observedAt ?? obs.createdAt).toISOString();
return {
...obs,
user: typeof obs.user === 'string' ? obs.user : user._id, // sometimes user is an empty {} instead of a uuid
observedAt,
};
});

// const observationIdsToDelete = newObservations.filter((obs) => !obs.territory || !obs.team).map((obs) => obs._id);
// const observationsWithFullData = newObservations.filter((obs) => !!obs.territory && !!obs.team);
const customFieldsObs = Array.isArray(organisation.customFieldsObs) ? organisation.customFieldsObs : defaultCustomFields;

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

// const response = await API.put({
// path: `/migration/reformat-observedAt-observations`,
// body: { encryptedObservations, observationIdsToDelete },
// query: { migrationLastUpdateAt },
// });
// if (response.ok) {
// setOrganisation(response.organisation);
// migrationLastUpdateAt = response.organisation.migrationLastUpdateAt;
// } else {
// return false;
// }
// }
const response = await API.put({
path: `/migration/reformat-observedAt-observations-fixed`,
body: { encryptedObservations, observationIdsToDelete: Object.keys(observationIdsToDelete) },
query: { migrationLastUpdateAt },
});
if (response.ok) {
setOrganisation(response.organisation);
migrationLastUpdateAt = response.organisation.migrationLastUpdateAt;
} else {
return false;
}
}
return true;
},
};
Expand Down
3 changes: 1 addition & 2 deletions dashboard/src/recoil/territoryObservations.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ export const customFieldsObsSelector = selector({
key: 'customFieldsObsSelector',
get: ({ get }) => {
const organisation = get(organisationState);
if (!organisation) return defaultCustomFields;
if (Array.isArray(organisation.customFieldsObs)) return organisation.customFieldsObs;
return defaultCustomFields;
},
});

const defaultCustomFields = [
export const defaultCustomFields = [
{
name: 'personsMale',
label: 'Nombre de personnes non connues hommes rencontrées',
Expand Down
5 changes: 3 additions & 2 deletions dashboard/src/scenes/auth/signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const SignIn = () => {
window.localStorage.setItem('mano-organisationId', organisation._id);
setOrganisation(organisation);
setUserName(user.name);
setUser(user);
if (!!organisation.encryptionEnabled && !['superadmin'].includes(user.role)) setShowEncryption(true);
}

Expand Down Expand Up @@ -108,19 +109,19 @@ const SignIn = () => {
if (organisation._id !== window.localStorage.getItem('mano-organisationId')) {
await resetCache();
}
setOrganisation(organisation);
setUser(user);
if (!!organisation.encryptionEnabled && !showEncryption && !['superadmin'].includes(user.role)) {
setShowEncryption(true);
return setIsSubmitting(false);
}
if (token) setToken(token);
setSessionInitialTimestamp(Date.now());
window.localStorage.setItem('mano-organisationId', organisation._id);
setOrganisation(organisation);
if (!['superadmin'].includes(user.role) && !!signinForm.orgEncryptionKey) {
const encryptionIsValid = await setOrgEncryptionKey(signinForm.orgEncryptionKey.trim(), organisation);
if (!encryptionIsValid) return setIsSubmitting(false);
}
setUser(user);
// now login !
// superadmin
if (['superadmin'].includes(user.role)) {
Expand Down

0 comments on commit 3952449

Please sign in to comment.