-
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.
[FEATURE] Script d'ajout d'un document legal (PIX-15582)
- Loading branch information
Showing
19 changed files
with
387 additions
and
28 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
api/scripts/legal-documents/add-new-legal-document-version.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,51 @@ | ||
import 'dotenv/config'; | ||
|
||
import { usecases } from '../../src/legal-documents/domain/usecases/index.js'; | ||
import { isoDateParser } from '../../src/shared/application/scripts/parsers.js'; | ||
import { Script } from '../../src/shared/application/scripts/script.js'; | ||
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js'; | ||
|
||
export class AddNewLegalDocumentVersion extends Script { | ||
constructor() { | ||
super({ | ||
description: 'Adds a new legal document version.', | ||
permanent: true, | ||
options: { | ||
type: { | ||
type: 'string', | ||
describe: 'Type of document (ex: "TOS", "PDP")', | ||
demandOption: true, | ||
requiresArg: true, | ||
}, | ||
service: { | ||
type: 'string', | ||
describe: 'Associated service (ex: "pix-app", "pix-orga",...)', | ||
demandOption: true, | ||
requiresArg: true, | ||
}, | ||
versionAt: { | ||
type: 'string', | ||
describe: 'Version date of the legal document, format "YYYY-MM-DD", (ex: "2020-02-27")', | ||
demandOption: true, | ||
requiresArg: true, | ||
coerce: isoDateParser(), | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async handle({ options, logger }) { | ||
let { type, service } = 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 }); | ||
logger.info(`New legal document for type:${type}, service:${service}, versionAt:${versionAt} added successfully.`); | ||
} | ||
} | ||
|
||
await ScriptRunner.execute(import.meta.url, AddNewLegalDocumentVersion); |
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,13 @@ | ||
import { DomainError } from '../../shared/domain/errors.js'; | ||
|
||
class LegalDocumentInvalidDateError extends DomainError { | ||
constructor({ | ||
code = 'LEGAL_DOCUMENT_INVALID_DATE', | ||
message = 'Document version must not be before or equal to same document type and service', | ||
} = {}) { | ||
super(message); | ||
this.code = code; | ||
} | ||
} | ||
|
||
export { LegalDocumentInvalidDateError }; |
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
13 changes: 13 additions & 0 deletions
13
api/src/legal-documents/domain/models/LegalDocumentService.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,13 @@ | ||
import Joi from 'joi'; | ||
|
||
const VALUES = { | ||
PIX_APP: 'pix-app', | ||
PIX_ORGA: 'pix-orga', | ||
PIX_CERTIF: 'pix-certif', | ||
}; | ||
|
||
const assert = (value) => { | ||
Joi.assert(value, Joi.string().valid(...Object.values(VALUES))); | ||
}; | ||
|
||
export const LegalDocumentService = { VALUES, assert }; |
11 changes: 11 additions & 0 deletions
11
api/src/legal-documents/domain/models/LegalDocumentType.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,11 @@ | ||
import Joi from 'joi'; | ||
|
||
const VALUES = { | ||
TOS: 'TOS', | ||
}; | ||
|
||
const assert = (value) => { | ||
Joi.assert(value, Joi.string().valid(...Object.values(VALUES))); | ||
}; | ||
|
||
export const LegalDocumentType = { VALUES, assert }; |
7 changes: 4 additions & 3 deletions
7
api/src/legal-documents/domain/usecases/accept-legal-document-by-user-id.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
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
import { LegalDocumentInvalidDateError } from '../errors.js'; | ||
import { LegalDocumentService } from '../models/LegalDocumentService.js'; | ||
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.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); | ||
LegalDocumentService.assert(service); | ||
|
||
const lastDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service }); | ||
|
||
if (lastDocument && lastDocument.versionAt >= versionAt) { | ||
throw new LegalDocumentInvalidDateError(); | ||
} | ||
|
||
return legalDocumentRepository.create({ type, service, versionAt }); | ||
}; | ||
|
||
export { createLegalDocument }; |
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
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
7 changes: 4 additions & 3 deletions
7
api/tests/legal-documents/integration/application/api/legal-documents-api.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
7 changes: 4 additions & 3 deletions
7
...al-documents/integration/domain/usecases/accept-legal-document-by-user-id.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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
import { LegalDocumentInvalidDateError } from '../../../../../src/legal-documents/domain/errors.js'; | ||
import { LegalDocumentService } from '../../../../../src/legal-documents/domain/models/LegalDocumentService.js'; | ||
import { LegalDocumentType } from '../../../../../src/legal-documents/domain/models/LegalDocumentType.js'; | ||
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; | ||
|
||
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(LegalDocumentInvalidDateError); | ||
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'); | ||
}); | ||
}); | ||
}); |
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
7 changes: 4 additions & 3 deletions
7
...egal-documents/integration/infrastructure/repositories/user-acceptance.repository.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
26 changes: 26 additions & 0 deletions
26
api/tests/legal-documents/unit/domain/models/LegalDocumentService.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,26 @@ | ||
import Joi from 'joi'; | ||
|
||
import { LegalDocumentService } from '../../../../../src/legal-documents/domain/models/LegalDocumentService.js'; | ||
import { expect } from '../../../../test-helper.js'; | ||
|
||
describe('Unit | Legal documents | Domain | Model | LegalDocumentService', function () { | ||
describe('VALUES', function () { | ||
it('has correct values', function () { | ||
expect(LegalDocumentService.VALUES.PIX_APP).to.equal('pix-app'); | ||
expect(LegalDocumentService.VALUES.PIX_ORGA).to.equal('pix-orga'); | ||
expect(LegalDocumentService.VALUES.PIX_CERTIF).to.equal('pix-certif'); | ||
}); | ||
}); | ||
|
||
describe('#assert', function () { | ||
it('does not throw an error for valid values', function () { | ||
expect(() => LegalDocumentService.assert('pix-app')).to.not.throw(); | ||
expect(() => LegalDocumentService.assert('pix-orga')).to.not.throw(); | ||
expect(() => LegalDocumentService.assert('pix-certif')).to.not.throw(); | ||
}); | ||
|
||
it('throws an error for invalid values', function () { | ||
expect(() => LegalDocumentService.assert('invalid-value')).to.throw(Joi.ValidationError); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.