Skip to content

Commit

Permalink
feat(api): use new legalDocumentApiRepository in team context
Browse files Browse the repository at this point in the history
  • Loading branch information
lego-technix committed Jan 4, 2025
1 parent a2f74df commit cf1409a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 30 deletions.
3 changes: 1 addition & 2 deletions api/src/team/domain/usecases/get-prescriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export const getPrescriber = async function ({
} else if (!_isCurrentOrganizationInMemberships(userOrgaSettings, memberships)) {
await userOrgaSettingsRepository.update(userId, firstOrganization.id);
}

return prescriberRepository.getPrescriber(userId);
return prescriberRepository.getPrescriber({ userId });
};

function _isCurrentOrganizationInMemberships(userOrgaSettings, memberships) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import { UserOrgaSettings } from '../../../shared/domain/models/UserOrgaSettings
import { Prescriber } from '../../domain/read-models/Prescriber.js';

/**
* @param {string} userId
* @param {Object} params
* @property {string} params.userId
* @param {any} params.legalDocumentApiRepository
* @return {Promise<Prescriber>}
*/
const getPrescriber = async function (userId) {
const user = await knex('users')
.select('id', 'firstName', 'lastName', 'pixOrgaTermsOfServiceAccepted', 'lang')
.where({ id: userId })
.first();
const getPrescriber = async function ({ userId, legalDocumentApiRepository }) {
const user = await knex('users').select('id', 'firstName', 'lastName', 'lang').where({ id: userId }).first();

if (!user) {
throw new UserNotFoundError(`User not found for ID ${userId}`);
}

const pixOrgaLegalDocumentStatus = await legalDocumentApiRepository.getPixOrgaTos({ userId });
user.pixOrgaTermsOfServiceAccepted = pixOrgaLegalDocumentStatus.status == 'accepted';

const memberships = await knex('memberships').where({ userId, disabledAt: null }).orderBy('id');

if (memberships.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as sharedMembershipRepository from '../../../../../src/shared/infrastructure/repositories/membership-repository.js';
import { usecases } from '../../../../../src/team/domain/usecases/index.js';
import { prescriberRepository } from '../../../../../src/team/infrastructure/repositories/prescriber-repository.js';
import { repositories } from '../../../../../src/team/infrastructure/repositories/index.js';
import { userOrgaSettingsRepository } from '../../../../../src/team/infrastructure/repositories/user-orga-settings-repository.js';
import { databaseBuilder, expect, knex } from '../../../../test-helper.js';

const getPrescriber = usecases.getPrescriber;
const prescriberRepository = repositories.prescriberRepository;

describe('Integration | Team | Domain | UseCases | get-prescriber', function () {
context('When prescriber does not have a userOrgaSettings', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import { ForbiddenAccess } from '../../../../../src/shared/domain/errors.js';
import { Membership } from '../../../../../src/shared/domain/models/Membership.js';
import { UserOrgaSettings } from '../../../../../src/shared/domain/models/UserOrgaSettings.js';
import { Prescriber } from '../../../../../src/team/domain/read-models/Prescriber.js';
import { prescriberRepository } from '../../../../../src/team/infrastructure/repositories/prescriber-repository.js';
import { repositories } from '../../../../../src/team/infrastructure/repositories/index.js';
import { legalDocumentApiRepository } from '../../../../../src/team/infrastructure/repositories/legal-document-api.repository.js';
import { catchErr, databaseBuilder, expect } from '../../../../test-helper.js';

const prescriberRepository = repositories.prescriberRepository;

describe('Integration | Team | Infrastructure | Repository | Prescriber', function () {
const userToInsert = {
firstName: 'estelle',
Expand Down Expand Up @@ -40,7 +43,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const error = await catchErr(prescriberRepository.getPrescriber)(userId);
const error = await catchErr(prescriberRepository.getPrescriber)({
userId: userId,
legalDocumentApiRepository,
});

// then
expect(error).to.be.an.instanceOf(ForbiddenAccess);
Expand Down Expand Up @@ -74,7 +80,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi

it('should return the found prescriber', async function () {
// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber).to.be.an.instanceOf(Prescriber);
Expand All @@ -92,7 +101,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
const nonExistentUserId = 678;

// when
const result = await catchErr(prescriberRepository.getPrescriber)(nonExistentUserId);
const result = await catchErr(prescriberRepository.getPrescriber)({
userId: nonExistentUserId,
legalDocumentApiRepository,
});

// then
expect(result).to.be.instanceOf(UserNotFoundError);
Expand All @@ -103,7 +115,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
expectedPrescriber.memberships = [membership];

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
const firstMembership = foundPrescriber.memberships[0];
Expand All @@ -126,7 +141,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.memberships[0].id).to.equal(3000000);
Expand All @@ -138,7 +156,7 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
expectedPrescriber.userOrgaSettings = userOrgaSettings;

// when
const foundUser = await prescriberRepository.getPrescriber(user.id);
const foundUser = await prescriberRepository.getPrescriber({ userId: user.id, legalDocumentApiRepository });

// then
expect(foundUser.userOrgaSettings).to.be.an.instanceOf(UserOrgaSettings);
Expand All @@ -153,7 +171,7 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
expectedPrescriber.userOrgaSettings = userOrgaSettings;
const expectedOrganization = new Organization(organization);
// when
const foundUser = await prescriberRepository.getPrescriber(user.id);
const foundUser = await prescriberRepository.getPrescriber({ userId: user.id, legalDocumentApiRepository });

// then
expect(foundUser.userOrgaSettings.currentOrganization).to.be.an.instanceOf(Organization);
Expand All @@ -171,7 +189,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.userOrgaSettings.currentOrganization.tags.map((tag) => tag.name)).to.have.members([
Expand All @@ -192,7 +213,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
const schoolMembership = foundPrescriber.memberships.find(
Expand All @@ -210,7 +234,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.userOrgaSettings.currentOrganization.sessionExpirationDate).to.deep.equal(
Expand Down Expand Up @@ -241,7 +268,7 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(userId);
const foundPrescriber = await prescriberRepository.getPrescriber({ userId, legalDocumentApiRepository });

// then
expect(foundPrescriber.areNewYearOrganizationLearnersImported).to.be.true;
Expand Down Expand Up @@ -269,7 +296,7 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(userId);
const foundPrescriber = await prescriberRepository.getPrescriber({ userId, legalDocumentApiRepository });

// then
expect(foundPrescriber.areNewYearOrganizationLearnersImported).to.be.true;
Expand Down Expand Up @@ -299,7 +326,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(userId);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.areNewYearOrganizationLearnersImported).to.be.false;
Expand All @@ -318,7 +348,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.participantCount).to.equal(1);
Expand All @@ -334,7 +367,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.participantCount).to.equal(0);
Expand All @@ -349,7 +385,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.participantCount).to.equal(0);
Expand All @@ -366,7 +405,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.participantCount).to.equal(2);
Expand All @@ -393,7 +435,10 @@ describe('Integration | Team | Infrastructure | Repository | Prescriber', functi
await databaseBuilder.commit();

// when
const foundPrescriber = await prescriberRepository.getPrescriber(user.id);
const foundPrescriber = await prescriberRepository.getPrescriber({
userId: user.id,
legalDocumentApiRepository,
});

// then
expect(foundPrescriber.features).to.deep.equal({
Expand Down
4 changes: 2 additions & 2 deletions api/tests/team/unit/domain/usecases/get-prescriber_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('Unit | Team | Domain | UseCase | get-prescriber', function () {
const membership = domainBuilder.buildMembership({ user });
sharedMembershipRepository.findByUserId.withArgs({ userId }).resolves([membership]);
userOrgaSettingsRepository.findOneByUserId.withArgs(userId).resolves(null);
prescriberRepository.getPrescriber.withArgs(userId).resolves(expectedResult);
prescriberRepository.getPrescriber.withArgs({ userId }).resolves(expectedResult);

// when
const result = await getPrescriber({
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('Unit | Team | Domain | UseCase | get-prescriber', function () {
user: membership.user,
});
userOrgaSettingsRepository.findOneByUserId.withArgs(userId).resolves(userOrgaSettings);
prescriberRepository.getPrescriber.withArgs(userId).resolves(expectedResult);
prescriberRepository.getPrescriber.withArgs({ userId }).resolves(expectedResult);

// when
const result = await getPrescriber({
Expand Down

0 comments on commit cf1409a

Please sign in to comment.