Skip to content

Commit

Permalink
feat(api): add getLegalDocumentStatusByUserId usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Dec 17, 2024
1 parent a130d31 commit 08cc22d
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { LegalDocumentStatus } from '../models/LegalDocumentStatus.js';

/**
* Get the legal document status by user ID.
*
* @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 {string} params.userId - The user ID.
* @returns {Promise<LegalDocumentStatus>} The legal document status.
* @throws {Error} If no legal document version is found for the type and service.
*/
const getLegalDocumentStatusByUserId = async ({
type,
service,
userId,
userRepository,
legalDocumentRepository,
userAcceptanceRepository,
config,
}) => {
const { isLegalDocumentsVersioningEnabled } = config.featureToggles;

if (!isLegalDocumentsVersioningEnabled) {
const user = await userRepository.findPixOrgaCgusByUserId(userId);
return LegalDocumentStatus.buildForLegacyPixOrgaCgu(user);
}

const lastLegalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ type, service });
if (!lastLegalDocument) {
throw new Error(`No legal document version found for type ${type} and service ${service}`);
}

const lastUserAcceptance = await userAcceptanceRepository.findLastForLegalDocument({ userId, type, service });

return LegalDocumentStatus.build(lastLegalDocument, lastUserAcceptance);
};

export { getLegalDocumentStatusByUserId };

0 comments on commit 08cc22d

Please sign in to comment.