|
| 1 | +import { LegalDocumentService } from '../../../../src/legal-documents/domain/models/LegalDocumentService.js'; |
| 2 | +import { LegalDocumentType } from '../../../../src/legal-documents/domain/models/LegalDocumentType.js'; |
| 3 | +import { ConvertUsersOrgaCguData } from '../../../../src/legal-documents/scripts/convert-users-orga-cgu-data.js'; |
| 4 | +import { databaseBuilder, expect, knex, sinon } from '../../../test-helper.js'; |
| 5 | + |
| 6 | +const { TOS } = LegalDocumentType.VALUES; |
| 7 | +const { PIX_ORGA } = LegalDocumentService.VALUES; |
| 8 | + |
| 9 | +describe('Integration | Legal documents | Scripts | convert-users-orga-cgu-data', function () { |
| 10 | + describe('Options', function () { |
| 11 | + it('has the correct options', function () { |
| 12 | + // given |
| 13 | + const script = new ConvertUsersOrgaCguData(); |
| 14 | + |
| 15 | + // when |
| 16 | + const { options, permanent } = script.metaInfo; |
| 17 | + |
| 18 | + // then |
| 19 | + expect(permanent).to.be.false; |
| 20 | + expect(options).to.deep.include({ |
| 21 | + dryRun: { |
| 22 | + type: 'boolean', |
| 23 | + describe: 'Executes the script in dry run mode', |
| 24 | + default: false, |
| 25 | + }, |
| 26 | + batchSize: { |
| 27 | + type: 'number', |
| 28 | + describe: 'Size of the batch to process', |
| 29 | + default: 1000, |
| 30 | + }, |
| 31 | + throttleDelay: { |
| 32 | + type: 'number', |
| 33 | + describe: 'Delay between batches in milliseconds', |
| 34 | + default: 300, |
| 35 | + }, |
| 36 | + }); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('#handle', function () { |
| 41 | + let clock; |
| 42 | + let legalDocumentVersion; |
| 43 | + let logger; |
| 44 | + |
| 45 | + beforeEach(async function () { |
| 46 | + clock = sinon.useFakeTimers({ now: new Date('2024-01-01'), toFake: ['Date'] }); |
| 47 | + legalDocumentVersion = databaseBuilder.factory.buildLegalDocumentVersion({ type: TOS, service: PIX_ORGA }); |
| 48 | + logger = { info: sinon.stub() }; |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(async function () { |
| 52 | + clock.restore(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('converts Pix Orga user cgus to legal document user acceptances', async function () { |
| 56 | + // given |
| 57 | + const userAcceptedCgu = databaseBuilder.factory.buildUser({ |
| 58 | + pixOrgaTermsOfServiceAccepted: true, |
| 59 | + lastPixOrgaTermsOfServiceValidatedAt: new Date('2021-01-01'), |
| 60 | + }); |
| 61 | + const userAcceptedCguWithoutDate = databaseBuilder.factory.buildUser({ |
| 62 | + pixOrgaTermsOfServiceAccepted: true, |
| 63 | + lastPixOrgaTermsOfServiceValidatedAt: null, |
| 64 | + }); |
| 65 | + const userNotAcceptedCgu = databaseBuilder.factory.buildUser({ |
| 66 | + pixOrgaTermsOfServiceAccepted: false, |
| 67 | + lastPixOrgaTermsOfServiceValidatedAt: null, |
| 68 | + }); |
| 69 | + await databaseBuilder.commit(); |
| 70 | + |
| 71 | + // when |
| 72 | + const script = new ConvertUsersOrgaCguData(); |
| 73 | + const options = { dryRun: false, batchSize: 1, throttleDelay: 0 }; |
| 74 | + await script.handle({ options, logger }); |
| 75 | + |
| 76 | + // then |
| 77 | + expect(logger.info).to.have.been.calledWith('Batch #1'); |
| 78 | + expect(logger.info).to.have.been.calledWith('Batch #2'); |
| 79 | + expect(logger.info).to.have.been.calledWith('Total users migrated: 2'); |
| 80 | + |
| 81 | + const userAcceptances = await knex('legal-document-version-user-acceptances').where({ |
| 82 | + legalDocumentVersionId: legalDocumentVersion.id, |
| 83 | + }); |
| 84 | + |
| 85 | + const acceptance1 = userAcceptances.find((user) => user.userId === userAcceptedCgu.id); |
| 86 | + expect(acceptance1.acceptedAt).to.deep.equal(new Date('2021-01-01')); |
| 87 | + |
| 88 | + const acceptance2 = userAcceptances.find((user) => user.userId === userAcceptedCguWithoutDate.id); |
| 89 | + expect(acceptance2.acceptedAt).to.deep.equal(new Date('2024-01-01')); |
| 90 | + |
| 91 | + const acceptance3 = userAcceptances.find((user) => user.userId === userNotAcceptedCgu.id); |
| 92 | + expect(acceptance3).to.be.undefined; |
| 93 | + }); |
| 94 | + |
| 95 | + context('when a user cgu is already converted to legal document user acceptances', function () { |
| 96 | + it('does not change already converted user acceptances', async function () { |
| 97 | + // given |
| 98 | + const alreadyConvertedUser = databaseBuilder.factory.buildUser({ |
| 99 | + pixOrgaTermsOfServiceAccepted: true, |
| 100 | + lastPixOrgaTermsOfServiceValidatedAt: new Date('2021-01-01'), |
| 101 | + }); |
| 102 | + const newUserAcceptedCgu = databaseBuilder.factory.buildUser({ |
| 103 | + pixOrgaTermsOfServiceAccepted: true, |
| 104 | + lastPixOrgaTermsOfServiceValidatedAt: new Date('2021-01-01'), |
| 105 | + }); |
| 106 | + databaseBuilder.factory.buildLegalDocumentVersionUserAcceptance({ |
| 107 | + legalDocumentVersionId: legalDocumentVersion.id, |
| 108 | + userId: alreadyConvertedUser.id, |
| 109 | + acceptedAt: new Date('2020-01-01'), |
| 110 | + }); |
| 111 | + await databaseBuilder.commit(); |
| 112 | + |
| 113 | + // when |
| 114 | + const script = new ConvertUsersOrgaCguData(); |
| 115 | + const options = { dryRun: false, batchSize: 1, throttleDelay: 0 }; |
| 116 | + await script.handle({ options, logger }); |
| 117 | + |
| 118 | + // then |
| 119 | + expect(logger.info).to.have.been.calledWith('Total users migrated: 1'); |
| 120 | + |
| 121 | + const userAcceptances = await knex('legal-document-version-user-acceptances').where({ |
| 122 | + legalDocumentVersionId: legalDocumentVersion.id, |
| 123 | + }); |
| 124 | + |
| 125 | + const acceptance1 = userAcceptances.find((user) => user.userId === alreadyConvertedUser.id); |
| 126 | + expect(acceptance1.acceptedAt).to.deep.equal(new Date('2020-01-01')); |
| 127 | + |
| 128 | + const acceptance2 = userAcceptances.find((user) => user.userId === newUserAcceptedCgu.id); |
| 129 | + expect(acceptance2.acceptedAt).to.deep.equal(new Date('2021-01-01')); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + context('when the script has the dry run mode', function () { |
| 134 | + it('does not create legal document user acceptance', async function () { |
| 135 | + // given |
| 136 | + databaseBuilder.factory.buildUser({ |
| 137 | + pixOrgaTermsOfServiceAccepted: true, |
| 138 | + lastPixOrgaTermsOfServiceValidatedAt: new Date('2021-01-01'), |
| 139 | + }); |
| 140 | + await databaseBuilder.commit(); |
| 141 | + |
| 142 | + // when |
| 143 | + const script = new ConvertUsersOrgaCguData(); |
| 144 | + const options = { dryRun: true, batchSize: 1, throttleDelay: 0 }; |
| 145 | + await script.handle({ options, logger }); |
| 146 | + |
| 147 | + // then |
| 148 | + expect(logger.info).to.have.been.calledWith('Batch #1'); |
| 149 | + expect(logger.info).to.have.been.calledWith('Total users to migrate: 1'); |
| 150 | + |
| 151 | + const userAcceptances = await knex('legal-document-version-user-acceptances').where({ |
| 152 | + legalDocumentVersionId: legalDocumentVersion.id, |
| 153 | + }); |
| 154 | + expect(userAcceptances.length).to.equal(0); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + context('when no legal document is found', function () { |
| 159 | + it('throws an error', async function () { |
| 160 | + // given |
| 161 | + const script = new ConvertUsersOrgaCguData(); |
| 162 | + |
| 163 | + // when / then |
| 164 | + const options = { dryRun: true, batchSize: 1, throttleDelay: 0 }; |
| 165 | + await expect(script.handle({ options, logger })).to.be.rejectedWith( |
| 166 | + 'No legal document found for type: TOS, service: pix-orga', |
| 167 | + ); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments