From 7968b237ecbc2619459b337217193e0fb582c48c Mon Sep 17 00:00:00 2001 From: Benjamin Petetot Date: Fri, 8 Nov 2024 15:10:32 +0100 Subject: [PATCH] chore(api): delete backfill anonymized users script --- .../scripts/backfill-anonymized-users.js | 39 ------------- .../backfill-anonymized-users.script.test.js | 58 ------------------- 2 files changed, 97 deletions(-) delete mode 100644 api/src/identity-access-management/scripts/backfill-anonymized-users.js delete mode 100644 api/tests/identity-access-management/acceptance/scripts/backfill-anonymized-users.script.test.js diff --git a/api/src/identity-access-management/scripts/backfill-anonymized-users.js b/api/src/identity-access-management/scripts/backfill-anonymized-users.js deleted file mode 100644 index 5551724a2f2..00000000000 --- a/api/src/identity-access-management/scripts/backfill-anonymized-users.js +++ /dev/null @@ -1,39 +0,0 @@ -import * as url from 'node:url'; - -import { disconnect } from '../../../db/knex-database-connection.js'; -import { usecases } from '../../../lib/domain/usecases/index.js'; -import { withTransaction } from '../../shared/domain/DomainTransaction.js'; -import { learningContentCache } from '../../shared/infrastructure/caches/learning-content-cache.js'; -import { temporaryStorage } from '../../shared/infrastructure/temporary-storage/index.js'; -import { logger } from '../../shared/infrastructure/utils/logger.js'; -import { anonymizedUserRepository } from '../infrastructure/repositories/anonymized-user.repository.js'; - -const modulePath = url.fileURLToPath(import.meta.url); -const isLaunchedFromCommandLine = process.argv[1] === modulePath; - -if (isLaunchedFromCommandLine) { - try { - await backfillAnonymizedUsers(); - } catch (error) { - logger.error('\x1b[31mErreur : %s\x1b[0m', error.message); - process.exitCode = 1; - } finally { - await disconnect(); - await learningContentCache.quit(); - await temporaryStorage.quit(); - } -} - -export async function backfillAnonymizedUsers() { - const anonymizedUserIds = await anonymizedUserRepository.findLegacyAnonymizedIds(); - - logger.info(`Total anonymized users to backfill: ${anonymizedUserIds.length}`); - - let current = 1; - for (const anonymizedUserId of anonymizedUserIds) { - logger.info(`Backfill anonymized user ${current++}/${anonymizedUserIds.length}: "${anonymizedUserId}"`); - await withTransaction(usecases.anonymizeUser)({ userId: anonymizedUserId, preventAuditLogging: true }); - } - - logger.info(`Anonymized users backfill finished.`); -} diff --git a/api/tests/identity-access-management/acceptance/scripts/backfill-anonymized-users.script.test.js b/api/tests/identity-access-management/acceptance/scripts/backfill-anonymized-users.script.test.js deleted file mode 100644 index ed2e090d4af..00000000000 --- a/api/tests/identity-access-management/acceptance/scripts/backfill-anonymized-users.script.test.js +++ /dev/null @@ -1,58 +0,0 @@ -import { backfillAnonymizedUsers } from '../../../../src/identity-access-management/scripts/backfill-anonymized-users.js'; -import { databaseBuilder, expect, knex } from '../../../test-helper.js'; - -describe('Acceptance | Identity Access Management | Scripts | backfill-anonymized-users', function () { - it('backfills the anonymized users', async function () { - // given - const admin = databaseBuilder.factory.buildUser.withRole(); - const legacyAnonymizedUser = databaseBuilder.factory.buildUser({ - firstName: 'Jane', - hasBeenAnonymised: true, - hasBeenAnonymisedBy: admin.id, - }); - const userNotAnonymized = databaseBuilder.factory.buildUser({ - firstName: 'Jack', - hasBeenAnonymised: false, - }); - databaseBuilder.factory.buildUser({ - lastName: '(anonymized)', - hasBeenAnonymised: true, - hasBeenAnonymisedBy: admin.id, - }); - await databaseBuilder.commit(); - - // when - await backfillAnonymizedUsers(); - - // then - const anonymizedUser = await knex('users').where({ id: legacyAnonymizedUser.id }).first(); - expect(anonymizedUser.firstName).to.be.equal('(anonymised)'); - expect(anonymizedUser.hasBeenAnonymised).to.be.true; - expect(anonymizedUser.hasBeenAnonymisedBy).to.be.equal(admin.id); - - const notAnonymizedUser = await knex('users').where({ id: userNotAnonymized.id }).first(); - expect(notAnonymizedUser.firstName).to.be.equal('Jack'); - expect(notAnonymizedUser.hasBeenAnonymised).to.be.false; - }); - - context('when the anonymized user has been anonymized without hasBeenAnonymisedBy set', function () { - it('backfills the anonymized user', async function () { - // given - const legacyAnonymizedUser = databaseBuilder.factory.buildUser({ - firstName: 'Jane', - hasBeenAnonymised: true, - hasBeenAnonymisedBy: null, - }); - await databaseBuilder.commit(); - - // when - await backfillAnonymizedUsers(); - - // then - const anonymizedUser = await knex('users').where({ id: legacyAnonymizedUser.id }).first(); - expect(anonymizedUser.firstName).to.be.equal('(anonymised)'); - expect(anonymizedUser.hasBeenAnonymised).to.be.true; - expect(anonymizedUser.hasBeenAnonymisedBy).to.be.null; - }); - }); -});