|
| 1 | +export const STATUS = { |
| 2 | + ACCEPTED: 'accepted', |
| 3 | + REQUESTED: 'requested', |
| 4 | + UPDATE_REQUESTED: 'update-requested', |
| 5 | +}; |
| 6 | + |
| 7 | +export class LegalDocumentStatus { |
| 8 | + constructor({ status, acceptedAt, documentPath }) { |
| 9 | + this.status = status; |
| 10 | + this.acceptedAt = acceptedAt; |
| 11 | + this.documentPath = documentPath; |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Builds a LegalDocumentStatus based on legacy PixOrga CGU. |
| 16 | + * |
| 17 | + * @param {Object} userPixOrgaCgu - The user object. |
| 18 | + * @param {boolean} userPixOrgaCgu.pixOrgaTermsOfServiceAccepted - Indicates if the PixOrga terms of service are accepted. |
| 19 | + * @param {Date} userPixOrgaCgu.lastPixOrgaTermsOfServiceValidatedAt - The date when the PixOrga terms of service were last validated. |
| 20 | + * @returns {LegalDocumentStatus} The legal document status. |
| 21 | + */ |
| 22 | + static buildForLegacyPixOrgaCgu(userPixOrgaCgu) { |
| 23 | + const LEGACY_PIXORGA_TOS_PATH = 'pix-orga-tos-2024-01-02'; |
| 24 | + const { pixOrgaTermsOfServiceAccepted, lastPixOrgaTermsOfServiceValidatedAt } = userPixOrgaCgu; |
| 25 | + |
| 26 | + return new LegalDocumentStatus({ |
| 27 | + status: pixOrgaTermsOfServiceAccepted ? STATUS.ACCEPTED : STATUS.REQUESTED, |
| 28 | + acceptedAt: lastPixOrgaTermsOfServiceValidatedAt, |
| 29 | + documentPath: LEGACY_PIXORGA_TOS_PATH, |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Builds a LegalDocumentStatus based on the last document version and user acceptance. |
| 35 | + * |
| 36 | + * @param {Object} lastDocumentVersion - The last document version object. |
| 37 | + * @param {string} lastDocumentVersion.id - The ID of the last document version. |
| 38 | + * @param {Object} lastUserAcceptance - The last user acceptance object. |
| 39 | + * @param {string} lastUserAcceptance.legalDocumentVersionId - The ID of the accepted legal document version. |
| 40 | + * @param {Date} lastUserAcceptance.acceptedAt - The date when the document was accepted. |
| 41 | + * @returns {LegalDocumentStatus} The legal document status. |
| 42 | + */ |
| 43 | + static build(lastDocumentVersion, lastUserAcceptance) { |
| 44 | + const documentPath = lastDocumentVersion.buildDocumentPath(); |
| 45 | + |
| 46 | + if (!lastUserAcceptance) { |
| 47 | + return new LegalDocumentStatus({ status: STATUS.REQUESTED, acceptedAt: null, documentPath }); |
| 48 | + } |
| 49 | + |
| 50 | + const { legalDocumentVersionId, acceptedAt } = lastUserAcceptance; |
| 51 | + if (lastDocumentVersion.id === legalDocumentVersionId) { |
| 52 | + return new LegalDocumentStatus({ status: STATUS.ACCEPTED, acceptedAt, documentPath }); |
| 53 | + } |
| 54 | + |
| 55 | + return new LegalDocumentStatus({ status: STATUS.UPDATE_REQUESTED, acceptedAt: null, documentPath }); |
| 56 | + } |
| 57 | +} |
0 commit comments