diff --git a/api/src/legal-documents/domain/usecases/create-legal-document.usecase.js b/api/src/legal-documents/domain/usecases/create-legal-document.usecase.js new file mode 100644 index 00000000000..e0a31306ddb --- /dev/null +++ b/api/src/legal-documents/domain/usecases/create-legal-document.usecase.js @@ -0,0 +1,22 @@ +import { ConflictError } from '../../../shared/application/http-errors.js'; + +/** + * Create 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.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 }) => { + const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service }); + + if (lastDocument && new Date(lastDocument.versionAt) >= new Date(versionAt)) { + throw new ConflictError('Document version must not be before or equal to same document type and service'); + } + + return legalDocumentRepository.create({ type, service, versionAt }); +}; + +export { createLegalDocument }; diff --git a/api/tests/legal-documents/integration/domain/usecases/create-legal-document.usecase.test.js b/api/tests/legal-documents/integration/domain/usecases/create-legal-document.usecase.test.js new file mode 100644 index 00000000000..680d19e40ea --- /dev/null +++ b/api/tests/legal-documents/integration/domain/usecases/create-legal-document.usecase.test.js @@ -0,0 +1,59 @@ +import { usecases } from '../../../../../src/legal-documents/domain/usecases/index.js'; +import { ConflictError } from '../../../../../src/shared/application/http-errors.js'; +import { catchErr, databaseBuilder, domainBuilder, expect } from '../../../../test-helper.js'; + +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 }); + + // when + const document = await usecases.createLegalDocument({ type, service, versionAt }); + + // then + expect(document).to.deepEqualInstanceOmitting(expectedDocument, 'id'); + }); + + context('when a previous version exists', function () { + it('throws an error if the new version date is before or equal to the existing version date', async function () { + // given + const type = 'TOS'; + const service = 'PIX_ORGA'; + const existingVersionAt = new Date('2024-12-01'); + const newVersionAt = new Date('2024-11-30'); + + databaseBuilder.factory.buildLegalDocumentVersion({ type, service, versionAt: existingVersionAt }); + await databaseBuilder.commit(); + + // when + const error = await catchErr(usecases.createLegalDocument)({ type, service, versionAt: newVersionAt }); + + //then + expect(error).to.be.instanceOf(ConflictError); + expect(error.message).to.be.equal( + 'Document version must not be before or equal to same document type and service', + ); + }); + + it('creates a new document if the new version date is after the existing version date', async function () { + // given + const type = 'TOS'; + 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 }); + + databaseBuilder.factory.buildLegalDocumentVersion({ type, service, versionAt: existingVersionAt }); + await databaseBuilder.commit(); + + // when + const document = await usecases.createLegalDocument({ type, service, versionAt: newVersionAt }); + + // then + expect(document).to.deepEqualInstanceOmitting(expectedDocument, 'id'); + }); + }); +});