-
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] Ajout de l'API getLegalDocumentStatusByUserId dans legal-do…
- Loading branch information
Showing
25 changed files
with
730 additions
and
72 deletions.
There are no files selected for viewing
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
29 changes: 21 additions & 8 deletions
29
api/src/legal-documents/application/api/legal-documents-api.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 |
---|---|---|
@@ -1,16 +1,29 @@ | ||
import { usecases } from '../../domain/usecases/index.js'; | ||
|
||
/** | ||
* Accept legal document by user id. | ||
* Accepts a legal document for a user by their ID. | ||
* | ||
* @param{string} params.service | ||
* @param{string} params.type | ||
* @param{string} params.userId | ||
* @param {Object} params - The parameters. | ||
* @param {string} params.userId - The ID of the user. | ||
* @param {string} params.service - The service associated with the legal document. (e.g. 'pix-orga') | ||
* @param {string} params.type - The type of the legal document. (e.g. 'TOS') | ||
* @returns {Promise<void>} - A promise that resolves when the legal document is accepted. | ||
*/ | ||
const acceptLegalDocumentByUserId = async ({ userId, service, type }) => { | ||
return usecases.acceptLegalDocumentByUserId({ userId, service, type }); | ||
}; | ||
|
||
/** | ||
* Gets the status of a legal document for a user by their ID. | ||
* | ||
* @returns {Promise<void>} | ||
* @param {Object} params - The parameters. | ||
* @param {string} params.userId - The ID of the user. | ||
* @param {string} params.service - The service associated with the legal document. (e.g. 'pix-orga') | ||
* @param {string} params.type - The type of the legal document. (e.g. 'TOS') | ||
* @returns {Promise<LegalDocumentStatus>} - A promise that resolves with the status of the legal document. | ||
*/ | ||
const acceptLegalDocumentByUserId = async ({ type, service, userId }) => { | ||
return usecases.acceptLegalDocumentByUserId({ type, service, userId }); | ||
const getLegalDocumentStatusByUserId = async ({ userId, service, type }) => { | ||
return usecases.getLegalDocumentStatusByUserId({ userId, service, type }); | ||
}; | ||
|
||
export { acceptLegalDocumentByUserId }; | ||
export { acceptLegalDocumentByUserId, getLegalDocumentStatusByUserId }; |
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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
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; | ||
constructor() { | ||
super( | ||
'Document version must not be before or equal to same document service and type', | ||
'LEGAL_DOCUMENT_INVALID_DATE', | ||
); | ||
} | ||
} | ||
|
||
export { LegalDocumentInvalidDateError }; | ||
class LegalDocumentVersionNotFoundError extends DomainError { | ||
constructor() { | ||
super('No legal document version found for service and type', 'LEGAL_DOCUMENT_VERSION_NOT_FOUND'); | ||
} | ||
} | ||
|
||
export { LegalDocumentInvalidDateError, LegalDocumentVersionNotFoundError }; |
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
import dayjs from 'dayjs'; | ||
|
||
export class LegalDocument { | ||
constructor({ id, type, service, versionAt }) { | ||
constructor({ id, service, type, versionAt }) { | ||
this.id = id; | ||
this.type = type; | ||
this.service = service; | ||
this.type = type; | ||
this.versionAt = versionAt; | ||
} | ||
|
||
buildDocumentPath() { | ||
const service = this.service.toLowerCase(); | ||
const type = this.type.toLowerCase(); | ||
const versionAt = dayjs(this.versionAt).format('YYYY-MM-DD'); | ||
return `${service}-${type}-${versionAt}`; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
api/src/legal-documents/domain/models/LegalDocumentStatus.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,57 @@ | ||
export const STATUS = { | ||
ACCEPTED: 'accepted', | ||
REQUESTED: 'requested', | ||
UPDATE_REQUESTED: 'update-requested', | ||
}; | ||
|
||
export class LegalDocumentStatus { | ||
constructor({ status, acceptedAt, documentPath }) { | ||
this.status = status; | ||
this.acceptedAt = acceptedAt; | ||
this.documentPath = documentPath; | ||
} | ||
|
||
/** | ||
* Builds a LegalDocumentStatus based on legacy PixOrga CGU. | ||
* | ||
* @param {Object} userPixOrgaCgu - The user object. | ||
* @param {boolean} userPixOrgaCgu.pixOrgaTermsOfServiceAccepted - Indicates if the PixOrga terms of service are accepted. | ||
* @param {Date} userPixOrgaCgu.lastPixOrgaTermsOfServiceValidatedAt - The date when the PixOrga terms of service were last validated. | ||
* @returns {LegalDocumentStatus} The legal document status. | ||
*/ | ||
static buildForLegacyPixOrgaCgu(userPixOrgaCgu) { | ||
const LEGACY_PIXORGA_TOS_PATH = 'pix-orga-tos-2024-01-02'; | ||
const { pixOrgaTermsOfServiceAccepted, lastPixOrgaTermsOfServiceValidatedAt } = userPixOrgaCgu; | ||
|
||
return new LegalDocumentStatus({ | ||
status: pixOrgaTermsOfServiceAccepted ? STATUS.ACCEPTED : STATUS.REQUESTED, | ||
acceptedAt: lastPixOrgaTermsOfServiceValidatedAt, | ||
documentPath: LEGACY_PIXORGA_TOS_PATH, | ||
}); | ||
} | ||
|
||
/** | ||
* Builds a LegalDocumentStatus based on the last document version and user acceptance. | ||
* | ||
* @param {Object} lastDocumentVersion - The last document version object. | ||
* @param {string} lastDocumentVersion.id - The ID of the last document version. | ||
* @param {Object} lastUserAcceptance - The last user acceptance object. | ||
* @param {string} lastUserAcceptance.legalDocumentVersionId - The ID of the accepted legal document version. | ||
* @param {Date} lastUserAcceptance.acceptedAt - The date when the document was accepted. | ||
* @returns {LegalDocumentStatus} The legal document status. | ||
*/ | ||
static build(lastDocumentVersion, lastUserAcceptance) { | ||
const documentPath = lastDocumentVersion.buildDocumentPath(); | ||
|
||
if (!lastUserAcceptance) { | ||
return new LegalDocumentStatus({ status: STATUS.REQUESTED, acceptedAt: null, documentPath }); | ||
} | ||
|
||
const { legalDocumentVersionId, acceptedAt } = lastUserAcceptance; | ||
if (lastDocumentVersion.id === legalDocumentVersionId) { | ||
return new LegalDocumentStatus({ status: STATUS.ACCEPTED, acceptedAt, documentPath }); | ||
} | ||
|
||
return new LegalDocumentStatus({ status: STATUS.UPDATE_REQUESTED, acceptedAt: null, documentPath }); | ||
} | ||
} |
17 changes: 10 additions & 7 deletions
17
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
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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
import { config } from '../../../shared/config.js'; | ||
import { LegalDocumentVersionNotFoundError } from '../errors.js'; | ||
import { LegalDocumentService } from '../models/LegalDocumentService.js'; | ||
import { LegalDocumentStatus } from '../models/LegalDocumentStatus.js'; | ||
import { LegalDocumentType } from '../models/LegalDocumentType.js'; | ||
|
||
/** | ||
* Gets the legal document status by user ID. | ||
* | ||
* @param {Object} params - The parameters. | ||
* @param {string} params.userId - The user ID. | ||
* @param {string} params.service - The service associated with the legal document. | ||
* @param {string} params.type - The type of the legal document. | ||
* @returns {Promise<LegalDocumentStatus>} The legal document status. | ||
* @throws {Error} If no legal document version is found for the type and service. | ||
*/ | ||
const getLegalDocumentStatusByUserId = async ({ | ||
userId, | ||
service, | ||
type, | ||
userRepository, | ||
legalDocumentRepository, | ||
userAcceptanceRepository, | ||
featureToggles = config.featureToggles, | ||
}) => { | ||
LegalDocumentService.assert(service); | ||
LegalDocumentType.assert(type); | ||
|
||
const { isLegalDocumentsVersioningEnabled } = featureToggles; | ||
|
||
if (!isLegalDocumentsVersioningEnabled) { | ||
const user = await userRepository.findPixOrgaCgusByUserId(userId); | ||
return LegalDocumentStatus.buildForLegacyPixOrgaCgu(user); | ||
} | ||
|
||
const lastLegalDocument = await legalDocumentRepository.findLastVersionByTypeAndService({ service, type }); | ||
|
||
if (!lastLegalDocument) throw new LegalDocumentVersionNotFoundError(); | ||
|
||
const lastUserAcceptance = await userAcceptanceRepository.findLastForLegalDocument({ userId, service, type }); | ||
|
||
return LegalDocumentStatus.build(lastLegalDocument, lastUserAcceptance); | ||
}; | ||
|
||
export { getLegalDocumentStatusByUserId }; |
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
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
Oops, something went wrong.