Skip to content

Commit

Permalink
feat(api): add script to convert users orga cgu accepted to legal doc…
Browse files Browse the repository at this point in the history
…ument user acceptances
  • Loading branch information
P-Jeremy committed Dec 12, 2024
1 parent 62bf10e commit 9700a13
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions api/scripts/legal-documents/convert-users-orga-cgu-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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;

const BATCH_SIZE = 1000;
const THROTTLE_DELAY = 100;

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 knexConnection = DomainTransaction.getConnection();

let hasMoreUsers = true;
let offset = 0;

while (hasMoreUsers) {
const batch = await this.#findOrgaCguAcceptedUsers({ knexConnection, offset });

if (batch.length === 0) {
hasMoreUsers = false;
break;
}

await this.#createNewLegalDocumentAcceptanceForUsersBatch({ batch, tosOrgaLegalDocument });

offset += batch.length;
await this.#throttle();
}

logger.info('Processing complete.');
}

async #createNewLegalDocumentAcceptanceForUsersBatch({ batch, tosOrgaLegalDocument }) {
await Promise.all(
batch.map((user) =>
this.#createLegalDocumentUserAcceptance({
userId: user.id,
legalDocumentVersionId: tosOrgaLegalDocument.id,
acceptedAt: user.lastPixOrgaTermsOfServiceValidatedAt,
}),
),
);
}

async #findOrgaCguAcceptedUsers({ knexConnection, offset }) {
return knexConnection('users')
.select('*')
.where('pixOrgaTermsOfServiceAccepted', true)
.limit(BATCH_SIZE)
.offset(offset);
}

async #createLegalDocumentUserAcceptance({ userId, legalDocumentVersionId, acceptedAt }) {
const knexConnection = DomainTransaction.getConnection();

await knexConnection('legal-document-version-user-acceptances').insert({
userId,
legalDocumentVersionId,
acceptedAt,
});
}

async #throttle() {
return new Promise((resolve) => setTimeout(resolve, THROTTLE_DELAY));
}
}

await ScriptRunner.execute(import.meta.url, ConvertUsersOrgaCguData);

0 comments on commit 9700a13

Please sign in to comment.