|
| 1 | +import 'dotenv/config'; |
| 2 | + |
| 3 | +import { DomainTransaction } from '../../lib/infrastructure/DomainTransaction.js'; |
| 4 | +import { LegalDocument } from '../../src/legal-documents/domain/models/LegalDocument.js'; |
| 5 | +import * as legalDocumentRepository from '../../src/legal-documents/infrastructure/repositories/legal-document.repository.js'; |
| 6 | +import { Script } from '../../src/shared/application/scripts/script.js'; |
| 7 | +import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js'; |
| 8 | + |
| 9 | +const { TOS } = LegalDocument.TYPES; |
| 10 | +const { PIX_ORGA } = LegalDocument.SERVICES; |
| 11 | + |
| 12 | +const BATCH_SIZE = 1000; |
| 13 | +const THROTTLE_DELAY = 100; |
| 14 | + |
| 15 | +export class ConvertUsersOrgaCguData extends Script { |
| 16 | + constructor() { |
| 17 | + super({ |
| 18 | + description: 'Convert users orga CGU data to legal-document-versions-user-acceptances', |
| 19 | + permanent: false, |
| 20 | + options: {}, |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + async handle({ logger }) { |
| 25 | + const tosOrgaLegalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ |
| 26 | + type: TOS, |
| 27 | + service: PIX_ORGA, |
| 28 | + }); |
| 29 | + |
| 30 | + if (!tosOrgaLegalDocument) { |
| 31 | + logger.info(`No legal document found for type:${TOS}, service:${PIX_ORGA}`); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const knexConnection = DomainTransaction.getConnection(); |
| 36 | + |
| 37 | + let hasMoreUsers = true; |
| 38 | + let offset = 0; |
| 39 | + |
| 40 | + while (hasMoreUsers) { |
| 41 | + const batch = await this.#findOrgaCguAcceptedUsers({ knexConnection, offset }); |
| 42 | + |
| 43 | + if (batch.length === 0) { |
| 44 | + hasMoreUsers = false; |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + await this.#createNewLegalDocumentAcceptanceForUsersBatch({ batch, tosOrgaLegalDocument }); |
| 49 | + |
| 50 | + offset += batch.length; |
| 51 | + await this.#throttle(); |
| 52 | + } |
| 53 | + |
| 54 | + logger.info('Processing complete.'); |
| 55 | + } |
| 56 | + |
| 57 | + async #createNewLegalDocumentAcceptanceForUsersBatch({ batch, tosOrgaLegalDocument }) { |
| 58 | + await Promise.all( |
| 59 | + batch.map((user) => |
| 60 | + this.#createLegalDocumentUserAcceptance({ |
| 61 | + userId: user.id, |
| 62 | + legalDocumentVersionId: tosOrgaLegalDocument.id, |
| 63 | + acceptedAt: user.lastPixOrgaTermsOfServiceValidatedAt, |
| 64 | + }), |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + async #findOrgaCguAcceptedUsers({ knexConnection, offset }) { |
| 70 | + return knexConnection('users') |
| 71 | + .select('*') |
| 72 | + .where('pixOrgaTermsOfServiceAccepted', true) |
| 73 | + .limit(BATCH_SIZE) |
| 74 | + .offset(offset); |
| 75 | + } |
| 76 | + |
| 77 | + async #createLegalDocumentUserAcceptance({ userId, legalDocumentVersionId, acceptedAt }) { |
| 78 | + const knexConnection = DomainTransaction.getConnection(); |
| 79 | + |
| 80 | + await knexConnection('legal-document-version-user-acceptances').insert({ |
| 81 | + userId, |
| 82 | + legalDocumentVersionId, |
| 83 | + acceptedAt, |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + async #throttle() { |
| 88 | + return new Promise((resolve) => setTimeout(resolve, THROTTLE_DELAY)); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +await ScriptRunner.execute(import.meta.url, ConvertUsersOrgaCguData); |
0 commit comments