Skip to content

[CLEANUP] Utilisation d'un read-model dans le service Current-user de Pix Orga (PIX-553).#1448

Merged
VincentHardouin merged 16 commits intodevfrom
pix-553-refacto-current-user-orga
May 28, 2020
Merged

[CLEANUP] Utilisation d'un read-model dans le service Current-user de Pix Orga (PIX-553).#1448
VincentHardouin merged 16 commits intodevfrom
pix-553-refacto-current-user-orga

Conversation

@chrc
Copy link
Copy Markdown
Contributor

@chrc chrc commented May 25, 2020

🦄 Problème

Depuis le front, dans le service current-user de Pix Orga, trois requêtes distinctes sont réalisées pour récupérer l'utilisateur connecté (tous ses attributs), ses memberships ainsi que ses paramètres (userOrgaSettings).
S'enchainent des opérations difficilement lisibles, qui pourraient être effectuées par l'API.

Seuls les attributs du user firstName, lastName et pixOrgaTermsOfServiceAccepted sont utilisés.
La récupération des memberships et de userOrgaSettings s'effectue en deux appels vers l'API (cf. api/lib/infrastructure/serializers/jsonapi/user-serializer.js).

🤖 Solution

  • Introduction d'un nouveau read-model Prescriber
class Prescriber {
  constructor({
    id,
    // attributes
    firstName,
    lastName,
    pixOrgaTermsOfServiceAccepted,
    // includes
    memberships = [],
    userOrgaSettings,
  } = {}) {...}
}
  • Nouveau endpoint api
{
  method: 'GET',
  path: '/api/prescription/prescribers/{userId}',
  config: {
    validate: {
      params: Joi.object({
        userId: Joi.number().required(),
      }),
    },
    pre: [{
      method: securityPreHandlers.checkRequestedUserIsAuthenticatedUser,
      assign: 'requestedUserIsAuthenticatedUser'
    }],
    handler: prescriberController.get,
    notes: [
      '- **Cette route est restreinte aux utilisateurs authentifiés**\n' +
      '- Récupération d’un prescripteur.\n' +
      '- L’id demandé doit correspondre à celui de l’utilisateur authentifié',
    ],
    tags: ['api', 'user', 'prescription'],
  }
}
  • Modification des tests e2e pour prendre en compte ce endpoint

🌈 Remarques

  • Ceci est un premier refacto
  • Dans un deuxième temps, les opérations seront effectuées par l'API

💯 Pour tester

⚠️ Ce connecter à Pix Orga et faire une recette globale des fonctionnalités.

@pix-service
Copy link
Copy Markdown
Contributor

@VincentHardouin VincentHardouin force-pushed the pix-553-refacto-current-user-orga branch 2 times, most recently from 7586fb6 to 1d0a39b Compare May 25, 2020 08:29
Comment thread api/tests/tooling/domain-builder/factory/build-prescriber.js Outdated
Comment thread api/lib/infrastructure/repositories/user-repository.js Outdated
@chrc chrc force-pushed the pix-553-refacto-current-user-orga branch from 1d0a39b to ae5d1fd Compare May 25, 2020 11:49
@VincentHardouin VincentHardouin force-pushed the pix-553-refacto-current-user-orga branch from ae5d1fd to 8adb4d8 Compare May 26, 2020 12:57
Comment thread orga/app/adapters/prescriber.js Outdated
Comment thread api/lib/infrastructure/repositories/prescriber-repository.js Outdated
Comment thread api/lib/infrastructure/repositories/prescriber-repository.js Outdated
Comment thread api/lib/infrastructure/serializers/jsonapi/prescriber-serializer.js Outdated
Comment thread api/lib/infrastructure/serializers/jsonapi/prescriber-serializer.js Outdated
Comment thread api/lib/infrastructure/serializers/jsonapi/prescriber-serializer.js Outdated
Comment thread api/lib/infrastructure/serializers/jsonapi/prescriber-serializer.js Outdated
Comment thread api/tests/acceptance/application/prescriber-controller_test.js Outdated
Comment thread api/tests/unit/infrastructure/serializers/jsonapi/prescriber-serializer_test.js Outdated
Comment thread orga/tests/acceptance/student-list-test.js Outdated
@VincentHardouin VincentHardouin force-pushed the pix-553-refacto-current-user-orga branch 2 times, most recently from e0b0507 to a88716f Compare May 28, 2020 13:05
@VincentHardouin VincentHardouin force-pushed the pix-553-refacto-current-user-orga branch from a88716f to 35da0c8 Compare May 28, 2020 14:40
@VincentHardouin VincentHardouin merged commit 96638e3 into dev May 28, 2020
@VincentHardouin VincentHardouin deleted the pix-553-refacto-current-user-orga branch May 28, 2020 15:12
Comment on lines +1 to +3
module.exports = function getPrescriber({ userId, prescriberRepository }) {
return prescriberRepository.getPrescriber(userId);
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quand on utilise les read-model (dans une logique de flux descendant / query) il n'y a pas de logique "métier" à appliquer, simplement de la logique de récupération de données et de présentation de la données (répo & serializer) et le usecase devient obsolète car il ne fait que passe plat, comme ici.

Tu peux donc t'en passer et appeler le repo directement depuis le controller, comme ce qui est fait ici.

C'est d'ailleurs assez cohérent avec la logique de séparation du read et du write, dans cette logique, seul le write est considérée comme étant du "métier" alors que read est un détail d'implémentation. Le usecase étant un classe d'orchestration de workflow "métier" il est normal qu'on ne l'utilise pas sur le write.

Par ailleurs, même si ce n'est pas ce qu'on a fait jusqu'à maintenant : les read-models ne devraient pas se trouver dans domain, ils font partie de l'infrastructure.

Voir ici : #649 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette PR etant mergé avant tes retours, on le prend en compte dans la prochaine PR de refacto

const UserOrgaSettings = require('../../domain/models/UserOrgaSettings');
const Organization = require('../../domain/models/Organization');

function _toPrescriberDomain(bookshelfUser) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le prescriber étant un read-model ne devrait pas faire partie du domain.
_toPrescriberDomain -> _toPrescriberReadModel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem

Comment on lines +15 to +16
memberships: _toMembershipsDomain(bookshelfUser.related('memberships')),
userOrgaSettings: _toUserOrgaSettingsDomain(bookshelfUser.related('userOrgaSettings'))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Membership et Organization semblent ne pas être des read-model.
Il faut soit :

  • qu'ils soient des read-models (s'ils ne servent pas dans le domain par ailleurs)
  • faire des read-model correspondant

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A creuser dans le prochain point design code !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants