From 79d7276618fba14bc9821013e705201ac0e02592 Mon Sep 17 00:00:00 2001 From: P-Jeremy Date: Wed, 11 Dec 2024 14:39:37 +0100 Subject: [PATCH] feat(api): add new repo method to save legal document version --- .../repositories/legal-document.repository.js | 20 ++++++++++++++++++- .../legal-document.repository.test.js | 20 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/api/src/legal-documents/infrastructure/repositories/legal-document.repository.js b/api/src/legal-documents/infrastructure/repositories/legal-document.repository.js index 2dc7f5ebe2b..ae24c16435e 100644 --- a/api/src/legal-documents/infrastructure/repositories/legal-document.repository.js +++ b/api/src/legal-documents/infrastructure/repositories/legal-document.repository.js @@ -21,4 +21,22 @@ const findLastVersionByTypeAndService = async ({ type, service }) => { return new LegalDocument(documentVersionDto); }; -export { findLastVersionByTypeAndService }; +/** + * 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 {Date} params.versionAt - The date of the legal document version. + * @returns {Promise} The newly created legal document. + */ +const createLegalDocument = async ({ type, service, versionAt }) => { + const knexConnection = DomainTransaction.getConnection(); + + const [documentVersionDto] = await knexConnection('legal-document-versions') + .insert({ type, service, versionAt }) + .returning('*'); + return new LegalDocument(documentVersionDto); +}; + +export { createLegalDocument, findLastVersionByTypeAndService }; diff --git a/api/tests/legal-documents/integration/infrastructure/repositories/legal-document.repository.test.js b/api/tests/legal-documents/integration/infrastructure/repositories/legal-document.repository.test.js index a5b1647160c..15284abdf34 100644 --- a/api/tests/legal-documents/integration/infrastructure/repositories/legal-document.repository.test.js +++ b/api/tests/legal-documents/integration/infrastructure/repositories/legal-document.repository.test.js @@ -47,4 +47,24 @@ describe('Integration | Legal document | Infrastructure | Repository | legal-doc expect(lastDocument).to.be.null; }); }); + + describe('#createLegalDocument', function () { + it('creates a new legal document in the database', async function () { + // given + const type = TOS; + const service = PIX_ORGA; + const versionAt = new Date('2024-12-01'); + + // when + const createdDocument = await legalDocumentRepository.createLegalDocument({ type, service, versionAt }); + + // then + expect(createdDocument).to.be.instanceOf(LegalDocument); + expect(createdDocument).to.deep.include({ + type, + service, + versionAt, + }); + }); + }); });