|
| 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 | +export class convertUsersOrgaCguData extends Script { |
| 13 | + constructor() { |
| 14 | + super({ |
| 15 | + description: 'convert users orga cgu data to legal-document-versions-user-acceptances', |
| 16 | + permanent: false, |
| 17 | + options: {}, |
| 18 | + }); |
| 19 | + } |
| 20 | + |
| 21 | + async handle({ logger }) { |
| 22 | + const tosOrgaLegalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ |
| 23 | + type: TOS, |
| 24 | + service: PIX_ORGA, |
| 25 | + }); |
| 26 | + |
| 27 | + if (!tosOrgaLegalDocument) { |
| 28 | + logger.info(`No legal document found for type:${TOS}, service:${PIX_ORGA}`); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const orgaCguAcceptedUsers = await this.#findOrgaCguAcceptedUsers(); |
| 33 | + |
| 34 | + logger.info(`User acceptances to be inserted count: ${orgaCguAcceptedUsers.length}`); |
| 35 | + |
| 36 | + const totalUsers = orgaCguAcceptedUsers.length; |
| 37 | + let processedCount = 0; |
| 38 | + |
| 39 | + for (const user of orgaCguAcceptedUsers) { |
| 40 | + await this.#createLegalDocumentUserAcceptance({ |
| 41 | + userId: user.id, |
| 42 | + legalDocumentVersionId: tosOrgaLegalDocument.id, |
| 43 | + acceptedAt: user.lastPixOrgaTermsOfServiceValidatedAt, |
| 44 | + }); |
| 45 | + |
| 46 | + processedCount++; |
| 47 | + const progressPercentage = ((processedCount / totalUsers) * 100).toFixed(2); |
| 48 | + logger.info(`Progress: ${progressPercentage}% (${processedCount}/${totalUsers} users processed)`); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + async #findOrgaCguAcceptedUsers() { |
| 53 | + const knexConnection = DomainTransaction.getConnection(); |
| 54 | + return knexConnection('users').select('*').where('pixOrgaTermsOfServiceAccepted', true); |
| 55 | + } |
| 56 | + |
| 57 | + async #createLegalDocumentUserAcceptance({ userId, legalDocumentVersionId, acceptedAt }) { |
| 58 | + const knexConnection = DomainTransaction.getConnection(); |
| 59 | + await knexConnection('legal-document-version-user-acceptances').insert({ |
| 60 | + userId, |
| 61 | + legalDocumentVersionId, |
| 62 | + acceptedAt, |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +await ScriptRunner.execute(import.meta.url, convertUsersOrgaCguData); |
0 commit comments