Skip to content

Commit b64d4f6

Browse files
committed
refactor(api): reorder legal document versions related properties to make it more logical
1 parent 2fb14ea commit b64d4f6

17 files changed

+70
-70
lines changed

api/db/database-builder/factory/build-legal-document-version.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { databaseBuffer } from '../database-buffer.js';
22

33
const buildLegalDocumentVersion = function ({
44
id = databaseBuffer.getNextId(),
5-
type,
65
service,
6+
type,
77
versionAt = new Date(),
88
} = {}) {
99
return databaseBuffer.pushInsertable({
1010
tableName: 'legal-document-versions',
11-
values: { id, type, service, versionAt },
11+
values: { id, service, type, versionAt },
1212
});
1313
};
1414

api/src/legal-documents/application/api/legal-documents-api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { usecases } from '../../domain/usecases/index.js';
1010
* @returns {Promise<void>} - A promise that resolves when the legal document is accepted.
1111
*/
1212
const acceptLegalDocumentByUserId = async ({ userId, service, type }) => {
13-
return usecases.acceptLegalDocumentByUserId({ type, service, userId });
13+
return usecases.acceptLegalDocumentByUserId({ userId, service, type });
1414
};
1515

1616
/**
@@ -23,7 +23,7 @@ const acceptLegalDocumentByUserId = async ({ userId, service, type }) => {
2323
* @returns {Promise<LegalDocumentStatus>} - A promise that resolves with the status of the legal document.
2424
*/
2525
const getLegalDocumentStatusByUserId = async ({ userId, service, type }) => {
26-
return usecases.getLegalDocumentStatusByUserId({ type, service, userId });
26+
return usecases.getLegalDocumentStatusByUserId({ userId, service, type });
2727
};
2828

2929
export { acceptLegalDocumentByUserId, getLegalDocumentStatusByUserId };

api/src/legal-documents/domain/models/LegalDocument.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import dayjs from 'dayjs';
22

33
export class LegalDocument {
4-
constructor({ id, type, service, versionAt }) {
4+
constructor({ id, service, type, versionAt }) {
55
this.id = id;
6-
this.type = type;
76
this.service = service;
7+
this.type = type;
88
this.versionAt = versionAt;
99
}
1010

api/src/legal-documents/domain/usecases/accept-legal-document-by-user-id.usecase.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { LegalDocumentService } from '../models/LegalDocumentService.js';
22
import { LegalDocumentType } from '../models/LegalDocumentType.js';
33

4-
const { TOS } = LegalDocumentType.VALUES;
54
const { PIX_ORGA } = LegalDocumentService.VALUES;
5+
const { TOS } = LegalDocumentType.VALUES;
66

77
/**
88
* Accepts a legal document by user ID.
99
*
1010
* @param {Object} params - The parameters.
11-
* @param {string} params.type - The type of the legal document.
12-
* @param {string} params.service - The service of the legal document.
1311
* @param {string} params.userId - The ID of the user.
12+
* @param {string} params.service - The service of the legal document.
13+
* @param {string} params.type - The type of the legal document.
1414
* @returns {Promise<void>} A promise that resolves when the operation is complete.
1515
*/
1616
const acceptLegalDocumentByUserId = async ({
17-
type,
18-
service,
1917
userId,
18+
service,
19+
type,
2020
userRepository,
2121
legalDocumentRepository,
2222
userAcceptanceRepository,
@@ -31,9 +31,9 @@ const acceptLegalDocumentByUserId = async ({
3131
}
3232

3333
// new document acceptance
34-
const legalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service });
34+
const legalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });
3535
if (!legalDocument) {
36-
logger.warn(`No legal document found for type: ${type} and service: ${service}`);
36+
logger.warn(`No legal document found for service: ${service} and type: ${type}`);
3737
return;
3838
}
3939

api/src/legal-documents/domain/usecases/create-legal-document.usecase.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import { LegalDocumentType } from '../models/LegalDocumentType.js';
66
* Creates a new legal document.
77
*
88
* @param {Object} params - The parameters.
9-
* @param {string} params.type - The type of the legal document.
109
* @param {string} params.service - The service of the legal document.
10+
* @param {string} params.type - The type of the legal document.
1111
* @param {string} params.versionAt - Version date of the new legal document.
1212
* @returns {Promise<LegalDocument>} A promise that resolves the new legal document.
1313
*/
14-
const createLegalDocument = async ({ type, service, versionAt, legalDocumentRepository }) => {
15-
LegalDocumentType.assert(type);
14+
const createLegalDocument = async ({ service, type, versionAt, legalDocumentRepository }) => {
1615
LegalDocumentService.assert(service);
16+
LegalDocumentType.assert(type);
1717

18-
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service });
18+
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });
1919

2020
if (lastDocument && lastDocument.versionAt >= versionAt) {
2121
throw new LegalDocumentInvalidDateError();
2222
}
2323

24-
return legalDocumentRepository.create({ type, service, versionAt });
24+
return legalDocumentRepository.create({ service, type, versionAt });
2525
};
2626

2727
export { createLegalDocument };

api/src/legal-documents/infrastructure/repositories/legal-document.repository.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const TABLE_NAME = 'legal-document-versions';
77
* Retrieves the latest version of a legal document by type and service.
88
*
99
* @param {Object} params - The parameters.
10-
* @param {string} params.type - The type of the legal document.
1110
* @param {string} params.service - The service associated with the legal document.
11+
* @param {string} params.type - The type of the legal document.
1212
* @returns {Promise<LegalDocument|null>} The latest version of the legal document or null if not found.
1313
*/
14-
const findLastVersionByTypeAndService = async ({ type, service }) => {
14+
const findLastVersionByTypeAndService = async ({ service, type }) => {
1515
const knexConnection = DomainTransaction.getConnection();
1616
const documentVersionDto = await knexConnection(TABLE_NAME)
17-
.where({ type, service })
17+
.where({ service, type })
1818
.orderBy('versionAt', 'desc')
1919
.first();
2020

@@ -27,15 +27,15 @@ const findLastVersionByTypeAndService = async ({ type, service }) => {
2727
* Creates a new legal document in the database.
2828
*
2929
* @param {Object} params - The parameters.
30-
* @param {string} params.type - The type of the legal document.
3130
* @param {string} params.service - The service associated with the legal document.
31+
* @param {string} params.type - The type of the legal document.
3232
* @param {Date} params.versionAt - The date of the legal document version.
3333
* @returns {Promise<LegalDocument>} The newly created legal document.
3434
*/
35-
const create = async ({ type, service, versionAt }) => {
35+
const create = async ({ service, type, versionAt }) => {
3636
const knexConnection = DomainTransaction.getConnection();
3737

38-
const [documentVersionDto] = await knexConnection(TABLE_NAME).insert({ type, service, versionAt }).returning('*');
38+
const [documentVersionDto] = await knexConnection(TABLE_NAME).insert({ service, type, versionAt }).returning('*');
3939

4040
return new LegalDocument(documentVersionDto);
4141
};

api/src/legal-documents/infrastructure/repositories/user-acceptance.repository.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ const create = async ({ userId, legalDocumentVersionId }) => {
2020
*
2121
* @param {Object} params - The parameters for finding the user acceptance.
2222
* @param {string} params.userId - The ID of the user.
23-
* @param {string} params.type - The type of the legal document.
2423
* @param {string} params.service - The service associated with the legal document.
24+
* @param {string} params.type - The type of the legal document.
2525
* @returns {Promise<Object|null>} A promise that resolves to the user acceptance record or null if not found.
2626
*/
27-
const findLastForLegalDocument = async ({ userId, type, service }) => {
27+
const findLastForLegalDocument = async ({ userId, service, type }) => {
2828
const knexConnection = DomainTransaction.getConnection();
2929
const userAcceptanceDto = await knexConnection(TABLE_NAME)
3030
.select('userId', 'legalDocumentVersionId', 'acceptedAt')
@@ -33,7 +33,7 @@ const findLastForLegalDocument = async ({ userId, type, service }) => {
3333
'legal-document-version-user-acceptances.legalDocumentVersionId',
3434
'legal-document-versions.id',
3535
)
36-
.where({ userId, type, service })
36+
.where({ userId, service, type })
3737
.orderBy('versionAt', 'desc')
3838
.first();
3939

api/src/legal-documents/scripts/add-new-legal-document-version.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ export class AddNewLegalDocumentVersion extends Script {
3333
}
3434

3535
async handle({ options, logger }) {
36-
let { type, service } = options;
36+
let { service, type } = options;
3737
const { versionAt } = options;
3838

3939
type = type.trim();
4040
service = service.trim();
4141

4242
logger.info(`Adding new legal document for type:${type}, service:${service}, versionAt:${versionAt}`);
4343

44-
await usecases.createLegalDocument({ type, service, versionAt });
44+
await usecases.createLegalDocument({ service, type, versionAt });
4545
logger.info(`New legal document for type:${type}, service:${service}, versionAt:${versionAt} added successfully.`);
4646
}
4747
}

api/tests/legal-documents/integration/domain/usecases/accept-legal-document-by-user-id.usecase.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { LegalDocumentType } from '../../../../../src/legal-documents/domain/mod
33
import { usecases } from '../../../../../src/legal-documents/domain/usecases/index.js';
44
import { databaseBuilder, expect, knex, sinon } from '../../../../test-helper.js';
55

6-
const { TOS } = LegalDocumentType.VALUES;
76
const { PIX_ORGA } = LegalDocumentService.VALUES;
7+
const { TOS } = LegalDocumentType.VALUES;
88

99
describe('Integration | Legal documents | Domain | Use case | accept-legal-document-by-user-id', function () {
1010
it('accepts the lastest legal document version for a user', async function () {
@@ -15,11 +15,11 @@ describe('Integration | Legal documents | Domain | Use case | accept-legal-docum
1515
service: PIX_ORGA,
1616
versionAt: new Date('2021-01-01'),
1717
});
18-
const document = databaseBuilder.factory.buildLegalDocumentVersion({ type: TOS, service: PIX_ORGA });
18+
const document = databaseBuilder.factory.buildLegalDocumentVersion({ service: PIX_ORGA, type: TOS });
1919
await databaseBuilder.commit();
2020

2121
// when
22-
await usecases.acceptLegalDocumentByUserId({ userId: user.id, type: TOS, service: PIX_ORGA });
22+
await usecases.acceptLegalDocumentByUserId({ userId: user.id, service: PIX_ORGA, type: TOS });
2323

2424
// then
2525
const userAcceptance = await knex('legal-document-version-user-acceptances')
@@ -33,12 +33,12 @@ describe('Integration | Legal documents | Domain | Use case | accept-legal-docum
3333
it('accepts the Pix Orga CGUs in the legacy and legal document model', async function () {
3434
// given
3535
const user = databaseBuilder.factory.buildUser({ pixOrgaTermsOfServiceAccepted: false });
36-
databaseBuilder.factory.buildLegalDocumentVersion({ type: TOS, service: PIX_ORGA });
36+
databaseBuilder.factory.buildLegalDocumentVersion({ service: PIX_ORGA, type: TOS });
3737

3838
await databaseBuilder.commit();
3939

4040
// when
41-
await usecases.acceptLegalDocumentByUserId({ userId: user.id, type: TOS, service: PIX_ORGA });
41+
await usecases.acceptLegalDocumentByUserId({ userId: user.id, service: PIX_ORGA, type: TOS });
4242

4343
// then
4444
const updatedUser = await knex('users').where('id', user.id).first();
@@ -52,11 +52,11 @@ describe('Integration | Legal documents | Domain | Use case | accept-legal-docum
5252
await databaseBuilder.commit();
5353

5454
// when
55-
await usecases.acceptLegalDocumentByUserId({ userId: user.id, type: TOS, service: PIX_ORGA, logger: loggerStub });
55+
await usecases.acceptLegalDocumentByUserId({ userId: user.id, service: PIX_ORGA, type: TOS, logger: loggerStub });
5656

5757
// then
5858
expect(loggerStub.warn).to.have.been.calledWith(
59-
`No legal document found for type: ${TOS} and service: ${PIX_ORGA}`,
59+
`No legal document found for service: ${PIX_ORGA} and type: ${TOS}`,
6060
);
6161
});
6262
});

api/tests/legal-documents/integration/domain/usecases/create-legal-document.usecase.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { LegalDocumentType } from '../../../../../src/legal-documents/domain/mod
44
import { usecases } from '../../../../../src/legal-documents/domain/usecases/index.js';
55
import { catchErr, databaseBuilder, domainBuilder, expect } from '../../../../test-helper.js';
66

7-
const { TOS } = LegalDocumentType.VALUES;
87
const { PIX_ORGA } = LegalDocumentService.VALUES;
8+
const { TOS } = LegalDocumentType.VALUES;
99

1010
describe('Integration | Legal documents | Domain | Use case | create-legal-document', function () {
1111
it('creates a new legal document when there is no previous version', async function () {
1212
// given
1313
const type = TOS;
1414
const service = PIX_ORGA;
1515
const versionAt = new Date('2024-12-01');
16-
const expectedDocument = domainBuilder.buildLegalDocument({ type, service, versionAt });
16+
const expectedDocument = domainBuilder.buildLegalDocument({ service, type, versionAt });
1717

1818
// when
19-
const document = await usecases.createLegalDocument({ type, service, versionAt });
19+
const document = await usecases.createLegalDocument({ service, type, versionAt });
2020

2121
// then
2222
expect(document).to.deepEqualInstanceOmitting(expectedDocument, 'id');
@@ -30,11 +30,11 @@ describe('Integration | Legal documents | Domain | Use case | create-legal-docum
3030
const existingVersionAt = new Date('2024-12-01');
3131
const newVersionAt = new Date('2024-11-30');
3232

33-
databaseBuilder.factory.buildLegalDocumentVersion({ type, service, versionAt: existingVersionAt });
33+
databaseBuilder.factory.buildLegalDocumentVersion({ service, type, versionAt: existingVersionAt });
3434
await databaseBuilder.commit();
3535

3636
// when
37-
const error = await catchErr(usecases.createLegalDocument)({ type, service, versionAt: newVersionAt });
37+
const error = await catchErr(usecases.createLegalDocument)({ service, type, versionAt: newVersionAt });
3838

3939
//then
4040
expect(error).to.be.instanceOf(LegalDocumentInvalidDateError);
@@ -49,13 +49,13 @@ describe('Integration | Legal documents | Domain | Use case | create-legal-docum
4949
const service = PIX_ORGA;
5050
const existingVersionAt = new Date('2024-12-01');
5151
const newVersionAt = new Date('2024-12-02');
52-
const expectedDocument = domainBuilder.buildLegalDocument({ type, service, versionAt: newVersionAt });
52+
const expectedDocument = domainBuilder.buildLegalDocument({ service, type, versionAt: newVersionAt });
5353

54-
databaseBuilder.factory.buildLegalDocumentVersion({ type, service, versionAt: existingVersionAt });
54+
databaseBuilder.factory.buildLegalDocumentVersion({ service, type, versionAt: existingVersionAt });
5555
await databaseBuilder.commit();
5656

5757
// when
58-
const document = await usecases.createLegalDocument({ type, service, versionAt: newVersionAt });
58+
const document = await usecases.createLegalDocument({ service, type, versionAt: newVersionAt });
5959

6060
// then
6161
expect(document).to.deepEqualInstanceOmitting(expectedDocument, 'id');

api/tests/legal-documents/integration/domain/usecases/get-legal-document-status-by-user-id.usecase.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { usecases } from '../../../../../src/legal-documents/domain/usecases/ind
66
import { config } from '../../../../../src/shared/config.js';
77
import { databaseBuilder, expect, sinon } from '../../../../test-helper.js';
88

9-
const { TOS } = LegalDocumentType.VALUES;
109
const { PIX_ORGA } = LegalDocumentService.VALUES;
10+
const { TOS } = LegalDocumentType.VALUES;
1111

1212
describe('Integration | Legal documents | Domain | Use case | get-legal-document-status-by-user-id', function () {
1313
beforeEach(async function () {
@@ -16,8 +16,8 @@ describe('Integration | Legal documents | Domain | Use case | get-legal-document
1616

1717
it('returns the legal document status for a user', async function () {
1818
// given
19-
const type = TOS;
2019
const service = PIX_ORGA;
20+
const type = TOS;
2121
const user = databaseBuilder.factory.buildUser();
2222
const documentVersion = databaseBuilder.factory.buildLegalDocumentVersion({
2323
type,
@@ -32,7 +32,7 @@ describe('Integration | Legal documents | Domain | Use case | get-legal-document
3232
await databaseBuilder.commit();
3333

3434
// when
35-
const legalDocumentStatus = await usecases.getLegalDocumentStatusByUserId({ type, service, userId: user.id });
35+
const legalDocumentStatus = await usecases.getLegalDocumentStatusByUserId({ userId: user.id, service, type });
3636

3737
// then
3838
expect(legalDocumentStatus).to.be.an.instanceOf(LegalDocumentStatus);
@@ -46,13 +46,13 @@ describe('Integration | Legal documents | Domain | Use case | get-legal-document
4646
context('when the legal document version does not exist', function () {
4747
it('throws an error', async function () {
4848
// given
49-
const type = TOS;
5049
const service = PIX_ORGA;
50+
const type = TOS;
5151
const user = databaseBuilder.factory.buildUser();
5252
await databaseBuilder.commit();
5353

5454
// when / then
55-
await expect(usecases.getLegalDocumentStatusByUserId({ type, service, userId: user.id })).to.be.rejectedWith(
55+
await expect(usecases.getLegalDocumentStatusByUserId({ userId: user.id, service, type })).to.be.rejectedWith(
5656
LegalDocumentVersionNotFoundError,
5757
);
5858
});
@@ -65,16 +65,16 @@ describe('Integration | Legal documents | Domain | Use case | get-legal-document
6565

6666
it('returns the legal document status for a user based on PixOrga CGU', async function () {
6767
// given
68-
const type = TOS;
6968
const service = PIX_ORGA;
69+
const type = TOS;
7070
const user = databaseBuilder.factory.buildUser({
7171
pixOrgaTermsOfServiceAccepted: true,
7272
lastPixOrgaTermsOfServiceValidatedAt: new Date('2024-03-01'),
7373
});
7474
await databaseBuilder.commit();
7575

7676
// when
77-
const legalDocumentStatus = await usecases.getLegalDocumentStatusByUserId({ type, service, userId: user.id });
77+
const legalDocumentStatus = await usecases.getLegalDocumentStatusByUserId({ userId: user.id, service, type });
7878

7979
// then
8080
expect(legalDocumentStatus).to.be.an.instanceOf(LegalDocumentStatus);

api/tests/legal-documents/integration/infrastructure/repositories/legal-document.repository.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { LegalDocumentType } from '../../../../../src/legal-documents/domain/mod
44
import * as legalDocumentRepository from '../../../../../src/legal-documents/infrastructure/repositories/legal-document.repository.js';
55
import { databaseBuilder, domainBuilder, expect } from '../../../../test-helper.js';
66

7-
const { TOS } = LegalDocumentType.VALUES;
87
const { PIX_ORGA, PIX_APP } = LegalDocumentService.VALUES;
8+
const { TOS } = LegalDocumentType.VALUES;
99

1010
describe('Integration | Legal document | Infrastructure | Repository | legal-document', function () {
1111
describe('#findLastVersionByTypeAndService', function () {
@@ -32,7 +32,7 @@ describe('Integration | Legal document | Infrastructure | Repository | legal-doc
3232
await databaseBuilder.commit();
3333

3434
// when
35-
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service });
35+
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });
3636

3737
// then
3838
expect(lastDocument).to.deepEqualInstance(domainBuilder.buildLegalDocument(expectedDocument));
@@ -58,11 +58,11 @@ describe('Integration | Legal document | Infrastructure | Repository | legal-doc
5858
const versionAt = new Date('2024-12-01');
5959

6060
// when
61-
const createdDocument = await legalDocumentRepository.create({ type, service, versionAt });
61+
const createdDocument = await legalDocumentRepository.create({ service, type, versionAt });
6262

6363
// then
6464
expect(createdDocument).to.be.instanceOf(LegalDocument);
65-
expect(createdDocument).to.deep.include({ type, service, versionAt });
65+
expect(createdDocument).to.deep.include({ service, type, versionAt });
6666
});
6767
});
6868
});

0 commit comments

Comments
 (0)