-
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 des règles métier pour canSelfDeleteAccount (PIX-15333)
- Loading branch information
Showing
20 changed files
with
523 additions
and
21 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
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
16 changes: 16 additions & 0 deletions
16
api/src/privacy/infrastructure/repositories/candidates-api.repository.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,16 @@ | ||
import * as candidatesApi from '../../../certification/enrolment/application/api/candidates-api.js'; | ||
|
||
/** | ||
* Checks if the user has been a candidate. | ||
* | ||
* @param {Object} params - The parameters. | ||
* @param {string} params.userId - The ID of the user. | ||
* @param {Object} [params.dependencies] - The dependencies. | ||
* @param {Object} [params.dependencies.candidatesApi] - The candidates API. | ||
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the user has been a candidate. | ||
*/ | ||
const hasBeenCandidate = async ({ userId, dependencies = { candidatesApi } }) => { | ||
return dependencies.candidatesApi.hasBeenCandidate({ userId }); | ||
}; | ||
|
||
export { hasBeenCandidate }; |
16 changes: 16 additions & 0 deletions
16
api/src/privacy/infrastructure/repositories/learners-api.repository.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,16 @@ | ||
import * as learnersApi from '../../../prescription/learner-management/application/api/learners-api.js'; | ||
|
||
/** | ||
* Checks if the user has been a learner. | ||
* | ||
* @param {Object} params - The parameters. | ||
* @param {string} params.userId - The ID of the user. | ||
* @param {Object} [params.dependencies] - The dependencies. | ||
* @param {Object} [params.dependencies.learnersApi] - The learners API. | ||
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the user has been a learner. | ||
*/ | ||
const hasBeenLearner = async ({ userId, dependencies = { learnersApi } }) => { | ||
return dependencies.learnersApi.hasBeenLearner({ userId }); | ||
}; | ||
|
||
export { hasBeenLearner }; |
16 changes: 16 additions & 0 deletions
16
api/src/privacy/infrastructure/repositories/user-teams-api.repository.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,16 @@ | ||
import * as userTeamsApi from '../../../team/application/api/user-teams.js'; | ||
|
||
/** | ||
* Get user teams info. | ||
* | ||
* @param {Object} params - The parameters. | ||
* @param {string} params.userId - The ID of the user. | ||
* @param {Object} [params.dependencies] - The dependencies. | ||
* @param {Object} [params.dependencies.userTeamsApi] - The user teams API. | ||
* @returns {Promise<UserTeamsInfo>} The user teams info. | ||
*/ | ||
const getUserTeamsInfo = async ({ userId, dependencies = { userTeamsApi } }) => { | ||
return dependencies.userTeamsApi.getUserTeamsInfo(userId); | ||
}; | ||
|
||
export { getUserTeamsInfo }; |
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,11 @@ | ||
/** | ||
* @class | ||
* @classdesc Model representing a user teams info. | ||
*/ | ||
export class UserTeamsInfo { | ||
constructor({ isPixAgent, isOrganizationMember, isCertificationCenterMember } = {}) { | ||
this.isPixAgent = isPixAgent; | ||
this.isOrganizationMember = isOrganizationMember; | ||
this.isCertificationCenterMember = isCertificationCenterMember; | ||
} | ||
} |
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,22 @@ | ||
import { usecases } from '../../domain/usecases/index.js'; | ||
import { UserTeamsInfo } from './models/user-teams-info.js'; | ||
|
||
/** | ||
* @module UserTeamsApi | ||
*/ | ||
|
||
/** | ||
* Retrieves information user's teams. | ||
* | ||
* @param {string} userId - The ID of the user. | ||
* @returns {Promise<UserTeamsInfo>} A promise that resolves to an instance of UserTeamsInfo. | ||
* @throws {TypeError} preconditions failed | ||
*/ | ||
export async function getUserTeamsInfo(userId) { | ||
if (!userId) { | ||
throw new TypeError('userId is required'); | ||
} | ||
|
||
const userTeamsInfo = await usecases.getUserTeamsInfo({ userId }); | ||
return new UserTeamsInfo(userTeamsInfo); | ||
} |
26 changes: 26 additions & 0 deletions
26
api/src/team/domain/usecases/get-user-teams-info.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,26 @@ | ||
/** | ||
* Get the user's team information. | ||
* | ||
* @param {Object} params - The parameters. | ||
* @param {string} params.userId - The ID of the user. | ||
* @param {Object} params.adminMemberRepository - The repository for Pix Admin membership. | ||
* @param {Object} params.certificationCenterMembershipRepository - The repository for certification center memberships. | ||
* @param {Object} params.membershipRepository - The repository for organization memberships. | ||
* @returns {Promise<Object>} The user's team information. | ||
*/ | ||
export const getUserTeamsInfo = async function ({ | ||
userId, | ||
adminMemberRepository, | ||
certificationCenterMembershipRepository, | ||
membershipRepository, | ||
}) { | ||
const pixAdminMembership = await adminMemberRepository.get({ userId }); | ||
const organizationMembershipsCount = await membershipRepository.countByUserId(userId); | ||
const certificationCenterMembershipsCount = await certificationCenterMembershipRepository.countByUserId(userId); | ||
|
||
return { | ||
isPixAgent: pixAdminMembership?.hasAccessToAdminScope ?? false, | ||
isOrganizationMember: organizationMembershipsCount > 0, | ||
isCertificationCenterMember: certificationCenterMembershipsCount > 0, | ||
}; | ||
}; |
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
130 changes: 130 additions & 0 deletions
130
api/tests/privacy/unit/domain/usecases/can-self-delete-account.usecase.test.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,130 @@ | ||
import { usecases } from '../../../../../src/privacy/domain/usecases/index.js'; | ||
import { expect, sinon } from '../../../../test-helper.js'; | ||
|
||
describe('Unit | Privacy | Domain | UseCase | can-self-delete-account', function () { | ||
const userId = '123'; | ||
let dependencies; | ||
|
||
beforeEach(function () { | ||
dependencies = { | ||
featureToggles: { isSelfAccountDeletionEnabled: false }, | ||
learnersApiRepository: { hasBeenLearner: sinon.stub().resolves(false) }, | ||
candidatesApiRepository: { hasBeenCandidate: sinon.stub().resolves(false) }, | ||
userTeamsApiRepository: { | ||
getUserTeamsInfo: sinon.stub().resolves({ | ||
isPixAgent: false, | ||
isOrganizationMember: false, | ||
isCertificationCenterMember: false, | ||
}), | ||
}, | ||
}; | ||
}); | ||
|
||
context('When feature flag is enabled', function () { | ||
beforeEach(function () { | ||
sinon.stub(dependencies.featureToggles, 'isSelfAccountDeletionEnabled').value(true); | ||
}); | ||
|
||
context('When user is eligible', function () { | ||
it('returns true', async function () { | ||
// when | ||
const result = await usecases.canSelfDeleteAccount({ userId, ...dependencies }); | ||
|
||
// then | ||
expect(result).to.be.true; | ||
}); | ||
}); | ||
|
||
context('When user has been a learner', function () { | ||
it('returns false', async function () { | ||
// given | ||
dependencies.learnersApiRepository.hasBeenLearner.withArgs({ userId }).resolves(true); | ||
|
||
// when | ||
const result = await usecases.canSelfDeleteAccount({ userId, ...dependencies }); | ||
|
||
// then | ||
expect(result).to.be.false; | ||
}); | ||
}); | ||
|
||
context('User has been a candidate to certification', function () { | ||
it('returns false', async function () { | ||
// given | ||
dependencies.candidatesApiRepository.hasBeenCandidate.withArgs({ userId }).resolves(true); | ||
|
||
// when | ||
const result = await usecases.canSelfDeleteAccount({ userId, ...dependencies }); | ||
|
||
// then | ||
expect(result).to.be.false; | ||
}); | ||
}); | ||
|
||
context('User if user is a Pix agent', function () { | ||
it('returns false', async function () { | ||
// given | ||
dependencies.userTeamsApiRepository.getUserTeamsInfo.withArgs({ userId }).resolves({ | ||
isPixAgent: true, | ||
isOrganizationMember: false, | ||
isCertificationCenterMember: false, | ||
}); | ||
|
||
// when | ||
const result = await usecases.canSelfDeleteAccount({ userId, ...dependencies }); | ||
|
||
// then | ||
expect(result).to.be.false; | ||
}); | ||
}); | ||
|
||
context('User if user is member of an organization', function () { | ||
it('returns false', async function () { | ||
// given | ||
dependencies.userTeamsApiRepository.getUserTeamsInfo.withArgs({ userId }).resolves({ | ||
isPixAgent: false, | ||
isOrganizationMember: true, | ||
isCertificationCenterMember: false, | ||
}); | ||
|
||
// when | ||
const result = await usecases.canSelfDeleteAccount({ userId, ...dependencies }); | ||
|
||
// then | ||
expect(result).to.be.false; | ||
}); | ||
}); | ||
|
||
context('User if user is member of a certification center', function () { | ||
it('returns false', async function () { | ||
// given | ||
dependencies.userTeamsApiRepository.getUserTeamsInfo.withArgs({ userId }).resolves({ | ||
isPixAgent: false, | ||
isOrganizationMember: false, | ||
isCertificationCenterMember: true, | ||
}); | ||
|
||
// when | ||
const result = await usecases.canSelfDeleteAccount({ userId, ...dependencies }); | ||
|
||
// then | ||
expect(result).to.be.false; | ||
}); | ||
}); | ||
}); | ||
|
||
context('Feature flag is disabled', function () { | ||
context('When user is eligible', function () { | ||
it('returns false', async function () { | ||
// given | ||
sinon.stub(dependencies.featureToggles, 'isSelfAccountDeletionEnabled').value(false); | ||
|
||
// when | ||
const result = await usecases.canSelfDeleteAccount({ userId: '123', ...dependencies }); | ||
|
||
// then | ||
expect(result).to.be.false; | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.