-
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 getLegalDocumentStatusByUserId usecase
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
api/src/legal-documents/domain/usecases/get-legal-document-status-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
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 }; |