Skip to content

Commit

Permalink
feat(api): add usecase to create a new legal document
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Jeremy committed Dec 12, 2024
1 parent 331d8fd commit 610142a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit 610142a

Please sign in to comment.