Skip to content

Commit

Permalink
♻️ refactor: move use-orga-settings routes & co to src
Browse files Browse the repository at this point in the history
  • Loading branch information
yaf committed Dec 5, 2024
1 parent e65a74f commit 22dc225
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 214 deletions.
51 changes: 0 additions & 51 deletions api/lib/application/user-orga-settings/index.js

This file was deleted.

2 changes: 0 additions & 2 deletions api/lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import * as scoOrganizationLearners from './application/sco-organization-learner
import * as sessions from './application/sessions/index.js';
import * as tags from './application/tags/index.js';
import * as targetProfiles from './application/target-profiles/index.js';
import * as userOrgaSettings from './application/user-orga-settings/index.js';
import * as users from './application/users/index.js';

const routes = [
Expand All @@ -34,7 +33,6 @@ const routes = [
tags,
targetProfiles,
frameworks,
userOrgaSettings,
users,
];

Expand Down
245 changes: 115 additions & 130 deletions api/src/shared/application/error-manager.js

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions api/src/shared/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,6 @@ class UserOrgaSettingsCreationError extends DomainError {
}
}

class UserNotMemberOfOrganizationError extends DomainError {
constructor(message = "L'utilisateur n'est pas membre de l'organisation.") {
super(message);
}
}

class FileValidationError extends DomainError {
constructor(code, meta) {
super('An error occurred, file is invalid');
Expand Down Expand Up @@ -1198,7 +1192,6 @@ export {
UserNotAuthorizedToUpdatePasswordError,
UserNotAuthorizedToUpdateResourceError,
UserNotFoundError,
UserNotMemberOfOrganizationError,
UserOrgaSettingsCreationError,
UserShouldNotBeReconciledOnAnotherAccountError,
WrongDateFormatError,
Expand Down
2 changes: 2 additions & 0 deletions api/src/team/application/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { membershipRoutes } from './membership/membership.route.js';
import { organizationInvitationAdminRoutes } from './organization-invitations/organization-invitation.admin.route.js';
import { organizationInvitationRoutes } from './organization-invitations/organization-invitation.route.js';
import { prescriberInformationsRoute } from './prescriber-informations.route.js';
import { userOrgaSettingsRoute } from './user-orga-settings.route.js';

const register = async function (server) {
server.route([
Expand All @@ -16,6 +17,7 @@ const register = async function (server) {
...certificationCenterMembershipRoute,
...membershipAdminRoutes,
...membershipRoutes,
...userOrgaSettingsRoute,
...prescriberInformationsRoute,
...organizationInvitationRoutes,
...organizationInvitationAdminRoutes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { usecases } from '../../domain/usecases/index.js';
import * as userOrgaSettingsSerializer from '../../infrastructure/serializers/jsonapi/user-orga-settings-serializer.js';
import { usecases } from '../domain/usecases/index.js';
import * as userOrgaSettingsSerializer from '../infrastructure/serializers/jsonapi/user-orga-settings.serializer.js';

const createOrUpdate = async function (request, h, dependencies = { userOrgaSettingsSerializer }) {
const userId = request.params.id;
Expand Down
46 changes: 46 additions & 0 deletions api/src/team/application/user-orga-settings.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Joi from 'joi';

import { securityPreHandlers } from '../../../src/shared/application/security-pre-handlers.js';
import { identifiersType } from '../../../src/shared/domain/types/identifiers-type.js';
import { userOrgaSettingsController } from './user-orga-settings.controller.js';

export const userOrgaSettingsRoute = [
{
method: 'PUT',
path: '/api/user-orga-settings/{id}',
config: {
pre: [
{
method: securityPreHandlers.checkRequestedUserIsAuthenticatedUser,
assign: 'requestedUserIsAuthenticatedUser',
},
],
validate: {
options: {
allowUnknown: true,
},
params: Joi.object({
id: identifiersType.userId,
}),
payload: Joi.object({
data: {
relationships: {
organization: {
data: {
id: identifiersType.organizationId,
},
},
},
},
}),
},
handler: (request, h) => userOrgaSettingsController.createOrUpdate(request, h),
notes: [
'- **Cette route est restreinte aux utilisateurs authentifiés**\n' +
'- Création ou Mise à jour des paramètres utilisateurs liés à Pix Orga, permet notamment d’enregistrer les préférences d’un prescripteur vis à vis de son espace Orga.\n' +
'- L’id en paramètre doit correspondre à celui de l’utilisateur authentifié',
],
tags: ['api', 'user-orga-settings'],
},
},
];
7 changes: 7 additions & 0 deletions api/src/team/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class UncancellableOrganizationInvitationError extends DomainError {
}
}

class UserNotMemberOfOrganizationError extends DomainError {
constructor(message = "L'utilisateur n'est pas membre de l'organisation.") {
super(message);
}
}

class MembershipNotFound extends DomainError {}

class OrganizationArchivedError extends DomainError {
Expand All @@ -45,4 +51,5 @@ export {
OrganizationArchivedError,
UncancellableCertificationCenterInvitationError,
UncancellableOrganizationInvitationError,
UserNotMemberOfOrganizationError,
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';

import { UserNotMemberOfOrganizationError } from '../../../src/shared/domain/errors.js';
import { UserNotMemberOfOrganizationError } from '../errors.js';

const createOrUpdateUserOrgaSettings = async function ({
userId,
Expand Down
2 changes: 1 addition & 1 deletion api/src/team/domain/usecases/get-prescriber.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';

import { UserNotMemberOfOrganizationError } from '../../../shared/domain/errors.js';
import { UserNotMemberOfOrganizationError } from '../errors.js';

/**
* @param {{
Expand Down
9 changes: 5 additions & 4 deletions api/tests/integration/application/error-manager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
} from '../../../src/identity-access-management/domain/errors.js';
import { CampaignParticipationDeletedError } from '../../../src/prescription/campaign-participation/domain/errors.js';
import * as DomainErrors from '../../../src/shared/domain/errors.js';
import { AlreadyAcceptedOrCancelledInvitationError } from '../../../src/team/domain/errors.js';
import {
AlreadyAcceptedOrCancelledInvitationError,
UserNotMemberOfOrganizationError,
} from '../../../src/team/domain/errors.js';
import { expect, HttpTestServer, sinon } from '../../test-helper.js';

describe('Integration | API | Controller Error', function () {
Expand Down Expand Up @@ -491,9 +494,7 @@ describe('Integration | API | Controller Error', function () {
});

it('responds Unprocessable Entity when a UserNotMemberOfOrganizationError error occurs', async function () {
routeHandler.throws(
new DomainErrors.UserNotMemberOfOrganizationError("L'utilisateur n'est pas membre de l'organisation."),
);
routeHandler.throws(new UserNotMemberOfOrganizationError("L'utilisateur n'est pas membre de l'organisation."));
const response = await server.requestObject(request);

expect(response.statusCode).to.equal(UNPROCESSABLE_ENTITY_ERROR);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { userOrgaSettingsController } from '../../../../lib/application/user-orga-settings/user-orga-settings-controller.js';
import { usecases } from '../../../../lib/domain/usecases/index.js';
import { userOrgaSettingsController } from '../../../../src/team/application/user-orga-settings.controller.js';
import { usecases } from '../../../../src/team/domain/usecases/index.js';
import { expect, hFake, sinon } from '../../../test-helper.js';

describe('Unit | Controller | user-orga-settings-controller', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as moduleUnderTest from '../../../../lib/application/user-orga-settings/index.js';
import { userOrgaSettingsController } from '../../../../lib/application/user-orga-settings/user-orga-settings-controller.js';
import { securityPreHandlers } from '../../../../src/shared/application/security-pre-handlers.js';
import { teamRoutes } from '../../../../src/team/application/routes.js';
import { userOrgaSettingsController } from '../../../../src/team/application/user-orga-settings.controller.js';
import { expect, HttpTestServer, sinon } from '../../../test-helper.js';

describe('Unit | Router | user-orga-settings-router', function () {
Expand All @@ -9,9 +10,12 @@ describe('Unit | Router | user-orga-settings-router', function () {

it('should exist', async function () {
// given
sinon
.stub(securityPreHandlers, 'checkRequestedUserIsAuthenticatedUser')
.callsFake((request, h) => h.response(true));
sinon.stub(userOrgaSettingsController, 'createOrUpdate').returns('ok');
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);
await httpTestServer.register(teamRoutes);

const method = 'PUT';
const url = `/api/user-orga-settings/${userId}`;
Expand Down Expand Up @@ -39,7 +43,7 @@ describe('Unit | Router | user-orga-settings-router', function () {
it('returns a 403 HTTP status code', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);
await httpTestServer.register(teamRoutes);

const method = 'PUT';
const url = `/api/user-orga-settings/99`;
Expand Down Expand Up @@ -68,7 +72,7 @@ describe('Unit | Router | user-orga-settings-router', function () {
it('should be mandatory', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);
await httpTestServer.register(teamRoutes);

const method = 'PUT';
const url = `/api/user-orga-settings/${userId}`;
Expand All @@ -84,7 +88,7 @@ describe('Unit | Router | user-orga-settings-router', function () {
it('should contain relationships.organization.data.id', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);
await httpTestServer.register(teamRoutes);

const method = 'PUT';
const url = `/api/user-orga-settings/${userId}`;
Expand All @@ -111,7 +115,7 @@ describe('Unit | Router | user-orga-settings-router', function () {
it('should contain relationships.organization.data.id as number', async function () {
// given
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);
await httpTestServer.register(teamRoutes);

const method = 'PUT';
const url = `/api/user-orga-settings/${userId}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createOrUpdateUserOrgaSettings } from '../../../../lib/domain/usecases/create-or-update-user-orga-settings.js';
import { UserNotMemberOfOrganizationError } from '../../../../src/shared/domain/errors.js';
import { catchErr, expect, sinon } from '../../../test-helper.js';
import { UserNotMemberOfOrganizationError } from '../../../../../src/team/domain/errors.js';
import { createOrUpdateUserOrgaSettings } from '../../../../../src/team/domain/usecases/create-or-update-user-orga-settings.usecase.js';
import { catchErr, expect, sinon } from '../../../../test-helper.js';

describe('Unit | UseCase | create-or-update-user-orga-settings', function () {
const userId = 1;
Expand Down
2 changes: 1 addition & 1 deletion api/tests/team/unit/domain/usecases/get-prescriber_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserNotMemberOfOrganizationError } from '../../../../../src/shared/domain/errors.js';
import { UserNotMemberOfOrganizationError } from '../../../../../src/team/domain/errors.js';
import { getPrescriber } from '../../../../../src/team/domain/usecases/get-prescriber.js';
import { catchErr, domainBuilder, expect, sinon } from '../../../../test-helper.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as serializer from '../../../../../lib/infrastructure/serializers/jsonapi/user-orga-settings-serializer.js';
import { UserOrgaSettings } from '../../../../../src/shared/domain/models/UserOrgaSettings.js';
import { domainBuilder, expect } from '../../../../test-helper.js';
import { UserOrgaSettings } from '../../../../../../src/shared/domain/models/UserOrgaSettings.js';
import * as serializer from '../../../../../../src/team/infrastructure/serializers/jsonapi/user-orga-settings.serializer.js';
import { domainBuilder, expect } from '../../../../../test-helper.js';

describe('Unit | Serializer | JSONAPI | user-orga-settings-serializer', function () {
describe('#serialize', function () {
Expand Down

0 comments on commit 22dc225

Please sign in to comment.