Skip to content

Commit

Permalink
feat(api): make legal document "write" usecases be transactional
Browse files Browse the repository at this point in the history
and the "read" usecases should not be made transactional
  • Loading branch information
lego-technix committed Jan 7, 2025
1 parent dd7595a commit 0396cd3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withTransaction } from '../../../shared/domain/DomainTransaction.js';
import { LegalDocumentService } from '../models/LegalDocumentService.js';
import { LegalDocumentType } from '../models/LegalDocumentType.js';

Expand All @@ -13,31 +14,25 @@ const { TOS } = LegalDocumentType.VALUES;
* @param {string} params.type - The type of the legal document.
* @returns {Promise<void>} A promise that resolves when the operation is complete.
*/
const acceptLegalDocumentByUserId = async ({
userId,
service,
type,
userRepository,
legalDocumentRepository,
userAcceptanceRepository,
logger,
}) => {
LegalDocumentType.assert(type);
LegalDocumentService.assert(service);
const acceptLegalDocumentByUserId = withTransaction(
async ({ userId, service, type, userRepository, legalDocumentRepository, userAcceptanceRepository, logger }) => {
LegalDocumentType.assert(type);
LegalDocumentService.assert(service);

// legacy document acceptance
if (type === TOS && service === PIX_ORGA) {
await userRepository.setPixOrgaCguByUserId(userId);
}
// legacy document acceptance
if (type === TOS && service === PIX_ORGA) {
await userRepository.setPixOrgaCguByUserId(userId);
}

// new document acceptance
const legalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });
if (!legalDocument) {
logger.warn(`No legal document found for service: ${service} and type: ${type}`);
return;
}
// new document acceptance
const legalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type });
if (!legalDocument) {
logger.warn(`No legal document found for service: ${service} and type: ${type}`);
return;
}

await userAcceptanceRepository.create({ userId, legalDocumentVersionId: legalDocument.id });
};
await userAcceptanceRepository.create({ userId, legalDocumentVersionId: legalDocument.id });
},
);

export { acceptLegalDocumentByUserId };
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withTransaction } from '../../../shared/domain/DomainTransaction.js';
import { LegalDocumentInvalidDateError } from '../errors.js';
import { LegalDocumentService } from '../models/LegalDocumentService.js';
import { LegalDocumentType } from '../models/LegalDocumentType.js';
Expand All @@ -11,7 +12,7 @@ import { LegalDocumentType } from '../models/LegalDocumentType.js';
* @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 ({ service, type, versionAt, legalDocumentRepository }) => {
const createLegalDocument = withTransaction(async ({ service, type, versionAt, legalDocumentRepository }) => {
LegalDocumentService.assert(service);
LegalDocumentType.assert(type);

Expand All @@ -22,6 +23,6 @@ const createLegalDocument = async ({ service, type, versionAt, legalDocumentRepo
}

return legalDocumentRepository.create({ service, type, versionAt });
};
});

export { createLegalDocument };
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import Joi from 'joi';
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 { expect } from '../../../../test-helper.js';
import { DomainTransaction } from '../../../../../src/shared/domain/DomainTransaction.js';
import { expect, sinon } from '../../../../test-helper.js';

const { PIX_ORGA } = LegalDocumentService.VALUES;
const { TOS } = LegalDocumentType.VALUES;

describe('Unit | Legal documents | Domain | Use case | accept-legal-document-by-user-id', function () {
beforeEach(function () {
sinon.stub(DomainTransaction, 'execute');
DomainTransaction.execute.callsFake((fn) => {
return fn({});
});
});

context('when the legal document type is invalid', function () {
it('throws an error', async function () {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import Joi from 'joi';
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 { expect } from '../../../../test-helper.js';
import { DomainTransaction } from '../../../../../src/shared/domain/DomainTransaction.js';
import { expect, sinon } from '../../../../test-helper.js';

const { PIX_ORGA } = LegalDocumentService.VALUES;
const { TOS } = LegalDocumentType.VALUES;

describe('Unit | Legal documents | Domain | Use case | create-legal-document', function () {
beforeEach(function () {
sinon.stub(DomainTransaction, 'execute');
DomainTransaction.execute.callsFake((fn) => {
return fn({});
});
});

context('when the legal document type is invalid', function () {
it('throws an error', async function () {
// given
Expand Down

0 comments on commit 0396cd3

Please sign in to comment.