-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add usecase to create a new legal document
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
api/src/legal-documents/domain/usecases/create-legal-document.usecase.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
59 changes: 59 additions & 0 deletions
59
api/tests/legal-documents/integration/domain/usecases/create-legal-document.usecase.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |