Skip to content

Commit

Permalink
refactor(api): reorder legal document versions related properties to …
Browse files Browse the repository at this point in the history
…make it more logical
  • Loading branch information
lego-technix authored Dec 20, 2024
1 parent 1704bb2 commit 10c267c
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { databaseBuffer } from '../database-buffer.js';

const buildLegalDocumentVersion = function ({
id = databaseBuffer.getNextId(),
type,
service,
type,
versionAt = new Date(),
} = {}) {
return databaseBuffer.pushInsertable({
tableName: 'legal-document-versions',
values: { id, type, service, versionAt },
values: { id, service, type, versionAt },
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { usecases } from '../../domain/usecases/index.js';
* @returns {Promise<void>} - A promise that resolves when the legal document is accepted.
*/
const acceptLegalDocumentByUserId = async ({ userId, service, type }) => {
return usecases.acceptLegalDocumentByUserId({ type, service, userId });
return usecases.acceptLegalDocumentByUserId({ userId, service, type });
};

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

export { acceptLegalDocumentByUserId, getLegalDocumentStatusByUserId };
4 changes: 2 additions & 2 deletions api/src/legal-documents/domain/models/LegalDocument.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import dayjs from 'dayjs';

export class LegalDocument {
constructor({ id, type, service, versionAt }) {
constructor({ id, service, type, versionAt }) {
this.id = id;
this.type = type;
this.service = service;
this.type = type;
this.versionAt = versionAt;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { LegalDocumentService } from '../models/LegalDocumentService.js';
import { LegalDocumentType } from '../models/LegalDocumentType.js';

const { TOS } = LegalDocumentType.VALUES;
const { PIX_ORGA } = LegalDocumentService.VALUES;
const { TOS } = LegalDocumentType.VALUES;

/**
* Accepts a legal document by user ID.
*
* @param {Object} params - The parameters.
* @param {string} params.type - The type of the legal document.
* @param {string} params.service - The service of the legal document.
* @param {string} params.userId - The ID of the user.
* @param {string} params.service - The service of the legal document.
* @param {string} params.type - The type of the legal document.
* @returns {Promise<void>} A promise that resolves when the operation is complete.
*/
const acceptLegalDocumentByUserId = async ({
type,
service,
userId,
service,
type,
userRepository,
legalDocumentRepository,
userAcceptanceRepository,
Expand All @@ -31,9 +31,9 @@ const acceptLegalDocumentByUserId = async ({
}

// new document acceptance
const legalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service });
const legalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });
if (!legalDocument) {
logger.warn(`No legal document found for type: ${type} and service: ${service}`);
logger.warn(`No legal document found for service: ${service} and type: ${type}`);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import { LegalDocumentType } from '../models/LegalDocumentType.js';
* Creates a new legal document.
*
* @param {Object} params - The parameters.
* @param {string} params.type - The type of the legal document.
* @param {string} params.service - The service of the legal document.
* @param {string} params.type - The type of the legal document.
* @param {string} params.versionAt - Version date of the new legal document.
* @returns {Promise<LegalDocument>} A promise that resolves the new legal document.
*/
const createLegalDocument = async ({ type, service, versionAt, legalDocumentRepository }) => {
LegalDocumentType.assert(type);
const createLegalDocument = async ({ service, type, versionAt, legalDocumentRepository }) => {
LegalDocumentService.assert(service);
LegalDocumentType.assert(type);

const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service });
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });

if (lastDocument && lastDocument.versionAt >= versionAt) {
throw new LegalDocumentInvalidDateError();
}

return legalDocumentRepository.create({ type, service, versionAt });
return legalDocumentRepository.create({ service, type, versionAt });
};

export { createLegalDocument };
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const TABLE_NAME = 'legal-document-versions';
* Retrieves the latest version of a legal document by type and service.
*
* @param {Object} params - The parameters.
* @param {string} params.type - The type of the legal document.
* @param {string} params.service - The service associated with the legal document.
* @param {string} params.type - The type of the legal document.
* @returns {Promise<LegalDocument|null>} The latest version of the legal document or null if not found.
*/
const findLastVersionByTypeAndService = async ({ type, service }) => {
const findLastVersionByTypeAndService = async ({ service, type }) => {
const knexConnection = DomainTransaction.getConnection();
const documentVersionDto = await knexConnection(TABLE_NAME)
.where({ type, service })
.where({ service, type })
.orderBy('versionAt', 'desc')
.first();

Expand All @@ -27,15 +27,15 @@ const findLastVersionByTypeAndService = async ({ type, service }) => {
* Creates a new legal document in the database.
*
* @param {Object} params - The parameters.
* @param {string} params.type - The type of the legal document.
* @param {string} params.service - The service associated with the legal document.
* @param {string} params.type - The type of the legal document.
* @param {Date} params.versionAt - The date of the legal document version.
* @returns {Promise<LegalDocument>} The newly created legal document.
*/
const create = async ({ type, service, versionAt }) => {
const create = async ({ service, type, versionAt }) => {
const knexConnection = DomainTransaction.getConnection();

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

return new LegalDocument(documentVersionDto);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const create = async ({ userId, legalDocumentVersionId }) => {
*
* @param {Object} params - The parameters for finding the user acceptance.
* @param {string} params.userId - The ID of the user.
* @param {string} params.type - The type of the legal document.
* @param {string} params.service - The service associated with the legal document.
* @param {string} params.type - The type of the legal document.
* @returns {Promise<Object|null>} A promise that resolves to the user acceptance record or null if not found.
*/
const findLastForLegalDocument = async ({ userId, type, service }) => {
const findLastForLegalDocument = async ({ userId, service, type }) => {
const knexConnection = DomainTransaction.getConnection();
const userAcceptanceDto = await knexConnection(TABLE_NAME)
.select('userId', 'legalDocumentVersionId', 'acceptedAt')
Expand All @@ -33,7 +33,7 @@ const findLastForLegalDocument = async ({ userId, type, service }) => {
'legal-document-version-user-acceptances.legalDocumentVersionId',
'legal-document-versions.id',
)
.where({ userId, type, service })
.where({ userId, service, type })
.orderBy('versionAt', 'desc')
.first();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ export class AddNewLegalDocumentVersion extends Script {
}

async handle({ options, logger }) {
let { type, service } = options;
let { service, type } = options;
const { versionAt } = options;

type = type.trim();
service = service.trim();

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

await usecases.createLegalDocument({ type, service, versionAt });
await usecases.createLegalDocument({ service, type, versionAt });
logger.info(`New legal document for type:${type}, service:${service}, versionAt:${versionAt} added successfully.`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { LegalDocumentType } from '../../../../../src/legal-documents/domain/mod
import { usecases } from '../../../../../src/legal-documents/domain/usecases/index.js';
import { databaseBuilder, expect, knex, sinon } from '../../../../test-helper.js';

const { TOS } = LegalDocumentType.VALUES;
const { PIX_ORGA } = LegalDocumentService.VALUES;
const { TOS } = LegalDocumentType.VALUES;

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

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

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

await databaseBuilder.commit();

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

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

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

// then
expect(loggerStub.warn).to.have.been.calledWith(
`No legal document found for type: ${TOS} and service: ${PIX_ORGA}`,
`No legal document found for service: ${PIX_ORGA} and type: ${TOS}`,
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { LegalDocumentType } from '../../../../../src/legal-documents/domain/mod
import { usecases } from '../../../../../src/legal-documents/domain/usecases/index.js';
import { catchErr, databaseBuilder, domainBuilder, expect } from '../../../../test-helper.js';

const { TOS } = LegalDocumentType.VALUES;
const { PIX_ORGA } = LegalDocumentService.VALUES;
const { TOS } = LegalDocumentType.VALUES;

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

// when
const document = await usecases.createLegalDocument({ type, service, versionAt });
const document = await usecases.createLegalDocument({ service, type, versionAt });

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

databaseBuilder.factory.buildLegalDocumentVersion({ type, service, versionAt: existingVersionAt });
databaseBuilder.factory.buildLegalDocumentVersion({ service, type, versionAt: existingVersionAt });
await databaseBuilder.commit();

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

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

databaseBuilder.factory.buildLegalDocumentVersion({ type, service, versionAt: existingVersionAt });
databaseBuilder.factory.buildLegalDocumentVersion({ service, type, versionAt: existingVersionAt });
await databaseBuilder.commit();

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

// then
expect(document).to.deepEqualInstanceOmitting(expectedDocument, 'id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { usecases } from '../../../../../src/legal-documents/domain/usecases/ind
import { config } from '../../../../../src/shared/config.js';
import { databaseBuilder, expect, sinon } from '../../../../test-helper.js';

const { TOS } = LegalDocumentType.VALUES;
const { PIX_ORGA } = LegalDocumentService.VALUES;
const { TOS } = LegalDocumentType.VALUES;

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

it('returns the legal document status for a user', async function () {
// given
const type = TOS;
const service = PIX_ORGA;
const type = TOS;
const user = databaseBuilder.factory.buildUser();
const documentVersion = databaseBuilder.factory.buildLegalDocumentVersion({
type,
Expand All @@ -32,7 +32,7 @@ describe('Integration | Legal documents | Domain | Use case | get-legal-document
await databaseBuilder.commit();

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

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

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

it('returns the legal document status for a user based on PixOrga CGU', async function () {
// given
const type = TOS;
const service = PIX_ORGA;
const type = TOS;
const user = databaseBuilder.factory.buildUser({
pixOrgaTermsOfServiceAccepted: true,
lastPixOrgaTermsOfServiceValidatedAt: new Date('2024-03-01'),
});
await databaseBuilder.commit();

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

// then
expect(legalDocumentStatus).to.be.an.instanceOf(LegalDocumentStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { LegalDocumentType } from '../../../../../src/legal-documents/domain/mod
import * as legalDocumentRepository from '../../../../../src/legal-documents/infrastructure/repositories/legal-document.repository.js';
import { databaseBuilder, domainBuilder, expect } from '../../../../test-helper.js';

const { TOS } = LegalDocumentType.VALUES;
const { PIX_ORGA, PIX_APP } = LegalDocumentService.VALUES;
const { TOS } = LegalDocumentType.VALUES;

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

// when
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service });
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });

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

// when
const createdDocument = await legalDocumentRepository.create({ type, service, versionAt });
const createdDocument = await legalDocumentRepository.create({ service, type, versionAt });

// then
expect(createdDocument).to.be.instanceOf(LegalDocument);
expect(createdDocument).to.deep.include({ type, service, versionAt });
expect(createdDocument).to.deep.include({ service, type, versionAt });
});
});
});
Loading

0 comments on commit 10c267c

Please sign in to comment.