-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add script to convert ursers orga cgu accepted to legal do…
…cument user acceptances
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
api/scripts/legal-documents/convert-users-orga-cgu-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'dotenv/config'; | ||
|
||
import { DomainTransaction } from '../../lib/infrastructure/DomainTransaction.js'; | ||
import { LegalDocument } from '../../src/legal-documents/domain/models/LegalDocument.js'; | ||
import * as legalDocumentRepository from '../../src/legal-documents/infrastructure/repositories/legal-document.repository.js'; | ||
import { Script } from '../../src/shared/application/scripts/script.js'; | ||
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js'; | ||
|
||
const { TOS } = LegalDocument.TYPES; | ||
const { PIX_ORGA } = LegalDocument.SERVICES; | ||
|
||
export class convertUsersOrgaCguData extends Script { | ||
constructor() { | ||
super({ | ||
description: 'convert users orga cgu data to legal-document-versions-user-acceptances', | ||
permanent: false, | ||
options: {}, | ||
}); | ||
} | ||
|
||
async handle({ logger }) { | ||
const tosOrgaLegalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ | ||
type: TOS, | ||
service: PIX_ORGA, | ||
}); | ||
|
||
if (!tosOrgaLegalDocument) { | ||
logger.info(`No legal document found for type:${TOS}, service:${PIX_ORGA}`); | ||
return; | ||
} | ||
|
||
const orgaCguAcceptedUsers = await this.#findOrgaCguAcceptedUsers(); | ||
|
||
logger.info(`User acceptances to be inserted count: ${orgaCguAcceptedUsers.length}`); | ||
|
||
const totalUsers = orgaCguAcceptedUsers.length; | ||
let processedCount = 0; | ||
|
||
for (const user of orgaCguAcceptedUsers) { | ||
await this.#createLegalDocumentUserAcceptance({ | ||
userId: user.id, | ||
legalDocumentVersionId: tosOrgaLegalDocument.id, | ||
acceptedAt: user.lastPixOrgaTermsOfServiceValidatedAt, | ||
}); | ||
|
||
processedCount++; | ||
const progressPercentage = ((processedCount / totalUsers) * 100).toFixed(2); | ||
logger.info(`Progress: ${progressPercentage}% (${processedCount}/${totalUsers} users processed)`); | ||
} | ||
} | ||
|
||
async #findOrgaCguAcceptedUsers() { | ||
const knexConnection = DomainTransaction.getConnection(); | ||
return knexConnection('users').select('*').where('pixOrgaTermsOfServiceAccepted', true); | ||
} | ||
|
||
async #createLegalDocumentUserAcceptance({ userId, legalDocumentVersionId, acceptedAt }) { | ||
const knexConnection = DomainTransaction.getConnection(); | ||
await knexConnection('legal-document-version-user-acceptances').insert({ | ||
userId, | ||
legalDocumentVersionId, | ||
acceptedAt, | ||
}); | ||
} | ||
} | ||
|
||
await ScriptRunner.execute(import.meta.url, convertUsersOrgaCguData); |