-
Notifications
You must be signed in to change notification settings - Fork 0
feat/HIT26_GDPR-anonymity-of-beneficiaries #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: hit-oort
Are you sure you want to change the base?
Changes from all commits
1146746
a7c0ae4
8e7a8b2
b915416
b82e95b
d93ec24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { Record, User } from '@models'; | ||
| import { Types } from 'mongoose'; | ||
|
|
||
| /** Staff resource ID */ | ||
| const AID_RESOURCE_ID = new Types.ObjectId('64e6e0933c7bf3962bf4f04c'); | ||
| const FAMILY_RESOURCE_ID = new Types.ObjectId('64de75fd3fb2a11c988dddb2'); | ||
|
|
||
| /** Anonymizes the beneficiary data, if didn't log in for more than 18 months */ | ||
| export const anonymizeBeneficiaries = async () => { | ||
| // For all family records, check if | ||
| // in the last 18 months they received aid | ||
|
|
||
| // Get all the family records | ||
| const allFamilies = await Record.find({ | ||
| resource: FAMILY_RESOURCE_ID, | ||
| }); | ||
|
|
||
| // For each family record, check if exists | ||
| // an aid record in the last 18 months | ||
| for (const family of allFamilies) { | ||
| const aidGivenToFamily = await Record.exists({ | ||
| resource: AID_RESOURCE_ID, | ||
| createdAt: { | ||
| $gt: new Date(Date.now() - 18 * 30 * 24 * 60 * 60 * 1000), | ||
| }, // 18 months ago | ||
| 'data.owner_resource': family._id.toString(), | ||
| }); | ||
|
|
||
| // If no aid was given to the family in the last 18 months | ||
| if (!aidGivenToFamily) { | ||
| // Find all members of the family | ||
| const members = await Record.find({ | ||
| _id: { $in: family?.data?.members }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would throw an error if
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would also be nice to check if the record is in the Person form, but no big deal |
||
| }); | ||
|
|
||
| // Anonymize all the members | ||
| members.forEach((member) => { | ||
| if (!member.data) { | ||
| return; | ||
| } | ||
| // Anonymize the member | ||
| member._createdBy = new User({ | ||
| name: 'ANONYMOUS', | ||
| username: `${member._id.toString()}@oort-anonymous.com`, | ||
| }); | ||
|
Comment on lines
+41
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this, the createdBy would be a staff member, and their anonymization is already handled in the other function |
||
|
|
||
| member.data = { | ||
| ...member.data, | ||
| location: 'ANONYMOUS', | ||
| surname: 'ANONYMOUS', | ||
| firstname: 'ANONYMOUS', | ||
| phone: 'ANONYMOUS', | ||
| nom_employes: 'ANONYMOUS', | ||
| gender: 'ANONYMOUS', | ||
| birthdate: 'ANONYMOUS', | ||
| prenom_employes: 'ANONYMOUS', | ||
| nom_prenom_employes: 'ANONYMOUS', | ||
| tel_staff: 'ANONYMOUS', | ||
| email_staff: 'ANONYMOUS', | ||
| birthdate_employes: 'ANONYMOUS', | ||
| file_gdpr_staff: [], | ||
| }; | ||
|
Comment on lines
+47
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When anonymizing the data, we don't need to remove fields that are not traceable back to the beneficiary. e.g: their location and gender. Also, make sure to only save valid data. For the birthdate for example, it should always be a valid date, check the other anonymization function to see how it should be dealt with. The email should also be a valid email, you can use the same logic of And finally, it looks like there are fields here that are not part of the form (and it's possibly missing some that should be there, but I didn't check) |
||
|
|
||
| member._lastUpdatedBy = new User({ | ||
| name: 'ANONYMOUS', | ||
| username: `${member._id.toString()}@oort-anonymous.com`, | ||
| }); | ||
|
Comment on lines
+64
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, can be removed. And actually, another change you should make is doing this but in the staff anonymization, so when anonymizing a staff, you have to check all records created or last updated by them and updated the _lastUpdatedby and the _createdBy. However, do not create a new user, as that creates a new id, and we do not want that. |
||
| }); | ||
|
|
||
| // Save all the records | ||
| await Record.bulkSave(members); | ||
| } | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { Record, User } from '@models'; | ||
| import { deleteFile } from '@utils/files'; | ||
| import { Types } from 'mongoose'; | ||
| import { posterizeAge } from './utils/posterizeAge'; | ||
| import { logger } from '@services/logger.service'; | ||
|
|
||
| /** Staff resource ID */ | ||
| const STAFF_RESOURCE_ID = new Types.ObjectId('649e9ec5eae9f89219921eff'); | ||
|
|
||
| /** Anonymizes the staff data, if didn't log in for more than 6 months */ | ||
| export const anonymizeStaff = async () => { | ||
| // Get all users with lastLogin 6 months ago | ||
| const usersToDelete = await User.find({ | ||
| $expr: { | ||
| $lt: [ | ||
| { | ||
| $ifNull: ['$lastLogin', '$modifiedAt'], | ||
| }, | ||
| new Date(Date.now() - 6 * 30 * 24 * 60 * 60 * 1000), // 6 months ago | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| // Find all the records of staff with the users found above | ||
| const usersStaffRecords = await Record.find({ | ||
| resource: STAFF_RESOURCE_ID, | ||
| user: { $in: usersToDelete.map((user) => user._id) }, | ||
| }); | ||
|
|
||
| // Hide the info on the user | ||
| usersToDelete.forEach((user) => { | ||
| user.username = `${user._id.toString()}@oort-anonymous.com`; | ||
| user.firstName = 'ANONYMOUS'; | ||
| user.lastName = 'ANONYMOUS'; | ||
| user.name = 'ANONYMOUS'; | ||
| user.roles = []; | ||
| user.oid = null; | ||
| }); | ||
|
|
||
| await User.bulkSave(usersToDelete); | ||
|
|
||
| // Hold all files that should be deleted in blob storage | ||
| const filesToDelete = []; | ||
|
|
||
| // Hide info on the staff record | ||
| usersStaffRecords.forEach((staffRecord) => { | ||
| if (!staffRecord.data) { | ||
| return; | ||
| } | ||
|
|
||
| // Add all files to delete | ||
| (staffRecord.data.file_gdpr_staff || []).forEach((file) => { | ||
| filesToDelete.push(file.content); | ||
| }); | ||
|
|
||
| staffRecord.data = { | ||
| ...staffRecord.data, | ||
| nom_employes: 'ANONYMOUS', | ||
| prenom_employes: 'ANONYMOUS', | ||
| tel_staff: 'ANONYMOUS', | ||
| email_staff: `${staffRecord.data.linked_user[0]}@oort-anonymous.com`, | ||
| file_gdpr_staff: [], | ||
| birthdate_employes: posterizeAge({ | ||
| birthdate: staffRecord.data.birthdate_employes, | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| // Delete all files | ||
| Promise.all(filesToDelete.map((file) => deleteFile('forms', file))).catch( | ||
| (err) => { | ||
| logger.error(`Error deleting files: ${err}`); | ||
| } | ||
| ); | ||
|
|
||
| // Save all the records | ||
| await Record.bulkSave(usersStaffRecords); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { CronJob } from 'cron'; | ||
| import { anonymizeStaff } from './anonymizeStaff'; | ||
| import { anonymizeBeneficiaries } from './anonymizeBeneficiaries'; | ||
| import { logger } from '@services/logger.service'; | ||
| import config from 'config'; | ||
|
|
||
| /** All available jobs */ | ||
| const JOBS: { | ||
| name: string; | ||
| description: string; | ||
| fn: () => Promise<void>; | ||
| // Schedule in cron format | ||
| schedule: string; | ||
| // Environments where the job should be started | ||
| envs: string[]; | ||
| }[] = [ | ||
| { | ||
| name: 'Anonymize staff', | ||
| description: "Anonymizes staff, if didn't log in for more than 6 months", | ||
| // Every week | ||
| schedule: '0 0 * * 0', | ||
| fn: anonymizeStaff, | ||
| envs: ['alimentaide'], | ||
| }, | ||
| { | ||
| name: 'Anonymize beneficiaries', | ||
| description: | ||
| 'Anonymizes all members of a family, if the family did not receive aid for the last 18 months', | ||
| // Every week | ||
| schedule: '0 0 * * 0', | ||
| fn: anonymizeBeneficiaries, | ||
| envs: ['alimentaide'], | ||
| }, | ||
| ]; | ||
|
|
||
| /** Starts all the jobs */ | ||
| export const startJobs = () => { | ||
| const isDev = config.util.getEnv('NODE_ENV') === 'development'; | ||
| const env = config.util.getEnv('NODE_CONFIG_ENV'); | ||
|
|
||
| // Start all the jobs | ||
| JOBS.forEach((job) => { | ||
| // Check if the job should be started | ||
| if (!isDev && !job.envs.includes(env)) { | ||
| return; | ||
| } | ||
|
|
||
| // Start the job | ||
| new CronJob( | ||
| job.schedule, | ||
| async () => { | ||
| try { | ||
| await job.fn(); | ||
| } catch (error) { | ||
| logger.error(error); | ||
| } | ||
| }, | ||
| null, | ||
| true | ||
| ).start(); | ||
|
|
||
| logger.info(`🤖 Job "${job.name}" started`); | ||
| }); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /** Defined age groups */ | ||
| const AGE_GROUPS = [ | ||
| [0, 3], | ||
| [4, 14], | ||
| [15, 17], | ||
| [18, 25], | ||
| [26, 64], | ||
| [65, 79], | ||
| [80, null], | ||
| ]; | ||
|
|
||
| /** | ||
| * Get the age of a person | ||
| * | ||
| * @param birthdate Date of birth of the person | ||
| * @returns The age of the person | ||
| */ | ||
| const getAge = (birthdate: Date) => { | ||
| const today = new Date(); | ||
| const birthDate = new Date(birthdate); | ||
| let age = today.getFullYear() - birthDate.getFullYear(); | ||
| const m = today.getMonth() - birthDate.getMonth(); | ||
| if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { | ||
| age--; | ||
| } | ||
| return age; | ||
| }; | ||
|
|
||
| /** | ||
| * Gets the age group of a person | ||
| * | ||
| * @param param Params object | ||
| * @param param.age Age of the person | ||
| * @param param.birthdate Birthdate of the person | ||
| * @returns The age group of the person | ||
| */ | ||
| export const posterizeAge = ({ | ||
| age, | ||
| birthdate, | ||
| }: { | ||
| age?: number; | ||
| birthdate?: string; | ||
| }): number | string | null => { | ||
| if (age && typeof age === 'number') { | ||
| // Find the age group | ||
| const ageGroup = AGE_GROUPS.find( | ||
| (group) => age >= group[0] && (!group[1] || age <= group[1]) | ||
| ); | ||
|
|
||
| // Get random age in the age group | ||
| const min = ageGroup[0]; | ||
| const max = ageGroup[1] || 100; | ||
| return Math.floor(Math.random() * (max - min + 1)) + min; | ||
| } else if (birthdate && !isNaN(new Date(birthdate).getTime())) { | ||
| const newAge = posterizeAge({ age: getAge(new Date(birthdate)) }) as number; | ||
|
|
||
| // Random month and day | ||
| return `${new Date().getFullYear() - newAge}-${ | ||
| Math.floor(Math.random() * 11) + 1 | ||
| }-${Math.floor(Math.random() * 27) + 1}`; | ||
| } | ||
| return null; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing jsdoc, don't forget to always run npm run lint before marking the PR as ready for review